Skip to main content

4.3 Direct Memory Access (DMA) Controllers & Cycle Stealing Mechanics

📚04Topic 4.3⏱️18 min
🎯High-Yield For:Every gigabyte of data streaming into modern computers—from NVMe SSD storage and 100-Gigabit Ethernet cards to GPU framebuffers—bypasses the CPU core entirely via Direct Memory Access (DMA) to achieve line-rate throughput without saturating processor registers.

💡 Core Intuition:​

🍳 The Everyday Analogy:​

Imagine you are the Chief Executive Architect designing a skyscraper:

  • The Manual Courier Approach (Programmed / Interrupt I/O): A delivery truck arrives with 10,00010,000 steel beams. Instead of designing buildings, you personally walk down to the delivery truck, pick up Beam #1, carry it up the elevator, place it neatly on the 4th floor storage rack, record it in your clipboard, and walk back down to repeat the process 9,9999,999 more times. Even if an assistant rings a chime every time a beam is unloaded (Interrupt-Driven), your executive mind is completely paralyzed by acting as a manual freight elevator.

  • The Dedicated Freight Crew Approach (Direct Memory Access): You hire a professional Freight Moving Crew (the DMA Controller). You hand the crew foreman a clipboard with three simple instructions:

    1. Source: Delivery Dock #3 (I/O Device Port).
    2. Destination: 4th Floor Storage Rack, Slot 500 (Starting Memory Address).
    3. Quantity: 10,00010,000 Beams (Word Count).

    You then say: "Take control of the main freight hallway (System Bus). Call me on the intercom only when all 10,00010,000 beams are stored." You return to your executive drafting table, spending 100% of your intellect designing the skyscraper.

  • Transfer Modes:

    • Burst Mode: The freight crew cordons off the hallway completely. Nobody else can use the hallway until all 10,00010,000 beams are moved.
    • Cycle Stealing Mode: The freight crew carries one beam across the hallway, pauses to let you walk past to the coffee machine, then carries another beam. The hallway is shared smoothly.
    • Transparent Mode: The freight crew only carries beams when you are seated at your desk with your door closed thinking or sketching, using the hallway strictly during your internal desk time.

💻 Bridging to Computer Science:​

In computer systems:

  1. High-speed peripherals (NVMe solid-state drives, Gigabit network adapters, graphics cards) produce or consume megabytes of data per second.
  2. If every byte must pass through the CPU via software loops (IN AL, DX followed by MOV [DI], AL), the processor's internal registers and arithmetic execution units become an intolerable bottleneck.
  3. Direct Memory Access (DMA) introduces a specialized hardware coprocessor—the DMA Controller (DMAC)—that temporarily assumes the role of Bus Master. The DMA controller drives memory addresses and control signals directly onto the system bus, streaming data directly between the peripheral interface and Main Memory (RAM) at physical bus speeds with zero CPU execution intervention.


Why DMA? The Fundamental Bandwidth Bottleneck​

To appreciate why Direct Memory Access is an absolute architectural necessity, consider the micro-architectural steps required to transfer a block of data under processor control versus DMA control.

CPU-Mediated Transfer vs. Direct Memory Access (DMA)

Comparing the instruction-by-instruction software bottleneck against hardware-driven memory streaming

CPU-Mediated Transfer

Programmed / Interrupt Transfer

🐢
Dominant Architecture / DomainLow-Speed Peripherals (Keyboards, Mice, Legacy UART Serial)
  • •Every byte requires executing 4 to 6 machine instructions (Load, Store, Pointer Inc, Counter Dec, Branch).
  • •Data travels: Peripheral -> Data Bus -> CPU Register -> Data Bus -> RAM (Double Bus Crossing).
  • •Transfer rate is severely capped by CPU instruction fetch, decode, and execute latency.
  • •CPU execution cores are 100% consumed or repeatedly context-switched.
"The CPU acts as a redundant middleman, holding every byte in its own registers."
Direct Memory Access

Hardware Bus Master Transfer

⚡
Dominant Architecture / DomainHigh-Speed Peripherals (NVMe SSDs, PCIe NICs, GPUs, Sound/Video)
  • •CPU executes only 3 instructions to initialize DMA registers, then executes user applications.
  • •Data travels directly: Peripheral -> Data Bus -> RAM in a single bus cycle.
  • •Transfer rate reaches the theoretical maximum physical throughput of the memory bus.
  • •CPU runs concurrently; interrupted only once when the entire multi-megabyte block finishes.
"The peripheral and RAM communicate directly across the bus while the CPU computes."

Quantitative Proof: Double Bus Crossing Penalty​

Under CPU-mediated transfer, moving a single 32-bit word requires two separate bus transactions:

  1. Bus Cycle 1 (I/O Read): CPU drives peripheral port address onto address bus, asserts IOR‾\overline{\text{IOR}}, and latches data from the data bus into an internal accumulator (EAXEAX).
  2. Bus Cycle 2 (Memory Write): CPU drives memory destination address onto address bus, places EAXEAX onto the data bus, and asserts MEMW‾\overline{\text{MEMW}}.

In addition, the CPU must execute register increment instructions for the memory pointer and decrement instructions for the loop counter.

Under DMA, the DMA controller drives the memory address while simultaneously asserting both MEMW‾\overline{\text{MEMW}} (telling RAM to store data) and DACK\text{DACK} (telling the peripheral interface to drive data onto the data bus). The transfer completes in a single bus cycle, immediately doubling physical bus bandwidth!


DMA Controller Internal Architecture & Register Model​

The DMA Controller is a specialized hardware bus master containing dedicated address generators, counters, and control logic.

Internal Architecture of a DMA Controller

The four core register blocks and bus interface engines governing autonomous transfers

📍

Memory Address Register (MAR)

Address Pointer
  • Holds the physical starting memory address in RAM for the read/write operation.
  • Automatically increments (or decrements) by word size after every successful transfer.
  • Connected directly to the system address bus when the DMAC is Bus Master.
🔢

Word Count Register (WCR)

Transfer Counter
  • Stores the total number of words/bytes to be transferred in the block.
  • Automatically decrements by 1 after each word transfer across the bus.
  • Triggers Terminal Count (TC) interrupt when decremented past zero.
⚙️

Control / Mode Register

Configuration Logic
  • Specifies transfer direction: I/O to Memory (Write) vs Memory to I/O (Read).
  • Selects operational mode: Burst Mode, Cycle Stealing Mode, or Transparent Mode.
  • Enables/disables individual DMA channel requests and interrupt flags.
📊

Status Register

Hardware Telemetry
  • Indicates whether a channel has reached its Terminal Count (TC).
  • Latches pending DMA requests (DREQ) and active grants.
  • Read by CPU during driver error checking and completion polling.

Tri-State Bus Interfacing & Bus Master Handshaking​

To prevent electrical contention, both the CPU and the DMA controller are equipped with tri-state bus drivers (capable of driving Logic High 1, Logic Low 0, or High-Impedance High-Z).

When the CPU controls the system, its drivers are active and the DMA controller's drivers are in High-Z. When the DMA controller takes over, the CPU floats its address, data, and control pins into High-Z, allowing the DMA controller to drive the physical traces directly.

This handshaking is mediated by two dedicated control lines:

  • Bus Request (HOLD or BR): The DMA controller asserts HOLD = 1 to signal the CPU that it requires ownership of the system buses.
  • Bus Grant (HLDA or BG - Hold Acknowledge): The CPU finishes its current bus cycle, floats its address, data, and control buses into high-impedance mode, and asserts HLDA = 1 to notify the DMA controller that it is now the Bus Master.
Architecture Flow
Step 1 of 6

DMA Computer System Interconnect Architecture

Textbook hardware standard (Morris Mano Fig 11-18) showing dual bus masters, arbitration handshakes, and direct memory streaming

🧠Host Processing & Main Memory
⚡DMA Engine & Peripheral Interconnect
1DREQ (Request)
2HOLD (Bus Req)
3HLDA (Bus Grant)
4DACK (Grant)
5Direct Bus Stream
6TC Interrupt
🧠Processor
Central CPU
Default Bus Master
🗄️Target RAM
Main Memory (RAM)
System Data Storage
⚡Coprocessor
DMA Controller (DMAC)
Alternate Bus Master
💾Peripheral
High-Speed Device
NVMe / Storage FIFO
💡Click or hover any card or transition arrow above to inspect deep-dive operational mechanics

The 5-Stage DMA Transfer Protocol​

The end-to-end execution of a DMA data block involves a tightly orchestrated five-stage lifecycle:

The 5-Stage DMA Bus Arbitration & Transfer Sequence

Hardware handshaking between Peripheral, DMA Controller, CPU, and System Memory

🧠CPU Core
💾Peripheral
⚡DMA Controller
🗄️Main Memory
1
CPU Core→DMA Controller
Initialization

CPU writes physical starting memory address into DMA MAR, block size into WCR, and configures read/write mode.

OUT DMAC_MAR, ADDR
2
Peripheral→DMA Controller
DMA Request

Peripheral FIFO buffer fills with data and asserts DREQ line to the DMA Controller.

DREQ = 1
3
DMA Controller→CPU Core
Bus Arbitration

DMAC asserts HOLD to CPU. CPU completes current bus cycle, floats buses to High-Z, and returns HLDA.

HOLD = 1 / HLDA = 1
4
DMA Controller→Main Memory & Device
Direct Streaming

DMAC asserts DACK to device, drives MAR onto address bus, and asserts MEMW. Data streams to RAM in one cycle.

DACK = 1, MEMW = 0
5
DMA Controller→CPU Core
Terminal Count

When WCR decrements to zero, DMAC drops HOLD to release bus mastership and asserts INTR to CPU.

TC = 1, INTR = 1

DMA Operational Transfer Modes​

The manner in which the DMA controller utilizes the system bus defines three distinct operational modes:

Architecture Flow

The Three Canonical DMA Transfer Modes

Contrasting bus allocation, latency, and CPU concurrency

💡 Hover or click any card for deep-dive operational details
⚡High Bandwidth

Burst Mode (Block)

DMAC seizes the bus and retains continuous ownership until the entire block of N words is transferred.

→
🔄Balanced Sharing

Cycle Stealing Mode

DMAC seizes the bus for exactly ONE memory cycle per word, then immediately releases it to the CPU.

→
👻Zero CPU Impact

Transparent / Hidden Mode

DMAC transfers data strictly when the CPU is executing internal decode or ALU cycles that do not use the bus.

1. Burst Mode (Block Transfer)​

In Burst Mode, once the CPU grants the bus (HLDA = 1), the DMA controller transfers the entire contiguous buffer of data without releasing the bus.

  • Advantage: Maximum possible throughput. The initial arbitration overhead (tarbitration=tHOLD+tHLDAt_{\text{arbitration}} = t_{\text{HOLD}} + t_{\text{HLDA}}) is incurred only once for thousands of bytes.
  • Disadvantage: The CPU cannot fetch new instructions from memory during the transfer (unless instructions reside entirely inside the on-chip L1 cache). If an urgent, time-critical hardware interrupt occurs, its servicing is delayed until the burst finishes.

2. Cycle Stealing Mode (Single-Word Transfer)​

In Cycle Stealing Mode, the DMA controller transfers exactly one data word (or byte), immediately releases the HOLD line, and returns bus mastership to the CPU. If the peripheral requires another transfer, the DMA controller must arbitrate again for the next cycle.

  • Micro-Architectural Mechanics: The CPU is not completely halted; rather, the DMA controller "steals" one memory access cycle out of every MM cycles.
  • Advantage: Excellent CPU responsiveness. The CPU continues running application threads in parallel, and interrupt latency is completely minimized.
  • Mathematical Bus Allocation Ratio:
📐Cycle Stealing Bus Consumption Ratioformula

Let a peripheral generate data at a transfer rate of R bytes/secondR\text{ bytes/second}.
The time between successive byte arrivals from the device is:

Δtdevice=1R\Delta t_{\text{device}} = \frac{1}{R}

Let the memory system cycle time required to store one byte be TmemoryT_{\text{memory}}.
In Cycle Stealing mode, the DMA controller occupies the bus for TmemoryT_{\text{memory}} seconds during each Δtdevice\Delta t_{\text{device}} interval.

Fraction of Bus Stolen (CPU Slowdown)=TmemoryΔtdevice=R×Tmemory\text{Fraction of Bus Stolen (CPU Slowdown)} = \frac{T_{\text{memory}}}{\Delta t_{\text{device}}} = R \times T_{\text{memory}}

% CPU Time Consumed by DMA=(R×Tmemory)×100%\% \text{ CPU Time Consumed by DMA} = \big(R \times T_{\text{memory}}\big) \times 100\%

% Productive CPU Time Remaining=100%−(R×Tmemory×100%)\% \text{ Productive CPU Time Remaining} = 100\% - \big(R \times T_{\text{memory}} \times 100\%\big)

3. Transparent / Hidden Mode​

In Transparent Mode, the DMA controller monitors the CPU's internal clock states (e.g., via status lines or bus cycle decoding). An instruction cycle consists of multiple sub-phases: Instruction Cycle=Fetch (IF)→Decode (ID)→Execute (EX)→Memory (MEM)→Write-Back (WB)\text{Instruction Cycle} = \text{Fetch (IF)} \to \text{Decode (ID)} \to \text{Execute (EX)} \to \text{Memory (MEM)} \to \text{Write-Back (WB)} During the Instruction Decode (ID) and internal Execute (EX) phases, the CPU core is performing internal register and ALU operations; it does not touch the external address or data bus.

The transparent DMA controller slips into the bus during these exact internal clock cycles, performs a transfer, and vacates the bus before the CPU initiates its next memory cycle.

  • Impact: The CPU experiences 0%0\% performance degradation, achieving true zero-cost background I/O!

🏭 Real-World Production Context: Cache Coherency & Scatter-Gather DMA​

Modern operating systems and high-performance server architectures face two critical challenges when utilizing DMA: Memory Fragmentation and Cache Incoherency.

Scatter-Gather DMA Architecture​

Early DMA controllers (like the Intel 8237 in original IBM PCs) required the target memory buffer to be physically contiguous in RAM. In modern operating systems running Virtual Memory with 4 KB4\text{ KB} paging, a 2 MB2\text{ MB} file buffer is virtually contiguous, but its physical pages are scattered randomly across physical RAM.

Under basic DMA, the OS would have to perform thousands of separate single-page DMA transfers, interrupting the CPU after every 4 KB4\text{ KB}.

The Modern Solution: Scatter-Gather DMA:

  • The operating system constructs a linked list of DMA Descriptors in memory:
    struct dma_descriptor {
    uint64_t physical_buffer_address; // Pointer to physical page
    uint32_t length_in_bytes; // Size of page fragment (e.g. 4096)
    uint32_t control_flags; // End-of-List flag, Interrupt flag
    uint64_t next_descriptor_address; // Hardware pointer to next descriptor
    };
  • The CPU writes only the starting address of Descriptor #1 into the DMA controller's Descriptor Pointer Register.
  • The DMA controller autonomously reads Descriptor #1, transfers its 4 KB4\text{ KB}, automatically fetches Descriptor #2 via memory pointer traversal, and continues without interrupting the CPU until all scattered fragments are fully transferred!

The DMA Cache Coherency Problem​

Because the DMA controller writes directly to physical RAM, it completely bypasses the processor's internal L1, L2, and L3 caches. This creates a severe Cache Incoherency Hazard:

The Two DMA Cache Coherency Hazards

Stale RAM and Stale Cache anomalies arising from asynchronous memory writes

⚠️

Hazard 1: Stale RAM on DMA Read

Memory-to-Device Transfer
  • A user process writes data into a buffer. The CPU cache operates in Write-Back mode.
  • The modified dirty data resides exclusively in L1/L2 cache; RAM contains old, stale data.
  • If DMA reads from RAM directly, it transmits stale data to the network card or disk!
  • Fix: OS driver must explicitly Flush (Clean) the cache before starting DMA.
💥

Hazard 2: Stale Cache on DMA Write

Device-to-Memory Transfer
  • A network card streams a new packet into physical RAM via DMA.
  • The CPU already had the old buffer contents cached in its L1/L2 cache lines.
  • When the application reads the packet, the CPU hits on stale cache lines, missing the new data!
  • Fix: OS driver must explicitly Invalidate the cache lines before reading.

In modern server architectures (x86-64 and ARM Neoverse), cache coherency during DMA is handled either by Hardware Bus Snooping (the cache controller snoops DMA bus transactions and automatically invalidates or flushes matching cache lines) or by operating system memory management using uncached, coherent DMA pools via Linux kernel APIs like dma_alloc_coherent().


📐 Numerical Applications & Worked Examples​

Example 1: DMA Word Count Register Sizing & File Transfer Segmentation​

Problem Statement:
A 32-bit computer system utilizes a DMA controller with a 16-bit Word Count Register (WCR). The memory system is byte-addressable.
The operating system must transfer a large scientific dataset file of size 29,154 Kilobytes29,154\text{ Kilobytes} (29,154 KB29,154\text{ KB}, where 1 KB=1,024 bytes1\text{ KB} = 1,024\text{ bytes}) from an NVMe storage controller into main memory.

Calculate:

  1. The maximum block size (in bytes and kilobytes) that the DMA controller can transfer in a single continuous bus grant session.
  2. The minimum number of times the DMA controller must request and obtain control of the system bus (HOLD/HLDA grants) from the central processor to transfer the entire file.

Step-by-Step Solution:

  1. Calculate Maximum Single-Session Transfer Capacity:

    • The Word Count Register contains 16 bits16\text{ bits}.
    • In binary counter design, a 16-bit register can represent: 216=65,536 distinct count states2^{16} = 65,536\text{ distinct count states}
    • Because memory is byte-addressable, the maximum number of bytes transferred before the counter rolls over / hits terminal count is: Max Bytes per Grant=65,536 bytes\text{Max Bytes per Grant} = 65,536\text{ bytes}
    • Expressing in Kilobytes: Max Block Size=65,536 B1,024 B/KB=64 KB\text{Max Block Size} = \frac{65,536\text{ B}}{1,024\text{ B/KB}} = \mathbf{64\text{ KB}}
  2. Calculate Total File Size in Kilobytes:

    • The total file size is given as: File Size=29,154 KB\text{File Size} = 29,154\text{ KB}
  3. Calculate Minimum Number of Bus Grant Operations:

    • Each bus grant allows a maximum transfer of 64 KB64\text{ KB}.
    • Number of required grants: Ngrants=⌈Total File SizeMax Block Size per Grant⌉N_{\text{grants}} = \left\lceil \frac{\text{Total File Size}}{\text{Max Block Size per Grant}} \right\rceil Ngrants=⌈29,154 KB64 KB⌉N_{\text{grants}} = \left\lceil \frac{29,154\text{ KB}}{64\text{ KB}} \right\rceil
    • Performing division: 29,15464=455.53125\frac{29,154}{64} = 455.53125
    • Because a fractional block requires a full distinct DMA initialization and bus grant session: Ngrants=⌈455.53125⌉=456 timesN_{\text{grants}} = \lceil 455.53125 \rceil = \mathbf{456\text{ times}}

Example 2: Cycle Stealing Bus Contention & CPU Slowdown Derivation​

Problem Statement:
A high-throughput storage drive with a continuous data transfer rate of 10 MB/s10\text{ MB/s} (10 Megabytes/second10\text{ Megabytes/second}, where 1 MB=106 bytes1\text{ MB} = 10^6\text{ bytes}) is streaming data into memory using a DMA controller in Cycle Stealing Mode.

  • The processor runs at a clock frequency of 600 MHz600\text{ MHz}.
  • The system bus is 32 bits (4 bytes4\text{ bytes}) wide, and each DMA memory write cycle consumes 4 bus clock cycles4\text{ bus clock cycles}.
  • To initiate each DMA transfer sequence, the processor incurs an initialization overhead of 300 clock cycles300\text{ clock cycles}.
  • When the transfer completes, the processor incurs an interrupt service completion overhead of 900 clock cycles900\text{ clock cycles}.
  • The total transfer size is 20 KB20\text{ KB} (20,000 bytes20,000\text{ bytes}).

Calculate:

  1. The total time required by the drive to transfer the 20 KB20\text{ KB} block.
  2. The percentage of processor time consumed by the combined initialization, cycle-stealing memory contention, and completion overhead.

Step-by-Step Solution:

  1. Calculate Peripheral Streaming Time (TtotalT_{\text{total}}):

    • Device transfer rate R=10 MB/s=10×106 B/sR = 10\text{ MB/s} = 10 \times 10^6\text{ B/s}.
    • Data size D=20,000 bytesD = 20,000\text{ bytes}.
    • Time required for the transfer: Ttotal=DR=20,000 B10×106 B/s=2×10−3 s=2 ms=2,000 μsT_{\text{total}} = \frac{D}{R} = \frac{20,000\text{ B}}{10 \times 10^6\text{ B/s}} = 2 \times 10^{-3}\text{ s} = 2\text{ ms} = 2,000\text{ }\mu\text{s}
  2. Calculate Total CPU Clock Cycles Occurring in TtotalT_{\text{total}}:

    • Processor frequency f=600 MHz=600×106 cycles/secondf = 600\text{ MHz} = 600 \times 10^6\text{ cycles/second}.
    • Clock period Tclk=1600×106 Hz=53 ns≈1.667 nsT_{\text{clk}} = \frac{1}{600 \times 10^6\text{ Hz}} = \frac{5}{3}\text{ ns} \approx 1.667\text{ ns}.
    • Total clock cycles available in 2 ms2\text{ ms}: Ctotal=f×Ttotal=(600×106)×(2×10−3)=1,200,000 clock cyclesC_{\text{total}} = f \times T_{\text{total}} = (600 \times 10^6) \times (2 \times 10^{-3}) = 1,200,000\text{ clock cycles}
  3. Calculate Cycle Stealing Bus Cycles Stolen:

    • System bus width = 4 bytes/transfer4\text{ bytes/transfer}.
    • Number of DMA transfers required: Ntransfers=20,000 bytes4 bytes/word=5,000 wordsN_{\text{transfers}} = \frac{20,000\text{ bytes}}{4\text{ bytes/word}} = 5,000\text{ words}
    • Each transfer steals 4 clock cycles4\text{ clock cycles} on the bus.
    • Total clock cycles stolen by DMA memory bus writes: Csteal=5,000×4=20,000 clock cyclesC_{\text{steal}} = 5,000 \times 4 = 20,000\text{ clock cycles}
  4. Calculate Total Processor Cycles Consumed:

    • Initialization overhead: Cinit=300 cyclesC_{\text{init}} = 300\text{ cycles}.
    • Completion interrupt overhead: Ccomplete=900 cyclesC_{\text{complete}} = 900\text{ cycles}.
    • Bus cycles stolen from CPU: Csteal=20,000 cyclesC_{\text{steal}} = 20,000\text{ cycles}.
    • Total processor cycles lost to the operation: Clost=Cinit+Ccomplete+Csteal=300+900+20,000=21,200 clock cyclesC_{\text{lost}} = C_{\text{init}} + C_{\text{complete}} + C_{\text{steal}} = 300 + 900 + 20,000 = 21,200\text{ clock cycles}
  5. Calculate Percentage of Processor Time Consumed: % Time Consumed=ClostCtotal×100%=21,2001,200,000×100%≈1.77%\% \text{ Time Consumed} = \frac{C_{\text{lost}}}{C_{\text{total}}} \times 100\% = \frac{21,200}{1,200,000} \times 100\% \approx \mathbf{1.77\%}


Example 3: Interrupt-Driven Transfer vs. DMA Speedup Factor​

Problem Statement:
On a sequential non-pipelined processor running at 10 MHz10\text{ MHz} (1 clock cycle=100 ns1\text{ clock cycle} = 100\text{ ns}), an operating system must transfer 500 bytes500\text{ bytes} from an I/O device to main memory. The system designer is evaluating two implementation approaches:

  • Approach A: Interrupt-Driven Software Transfer:
    The ISR executes the following machine loop for each byte:

    LOOP: LOAD  R1, [DEV_DATA]    ; Load byte from device interface (2 clock cycles)
    STORE [MEM_PTR], R1 ; Store byte into memory buffer (2 clock cycles)
    INC MEM_PTR ; Increment memory address pointer (1 clock cycle)
    DEC COUNT ; Decrement byte loop counter (1 clock cycle)
    JNZ LOOP ; Loop back if COUNT != 0 (2 clock cycles)

    In addition, the initial setup and return instructions of the ISR consume a total of 15 clock cycles15\text{ clock cycles}.

  • Approach B: Direct Memory Access (DMA) Transfer:
    The DMA controller requires 20 clock cycles20\text{ clock cycles} for initial software register configuration. Each DMA memory transfer cycle requires 2 clock cycles2\text{ clock cycles} to transfer one byte directly from the device to memory.

Calculate:

  1. The total clock cycles required by Approach A.
  2. The total clock cycles required by Approach B.
  3. The speedup factor achieved by utilizing DMA in place of interrupt-driven transfer.

Step-by-Step Solution:

  1. Calculate Execution Cycles for Approach A (Interrupt Software Loop):

    • Cycles consumed per iteration of the inner loop: Cloop=2(LOAD)+2(STORE)+1(INC)+1(DEC)+2(JNZ)=8 clock cycles/byteC_{\text{loop}} = 2 (\text{LOAD}) + 2 (\text{STORE}) + 1 (\text{INC}) + 1 (\text{DEC}) + 2 (\text{JNZ}) = 8\text{ clock cycles/byte}
    • For 500 bytes500\text{ bytes}, the loop runs 500500 times. Note that on the final iteration, JNZJNZ is not taken, taking 1 cycle1\text{ cycle} instead of 2 cycles2\text{ cycles} (saving 1 cycle1\text{ cycle}): Citerations=(500×8)−1=4,000−1=3,999 clock cyclesC_{\text{iterations}} = (500 \times 8) - 1 = 4,000 - 1 = 3,999\text{ clock cycles}
    • Add initial setup and return overhead: CApproach_A=15+3,999=4,014 clock cyclesC_{\text{Approach\_A}} = 15 + 3,999 = \mathbf{4,014\text{ clock cycles}}
  2. Calculate Execution Cycles for Approach B (DMA Controller):

    • Initialization overhead = 20 clock cycles20\text{ clock cycles}.
    • Hardware transfer cycles: 500 bytes×2 cycles/byte=1,000 clock cycles500\text{ bytes} \times 2\text{ cycles/byte} = 1,000\text{ clock cycles}.
    • Total cycles consumed: CApproach_B=20+1,000=1,020 clock cyclesC_{\text{Approach\_B}} = 20 + 1,000 = \mathbf{1,020\text{ clock cycles}}
  3. Calculate Speedup Factor: Speedup S=TimeApproach_ATimeApproach_B=CApproach_ACApproach_B\text{Speedup } S = \frac{\text{Time}_{\text{Approach\_A}}}{\text{Time}_{\text{Approach\_B}}} = \frac{C_{\text{Approach\_A}}}{C_{\text{Approach\_B}}} S=4,0141,020≈3.94×S = \frac{4,014}{1,020} \approx \mathbf{3.94\times} The DMA controller accelerates the data transfer by nearly 400%400\%, while freeing the CPU to execute application instructions during the 1,000 cycles1,000\text{ cycles} of bus transfer!


🎯 Exam & Interview Pitfall Check​

Core Conceptual Questions
  • Question 1: In Cycle Stealing mode, does the DMA controller steal an entire CPU Instruction Cycle or a Memory Bus Cycle?
    Answer: The DMA controller steals a Memory Bus Cycle, never an entire instruction cycle! An instruction cycle consists of multiple machine cycles (Fetch, Decode, Execute, Memory, Write-Back). If the CPU is currently in an execution phase that does not require the memory bus (e.g., an ALU multiplication or shift), the DMA transfer proceeds with zero delay to the CPU. If the CPU needs to access memory during that exact bus cycle, it is held in a wait state for just one bus cycle.

  • Question 2: Why must the CPU float its Address, Data, and Control lines to High-Impedance (High-Z) when asserting HLDA?
    Answer: Because both the CPU and the DMA controller are physically connected to the same copper traces of the system bus. If the CPU did not disable its output drivers into high impedance, both the CPU and DMA transceivers would drive conflicting voltage levels (e.g., CPU driving 0V while DMA drives 5V), creating a dead short-circuit that damages silicon drivers and causes severe bus data corruption.

  • Question 3: What is the primary operational trade-off between Burst Mode DMA and Cycle Stealing Mode DMA?
    Answer: Burst Mode delivers maximum raw data transfer throughput by eliminating repeated HOLD/HLDA arbitration overhead, but it monopolizes the memory bus and locks out the CPU, causing high interrupt latency. Cycle Stealing Mode incurs higher arbitration overhead because it renegotiates bus access per word, but it ensures high CPU responsiveness and allows concurrent instruction execution.

Common Interview Traps
  • Trap 1: Believing DMA Transfers Require Zero CPU Involvement.
    DMA eliminates CPU involvement during the data transfer itself. However, the CPU is still required to: (1) configure the DMA Address, Word Count, and Control registers prior to the transfer, and (2) execute an Interrupt Service Routine (ISR) when the DMA signals Terminal Count upon block completion.

  • Trap 2: Ignoring Word Count Register Overflow Limits.
    Engineers frequently assume a DMA controller can transfer an arbitrarily large file in one go. If a DMA controller has a 16-bit Word Count Register, its hardware counter rolls over at 65,536 units65,536\text{ units}. Transferring a larger buffer requires multiple segmented DMA operations or Scatter-Gather descriptor chaining.

  • Trap 3: Forgetting Cache Coherency during DMA Writes.
    Assuming that because data was successfully written to RAM by a DMA controller, the CPU application will read the correct data. If the CPU already holds the memory buffer in its L1/L2 cache, the CPU reads stale cache data unless the cache controller performs hardware bus snooping or the software driver explicitly invalidates the cache lines!


💬

Discussion & Doubts