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.

Five questions this page answers
Question
1What are the two phases of an AHB-Lite transfer?
2Which transfer types does ap_rv32 use?
3What must a manager do while hready is 0?
4How many cycles does an ERROR response take?
5What 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

New words on this page
WordMeaning
Manager, subordinateThe block that starts transfers on a bus, and the block that answers them.
Address phaseThe cycle in which the manager shows the address, the direction and the size of a transfer.
Data phaseThe cycle or cycles after it, in which the data moves and the subordinate answers.
hready, hrespFrom the subordinate: the data phase ends in this cycle, and the transfer is refused.
Transfer typeThe signal htrans. ap_rv32 uses two values: IDLE, no transfer, and NONSEQ, a single transfer.
BurstSeveral transfers in a row that a manager marks as one group. ap_rv32 uses none.
ERROR responseThe answer of a subordinate that refuses a transfer: hresp at 1, for two cycles.
Port adapterA small block that turns one kind of port into another. Here: the adapter.
haddr, hwrite, hsizeThe address of a transfer, 1 for a write, and the number of bytes: 1, 2 or 4.
hwdata, hrdataThe 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

Two Phases, Seen from Both SidesStep through the picture. On the left is the simple memory port of the pipeline. On the right is the AHB-Lite bus of the chip.
  • Processor
  • Control / FSM
  • Bus
  • Memory
Two Phases, Seen From Both SidesStep through the picture. On the left is the simple memory port of the pipeline. On the right is the AHB-Lite bus of the chip. component Pipeline EX shows a request,MEM takes theanswer component Adapter names the transfer,and remembers anERROR component AHB-Lite bus address phase, thendata phase component Memory or device answers: ready, orwait, or ERROR component One flip-flop the cycle before wasthe first of anERROR request: address,read or write, sizehtrans, haddr, hwrite,hsizethe transferhready, hresp, hrdataready, read datahresp at 1, hready at0
  1. component PipelineEX shows a request, MEM takes the answer
    • request: address, read or write, size → Adapter
  2. component Adapternames the transfer, and remembers an ERROR
    • htrans, haddr, hwrite, hsize → AHB-Lite bus
    • ready, read data → Pipeline
    • hresp at 1, hready at 0 → One flip-flop
  3. component AHB-Lite busaddress phase, then data phase
    • the transfer → Memory or device
  4. component Memory or deviceanswers: ready, or wait, or ERROR
    • hready, hresp, hrdata → Adapter
  5. component One flip-flopthe cycle before was the first of an ERROR
  1. 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.

  2. 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.

  3. 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.

  4. A wait

    The memory holds hready at 0. Nothing may change: the data phase and the next address phase both stay as they are.

  5. 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.

The signals of one bus port of ap_rv32
SignalDirectionOn the instruction portOn the data port
haddroutthe address of the next instructionthe address of the load or store
htransoutNONSEQ to fetch, IDLE when asleepNONSEQ for a load or store, IDLE otherwise
hwriteoutalways 01 for a store
hsizeoutalways four bytesone, two or four bytes
hburst, hmastlockoutalways 0always 0
hprotouta constant: an instruction fetcha constant: a data access
hwdataoutalways 0the store data, in the data phase
hrdatainthe instructionthe load data
hready, hrespindone, and ERRORdone, and ERROR

Eleven signals on each port.

The Smallest Example, Worked by Hand

Four instructions that use the data port
Address, in hexInstructionWhat it is for
014sw x5, 0(x8)a store
018lw x6, 0(x8)a load, right behind it
01clw x7, 0(x9)a load that the bus refuses
020sw 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.

Two transfers, overlapped
Cyclehtrans, haddr, hwritehwdatahrdataWhat happens
1NONSEQ, 8000_0000, 1address phase of the store
2NONSEQ, 8000_0000, 00000_0055data phase of the store, and address phase of the load
3the next transfer, or IDLE0000_0055data 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

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

Rows 1 to 6, rows 22 and 23, and the last line of the lab. The signals are recorded from the real core ap_rv32. The notes are made by the analyser.
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=0

Rule 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.

Rows 1 to 9 of the second run, recorded from the real core. The memory adds one wait state to each transfer.
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

Rule 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 choiceThe alternativeWhat the choice buys, and costs
A native port with the timing of the busA port with address and data in one cycleA thin adapter with no state machine. The pipeline must be built around two phases from the start.
Only IDLE and NONSEQBursts for a run of fetchesA simple manager that any subordinate understands. A cache line could be read faster with a burst.
Register the first ERROR cycleAct on hresp in the same cycleNo 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 ERRORLet it runA store behind a trapping load never reaches a device. The fetch port does not need this.
Two portsOne port, sharedA fetch and a load in the same cycle. The chip needs a fabric that takes two managers.
Drive unused store data lanes to 0Leave them as they areWaveforms 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 lab1 data port, 1 memory, 4 transfers
ap_soc2 manager ports of the core, a fabric, memories and bridges

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.

  1. 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.

  2. 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.

  3. 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.

  4. 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?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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=0

Check Yourself

Predict the outputTwo stores and two loads use the RAM. The memory makes every transfer wait two cycles, and nothing is refused. What does the last line say?
// Two stores and two loads. The memory makes every transfer wait two cycles. What does the last line say?
// Test code: a protocol analyser. The arrays hold the signals of the data port of the real core ap_rv32,
// recorded cycle by cycle while it ran a small program. The analyser reads them as a subordinate would, and
// says what each cycle is. It holds no logic of the core.
module tb;
  localparam int CYCLES = 14;
  logic [1:0]  htrans [0:13];   // 00: IDLE, no transfer. 10: NONSEQ, a single transfer
  logic [31:0] haddr [0:13];
  logic        hwrite [0:13];
  logic [2:0]  hsize [0:13];    // 0, 1, 2: one, two or four bytes
  logic [31:0] hwdata [0:13], hrdata [0:13];
  logic        hready [0:13], hresp [0:13];
  task automatic recorded;   // one row for each clock cycle, as the real core and its memory gave them
    htrans[0] = 2'b00;  haddr[0] = 32'h00000007;  hwrite[0] = 0;  hsize[0] = 0;  hwdata[0] = 32'h00000000;  hrdata[0] = 32'h00000000;  hready[0] = 1;  hresp[0] = 0;
    htrans[1] = 2'b10;  haddr[1] = 32'h80000000;  hwrite[1] = 1;  hsize[1] = 2;  hwdata[1] = 32'h00000000;  hrdata[1] = 32'h00000000;  hready[1] = 1;  hresp[1] = 0;
    htrans[2] = 2'b10;  haddr[2] = 32'h80000004;  hwrite[2] = 1;  hsize[2] = 2;  hwdata[2] = 32'h00000007;  hrdata[2] = 32'h00000000;  hready[2] = 0;  hresp[2] = 0;
    htrans[3] = 2'b10;  haddr[3] = 32'h80000004;  hwrite[3] = 1;  hsize[3] = 2;  hwdata[3] = 32'h00000007;  hrdata[3] = 32'h00000000;  hready[3] = 0;  hresp[3] = 0;
    htrans[4] = 2'b10;  haddr[4] = 32'h80000004;  hwrite[4] = 1;  hsize[4] = 2;  hwdata[4] = 32'h00000007;  hrdata[4] = 32'h00000000;  hready[4] = 1;  hresp[4] = 0;
    htrans[5] = 2'b10;  haddr[5] = 32'h80000000;  hwrite[5] = 0;  hsize[5] = 2;  hwdata[5] = 32'h00000007;  hrdata[5] = 32'h00000000;  hready[5] = 0;  hresp[5] = 0;
    htrans[6] = 2'b10;  haddr[6] = 32'h80000000;  hwrite[6] = 0;  hsize[6] = 2;  hwdata[6] = 32'h00000007;  hrdata[6] = 32'h00000000;  hready[6] = 0;  hresp[6] = 0;
    htrans[7] = 2'b10;  haddr[7] = 32'h80000000;  hwrite[7] = 0;  hsize[7] = 2;  hwdata[7] = 32'h00000007;  hrdata[7] = 32'h00000000;  hready[7] = 1;  hresp[7] = 0;
    htrans[8] = 2'b10;  haddr[8] = 32'h80000004;  hwrite[8] = 0;  hsize[8] = 2;  hwdata[8] = 32'h00000000;  hrdata[8] = 32'h00000000;  hready[8] = 0;  hresp[8] = 0;
    htrans[9] = 2'b10;  haddr[9] = 32'h80000004;  hwrite[9] = 0;  hsize[9] = 2;  hwdata[9] = 32'h00000000;  hrdata[9] = 32'h00000000;  hready[9] = 0;  hresp[9] = 0;
    htrans[10] = 2'b10;  haddr[10] = 32'h80000004;  hwrite[10] = 0;  hsize[10] = 2;  hwdata[10] = 32'h00000000;  hrdata[10] = 32'h00000007;  hready[10] = 1;  hresp[10] = 0;
    htrans[11] = 2'b00;  haddr[11] = 32'h00000000;  hwrite[11] = 0;  hsize[11] = 0;  hwdata[11] = 32'h00000000;  hrdata[11] = 32'h00000000;  hready[11] = 0;  hresp[11] = 0;
    htrans[12] = 2'b00;  haddr[12] = 32'h00000000;  hwrite[12] = 0;  hsize[12] = 0;  hwdata[12] = 32'h00000000;  hrdata[12] = 32'h00000000;  hready[12] = 0;  hresp[12] = 0;
    htrans[13] = 2'b00;  haddr[13] = 32'h00000000;  hwrite[13] = 0;  hsize[13] = 0;  hwdata[13] = 32'h00000000;  hrdata[13] = 32'h00000007;  hready[13] = 1;  hresp[13] = 0;
  endtask

  // ---- The analyser. It remembers one thing: the transfer whose data phase is running.
  bit          busy, b_write, b_seen;
  logic [31:0] b_addr, b_data;
  int          transfers, waits, errors, cancelled, broken;
  string       note, kind;
  function automatic string add(input string list, input string item);
    if (list == "") return item;
    return {list, "; ", item};
  endfunction
  initial begin
    recorded();
    busy = 0;  transfers = 0;  waits = 0;  errors = 0;  cancelled = 0;  broken = 0;
    $display("cy | type address  rw bytes | hwdata   hrdata   | hready hresp | what this cycle is");
    for (int n = 0; n < CYCLES; n++) begin
      note = "";
      // Three rules that a manager of this kind must keep.
      if (htrans[n] != 2'b00 && htrans[n] != 2'b10) broken++;                               // only IDLE and NONSEQ
      if (n > 0 && htrans[n - 1] == 2'b10 && !hready[n - 1] && !hresp[n - 1] &&
          (htrans[n] != 2'b10 || haddr[n] != haddr[n - 1] || hwrite[n] != hwrite[n - 1] || hsize[n] != hsize[n - 1])) broken++;   // held while the bus waits
      if (busy) begin
        if (b_write && b_seen && hwdata[n] != b_data) broken++;                             // write data held for the whole data phase
        b_data = hwdata[n];  b_seen = 1;
        if (!hready[n] && !hresp[n]) begin note = add(note, "data phase: wait");  waits++; end
        else if (!hready[n] && hresp[n]) begin note = add(note, "data phase: ERROR, first cycle");  errors++; end
        else if (hresp[n]) note = add(note, "data phase: ERROR, second cycle");
        else if (b_write) note = add(note, $sformatf("data phase: write done, mem[%08h] = %08h", b_addr, hwdata[n]));
        else note = add(note, $sformatf("data phase: read done, %08h", hrdata[n]));
      end
      if (htrans[n] == 2'b10) begin
        if (hready[n] && hwrite[n]) note = add(note, $sformatf("address phase: write at %08h", haddr[n]));
        else if (hready[n]) note = add(note, $sformatf("address phase: read at %08h", haddr[n]));
        else note = add(note, "address phase: held, the bus is not ready");
      end
      else if (n > 0 && htrans[n - 1] == 2'b10 && !hready[n - 1]) begin note = add(note, "IDLE: the transfer shown before is cancelled");  cancelled++; end
      case (htrans[n])
        2'b00: kind = "IDLE";
        2'b10: kind = "NSEQ";
        default: kind = "????";
      endcase
      $display("%2d | %s %08h %s  %0d     | %08h %08h | %0d      %0d     | %s", n + 1, kind, haddr[n], hwrite[n] ? "w" : "r", 1 << hsize[n], hwdata[n], hrdata[n], hready[n], hresp[n], note);
      if (hready[n]) begin                         // the address phase is accepted: its data phase is the next cycle
        busy = (htrans[n] == 2'b10);
        if (busy) begin transfers++;  b_write = hwrite[n];  b_addr = haddr[n];  b_seen = 0; end
      end
    end
    $display("transfers=%0d wait cycles=%0d errors=%0d cancelled=%0d rules broken=%0d", transfers, waits, errors, cancelled, broken);
  end
endmodule
ConceptWhat are the two phases of an AHB-Lite transfer?
ConceptWhich transfer types does ap_rv32 use?
ConceptWhat must a manager do while hready is 0?
ConceptHow many cycles does an ERROR response take?
ConceptWhat does the data port of ap_rv32 do with the transfer behind an ERROR?

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.

Practice

Report a problem with this page