One Access, End to End
Follow one read from the processor out to a pin register and back. One address picks the path, and one clock edge makes the value count.
You Will Be Able To
- Split a device address into a region and a register offset.
- Name every hop of a device read, and the edges that take the address and return the value.
- Count the clock periods of a read, and say where each one goes.
- Lesson
- S1.2
- Time
- 24 min
- Runs on
- Your browser
- Check
- 5 questions
A switch is high at a pin of ap_soc, yet your probe shows zero on the bus. The chip seems broken. It may only be that you looked one clock edge too early.
Before You Start
You need the region, base and offset of S0.2. You need the two phases of APB from S0.4. You need AHB and the bridge from S0.5, and the blocks of ap_soc from S1.1.
| Question | |
|---|---|
| 1 | How does one address name both a device and a register inside it? |
| 2 | Which blocks does a read of a device register pass, on the way out and on the way back? |
| 3 | Which clock edge takes the address, and which edge returns the value? |
| 4 | Why can the right value sit on the wires before the read has happened? |
| 5 | How many clock periods does the read cost, and where does each one go? |
The Idea: One Read Is One Round Trip
A program wants to know whether a switch is pressed. To the processor, the switch is a number at an address. One load instruction asks for that number. Everything after that is the work of the rest of the chip.
The earlier lessons taught each piece alone: the memory map, APB, AHB and the bridge. This page joins them. One address travels out through four blocks. One value travels back the same way. Call the whole round trip one access, as before.
Two things are worth learning from the trip. The first is how the address steers itself: its high bits choose the path, and its low bits choose the register. The second is timing. The value appears on the wires early, and it counts at one edge only.
New Words
| Word | Meaning |
|---|---|
| Input register | A device register whose value comes from outside the chip. Software can read it and cannot set it. |
| HSEL | The select wire that the AHB decoder raises for one subordinate. Here that subordinate is the bridge. |
| Return path | The wires that carry the read value and the "done" signal back to the manager. |
| Latency | The number of clock periods from the edge that takes the address to the edge that returns the value. |
One more name from the earlier lessons matters here. The edge where a transfer happens is the transfer edge. An access through the bridge has two of them, one on each bus.
One Picture: Out and Back
- Processor
- Bus
- Peripheral
- Pads
- The request leaves
A load instruction needs the word at 0x1000_1004. The manager puts that address on AHB in an address phase.
- The decoder picks the bridge
Every address that starts 0x1000 belongs to a device register. So the AHB decoder selects the bridge.
- The bridge keeps the address
The bridge stores the address it took. The manager is free to change the AHB address wires.
- One APB transfer
The bridge selects GPIO and runs a setup phase and an access phase. GPIO may add wait states.
- The value returns
At the APB transfer edge, the pin word crosses the bridge. The same edge ends the AHB data phase.
Read the diagram as text
- Manager: the processor; in the lab, test code plays it
- AHB decoder: compares the high address bits and selects one subordinate
- Bridge: an AHB subordinate, and the APB requester
- GPIO: an APB completer; its register at offset 4 reports the pins
- Pins: sixteen pins of the chip, wired to switches
- Manager → AHB decoder: address 0x1000_1004
- AHB decoder → Bridge: select
- Bridge → GPIO: setup, then access
- Pins → GPIO: pin levels
- GPIO → Bridge: PRDATA and PREADY
- Bridge → Manager: HRDATA and HREADY
Each arrow stands for a group of wires. The way back carries a value and a "done" wire together.
The manager sees none of the middle. It puts an address on AHB and waits for HREADY, exactly as it does for memory.
The Smallest Example, Worked by Hand: One Address, Two Answers
Take the address 0x1000_1004 and the four regions of the ap_soc map that the foundation lessons used.
| Question | How to answer it | Answer |
|---|---|---|
| Which region? | Find the region whose base is at or below the address, and whose end is above it. | GPIO: 0x1000_1000 to 0x1000_1FFF |
| Which offset? | Subtract the base of the region from the address. | 0x1000_1004 - 0x1000_1000 = 4 |
| Which register? | A register is four bytes wide. Divide the offset by four. | register 1, the second one |
| Which AHB subordinate? | Every address that starts 0x1000 is a device register. | the bridge |
In this chip, GPIO register 1 is an input register. It reports sixteen pins in the low half of the word. Now count the trip by hand, for a GPIO that asks for one wait state.
| Edge | What ends at this edge | Bus |
|---|---|---|
| first | the AHB address phase: the address is taken | AHB |
| second | the APB setup phase | APB |
| third | an access cycle with PREADY at 0: a wait state | APB |
| fourth | the access cycle with PREADY at 1: the read happens | both |
Four edges are marked, and three clock periods lie between the first and the last. So the latency is three periods.
The Rules
Rule 1: One address answers two questions.
The region that contains it chooses the device. The offset inside the region chooses the register.
Rule 2: The address is taken at one edge.
It is the edge that sees HTRANS at NONSEQ, HSEL at 1 and HREADY at 1. The bridge keeps its own copy from then on.
Rule 3: The bridge turns the AHB data phase into one APB transfer.
It runs a setup phase and an access phase, and holds HREADY at 0 until the APB transfer has happened.
Rule 4: The read happens at one edge.
On APB it is the edge with PSEL, PENABLE and PREADY all at 1. On this bridge the same edge sees HREADY at 1.
Rule 5: A value seen before that edge promises nothing.
The read data wires may show the final value early. They may also change again. Only the transfer edge makes the value count.
Sources for These Rules
Arm AMBA AHB Protocol Specification, document IHI 0033, issue C. The two phases: section 3.1. The select signal, and when a subordinate samples it: section 4.2. The read data multiplexor: section 4.3.
Arm AMBA APB Protocol Specification, document IHI 0024, issue E. Read transfers and wait states: section 3.3. The operating states: section 4.1.
A device reached through addresses: Harris and Harris, Digital Design and Computer Architecture, ARM edition, section 9.2.
The region, the offset, the pin word and the edge count describe ap_soc and this lab. A different bridge may add a cycle on the way back.
Now Run It
The lab at the end of this page runs this read on the bridge and the GPIO register of ap_soc. The manager is test code again, a bus-functional model. The pins are set to the pattern 0x8042, and GPIO is told to add one wait state.
| Edge | HADDR | HTRANS | HREADY | HRDATA | PSEL | PENABLE | PREADY | PRDATA |
|---|---|---|---|---|---|---|---|---|
| e0 | 10001004 | NONSEQ | 1 | 00000000 | 0 | 0 | 0 | 00000000 |
| e1 | 00000000 | IDLE | 0 | 00000000 | 1 | 0 | 0 | 00008042 |
| e2 | 00000000 | IDLE | 0 | 00008042 | 1 | 1 | 0 | 00008042 |
| e3 | 00000000 | IDLE | 1 | 00008042 | 1 | 1 | 1 | 00008042 |
Recorded from the lab. Each row is what one rising clock edge sees. Addresses and data are in hexadecimal. The lab prints these columns under shorter names: req, sel, enable and ready.
- Cycle 0
The address phase. HTRANS asks for a transfer and HREADY is 1, so this edge takes the address.
- Cycle 1
The APB setup phase: PSEL is 1 and PENABLE is 0. HREADY is 0, so AHB waits. PRDATA already shows the pins.
- Cycle 2
The access phase, with PREADY at 0: a wait state. HRDATA shows the value, and it still does not count.
- Cycle 3
PSEL, PENABLE and PREADY are all 1, and HREADY is 1. This is the edge where the read happens.
Built from the recorded rows. The HTRANS lane is 1 for NONSEQ and 0 for IDLE.
Rule 2 at work: e0 sees NONSEQ with HREADY at 1, so e0 takes the address. At e1 the manager has already moved HADDR back to zero. The bridge still knows where the read is going.
Rule 3 at work: e1 is the setup phase and e2 is an access cycle that waits. HREADY is 0 at both. Rule 4 at work: e3 is the only row with PSEL, PENABLE and PREADY all at 1.
Rule 5 at work: PRDATA shows 00008042 from e1, and HRDATA shows it from e2. A probe that trusts those rows is right by luck. The last line of the lab, manager received=00008042, is the value taken at e3.
Decode the word. In binary, 0x8042 has bits 15, 6 and 1 set, so three pins are high. Flip a switch and the same address returns another word. That is what makes it an input register and not memory.
This lab leaves things out. Its processor is test code, and it treats the pins as steady digital values. A real pin can change at any moment, and later lessons deal with that.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| The bridge keeps a copy of the address | The manager holds the address for the whole access | AHB can show its next address at once. The bridge needs a register for the address and the direction. |
| The decoder and the return path hold no register | A register at each hop | They add no clock period: they finish inside a cycle already paid for. Their delay limits how fast the clock can run. |
| One "done" edge for the value | Take the value as soon as it is not zero | A read of zero is as valid as any other. The manager must watch a second wire. |
The second row answers a common question. Not every box in the picture costs a clock period. Count periods between edges, never boxes.
The Trap: Looking One Edge Early
An engineer probes HRDATA during a read of the switches and sees zero. The report says the chip is broken. A colleague looks one edge later, sees 00008042, and reports it fixed. Nothing was repaired in between.
Both looked at e1 or e2 of the capture above. Both read data wires without the handshake. The first saw a value that had not arrived. The second saw the right value one edge before it counted, and would be wrong on a slower device.
The cure is one habit. Find the transfer edge first, with HREADY on AHB or the three APB wires. Only then read the data column, in that row alone.
At Scale
This section looks ahead. The read above had the bus to itself. On a full chip the same read can take longer, and you must find which part of it grew.
Before the address is taken
A second manager may be using the bus. Then the read waits before its address phase ends. The lesson on many managers covers that wait.
Inside the device
Count the access cycles with PREADY at 0. More of them means the delay is in the device.
On the way back
Compare the APB transfer edge with the edge that ends the AHB data phase. Here they are the same edge. A bridge with a register on its return path adds one.
Which device was asked
Record the address at the edge that takes it. During a wait, the AHB address wires may already describe the next transfer.
Try It
Run the read. Find two rows with the same hrdata and different hready. Say which row the manager may use.
Set waits to 0 and run again. Name the phase of every row. The value should arrive one edge sooner.
Change pins to 16'h0000. The read now returns zero. Use rule 4 to show that it still happened.
e0 req=1 window=1 sel=0 gpio=0 uart=0 enable=0 ready=0 hready=1 prdata=00000000 hrdata=00000000 pwdata=00000000 haddr=10001004 paddr=00000000 hresp=0 e1 req=0 window=0 sel=1 gpio=1 uart=0 enable=0 ready=0 hready=0 prdata=00008042 hrdata=00000000 pwdata=00000000 haddr=00000000 paddr=10001004 hresp=0 e2 req=0 window=0 sel=1 gpio=1 uart=0 enable=1 ready=0 hready=0 prdata=00008042 hrdata=00008042 pwdata=00000000 haddr=00000000 paddr=10001004 hresp=0 e3 req=0 window=0 sel=1 gpio=1 uart=0 enable=1 ready=1 hready=1 prdata=00008042 hrdata=00008042 pwdata=00000000 haddr=00000000 paddr=10001004 hresp=0 manager received=00008042Read 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: io_lab_regs.sv
// Two word registers for the S1 labs. Inputs have already crossed into PCLK.
// The UART model exposes accepted bytes; it does not model the serial shifter.
module io_lab_regs (
input logic pclk, presetn,
input logic psel, penable, pwrite,
input logic [31:0] paddr, pwdata,
input logic [3:0] pstrb,
input logic [15:0] pins,
input logic [31:0] wait_edges,
output logic pready, pslverr,
output logic [31:0] prdata,
output logic uart_selected, gpio_selected,
output logic tx_accepted,
output logic [7:0] tx_byte
);
localparam logic [19:0] UART_PAGE = 20'h10000, GPIO_PAGE = 20'h10001;
logic [31:0] elapsed;
wire tx_word = uart_selected && paddr[11:0] == 12'h000 && pwrite;
wire input_word = gpio_selected && paddr[11:0] == 12'h004 && !pwrite;
assign uart_selected = psel && paddr[31:12] == UART_PAGE;
assign gpio_selected = psel && paddr[31:12] == GPIO_PAGE;
assign prdata = input_word ? {16'b0, pins} : 32'b0;
assign pready = psel && penable && elapsed >= wait_edges;
assign pslverr = pready && !(tx_word || input_word);
assign tx_accepted = tx_word && pready && pstrb[0];
assign tx_byte = pwdata[7:0];
always_ff @(posedge pclk or negedge presetn) begin
if (!presetn) elapsed <= 0;
else if (psel && penable && !pready) elapsed <= elapsed + 1;
else elapsed <= 0;
end
endmodule
Read included file: io_lab.svh
// Include inside the lesson's testbench. One manager, one clock, aligned words.
// The exercise drives only the UART TX and GPIO input registers.
`ifndef AP_SOC_EXTERNAL_HCLK
logic hclk = 0;
always #5 hclk = ~hclk;
`endif
logic hresetn = 0;
logic [31:0] haddr = 0, hwdata = 0;
logic [1:0] htrans = 0;
logic hwrite = 0;
wire hsel = haddr[31:16] == 16'h1000;
wire hready, hresp, psel, penable, pwrite;
wire [31:0] hrdata, paddr, pwdata;
wire [3:0] pstrb;
localparam logic [31:0] TX = 32'h1000_0000, INPUT_WORD = 32'h1000_1004;
logic [15:0] pins = 0; // Stable, already synchronized GPIO inputs.
integer waits = 0;
wire pready, pslverr, uart_selected, gpio_selected, tx_accepted;
wire [31:0] prdata;
wire [7:0] tx_byte;
io_lab_regs registers (
.pclk(hclk), .presetn(hresetn), .psel(psel), .penable(penable),
.pwrite(pwrite), .paddr(paddr), .pwdata(pwdata), .pstrb(pstrb),
.pins(pins), .wait_edges(waits), .pready(pready), .pslverr(pslverr),
.prdata(prdata), .uart_selected(uart_selected), .gpio_selected(gpio_selected),
.tx_accepted(tx_accepted), .tx_byte(tx_byte)
);
ahb_apb_bridge bridge (
.hclk(hclk), .hresetn(hresetn), .hsel(hsel), .haddr(haddr),
.htrans(htrans), .hwrite(hwrite), .hprot(4'b0011), .hwdata(hwdata),
.hwstrb(4'hf), .hready(hready), .hreadyout(hready), .hresp(hresp),
.hrdata(hrdata), .psel(psel), .penable(penable), .pwrite(pwrite),
.paddr(paddr), .pwdata(pwdata), .pstrb(pstrb), .pprot(),
.pready(pready), .prdata(prdata), .pslverr(pslverr)
);
logic [39:0] console_text = 0;
bit trace_bus = 0;
int cycle = 0;
always @(posedge hclk) begin
if (trace_bus) begin
$display("e%0d req=%b window=%b sel=%b gpio=%b uart=%b enable=%b ready=%b hready=%b prdata=%08h hrdata=%08h pwdata=%08h haddr=%08h paddr=%08h hresp=%b",
cycle, htrans[1], hsel, psel, gpio_selected, uart_selected, penable,
pready, hready, prdata, hrdata, pwdata, haddr, paddr, hresp);
cycle++;
end
if (tx_accepted) begin
console_text = {console_text[31:0], tx_byte};
$display("UART TX byte=%02h character=%c", tx_byte, tx_byte);
end
end
task automatic access_word(input bit writing, input logic [31:0] address,
input logic [31:0] data, output logic [31:0] result);
if (address[31:16] != 16'h1000 || address[1:0] != 0)
$fatal(1, "This lab models aligned words in the APB window");
@(negedge hclk);
haddr = address;
hwrite = writing;
htrans = 2'b10;
@(negedge hclk); // The address was accepted on the intervening rising edge.
htrans = 0;
haddr = 0;
hwdata = data;
do @(posedge hclk); while (!hready);
if (hresp) $fatal(1, "Access failed at %08h", address);
result = hrdata;
#1;
endtask
Check Yourself
Common Mistakes
Wrong Treat offset 4 as the fourth register.
An address counts bytes, and each register here is four bytes wide.
Fix Divide the offset by four, and count registers from zero.
Wrong Take the first value on the read data wires that is not zero.
The wires can show a value before the transfer edge, and zero can be the true value.
Fix Take the value at the transfer edge, whatever it is.
Wrong Charge one clock period for each box in the picture.
A decoder and a return path with no register finish inside a cycle already counted.
Fix Count the periods between the edge that takes the address and the transfer edge.
Remember
- The region of an address chooses the device; the offset chooses the register.
- The address is taken at the edge that ends the AHB address phase; the bridge keeps a copy.
- The bridge turns one AHB data phase into an APB setup phase and an access phase.
- A read value counts only at the transfer edge, even when it appears earlier.
- Latency is counted in clock periods between edges, not in boxes.