CampusFlow
ArchitectureI/O Organization

🔌 I/O Organization

How input and output devices communicate with the CPU, including programmed I/O, interrupt-driven I/O, and Direct Memory Access (DMA).

I/O Modules and Their Functions

â„šī¸

Role of I/O Modules

I/O modules act as interfaces between the CPU/memory and peripheral devices. They handle data buffering, protocol conversion, error detection, and device synchronization so the CPU can communicate with diverse peripherals through a standard interface.

Data Buffering

Temporarily stores data to bridge speed differences between CPU and device.

Control & Timing

Coordinates data flow between internal and external devices.

Device Selection

Decodes address lines to select the correct peripheral.

Error Detection

Checks parity and reports transmission errors to the CPU.

Programmed I/O (Polling)

â„šī¸

How It Works

The CPU repeatedly checks the status of an I/O device to see if it is ready for data transfer. The CPU sits in a busy loop, wasting cycles until the device is ready.

c

// Programmed I/O — CPU actively polls device status
while (true) {
    status = read_device_status();  // Poll device
    if (status == READY) {
        data = read_device_data();  // Transfer byte
        memory[addr++] = data;
        if (block_complete) break;
    }
    // CPU is busy-waiting — no other work done
}

✅ Advantages

  • Simple to implement
  • No special hardware needed
  • Deterministic timing

❌ Disadvantages

  • Wastes CPU cycles (busy-waiting)
  • Poor utilization — CPU can not do other work
  • Not suitable for high-speed devices

Interrupt-Driven I/O

💡

Key Concept

Instead of polling, the device interrupts the CPU when it is ready. The CPU can execute other tasks until the interrupt occurs, dramatically improving system efficiency.

c

// Interrupt-driven I/O — CPU works until device signals
void main_loop() {
    while (true) {
        do_other_work();      // CPU is productive
    }
}

// Interrupt Service Routine (ISR)
void device_interrupt_handler() {
    data = read_device_data();
    memory[addr++] = data;
    // Return to main loop — CPU does not waste cycles polling
}

Interrupt-Driven I/O Flow

CPU
Running tasks
→
Device Ready
Sends IRQ
→
CPU Saves State
Context switch
→
ISR Executes
Data transfer
→
Resume
Restore context

Direct Memory Access (DMA)

â„šī¸

How DMA Works

A dedicated DMA controller handles data transfers directly between I/O devices and memory without CPU involvement for each byte. The CPU sets up the transfer, then the DMA controller manages the data movement, interrupting the CPU only when the entire block is complete.
1

CPU Initiation

CPU sets source, destination, and count in DMA registers.

2

DMA Transfer

DMA controller moves data byte-by-byte. CPU operates in parallel.

3

Completion IRQ

DMA sends interrupt to CPU when block transfer is done.

I/O Processor / Channel

â„šī¸

I/O Channel

An I/O processor (or channel) is a specialized processor dedicated to handling all I/O operations. It can execute its own instruction set for I/O, freeing the CPU entirely from I/O management. Used in mainframes and high-performance systems.

Selector Channel

Handles one high-speed device at a time (e.g., disk drive). Transfers data at full device speed.

Multiplexor Channel

Handles multiple slow devices simultaneously by interleaving byte transfers.

Comparison of I/O Methods

FeatureProgrammed I/OInterrupt-DrivenDMA
CPU InvolvementPer bytePer byteSetup + completion
CPU UtilizationPoor (busy-wait)GoodExcellent
Hardware RequiredMinimalInterrupt controllerDMA controller
Transfer RateSlowMediumHigh
Best ForSimple, slow devicesMedium-speed devicesHigh-speed devices
ComplexityLowMediumHigh
Overhead per byteVery highHighVery low

Code Example: Polling vs Interrupt

💡

Assembly-Level Comparison

This simplified example shows the fundamental difference. Polling checks device status in a tight loop. Interrupt-driven I/O lets the CPU work until the device signals readiness.

assembly

; === Programmed I/O (Polling) ===
POLL:   IN  STATUS, DEVICE    ; Read device status
        AND STATUS, #READY    ; Check ready flag
        BZ   POLL             ; Not ready? Keep polling
        IN  DATA, DEVICE      ; Read data from device
        STORE DATA, MEM_ADDR  ; Store to memory
        ; CPU wasted all cycles polling

; === Interrupt-Driven I/O ===
MAIN:   ; Main program does useful work
        ADD  R1, R2, R3      ; Compute...
        ; Device will interrupt when ready

ISR:    ; Interrupt Service Routine
        IN  DATA, DEVICE      ; Read data
        STORE DATA, MEM_ADDR  ; Store to memory  
        IRET                  ; Return from interrupt
        ; CPU was productive until interrupt

Interview Questions

What are the three main methods of I/O data transfer?

1) Programmed I/O (polling) — CPU actively checks device status wasting cycles. 2) Interrupt-driven I/O — Device interrupts CPU when ready, improving utilization. 3) DMA — Dedicated controller transfers data directly between device and memory with minimal CPU intervention.

Explain the difference between an I/O processor and a DMA controller.

A DMA controller simply moves data between memory and devices. An I/O processor is a more sophisticated unit that can execute its own instructions, handle multiple devices, perform data formatting, and manage complex I/O protocols — essentially acting as a dedicated I/O CPU.

Why is programmed I/O inefficient for high-speed devices?

With programmed I/O, the CPU must poll the device for every byte transferred. High-speed devices transfer millions of bytes per second, meaning the CPU would spend all its time polling, leaving no time for actual computation. Interrupt-driven I/O or DMA is essential for such devices.

What is cycle stealing in DMA?

Cycle stealing is a DMA transfer mode where the DMA controller forces the CPU to pause for one bus cycle to transfer a single byte of data, then returns control to the CPU. This 'steals' individual bus cycles from the CPU, allowing it to continue processing between transfers. It is the most common DMA mode.