Choosing a Bus

Which bus for which block? The answer starts from what one transfer of the block needs, and never from the name of a bus. This page gives the questions in order, and prices the answers.

You Will Be Able To

  • Choose a bus for a block with three questions about its transfers, asked in a fixed order.
  • Say what a port costs in flip-flops and gates, and which feature each cost pays for.
  • Say what the choice of a bus leaves open: which blocks wait for each other.
Lesson
S4.10
Time
26 min
Runs on
Your browser
Check
5 questions

A block has six registers that software sets once, at start-up. Its designer gives it the port with five channels, because the memory next to it has one. It works. It also costs more than twice the flip-flops, and nothing got faster.

Before You Start

This page closes the module. You need APB from S0.4, and AHB, the two buses and the bridge from S0.5. You need the bus matrix from Many Managers.

AXI is named on this page, with one line for each thing it adds. For more, read AXI in one picture.

Five questions this page answers
Question
1Which questions pick a bus for a block, and in which order?
2Which bus does each kind of block get, and why?
3What does an AXI port cost that an APB port does not, and what does the cost buy?
4In the lab, why does the AHB port need fewer cells than the APB port?
5What does the choice of a bus leave open?

The Idea: The Cheapest Bus That Is Enough

A bus is a promise about transfers. It says what each transfer carries, how many may be on the way at once, and who may make whom wait. Every promise costs wires and flip-flops at both ends, and work for the people who check it.

So "which bus is best" is the wrong question. Ask what the traffic of this block needs. Then take the cheapest bus that keeps that promise.

Three needs separate nearly every case. Does a transfer carry an address at all? Is the block a set of registers that software touches now and then? Must several requests be outstanding at once? A stream of samples needs no address. Registers need the least of an addressed bus. A memory needs more. A block with many requests on the way needs the most.

That is why one chip holds several buses, with bridges between them. Each block pays only for what its traffic needs.

New Words

New words on this page
WordMeaning
StreamData that flows in order to one receiver, with no address.
AXI-StreamThe AMBA interface for a stream: a valid wire, a ready wire and the data.
AXI4-LiteAXI cut down for registers: five channels, one transfer for each request, and no ID.
Byte strobeA wire for each byte of a word. It says whether a write changes that byte.
Bus converterA block that joins two different buses. A bridge is one. This page says "converter" for short.

One more name appears once, at the end of the page. CHI is the AMBA interface for processors whose caches must agree about shared data. A later module is about it.

Lab names
In the labMeaning
setup, accessthe two phases of the APB write: 1 at the edge that sees that phase
address1 at the edge that takes the AHB address
AW, W, B, AR, Rthe five channels of AXI4-Lite: 1 at the edge of a handshake on that channel
AHB_datathe write data in the AHB data phase
APB_word, AHB_word, Lite_wordthe value of the register behind each port

One Picture: Three Questions

Three Questions, in This OrderStep through the picture. Each question is asked about one transfer of the block.
  • Bus
  • Peripheral
  • Memory
  • Processor
Three Questions, in This OrderStep through the picture. Each question is asked about one transfer of the block. component Does it carry anaddress? question 1: is there a"where" at all? component AXI-Stream no address: data inorder, to onereceiver component Registers,touched now andthen? question 2: is speedof no concern? component APB two cycles for atransfer, onetransfer at a time component Several requestsoutstanding? question 3: mustrequests overtakeeach other? component AHB one transfer afterthe other, with no gap component AXI five channels and IDs:many requests on theway noyesyesnonoyes
  1. component Does it carry an address?question 1: is there a "where" at all?
    • no → AXI-Stream
    • yes → Registers, touched now and then?
  2. component AXI-Streamno address: data in order, to one receiver
  3. component Registers, touched now and then?question 2: is speed of no concern?
    • yes → APB
    • no → Several requests outstanding?
  4. component APBtwo cycles for a transfer, one transfer at a time
  5. component Several requests outstanding?question 3: must requests overtake each other?
    • no → AHB
    • yes → AXI
  6. component AHBone transfer after the other, with no gap
  7. component AXIfive channels and IDs: many requests on the way
  1. Is there an address?

    Samples from a sensor, or pixels to a screen, go to one receiver in order. Nothing needs to say where. That is a stream, and a stream needs no address wires.

  2. Is it a set of registers?

    Software writes a few registers at start-up, and reads a status now and then. Two cycles for a transfer do no harm. The cheapest port is the right one.

  3. One at a time, at speed?

    A processor fetches from memory in every cycle. It needs transfers with no gap between them. It does not need two requests on the way at once.

  4. Many on the way?

    A manager sends several requests before the first reply is back, and a fast reply may overtake a slow one. That needs separate channels and IDs.

Read the diagram as text
  • Does it carry an address?: question 1: is there a "where" at all?
  • AXI-Stream: no address: data in order, to one receiver
  • Registers, touched now and then?: question 2: is speed of no concern?
  • APB: two cycles for a transfer, one transfer at a time
  • Several requests outstanding?: question 3: must requests overtake each other?
  • AHB: one transfer after the other, with no gap
  • AXI: five channels and IDs: many requests on the way
  • Does it carry an address? → AXI-Stream: no
  • Does it carry an address? → Registers, touched now and then?: yes
  • Registers, touched now and then? → APB: yes
  • Registers, touched now and then? → Several requests outstanding?: no
  • Several requests outstanding? → AHB: no
  • Several requests outstanding? → AXI: yes

The order matters. Each "yes" or "no" on the upper branch ends the search at the cheapest bus that is enough.

The picture has no best bus in it. It has an order of questions, and each answer stops as early as it can.

The Smallest Example, Worked by Hand: Six Blocks, Six Answers

Take four blocks of ap_soc and two visitors. For each, write down what one transfer needs. Then ask the three questions.

What the traffic needs, and the bus that follows
BlockIts trafficAddress?Registers now and then?Several outstanding?Bus
GPIOa few registers, written when a pin must changeyesyesAPB
Timer and PWMa period and a compare value, set onceyesyesAPB
SRAMthe processor fetches from it in every cycleyesnonoAHB
DMA, as a managercopies memory, one transfer after the otheryesnonoAHB
Visitor: a camera inputpixels in order, to one filternoAXI-Stream
Visitor: a large, slow memory for four processorsmany reads on the way, fast replies firstyesnoyesAXI

Two notes on the table. The camera also has a few control registers, and those are an APB port of their own. A block can have two ports with two buses. And DMA is asked the questions as a manager. Its own control registers sit on APB.

This is also why ap_soc has no AXI. No block of it answers "yes" to the third question. Buying the five channels would buy nothing.

The Rules

  1. Rule 1: Start from the transfer, not from the bus.

    Write down what one transfer carries, how many are on the way at once, and what may wait. Only then look at bus names.

  2. Rule 2: No address: a stream.

    AXI-Stream carries data in order to one receiver. The receiver can still say "not now".

  3. Rule 3: Registers that are touched now and then: APB.

    Every transfer takes two cycles at least, and one transfer runs at a time. The device needs almost no logic.

  4. Rule 4: One transfer after the other, at speed: AHB.

    The address of the next transfer overlaps the data of this one. A manager still has one transfer in its data phase.

  5. Rule 5: Several requests outstanding, or replies in another order: AXI.

    Five channels and IDs make that possible. Every port pays for them in flip-flops.

One case bends rule 3. A chip whose other blocks all speak AXI may have no APB branch near a register block. A converter would then cost more than it saves. AXI4-Lite exists for that case.

Sources for These Rules

Arm AMBA APB Protocol Specification, IHI 0024 issue E, section 1.1. It describes APB as a low-cost interface for the control registers of peripherals. It is not pipelined, and a transfer takes two cycles at least. Arm AMBA AHB Protocol Specification, IHI 0033 issue C, sections 1.1 and 3.1: the overlap of address and data phases.

Arm AMBA AXI Protocol Specification, IHI 0022 issue L, sections A2.3 and A5.1: channels with their own handshakes, and IDs. AXI4-Lite: IHI 0022 issue H.c, section B1.1. Arm AMBA AXI-Stream Protocol Specification, IHI 0051 issue B, sections 2.1 and 2.2. CHI: Arm AMBA CHI Architecture Specification, IHI 0050 issue G, chapter B1.

Each specification describes its own bus. None says when to choose it. The order of the three questions is engineering practice.

Now Run It

The lab at the end of this page prices three of the answers. It puts the same 32-bit register behind three ports: APB, AHB and AXI4-Lite. Test code writes the value 68 through each port at the same time, and reads it back through the last.

Printed by the lab at four rising clock edges. The names are in the table of lab names.
e0 setup=1 address=1 AW=1 W=0
e1 access=1 AHB_data=00000068 W=1
e2 APB_word=00000068 AHB_word=00000068 B=1 AR=1
e3 R=1 Lite_word=00000068

Rule 3 at work: APB is in its setup phase at e0 and in its access phase at e1. Rule 4 at work: AHB takes the address at e0 and the data at e1. By e2 both registers hold 68.

Rule 5 at work: AXI4-Lite takes the address at e0 and the data at e1, each with a handshake of its own. It reports the write on B at e2. A read then needs two more handshakes, at e2 and e3.

All three ports got the value across. Now the price. The lab can map each port to gates and flip-flops and count them. Each gate or flip-flop counts as one cell.

One register word behind four ports
PortCellsFlip-flopsOther cellsWhat the port does
APB, with byte strobes38326stores the word, and can write each of its four bytes alone
APB, with no byte strobes34322stores the word, and writes whole words only
AHB, whole words only36333stores the word, and remembers the address phase in one flip-flop
AXI4-Lite, with byte strobes816714stores the word, keeps a copy for a read that stalls, and holds three flip-flops of handshake state

Measured with Yosys 0.70+290, generic cells. The second row is the first port with its byte strobes removed, and nothing else changed.

The AHB port needs 36 cells and the first APB port 38. That is not because AHB is the cheaper bus. The two ports do not do the same job. This APB port can write single bytes, and this AHB port cannot. Take the byte strobes away and APB needs 34.

The last row is the price of rule 5. Of its 67 flip-flops, 32 are the word. Another 32 keep a read value still while the manager is not ready to take it. Three hold the state of the handshakes.

These numbers leave things out. They count one mapping of one small register, with generic cells. They are not an area, and they say nothing about clock speed. Use them to compare features, as the table does.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
Several buses in one chipOne bus for every blockEach block pays only for what its traffic needs. Every boundary needs a bridge, and a bridge costs cycles.
The cheapest bus that is enoughThe most capable bus everywhereSmall ports and simple checks. A block that grows later may need a new port.
A stream port for data, a register port for controlOne addressed port for bothThe data flows with no address logic at all. The block has two ports to connect and to verify.

One thing the choice of a bus does not buy is speed between blocks. A bus name says how one port talks. Whether two managers wait for each other depends on what joins the ports. That was the subject of the bus matrix.

The Trap: Five Channels for Six Registers

Back to the block with six registers. Its designer chose the port with five channels because the memory beside it had one. One kind of port everywhere seemed tidy.

Look at the cell table for what that tidiness costs. One register word needs 81 cells behind AXI4-Lite, against 38 behind APB. Most of the difference is the copy that a stalled read needs. Software sets these registers once, at start-up. No read of them ever had to be fast.

The opposite mistake is as common. A memory that the processor reads in every cycle is put behind APB, because APB is simple. Every access then takes two cycles at least, where AHB needs one. Ask the three questions first. The answer is usually plain.

At Scale

ap_soc2 buses: AHB and APB
A large chipAXI between most blocks, streams for data, APB for registers

This section looks ahead. In ap_soc the three questions give two buses. A large chip answers "yes" to the third question in many places, and adds a fourth question about caches.

  1. The signals of each bus

    The Protocols track has a first lesson for each: APB, AHB and AXI. The family of AXI, with AXI4-Lite, is in the lesson on the AXI family.

  2. Converters

    Every boundary between two buses holds one. This module opened the one from AHB to APB, in Bridges. The Protocols track has the one from AXI to APB.

  3. A fourth question

    When several processors keep copies of the same data in their caches, the copies must agree. CHI carries the extra messages for that.

  4. Write the traffic down

    For a large chip, the first table of this page becomes a document. It lists every block, what its transfers carry, its peak rate and its deadline. The bus choices follow from it.

Try It

Run the lab. For each of the three ports, name the edge at which the register takes the value 68.

Press Synthesize. The constant PORT picks the port that is mapped: 0 for APB, 1 for AHB and 2 for AXI4-Lite. Compare the cells and flip-flops with the table.

In the second file of the lab, find the loop that tests one strobe for each byte in the APB port. Replace it with one plain write of the whole word. Map PORT 0 again and compare with the second row of the table.

RTLIcarus Verilog · in your browser · Lint: Verilator service · Synthesize: Yosys service
Edit the design, then Run, Lint or Synthesize
Expected: e0 setup=1 address=1 AW=1 W=0 e1 access=1 AHB_data=00000068 W=1 e2 APB_word=00000068 AHB_word=00000068 B=1 AR=1 e3 R=1 Lite_word=00000068

Original design · 81 cells · 67 flip-flops · 0 latches · 3 logic levels. Synthesis uses Yosys; counts depend on the toolchain.

Read included file: scratch.sv
// All three tops implement one 32-bit scratch register, selected upstream.
// The AHB port serves aligned word accesses. APB and AXI support byte strobes.
module apb_word(input logic clk, reset_n, psel, penable, pwrite,
                input logic [3:0] pstrb, input logic [31:0] pwdata,
                output logic [31:0] prdata, output wire pready, pslverr);
  assign pready = 1;
  assign pslverr = 0;
  always_ff @(posedge clk)
    if (!reset_n) prdata <= 0;
    else if (psel && penable && pwrite)
      for (int b=0; b<4; b++)
        if (pstrb[b]) prdata[b*8+:8] <= pwdata[b*8+:8];
endmodule

module ahb_word(input logic clk, reset_n, hsel, hready, hwrite,
                input logic [1:0] htrans, input logic [31:0] hwdata,
                output logic [31:0] hrdata, output wire hreadyout, hresp);
  logic pending;
  assign hreadyout = 1;
  assign hresp = 0;
  always_ff @(posedge clk)
    if (!reset_n) begin
      pending <= 0;
      hrdata <= 0;
    end else if (hready) begin
      pending <= hsel && htrans[1] && hwrite;
      if (pending) hrdata <= hwdata;
    end
endmodule

module lite_word(input logic clk, reset_n, awvalid, wvalid, arvalid, bready, rready,
                 input logic [3:0] wstrb, input logic [31:0] wdata,
                 output wire awready, wready, arready,
                 output logic bvalid, rvalid, output logic [31:0] rdata,
                 output wire [1:0] bresp, rresp);
  // Accept the address first. The manager can present data before the address
  // and hold it until the data channel opens. A stalled read holds a snapshot.
  logic addressed;
  logic [31:0] scratch;
  assign awready = !addressed && !bvalid;
  assign wready = addressed;
  assign arready = !rvalid;
  assign bresp = 0;
  assign rresp = 0;
  always_ff @(posedge clk)
    if (!reset_n) begin
      addressed <= 0;
      bvalid <= 0;
      rvalid <= 0;
      rdata <= 0;
      scratch <= 0;
    end else begin
      if (awvalid && awready) addressed <= 1;
      if (bvalid && bready) bvalid <= 0;
      if (wvalid && wready) begin
        addressed <= 0;
        bvalid <= 1;
        for (int b=0; b<4; b++)
          if (wstrb[b]) scratch[b*8+:8] <= wdata[b*8+:8];
      end
      if (rvalid && rready) rvalid <= 0;
      if (arvalid && arready) begin
        rdata <= scratch;
        rvalid <= 1;
      end
    end
endmodule

Check Yourself

Predict the outputThe test code now writes the value 41 in place of 68. What is the last line the lab prints?
module tb;
  bit clk, rst, sel, access, write_en, addr_valid, data_valid, read_valid;
  bit [1:0] transfer;
  bit [31:0] word = 32'h00000041;
  wire [31:0] apb, ahb, lite;
  wire av, wv, rv, bvalid, rvalid;
  apb_word p(clk,rst,sel,access,write_en,4'hf,word,apb,,);
  ahb_word h(clk,rst,sel,1'b1,write_en,transfer,word,ahb,,);
  lite_word x(clk,rst,addr_valid,data_valid,read_valid,1'b1,1'b1,
              4'hf,word,av,wv,rv,bvalid,rvalid,lite,,);
  always #5 clk=!clk;
  initial begin
    @(posedge clk); rst <= 1;
    @(posedge clk);
    sel <= 1; write_en <= 1; transfer <= 2; addr_valid <= 1; data_valid <= 1;
    @(posedge clk);
    $display("e0 setup=%b address=%b AW=%b W=%b",sel&&!access,transfer[1],av&&addr_valid,wv&&data_valid);
    access <= 1; transfer <= 0; addr_valid <= 0;
    @(posedge clk);
    $display("e1 access=%b AHB_data=%h W=%b",sel&&access,word,wv&&data_valid);
    sel <= 0; access <= 0; data_valid <= 0; read_valid <= 1;
    @(posedge clk);
    $display("e2 APB_word=%h AHB_word=%h B=%b AR=%b",apb,ahb,bvalid,rv&&read_valid);
    read_valid <= 0;
    @(posedge clk);
    $display("e3 R=%b Lite_word=%h",rvalid,lite);
    $finish;
  end
endmodule
Read included file: scratch.sv
// All three tops implement one 32-bit scratch register, selected upstream.
// The AHB port serves aligned word accesses. APB and AXI support byte strobes.
module apb_word(input logic clk, reset_n, psel, penable, pwrite,
                input logic [3:0] pstrb, input logic [31:0] pwdata,
                output logic [31:0] prdata, output wire pready, pslverr);
  assign pready = 1;
  assign pslverr = 0;
  always_ff @(posedge clk)
    if (!reset_n) prdata <= 0;
    else if (psel && penable && pwrite)
      for (int b=0; b<4; b++)
        if (pstrb[b]) prdata[b*8+:8] <= pwdata[b*8+:8];
endmodule

module ahb_word(input logic clk, reset_n, hsel, hready, hwrite,
                input logic [1:0] htrans, input logic [31:0] hwdata,
                output logic [31:0] hrdata, output wire hreadyout, hresp);
  logic pending;
  assign hreadyout = 1;
  assign hresp = 0;
  always_ff @(posedge clk)
    if (!reset_n) begin
      pending <= 0;
      hrdata <= 0;
    end else if (hready) begin
      pending <= hsel && htrans[1] && hwrite;
      if (pending) hrdata <= hwdata;
    end
endmodule

module lite_word(input logic clk, reset_n, awvalid, wvalid, arvalid, bready, rready,
                 input logic [3:0] wstrb, input logic [31:0] wdata,
                 output wire awready, wready, arready,
                 output logic bvalid, rvalid, output logic [31:0] rdata,
                 output wire [1:0] bresp, rresp);
  // Accept the address first. The manager can present data before the address
  // and hold it until the data channel opens. A stalled read holds a snapshot.
  logic addressed;
  logic [31:0] scratch;
  assign awready = !addressed && !bvalid;
  assign wready = addressed;
  assign arready = !rvalid;
  assign bresp = 0;
  assign rresp = 0;
  always_ff @(posedge clk)
    if (!reset_n) begin
      addressed <= 0;
      bvalid <= 0;
      rvalid <= 0;
      rdata <= 0;
      scratch <= 0;
    end else begin
      if (awvalid && awready) addressed <= 1;
      if (bvalid && bready) bvalid <= 0;
      if (wvalid && wready) begin
        addressed <= 0;
        bvalid <= 1;
        for (int b=0; b<4; b++)
          if (wstrb[b]) scratch[b*8+:8] <= wdata[b*8+:8];
      end
      if (rvalid && rready) rvalid <= 0;
      if (arvalid && arready) begin
        rdata <= scratch;
        rvalid <= 1;
      end
    end
endmodule
ConceptA block receives a flow of samples from one source. Nothing in a sample says where it goes. Which interface fits?
ConceptWhich need calls for AXI where AHB is not enough?
ConceptIn the lab the AHB port maps to fewer cells than the first APB port. Why?
ConceptTwo managers both get AXI ports. What has that choice not decided?

Common Mistakes

Wrong Give every block the most capable bus, to be safe.

Each port pays for channels and stores it never uses.

Fix Take the cheapest bus that keeps the promise the traffic needs.

Wrong Read a cell count as "this bus is cheaper than that one".

The count prices the features of one port, such as byte strobes.

Fix Compare ports that offer the same features, or say which feature the difference pays for.

Wrong Expect a bigger bus to end the waiting between two managers.

They wait where their paths meet, whatever the ports speak.

Fix Look at what joins the ports: a shared bus, a matrix or a crossbar.

Remember

  • Choose a bus from what one transfer of the block needs, not from the name of a bus.
  • No address: a stream. Registers now and then: APB. One after the other at speed: AHB. Several outstanding: AXI.
  • A port pays for each feature in flip-flops and gates; compare ports only feature for feature.
  • One chip holds several buses, with a converter at each boundary.
  • The choice of a bus does not decide which blocks wait for each other.

Practice

Report a problem with this page