Sub topic 8
How a DMA Controller Works: Registers and Bus Handshake
This chapter assumes you know the CPU, registers, memory, buses, and the basic fetch-decode-execute cycle. You should also be comfortable with binary and hexadecimal numbers and with stack-based subroutine calls.
Direct memory access (DMA) lets a peripheral transfer data directly to main memory without making the CPU execute one instruction for every byte or word. A DMA controller manages this transfer. It temporarily becomes the bus master, performs the transfers, and then gives control back to the CPU.
The DMA controller's job
A DMA controller is a hardware module connected to the system bus. Before a transfer begins, the CPU programs the controller by writing values into its registers.
| Register | Purpose |
|---|---|
| Memory address register | Holds the next memory address involved in the transfer |
| Count register | Holds the number of bytes or words remaining |
| Control register | Stores direction, mode, channel, and enable settings |
| Status register | Reports active, complete, error, or request states |
| Device register or channel logic | Connects the controller to a peripheral request line |
For a device-to-memory transfer, the controller reads data from the device and writes it to the address in the memory address register. After each transfer, it normally increments the address and decrements the count.
For a memory-to-device transfer, the direction is reversed. The controller reads from memory and sends the data to the peripheral.
Programming a DMA transfer
The CPU usually performs these steps:
- Write the starting memory address into the memory address register.
- Write the transfer length into the count register.
- Select the device and transfer direction.
- Select a transfer mode.
- Enable the DMA channel.
- Continue executing other instructions.
The CPU does not need to copy each data item. It only prepares the operation and later checks the completion status or handles a completion interrupt.
The count register is important because it tells the controller when the block ends. If the initial count is 4096, the controller performs 4096 byte transfers, or the equivalent number of larger transfers if the count is defined in words.
Bus request and bus grant
The CPU and DMA controller cannot safely drive the address, data, and control buses at the same time. A bus handshake prevents this conflict.
A common sequence uses these signals:
- Bus request, often called
BRorHOLD, is sent by the DMA controller to ask for the bus. - Bus grant, often called
BGorHLDA, is sent by the CPU when it releases the bus. - The CPU stops driving the bus lines and places them in a high-impedance state.
- The DMA controller drives the bus and performs transfers.
- The controller removes its request after the transfer finishes.
- The CPU removes its grant and resumes normal bus operation.
The exact signal names differ between processors. The meaning is the same: one unit requests control, and the current bus master grants control only after it reaches a safe point.
One DMA transfer
Suppose the memory address register contains 0x8000 and the count register contains 4. The device supplies four bytes.
The controller follows this pattern:
- Request the system bus.
- Wait for the CPU to grant it.
- Put
0x8000on the address bus. - Activate the device-read and memory-write control signals.
- Move the first byte from the device to memory.
- Increment the address to
0x8001. - Decrement the count to 3.
- Repeat until the count becomes zero.
- Set the completion status and optionally interrupt the CPU.
- Release the bus.
The controller may combine device and memory operations in a way supported by the system bus. The essential result is that the data path bypasses CPU registers.
What the CPU does during DMA
The CPU can execute instructions while the peripheral prepares data or while the DMA controller waits for a bus grant. During the actual DMA bus cycles, however, the CPU may be unable to access main memory. It might pause briefly, or the controller and CPU might share bus cycles depending on the DMA mode.
This creates a trade-off. DMA reduces the CPU's copying work, but it consumes memory-bus bandwidth. A large transfer can therefore slow CPU memory accesses even though the CPU is not handling every data item.
> callout note > The DMA controller does not eliminate all CPU involvement. The CPU usually performs setup and completion handling. DMA eliminates repeated per-byte or per-word instructions.
Common errors
- Confusing the transfer count with the final memory address. The count says how much data remains; the address register says where the next transfer occurs.
- Assuming DMA can use the bus while the CPU is still driving it. Bus ownership must change first.
- Forgetting the transfer direction. Device-to-memory and memory-to-device use opposite control signals.
- Treating the DMA completion interrupt as one interrupt per byte. Normally, the CPU receives one interrupt after a block completes.
- Ignoring address width and count width. Hardware register sizes limit the maximum transfer address and block length.
Exercise
A DMA controller starts with memory address 0x4000 and a count of 8 bytes. What address is in the memory address register after the transfer, and what is the final count?
Show answer
The address register becomes 0x4008 because it advances once per byte. The final count is 0.