Loading lessons...
Direct Memory Access (DMA), Input-Output Processor & Serial Communication
Direct Memory Access (DMA), Input-Output Processor & Serial Communication
With interrupt-driven I/O the CPU still moves every byte itself. For fast devices such as disks that is too slow, so the device's interface is allowed to use the memory buses directly: this is direct memory access.
A computer with many devices can go one step further and give I/O its own processor. This lesson covers both, and the protocols used on serial lines.
1. Direct Memory Access
During DMA the CPU is idle on the bus: it gives up the address, data and read/write lines to the DMA controller, which moves data between the device and memory without running any CPU instructions. The CPU and DMA agree using two control signals:
- BR (bus request): the DMA controller asks the CPU for the buses.
- BG (bus grant): the CPU finishes its current bus cycle, puts its bus drivers in the high-impedance state and answers BG = 1. The DMA now owns the buses until it drops BR.
DMA transfer in a computer system
The DMA Controller
Block diagram of a DMA controller
The DMA controller looks like an ordinary interface to the CPU (it is selected with DS and RS and read or written with RD and WR), but it has three registers of its own:
| Register | Holds | Changes during the transfer |
|---|---|---|
| Address register (AR) | the memory address of the next word | incremented after each word |
| Word count register (WC) | the number of words still to transfer | decremented after each word; 0 means done |
| Control register | the mode: read or write, burst or cycle stealing | set once by the CPU |
The DMA Transfer Sequence
- Initialisation by the CPU: it sends the starting address (to AR), the word count (to WC), the direction and mode (to the control register), and starts the transfer.
- The device raises DMA request; the controller raises BR.
- The CPU replies BG = 1 and disconnects from the bus.
- The controller puts AR on the address bus, sends DMA acknowledge to the device, and the word moves straight between the device and memory (RD or WR).
- AR ← AR + 1, WC ← WC − 1. If WC ≠ 0 the controller waits for the next request.
- When WC = 0 the controller drops BR and interrupts the CPU to say the block is finished.
The CPU sets up a DMA transfer of 256 words into memory starting at address 2000H. What do AR and WC hold at the end, and where is the last word stored?
- Each word adds 1 to AR and subtracts 1 from WC.
- After 256 words: AR = 2000H + 256 = 2000H + 100H = 2100H, WC = 0.
- The last word went to the address before that: 20FFH.
Modes of DMA Transfer
| Mode | How it works | Effect on the CPU |
|---|---|---|
| Burst (block) transfer | the DMA keeps the bus for the whole block | CPU cannot use memory until the block ends; fastest for the device |
| Cycle stealing | the DMA takes the bus for one word, then gives it back | CPU is slowed a little, one memory cycle at a time |
| Transparent (hidden) | the DMA uses the bus only in cycles when the CPU is not using it | no slowdown, but needs extra logic to detect free cycles |
A disk delivers 2 MB/s by cycle stealing. Each DMA transfer moves one 4-byte word and takes one 100 ns memory cycle. What fraction of memory cycles does the DMA take?
- Words per second = 2 × 10⁶ / 4 = 5 × 10⁵.
- Time used per second = 5 × 10⁵ × 100 ns = 0.05 s.
- Fraction = 0.05 / 1 = 5%.
A 4 KB block (4096 bytes) is read from a disk. With interrupt-driven I/O, each byte costs the CPU 10 µs (interrupt plus service routine). With DMA, the CPU spends 20 µs setting up the controller and 10 µs on the final interrupt. Compare the CPU time.
- Interrupt-driven: 4096 × 10 µs = 40960 µs = 40.96 ms.
- DMA: 20 + 10 = 30 µs.
- Ratio = 40960 / 30 ≈ 1365.
In burst mode a DMA controller moves 1024 words, each taking a 200 ns memory cycle. How long is the CPU locked out of memory?
The model below has the CPU grant the bus one clock after each request (cycle-stealing mode). The CPU loads AR = 40H and WC = 3:
// DMA controller (cycle-stealing mode): the CPU loads the address
// register and word count; for each word the DMA asks for the bus (BR),
// waits for the grant (BG), writes the word to memory and counts down.
module dma_controller (
input clk,
input start, // CPU has loaded AR and WC
input [7:0] ar_in,
input [7:0] wc_in,
input dma_request, // device has a word ready
input bg, // bus grant from the CPU
input [7:0] device_data,
output reg br, // bus request to the CPU
output reg we, // memory write
output reg [7:0] addr, // address register AR
output reg [7:0] mem_addr, // address sent to memory
output reg [7:0] data,
output reg dma_ack,
output reg interrupt, // block finished
output reg [7:0] wc
);
always @(posedge clk) begin
we <= 0;
dma_ack <= 0;
if (start) begin
addr <= ar_in;
wc <= wc_in;
br <= 0;
interrupt <= 0;
end else if (wc != 0) begin
if (dma_request && !br)
br <= 1; // ask for the bus
else if (br && bg) begin // CPU has given the bus
data <= device_data;
we <= 1; // write the word to memory
dma_ack <= 1;
br <= 0; // give the bus back
mem_addr <= addr;
addr <= addr + 1'b1; // AR <- AR + 1
wc <= wc - 1'b1;
if (wc == 1)
interrupt <= 1; // word count reached 0
end
end
end
endmodule
module tb;
reg clk = 0, start = 1, dma_request = 0;
reg [7:0] device_data = 8'hA0;
reg bg = 0;
wire br, we, dma_ack, interrupt;
wire [7:0] addr, mem_addr, data, wc;
reg [7:0] mem [0:255];
dma_controller u (clk, start, 8'h40, 8'd3, dma_request, bg, device_data,
br, we, addr, mem_addr, data, dma_ack, interrupt, wc);
always #5 clk = ~clk;
// the CPU grants the bus one clock after the request (end of its cycle)
always @(posedge clk) bg <= br;
always @(posedge clk)
if (we)
mem[mem_addr] <= data;
integer t;
initial begin
@(posedge clk);
#1 start = 0;
dma_request = 1;
$display("clk BR BG AR WC event");
for (t = 1; t <= 10; t = t + 1) begin
@(posedge clk);
#1;
if (dma_ack)
device_data = device_data + 8'h11;
$write("%3d %b %b %h %0d ", t, br, bg, addr, wc);
if (we)
$display("M[%h] <- %h, bus returned to CPU", mem_addr, data);
else if (bg)
$display("CPU grants the bus");
else
$display("DMA requests the bus");
if (interrupt) begin
@(posedge clk);
#1 $display("interrupt: block done; M[40..42] = %h %h %h",
mem[8'h40], mem[8'h41], mem[8'h42]);
$finish;
end
end
$finish;
end
endmoduleEvery word takes a request, a grant and a write. AR goes up and WC goes down by one each time, and the interrupt comes when WC reaches 0:
clk BR BG AR WC event
1 1 0 40 3 DMA requests the bus
2 1 1 40 3 CPU grants the bus
3 0 1 41 2 M[40] <- a0, bus returned to CPU
4 1 0 41 2 DMA requests the bus
5 1 1 41 2 CPU grants the bus
6 0 1 42 1 M[41] <- b1, bus returned to CPU
7 1 0 42 1 DMA requests the bus
8 1 1 42 1 CPU grants the bus
9 0 1 43 0 M[42] <- c2, bus returned to CPU
interrupt: block done; M[40..42] = a0 b1 c22. Input-Output Processor (IOP)
An input-output processor is a processor with direct memory access that handles all I/O for many devices. Unlike a DMA controller, which is set up for every block, an IOP fetches and executes its own I/O program from memory. The CPU only starts it and checks the result.
- The CPU is the master; the IOP is a slave that works on its own once started.
- The IOP's instructions are called commands (in IBM machines, channel command words, CCWs). They say what to do (read, write, control, sense status), where in memory and how many bytes.
- The IOP takes memory cycles by DMA, competing with the CPU.
CPU-IOP communication
CPU-IOP communication goes through a status word and a program in memory:
- The CPU tests the I/O path; the IOP stores a status word in memory.
- If the status is OK, the CPU sends start I/O, with the address of the IOP program.
- The IOP runs its program and does the transfers by DMA, while the CPU runs other programs.
- When finished the IOP interrupts the CPU. The CPU asks for the status, and the IOP stores the status word, which the CPU checks.
IBM channel types:
- Selector channel: serves one fast device (disk) at a time, in burst mode.
- Multiplexer (byte multiplexer) channel: interleaves bytes from many slow devices (terminals, printers).
- Block multiplexer channel: interleaves blocks from several fast devices.
| DMA controller | IOP (channel) | |
|---|---|---|
| Programmable | no: the CPU sets AR, WC and mode for each block | yes: runs its own I/O program |
| Devices served | one or a few | many |
| CPU involvement | set up every block | start the program and check status |
| Code and format conversion | no | yes |
3. Serial Communication
Remote terminals and computers are connected by serial lines through modems (modulator-demodulator) or line drivers.
Transmission modes:
- Simplex: data in one direction only (like radio broadcast).
- Half-duplex: both directions, but one at a time (like a walkie-talkie). The line has a turnaround time.
- Full-duplex: both directions at the same time, on two pairs of wires or two frequency bands.
Synchronous versus asynchronous: asynchronous transmission frames each character with start and stop bits (lesson 40). Synchronous transmission sends a continuous block of bits; the receiver recovers the clock from the signal, so there are no start and stop bits, and a protocol marks where messages begin and end.
Character-Oriented Protocol
The message is made of ASCII characters, with control characters for framing:
| Character | Meaning |
|---|---|
| SYN | synchronous idle: keeps the receiver in step |
| SOH | start of heading |
| STX | start of text |
| ETX | end of text |
| ACK / NAK | positive / negative acknowledgment |
A data byte that happens to equal a control character is preceded by DLE (data link escape), so the receiver does not mistake it for control.
Bit-Oriented Protocol (HDLC / SDLC)
The message is a frame of bits, not characters:
| Flag | Address | Control | Information | Frame check | Flag |
|---|---|---|---|---|---|
| 01111110 | 8 bits | 8 bits | any number of bits | 16 bits (CRC) | 01111110 |
The flag 01111110 marks both ends of the frame. To make sure it never appears inside the frame, the transmitter uses zero insertion (bit stuffing): after five consecutive 1s it inserts a 0. The receiver deletes any 0 that follows five 1s; six 1s can then only be a flag.
Apply zero insertion to the data 0111111011111100.
- Bits 2 to 6 are five 1s: insert a 0 after them.
- The next bits are 1, 0, then five 1s again: insert a 0 after them.
- The rest is 1, 0, 0.
| Data | 0 11111 | 1 0 11111 | 1 00 |
|---|---|---|---|
| Sent | 0 11111 0 | 1 0 11111 0 | 1 00 |
The bit stuffer counts consecutive 1s and holds the sender for one clock while it inserts the 0:
// Zero insertion (bit stuffing) for a bit-oriented protocol: after five
// 1s in a row the transmitter inserts a 0, so the data can never contain
// the flag 01111110. The receiver deletes the 0 that follows five 1s.
module bit_stuffer (
input clk,
input rst,
input in_valid,
input in_bit,
output reg out_valid,
output reg out_bit,
output stall // sender must hold its bit for one clock
);
reg [2:0] ones;
assign stall = (ones == 5);
always @(posedge clk)
if (rst) begin
ones <= 0;
out_valid <= 0;
end else if (ones == 5) begin
out_bit <= 0; // the inserted 0
out_valid <= 1;
ones <= 0;
end else if (in_valid) begin
out_bit <= in_bit;
out_valid <= 1;
ones <= in_bit ? ones + 1'b1 : 3'd0;
end else
out_valid <= 0;
endmodule
module tb;
reg clk = 0, rst = 1, in_valid = 0, in_bit = 0;
wire out_valid, out_bit, stall;
bit_stuffer u (clk, rst, in_valid, in_bit, out_valid, out_bit, stall);
always #5 clk = ~clk;
reg [15:0] data = 16'b0111111011111100;
integer i;
always @(posedge clk)
if (out_valid)
$write("%b", out_bit);
initial begin
$display("data: %b", data);
@(negedge clk) rst = 0;
$write("sent: ");
i = 15;
while (i >= 0) begin
in_valid = 1;
in_bit = data[i];
@(negedge clk);
if (stall) begin
in_valid = 0; // wait while the 0 is inserted
@(negedge clk);
end
i = i - 1;
end
in_valid = 0;
@(negedge clk);
@(negedge clk);
$display("");
$finish;
end
endmoduleIts output matches Example 5, so the flag pattern can no longer appear inside the data:
data: 0111111011111100
sent: 011111010111110100Key points to remember
- DMA: BR from DMA, BG from CPU; CPU's bus drivers go to high impedance.
- DMA registers: address (incremented), word count (decremented; 0 means done), control.
- Burst, cycle stealing and transparent modes.
- IOP runs its own program of commands; CPU only starts it and reads the status word.
- HDLC flag 01111110; insert a 0 after five 1s.
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. What is DMA? Draw the block diagram of a DMA controller and explain the DMA transfer. (10 Marks)
- Write: BR and BG, the three registers, and the six steps.
- Q2. Explain the burst and cycle-stealing modes of DMA. A device transfers 2 MB/s by cycle stealing with 4-byte words and 100 ns cycles: what fraction of cycles is stolen? (5 Marks)
- Q3. What is an input-output processor? Explain CPU-IOP communication. (10 Marks)
- Q4. Differentiate between a DMA controller and an IOP. (5 Marks)
- Q5. Explain character-oriented and bit-oriented protocols. What is zero insertion? (5 Marks)