G27
Self-checkedTier 1 13 min readcoa.io-interface

I/O interface and programmed I/O

How a processor talks to a device: the interface registers, memory-mapped and isolated I/O, strobe and handshake transfer, serial framing, and programmed I/O by polling with its cost, compared with interrupt-driven I/O and DMA.

Why it exists

Devices are slow, many and different: a keyboard produces a few bytes a second, a disk millions. A processor cannot be wired to each of them directly, and it cannot wait for them. An I/O interface is the small circuit between the buses and a device. It gives the processor a uniform view (a few registers with addresses), hides the device's speed and format, and tells the processor when a transfer can happen. This page covers the registers and how they are addressed, how a single word crosses the interface, and the simplest way to run a transfer: the processor itself tests the device in a loop (programmed I/O). The better schemes, interrupts and DMA, are defined against it, and GATE asks you to compare the three by the fraction of CPU time they consume.

Prerequisites

None. This is a starting point.

No other concept page is needed, but the picture of fetching and executing instructions from the instruction cycle helps: programmed I/O is just a loop of ordinary instructions.

Precise definitions

Interface registers. A typical device interface offers, at fixed addresses:

  • a data register (data in, data out, or both),
  • a status register with bits such as READY (a word is available, or the device can accept one), BUSY and ERROR,
  • a control register that the processor writes to start an operation, choose a mode or enable interrupts.

Reading the data register normally also tells the device that the word has been taken, which clears READY. This is how the interface knows the transfer is complete.

Addressing the registers.

  • Memory-mapped I/O. The registers occupy addresses of the ordinary memory address space. Ordinary load, store, test and logic instructions (with all their addressing modes) operate on them, and no special instructions or control line are needed. The price: those addresses can no longer be used for memory, and the registers must never be cached or the processor would read an old status (memory interfacing shows the decoder that produces the device select).
  • Isolated (port-mapped, I/O-mapped) I/O. There is a separate I/O address space reached by special instructions (IN and OUT) and a control signal that says "this address is an I/O port, not memory". All memory addresses remain available; the port numbers are usually few (an 8-bit port number gives 256 ports), and the processor needs the extra instructions and signal.

Ways to run a transfer.

  • Programmed I/O (polling, busy waiting). The processor repeatedly reads the status register until READY is 1, then moves the word itself. The processor does nothing else while it waits.
  • Interrupt-driven I/O. The processor works on something else; the device raises an interrupt request when it is ready and the processor runs a service routine that moves the word (interrupts).
  • Direct memory access (DMA). A DMA controller moves a whole block between the device and memory by itself, and interrupts the processor once at the end (DMA).
  • I/O processor (channel). A separate small processor with its own instructions runs I/O programs, taking the whole job off the CPU; DMA is its simplest form.

Data transfer across the interface.

  • Synchronous: source and destination share a clock, and a word is transferred at a clock edge. Simple, but both sides must be able to keep up with the clock.
  • Asynchronous: no common clock, so control lines say when data is valid. With a strobe, one control line from the source (or from the destination) marks the moment the data is valid; the source cannot tell whether the destination really took it. With handshaking, two lines pass signals in both directions. In the four-phase source-initiated handshake: (1) the source puts data on the bus and raises data valid; (2) the destination takes the data and raises data accepted; (3) the source drops data valid; (4) the destination drops data accepted. Each side waits for the other, so the transfer works whatever the two speeds are.
  • Parallel transfer sends all the bits of a word at once on separate wires; serial transfer sends them one after another on one wire.
  • Asynchronous serial transfer (as in a UART) sends each character in a frame: an idle line is 1; a start bit 0 begins the frame; then the data bits (least significant first), an optional parity bit, and one or two stop bits of value 1. For a frame of ff bits at BB bits per second the character rate is B/fB/f and the fraction of the line carrying data is d/fd/f for dd data bits. (For simple two-level signalling the baud rate equals the bit rate; that is the assumption throughout this page.)

Cost of polling. If a device must be serviced rr times per second (once per unit of data it delivers), the processor must poll at least rr times per second. If one poll costs cc cycles and the clock rate is fclkf_{clk},

fraction of CPU time spent polling=r⋅cfclk.\text{fraction of CPU time spent polling} = \frac{r \cdot c}{f_{clk}}.

Polling costs this whether or not the device has anything to send (the figure assumes the device is serviced exactly at its rate). Interrupt-driven I/O costs time only when an event happens, but each event is more expensive.

How it works

The first animation runs a polling loop on a small machine whose device registers are memory-mapped (STATUS at address F0, DATA at F1). Watch the wasted passes: the CPU reads STATUS again and again while the device is not ready, and it moves each of the three words itself.

Programmed I/O: polling a device for 3 words: step 1

Machine run, not typed
start(CPU about to run the loop)
PC
0
R1
0
R2
-
PTR
32
CNT
3
polls
0
addrhexmeaning
000LD R1,[STATUS]PC
001JZ R1,0
002LD R2,[DATA]
003ST R2,[PTR]; PTR+=1
004DEC CNT
005JNZ CNT,0
006HLT
020.buffer
021.buffer
022.buffer
0F00device STATUS register (bit 0 = ready)
0F1-device DATA register

Step 1

The device registers are ordinary memory addresses (memory-mapped I/O): STATUS at F0 and DATA at F1 are read and written with ordinary load and store instructions. The program must read 3 words into the buffer at 20. The device is not ready yet. The loop reads STATUS, branches back while it is 0 (busy waiting or POLLING), then reads DATA. PTR is the buffer pointer and CNT the number of words still wanted.

1 / 30
computed by script: a simulation of the machine or protocol, one event per step. python3 scripts/gen-coa-io-traces.py

The second animation shows the four-phase handshake that makes an asynchronous transfer safe. Notice that the data is on the bus before valid rises and stays there until accepted has been seen.

A four-phase handshake moving two words: Idle

Simulated, not typed
data bus--D0--D1--data valid (source)data accepted (destination)t = 0

Dashed lines mark rising clock edges. Green is an output, blue a stored state, black an input, grey the clock. One tick = one tick (reaction time of each side).

Step 1

Both control lines are 0 and the bus carries nothing meaningful. A source-initiated handshake moves each word with four ordered events, so neither side needs to know the other's speed.

1 / 7
computed by script: a simulation of the machine or protocol, one event per step. python3 scripts/gen-coa-io-traces.py

The third animation puts the same job, three words arriving at ticks 6, 16 and 26, through the three schemes on one time axis. Count the ticks marked work: polling gets 8 of 36, interrupt-driven I/O 27, DMA 29. The numbers are a model (the costs per word are the ones stated in the steps), but the ordering and the reason for it are general.

Polling, interrupts and DMA moving 3 words: The same job three ways

Simulated, not typed
device readyCPU: pollingpollpollpollworkCPU: interruptsworkISRworkISRworkISRworkCPU: DMAworkworkworkworkbus owner (DMA)CPUCPUCPUCPUt = 0

Dashed lines mark rising clock edges. Green is an output, blue a stored state, black an input, grey the clock. One tick = one tick (CPU time unit).

Step 1

A device delivers 3 words, one at ticks 6, 16, 26. Each row shows what the CPU is doing in each tick: useful work (work), polling, reading a word, running an interrupt service routine (ISR), setting up a DMA transfer, or being stalled because the DMA controller owns the bus.

1 / 5
computed by script: a simulation of the machine or protocol, one event per step. python3 scripts/gen-coa-io-traces.py

Worked examples

EX 1What polling costs for three devices

Easy

Every poll costs 400 cycles and the CPU runs at 500 MHz. A mouse is polled 30 times a second. A floppy-disk-like device delivers 50 KB/s in 2-byte units. A disk delivers 4 MB/s in 16-byte units.

#include <stdio.h>
 
static void row(const char *name, double bytes_per_s, double unit_bytes, double cycles, double clock) {
    double polls = bytes_per_s / unit_bytes;
    printf("%-7s %8.0f polls/s -> %8.4f%% of the CPU\n", name, polls, 100.0 * polls * cycles / clock);
}
 
int main(void) {
    /* every poll costs 400 cycles; the CPU runs at 500 MHz */
    row("mouse", 30, 1, 400, 500e6);
    row("floppy", 50e3, 2, 400, 500e6);
    row("disk", 4e6, 16, 400, 500e6);
    return 0;
}
mouse         30 polls/s ->   0.0024% of the CPU
floppy     25000 polls/s ->   2.0000% of the CPU
disk      250000 polls/s ->  20.0000% of the CPU

Reading it. The number of polls per second is the data rate divided by the size of a unit: 4 000 000/16=250 0004\,000\,000/16 = 250\,000. Multiply by the cycles per poll and divide by the clock: 250 000×400/(500×106)=0.2250\,000 \times 400 / (500 \times 10^6) = 0.2. Polling is harmless for slow devices and a heavy tax (here a fifth of the machine) for a fast one.

EX 2The polling loop as a C program

Easy

The loop in the first animation, written in C. The device here is simulated by two functions; its ready bit appears on the 4th read of the status register.

#include <stdio.h>
 
/* a device whose ready bit shows up on the 4th read of its status register */
static int status_reads;
static unsigned read_status(void) { return ++status_reads >= 4; }
static unsigned read_data(void) { return 0x41; }
 
int main(void) {
    int wasted = 0;
    while (!read_status()) wasted++;      /* busy waiting: the polling loop */
    unsigned word = read_data();
    printf("status read %d times, %d passes wasted, data = 0x%02X\n", status_reads, wasted, word);
    return 0;
}
status read 4 times, 3 passes wasted, data = 0x41

Reading it. Three reads found the device not ready and were wasted; the fourth found it ready. In a real driver the status register is a memory-mapped location, so a C pointer to it must be declared volatile. Otherwise the compiler may read it once and loop forever on the stale value.

EX 3Serial framing: how fast is a UART line?

Medium

A line runs at 9600 bits per second. Compare three frame formats: 8 data bits with no parity and 1 stop bit (8N1); 8 data bits with parity and 2 stop bits (8E2); 7 data bits with parity and 1 stop bit (7E1).

#include <stdio.h>
 
static void line(const char *name, int baud, int start, int data, int parity, int stop) {
    int frame = start + data + parity + stop;
    printf("%-4s frame of %2d bits: %6.1f characters/s, %5.1f%% of the bits are data\n", name, frame, (double)baud / frame, 100.0 * data / frame);
}
 
int main(void) {
    line("8N1", 9600, 1, 8, 0, 1);
    line("8E2", 9600, 1, 8, 1, 2);
    line("7E1", 9600, 1, 7, 1, 1);
    return 0;
}
8N1  frame of 10 bits:  960.0 characters/s,  80.0% of the bits are data
8E2  frame of 12 bits:  800.0 characters/s,  66.7% of the bits are data
7E1  frame of 10 bits:  960.0 characters/s,  70.0% of the bits are data

Reading it. The frame always has one start bit, the data bits, the optional parity bit and the stop bits, so 8N1 is 10 bits per character: 960 characters per second, and 80% of the line carries data. Every extra parity or stop bit lowers the useful rate.

EX 4Memory-mapped or isolated?

Medium

A processor has 16 address lines. A device has 16 registers.

  • With memory-mapped I/O the 16 registers take 16 of the 65536 addresses (say FF00\text{FF00} to FF0F\text{FF0F}). Memory loses 16 locations, the device registers are read with ordinary LOAD, and bit tests use ordinary AND.
  • With isolated I/O the registers are ports 00 to 1515 reached with IN and OUT; memory keeps all 65536 addresses. The processor needs the IN and OUT instructions and a signal distinguishing I/O cycles from memory cycles.

If the question asks which scheme needs a separate control signal, it is isolated I/O. If it asks which scheme reduces the usable memory space, it is memory-mapped I/O.

EX 5Which scheme for which device?

GATE level

A device is ready on average once per millisecond and the CPU has nothing else to do while waiting. Polling every microsecond wastes the CPU, but if the CPU truly has nothing else to do, the waste costs nothing and polling is the simplest and fastest-responding scheme (no interrupt latency). If the CPU has other work, interrupts let it use the waiting time, which is the sort of gain the third animation shows. For a block of thousands of words, DMA beats both, because the CPU pays once per block, not once per word. The cheapest correct design is the one matched to the device's rate and to whether the CPU has other work.

Cheat box

Cheat box

  • Interface registers: data, status (READY, BUSY, ERROR), control. Reading data clears READY.
  • Memory-mapped I/O: ordinary instructions, uses memory addresses, no extra signal. Isolated I/O: separate space, IN/OUT, extra control signal, all memory addresses kept.
  • Programmed I/O = polling = busy waiting. CPU fraction =r c/fclk= r\,c / f_{clk}.
  • Strobe: one line, no confirmation. Handshake: two lines, four phases (valid up, accepted up, valid down, accepted down), adapts to speed.
  • Serial asynchronous frame: 1 start + data + optional parity + 1 or 2 stop. Characters per second =B/f= B/f.
  • Interrupt-driven: pay per event. DMA: pay per block.

Traps and GATE patterns

Connections

Device registers placed in the address space are the memory-mapped case of memory interfacing. The instruction that polls is an ordinary instruction of the instruction cycle. The two alternatives to polling are interrupts, which let the device announce itself, and DMA, which takes the CPU out of the data path altogether.

PYQs

4 previous year questions from your book. I re-solved each one myself (2 by running or simulating, 2 by reasoning only); the book's key is shown next to my answer. All PYQs

GATE 2024 · MCQQ4 · page 522 of the book
Which one of the following statements is FALSE?
  • (a)In the cycle stealing mode of DMA, one word of data is transferred between an I/O device and main memory in a stolen cycle
  • (b)For bulk data transfer, the burst mode of DMA has a higher throughput than the cycle stealing mode
  • (c)Programmed I/O mechanism has a better CPU utilization than the interrupt driven I/O mechanism
  • (d)The CPU can start executing an interrupt service routine faster with vectored interrupts than with non-vectored interrupts
GATE 2023 · NAT · 1 markQ5 · page 522 of the book
A keyboard connected to a computer is used at a rate
of 1 keystroke per second The computer system
polls the keyboard every 10 ms to check for a
keystroke and consumes 100 μs (micro seconds) for
each poll. If it is determined after polling that a key
has been pressed, the system consumes an additional
Computer Organization and Architecture

200 μs to process the keystroke. Let T1 denote the
fraction of a second spent in polling and
processing a keystroke.
In an alternative implementation, the system uses
interrupts instead of polling. An interrupt is raised
for every keystroke. It takes a total of 1 ms for
servicing an interrupt and processing a keystroke.
Let T2 denote the fraction of a second spent in
servicing the interrupt and processing a keystroke.
The ratio T1 / T2 is
. (Rounded off to
one decimal place)
GATE 2005 · MCQ · 2 marksQ16 · page 524 of the book
A device with data transfer rate 10 KB/sec is
connected to a CPU. Data is transferred byte-wise.
Let the interrupt overhead be 4 μ sec The byte
transfer time between the device interface register
and CPU or memory is negligible.
What is the minimum performance gain of operating
the device under interrupt mode over operating it
under program controlled mode?
Computer Organization and Architecture
  • (a)15
  • (b)25
  • (c)35
  • (d)45
GATE 2005 · MCQ · 1 markQ30 · page 361 of the book
Normally user programs are prevented from
handling I/O directly by I/O instructions in them.
For CPUs having explicit I/O instructions, such I/O
protection is ensured by having the I/O instruction
privileged. In a CPU with memory mapped I/O,
there is no explicit I/O instruction. Which one of the
following is true for a CPU with memory mapped
I/O?
  • (a)I/O protection is ensured by operating system routine(s)
  • (b)I/O protection is ensured by a hardware trap
  • (c)I/O protection is ensured during system configuration
  • (d)I/O protection is not possible OPERATING SYSTEMS

Self-check

Question 1

A 1 GHz processor polls a device at 10000 times per second and each poll costs 500 cycles. What fraction of CPU time is spent polling?

Show answer

10000×500/109=0.00510000 \times 500 / 10^9 = 0.005, that is 0.5%.

Question 2

Why must a memory-mapped status register not be cached?

Show answer

The device changes it without the processor's knowledge. A cached copy would keep returning the old value and the polling loop would never see READY.

Question 3

A line at 19200 bits per second uses 1 start bit, 8 data bits, no parity, 1 stop bit. How many characters per second?

Show answer

The frame has 10 bits, so 19200/10=192019200/10 = 1920 characters per second.

Question 4

In a source-initiated handshake, what does the destination do right after it sees data valid go high?

Show answer

It takes the data and raises data accepted. Only after the source sees that does it drop data valid.

Revision card

One-screen revision

  • Interface = data, status, control registers; reading data clears READY.
  • Memory-mapped: ordinary instructions, uses addresses. Isolated: IN/OUT, separate signal.
  • Polling: CPU busy waits; cost r c/fclkr\,c/f_{clk} whether or not data arrives.
  • Handshake: valid up, accepted up, valid down, accepted down.
  • Serial frame: start + data + parity + stop; rate B/fB/f.

References

  • ·Hamacher, Vranesic, Zaky, Computer Organization, 5th ed., Chapter 4 Input/Output Organization (accessing I/O devices, interrupts, DMA, buses)
  • ·Morris Mano, Computer System Architecture, 3rd ed., Chapter 11 Input-Output Organization (peripheral devices, I/O interface, asynchronous data transfer, modes of transfer)
  • ·Hennessy and Patterson, Computer Organization and Design, Chapter 5 and Appendix (I/O devices and communicating with them)

Verification

The polling-overhead figures, the serial frame arithmetic and the busy-waiting loop are produced by the C programs on this page (npm run verify:c) and cross-checked with exact fractions in scripts/enum-checks/coa_io.py. The animations are machine runs generated by scripts/gen-coa-io-traces.py and re-checked by tests/coa-io-traces.test.ts: the polling loop (buffer contents, wasted passes, the ready bit cleared by reading the data register), the four-phase handshake (edge order, data stable while valid) and the three-scheme comparison (useful ticks against closed forms). Register names, bit positions and cycle counts of the example device are an idealised teaching model and not any real controller. Textbook chapter titles are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand (long C programs were not re-read line by line: they are compiled and compared by npm run verify:c, and their printed outputs were checked against the prose). No defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never