Sub topic 3
Programmed I/O and Polling
This chapter assumes that you know the basic CPU, register, memory, and bus model, including the instruction cycle. You should also be comfortable with binary and hexadecimal numbers and with subroutine calls and returns.
The basic idea
An I/O device, such as a keyboard, disk, or network adapter, usually operates at a different speed from the CPU. The CPU therefore communicates with the device through an I/O interface module. An I/O interface module is hardware that connects a peripheral to the system bus and presents a simple register-based view to the CPU.
Common interface registers include:
| Register | Purpose |
|---|---|
| Data register | Holds data being sent to or received from the device |
| Status register | Reports conditions such as ready, busy, or error |
| Control register | Stores commands and configuration bits |
In programmed I/O, the CPU directly controls every transfer. It executes instructions to check the device, move data, and decide when the next transfer can occur.
A device often sets a READY bit in its status register when it can provide or accept data. The CPU repeatedly reads that register until the required condition appears. This repeated checking is called polling.
The registers can use memory-mapped addresses or isolated I/O addresses. With memory-mapped I/O, ordinary load and store instructions access device registers. With isolated I/O, the processor uses special I/O instructions. The polling algorithm is similar in both cases.
A polling loop
Suppose a device has these memory-mapped registers:
STATUS_REGat address0x4000DATA_REGat address0x4001- Bit 0 of the status register means “data ready”
A C implementation can look like this:
#include <stdint.h>
/* Hypothetical memory-mapped device registers */
#define STATUS_REG (*(volatile uint8_t *)0x4000)
#define DATA_REG (*(volatile uint8_t *)0x4001)
#define READY_BIT 0x01
uint8_t read_byte(void) {
while ((STATUS_REG & READY_BIT) == 0) {
/* Busy-wait: the CPU repeatedly checks the device. */
}
return DATA_REG;
}The volatile qualifier is important. It tells the compiler that the register can change outside the current program, so every read must actually access the hardware. Without volatile, an optimizing compiler might read the status register once and reuse the old value forever.
The sequence is:
- Read the status register.
- Apply a bit mask to test the ready bit.
- Repeat while the device is not ready.
- Read the data register when the ready bit becomes one.
- Return the received byte.
The device may clear the ready bit when the CPU reads the data register. This behavior depends on the device specification.
Input and output polling
For input, the CPU waits until data is available:
while data_ready is false:
read status
read dataFor output, the CPU waits until the device can accept another value:
while output_ready is false:
read status
write dataThe status bit might mean READY, NOT_FULL, or the opposite condition, such as BUSY. Always check the definition of the bit. A common error is to reverse the condition and either read invalid data or wait forever.
Why polling wastes CPU time
During a busy-wait, the CPU executes instructions but performs no useful application work. If a device takes a long time to become ready, the processor can spend most of its time reading the same status register.
Polling is still useful in some situations:
- The device responds almost immediately.
- The hardware or operating system is very simple.
- The transfer is rare and low priority.
- Predictable timing is more important than CPU utilization.
For slow or frequent devices, another I/O technique can let the CPU do other work while the device operates. The choice depends on transfer rate, response time, hardware complexity, and CPU overhead.
GATE-style calculation
Assume a CPU runs at 500 MHz and one polling iteration takes 20 cycles. A device becomes ready after 100 microseconds. The number of polling iterations is:
100 microseconds × 500,000,000 cycles/second ÷ 20 cycles/iteration
= 2,500 iterationsThe CPU spends all 100 microseconds in the polling loop. Therefore, if it polls continuously while waiting, the CPU utilization for this wait is 100 percent.
For a stream of transfers, use the same method:
- Find the number of polling checks per second.
- Multiply by the cycles per check.
- Divide by the CPU frequency.
For example, if polling requires 50 cycles per check and occurs 1,000,000 times per second on a 100 MHz CPU:
CPU fraction = (1,000,000 × 50) / 100,000,000
= 0.5
= 50%Polling sequence
The central feature is that the CPU owns the entire sequence. It must initiate the check, perform the test, and execute the data transfer.
Exercise
A CPU runs at 200 MHz. Each polling iteration takes 25 cycles, and the device becomes ready after 40 microseconds. How many polling iterations occur, and what fraction of the CPU's time is spent waiting?
Show answer
The CPU executes 40 × 10^-6 × 200 × 10^6 = 8,000 cycles. Therefore, it performs 8,000 / 25 = 320 polling iterations. Because the CPU polls continuously for the whole 40 microseconds, 100% of its time is spent waiting during this input operation.