Input/Output
Open in the Padhaiko app7.1 External devices
External devices (peripherals) connect the computer's internal CPU/memory subsystem to the outside world. Rather than attaching directly to the system bus, peripherals attach through I/O modules because external devices vary enormously in data rate, word length, electrical signaling, and mechanical timing.
External devices fall into three functional categories:
- Human-Readable: Communicate with human users (monitors/displays, keyboards, mice, printers).
- Machine-Readable: Communicate with equipment or provide secondary storage (magnetic disks, SSDs, optical drives, sensors, actuators).
- Communication: Exchange data with remote devices over networks or serial links (Ethernet NICs, Wi-Fi adapters, modems).
Inside an external device, control logic receives commands from the I/O module, a buffer temporarily holds data words/bytes, and a transducer converts electrical signals to/from physical energy (magnetic flux, light, keystrokes).
7.2 I/O modules: Module function, module structure
An I/O module is the hardware interface between the internal system bus (CPU + memory) and one or more peripheral devices.
Major Functions of an I/O Module
- Control and Timing: Coordinates bus access and internal/external timing between high-speed CPU/memory buses and slower peripherals.
- Processor Communication: Decodes CPU commands, exchanges data over the data bus, reports device status (
BUSY,READY,ERROR), and recognizes its assigned port/memory-mapped address. - Device Communication: Sends commands, status queries, and data across the external interface cable/link to the peripheral.
- Data Buffering: Absorbs speed mismatches between bursts on the memory bus (GB/s) and peripheral streams (KB/s–MB/s).
- Error Detection: Detects mechanical/device errors (paper jam, bad sector) and transmission errors (parity, CRC) and flags them in the status register.
I/O Module Structure
An I/O module connects to the system bus via data, address, and control lines, containing:
- Data Registers: Hold input/output words being transferred.
- Status/Control Registers: Store command codes written by the CPU and status/error flags read by the CPU.
- I/O Logic: Decodes addresses and control signals (
IORD,IOWR) to route data between the system bus and external device interfaces (External Device Interface Logic).
7.3 Programmed I/O, I/O commands, I/O instructions, flowchart
In Programmed I/O, the CPU retains complete control over every byte or word transferred and busy-waits (polls) the I/O module's status register until the peripheral is ready.
I/O Commands
The CPU issues four types of commands to an I/O module:
- Control: Activates a peripheral and tells it what to do (e.g., rewind tape, seek disk head).
- Test: Queries the status register of the I/O module and peripheral (power on, ready, error).
- Read: Causes the I/O module to obtain data from the peripheral into its internal buffer so the CPU can read it.
- Write: Causes the I/O module to take a word from the data bus and transmit it to the peripheral.
I/O Instructions & Addressing Modes
- Memory-Mapped I/O: I/O module registers share the single main-memory address space. Standard
LOAD/STOREinstructions access I/O ports—requiring no special I/O opcodes, at the cost of sacrificing part of the memory address range. - Isolated (Port-Mapped) I/O: I/O devices occupy a separate I/O address space accessed via dedicated CPU instructions (
IN,OUT) and separateIOR/IOWcontrol lines.
[CPU issues READ command to I/O Module]
|
v
+---> [CPU reads Status Register of I/O Module]
| |
| (Ready bit == 1?)
| / \
+--- NO -+ +-- YES --> [CPU reads Word from I/O Data Reg]
(Error -> Handle) |
v
[CPU writes Word to Memory]
|
(More words left?)
/ \
YES NO --> [Done]
|
[Loop to Next Word]Drawback: Because peripherals are thousands to millions of times slower than the CPU, the CPU wastes nearly of its cycles spinning in the status-polling loop.
7.4 Interrupt driven I/O, interrupt processing and flowchart
Interrupt-Driven I/O eliminates busy-waiting: the CPU issues a READ command to the I/O module and immediately switches to executing other programs. When the peripheral finishes preparing a word, the I/O module raises an Interrupt Request (INTR) signal to the CPU.
CPU Main Execution I/O Module & Device
------------------ -------------------
1. Issue READ command ------------> 2. Device prepares 1 word
3. Switch to other task 4. Word latched in Data Reg
(CPU does useful work) 5. Assert INTR line to CPU
|
6. Finish current instruction <--------------+
7. Save PC & PSW on stack; send INTA
8. Jump to ISR -> Read word from I/O Data Reg -> Write to Memory
9. Decrement count; if more words, issue next READ; IRET to resume taskInterrupt Identification Methods
When multiple I/O modules share interrupt lines, the CPU identifies the source using:
- Multiple Interrupt Lines: Dedicated
INTRpins (limited by CPU pin count). - Software Poll: CPU polls each module's status register sequentially in the ISR (simple, slow for many devices).
- Daisy Chain (Hardware Poll): One shared
INTRline and an Interrupt Acknowledge (INTA) line chained serially through modules in priority order; the highest-priority requesting module blocksINTAfrom passing downstream and places its vector on the bus. - Bus Arbitration / Vectored Controller (PIC / APIC): An interrupt controller prioritizes requests and sends the winning device's interrupt vector directly to the CPU.
7.5 Direct memory access (DMA): Drawbacks of programmed and interrupt-driven I/O, DMA function, typical DMA block diagram and possible DMA configuration
Drawbacks of Programmed and Interrupt-Driven I/O
- Both techniques funnel every single byte or word through the CPU's registers (
Device -> CPU -> Memory). - Even with interrupt-driven I/O, high-speed block transfers (disks, SSDs, gigabit NICs) would trigger millions of interrupts per second, saturating the CPU with context-save/restore overhead.
DMA Function and Block Diagram
A Direct Memory Access (DMA) controller is a specialized bus master module that transfers entire blocks of data directly between an I/O device and main memory without CPU intervention per word:
- The CPU initializes the DMA controller with:
- Starting memory address (in the DMA Address Register)
- Word/byte transfer count (in the Data Count Register)
- Transfer direction (
ReadorWritein the Control Register) - Target I/O device
- The CPU resumes other work. When the peripheral has data ready, the DMA controller asserts Bus Request (
BR/HOLD) to acquire the system bus; the CPU finishes its current bus cycle, puts its bus drivers into high impedance, and replies with Bus Grant (BG/HLDA). - The DMA controller drives the address, data, and control buses directly (
Memory <-> I/O), incrementing the address register and decrementing the count register after each word (via Burst Mode or Cycle Stealing). - When the count reaches
0, the DMA controller releases the bus and asserts a single interrupt to notify the CPU that the entire block transfer is complete.
Possible DMA Configurations
- Single-Bus, Detached DMA: Both DMA and I/O modules sit on the shared system bus; transferring one word takes two bus cycles (
I/O -> DMA, thenDMA -> Memory). - Single-Bus, Integrated DMA-I/O: DMA logic is integrated inside the I/O module (or shares a local path with one or more I/O devices), cutting system-bus usage to one cycle per word (
Device -> Memorydirectly). - Separate I/O Bus: An independent I/O bus connects all I/O modules directly to the DMA controller, keeping peripheral transfers completely off the main system bus except for the single
DMA <-> Memorycycle.
7.6 I/O channels and processors: The evolution of the I/O function, characteristics of I/O channels
As computers scaled from simple microcontrollers to mainframes, I/O hardware evolved through five stages of increasing CPU offload:
- CPU directly controls peripherals (no I/O module).
- I/O controller added (Programmed I/O).
- Interrupts added (Interrupt-Driven I/O).
- DMA Controller takes over raw block transfers, interrupting CPU only once per block.
- I/O Channel / I/O Processor (IOP): An autonomous special-purpose processor with its own local instruction set tailored for I/O. The CPU builds a channel program (sequence of Channel Command Words) in main memory and instructs the IOP to execute it; the IOP manages multiple devices, error retries, scatter-gather transfers, and status reporting independently.
Types of I/O Channels
- Selector Channel: Serves multiple high-speed devices (e.g., disks, tapes), dedicating the channel exclusively to one high-speed device at a time until its block transfer completes.
- Multiplexor Channel: Handles multiple slow-to-medium devices simultaneously by interleaving bytes or short blocks (
Byte Multiplexorfor terminals/printers;Block Multiplexorfor concurrent high-speed disks).
7.7 The external interface: Types of interfaces, point-to point and multiple configurations, small computer system interface (SCSI)
An external interface links an I/O module to external peripherals:
- Parallel vs. Serial Interfaces:
- Parallel: Transmits multiple bits simultaneously across separate data wires (historically used for short internal ribbons/printers; limited at high speeds by clock skew and crosstalk).
- Serial: Transmits bits sequentially over one or a few differential wire pairs/lanes (USB, SATA, PCIe, SAS)—achieves far higher clock rates and longer cable runs without inter-wire skew.
- Point-to-Point vs. Multipoint Configurations:
- Point-to-Point: Dedicated link between a single I/O port and a single external device (e.g., USB port, SATA lane, PCIe lane), offering simple electrical termination and no bus contention.
- Multipoint (Shared Bus / Daisy Chain): One external bus connects multiple peripherals sharing the same physical cable via device IDs and bus arbitration (e.g., classic parallel SCSI, FireWire).
Small Computer System Interface (SCSI)
SCSI is an intelligent, high-speed peripheral interface standard originally designed as a parallel multipoint daisy-chained bus (connecting up to 7 or 15 devices plus a host adapter via unique SCSI IDs and resistive bus terminators), and later evolved into Serial Attached SCSI (SAS) point-to-point fabrics:
- Peer-to-Peer Architecture: Any device (initiator or target) can arbitrate for the bus, issue standardized SCSI Command Descriptor Blocks (CDBs), and disconnect/reconnect while a slow mechanical seek occurs so other devices can use the bus in the meantime.
- Bus Phases: Parallel SCSI cycles through well-defined phases:
Bus Free -> Arbitration -> Selection/Reselection -> Command -> Data In/Out -> Status -> Message.
Worked Out Examples
1
A CPU transfers a (-byte) file from a disk in -byte chunks under Interrupt-Driven I/O versus DMA:
- Under Interrupt-Driven I/O, each byte causes an interrupt requiring CPU clock cycles (save context, transfer byte, restore context).
- Under DMA (cycle stealing), the CPU spends cycles on initial setup + completion ISR, and the DMA steals memory bus cycle ( CPU cycle equivalent) per -byte word transferred ( words) without stalling non-memory instructions, or assume cycle per word ( cycles) of bus delay.
Compare the CPU overhead (in cycles and percentage of the disk transfer window) for Interrupt-Driven I/O vs. DMA! (Use , during which the CPU has clock cycles.)
Computing CPU cycles consumed by Interrupt-Driven I/O: One interrupt per byte across bytes at cycles per interrupt:
Since cycles exceeds the cycles available in ( CPU load!), the CPU cannot even keep up with a byte-by-byte interrupt rate!
Computing CPU cycles consumed by DMA:
Percentage of CPU capacity over the window ( cycles):
Interrupt-Driven I/O demands CPU cycles (overloading of the CPU), whereas DMA consumes only cycles—just of CPU time over the transfer window.
2
A CPU operates at ( cycle) and runs a -cycle program while a DMA controller steals bus cycle every to transfer a block of words ( word per stolen cycle). Assuming each memory word takes stolen cycle from the CPU, calculate:
- Total CPU time added by DMA cycle stealing.
- Percentage slowdown experienced by the CPU program during the DMA transfer.
Counting stolen cycles for the -word transfer: Since word is transferred per stolen cycle, transferring words steals CPU cycles. At per cycle:
Calculating CPU percentage slowdown: During the transfer, the DMA steals cycle out of every CPU cycles, so the CPU slows down by:
Equivalently, stolen cycles occur across total elapsed cycles ().
- DMA adds cycles () of bus wait to the CPU; 2. The CPU experiences only a slowdown during the transfer.
3
Explain why Memory-Mapped I/O and Isolated (Port-Mapped) I/O differ in instruction set, address decoding, and bus control signals, and write short comparison pseudocode/instructions showing how a CPU reads a status flag and data byte from a keyboard controller at address 0x60 (data) / 0x64 (status) under each scheme.
Comparing Memory-Mapped I/O and Isolated I/O:
- Memory-Mapped I/O: I/O registers share the main memory address space; CPU uses ordinary memory instructions (
LOAD,STORE) and standardMEMR/MEMWsignals. Full addressing modes and ALU-memory instructions work directly on I/O, at the cost of consuming part of the physical address space. - Isolated I/O: Dedicated
IN/OUTinstructions and separateIOR/IOWcontrol lines keep the entire memory address space free for RAM/ROM, while restricting I/O transfers to accumulator/registerIN/OUTopcodes.
Illustrating status polling + read under both schemes:
; Memory-Mapped I/O (Status at 0xFF64, Data at 0xFF60)
WAIT_MM: LOAD R1, [0xFF64] ; read status via normal memory read
AND R1, #0x01 ; test bit 0 (Data Ready)
JZ WAIT_MM
LOAD R2, [0xFF60] ; read keyboard byte
; Isolated I/O (Status port 0x64, Data port 0x60)
WAIT_IO: IN AL, 0x64 ; read I/O port using IOR# line
TEST AL, 0x01
JZ WAIT_IO
IN AL, 0x60 ; read keyboard byteMemory-mapped I/O uses standard memory instructions and control lines within a unified address space, whereas isolated I/O uses dedicated IN/OUT instructions and separate IOR/IOW lines in an independent I/O port space.
Exercises
Exercise — Input/Output
Why are external devices not connected directly to the CPU system bus? List the five main functions of an I/O module and sketch its internal structural blocks.
Explaining why an I/O module is necessary:
- Peripherals operate at vastly slower, diverse data rates compared to the CPU and RAM.
- Peripherals use different word formats, voltage levels, and mechanical timing than the system bus.
- Connecting hundreds of heterogeneous devices directly to CPU control lines would overload the CPU and instruction set.
Listing the five I/O module functions and structure:
- Functions: (1) Control and timing, (2) Processor communication (command decode, data, status, address recognition), (3) Device communication, (4) Data buffering, and (5) Error detection.
- Structure: System bus interface Data Registers + Status/Control Registers + I/O Logic External Device Interface Logic connected to external peripherals.
I/O modules bridge speed, format, and electrical differences between peripherals and the system bus by providing control/timing, CPU communication, device communication, buffering, and error detection via data, status/control, and interface logic blocks.
Compare Programmed I/O, Interrupt-Driven I/O, and Direct Memory Access (DMA) across CPU wait behavior, data path, interrupt frequency, and best application domain.
Comparing the three I/O transfer mechanisms:
| Criterion | Programmed I/O | Interrupt-Driven I/O | DMA |
|---|---|---|---|
| CPU Wait State | Busy-waits polling status flag | Executes other tasks until interrupted | Executes other tasks; interrupted once per block |
| Data Path | directly | ||
| Interrupts | None | interrupt per word/byte | interrupt per entire block |
| Best Use Case | Simple microcontrollers / ultra-fast ports | Low-rate devices (keyboard, mouse, UART) | High-speed bulk transfers (HDD, SSD, NIC) |
Programmed I/O polls in the CPU, interrupt-driven I/O eliminates polling but still interrupts per word through the CPU, and DMA transfers whole blocks directly between device and memory with only one completion interrupt.
Describe how a DMA controller acquires the system bus using HOLD/HLDA (BR/BG), contrast Burst Mode with Cycle Stealing Mode, and name the three standard DMA bus configurations.
Explaining DMA bus arbitration and transfer modes:
- Arbitration: DMA asserts Bus Request (
BR/HOLD); CPU completes its current bus cycle, tri-states its address/data/control lines, and asserts Bus Grant (BG/HLDA). DMA then drives memory read/write cycles directly untilData Count == 0. - Burst Mode: DMA holds the bus continuously until the entire block finishes—maximizing I/O speed while temporarily stalling CPU memory access.
- Cycle Stealing Mode: DMA steals one bus cycle at a time to transfer one word and immediately returns the bus to the CPU—slowing CPU execution minimally.
Listing the three DMA configurations:
- Single-bus, detached DMA (2 bus cycles per word).
- Single-bus, integrated DMA-I/O module (1 bus cycle per word).
- Separate I/O bus connecting peripherals to the DMA controller (1 system-bus cycle per word).
DMA uses BR/BG (HOLD/HLDA) handshaking to master the bus either in continuous Burst Mode or interleaved Cycle Stealing Mode, across detached single-bus, integrated single-bus, or separate I/O-bus topologies.
A CPU initiates a DMA transfer of a (-byte) block over a -bit (-byte) memory bus. Suppose DMA setup and completion ISR together take CPU cycles, and the disk delivers data at (), stealing CPU cycle per -byte word. How many CPU cycles are consumed by DMA, and what percentage of CPU cycles during the transfer window are spent on I/O?
Computing transfer window duration and available CPU cycles:
- Transfer window ().
- Total CPU cycles in : .
Computing DMA CPU overhead:
- Number of -byte words .
- Total CPU cycles used .
- Percentage of CPU time .
DMA consumes CPU cycles ( setup/ISR stolen), representing only of the CPU cycles during the transfer window.
Trace the evolution of the I/O function up to I/O Channels / I/O Processors (IOPs), distinguish between a selector channel and a multiplexor channel, and explain the key characteristics of SCSI (including disconnect/reconnect and serial SAS evolution).
Tracing I/O evolution and channel types: I/O evolved from direct CPU polling I/O module (Programmed I/O) Interrupt-Driven I/O DMA controller I/O Channel / IOP (a dedicated processor executing memory-resident channel programs independently):
- Selector Channel: Dedicates itself to one high-speed device (disk/tape) at a time for an entire block transfer.
- Multiplexor Channel: Interleaves bytes or blocks concurrently across multiple slow/medium-speed devices.
Summarizing SCSI characteristics:
- Peer-to-peer multipoint bus supporting multiple initiators and targets identified by unique SCSI IDs (or point-to-point lanes in Serial Attached SCSI, SAS).
- Uses standardized Command Descriptor Blocks (CDBs) and allows a target device to disconnect from the bus during slow mechanical seeks and reconnect when data is ready, freeing the bus for other devices.
I/O progressed from CPU-controlled polling through interrupts and DMA to programmable I/O channels (selector for single high-speed streams; multiplexor for interleaved devices). SCSI supports peer-to-peer arbitration, standardized CDB commands, disconnect/reconnect during seeks, and evolved into point-to-point serial SAS.