Sub topic 8
How a DMA Controller Works: Registers and Bus Handshake
This chapter assumes you know the concepts from the earlier chapters. These are the I/O interface and its registers, memory-mapped I/O, and interrupt-driven I/O with the interrupt cycle. It also assumes you can read hexadecimal addresses and know how the address, data and control buses connect the CPU, memory and I/O modules.
The previous chapter explained why DMA exists: interrupt-driven I/O still costs CPU cycles for every word. This chapter covers the hardware that removes that cost. It explains which registers the CPU programs, which signals the DMA controller and CPU exchange, and what happens on the buses during a transfer.
The DMA controller as a second bus master
A DMA controller (DMAC) is a hardware module that can drive the address, data and control buses on its own. Normally only the CPU does this, and it is called the bus master. During a DMA transfer the DMAC becomes bus master, and the CPU stays off the system bus until the DMAC releases it.
The CPU is still in charge of the whole operation. It decides what to transfer, where, and in which direction. It programs the DMAC, starts it, and goes back to other work. The DMAC does the repetitive part: generating one memory address per word, counting words, and signalling when it is done.
A DMAC needs four things to work:
- A place to hold the current memory address.
- A counter for the words remaining.
- A way to know the direction (device to memory, or memory to device).
- A set of wires to request and receive control of the bus.
The first three are registers. The fourth is the handshake.
DMAC registers
The register set is small. Names vary between textbooks and chips, but the roles are the same.
| Register | Holds | Updated by DMAC during transfer |
|---|---|---|
| Address register (AR) | Memory address of the next word to transfer | Incremented (or decremented) after each word |
| Word count register (WC) | Number of words still to transfer | Decremented after each word |
| Control register | Direction (read or write), mode, enable bit, interrupt-enable bit | Not changed by the transfer |
| Status register | Busy, done (terminal count reached), error flags | Set by the DMAC |
Some DMACs also have a data buffer register. It is used when the controller transfers each word in two bus cycles, first reading it into the buffer and then writing it out.
The CPU reaches these registers like any other device registers, either as memory-mapped locations or as I/O ports. So the DMAC is itself an I/O device, and the CPU talks to it with ordinary programmed I/O. The difference is that a handful of register writes starts a transfer of thousands of words.
Many DMACs have several channels. Each channel has its own address and count registers, and each serves one device.
Programming a transfer
To read a block from a disk into memory, the CPU does the following:
- Writes the starting memory address into AR.
- Writes the number of words into WC.
- Writes the control register with direction = device to memory, interrupt-enable = 1, and enable = 1.
- Tells the device to start (usually through the device's own control register).
After step 4 the CPU continues with other instructions. The sketch below shows steps 1 to 3 in C, using a hypothetical register layout in the style of the earlier chapters.
#include <stdint.h>
/* Hypothetical memory-mapped DMA channel registers */
typedef struct {
volatile uint32_t address; /* AR: next memory address */
volatile uint32_t count; /* WC: words remaining */
volatile uint32_t control; /* bit0 enable, bit1 dir, bit2 int-enable */
volatile uint32_t status; /* bit0 busy, bit1 done */
} dma_channel_t;
#define DMA0 ((dma_channel_t *)0x5000)
#define DMA_ENABLE 0x1u
#define DMA_TO_MEM 0x2u /* 1 = device to memory */
#define DMA_INT_EN 0x4u
void dma_start_read(uint32_t mem_addr, uint32_t words) {
DMA0->address = mem_addr;
DMA0->count = words;
DMA0->control = DMA_ENABLE | DMA_TO_MEM | DMA_INT_EN;
/* The device is then told to begin (device-specific, omitted). */
}The bus handshake
Four signals matter. Their names differ between processors (the Intel 8085 and 8086 use HOLD and HLDA, other designs use BR and BG), but the logic is the same.
| Signal | Direction | Meaning |
|---|---|---|
| DREQ (DMA request) | Device to DMAC | The device has data ready or is ready to accept data |
| HOLD or BR (bus request) | DMAC to CPU | The DMAC wants the bus |
| HLDA or BG (bus grant) | CPU to DMAC | The CPU has released the bus |
| DACK (DMA acknowledge) | DMAC to device | Your request is accepted, and the current transfer is for you |
The order of events for one word is as follows:
- The device raises DREQ.
- The DMAC raises the bus request signal to the CPU.
- The CPU finishes its current bus cycle, puts its address, data and control outputs into a high-impedance (disconnected) state, and raises the grant signal.
- The DMAC places the address from AR on the address bus and issues the memory and I/O control signals. It sends DACK to the device.
- The word moves between device and memory.
- The DMAC increments AR and decrements WC.
- If WC is not zero, the DMAC repeats from step 4 for the next word or releases the bus, depending on the transfer mode.
- When WC reaches zero, the DMAC drops the bus request, the CPU takes back the bus, and the DMAC raises an interrupt.
The CPU yields at a bus cycle, not an instruction
The CPU grants the bus at the end of the current bus cycle, not the end of the current instruction. One instruction can use several bus cycles (opcode fetch, operand read, result write), and the DMAC can step in between them. This is why DMA response time is short.
An interrupt behaves differently. The CPU normally checks for interrupts only at the end of an instruction. That difference is a frequent source of GATE questions.
While the DMAC holds the bus
The CPU cannot use the system bus. It can still execute instructions that need no bus access, such as register-to-register operations, or instructions already in an on-chip cache. This is why DMA slows the CPU down only when the CPU and the DMAC both need memory.
Two ways a word crosses the bus
How many bus cycles one word needs depends on the DMAC design.
- Single-cycle (fly-by) transfer. The DMAC activates the device's read line and the memory's write line at the same time. The word travels directly from device to memory in one cycle and never enters the DMAC. The DMAC only supplies the address and control signals.
- Two-cycle transfer. The DMAC first reads the word into its internal data buffer, then writes it to the destination in a second cycle. This is needed when the source and destination are both memory, or when the device and memory have different widths or timings.
Single-cycle transfer is faster and is the default assumption when a question gives you "one bus cycle per word". If a question states that each word takes two cycles, count both.
Termination and the completion interrupt
The DMAC stops when WC reaches zero. This condition is called the terminal count. The DMAC then sets the done bit in the status register and, if the interrupt-enable bit is set, raises an interrupt.
The interrupt is the only point at which the CPU is involved again. The ISR typically does three things:
- Reads the status register to check for success or error.
- Clears the done flag so the next transfer can start.
- Wakes the process that was waiting for the data, or starts the next block.
The CPU therefore spends cycles in two places per block: setting up the registers before, and servicing one interrupt after. Compare this with interrupt-driven I/O, which charges one interrupt per word.
Several channels and priority
When several devices raise DREQ at the same time, the DMAC picks one. It uses a fixed priority (channel 0 highest) or a rotating one. The DMAC sends DACK only to the chosen device. Other devices keep DREQ asserted and wait. A DMAC with many channels is therefore also an arbiter, and a high-priority channel can starve a low-priority one if it never lets go.
A small simulation
The following Python model shows how AR and WC change. It copies bytes from a device (represented by a byte string) into a bytearray used as memory.
class DmaController:
def __init__(self, memory):
self.memory = memory
self.address = 0
self.count = 0
self.done = False
def program(self, address, count):
self.address = address
self.count = count
self.done = False
def run(self, device_bytes):
data = iter(device_bytes)
while self.count > 0:
self.memory[self.address] = next(data) # one transfer
self.address += 1 # AR incremented
self.count -= 1 # WC decremented
self.done = True # terminal count reached
mem = bytearray(32)
dma = DmaController(mem)
dma.program(address=8, count=5)
dma.run(b"HELLO")
print(mem[8:13].decode()) # HELLO
print("AR after:", dma.address) # 13
print("WC after:", dma.count) # 0
print("done:", dma.done) # TrueAfter the transfer, AR points one past the last word written (8 + 5 = 13), and WC is zero. These two facts are common exam answers.
Worked example: register values
A DMAC moves a 4096-byte block to memory starting at address 0x2000. The bus is 32 bits wide, and the DMAC transfers one 4-byte word per bus cycle.
- WC is loaded with 4096 / 4 = 1024 words. If WC were loaded with 4096, the DMAC would move 16 KB.
- AR starts at 0x2000 and increases by 4 per word if memory is byte-addressed.
- The last word is written to 0x2000 + 1023 × 4 = 0x2FFC.
- After the last word, AR = 0x3000 and WC = 0.
- The interrupt count for the whole block is 1. Interrupt-driven I/O would have needed 1024.
Common errors
- Confusing who generates addresses. In DMA the address comes from the DMAC's AR, not the CPU's program counter or address registers.
- Saying the CPU is idle during DMA. The CPU is only blocked from the system bus. It keeps executing from registers and cache.
- Placing the interrupt per word. DMA interrupts once per block (or on error), not per word.
- Forgetting the setup cost. The CPU still writes the registers, and that costs cycles (this matters in the performance calculations later in the tutorial).
- Ignoring cache effects. The DMAC writes to memory without going through the CPU cache. Unless hardware keeps them coherent, the cache can hold stale data, so software may need to invalidate cache lines before reading a DMA buffer.
Summary
- The DMAC has an address register, a word count register, a control register and a status register. The CPU programs them like any I/O device.
- The handshake is DREQ, then bus request, then bus grant, then DACK. The CPU grants the bus after the current bus cycle.
- For each word the DMAC puts out the address, moves the word, increments AR and decrements WC.
- At terminal count (WC = 0) the DMAC releases the bus and interrupts the CPU once.
- Single-cycle transfers move the word directly between device and memory. Two-cycle transfers go through the DMAC's buffer.
Exercise
A DMA controller is programmed to transfer 2 KB from a device to memory starting at byte address 0x8000. The memory is byte-addressed, the DMAC moves 4-byte words, and each word takes one bus cycle. What value is loaded into the word count register, what are AR and WC when the transfer finishes, and how many interrupts does the CPU receive for the block?
Show answer
2 KB = 2048 bytes, and 2048 / 4 = 512 words, so WC is loaded with 512. AR increases by 4 per word, so the final value is 0x8000 + 512 × 4 = 0x8000 + 0x800 = 0x8800. WC finishes at 0. The CPU receives one interrupt, at terminal count.
Exercise
A device raises DREQ while the CPU is in the middle of a multi-cycle instruction that needs three bus cycles. When can the DMAC obtain the bus, and when would an interrupt raised at the same moment be serviced?
Show answer
The CPU can grant the bus at the end of the current bus cycle, so the DMAC may get the bus before the instruction has finished. An interrupt is normally recognised only after the whole instruction completes, so it is serviced later than the DMA request is granted.
Exercise
Explain why a fly-by (single-cycle) DMA transfer needs the DMAC to supply only the address and control signals, and state how many bus cycles one word needs in this mode compared with the two-cycle mode.
Show answer
In fly-by mode the DMAC enables the device's output and the memory's write at the same time, so the data travels directly over the data bus from the device to memory without entering the DMAC. The DMAC only drives the address and control lines. One word takes one bus cycle, against two for the two-cycle mode, where the word is first read into the DMAC's buffer and then written out.