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.
| Question | |
|---|---|
| 1 | Which questions pick a bus for a block, and in which order? |
| 2 | Which bus does each kind of block get, and why? |
| 3 | What does an AXI port cost that an APB port does not, and what does the cost buy? |
| 4 | In the lab, why does the AHB port need fewer cells than the APB port? |
| 5 | What 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
| Word | Meaning |
|---|---|
| Stream | Data that flows in order to one receiver, with no address. |
| AXI-Stream | The AMBA interface for a stream: a valid wire, a ready wire and the data. |
| AXI4-Lite | AXI cut down for registers: five channels, one transfer for each request, and no ID. |
| Byte strobe | A wire for each byte of a word. It says whether a write changes that byte. |
| Bus converter | A 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.
| In the lab | Meaning |
|---|---|
| setup, access | the two phases of the APB write: 1 at the edge that sees that phase |
| address | 1 at the edge that takes the AHB address |
| AW, W, B, AR, R | the five channels of AXI4-Lite: 1 at the edge of a handshake on that channel |
| AHB_data | the write data in the AHB data phase |
| APB_word, AHB_word, Lite_word | the value of the register behind each port |
One Picture: Three Questions
- Bus
- Peripheral
- Memory
- Processor
- 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.
- 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.
- 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.
- 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.
| Block | Its traffic | Address? | Registers now and then? | Several outstanding? | Bus |
|---|---|---|---|---|---|
| GPIO | a few registers, written when a pin must change | yes | yes | APB | |
| Timer and PWM | a period and a compare value, set once | yes | yes | APB | |
| SRAM | the processor fetches from it in every cycle | yes | no | no | AHB |
| DMA, as a manager | copies memory, one transfer after the other | yes | no | no | AHB |
| Visitor: a camera input | pixels in order, to one filter | no | AXI-Stream | ||
| Visitor: a large, slow memory for four processors | many reads on the way, fast replies first | yes | no | yes | AXI |
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
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.
Rule 2: No address: a stream.
AXI-Stream carries data in order to one receiver. The receiver can still say "not now".
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.
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.
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.
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=00000068Rule 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.
| Port | Cells | Flip-flops | Other cells | What the port does |
|---|---|---|---|---|
| APB, with byte strobes | 38 | 32 | 6 | stores the word, and can write each of its four bytes alone |
| APB, with no byte strobes | 34 | 32 | 2 | stores the word, and writes whole words only |
| AHB, whole words only | 36 | 33 | 3 | stores the word, and remembers the address phase in one flip-flop |
| AXI4-Lite, with byte strobes | 81 | 67 | 14 | stores 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 choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Several buses in one chip | One bus for every block | Each block pays only for what its traffic needs. Every boundary needs a bridge, and a bridge costs cycles. |
| The cheapest bus that is enough | The most capable bus everywhere | Small ports and simple checks. A block that grows later may need a new port. |
| A stream port for data, a register port for control | One addressed port for both | The 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
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.
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.
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.
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.
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.
Synthesize the design to measure its cells and inspect the mapped gates.
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=00000068Original 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
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.