The Bus Ports: From a Memory Port to an AHB-Lite Manager
Inside the core, a load is simple: an address in EX, the data in MEM. Outside, the chip speaks a bus protocol with phases, waits and errors. Something must stand between the two.
You Will Be Able To
- Match the request and the answer of the memory port of the pipeline to the two phases of AHB-Lite.
- Say what the core must hold while the bus is not ready.
- Follow an ERROR response through its two cycles, and say what the core does in each.
- Lesson
- S2.33
- Time
- 35 min
- Runs on
- Your browser
- Check
- 6 questions
S2.16 counted 22 bus signals on the core and left them closed. This page opens them. The rows you will read are those of the real core.
Before You Start
You need the stages EX and MEM of the pipeline from S2.7. A load shows its address in EX and gets its data in MEM.
You need the access fault from S2.29. The AHB lessons from P4.2 on teach the bus itself in depth.
| Question | |
|---|---|
| 1 | What are the two phases of an AHB-Lite transfer? |
| 2 | Which transfer types does ap_rv32 use? |
| 3 | What must a manager do while hready is 0? |
| 4 | How many cycles does an ERROR response take? |
| 5 | What does the data port do with the transfer behind an ERROR? |
The Idea: The Pipeline Already Thinks in Two Phases
A block that starts transfers on a bus is a manager. A block that answers them is a subordinate. The core is a manager, twice.
An AHB-Lite transfer has two parts. In the address phase the manager says where, and whether it reads or writes.
In the data phase, one cycle later, the data moves. The subordinate can stretch this phase with cycles of waiting, called wait states.
Now look at the pipeline. A load shows its address in EX. In the next cycle, in MEM, it takes the data.
That is the same shape: a request, and an answer one cycle later. So the pipeline needs only a thin port adapter: a small block between two kinds of port.
The adapter gives each request a transfer type: it tells the bus that a transfer starts. And it drives a few signals that never change.
One case needs memory. A subordinate can refuse a transfer. Its answer is then called ERROR.
An ERROR response takes two cycles.
The core must not act on the first cycle at once. It notes it in a flip-flop, and acts in the second.
New Words
| Word | Meaning |
|---|---|
| Manager, subordinate | The block that starts transfers on a bus, and the block that answers them. |
| Address phase | The cycle in which the manager shows the address, the direction and the size of a transfer. |
| Data phase | The cycle or cycles after it, in which the data moves and the subordinate answers. |
hready, hresp | From the subordinate: the data phase ends in this cycle, and the transfer is refused. |
| Transfer type | The signal htrans. ap_rv32 uses two values: IDLE, no transfer, and NONSEQ, a single transfer. |
| Burst | Several transfers in a row that a manager marks as one group. ap_rv32 uses none. |
| ERROR response | The answer of a subordinate that refuses a transfer: hresp at 1, for two cycles. |
| Port adapter | A small block that turns one kind of port into another. Here: the adapter. |
haddr, hwrite, hsize | The address of a transfer, 1 for a write, and the number of bytes: 1, 2 or 4. |
hwdata, hrdata | The data of a write and the data of a read. Both belong to the data phase. |
In the analyser rows NSEQ stands for NONSEQ.
One Picture: The Adapter Between Two Worlds
- Processor
- Control / FSM
- Bus
- Memory
- Cycle 1: the request
The pipeline shows an address and says read or write. The adapter puts it on the bus as a transfer. This is the address phase.
- Cycle 2: the answer
The memory answers. For a write, the pipeline now shows the data. For a read, the data comes back. This is the data phase.
- Both at once
While one transfer is in its data phase, the next one is in its address phase. So a transfer can start in every cycle.
- A wait
The memory holds hready at 0. Nothing may change: the data phase and the next address phase both stay as they are.
- An ERROR
It takes two cycles. The adapter notes the first in a flip-flop. In the second, the next transfer is taken back.
Read the diagram as text
- Pipeline: EX shows a request, MEM takes the answer
- Adapter: names the transfer, and remembers an ERROR
- AHB-Lite bus: address phase, then data phase
- Memory or device: answers: ready, or wait, or ERROR
- One flip-flop: the cycle before was the first of an ERROR
- Pipeline → Adapter: request: address, read or write, size
- Adapter → AHB-Lite bus: htrans, haddr, hwrite, hsize
- AHB-Lite bus → Memory or device: the transfer
- Memory or device → Adapter: hready, hresp, hrdata
- Adapter → Pipeline: ready, read data
- Adapter → One flip-flop: hresp at 1, hready at 0
The port of the pipeline already has the two phases of the bus. So the adapter is thin: names, constants and one flip-flop.
| Signal | Direction | On the instruction port | On the data port |
|---|---|---|---|
haddr | out | the address of the next instruction | the address of the load or store |
htrans | out | NONSEQ to fetch, IDLE when asleep | NONSEQ for a load or store, IDLE otherwise |
hwrite | out | always 0 | 1 for a store |
hsize | out | always four bytes | one, two or four bytes |
hburst, hmastlock | out | always 0 | always 0 |
hprot | out | a constant: an instruction fetch | a constant: a data access |
hwdata | out | always 0 | the store data, in the data phase |
hrdata | in | the instruction | the load data |
hready, hresp | in | done, and ERROR | done, and ERROR |
Eleven signals on each port.
The Smallest Example, Worked by Hand
| Address, in hex | Instruction | What it is for |
|---|---|---|
| 014 | sw x5, 0(x8) | a store |
| 018 | lw x6, 0(x8) | a load, right behind it |
| 01c | lw x7, 0(x9) | a load that the bus refuses |
| 020 | sw x6, 4(x8) | a store right behind the refused load |
x8 holds 8000_0000, where the RAM is. x9 holds 4000_0000, where nothing is. x5 holds 55 in hex.
Follow the store at 14 and the load at 18 on the bus. Let the memory answer at once.
| Cycle | htrans, haddr, hwrite | hwdata | hrdata | What happens |
|---|---|---|---|---|
| 1 | NONSEQ, 8000_0000, 1 | address phase of the store | ||
| 2 | NONSEQ, 8000_0000, 0 | 0000_0055 | data phase of the store, and address phase of the load | |
| 3 | the next transfer, or IDLE | 0000_0055 | data phase of the load |
In cycle 2 two transfers share the bus: one is ending and one is starting.
Now the load at 1c. Its address is 4000_0000. No memory is there, so the subordinate answers with an ERROR.
Its address phase is cycle 3. In cycle 4, its data phase, the bus shows hresp at 1 and hready at 0.
But in cycle 4 the store at 20 is already in its address phase. That store is behind an instruction that will trap.
It must not happen. In cycle 5, the second cycle of the ERROR, the data port shows IDLE. The store never starts.
The Rules
Rule 1: A request of the pipeline becomes htrans NONSEQ. With no request, htrans is IDLE.
ap_rv32 uses no other transfer type: every transfer is a single one.
Rule 2: The data of a transfer moves one phase after its address.
The store data is on hwdata in the data phase. The load data is taken from hrdata when hready is 1.
Rule 3: While hready is 0, the manager holds everything.
The address phase that is shown stays the same. The write data of the data phase stays the same.
Rule 4: When htrans is IDLE, the other signals of the address phase mean nothing.
haddr shows whatever the ALU holds. A subordinate must not look at it.
Rule 5: An ERROR response has two cycles: hresp 1 with hready 0, then hresp 1 with hready 1.
The core notes the first cycle in a flip-flop and acts in the second.
Rule 6: In the second cycle of an ERROR, the data port shows IDLE.
The transfer of the younger instruction is cancelled. The instruction port does not cancel: a fetch harms nothing.
Sources for These Rules
Checked in the library: Arm, AMBA AHB Protocol Specification, IHI 0033C. Section 3.1 has the two phases of a transfer. Section 3.2 has the transfer types IDLE and NONSEQ. Section 3.7 has waited transfers, and what a manager must hold. Section 5.1.3 has the ERROR response of two cycles, and says that the manager can cancel the next transfer with IDLE. Section 6.1.1 has the write data held for the whole data phase. The rules of the core, B1 to B9, are in docs/soc/ap_rv32-blueprint.md, section 3.2. Every bus row on this page is recorded from the data port of the real core ap_rv32. The memory behind the port, with its wait states, is a small bench of the recording tool.
Now Run It
An analyser holds the data port of the real core, as recorded, and names each cycle. First a run with a memory that answers at once.
cy | type address rw bytes | hwdata hrdata | hready hresp | what this cycle is
1 | IDLE 00000055 r 1 | 00000000 00000000 | 1 0 |
2 | NSEQ 80000000 w 4 | 00000000 00000000 | 1 0 | address phase: write at 80000000
3 | NSEQ 80000000 r 4 | 00000055 00000000 | 1 0 | data phase: write done, mem[80000000] = 00000055; address phase: read at 80000000
4 | NSEQ 40000000 r 4 | 00000000 00000055 | 1 0 | data phase: read done, 00000055; address phase: read at 40000000
5 | NSEQ 80000004 w 4 | 00000000 00000000 | 0 1 | data phase: ERROR, first cycle; address phase: held, the bus is not ready
6 | IDLE 80000004 w 4 | 00000000 00000000 | 1 1 | data phase: ERROR, second cycle; IDLE: the transfer shown before is cancelled
22 | NSEQ 80000004 w 4 | 00000000 00000000 | 1 0 | address phase: write at 80000004
23 | IDLE 00000000 r 1 | 00000055 00000000 | 1 0 | data phase: write done, mem[80000004] = 00000055
transfers=4 wait cycles=0 errors=1 cancelled=1 rules broken=0Rule 1 at work: row 2 shows NSEQ with the address 80000000 and w for write. It is the store at 14.
Rule 2 at work: row 3. The store is in its data phase: hwdata holds 55. In the same row the load starts.
Row 4: the load is done, and hrdata holds 55. In the same row the load at 1c starts, at 40000000.
Rule 5 at work: rows 5 and 6. In both, hresp is 1. In row 5 hready is 0, and in row 6 it is 1.
Rule 6 at work: row 5 still shows NSEQ for the store at 80000004. Row 6 shows IDLE. That store did not start.
Rows 7 to 21 are all IDLE. The load trapped, and the handler ran. It uses no memory.
Rows 22 and 23: the handler has returned behind the load. Now the store at 20 is executed for real, and 55 is written.
Rule 4 at work: look at haddr in row 23. It is 0, with htrans at IDLE. Nobody reads it.
Now the same program with a memory that makes every transfer wait one cycle. This run is the lab at the end of the page.
cy | type address rw bytes | hwdata hrdata | hready hresp | what this cycle is
1 | IDLE 00000055 r 1 | 00000000 00000000 | 1 0 |
2 | NSEQ 80000000 w 4 | 00000000 00000000 | 1 0 | address phase: write at 80000000
3 | NSEQ 80000000 r 4 | 00000055 00000000 | 0 0 | data phase: wait; address phase: held, the bus is not ready
4 | NSEQ 80000000 r 4 | 00000055 00000000 | 1 0 | data phase: write done, mem[80000000] = 00000055; address phase: read at 80000000
5 | NSEQ 40000000 r 4 | 00000000 00000000 | 0 0 | data phase: wait; address phase: held, the bus is not ready
6 | NSEQ 40000000 r 4 | 00000000 00000055 | 1 0 | data phase: read done, 00000055; address phase: read at 40000000
7 | NSEQ 80000004 w 4 | 00000000 00000000 | 0 0 | data phase: wait; address phase: held, the bus is not ready
8 | NSEQ 80000004 w 4 | 00000000 00000000 | 0 1 | data phase: ERROR, first cycle; address phase: held, the bus is not ready
9 | IDLE 80000004 w 4 | 00000000 00000000 | 1 1 | data phase: ERROR, second cycle; IDLE: the transfer shown before is cancelledRule 3 at work: rows 3 and 4. hready is 0 in row 3. The read at 80000000 is shown in both rows, unchanged.
And hwdata holds 55 in both rows: the store keeps its data for its whole data phase.
Rows 7 to 9: one wait, then the two cycles of the ERROR. The store at 80000004 is shown three times, and never starts.
The last line of this run counts 4 wait cycles. The whole program took 40 cycles on the real core, 4 more than without waits.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| A native port with the timing of the bus | A port with address and data in one cycle | A thin adapter with no state machine. The pipeline must be built around two phases from the start. |
| Only IDLE and NONSEQ | Bursts for a run of fetches | A simple manager that any subordinate understands. A cache line could be read faster with a burst. |
| Register the first ERROR cycle | Act on hresp in the same cycle | No path from the bus response through the core and back to the bus in one cycle. One cycle later to react. |
| Cancel the transfer behind an ERROR | Let it run | A store behind a trapping load never reaches a device. The fetch port does not need this. |
| Two ports | One port, shared | A fetch and a load in the same cycle. The chip needs a fabric that takes two managers. |
| Drive unused store data lanes to 0 | Leave them as they are | Waveforms are easier to read. A few gates. |
In the real core the cancel is done twice. The pipeline throws the younger instruction away, and the adapter turns its request into IDLE.
Two checks for one rule are a choice of this design. A test of the adapter must switch the first one off, or it tests nothing.
The Trap: Dropping the Transfer in the First ERROR Cycle
An engineer sees hresp at 1 and wants the next transfer gone at once. That is one cycle too early.
Look at rows 7 and 8 of the second run. In row 7 the store is shown while the bus waits. Row 8 is the first cycle of the ERROR.
hready is still 0 in row 8. For the bus this is a wait like any other. A transfer that was shown must stay as it is.
A manager that goes to IDLE in row 8 breaks rule 3. The analyser counts that as a broken rule. On the real core its count is 0.
There is a second reason. A signal that goes from the response straight to htrans is a long path through the whole bus fabric.
So the order is fixed: hold in the first cycle, change in the second. The two-cycle ERROR exists to give the manager that time.
At Scale
This section looks ahead. One core and one memory are the smallest bus. In the chip, the two ports meet a fabric and a dozen subordinates.
Many subordinates
An address decoder picks one subordinate for each transfer, and a multiplexer brings its answer back. That is taught later, on S4.3.
The bus in depth
Wait states, the ERROR response and bursts each have a lesson of their own in the AHB module, from P4.2 to P4.6.
Fetches may repeat
The instruction port can fetch an address twice, or fetch one that is never executed. So nothing with side effects on a read may be executed from.
Checked in every run
In the test bench of the core, a protocol checker watches both ports all the time. It found no broken rule in ten thousand random programs.
Try It
Run the lab. Find the row with the first cycle of the ERROR, and the row in which the store is cancelled.
In the task recorded, change htrans[7] from 2'b10 to 2'b00. The store now vanishes in the first ERROR cycle. What does the last line say, and which rule is broken?
Change hwdata[3] to another value. The write data now changes inside its data phase. What does the analyser count?
cy | type address rw bytes | hwdata hrdata | hready hresp | what this cycle is 1 | IDLE 00000055 r 1 | 00000000 00000000 | 1 0 | 2 | NSEQ 80000000 w 4 | 00000000 00000000 | 1 0 | address phase: write at 80000000 3 | NSEQ 80000000 r 4 | 00000055 00000000 | 0 0 | data phase: wait; address phase: held, the bus is not ready 4 | NSEQ 80000000 r 4 | 00000055 00000000 | 1 0 | data phase: write done, mem[80000000] = 00000055; address phase: read at 80000000 5 | NSEQ 40000000 r 4 | 00000000 00000000 | 0 0 | data phase: wait; address phase: held, the bus is not ready 6 | NSEQ 40000000 r 4 | 00000000 00000055 | 1 0 | data phase: read done, 00000055; address phase: read at 40000000 7 | NSEQ 80000004 w 4 | 00000000 00000000 | 0 0 | data phase: wait; address phase: held, the bus is not ready 8 | NSEQ 80000004 w 4 | 00000000 00000000 | 0 1 | data phase: ERROR, first cycle; address phase: held, the bus is not ready 9 | IDLE 80000004 w 4 | 00000000 00000000 | 1 1 | data phase: ERROR, second cycle; IDLE: the transfer shown before is cancelled 10 | IDLE 00000000 r 1 | 00000055 00000000 | 1 0 | 11 | IDLE 00000000 r 1 | 00000000 00000000 | 1 0 | 12 | IDLE 00000341 r 4 | 00000000 00000000 | 1 0 | 13 | IDLE 00000345 r 1 | 00000000 00000000 | 1 0 | 14 | IDLE 0000035d r 2 | 00000000 00000000 | 1 0 | 15 | IDLE 0000035d r 2 | 00000000 00000000 | 1 0 | 16 | IDLE 00000020 r 1 | 00000000 00000000 | 1 0 | 17 | IDLE 00000361 r 2 | 00000000 00000000 | 1 0 | 18 | IDLE 00000302 r 1 | 00000000 00000000 | 1 0 | 19 | IDLE 00000000 r 1 | 00000000 00000000 | 1 0 | 20 | IDLE 00000000 r 1 | 00000000 00000000 | 1 0 | 21 | IDLE 00000302 r 1 | 00000000 00000000 | 1 0 | 22 | IDLE 00000000 r 1 | 00000000 00000000 | 1 0 | 23 | IDLE 00000000 r 1 | 00000000 00000000 | 1 0 | 24 | IDLE 00000000 r 1 | 00000000 00000000 | 1 0 | 25 | NSEQ 80000004 w 4 | 00000000 00000000 | 1 0 | address phase: write at 80000004 26 | IDLE 00000000 r 1 | 00000055 00000000 | 0 0 | data phase: wait 27 | IDLE 00000000 r 1 | 00000055 00000000 | 1 0 | data phase: write done, mem[80000004] = 00000055 transfers=4 wait cycles=4 errors=1 cancelled=1 rules broken=0Check Yourself
Common Mistakes
Wrong Read haddr while htrans is IDLE.
With no transfer, the address means nothing. The core shows whatever its ALU holds.
Fix Qualify every address-phase signal with htrans.
Wrong Take a shown transfer back in the first cycle of an ERROR.
hready is 0: a wait. The manager must hold what it shows.
Fix Hold in the first cycle. Cancel in the second.
Wrong Test the adapter inside the core only.
The pipeline also throws the younger instruction away. A missing cancel in the adapter would never show.
Fix Test the adapter alone, with a bench that keeps the request up.
Remember
- The memory port of the pipeline has the two phases of AHB-Lite: a request, then the answer.
- ap_rv32 uses IDLE and NONSEQ only. While hready is 0 it holds everything.
- An ERROR response takes two cycles. The core registers the first and acts in the second.
- In that second cycle the data port shows IDLE: the transfer of the younger instruction is cancelled.