DMA
Four letters arrive at UART0. The processor stays off the bus, yet the letters appear in SRAM. Follow the last letter from the moment UART0 lets go until SRAM takes it.
You Will Be Able To
- Tell describing a job to a DMA controller from the moving of its bytes.
- Find the two moments at which one byte changes hands.
- Decide which bytes are safe in memory after a copy that failed.
- Lesson
- S6.12
- Time
- 27 min
- Runs on
- Your browser
- Check
- 6 questions
You stop a test just as UART0 hands over the last letter it received. The letter is gone from UART0, but the last byte of the buffer in SRAM still holds zero. Have you lost the letter, or stopped one transfer too soon?
Before You Start
You need Drivers: what a DMA controller takes over from the processor. You need Many Managers: two managers share one bus, and an arbiter gives turns.
You need Registers: a read of a register can take the data out of a device.
| Question | |
|---|---|
| 1 | What are the two sides of a DMA controller? |
| 2 | What does software write to describe a job, and what does it write last? |
| 3 | Which address steps during a copy from a device to memory, and which stays? |
| 4 | UART0 has let go of the last byte. Is the copy finished? |
| 5 | A copy failed at its third byte. What is in memory, and where is the third byte? |
The Idea: A Block That Does the Copying Itself
A DMA controller is a block that copies bytes from one place to another by itself. DMA is short for direct memory access. This page is about that one block.
It has two sides. To software it is a device like any other: a few registers in which you describe a job. To the bus it is a manager like the processor: it sends addresses and makes reads and writes of its own. So during a copy the processor is free, and the bus is not.
The copy itself is plain. Read one byte from the source, hold it, write it to the destination, and repeat. The interesting moment is the middle one. Between the read and the write, the byte is in neither place. The source has let go, and the destination has not yet taken it.
Almost every DMA bug lives in that gap. "The device is empty" and "the data is in memory" are two different facts, and they become true at two different times.
New Words
| Word | Meaning |
|---|---|
| DMA controller | A block with registers on one side and a bus manager on the other. It copies bytes by itself. |
| Job description | The three things that describe one copy: where from, where to, how many bytes. One such copy is a job. |
| Job source | Where the bytes of a job are read from. Here: the receive data register of UART0. The source for short. |
| Job destination | Where the bytes of a job are written to. Here: four bytes of SRAM. The destination for short. |
| Held byte | The one byte inside the DMA controller between its read and its write. |
| Receive queue | The bytes that a device has received and that nobody has read yet. |
| Done flag | Set by the DMA controller after the last write of a job. Done for short. |
| Fault flag | Set by the DMA controller when a bus error stopped a job. Fault for short. |
| Offset | Name | What software writes |
|---|---|---|
| 0x00 | SRC | The address of the first byte to read |
| 0x04 | DST | The address of the first byte to write |
| 0x08 | LEN | The number of bytes |
| 0x0C | START | 1 in bit 0 starts the job. Software writes it last. |
The DMA controller is at base 0x10006000, on APB. Its interrupt is source 5.
One Picture: Describe, Read, Hold, Write, Report
- Processor
- Bus
- Peripheral
- Memory
- Describe the job
The processor writes three registers: where to read, where to write, how many bytes. Then it writes the start bit, last.
- Read one byte
The DMA controller waits until UART0 has a byte. Then it reads the data register itself, as a manager on the bus. UART0 lets go of the byte.
- Hold it
Between the read and the write, the byte is in neither UART0 nor SRAM. The DMA controller holds the only copy.
- Write that byte
The DMA controller writes the byte to SRAM. The next read uses the same device address. The next write uses the next memory address.
- Report the ending
After the last write the controller raises done. After a bus error it stops and raises fault. Either one can interrupt the processor.
Read the diagram as text
- Processor: describes the job, then does other work
- DMA controller: four registers, and a bus manager
- UART0 receive: the source: one data register
- SRAM: the destination: four bytes
- Processor → DMA controller: from, to, how many, start
- UART0 receive → DMA controller: read one byte
- DMA controller → SRAM: write that byte
- DMA controller → Processor: done, or fault
The DMA controller of ap_soc and one job: four received bytes from UART0 into SRAM. The lab below is this picture.
The Smallest Example, Worked by Hand
A job of two bytes: the letters o and k wait in the receive queue of a device. LEN is 2. Follow each transfer, and keep three counts.
| Transfer | What the DMA controller does | Bytes the device has let go of | Held byte | Bytes in memory |
|---|---|---|---|---|
| 1 | Reads the device data register: gets o | 1 | o | 0 |
| 2 | Writes o to the first address of the destination | 1 | none | 1 |
| 3 | Reads the same device address: gets k | 2 | k | 1 |
| 4 | Writes k to the next address of the destination | 2 | none | 2 |
Look at the row of transfer 3. The device has let go of both bytes and its queue is empty. Memory holds one. The k exists only as the held byte. A test that stops here and looks at memory sees a missing letter.
Notice the addresses as well. Transfers 1 and 3 read the same address, because a device hands out its next byte through one register. Transfers 2 and 4 write two different addresses, because memory must keep both.
The Rules
Rule 1: Software describes the job, and writes START last.
A start that comes first would run with the address or the length of the job before.
Rule 2: The DMA controller makes its own transfers.
It is a second manager. It shares the bus with the processor and waits for its turn.
Rule 3: One byte at a time: read, hold, write.
The controller waits for the device before each read.
Rule 4: A device address stays. A memory address steps by one.
The device hands out its next byte through the same register.
Rule 5: The job is done after the last write, not after the last read.
An empty device says who let go. It does not say that memory has the data.
Rule 6: A bus error stops the job at that byte.
Fault is set and done is not. The bytes written before it stay written.
Sources for These Rules
DMA, and who owns the data during a transfer: Patterson and Hennessy, Computer Organization and Design, ARM edition, section 6.9.
Managers, transfers, errors and a register access through the bridge: Arm AMBA AHB Protocol Specification, IHI 0033 issue C, sections 1.3, 3.1, 4.2 to 4.3 and 5.1.3. Arm AMBA APB Protocol Specification, IHI 0024 issue E, sections 3.1, 3.3 and 3.4.
All six rules describe our own DMA controller. Its four registers, the one held byte, the two flags and its interrupt source are our own choices for ap_soc.
Real DMA controllers hold more than one byte and have many channels. The two sides, and the gap between read and write, are common to them.
Now Run It
The lab at the end of this page copies four received letters, "send", from UART0 into SRAM. A bus-functional model stands in for the processor: test code that writes the four registers and then leaves the bus alone.
e18 cpu_request=0 read 1000000c byte=73 error=0
e20 cpu_request=0 write 80000000 byte=73 error=0
e23 cpu_request=0 read 1000000c byte=65 error=0
e25 cpu_request=0 write 80000001 byte=65 error=0
e28 cpu_request=0 read 1000000c byte=6e error=0
e30 cpu_request=0 write 80000002 byte=6e error=0
e33 cpu_request=0 read 1000000c byte=64 error=0
e35 cpu_request=0 write 80000003 byte=64 error=0
rx_ack=4 ram=73656e64 done=1 error=0Rule 2 at work: the field named cpu_request is 0 in every row. The processor asks for nothing, and still eight transfers complete. The DMA controller made them.
Rules 3 and 4 at work: e18 reads 1000000c and gets 73, the code of s. e20 writes 73 to 80000000. At e23 the read address is the same and the byte is 65, the e. The write addresses go 80000000, 80000001, 80000002, 80000003.
- Peripheral
- Bus
- Memory
A lit wire carries a 1. A pill is a transfer in that cycle.
- Cycle 0
Edge 18: UART0 lets go of the s. The DMA controller now holds the only copy.
- Cycle 1
Edge 20: the first byte of SRAM takes 73, the code of s.
- Cycle 6
Edge 33: the d has left UART0. One write is still owed.
- Cycle 7
Edge 35: SRAM takes the last byte. Only now is the job done.
Each column is one completed bus transfer of the lab on Icarus. The row named edge gives the clock edge. The columns are not evenly spaced in time.
Rule 5 at work: at e33 the d leaves UART0. Only at e35 does SRAM take it. The last row then says rx_ack=4, ram=73656e64 and done=1.
This lab leaves things out. The DMA controller here is test code, not a circuit you could build as it stands. The four letters are in the receive queue before the job starts. Done and fault stop at the controller: they are not sent on to the interrupt controller.
The Same Job From ROM
With a source in memory, both addresses step. This run copies the same four letters from ROM.
e29 cpu_request=0 read 00000003 byte=64 error=0
e31 cpu_request=0 write 80000003 byte=64 error=0
rx_ack=0 ram=73656e64 done=1 error=0The last read is at address 00000003, not at a fixed register. UART0 let go of nothing: rx_ack=0.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| START is a register of its own, written last | The job starts when LEN is written | The three other writes can come in any order. One more write for each job. |
| The controller is a bus manager | A private path from each device to memory | One block serves every device and all of memory. It competes with the processor for the bus. |
| Hold one byte | Hold many bytes | Almost no storage, and at most one byte is ever in the gap. Every byte costs a read and a write, one after the other. |
| Two endings, done and fault | One "finished" flag | Software knows whether it may use the whole buffer. Two causes to handle. |
| Wait for the device before each read | Read at a fixed pace | No byte is read twice and none is missed. The job lasts as long as the device takes. |
The Trap: The Last Byte, and the Failed Copy
Back to the stopped test. The receive queue is empty and the last byte of the buffer is zero. You saw the answer in the lab: e33 and e35 are two edges apart. Stop between them and UART0 is empty while SRAM is not yet full. Nothing was lost. Rule 5 is the lesson: check the last write, or done.
Now a harder case. The third read fails with a bus error. What may software believe?
e25 cpu_request=0 write 80000001 byte=65 error=0
e30 cpu_request=0 read 1000000c byte=00 error=1
rx_ack=2 ram=73650000 done=0 error=1Rule 6 at work: the read at e30 has error=1, and no write follows it. The totals say rx_ack=2, ram=73650000, done=0 and error=1. Two letters, s and e, are safe in memory. UART0 still has the n: a failed read did not take it.
An error on a write would be worse. By then the device has let go, and the held byte is the only copy. A driver that simply starts the job again reads the next byte, and one letter is gone. Before any retry, ask which side still owns the byte.
At Scale
This section looks ahead. A large chip has many DMA controllers, each with many channels, and the gap of this page is still there.
Many buffers for one job
A list in memory can describe several pieces: each entry gives an address and a length. The controller follows the list, so software need not restart it for each piece.
A share of the bus
Every byte needs two turns on the bus. Throughput, Latency and Back-Pressure asks whether the device can wait that long.
DMA and a cache
This item needs S0.7 first. A processor with a cache may hold an older copy of the buffer that the DMA controller just wrote. Software must throw that copy away before it reads the buffer. The multicore module returns to this.
After a fault
Log the source address, the destination address and the number of bytes done. Without them a retry is a guess.
Try It
Run the four-byte copy. Find the last read and the last write.
Change the length from 4 to 1, and find them again.
The lab has a variable named fault_at. Put fault_at=2; in front of log_moves=1; and the third read fails. This is the trap run of this page.
Change the first value given to program_channel to 0, to copy from ROM. Both addresses now step.
Names Printed by the Lab
| Name | Meaning |
|---|---|
| e18, e20 | The clock edge at which a transfer of the DMA controller completed. |
| cpu_request | 1 if the processor asked for the bus at that edge. |
| read, write | The kind of transfer, then its address. |
| byte | The byte that was read or written, in hexadecimal. |
| error | 1 if the bus reported an error for that transfer. |
| rx_ack | The number of bytes that UART0 has let go of. |
| ram | The four bytes of SRAM, first byte on the left. |
| done | 1 after the last write of a job. The other ending is fault, printed as the last error field. |
e18 cpu_request=0 read 1000000c byte=73 error=0 e20 cpu_request=0 write 80000000 byte=73 error=0 e23 cpu_request=0 read 1000000c byte=65 error=0 e25 cpu_request=0 write 80000001 byte=65 error=0 e28 cpu_request=0 read 1000000c byte=6e error=0 e30 cpu_request=0 write 80000002 byte=6e error=0 e33 cpu_request=0 read 1000000c byte=64 error=0 e35 cpu_request=0 write 80000003 byte=64 error=0 rx_ack=4 ram=73656e64 done=1 error=0Read included file: ahb_2m.sv
`timescale 1ns/1ns
// ahb_2m.sv: a two-manager interconnect with one shared bus to one subordinate (read-only).
// Each manager is a plain AHB-Lite manager with its own HREADY. The owner drives the shared address
// phase and sees the bus HREADY. A manager that wants the bus but does not own it sees HREADY low, so
// its address phase simply waits; an idle manager sees HREADY high.
// This bench's arbiter changes owner only at an edge where the bus HREADY is high and the owner's
// address phase is IDLE, round robin. It never changes owner while the bus is locked: after a cycle
// with HMASTLOCK and HREADY high, until a cycle with HMASTLOCK low and HREADY high (3.3).
// Switches: lock_ignored (the broken twin: the arbiter does not look at HMASTLOCK); trace.
module ahb_2m (
input logic hclk, hresetn,
// manager ports 0 and 1
input logic [1:0] htrans0, htrans1,
input logic [31:0] haddr0, haddr1, hwdata0, hwdata1,
input logic hwrite0, hwrite1, hmastlock0, hmastlock1,
input logic [2:0] hsize0, hsize1,
output logic hready0, hready1,
// the shared bus
output logic [1:0] s_htrans,
output logic [31:0] s_haddr, s_hwdata,
output logic s_hwrite, s_hmastlock,
output logic [2:0] s_hsize,
input logic s_hready);
bit lock_ignored = 0, trace = 0;
int cycle = -1;
logic own = 0, downer = 0; // who drives the address phase; whose data phase runs
assign s_htrans = own ? htrans1 : htrans0;
assign s_haddr = own ? haddr1 : haddr0;
assign s_hwrite = own ? hwrite1 : hwrite0;
assign s_hsize = own ? hsize1 : hsize0;
assign s_hmastlock = own ? hmastlock1 : hmastlock0;
assign s_hwdata = downer ? hwdata1 : hwdata0;
assign hready0 = !own ? s_hready : !htrans0[1]; // the other manager waits while it wants the bus
assign hready1 = own ? s_hready : !htrans1[1];
wire other_req = own ? htrans0[1] : htrans1[1];
wire locked = s_hmastlock && !lock_ignored;
always @(posedge hclk or negedge hresetn) begin
cycle = cycle + 1;
if (!hresetn) begin own <= 0; downer <= 0; end
else begin
if (s_hready) begin
downer <= own;
if (s_htrans == 2'b00 && !locked && other_req) begin
own <= !own;
if (trace) $display("arb e%0d: bus to manager %0d", cycle, !own);
end
end
end
end
endmodule
Read included file: ahb_apb_bridge.sv
`timescale 1ns/1ns
// ahb_apb_bridge.sv: an AHB subordinate that turns each accepted transfer into one APB transfer
// (read-only). The address phase is registered when HSEL, HREADY and a NONSEQ or SEQ transfer are
// all present. During SETUP and unfinished ACCESS the bridge keeps HREADYOUT low. A successful APB
// completion ends the AHB data phase with OKAY and HRDATA = PRDATA. A failed APB completion starts
// the two-cycle ERROR (HREADYOUT low then high, HRESP high in both). HWDATA and HWSTRB are data-phase signals the manager holds
// through the wait states, so PWDATA and PSTRB are driven from them directly; PADDR is the aligned
// word address (IHI 0024E 2.1.1 leaves an unaligned PADDR unpredictable). HPROT maps onto PPROT:
// privileged from HPROT[1], instruction from HPROT[0] = 0, and the access is Secure.
// A write therefore costs one setup and one access cycle: two cycles of data phase with no APB wait.
module ahb_apb_bridge (
input logic hclk,
input logic hresetn,
input logic hsel,
input logic [31:0] haddr,
input logic [1:0] htrans,
input logic hwrite,
input logic [3:0] hprot,
input logic [31:0] hwdata,
input logic [3:0] hwstrb,
input logic hready,
output logic hreadyout,
output logic hresp,
output logic [31:0] hrdata,
output logic psel,
output logic penable,
output logic pwrite,
output logic [31:0] paddr,
output logic [31:0] pwdata,
output logic [3:0] pstrb,
output logic [2:0] pprot,
input logic pready,
input logic [31:0] prdata,
input logic pslverr);
localparam int IDLE = 0, SETUP = 1, ACCESS = 2, ERR1 = 3, ERR2 = 4;
bit trace = 0;
int st = IDLE;
logic wr = 0;
logic [31:0] a = 0;
logic [2:0] prot = 0;
wire take = hsel && hready && htrans[1];
assign psel = (st == SETUP || st == ACCESS);
assign penable = (st == ACCESS);
assign pwrite = wr;
assign paddr = {a[31:2], 2'b00}; // APB leaves an unaligned PADDR unpredictable: the word, and PSTRB picks the bytes
assign pwdata = wr ? hwdata : 32'h0;
assign pstrb = wr ? hwstrb : 4'h0;
assign pprot = prot;
assign hreadyout = (st == IDLE || st == ERR2) ? 1'b1 : (st == ACCESS) ? (pready === 1'b1 && pslverr !== 1'b1) : 1'b0;
assign hresp = (st == ERR1 || st == ERR2);
assign hrdata = (st == ACCESS) ? prdata : 32'h0;
always @(posedge hclk or negedge hresetn) begin
if (!hresetn) st <= IDLE;
else begin
case (st)
SETUP: st <= ACCESS;
ACCESS: if (pready === 1'b1) begin
if (trace) $display("apb e%0d: %0s 0x%03h%0s", ($time - 5) / 10, wr ? "write" : "read", a[11:0], pslverr === 1'b1 ? " PSLVERR" : "");
st <= (pslverr === 1'b1) ? ERR1 : IDLE;
end
ERR1: st <= ERR2;
default: st <= IDLE;
endcase
// A new address phase can be taken whenever HREADY is 1: in IDLE, in the last cycle of ERROR,
// or at the edge an access completes with OKAY.
if (take && (st == IDLE || st == ERR2 || (st == ACCESS && pready === 1'b1 && pslverr !== 1'b1))) begin
wr <= hwrite; a <= haddr; prot <= {~hprot[0], 1'b0, hprot[1]}; st <= SETUP;
end
end
end
endmodule
Read included file: s6_io_slice.svh
// Simulation fixture. A transfer task stands in for each manager.
// P4 supplies the two-manager arbitration point and APB bridge.
logic clk = 0, reset_n = 0;
always #5 clk = ~clk;
localparam logic [31:0] UART = 'h10000000, STATUS = UART+4, MASK = UART+8,
RX = UART+12, CFG = 'h10006000, RAM = 'h80000000;
logic [31:0] addr[2], data[2];
logic [1:0] trans[2];
logic wr[2];
logic [2:0] size[2];
wire ready[2];
wire [31:0] address, payload, paddr, pwdata, bridge_data;
wire [1:0] transfer;
wire [2:0] bus_size;
wire write_en, bridge_ready, bridge_error, psel, penable, pwrite;
ahb_2m matrix (
.hclk(clk), .hresetn(reset_n),
.htrans0(trans[0]), .htrans1(trans[1]), .haddr0(addr[0]), .haddr1(addr[1]),
.hwrite0(wr[0]), .hwrite1(wr[1]), .hwdata0(data[0]), .hwdata1(data[1]),
.hmastlock0(1'b0), .hmastlock1(1'b0), .hsize0(size[0]), .hsize1(size[1]),
.hready0(ready[0]), .hready1(ready[1]), .s_htrans(transfer), .s_haddr(address),
.s_hwdata(payload), .s_hwrite(write_en), .s_hmastlock(), .s_hsize(bus_size),
.s_hready(bridge_ready)
);
logic [7:0] rom[0:3], ram[0:3];
logic [31:0] saved_addr;
logic saved_write, saved_active;
wire [31:0] rom_word = {rom[3],rom[2],rom[1],rom[0]};
wire [31:0] bus_read_data = saved_addr < 4 ? rom_word : bridge_data;
wire peripheral = address[31:16] == 16'h1000;
logic [31:0] src, dst, length;
logic launch, mask;
logic done = 0, fault = 0;
int cooldown, gap = 12, rx_count, fault_at = -1;
int cycle = 0, busy_cycles = 0;
bit charge_cpu, log_moves, trace_access;
logic [31:0] console_text;
int tx_count, cpu_at_tx[4];
wire room = cooldown == 0;
wire irq = mask && room;
wire rx_request = rx_count < 4;
wire rx_fault = paddr == RX && rx_count == fault_at;
wire [31:0] prdata = paddr == STATUS ? {31'b0,room} :
paddr == RX && rx_request ? {24'b0,rom[rx_count]} : 0;
ahb_apb_bridge bridge (
.hclk(clk), .hresetn(reset_n), .hsel(peripheral), .haddr(address),
.htrans(transfer), .hwrite(write_en), .hprot(4'b0011), .hwdata(payload),
.hwstrb(4'hf), .hready(bridge_ready), .hreadyout(bridge_ready),
.hresp(bridge_error), .hrdata(bridge_data), .psel(psel), .penable(penable),
.pwrite(pwrite), .paddr(paddr), .pwdata(pwdata), .pstrb(), .pprot(),
.pready(1'b1), .prdata(prdata), .pslverr(rx_fault)
);
always @(posedge clk) begin
cycle <= cycle + 1;
if (trace_access && (transfer[1] || psel))
$display("access e%0d active=%b setup=%b access=%b ready=%b",cycle,
transfer[1],psel && !penable,psel && penable,bridge_ready);
if (charge_cpu) busy_cycles <= busy_cycles + 1;
if (bridge_ready) begin
saved_active <= transfer[1];
saved_addr <= address;
saved_write <= write_en;
end
if (saved_active && saved_write && saved_addr >= RAM && saved_addr < RAM+4)
ram[saved_addr-RAM] <= payload[8*saved_addr[1:0] +: 8];
launch <= 0;
if (!reset_n) begin
cooldown <= 0;
rx_count <= 0;
console_text <= 0;
tx_count <= 0;
mask <= 0;
end else begin
if (!room) cooldown <= cooldown-1;
if (psel && penable && !rx_fault) begin
if (pwrite) case (paddr)
UART: begin
if (!room) $fatal(1,"TX register full");
if (tx_count < 4) cpu_at_tx[tx_count] <= busy_cycles + int'(charge_cpu);
tx_count <= tx_count + 1;
console_text <= {console_text[23:0],pwdata[7:0]};
cooldown <= gap;
end
MASK: mask <= pwdata[0];
CFG: src <= pwdata;
CFG+4: dst <= pwdata;
CFG+8: length <= pwdata;
CFG+12: launch <= pwdata[0];
endcase
else if (paddr == RX && rx_request) rx_count <= rx_count+1;
end
end
end
// All bus drives change away from the active edge. Count address arbitration,
// address acceptance and every data-phase wait inside the CPU charge interval.
task automatic access_bus(input int m, input bit writing, input logic [31:0] a, d,
output logic [31:0] result, output bit failed);
@(negedge clk);
if (m == 0) charge_cpu = 1;
addr[m] = a;
wr[m] = writing;
size[m] = a[31:16] == 16'h1000 ? 2 : 0;
data[m] = d;
trans[m] = 2'b10;
do @(posedge clk); while (!ready[m]);
@(negedge clk);
trans[m] = 0;
do @(posedge clk); while (!bridge_ready);
failed = bridge_error;
result = a < 4 ? {24'b0,bus_read_data[8*a[1:0] +: 8]} : bus_read_data;
if (log_moves && m == 1)
$display("e%0d cpu_request=%b %s %08h byte=%02h error=%b",cycle,trans[0][1],
writing ? "write" : " read",a,writing ? d[8*a[1:0] +: 8] : result[7:0],failed);
#1;
if (m == 0) charge_cpu = 0;
endtask
task automatic cpu_write(input logic [31:0] a, d);
logic [31:0] unused;
bit failed;
access_bus(0,1,a,d,unused,failed);
if (failed) $fatal(1,"control write failed");
endtask
task automatic program_channel(input logic [31:0] from_addr,to_addr,count);
if (!(from_addr == RX || from_addr < 4) ||
!(to_addr == UART || (to_addr >= RAM && to_addr < RAM+4)))
$fatal(1,"Use the four-byte ROM or UART RX and the four-byte RAM or UART TX");
if (count > 4 || (from_addr == RX && count > 4-rx_count) ||
(from_addr < 4 && count > 4-from_addr) ||
(to_addr != UART && count > RAM+4-to_addr))
$fatal(1,"Job would exceed the fixture buffer");
cpu_write(CFG,from_addr);
cpu_write(CFG+4,to_addr);
cpu_write(CFG+8,count);
cpu_write(CFG+12,1);
endtask
// Behavioral transfer agent, for the documented ROM/RX and RAM/TX routes.
// ROM reads below return a byte extracted from its addressed lane.
initial forever begin : channel
logic [31:0] a,b,value,unused,byte_count;
bit failed;
@(posedge launch);
done = 0;
fault = 0;
a = src;
b = dst;
byte_count = length;
for (int i=0; i<byte_count && !fault; i++) begin
if (a == RX) wait(rx_request);
if (b == UART) wait(room);
access_bus(1,0,a,0,value,failed);
if (!failed)
access_bus(1,1,b,{4{value[7:0]}},unused,failed);
fault = failed;
if (a != RX) a++;
if (b != UART) b++;
end
done = !fault;
end
task automatic initialize;
for (int m=0; m<2; m++) begin
trans[m]=0; wr[m]=0; addr[m]=0; data[m]=0; size[m]=0;
end
{rom[0],rom[1],rom[2],rom[3]} = "send";
for (int i=0; i<4; i++) ram[i]=0;
repeat (2) @(negedge clk);
reset_n=1;
endtask
Check Yourself
Common Mistakes
Wrong Step the address of the device data register.
The next byte comes through the same register.
Fix Step the memory address, and keep the device address fixed.
Wrong Report success when the device is empty.
The last write may still be owed, and it can fail.
Fix Wait for done, which follows the last write.
Wrong Start a failed copy again from the beginning.
A device data register gives each byte only once.
Fix Find the last byte that reached the destination, and who owns the next one.
Remember
- A DMA controller has registers for software and is a manager on the bus.
- Software describes a job with a source, a destination and a length, and writes the start bit last.
- Each byte is read, held and written. In between, the controller has the only copy.
- The job is done after the last write. After a fault, find out who owns the next byte before a retry.