Two Buses and the Bridge

APB is too slow for memory, and a fast bus is too costly for every small device. So a chip has both, and a bridge between them.

You Will Be Able To

  • Explain how AHB overlaps two transfers, and what that buys.
  • Find the address phase and the data phase of every transfer in a capture, with and without a wait.
  • Say why a chip has two buses, and follow one store across the bridge.
Lesson
S0.5
Time
25 min
Runs on
Your browser
Check
5 questions

On APB the quickest transfer takes two cycles. A processor fetches an instruction from memory in almost every cycle. Put memory on APB, and the processor runs at half speed before it has done any work.

Before You Start

You need S0.3 and S0.4: manager and subordinate, the transfer edge, wait states, the setup phase and the access phase. You also need the decoder and the read multiplexer of S0.2.

Five questions this page answers
Question
1Why is APB too slow for memory, and what does AHB do about it?
2What are the address phase and the data phase, and how do two transfers overlap?
3What does HREADY at 0 do to the data phase, and to the address phase behind it?
4Why does a chip keep its device registers on a second, simpler bus?
5What does a bridge do, and what does a device access cost through it?

The Idea: Ask the Next Question Early

Every bus so far finishes one transfer before it starts the next. The fast bus breaks that habit. It splits each transfer into two halves. In the first half, the address phase, the manager says what it wants. In the second half, the data phase, the value moves.

The trick is the overlap. While the data of one transfer moves, the manager already shows the address of the next. Each transfer still takes two cycles. But one transfer finishes in every cycle, because two are always in progress. A bus built this way is called pipelined.

The standard bus that works this way is AHB, the Advanced High-performance Bus. It belongs to the same AMBA family as APB.

So why not put every device on AHB? Because the speed has a price. An AHB subordinate needs more logic than an APB completer. Every block added to AHB also makes its decoder and its read multiplexer larger, and so slower. A chip has dozens of small device registers that software touches now and then. They gain nothing from speed.

So a chip keeps two buses. Memory sits on AHB. Device registers sit on APB. One block, the bridge, joins them: it takes a transfer from AHB and repeats it on APB.

New Words

New words on this page
WordMeaning
AHBAdvanced High-performance Bus: a standard bus for memory and fast blocks.
Address phaseThe first half of an AHB transfer. The manager shows the address and the direction.
Data phaseThe second half. The write value or the read value moves. A subordinate may stretch it with wait states.
PipelinedWorking on two transfers at once: the address phase of one overlaps the data phase of the one before.
BridgeA block between two buses. It is a subordinate on the first bus and the manager, or requester, on the second.

Every AHB signal name starts with H. Here is the toy bus of S0.3 beside AHB.

From the toy bus to AHB
Toy busAHBPhaseWhat changed
the clockHCLKbothonly the name
validHTRANSaddresstwo bits wide: IDLE (binary 00) means no transfer, NONSEQ (binary 10) means one transfer
addrHADDRaddressthe name, and the width: up to 32 bits (8 in this lab)
writeHWRITEaddressonly the name: 1 = write, 0 = read
wdataHWDATAdatait now arrives one phase after the address
rdataHRDATAdataonly the name
readyHREADYdatait ends the data phase, and lets the next address phase be taken

HTRANS has two more values. They belong to transfers that run in a row, which the Protocols track covers.

One Picture: Two Buses and a Bridge

Two Buses and the Bridge Between ThemStep through the picture. It starts with the fast bus, adds the simple bus, and then follows two stores.
  • Processor
  • Bus
  • Memory
  • Peripheral
Two Buses and the Bridge Between ThemStep through the picture. It starts with the fast bus, adds the simple bus, and then follows two stores. component Processor the manager: itstarts everytransfer component AHB the fast bus: atransfer can finish inevery cycle component Memory addresses 0 to 13, anAHB subordinate component Bridge an AHB subordinateabove, the APBrequester below component APB the simple bus: twocycles or more foreach transfer component Lamp register address 14, an APBcompleter component Other deviceregisters more APB completers address, then datamemory addressesdevice addressessetup, then accessone select eachone select each
  1. component Processorthe manager: it starts every transfer
    • address, then data → AHB
  2. component AHBthe fast bus: a transfer can finish in every cycle
    • memory addresses → Memory
    • device addresses → Bridge
  3. component Memoryaddresses 0 to 13, an AHB subordinate
  4. component APBthe simple bus: two cycles or more for each transfer
    • one select each → Lamp register
    • one select each → Other device registers
  5. component Lamp registeraddress 14, an APB completer
  6. component Other device registersmore APB completers
  7. component Bridgean AHB subordinate above, the APB requester below
    • setup, then access → APB
  1. The fast bus

    The processor and memory talk over AHB. The processor uses memory in almost every cycle, so this bus is built for speed.

  2. The simple bus

    Device registers sit on APB, as in S0.4. They are many, and each is used now and then. Simple matters more than fast here.

  3. The bridge joins them

    To AHB, the bridge is one more subordinate. It answers for every device address. To APB, it is the requester.

  4. A store to memory

    The address is 3, so the AHB decoder selects memory. The bridge and APB stay idle. The store costs one cycle of data phase.

  5. A store to the lamps

    The address is 14, so the AHB decoder selects the bridge. The bridge runs a setup phase and an access phase on APB. AHB waits one cycle for it.

Read the diagram as text
  • Processor: the manager: it starts every transfer
  • AHB: the fast bus: a transfer can finish in every cycle
  • Memory: addresses 0 to 13, an AHB subordinate
  • Bridge: an AHB subordinate above, the APB requester below
  • APB: the simple bus: two cycles or more for each transfer
  • Lamp register: address 14, an APB completer
  • Other device registers: more APB completers
  • Processor → AHB: address, then data
  • AHB → Memory: memory addresses
  • AHB → Bridge: device addresses
  • Bridge → APB: setup, then access
  • APB → Lamp register: one select each
  • APB → Other device registers: one select each

Each arrow stands for a group of wires. The processor sees one bus and one set of addresses. Only the decoder knows about the bridge.

Notice what the processor does not know. It puts an address on AHB and waits for HREADY. Whether the answer came from memory or crossed the bridge, it sees the same wires.

The Smallest Example, Worked by Hand: Transfers as Bars

Draw each transfer as a bar of two parts, one cycle wide each. Transfer A writes memory box 3. Transfer B reads it back. Memory needs no wait states.

Two transfers that overlap
TransferCycle 1Cycle 2Cycle 3
A: write box 3address phasedata phase
B: read box 3address phasedata phase

Look down the column of cycle 2. The address wires carry B while the data wires carry A. Two transfers took three cycles. Ten would take eleven. On APB, ten transfers take twenty cycles or more.

Now add a wait. Transfer C writes the lamps, and its data phase needs one wait state. Transfer D, a read of box 3, comes right behind it.

A wait state stretches two bars
TransferCycle 1Cycle 2Cycle 3Cycle 4
C: write lampsaddress phasedata phase (wait)data phase
D: read box 3address phase (held)address phasedata phase
HREADY1011

In cycle 2, HREADY is 0. The data phase of C cannot end. The address phase of D cannot end either. The bus takes a new address only when the old data phase is over. One wire holds both bars in place.

The Rules

  1. Rule 1: Every transfer has an address phase and then a data phase.

    The address phase carries HTRANS, HADDR and HWRITE. The data phase carries HWDATA for a write, or HRDATA for a read.

  2. Rule 2: The address phase of one transfer overlaps the data phase of the one before.

    By the data phase, the address wires show another transfer. So the bus must remember the address it took.

  3. Rule 3: A data phase ends at the rising edge that sees HREADY at 1.

    That edge is the transfer edge. A write changes the register there. A read is taken there.

  4. Rule 4: HREADY at 0 holds both phases.

    The data phase gains a wait state. The manager holds the next address phase unchanged, and keeps HWDATA still.

  5. Rule 5: A bridge turns one AHB data phase into one whole APB transfer.

    It runs the setup phase and the access phase, and answers HREADY at 0 until the APB transfer has happened.

Sources for These Rules

Arm AMBA AHB Protocol Specification, document IHI 0033, issue C. The two phases, their overlap and HREADY: section 3.1. The values of HTRANS: section 3.2. Holding the address and the write data through wait states: sections 3.7.2 and 6.1.1. The decoder and the multiplexer: section 1.1.3.

The APB bridge as the APB requester: Arm AMBA APB Protocol Specification, document IHI 0024, issue E, section 1.1.

The reasons for two buses are engineering practice. The specifications describe each bus and leave the choice to the designer. The bridge in the lab is our own, and is the smallest one that obeys both sets of rules.

Now Run It

The lab at the end of this page holds the whole path in one file. It has a manager, AHB, memory, the bridge, APB and the lamp register. The manager is test code again, a bus-functional model. Each row ends with the transfer that is in its data phase.

Printed by the lab at each rising clock edge. The left group is the address phase. The middle group is the data phase. The right group is APB.
e0 haddr=0 hwrite=0 htrans=IDLE | hwdata=0 hrdata=x hready=1 | psel=0 penable=0 lamps=0 | data phase: none
e1 haddr=3 hwrite=1 htrans=NONSEQ | hwdata=0 hrdata=x hready=1 | psel=0 penable=0 lamps=0 | data phase: none
e2 haddr=3 hwrite=0 htrans=NONSEQ | hwdata=7 hrdata=x hready=1 | psel=0 penable=0 lamps=0 | data phase: write 3  <- transfer
e3 haddr=14 hwrite=1 htrans=NONSEQ | hwdata=7 hrdata=7 hready=1 | psel=0 penable=0 lamps=0 | data phase: read 3  <- transfer
e4 haddr=3 hwrite=0 htrans=NONSEQ | hwdata=9 hrdata=x hready=0 | psel=1 penable=0 lamps=0 | data phase: write 14  (wait)
e5 haddr=3 hwrite=0 htrans=NONSEQ | hwdata=9 hrdata=x hready=1 | psel=1 penable=1 lamps=0 | data phase: write 14  <- transfer
e6 haddr=3 hwrite=0 htrans=IDLE | hwdata=9 hrdata=7 hready=1 | psel=0 penable=0 lamps=9 | data phase: read 3  <- transfer
e7 haddr=3 hwrite=0 htrans=IDLE | hwdata=9 hrdata=x hready=1 | psel=0 penable=0 lamps=9 | data phase: none
Four Transfers, RecordedThe lab writes box 3, reads box 3, writes the lamps and reads box 3 again. Step through the clock edges.
01234567clkhtransIDLENONSEQIDLEhaddr03143hwritehwdata079hrdatax7x7xhreadypselpenablelamps09
Scroll horizontally ↔
  1. Cycle 1

    Address phase of the first transfer: write, address 3. No transfer is in its data phase yet.

  2. Cycle 2

    Two transfers at once. HWDATA carries 7 for the write to box 3. HADDR already shows the next transfer, a read of box 3.

  3. Cycle 3

    HRDATA returns 7 for the read. HADDR shows 14: the address phase of the write to the lamps.

  4. Cycle 4

    The bridge runs the APB setup phase: PSEL is 1 and PENABLE is 0. It answers HREADY at 0, so AHB waits. The read behind it stays on HADDR.

  5. Cycle 5

    The APB access phase. HREADY is 1 again. The lamps take 9 at this edge, and the waiting read is accepted.

  6. Cycle 6

    The lamps show 9. HRDATA returns 7 for the last read.

Recorded from the lab. Each column is what one rising clock edge sees. Names are in lower case in the lab code and in capitals in the text.

Rule 2 at work: in row e2, HADDR and HWRITE describe the read. HWDATA carries the 7 of the write before it. Rule 4 at work: at e4, HREADY is 0, and HADDR and HWRITE are the same at e5.

Rule 5 at work: e4 and e5 are one AHB data phase. On APB they are a setup phase and an access phase. The lamps change after e5, the edge with PSEL and PENABLE both at 1. This completer keeps PREADY at 1.

Count the cost. The two memory transfers each added one cycle. The lamp write added two. A device access through the bridge always costs at least one wait state on AHB.

This lab leaves things out. Its bridge is three lines, and its APB has one completer that answers at once. Real AHB also carries wires for the size of a transfer and for errors.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
Overlap the address and data phasesFinish each transfer before the next startsOne transfer can finish in every cycle. Every subordinate must remember its address.
One HREADY for the whole busA ready wire for each phaseOne wire stops everything, so nothing can be lost. A slow block stops every transfer behind it.
Device registers on a second, simple busEvery block on the fast busSmall devices stay small, and the fast bus keeps a short decoder. Each device access costs extra cycles.
A bridge that looks like one subordinateA processor that speaks two busesThe processor and its software see one bus and one memory map.

The third row is the heart of it. Speed is spent where transfers are frequent, and saved where they are rare.

The Trap: Reading One Column as One Transfer

An engineer opens a capture and finds HADDR at 14 and HWDATA at 7 in the same column. The report says "7 was written to the lamps". Look at row e3 of the lab. HADDR is 14 and HWDATA is 7. The lamps never receive 7. They receive 9, two edges later.

On a pipelined bus, the address and the data in one column belong to different transfers. The 7 was the write to box 3. To pair them correctly, take the address at the edge that ends its address phase. Then take the data at the edge that ends the data phase after it.

At Scale

This page1 AHB subordinate, 1 APB completer
ap_soc4 blocks on AHB, 7 device blocks on APB

This section looks ahead. The chip of this track, ap_soc, has exactly this shape, with more blocks on each bus.

  1. In ap_soc

    Boot ROM and SRAM sit on AHB. UART0, GPIO and five more device blocks sit on APB, behind one bridge. Lesson S4.5 opens that bridge.

  2. The full AHB

    The Protocols track teaches AHB signal by signal, starting at AHB in one picture and wait states.

  3. A third bus

    AXI, named in S0.3, serves the largest chips. It lets several transfers wait for their data at once, and return out of order. A small chip like ap_soc does not need it.

Try It

Run the lab. For each row, name the transfer in its address phase and the transfer in its data phase.

Add a fifth transfer: a read of the lamps. Predict the edge where it happens and the value of hrdata there.

Send two lamp writes in a row. Predict how psel and penable move between them.

Break rule 2: make memory use haddr where it uses d_addr. Find the box that the first write changes.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: e0 haddr=0 hwrite=0 htrans=IDLE | hwdata=0 hrdata=x hready=1 | psel=0 penable=0 lamps=0 | data phase: none e1 haddr=3 hwrite=1 htrans=NONSEQ | hwdata=0 hrdata=x hready=1 | psel=0 penable=0 lamps=0 | data phase: none e2 haddr=3 hwrite=0 htrans=NONSEQ | hwdata=7 hrdata=x hready=1 | psel=0 penable=0 lamps=0 | data phase: write 3 <- transfer e3 haddr=14 hwrite=1 htrans=NONSEQ | hwdata=7 hrdata=7 hready=1 | psel=0 penable=0 lamps=0 | data phase: read 3 <- transfer e4 haddr=3 hwrite=0 htrans=NONSEQ | hwdata=9 hrdata=x hready=0 | psel=1 penable=0 lamps=0 | data phase: write 14 (wait) e5 haddr=3 hwrite=0 htrans=NONSEQ | hwdata=9 hrdata=x hready=1 | psel=1 penable=1 lamps=0 | data phase: write 14 <- transfer e6 haddr=3 hwrite=0 htrans=IDLE | hwdata=9 hrdata=7 hready=1 | psel=0 penable=0 lamps=9 | data phase: read 3 <- transfer e7 haddr=3 hwrite=0 htrans=IDLE | hwdata=9 hrdata=x hready=1 | psel=0 penable=0 lamps=9 | data phase: none

Check Yourself

Predict the outputThe manager writes 5 to box 2 and 6 to the lamps. It then reads the lamps, and last it reads box 2. The last line prints the value of the latest read and the edge where it happened. What is that line?
// Two buses in one file. The manager is test code and speaks AHB.
// Memory sits on AHB. The lamp register of S0.4 sits on APB, behind a bridge.
module tb;
  logic hclk = 0;
  always #5 hclk = ~hclk;

  // ---- AHB wires driven by the manager ----
  localparam IDLE = 0, NONSEQ = 2;  // two values of htrans: no transfer, or one transfer
  logic [1:0] htrans = IDLE;   // address phase: is a transfer asked for?
  logic [7:0] haddr  = 0;      // address phase: which address
  logic       hwrite = 0;      // address phase: 1 = write, 0 = read
  logic [7:0] hwdata = 0;      // data phase: the value to write
  // ---- AHB wires driven back to the manager ----
  logic       hready;          // data phase: 1 = this edge ends the phase
  logic [7:0] hrdata;          // data phase: the value read

  // What the bus remembers. At every edge that sees hready at 1, the address phase
  // on the wires becomes the data phase of the next cycle.
  logic       d_busy  = 0;     // a transfer is in its data phase
  logic [7:0] d_addr  = 0;     // the address of that transfer
  logic       d_write = 0;     // the direction of that transfer
  always @(posedge hclk) if (hready) begin
    d_busy <= (htrans == NONSEQ);  d_addr <= haddr;  d_write <= hwrite;
  end

  // The decoder works on the remembered address: 0 to 13 is memory, 14 is the lamps.
  wire sel_mem  = d_busy && (d_addr <= 13);
  wire sel_lamp = d_busy && (d_addr == 14);

  // ---- Memory, an AHB subordinate: its data phase always takes one cycle ----
  logic [7:0] mem [0:13];
  always @(posedge hclk) if (sel_mem && d_write) mem[d_addr] <= hwdata;

  // ---- The bridge: an AHB subordinate on one side, the APB requester on the other ----
  wire  psel = sel_lamp;       // a data phase for the lamps becomes one APB transfer
  logic penable = 0;           // 0 in the first cycle (setup), 1 afterwards (access)
  wire  pready;
  always @(posedge hclk) penable <= psel && !(penable && pready);
  // The bridge passes the rest straight on: paddr is d_addr, pwrite is d_write, pwdata is hwdata.

  // ---- The lamp register, an APB completer that answers at once ----
  logic [7:0] lamps = 0;
  assign pready = 1;
  always @(posedge hclk) if (psel && penable && pready && d_write) lamps <= hwdata;

  // ---- The way back: one hready and one hrdata, chosen by the same selects ----
  assign hready = sel_lamp ? (penable && pready) : 1;   // the bridge answers 0 during the setup cycle
  assign hrdata = !(d_busy && !d_write) ? 8'hxx         // 8'hxx: unknown unless a read is in its data phase
                : sel_mem               ? mem[d_addr]
                : penable               ? lamps : 8'hxx;

  // The manager: test code that plays the processor (a bus-functional model).
  // send() runs one address phase. The data phase follows while the next send() runs.
  task send(input logic w, input logic [7:0] a, input logic [7:0] d);
    htrans = NONSEQ; haddr = a; hwrite = w;      // the address phase
    @(posedge hclk);
    while (!hready) @(posedge hclk);             // it ends at an edge that sees hready at 1
    #1 htrans = IDLE;                            // "#1" is a tiny delay: just after that edge
    if (w) hwdata = d;                           // the data phase begins: write data goes out now
  endtask

  // Print what each rising clock edge sees.
  int         edge_no = 0;     // counts the rising edges
  int         last_read = 0;   // the edge where the latest read happened
  logic [7:0] got = 0;         // the value that read returned
  string      kind, what;      // text variables, for printing only
  always @(posedge hclk) begin
    kind = (htrans == NONSEQ) ? "NONSEQ" : "IDLE";
    what = d_write ? "write" : "read";
    // $write prints without ending the line. $display ends it.
    $write("e%0d haddr=%0d hwrite=%b htrans=%s | hwdata=%0d hrdata=%0d hready=%b | psel=%b penable=%b lamps=%0d | data phase: ",
      edge_no, haddr, hwrite, kind, hwdata, hrdata, hready, psel, penable, lamps);
    if (!d_busy)     $display("none");
    else if (hready) $display("%s %0d  <- transfer", what, d_addr);
    else             $display("%s %0d  (wait)", what, d_addr);
    if (d_busy && hready && !d_write) begin got = hrdata; last_read = edge_no; end
    edge_no++;
  end

  initial begin
    for (int i = 0; i < 14; i++) mem[i] = 0;
    @(posedge hclk); #1;
    send(1, 2, 5);
    send(1, 14, 6);
    send(0, 14, 0);
    send(0, 2, 0);
    repeat (2) @(posedge hclk);        // let the last data phase finish
    #1;
    $display("read %0d at edge %0d", got, last_read);
    $finish;                           // $finish ends the simulation
  end
endmodule
ConceptWhy would a processor be slow with its memory on APB?
ConceptIn one cycle, HADDR shows 14 and HWDATA shows 7. No wait state is in progress. What does the 7 belong to?
ConceptAt a rising edge, HREADY is 0. What happens at that edge?
ConceptThe processor stores to a device register behind the bridge. What does the bridge do?

Common Mistakes

Wrong Pair the HADDR and the HWDATA seen in one column.

They belong to two different transfers. The data is one phase behind its address.

Fix Match each address with the data phase that follows it.

Wrong Change HADDR while HREADY is 0.

The waiting address phase has not been taken yet. The bus would take the wrong address.

Fix Hold the address phase unchanged until an edge sees HREADY at 1.

Wrong Expect a device register to answer as fast as memory.

The bridge needs a setup phase and an access phase on APB for each transfer.

Fix Count at least one wait state on AHB for every access through the bridge.

Remember

  • AHB splits a transfer into an address phase and a data phase, and overlaps them with the next transfer.
  • One transfer can finish in every cycle, so memory and the processor talk over AHB.
  • HREADY at 0 holds the data phase and the address phase behind it.
  • Address and data seen in one cycle belong to different transfers.
  • Device registers stay on APB; a bridge turns one AHB data phase into a setup phase and an access phase.

Practice

Report a problem with this page