G27
Self-checkedTier 1 12 min readcoa.dma

Direct memory access

DMA: the controller's registers, the sequence from setup to the final interrupt, burst, cycle-stealing and transparent modes, bus request and grant, how long the CPU is held up, the break-even against interrupt-driven I/O, and cache and address traps.

Why it exists

Programmed I/O keeps the processor in a loop, and interrupt-driven I/O still makes the processor handle every word: save state, move one word, restore state. For a disk delivering millions of bytes per second that is far too much. Direct memory access (DMA) removes the processor from the data path. A DMA controller (DMAC) is told once where in memory the block goes and how long it is; after that it moves the words between the device and memory by itself and interrupts the processor once, at the end. GATE asks for the sequence of events, the three transfer modes, the CPU time lost to stolen cycles, and the comparison with interrupt-driven I/O.

Prerequisites

You need the device interface and the three ways of running a transfer from I/O interface and programmed I/O. The comparison with interrupts is part of this page.

Precise definitions

DMA controller. An interface that can act as a bus master: it drives the address bus, the data bus and the read/write lines in place of the processor. The registers a processor programs (idealised):

  • address register (AR): the memory address of the next word, incremented after each word;
  • word count register (WC): the number of words still to move, decremented after each word; the transfer ends when it reaches 0;
  • control/status register: direction (device to memory, or memory to device), mode, start bit, interrupt enable, done and error flags.

Sequence of a DMA transfer.

  1. The processor sets up the DMAC: writes AR (start address), WC (count) and the control register (direction, start). It then goes back to other work.
  2. The device raises a DMA request (DREQ) when it has a word ready (or can take one).
  3. The DMAC raises a bus request to the processor (HOLD in Intel's naming, BR in others).
  4. The processor finishes its current bus cycle (not necessarily the current instruction), floats its address, data and control lines and answers with a bus grant (HLDA, BG). The DMAC is now the bus master.
  5. The DMAC sends a DMA acknowledge (DACK) to the device, places AR on the address bus and performs the memory read or write. In the usual single-address (fly-by) transfer the word goes between the device and memory in one bus cycle without passing through the DMAC or the processor.
  6. The DMAC increments AR and decrements WC.
  7. It releases the bus (end of burst, or after each word).
  8. When WC reaches 0 the DMAC raises an interrupt to the processor to report that the block is complete.

Transfer modes.

  • Burst (block) mode. Once the DMAC has the bus it keeps it for the entire block (or until the device has no more data). The device runs at full speed but the processor cannot use the memory bus for the whole block.
  • Cycle stealing. The DMAC takes the bus for one word (one memory cycle) at a time and gives it back. The processor is slowed down only by the stolen cycles, and in the worst case waits one cycle at a time.
  • Transparent (hidden) mode. The DMAC transfers only in bus cycles the processor is not using (for example while it computes internally). The processor is not slowed at all, but the DMAC is more complex and the transfer may be slow if the processor uses the bus a lot.

Cost. For a device with word rate ww words per second and a memory cycle time tmt_m, cycle stealing takes a fraction

fraction of memory cycles stolen=w⋅tm.\text{fraction of memory cycles stolen} = w \cdot t_m.

A burst of nn words holds the bus for n⋅tmn \cdot t_m. For a block of nn words, DMA costs the processor a fixed setup SS and a final interrupt II (in processor cycles) plus the stolen memory cycles; interrupt-driven I/O costs it n⋅cin \cdot c_i (an interrupt per word of cic_i cycles). DMA is cheaper when

S+I+n t<n ci  ⟺  n>S+Ici−t,S + I + n\,t < n\,c_i \iff n > \frac{S + I}{c_i - t},

where tt is the stolen time per word in processor cycles.

Several DMA channels share the bus by priority, using the same ideas as interrupt priority: a daisy chain of bus grants or a priority encoder. The processor has the lowest priority for the bus when a DMA request is pending.

How it works

The animation moves four words from a disk controller to memory addresses 512 to 515 by cycle stealing. The CPU only programs AR, WC and the control register, then carries on; for each word the DMAC requests the bus, is granted it for one cycle, writes the word directly into memory, updates AR and WC and releases the bus. After the fourth word WC is 0 and a single interrupt is raised. Watch that AR + WC stays constant (512 + 4) and that memory changes only while the DMAC is the bus master.

A DMA transfer of 4 words by cycle stealing: step 1

Machine run, not typed
start(CPU running a program)
AR
-
WC
-
CTRL
-
DREQ
0
HOLD
0
HLDA
0
bus
CPU
IRQ
0
addrdecimalmeaning
200.memory buffer
201.memory buffer
202.memory buffer
203.memory buffer

Step 1

A disk controller must deliver 4 words to memory at 512..515. With programmed I/O or interrupts the CPU would move every word. With DMA the CPU only starts the transfer. The DMA controller (DMAC) has an address register AR, a word count WC and a control register; it can take over the buses (the CPU is the bus master now).

1 / 29
computed by script: a simulation of the machine or protocol, one event per step. python3 scripts/gen-coa-io-traces.py

The time-axis comparison of polling, interrupts and DMA moving the same three words is on the I/O interface page: the DMA row loses only the setup, one stolen tick per word and the final interrupt, and gets the most useful work (29 of 36 ticks in that model). With more words per block the advantage grows, since the fixed costs are paid once.

Worked examples

EX 1How many memory cycles does the device steal?

Easy

A disk delivers 4-byte words at 4 MB/s into a memory with a 100 ns cycle. The DMAC steals one memory cycle per word. A 512-byte sector is also moved as one burst.

#include <stdio.h>
 
int main(void) {
    /* a disk delivers 4-byte words at 4 MB/s into a memory with a 100 ns cycle; the DMA controller steals one memory cycle per word */
    double words_per_s = 4e6 / 4, cycle_ns = 100;
    printf("%.0f words/s, each steals %.0f ns: %.1f%% of the memory cycles\n", words_per_s, cycle_ns, 100.0 * words_per_s * cycle_ns * 1e-9);
    /* a 512-byte sector moved as one burst holds the bus for */
    int words = 512 / 4;
    printf("burst of %d words holds the bus %.1f us\n", words, words * cycle_ns / 1000.0);
    return 0;
}
1000000 words/s, each steals 100 ns: 10.0% of the memory cycles
burst of 128 words holds the bus 12.8 us

Reading it. Words per second =4 000 000/4=106= 4\,000\,000/4 = 10^6, each taking 100 ns100\ \text{ns} of memory time: 106×100×10−9=0.110^6 \times 100 \times 10^{-9} = 0.1. At most a tenth of the memory cycles are stolen; the processor slows down by less than that if it often works from its cache. As one burst the 128 words hold the bus for 128×100 ns=12.8 μs128 \times 100\ \text{ns} = 12.8\ \mu\text{s}, during which the processor cannot touch memory.

EX 2When does DMA beat interrupts?

Medium

Interrupt-driven I/O costs 120 cycles per word. DMA costs 1000 cycles of setup, 1 cycle of stolen time per word and 500 cycles for the final interrupt. From what block size does DMA use fewer processor cycles?

#include <stdio.h>
 
int main(void) {
    /* interrupt-driven: every word costs 120 cycles. DMA: 1000 cycles setup + 1 cycle per word + 500 cycles for the final interrupt */
    int n = 1;
    while (1000 + n * 1 + 500 >= n * 120) n++;
    printf("DMA is cheaper from %d words up\n", n);
    int block = 1024;
    printf("block of %d words: interrupts %d cycles, DMA %d cycles\n", block, block * 120, 1000 + block + 500);
    return 0;
}
DMA is cheaper from 13 words up
block of 1024 words: interrupts 122880 cycles, DMA 2524 cycles

Reading it. Using n>(S+I)/(ci−t)=1500/119≈12.6n > (S + I)/(c_i - t) = 1500/119 \approx 12.6, the smallest integer is 13, and the program agrees. For a 1024-word block DMA costs about 2% of what interrupts cost. For a block of 3 words, interrupts would win, which is why DMA is for bulk devices and interrupts for slow ones.

EX 3Put the DMA events in order

Medium

The correct order for a device-to-memory transfer is: the processor programs AR, WC and the control register; the device raises DREQ; the DMAC raises HOLD; the processor finishes its bus cycle and raises HLDA; the DMAC (via DACK) lets the device place its word on the data bus and writes it to memory at AR; AR is incremented and WC decremented; the DMAC releases HOLD (cycle stealing), and the steps from DREQ repeat until WC is 0; the DMAC interrupts the processor. The processor takes part only in the first step, in each bus grant (a one-cycle pause at most) and in the last step.

EX 4Burst or cycle stealing: how long is the CPU held up?

GATE level

A DMA transfer of 128 words, one memory cycle each. In burst mode the processor is locked out of the bus for 128 consecutive cycles, so a processor request that arrives just after the burst starts waits up to 128 cycles. In cycle-stealing mode a processor request waits at most one cycle (the word being transferred), though it can be delayed once for each word, 128 times in all if it needs the bus all the time. The total time lost can be the same; what differs is the longest single wait. Burst mode is faster for the device, cycle stealing is better for a processor that cannot afford long stalls (for example one serving interrupts with deadlines).

EX 5Which statement is correct?

GATE level

"The processor can be interrupted in the middle of a bus cycle by a DMA request." No. The processor grants the bus only at the end of a bus cycle; an interrupt is accepted only at the end of an instruction. A DMA request can therefore be granted in the middle of a multi-cycle instruction, while an interrupt request cannot. "After a DMA grant, the processor must save its registers." No. The processor only floats its bus lines and waits; no registers are saved and no context is switched, which is why cycle stealing is cheap.

Cheat box

Cheat box

  • DMAC registers: AR (address, incremented), WC (count, decremented), control. Transfer ends when WC =0= 0, then one interrupt.
  • Handshake: DREQ, then HOLD (bus request), then HLDA (bus grant), then DACK and the transfer.
  • The bus is granted at the end of a bus cycle; an interrupt is accepted at the end of an instruction.
  • Burst: whole block, long CPU stall. Cycle stealing: one word at a time. Transparent: only unused cycles, no slow-down.
  • Stolen fraction =w tm= w\,t_m (word rate times memory cycle time).
  • DMA beats interrupts when n>(S+I)/(ci−t)n > (S + I)/(c_i - t).
  • Data goes directly between device and memory (fly-by); the CPU is not in the data path.

Traps and GATE patterns

Connections

DMA takes over the buses that memory interfacing connects the memory to. It is the third scheme after polling and interrupts, and it ends with an interrupt of its own. The effect on the cache is the coherence problem named above, linked to the write policies.

PYQs

8 previous year questions from your book. I re-solved each one myself (5 by running or simulating, 3 by reasoning only); the book's key is shown next to my answer. All PYQs

year not read yetQ3 · page 522 of the book
Consider a computer with a 4 MHz processor. Its
DMA controller can transfer 8 bytes in 1 cycle from
a device to main memory through cycle stealing at
regular intervals. Which one of the following is the
data transfer rate (in bits per second) of the DMA
controller if 1% of the processor cycles are used for
DMA?
  • (a)2,56,000
  • (b)3,200
  • (c)25,60,000 (d 32,000
GATE 2024 · MCQQ4 · page 522 of the book
Which one of the following statements is FALSE?
  • (a)In the cycle stealing mode of DMA, one word of data is transferred between an I/O device and main memory in a stolen cycle
  • (b)For bulk data transfer, the burst mode of DMA has a higher throughput than the cycle stealing mode
  • (c)Programmed I/O mechanism has a better CPU utilization than the interrupt driven I/O mechanism
  • (d)The CPU can start executing an interrupt service routine faster with vectored interrupts than with non-vectored interrupts
GATE 2022 · MCQ · 1 markQ6 · page 523 of the book
Which one of the following facilitates transfer of
bulk data from hard disk to main memory with the
highest throughput?
  • (a)DMA based I/O transfer
  • (b)Interrupt driven I/O transfer
  • (c)Polling based I/O transfer
  • (d)Programmed I/O transfer
GATE 2021 · Set 2 · NATQ7 · page 523 of the book
Consider a computer system with DMA support.
The DMA module is transferring one 8-bit
character in one CPU cycle from a device to
memory through cycle stealing at regular
intervals. Consider a 2 MHz processor. If 0.5%
processor cycles are used for DMA, the data
transfer rate of the device is
bits per
second.
GATE 2020 · MCQ · 1 markQ8 · page 523 of the book
Consider the following statements.
I. Daisy chaining is used to assign priorities in
attending interrupts.
II. When a device raises a vectored interrupt, the
CPU does polling to identify the source of the
interrupt.
III. In polling, the CPU periodically checks the
status bits to know if any device needs its attention.
IV. During DMA, both the CPU and DMA
controller can be bus masters at the same time.
Which of the above statements is/are TRUE?
  • (a)I and IV only
  • (b)I and II only
  • (c)III only
  • (d)I and III only
GATE 2016 · Set 1 · MCQ · 2 marksQ9 · page 523 of the book
The size of the data count register of a DMA controller
is 16 bits.The processor needs to transfer a file of
29,154 kilobytes from disk to main memory.The
memory is byte addressable
The minimum number of times the DMA controller
needs to get the control of the system bus from the
processor to transfer the file from the disk to main
memory is
GATE 2011 · MCQQ14 · page 524 of the book
On a non-pipelined sequential processor, a
program segment, which is the part of the interrupt
service routine, is given to transfer 500 bytes from
an I/O device to memory.
Intialize the address register
Intilatize the count to 500
LOOP: Load a byte from device
Store in memory at address given by address
register
Increment the address register
Decrement the count
If count != 0 go to LOOP
Assume that each statement in this program is
equivalent to a machine instruction which takes one
clock cycle to execute if it is a non-load/store
instruction. The load-store instructions take two
clock cycles to execute.
The designer of the system also has an alternate
approach of using the DMA controller to implement
the same transfer. The DMA controller requires 20
clock cycles for initialization and other overheads.
Each DMA transfer cycle takes two clock cycles to
transfer one byte of data from the device to the
memory.
What is the approximate speed up when the DMA
controller based design is used in a place of the
interrupt driven program based input-output?
  • (a)3.4
  • (b)4.4
  • (c)5.1
  • (d)6.7
GATE 2005 · MCQQ17 · page 525 of the book
Consider the disk drive with the following
specifications: 16 surfaces, 512 tracks/surface, 512
sectors/track, 1 KB/sector, rotation speed 3000
rpm. The disk is operated in cycle stealing mode
whereby whenever one byte is ready it is sent to
memory, similarly, for writing, the disk interface
reads a 4-byte word from the memory in each
DMA cycle. Memory cycle time is 40 nsec The
maximum percentage of time that the CPU gets
blocked during DMA operation is
  • (a)10
  • (b)25
  • (c)40
  • (d)50

Self-check

Question 1

A device delivers 2 MB/s in 4-byte words and the memory cycle is 200 ns. What fraction of the memory cycles does cycle stealing take?

Show answer

Words per second =500 000= 500\,000; 500 000×200×10−9=0.1500\,000 \times 200 \times 10^{-9} = 0.1: 10%.

Question 2

When is a DMA bus grant given: at the end of an instruction or of a bus cycle?

Show answer

At the end of the current bus cycle. Interrupts are taken at the end of an instruction.

Question 3

A 256-word block is moved by DMA with a setup of 800 cycles, 1 stolen cycle per word and a final interrupt of 400 cycles. Interrupt-driven I/O costs 100 cycles per word. Which is cheaper and by how much?

Show answer

DMA: 800+256+400=1456800 + 256 + 400 = 1456 cycles. Interrupts: 256×100=25600256 \times 100 = 25600 cycles. DMA saves 24144 cycles.

Question 4

How many interrupts does the processor receive for a 1000-word DMA transfer that completes without errors?

Show answer

One, when the word count reaches 0.

Revision card

One-screen revision

  • Setup: AR, WC, control. Request: DREQ, HOLD, HLDA. Transfer: M[AR], AR + 1, WC - 1. At WC =0= 0: one interrupt.
  • Burst (whole block), cycle stealing (one word), transparent (idle cycles only).
  • Grant at the end of a bus cycle; interrupt at the end of an instruction.
  • Stolen fraction =w tm= w\,t_m. DMA wins for large blocks.
  • Cache coherence and physical addresses are the usual traps.

References

  • ·Hamacher, Vranesic, Zaky, Computer Organization, 5th ed., Chapter 4 Input/Output Organization (direct memory access, bus arbitration)
  • ·Morris Mano, Computer System Architecture, 3rd ed., Chapter 11 Input-Output Organization (direct memory access, DMA controller, DMA transfer)
  • ·Stallings, Computer Organization and Architecture, Input/Output chapter (direct memory access)

Verification

The stolen-cycle fraction, the burst bus-hold time and the break-even block size are produced by the C programs on this page (npm run verify:c); scripts/enum-checks/coa_io.py checks the same formulas with exact fractions and the break-even over every block size up to 2000 words. The transfer animation is a machine run (scripts/gen-coa-io-traces.py) re-checked by tests/coa-io-traces.test.ts (AR + WC constant, one bus tenure per word, memory changes only while the controller owns the bus, one interrupt at the end). The DMA controller is an idealised single-channel model with fly-by transfers (one bus cycle per word); real controllers have several channels, modes and signal names (HOLD/HLDA, BR/BG/BGACK, DREQ/DACK). The cache and address-translation remarks describe the usual arrangement, not every system. Textbook chapter titles are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand (long C programs were not re-read line by line: they are compiled and compared by npm run verify:c, and their printed outputs were checked against the prose). No defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never