Bridges: From the Fast Bus to APB

The bridge of S0.5 was three lines. A real one is a small state machine. It keeps an address and hands over a write exactly once. It also carries a failure back to a bus that has already moved on.

You Will Be Able To

  • Walk the five states of an AHB-to-APB bridge for a good transfer and for a failed one.
  • Say what the bridge keeps, what the manager holds, and on which edge a write counts.
  • Explain how a one-edge APB error becomes a two-cycle AHB error.
Lesson
S4.5
Time
26 min
Runs on
Your browser
Check
5 questions

A program writes one exclamation mark to UART0. The console prints a row of them. Either the program ran many times, or one write was taken more than once on its way across the bridge.

Before You Start

You need the three APB states of S0.4, and the small bridge of S0.5. S0.5 also explains why a chip has two buses. You need HRESP and the two cycles of an error from The Default Subordinate.

Five questions this page answers
Question
1Which states does a bridge pass through for one transfer?
2What does the bridge keep in registers, and what must the manager hold still?
3On which single clock edge does a write reach the device?
4How does an APB error become an AHB error, and why does that need two more states?
5What does one slow device cost the other devices behind the same bridge?

The Idea: A Bridge Remembers

The two buses disagree about time. AHB shows an address for one cycle and moves on. APB wants the same address on its wires for a whole transfer. That is a setup cycle, then access cycles until the device is done. So the bridge must remember the address.

They also disagree about failure. On APB, a device reports an error on one wire at the one edge where the transfer ends. Then it lets go. AHB wants an error to last two cycles. So the bridge must remember the failure too.

Both memories fit in one small state machine. Its first three states are the APB states you know: idle, setup and access. Two more states exist only to give AHB its two cycles of error.

New Words

New words on this page
WordMeaning
PSLVERRThe APB error wire. It counts only at the edge where the transfer ends. 1 means the transfer failed.
ERR1A state of the bridge: the first cycle of the AHB error.
ERR2A state of the bridge: the second cycle of the AHB error.
TranslateTo report on one bus, in that bus's own form, what happened on the other.

The lab on this page prints short names for the wires. Here is what each one stands for.

Lab names
In the labOn the busMeaning
UARTthe PSEL of UART01 while UART0 is selected
GPIOthe PSEL of GPIO1 while GPIO is selected
ENPENABLE0 in the setup phase, 1 in the access phase
PWPWDATAthe value being written
PRDYPREADY1 when the device is done
PERRPSLVERR1 when the transfer failed
HRDYHREADY1 when the AHB data phase ends
ERRHRESP1 for an AHB error

One Picture: The Five States

The Five States of the BridgeStep through the picture. It follows one good write, and then one that fails.
  • Bus
The Five States of the BridgeStep through the picture. It follows one good write, and then one that fails. component IDLE PSEL 0. HREADY 1:an address may betaken component SETUP PSEL 1, PENABLE 0.HREADY 0. Lasts onecycle component ACCESS PSEL 1, PENABLE 1.Stays here whilePREADY is 0 component ERR1 PSEL 0. HRESP 1,HREADY 0: firstcycle of the error component ERR2 PSEL 0. HRESP 1,HREADY 1: secondcycle of the error an address is takenalwaysPREADY 1, PSLVERR0PREADY 1, PSLVERR1alwaysalways
  1. component IDLEPSEL 0. HREADY 1: an address may be taken
    • an address is taken → SETUP
  2. component SETUPPSEL 1, PENABLE 0. HREADY 0. Lasts one cycle
    • always → ACCESS
  3. component ACCESSPSEL 1, PENABLE 1. Stays here while PREADY is 0
    • PREADY 1, PSLVERR 0 → IDLE
    • PREADY 1, PSLVERR 1 → ERR1
  4. component ERR1PSEL 0. HRESP 1, HREADY 0: first cycle of the error
    • always → ERR2
  5. component ERR2PSEL 0. HRESP 1, HREADY 1: second cycle of the error
    • always → IDLE
  1. Take the address

    In IDLE the bridge answers HREADY at 1. When the decoder selects it for a transfer, it stores the address and the direction, and goes to SETUP.

  2. Setup, then access

    SETUP is the APB setup phase. It lasts one cycle. ACCESS is the access phase. The bridge stays in it for as long as the device holds PREADY at 0.

  3. A good ending

    PREADY is 1 and PSLVERR is 0. The APB transfer happens at this edge. The bridge answers HREADY at 1 in the same cycle, so the AHB transfer happens too.

  4. A bad ending

    PREADY is 1 and PSLVERR is 1. The APB transfer is over, and it failed. The bridge keeps HREADY at 0 and goes to ERR1.

  5. Two cycles of error

    The device has already let go. The bridge remembers the failure in its state: ERR1 gives the first cycle of the AHB error, and ERR2 the second.

Read the diagram as text
  • IDLE: PSEL 0. HREADY 1: an address may be taken
  • SETUP: PSEL 1, PENABLE 0. HREADY 0. Lasts one cycle
  • ACCESS: PSEL 1, PENABLE 1. Stays here while PREADY is 0
  • ERR1: PSEL 0. HRESP 1, HREADY 0: first cycle of the error
  • ERR2: PSEL 0. HRESP 1, HREADY 1: second cycle of the error
  • IDLE → SETUP: an address is taken
  • SETUP → ACCESS: always
  • ACCESS → IDLE: PREADY 1, PSLVERR 0
  • ACCESS → ERR1: PREADY 1, PSLVERR 1
  • ERR1 → ERR2: always
  • ERR2 → IDLE: always

If another address is taken at the edge that leaves ACCESS or ERR2, the bridge goes straight to SETUP. Those two arrows are not drawn.

Compare this with the three states of APB in S0.4. IDLE, SETUP and ACCESS are the same. The bridge runs that machine, as the APB requester, and adds the two error states for its AHB side.

The Smallest Example, Worked by Hand: Two Writes, State by State

Follow one write to a device that answers at once. Write down the state, the two APB wires and what AHB is told.

A write that succeeds
CycleStatePSELPENABLEHREADYWhat happens at the edge that ends the cycle
1IDLE001the address is taken and stored
2SETUP100nothing: setup is never the transfer
3ACCESS111the device takes the value; the AHB transfer ends

The AHB data phase is cycles 2 and 3. So a device access costs AHB one wait state at least, as S0.5 found. Now let the device refuse the write.

A write that fails
CycleStatePSLVERRHREADYHRESPWhat AHB sees
1IDLE010the address is taken
2SETUP000a wait state
3ACCESS100still a wait state: the device has failed, AHB has not been told
4ERR1001the first cycle of the error
5ERR2011the second cycle: the transfer ends as an error

PSLVERR is 1 in cycle 3 only. HRESP is 1 in cycles 4 and 5. The two never overlap. Whatever joins them must be stored.

The Rules

  1. Rule 1: The bridge takes an address when it is selected for a transfer and HREADY is 1.

    It stores the address and the direction. From then on the APB address comes from that store, not from AHB.

  2. Rule 2: SETUP lasts one cycle. ACCESS lasts until PREADY is 1.

    The bridge answers HREADY at 0 through SETUP and through every ACCESS cycle that waits.

  3. Rule 3: Nothing on APB changes from SETUP to the end of ACCESS.

    The bridge holds the address and the direction. The manager holds HWDATA, because the AHB data phase is still in progress.

  4. Rule 4: A write counts at one edge: PSEL, PENABLE and PREADY all at 1.

    A device that acts on PSEL alone, or on PENABLE alone, acts once in every cycle of the transfer.

  5. Rule 5: PSLVERR at the transfer edge becomes ERR1 and then ERR2.

    The device reports the failure once. The bridge gives AHB its two cycles from its own state.

Sources for These Rules

Arm AMBA APB Protocol Specification, document IHI 0024, issue E. The bridge as the APB requester: section 1.1. Write and read transfers and wait states: sections 3.1 and 3.3. The error response, and the one cycle in which PSLVERR counts: section 3.4. The operating states: section 4.1.

Arm AMBA AHB Protocol Specification, document IHI 0033, issue C. Holding the write data through wait states: sections 3.1 and 3.7. The two-cycle error: section 5.1.3.

The five states are those of the bridge used in this course. The specifications give the rules on each side and leave the design of the bridge open. Another bridge may store the write data, or add a cycle on the way back.

Now Run It

The lab at the end of this page holds the bridge of ap_soc with UART0 and GPIO behind it. The manager is test code, a bus-functional model. It writes an exclamation mark to UART0, which asks for two wait states. Then it writes to the GPIO input register, which can only be read.

The first write. Printed by the lab at each rising clock edge. HADDR and PADDR are in hexadecimal.
e0 HADDR=10000000 PADDR=00000000 UART=0 GPIO=0 EN=0 PW=00000000 PRDY=0 PERR=0 HRDY=1 ERR=0
e1 HADDR=00000000 PADDR=10000000 UART=1 GPIO=0 EN=0 PW=00000021 PRDY=0 PERR=0 HRDY=0 ERR=0
e2 HADDR=00000000 PADDR=10000000 UART=1 GPIO=0 EN=1 PW=00000021 PRDY=0 PERR=0 HRDY=0 ERR=0
e3 HADDR=00000000 PADDR=10000000 UART=1 GPIO=0 EN=1 PW=00000021 PRDY=0 PERR=0 HRDY=0 ERR=0
e4 HADDR=00000000 PADDR=10000000 UART=1 GPIO=0 EN=1 PW=00000021 PRDY=1 PERR=0 HRDY=1 ERR=0
UART TX byte=21 character=!
One Write with Two Wait States, RecordedThe lab writes one character to UART0. Step through the clock edges.
01234clkUARTENPRDYHRDYPW0000000000000021
Scroll horizontally ↔
  1. Cycle 0

    IDLE. The manager shows the address of UART0, and HRDY is 1. This edge takes the address.

  2. Cycle 1

    SETUP. UART is 1 and EN is 0. PW carries the character, hexadecimal 21. HRDY is 0.

  3. Cycle 2

    ACCESS. EN is 1, but PRDY is 0: UART0 asks for a wait state.

  4. Cycle 3

    ACCESS again. Nothing may change, and nothing does.

  5. Cycle 4

    ACCESS with PRDY at 1. UART0 takes the character at this edge. HRDY is 1, so AHB is done too.

Recorded from the lab. Each column is what one rising clock edge sees. The short names are in the table of lab names.

Rule 1 at work: e0 takes the address. At e1 HADDR is back at zero, and PADDR still names UART0. Rule 3 at work: PW shows 21 from e1 to e4. The bridge does not store that value. The manager is holding it.

Rule 4 at work: the line that reports the byte 21 appears once, after e4. UART is 1 at four edges and EN is 1 at three. A device that counted either of those would print the mark four times, or three.

The second write, to a register that can only be read. The last two lines are printed after the run.
e5 HADDR=10001004 PADDR=10000000 UART=0 GPIO=0 EN=0 PW=00000021 PRDY=0 PERR=0 HRDY=1 ERR=0
e6 HADDR=00000000 PADDR=10001004 UART=0 GPIO=1 EN=0 PW=00000002 PRDY=0 PERR=0 HRDY=0 ERR=0
e7 HADDR=00000000 PADDR=10001004 UART=0 GPIO=1 EN=1 PW=00000002 PRDY=1 PERR=1 HRDY=0 ERR=0
e8 HADDR=00000000 PADDR=10001004 UART=0 GPIO=0 EN=0 PW=00000002 PRDY=0 PERR=0 HRDY=0 ERR=1
e9 HADDR=00000000 PADDR=10001004 UART=0 GPIO=0 EN=0 PW=00000002 PRDY=0 PERR=0 HRDY=1 ERR=1
GPIO write response=1
checker: 2 writes, 0 reads, 2 wait edges, 0 rules broken, 0 notes

Rule 5 at work: at e7 GPIO answers with PERR at 1. HRDY is 0 in that row, and ERR is still 0. At e8 GPIO is no longer selected, and ERR is 1 with HRDY at 0. At e9 both are 1, and the failed write ends.

The last line comes from a checker in the lab. It watches the APB side and counts two writes with no rule broken. Only one of them reached the console.

This lab leaves things out. Both buses run on the same clock. The manager is test code, and it sends one transfer at a time.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
Store the address, pass the write data throughStore the write data as wellOne register set fewer. It works only because AHB makes the manager hold HWDATA during wait states.
Two error states in the bridgeWire PSLVERR straight to HRESPThe error lasts the two cycles AHB needs. Wired straight, it would be gone before the AHB transfer ends, and a failed write would look like a good one.
One transfer in progressA path for each deviceDevices stay simple, and one bridge serves them all. A slow device makes every other device behind the bridge wait.

The last row is the price of the cheap bus. It is worth paying for registers that software touches now and then.

The Trap: Acting on Select Alone

Back to the row of exclamation marks. The program ran once, and the bridge behaved. The fault was in the device. Its designer wrote "take the byte when I am selected for a write".

On a fast test with no wait state, that device took the byte twice: once in SETUP and once in ACCESS. With two wait states it took it four times. Each copy went down the serial line.

The bridge cannot protect a device from this. It must keep PSEL at 1 for the whole transfer. The device has to wait for the one edge of rule 4. A read usually hides the same fault, because reading twice does no harm, until the register is one that changes when read.

At Scale

This page2 devices behind 1 bridge
ap_soc7 device blocks behind 1 bridge

This section looks ahead. The lab has two devices behind its bridge. ap_soc has seven, and they share it one transfer at a time.

  1. Who is using the bridge

    When a write to one device is late, find the transfer that was in progress. A slow device somewhere else behind the bridge may hold it.

  2. Where to split

    A device with a deadline can get a bridge of its own. Check the arbiter in front as well: two bridges behind one waiting manager change nothing.

  3. More than one device

    The bridge picks one PSEL from the stored address, and returns that device's PREADY, PRDATA and PSLVERR. The Protocols track builds this in the APB decoder and read multiplexer.

  4. Another fast side

    A larger chip may put AXI in place of AHB. The APB side stays the same. See an AXI-to-APB bridge.

Try It

Run the two writes. Name the state of the bridge at every row. Then point at the one row where UART0 takes the character.

In the test code, change waits to 0. Predict which two rows disappear, and how many characters UART0 takes.

The second write goes to the GPIO input register, which can only be read. Find the row where GPIO reports the failure, and the two rows where the manager sees it.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: e0 HADDR=10000000 PADDR=00000000 UART=0 GPIO=0 EN=0 PW=00000000 PRDY=0 PERR=0 HRDY=1 ERR=0 e1 HADDR=00000000 PADDR=10000000 UART=1 GPIO=0 EN=0 PW=00000021 PRDY=0 PERR=0 HRDY=0 ERR=0 e2 HADDR=00000000 PADDR=10000000 UART=1 GPIO=0 EN=1 PW=00000021 PRDY=0 PERR=0 HRDY=0 ERR=0 e3 HADDR=00000000 PADDR=10000000 UART=1 GPIO=0 EN=1 PW=00000021 PRDY=0 PERR=0 HRDY=0 ERR=0 e4 HADDR=00000000 PADDR=10000000 UART=1 GPIO=0 EN=1 PW=00000021 PRDY=1 PERR=0 HRDY=1 ERR=0 UART TX byte=21 character=! e5 HADDR=10001004 PADDR=10000000 UART=0 GPIO=0 EN=0 PW=00000021 PRDY=0 PERR=0 HRDY=1 ERR=0 e6 HADDR=00000000 PADDR=10001004 UART=0 GPIO=1 EN=0 PW=00000002 PRDY=0 PERR=0 HRDY=0 ERR=0 e7 HADDR=00000000 PADDR=10001004 UART=0 GPIO=1 EN=1 PW=00000002 PRDY=1 PERR=1 HRDY=0 ERR=0 e8 HADDR=00000000 PADDR=10001004 UART=0 GPIO=0 EN=0 PW=00000002 PRDY=0 PERR=0 HRDY=0 ERR=1 e9 HADDR=00000000 PADDR=10001004 UART=0 GPIO=0 EN=0 PW=00000002 PRDY=0 PERR=0 HRDY=1 ERR=1 GPIO write response=1 checker: 2 writes, 0 reads, 2 wait edges, 0 rules broken, 0 notes
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: apb_checker.sv
`timescale 1ns/1ns
// apb_checker.sv: the rules of an APB interface, checked at every rising edge (read-only).
//   reset        PSEL is 1 while PRESETn is low;
//   known        PSEL is x, or a control signal, a strobed write byte or PREADY in access is x;
//   setup        PENABLE is 1 without a setup cycle (PSEL 1, PENABLE 0) before it;
//   setup_len    a setup cycle not followed by an access cycle;
//   stable       PSEL, PADDR, PWRITE, PPROT, PSTRB or the write data changed inside a transfer,
//                or PSEL or PENABLE dropped before PREADY;
//   enable_drop  PENABLE still 1 in the cycle after a completed transfer;
//   strb_read    PSTRB not zero during a read;
//   onehot       more than one PSELx at once;
//   timeout      no PREADY after TIMEOUT wait cycles (a liveness watchdog, not a protocol rule).
// A NOTE (not a broken rule): PSLVERR is 1 outside the completion cycle, which the spec allows
// and recommends against (IHI 0024E 3.4).
module apb_checker #(parameter int NSEL = 1, parameter int TIMEOUT = 16) (
  input logic            pclk,
  input logic            presetn,
  input logic [NSEL-1:0] psel,
  input logic            penable,
  input logic            pwrite,
  input logic [31:0]     paddr,
  input logic [31:0]     pwdata,
  input logic [3:0]      pstrb,
  input logic [2:0]      pprot,
  input logic            pready,
  input logic            pslverr);
  int broken = 0, notes = 0, writes = 0, reads = 0, waits = 0, edge_n = -1, stall = 0;
  logic            p_any = 0, p_en = 0, p_rdy = 0, p_wr = 0;
  logic [NSEL-1:0] p_sel = 0;
  logic [31:0]     p_addr = 0, p_wdata = 0;
  logic [3:0]      p_strb = 0;
  logic [2:0]      p_prot = 0;
  task automatic report(input string rule, input string msg);
    broken++;
    $display("RULE %0s broken at edge %0d: %0s", rule, edge_n, msg);
  endtask
  task automatic note(input string rule, input string msg);
    notes++;
    $display("NOTE %0s at edge %0d: %0s", rule, edge_n, msg);
  endtask
  always @(posedge pclk) begin
    logic any, en, rdy, bytes_x;
    edge_n = edge_n + 1;
    any = (|psel) === 1'b1; en = (penable === 1'b1); rdy = (pready === 1'b1);
    if (presetn !== 1'b1) begin
      if (any) report("reset", "PSEL is 1 while PRESETn is low");
      any = 0; en = 0;
    end else begin
      bytes_x = 0;
      for (int l = 0; l < 4; l++) if (pstrb[l] === 1'b1 && (^pwdata[8*l +: 8]) === 1'bx) bytes_x = 1;
      if ((^psel) === 1'bx) report("known", "PSEL is x");
      else if (any && (^{paddr, pwrite, penable, pstrb, pprot}) === 1'bx) report("known", "a control signal is x while PSEL is 1");
      else if (any && pwrite === 1'b1 && bytes_x) report("known", "a strobed byte of PWDATA is x");
      else if (any && en && pready === 1'bx) report("known", "PREADY is x in an access cycle");
      if (any && en && !(p_any && (!p_en || !p_rdy)))
        report("setup", "PENABLE is 1 without a setup cycle (PSEL 1, PENABLE 0) before it");
      if (p_any && !p_en && !(any && en))
        report("setup_len", "the setup cycle was not followed by an access cycle");
      if (p_any && (!p_en || !p_rdy) && any) begin
        if (psel !== p_sel) report("stable", $sformatf("PSEL changed from %b to %b inside a transfer", p_sel, psel));
        if (paddr !== p_addr) report("stable", $sformatf("PADDR changed from 0x%0h to 0x%0h inside a transfer", p_addr, paddr));
        if (pwrite !== p_wr) report("stable", "PWRITE changed inside a transfer");
        if (pprot !== p_prot) report("stable", "PPROT changed inside a transfer");
        if (pstrb !== p_strb) report("stable", "PSTRB changed inside a transfer");
        if (p_wr && pwdata !== p_wdata) report("stable", $sformatf("PWDATA changed from 0x%0h to 0x%0h inside a transfer", p_wdata, pwdata));
        if (p_en && !en) report("stable", "PENABLE dropped before PREADY");
      end
      if (p_any && p_en && !p_rdy && !any) report("stable", "PSEL dropped before PREADY");
      if (p_any && p_en && p_rdy && en) report("enable_drop", "PENABLE is still 1 in the cycle after a completed transfer");
      if (any && pwrite === 1'b0 && pstrb !== 4'b0000) report("strb_read", $sformatf("PSTRB is %b during a read", pstrb));
      if ($countones(psel) > 1) report("onehot", $sformatf("PSELx is %b: two completers selected", psel));
      if (any && en && rdy) begin
        if (pwrite === 1'b1) writes++; else reads++;
        stall = 0;
      end else if (any && en) begin
        waits++; stall++;
        if (stall == TIMEOUT) report("timeout", $sformatf("no PREADY after %0d wait cycles", TIMEOUT));
      end
      if (pslverr === 1'b1 && !(any && en && rdy))
        note("pslverr", "PSLVERR is 1 outside the completion cycle (ignored by the requester; recommended LOW)");
    end
    p_any = any; p_en = en; p_rdy = rdy; p_sel = psel; p_addr = paddr; p_wr = (pwrite === 1'b1);
    p_wdata = pwdata; p_strb = pstrb; p_prot = pprot;
  end
  final $display("checker: %0d writes, %0d reads, %0d wait edges, %0d rules broken, %0d notes", writes, reads, waits, broken, notes);
endmodule
Read included file: apb_io_pair.sv
// The two register ports exercised in S4.5. Only UART can stretch a transfer.
module ap_uart_tx_port(
  input  logic        clk, reset_n, sel, enable, write,
  input  logic [11:0] offset,
  input  logic [31:0] data, wait_edges,
  input  logic [3:0]  strb,
  output logic        ready, error, push,
  output logic [7:0]  tx_byte
);
  logic [31:0] elapsed;
  always_ff @(posedge clk or negedge reset_n) begin
    if (!reset_n) elapsed <= 0;
    else if (sel && enable && !ready) elapsed <= elapsed + 1;
    else elapsed <= 0;
  end

  assign ready = sel && enable && elapsed >= wait_edges;
  assign error = ready && (!write || offset != 12'h000);
  assign push = ready && !error && strb[0];
  assign tx_byte = data[7:0];
endmodule

module ap_gpio_input_port(
  input  logic        sel, enable, write,
  input  logic [11:0] offset,
  input  logic [15:0] pins,
  output logic        ready, error,
  output logic [31:0] data
);
  wire input_word = !write && offset == 12'h004;
  assign ready = sel && enable;
  assign error = ready && !input_word;
  assign data = sel && input_word ? {16'b0, pins} : 32'b0;
endmodule
Read included file: bridge_walk.svh
// Two pages of the ap_soc v1.0 map. Other addresses in this bench's APB
// envelope receive a local error. Both buses use the same clock.
localparam logic [31:0] TX = 32'h1000_0000, INPUT_WORD = 32'h1000_1004;
logic hclk=0, hresetn=0;
always #5 hclk = ~hclk;
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] paddr, pwdata, hrdata, gpio_data;
wire [3:0] pstrb;
wire [2:0] pprot;
wire uart_selected = psel && paddr[31:12] == TX[31:12];
wire gpio_selected = psel && paddr[31:12] == INPUT_WORD[31:12];
logic [31:0] waits=0;
wire uart_ready, uart_error, push, gpio_ready, gpio_error;
wire [7:0] tx_byte;
wire selected_hole = psel && !uart_selected && !gpio_selected;
wire pready = uart_selected ? uart_ready :
              gpio_selected ? gpio_ready : selected_hole && penable;
wire pslverr = uart_selected ? uart_error :
               gpio_selected ? gpio_error : selected_hole && penable;
wire [31:0] prdata = gpio_selected ? gpio_data : 32'b0;

ap_uart_tx_port uart(
  .clk(hclk), .reset_n(hresetn), .sel(uart_selected), .enable(penable), .write(pwrite),
  .offset(paddr[11:0]), .data(pwdata), .strb(pstrb), .wait_edges(waits),
  .ready(uart_ready), .error(uart_error), .push(push), .tx_byte(tx_byte)
);
ap_gpio_input_port gpio(
  .sel(gpio_selected), .enable(penable), .write(pwrite), .offset(paddr[11:0]),
  .pins(16'h8042), .ready(gpio_ready), .error(gpio_error), .data(gpio_data)
);
ahb_apb_bridge bridge(
  .hclk, .hresetn, .hsel, .haddr, .htrans, .hwrite, .hwdata, .hprot(4'b0011),
  .hwstrb(4'hf), .hready, .hreadyout(hready), .hresp, .hrdata,
  .psel, .penable, .pwrite, .paddr, .pwdata, .pstrb, .pprot, .pready, .pslverr, .prdata
);
apb_checker #(.NSEL(2)) watch_bus(
  .pclk(hclk), .presetn(hresetn), .psel({gpio_selected,uart_selected}),
  .penable, .pwrite, .paddr, .pwdata, .pstrb, .pprot, .pready, .pslverr
);

// Aligned, non-overlapping, full-word writes from a bus-functional manager.
task automatic write_word(input logic [31:0] address, data, output logic failed);
  @(negedge hclk);
  haddr=address;
  hwrite=1;
  htrans=2'b10;
  @(negedge hclk);
  htrans=0;
  haddr=0;
  hwdata=data;
  do @(posedge hclk); while (!hready);
  failed=hresp;
  #1;
endtask

Check Yourself

Predict the outputUART0 now needs no wait state. The last line of the lab is the count of its checker, as in the run above. What is that line now?
`timescale 1ns/1ns
module tb;
  `include "bridge_walk.svh"
  logic monitor = 0;
  logic failed;
  int cycle=0;
  always @(posedge hclk) if (monitor) begin
    $display("e%0d HADDR=%08h PADDR=%08h UART=%b GPIO=%b EN=%b PW=%08h PRDY=%b PERR=%b HRDY=%b ERR=%b",
      cycle, haddr, paddr, uart_selected, gpio_selected, penable, pwdata,
      pready, pslverr, hready, hresp);
    if (push) $display("UART TX byte=%02h character=%c",tx_byte,tx_byte);
    cycle++;
  end
  initial begin
    repeat (2) @(posedge hclk);
    hresetn <= 1;
    waits = 0;
    @(posedge hclk);
    #1 monitor = 1;
    write_word(TX, 32'h21, failed);
    if (failed) $fatal(1, "UART write failed");
    write_word(INPUT_WORD, 32'h02, failed);
    $display("GPIO write response=%b",failed);
    $finish;
  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: apb_checker.sv
`timescale 1ns/1ns
// apb_checker.sv: the rules of an APB interface, checked at every rising edge (read-only).
//   reset        PSEL is 1 while PRESETn is low;
//   known        PSEL is x, or a control signal, a strobed write byte or PREADY in access is x;
//   setup        PENABLE is 1 without a setup cycle (PSEL 1, PENABLE 0) before it;
//   setup_len    a setup cycle not followed by an access cycle;
//   stable       PSEL, PADDR, PWRITE, PPROT, PSTRB or the write data changed inside a transfer,
//                or PSEL or PENABLE dropped before PREADY;
//   enable_drop  PENABLE still 1 in the cycle after a completed transfer;
//   strb_read    PSTRB not zero during a read;
//   onehot       more than one PSELx at once;
//   timeout      no PREADY after TIMEOUT wait cycles (a liveness watchdog, not a protocol rule).
// A NOTE (not a broken rule): PSLVERR is 1 outside the completion cycle, which the spec allows
// and recommends against (IHI 0024E 3.4).
module apb_checker #(parameter int NSEL = 1, parameter int TIMEOUT = 16) (
  input logic            pclk,
  input logic            presetn,
  input logic [NSEL-1:0] psel,
  input logic            penable,
  input logic            pwrite,
  input logic [31:0]     paddr,
  input logic [31:0]     pwdata,
  input logic [3:0]      pstrb,
  input logic [2:0]      pprot,
  input logic            pready,
  input logic            pslverr);
  int broken = 0, notes = 0, writes = 0, reads = 0, waits = 0, edge_n = -1, stall = 0;
  logic            p_any = 0, p_en = 0, p_rdy = 0, p_wr = 0;
  logic [NSEL-1:0] p_sel = 0;
  logic [31:0]     p_addr = 0, p_wdata = 0;
  logic [3:0]      p_strb = 0;
  logic [2:0]      p_prot = 0;
  task automatic report(input string rule, input string msg);
    broken++;
    $display("RULE %0s broken at edge %0d: %0s", rule, edge_n, msg);
  endtask
  task automatic note(input string rule, input string msg);
    notes++;
    $display("NOTE %0s at edge %0d: %0s", rule, edge_n, msg);
  endtask
  always @(posedge pclk) begin
    logic any, en, rdy, bytes_x;
    edge_n = edge_n + 1;
    any = (|psel) === 1'b1; en = (penable === 1'b1); rdy = (pready === 1'b1);
    if (presetn !== 1'b1) begin
      if (any) report("reset", "PSEL is 1 while PRESETn is low");
      any = 0; en = 0;
    end else begin
      bytes_x = 0;
      for (int l = 0; l < 4; l++) if (pstrb[l] === 1'b1 && (^pwdata[8*l +: 8]) === 1'bx) bytes_x = 1;
      if ((^psel) === 1'bx) report("known", "PSEL is x");
      else if (any && (^{paddr, pwrite, penable, pstrb, pprot}) === 1'bx) report("known", "a control signal is x while PSEL is 1");
      else if (any && pwrite === 1'b1 && bytes_x) report("known", "a strobed byte of PWDATA is x");
      else if (any && en && pready === 1'bx) report("known", "PREADY is x in an access cycle");
      if (any && en && !(p_any && (!p_en || !p_rdy)))
        report("setup", "PENABLE is 1 without a setup cycle (PSEL 1, PENABLE 0) before it");
      if (p_any && !p_en && !(any && en))
        report("setup_len", "the setup cycle was not followed by an access cycle");
      if (p_any && (!p_en || !p_rdy) && any) begin
        if (psel !== p_sel) report("stable", $sformatf("PSEL changed from %b to %b inside a transfer", p_sel, psel));
        if (paddr !== p_addr) report("stable", $sformatf("PADDR changed from 0x%0h to 0x%0h inside a transfer", p_addr, paddr));
        if (pwrite !== p_wr) report("stable", "PWRITE changed inside a transfer");
        if (pprot !== p_prot) report("stable", "PPROT changed inside a transfer");
        if (pstrb !== p_strb) report("stable", "PSTRB changed inside a transfer");
        if (p_wr && pwdata !== p_wdata) report("stable", $sformatf("PWDATA changed from 0x%0h to 0x%0h inside a transfer", p_wdata, pwdata));
        if (p_en && !en) report("stable", "PENABLE dropped before PREADY");
      end
      if (p_any && p_en && !p_rdy && !any) report("stable", "PSEL dropped before PREADY");
      if (p_any && p_en && p_rdy && en) report("enable_drop", "PENABLE is still 1 in the cycle after a completed transfer");
      if (any && pwrite === 1'b0 && pstrb !== 4'b0000) report("strb_read", $sformatf("PSTRB is %b during a read", pstrb));
      if ($countones(psel) > 1) report("onehot", $sformatf("PSELx is %b: two completers selected", psel));
      if (any && en && rdy) begin
        if (pwrite === 1'b1) writes++; else reads++;
        stall = 0;
      end else if (any && en) begin
        waits++; stall++;
        if (stall == TIMEOUT) report("timeout", $sformatf("no PREADY after %0d wait cycles", TIMEOUT));
      end
      if (pslverr === 1'b1 && !(any && en && rdy))
        note("pslverr", "PSLVERR is 1 outside the completion cycle (ignored by the requester; recommended LOW)");
    end
    p_any = any; p_en = en; p_rdy = rdy; p_sel = psel; p_addr = paddr; p_wr = (pwrite === 1'b1);
    p_wdata = pwdata; p_strb = pstrb; p_prot = pprot;
  end
  final $display("checker: %0d writes, %0d reads, %0d wait edges, %0d rules broken, %0d notes", writes, reads, waits, broken, notes);
endmodule
Read included file: apb_io_pair.sv
// The two register ports exercised in S4.5. Only UART can stretch a transfer.
module ap_uart_tx_port(
  input  logic        clk, reset_n, sel, enable, write,
  input  logic [11:0] offset,
  input  logic [31:0] data, wait_edges,
  input  logic [3:0]  strb,
  output logic        ready, error, push,
  output logic [7:0]  tx_byte
);
  logic [31:0] elapsed;
  always_ff @(posedge clk or negedge reset_n) begin
    if (!reset_n) elapsed <= 0;
    else if (sel && enable && !ready) elapsed <= elapsed + 1;
    else elapsed <= 0;
  end

  assign ready = sel && enable && elapsed >= wait_edges;
  assign error = ready && (!write || offset != 12'h000);
  assign push = ready && !error && strb[0];
  assign tx_byte = data[7:0];
endmodule

module ap_gpio_input_port(
  input  logic        sel, enable, write,
  input  logic [11:0] offset,
  input  logic [15:0] pins,
  output logic        ready, error,
  output logic [31:0] data
);
  wire input_word = !write && offset == 12'h004;
  assign ready = sel && enable;
  assign error = ready && !input_word;
  assign data = sel && input_word ? {16'b0, pins} : 32'b0;
endmodule
Read included file: bridge_walk.svh
// Two pages of the ap_soc v1.0 map. Other addresses in this bench's APB
// envelope receive a local error. Both buses use the same clock.
localparam logic [31:0] TX = 32'h1000_0000, INPUT_WORD = 32'h1000_1004;
logic hclk=0, hresetn=0;
always #5 hclk = ~hclk;
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] paddr, pwdata, hrdata, gpio_data;
wire [3:0] pstrb;
wire [2:0] pprot;
wire uart_selected = psel && paddr[31:12] == TX[31:12];
wire gpio_selected = psel && paddr[31:12] == INPUT_WORD[31:12];
logic [31:0] waits=0;
wire uart_ready, uart_error, push, gpio_ready, gpio_error;
wire [7:0] tx_byte;
wire selected_hole = psel && !uart_selected && !gpio_selected;
wire pready = uart_selected ? uart_ready :
              gpio_selected ? gpio_ready : selected_hole && penable;
wire pslverr = uart_selected ? uart_error :
               gpio_selected ? gpio_error : selected_hole && penable;
wire [31:0] prdata = gpio_selected ? gpio_data : 32'b0;

ap_uart_tx_port uart(
  .clk(hclk), .reset_n(hresetn), .sel(uart_selected), .enable(penable), .write(pwrite),
  .offset(paddr[11:0]), .data(pwdata), .strb(pstrb), .wait_edges(waits),
  .ready(uart_ready), .error(uart_error), .push(push), .tx_byte(tx_byte)
);
ap_gpio_input_port gpio(
  .sel(gpio_selected), .enable(penable), .write(pwrite), .offset(paddr[11:0]),
  .pins(16'h8042), .ready(gpio_ready), .error(gpio_error), .data(gpio_data)
);
ahb_apb_bridge bridge(
  .hclk, .hresetn, .hsel, .haddr, .htrans, .hwrite, .hwdata, .hprot(4'b0011),
  .hwstrb(4'hf), .hready, .hreadyout(hready), .hresp, .hrdata,
  .psel, .penable, .pwrite, .paddr, .pwdata, .pstrb, .pprot, .pready, .pslverr, .prdata
);
apb_checker #(.NSEL(2)) watch_bus(
  .pclk(hclk), .presetn(hresetn), .psel({gpio_selected,uart_selected}),
  .penable, .pwrite, .paddr, .pwdata, .pstrb, .pprot, .pready, .pslverr
);

// Aligned, non-overlapping, full-word writes from a bus-functional manager.
task automatic write_word(input logic [31:0] address, data, output logic failed);
  @(negedge hclk);
  haddr=address;
  hwrite=1;
  htrans=2'b10;
  @(negedge hclk);
  htrans=0;
  haddr=0;
  hwdata=data;
  do @(posedge hclk); while (!hready);
  failed=hresp;
  #1;
endtask
ConceptWhich states does the bridge pass through for a write that succeeds with no wait state?
ConceptDuring the wait states of a write, who keeps the write data still?
ConceptIn the recorded run, at which edge does UART0 take the character?
ConceptWhy does the bridge need the states ERR1 and ERR2?

Common Mistakes

Wrong Store the write data at the edge that takes the address.

At that edge HWDATA still belongs to the transfer before.

Fix Take the address first. Use the write data of the data phase that follows.

Wrong Let a device act whenever it is selected.

It is selected in SETUP and in every waiting ACCESS cycle as well.

Fix Act once, at the edge with PSEL, PENABLE and PREADY all at 1.

Wrong Wire PSLVERR straight to HRESP.

The device drops PSLVERR after one edge. AHB needs the error for two cycles.

Fix Remember the failure in the state of the bridge, through ERR1 and ERR2.

Remember

  • A bridge is a state machine: IDLE, SETUP, ACCESS, and ERR1 and ERR2 for a failure.
  • The bridge stores the address and the direction; the manager holds the write data.
  • A write counts at one edge: PSEL, PENABLE and PREADY all at 1.
  • PSLVERR lasts one edge; the bridge turns it into the two cycles of an AHB error.
  • One transfer crosses at a time, so a slow device delays every device behind the bridge.

Practice

Report a problem with this page