One Manager, Many Subordinates

Sending a request to the right block is half of a bus. The other half is bringing the right answer back, while the next request is already on the wires.

You Will Be Able To

  • Name the three answers that return from a subordinate, and the block that chooses among them.
  • State when the select is saved, and why the return multiplexer needs the saved copy.
  • Say whose ready counts during a wait, and what a silent subordinate does to the bus.
Lesson
S4.3
Time
24 min
Runs on
Your browser
Check
5 questions

At one clock edge of a capture, the address wires say SRAM and the data wires show the switches of GPIO. Nothing is broken. On this bus, the two sets of wires belong to different reads.

Before You Start

You need the address phase, the data phase and their overlap from S0.5, and the read multiplexer of S0.2. You need the decoder of Address Decode and Aliases, and HRESP from The Default Subordinate.

Five questions this page answers
Question
1Which three answers return from a subordinate, and why must one block choose all three?
2Why can the output of the decoder not steer the return multiplexer directly?
3At which clock edges does the saved select change?
4Whose ready reaches the manager while a transfer waits?
5What happens when a selected subordinate never answers, and why can the default subordinate not help?

The Idea: The Answer Belongs to an Earlier Address

One manager talks to many subordinates. Outward, that is the decoder's work: one select for each. Inward, many subordinates must share the one set of wires that leads back. A multiplexer does that. It is the read multiplexer of S0.2, grown to carry three answers at once.

The hard part is the question "whose answer?". On AHB, two transfers are in progress at once. The address wires show the transfer in its address phase. The answer on the way back belongs to the transfer in its data phase, which is the one before.

So the decoder, which reads the address wires, is always one transfer ahead of the answer. The fix is one small register. When an address is taken, the bus saves which subordinate was selected. That saved select steers the way back. This is the two-phase rule: decode in the address phase, return in the data phase.

New Words

New words on this page
WordMeaning
Return multiplexerThe block that passes the answers of one subordinate back to the manager: HRDATA, HREADY and HRESP.
Saved selectA register that remembers which subordinate owns the transfer in its data phase.
HREADYOUTThe ready output of one subordinate. The return multiplexer picks one HREADYOUT, and it becomes HREADY for the whole bus.
HangA bus that stays in a wait state for ever, because the subordinate that must answer never does.

The lab on this page prints two numbers at each edge. It calls the decoder's present choice route, and the saved select owner. Both use 0 for ROM, 1 for SRAM, 2 for GPIO behind its bridge, and 3 for the default subordinate.

One Picture: The Way Back

The Way Back, and the Select That Steers ItStep through the picture. Addresses travel to the right along the top. Answers return along the bottom.
  • Processor
  • Bus
  • Memory
The Way Back, and the Select That Steers ItStep through the picture. Addresses travel to the right along the top. Answers return along the bottom. component Manager one set of wires out,one set back component Decoder works on the addressthat is on the wiresnow component Saved select a register: who ownsthe transfer in itsdata phase component ROM, GPIO, SRAM three subordinates;each drives data, aready and a response component Returnmultiplexer passes the threeanswers of onesubordinate addressselect, saved whenHREADY is 1steersthree answers eachHRDATA, HREADY,HRESP
  1. component ROM, GPIO, SRAMthree subordinates; each drives data, a ready and a response
    • three answers each → Return multiplexer
  2. component Return multiplexerpasses the three answers of one subordinate
    • HRDATA, HREADY, HRESP → Manager
  3. component Managerone set of wires out, one set back
    • address → Decoder
  4. component Decoderworks on the address that is on the wires now
    • select, saved when HREADY is 1 → Saved select
  5. component Saved selecta register: who owns the transfer in its data phase
    • steers → Return multiplexer
  1. Many answers, one input

    Three subordinates each have a word to send. The manager has one input for read data. A multiplexer chooses whose answer passes.

  2. The decoder looks ahead

    The decoder works on the address wires. On AHB those wires show the transfer in its address phase. That is the next transfer, not the one being answered.

  3. Save the select

    At each edge that sees HREADY at 1, an address phase ends. At that edge the select of the decoder is copied into a register.

  4. Steer with the saved copy

    In the data phase, the saved select steers the multiplexer. The decoder is already busy with the address after.

  5. A wait

    While HREADY is 0, no address phase ends, so the register keeps its value. The slow subordinate keeps the way back until it is done.

Read the diagram as text
  • Manager: one set of wires out, one set back
  • Decoder: works on the address that is on the wires now
  • Saved select: a register: who owns the transfer in its data phase
  • ROM, GPIO, SRAM: three subordinates; each drives data, a ready and a response
  • Return multiplexer: passes the three answers of one subordinate
  • Manager → Decoder: address
  • Decoder → Saved select: select, saved when HREADY is 1
  • Saved select → Return multiplexer: steers
  • ROM, GPIO, SRAM → Return multiplexer: three answers each
  • Return multiplexer → Manager: HRDATA, HREADY, HRESP

One multiplexer carries all three answers, so they can never come from two different subordinates.

The register in the picture is the whole idea. It is as wide as one select, and it changes only at an edge that sees HREADY at 1.

The Smallest Example, Worked by Hand: Two Reads

Read ROM, then SRAM, back to back, with no wait state. Write down, cycle by cycle, what the decoder says and what the saved select holds.

Two reads, cycle by cycle
Cycle 1Cycle 2Cycle 3
Address wiresROM addressSRAM addressnothing
The decoder saysROMSRAMnobody
The saved select holdsnothing yetROMSRAM
The answer on the way backnonethe ROM wordthe SRAM word

Look down the column of cycle 2. The decoder says SRAM, and the answer that must pass is ROM's. Steer the multiplexer with the decoder, and the manager receives an SRAM word for its ROM read. The saved select, one row lower, has the right name.

Now let ROM need one wait state. Cycle 2 repeats: HREADY is 0, the SRAM address is held, and the saved select still holds ROM. A copy made at every clock edge would have moved on to SRAM one cycle too soon.

The Rules

  1. Rule 1: Three answers return together.

    HRDATA, HREADY and HRESP pass through one return multiplexer, so all three come from the same subordinate.

  2. Rule 2: Decode in the address phase, return in the data phase.

    The decoder works on the address wires. The return multiplexer works for the transfer before.

  3. Rule 3: The select is saved at every edge that sees HREADY at 1, and at no other edge.

    During a wait the saved select holds, however many cycles the wait lasts.

  4. Rule 4: Only the answering subordinate's ready counts.

    Its HREADYOUT becomes HREADY for the manager and for every subordinate. The others are not asked.

  5. Rule 5: A selected subordinate that never answers stops the bus.

    The default subordinate cannot help. The address had an owner, so the default subordinate was never selected.

Sources for These Rules

Arm AMBA AHB Protocol Specification, document IHI 0033, issue C. The read data and response multiplexor: section 4.3. A subordinate samples its select and the address only when HREADY is high: section 4.2. The overlap of the two phases: section 3.1.

On rule 5: a note in section 5.1.2 asks, in general, for a fixed maximum number of wait states in every subordinate. It gives no number, and nothing on the bus enforces it. The design team must.

The words "saved select" are ours. The specification says only that the decoder selects the subordinate during the data phase.

Now Run It

The lab at the end of this page has ROM, SRAM and the GPIO bridge on one AHB bus. The manager is test code, a bus-functional model. It reads ROM, then the GPIO pins, then SRAM, back to back. GPIO needs wait states. In the bus file of the lab, the saved select is two lines.

From the lab file read_slice.sv, without its reset lines. The name ready is HREADY. The name route is the choice of the decoder.
always_ff @(posedge clk)
  if (ready) owner <= route;   // save the select when an address phase ends
Printed by the lab at each rising clock edge. route and owner use 0 for ROM, 1 for SRAM, 2 for GPIO and 3 for the default subordinate.
e0 addr=00000000 active=1 route=0 owner=3 ready=1 error=0 data=00000000
e1 addr=10001004 active=1 route=2 owner=0 ready=1 error=0 data=00000013
e2 addr=80000000 active=1 route=1 owner=2 ready=0 error=0 data=00000000
e3 addr=80000000 active=1 route=1 owner=2 ready=0 error=0 data=00008042
e4 addr=80000000 active=1 route=1 owner=2 ready=1 error=0 data=00008042
e5 addr=00000000 active=0 route=0 owner=1 ready=1 error=0 data=face0001
Three Reads, RecordedStep through samples taken immediately before each rising edge updates the registers.
012345clkaddr00000000100010048000000000000000activeroute0210owner3021readyerrordata00000000000000130000000000008042face0001
Scroll horizontally ↔
  1. Cycle 0

    The address phase of the read of ROM. The decoder says 0, which is ROM.

  2. Cycle 1

    ROM answers: the saved select is 0 and the data is its word. The address wires already show GPIO, and route says 2.

  3. Cycle 2

    The data phase of the GPIO read begins: the saved select is 2. Ready is 0, so the SRAM address behind it is held.

  4. Cycle 3

    The switch word is on the data wires, but ready is still 0. The saved select stays at 2.

  5. Cycle 4

    Ready is 1: the GPIO read happens. This edge also takes the SRAM address.

  6. Cycle 5

    The saved select is 1, and SRAM returns its word. The address wires are back at zero.

Recorded Icarus samples from the editable lab.

Rule 2 at work: in row e1 the address is GPIO's and route is 2. Yet the saved select is 0, and the data is 00000013, the ROM word. Rule 3 at work: e2 and e3 see ready at 0, and the saved select stays at 2 through both.

Rule 4 at work: at e3 the data wires already show 00008042, the switches. SRAM could answer at once, but nobody asks it. GPIO holds the way back. At e4 GPIO says ready, and the same edge takes the SRAM address. At e5 the saved select is 1 and the word is face0001.

Now make GPIO silent. A second run of the lab holds its ready at 0 for good.

The last rows of the run with a silent GPIO. The test code gives up after eight edges.
e6 addr=80000000 active=1 route=1 owner=2 ready=0 error=0 data=00008042
e7 addr=80000000 active=1 route=1 owner=2 ready=0 error=0 data=00008042
Observation ended before all reads completed

Rule 5 at work: at e7, the saved select is still 2 and ready is still 0. The SRAM read behind it was never taken. On a real chip this looks like a slow read, until it never ends.

This lab leaves things out. It only reads, and its manager is test code. Its GPIO sits behind the bridge, which is why a GPIO read needs wait states.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
A saved select steers the way backSteer with the decoder's present outputThe answer always matches its own address. It costs one small register.
One multiplexer for all three answersJoin the ready wires of all subordinates in one gateIf any ready could end a transfer, an idle memory would end a GPIO read early. If all had to be ready, one slow device would stop every transfer on the chip.
One HREADY for the whole busA ready wire for each subordinate, straight to the managerEvery subordinate sees when the transfer in front has ended. A silent subordinate stops them all.

The last cost is rule 5 again. One shared "done" wire is simple, and it trusts every subordinate to answer. Before you add a device to a bus, ask how it answers when its clock has stopped.

The Trap: The Test That Reads One Device

A designer wires the return multiplexer to the decoder's present output, by mistake. The test reads ROM a thousand times, and every word is right. The bus is signed off.

The test could not fail. When the same device is read again and again, the decoder's output and the saved select are equal in every cycle. The fault needs two different subordinates in a row. It shows best when one of them waits.

So a test of the way back needs three things. It needs at least two subordinates, a different word in each, and a wait state on one of them. The lab above has all three.

At Scale

This page3 subordinates, 1 return multiplexer
A large chipa hundred subordinates, several stages

This section looks ahead. The lab has three subordinates and one multiplexer. A chip with a hundred has a tree of them.

  1. Record the address when it is taken

    Capture the address and the select at each edge with HREADY at 1. On a stalled bus, the address wires point one request ahead of the device that is stalling.

  2. Follow ready through each stage

    Probe ready at the subordinate and after each multiplexer. The first place where the two differ separates a silent device from a wiring fault.

  3. A way out of a hang

    A chip can add a timer that ends a transfer after too many wait states, with an error. A later lesson of this module shows what must happen next. The lesson on the watchdog covers the last resort: a reset.

  4. Signal by signal

    The Protocols track draws the same multiplexer with every AHB signal, in Decoder, multiplexor and default subordinate.

Try It

Run the three reads. For each row, name the read in its address phase and the read in its data phase.

In the line that creates read_slice, change the wait count from 32'd1 to 32'd0. Find the edge where the GPIO read now happens.

Give the SRAM word a new value and follow it back to the last row.

Add a fourth subordinate. The bus file has a switch called ADD_ID. Put #(.ADD_ID(1)) after the word read_slice. Then change the second read to address 0x4000_0000. The recorded result is in the fold-out.

Check the Fourth Subordinate
Recorded with the extra ID register enabled. Its route number is 4, and its word is 1d00cafe.
e1 addr=40000000 active=1 route=4 owner=0 ready=1 error=0 data=00000013
e2 addr=80000000 active=1 route=1 owner=4 ready=1 error=0 data=1d00cafe
e3 addr=00000000 active=0 route=0 owner=1 ready=1 error=0 data=face0001

At e2 route is 1, for the SRAM address, while the word on the way back is 1d00cafe from number 4. ROM and SRAM were not changed. The new subordinate needed a line in the decoder and an input on the return multiplexer.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: e0 addr=00000000 active=1 route=0 owner=3 ready=1 error=0 data=00000000 e1 addr=10001004 active=1 route=2 owner=0 ready=1 error=0 data=00000013 e2 addr=80000000 active=1 route=1 owner=2 ready=0 error=0 data=00000000 e3 addr=80000000 active=1 route=1 owner=2 ready=0 error=0 data=00008042 e4 addr=80000000 active=1 route=1 owner=2 ready=1 error=0 data=00008042 e5 addr=00000000 active=0 route=0 owner=1 ready=1 error=0 data=face0001
Read included file: read_slice.sv
// Read-only AHB slice with three regions and an optional expansion word.
module read_slice #(parameter bit ADD_ID = 0, GPIO_OFFLINE = 0) (
  input logic clk, reset_n,
  input logic [31:0] address,
  input logic [1:0] transfer,
  input logic [15:0] pins,
  input logic [31:0] wait_edges,
  output logic [2:0] route, owner,
  output logic [31:0] data,
  output logic ready, error
);
  localparam ROM=0, RAM=1, GPIO=2, MISS=3, ID_PAGE=4;
  logic [31:0] boot_rom [0:1023], sram [0:16383];
  logic [31:0] id_word, memory_word, bridge_data, paddr, pwdata, prdata;
  logic bridge_ready, bridge_error, miss_ready, miss_error;
  logic psel, penable, pwrite, pready, pslverr;
  logic [3:0] pstrb;

  always_comb begin
    if (address[31:12] == 20'h00000) route = ROM;
    else if (address[31:16] == 16'h8000) route = RAM;
    else if (address[31:12] == 20'h10001) route = GPIO;
    else if (ADD_ID && address[31:12] == 20'h40000) route = ID_PAGE;
    else route = MISS;
  end
  always_ff @(posedge clk) begin
    if (!reset_n) begin
      owner <= MISS;
      memory_word <= 0;
      id_word <= 0;
    end else if (ready) begin
      owner <= route;
      if (transfer[1] && route == ID_PAGE)
        id_word <= address[11:2] == 0 ? 32'h1d00cafe : 32'b0;
      if (transfer[1] && route == ROM) memory_word <= boot_rom[address[11:2]];
      if (transfer[1] && route == RAM) memory_word <= sram[address[15:2]];
    end
  end
  always_comb begin
    case (owner)
      ROM, RAM: {error, ready, data} = {2'b01, memory_word};
      ID_PAGE:  {error, ready, data} = {2'b01, id_word};
      GPIO:     {error, ready, data} = {bridge_error, bridge_ready, bridge_data};
      default:  {error, ready, data} = {miss_error, miss_ready, 32'b0};
    endcase
  end
  ahb_default fallback(clk, reset_n, route == MISS, transfer, ready, miss_ready, miss_error);
  ahb_apb_bridge bridge(
    .hclk(clk), .hresetn(reset_n), .hsel(route == GPIO), .haddr(address), .htrans(transfer),
    .hwrite(1'b0), .hprot(4'b0011), .hwdata(32'b0), .hwstrb(4'b0), .hready(ready),
    .hreadyout(bridge_ready), .hresp(bridge_error), .hrdata(bridge_data),
    .psel(psel), .penable(penable), .pwrite(pwrite), .paddr(paddr), .pwdata(pwdata),
    .pstrb(pstrb), .pprot(), .pready(pready && !GPIO_OFFLINE), .prdata(prdata), .pslverr(pslverr)
  );
  io_lab_regs registers(
    .pclk(clk), .presetn(reset_n), .psel(psel), .penable(penable), .pwrite(pwrite),
    .paddr(paddr), .pwdata(pwdata), .pstrb(pstrb), .pins(pins), .wait_edges(wait_edges),
    .pready(pready), .pslverr(pslverr), .prdata(prdata), .uart_selected(),
    .gpio_selected(), .tx_accepted(), .tx_byte()
  );
endmodule
Read included file: io_lab_regs.sv
// Two word registers for the S1 labs. Inputs have already crossed into PCLK.
// The UART model exposes accepted bytes; it does not model the serial shifter.
module io_lab_regs (
  input  logic        pclk, presetn,
  input  logic        psel, penable, pwrite,
  input  logic [31:0] paddr, pwdata,
  input  logic [3:0]  pstrb,
  input  logic [15:0] pins,
  input  logic [31:0] wait_edges,
  output logic        pready, pslverr,
  output logic [31:0] prdata,
  output logic        uart_selected, gpio_selected,
  output logic        tx_accepted,
  output logic [7:0]  tx_byte
);
  localparam logic [19:0] UART_PAGE = 20'h10000, GPIO_PAGE = 20'h10001;
  logic [31:0] elapsed;
  wire tx_word = uart_selected && paddr[11:0] == 12'h000 && pwrite;
  wire input_word = gpio_selected && paddr[11:0] == 12'h004 && !pwrite;

  assign uart_selected = psel && paddr[31:12] == UART_PAGE;
  assign gpio_selected = psel && paddr[31:12] == GPIO_PAGE;
  assign prdata = input_word ? {16'b0, pins} : 32'b0;
  assign pready = psel && penable && elapsed >= wait_edges;
  assign pslverr = pready && !(tx_word || input_word);
  assign tx_accepted = tx_word && pready && pstrb[0];
  assign tx_byte = pwdata[7:0];

  always_ff @(posedge pclk or negedge presetn) begin
    if (!presetn) elapsed <= 0;
    else if (psel && penable && !pready) elapsed <= elapsed + 1;
    else elapsed <= 0;
  end
endmodule
Read included file: ahb_apb_bridge.sv
`timescale 1ns/1ns
// ahb_apb_bridge.sv: an AHB subordinate that turns each accepted transfer into one APB transfer
// (read-only). The address phase is registered when HSEL, HREADY and a NONSEQ or SEQ transfer are
// all present. During SETUP and unfinished ACCESS the bridge keeps HREADYOUT low. A successful APB
// completion ends the AHB data phase with OKAY and HRDATA = PRDATA. A failed APB completion starts
// the two-cycle ERROR (HREADYOUT low then high, HRESP high in both). HWDATA and HWSTRB are data-phase signals the manager holds
// through the wait states, so PWDATA and PSTRB are driven from them directly; PADDR is the aligned
// word address (IHI 0024E 2.1.1 leaves an unaligned PADDR unpredictable). HPROT maps onto PPROT:
// privileged from HPROT[1], instruction from HPROT[0] = 0, and the access is Secure.
// A write therefore costs one setup and one access cycle: two cycles of data phase with no APB wait.
module ahb_apb_bridge (
  input  logic        hclk,
  input  logic        hresetn,
  input  logic        hsel,
  input  logic [31:0] haddr,
  input  logic [1:0]  htrans,
  input  logic        hwrite,
  input  logic [3:0]  hprot,
  input  logic [31:0] hwdata,
  input  logic [3:0]  hwstrb,
  input  logic        hready,
  output logic        hreadyout,
  output logic        hresp,
  output logic [31:0] hrdata,
  output logic        psel,
  output logic        penable,
  output logic        pwrite,
  output logic [31:0] paddr,
  output logic [31:0] pwdata,
  output logic [3:0]  pstrb,
  output logic [2:0]  pprot,
  input  logic        pready,
  input  logic [31:0] prdata,
  input  logic        pslverr);
  localparam int IDLE = 0, SETUP = 1, ACCESS = 2, ERR1 = 3, ERR2 = 4;
  bit trace = 0;
  int st = IDLE;
  logic        wr = 0;
  logic [31:0] a = 0;
  logic [2:0]  prot = 0;
  wire take = hsel && hready && htrans[1];
  assign psel      = (st == SETUP || st == ACCESS);
  assign penable   = (st == ACCESS);
  assign pwrite    = wr;
  assign paddr     = {a[31:2], 2'b00};     // APB leaves an unaligned PADDR unpredictable: the word, and PSTRB picks the bytes
  assign pwdata    = wr ? hwdata : 32'h0;
  assign pstrb     = wr ? hwstrb : 4'h0;
  assign pprot     = prot;
  assign hreadyout = (st == IDLE || st == ERR2) ? 1'b1 : (st == ACCESS) ? (pready === 1'b1 && pslverr !== 1'b1) : 1'b0;
  assign hresp     = (st == ERR1 || st == ERR2);
  assign hrdata    = (st == ACCESS) ? prdata : 32'h0;
  always @(posedge hclk or negedge hresetn) begin
    if (!hresetn) st <= IDLE;
    else begin
      case (st)
        SETUP:  st <= ACCESS;
        ACCESS: if (pready === 1'b1) begin
                  if (trace) $display("apb e%0d: %0s 0x%03h%0s", ($time - 5) / 10, wr ? "write" : "read", a[11:0], pslverr === 1'b1 ? " PSLVERR" : "");
                  st <= (pslverr === 1'b1) ? ERR1 : IDLE;
                end
        ERR1:   st <= ERR2;
        default: st <= IDLE;
      endcase
      // A new address phase can be taken whenever HREADY is 1: in IDLE, in the last cycle of ERROR,
      // or at the edge an access completes with OKAY.
      if (take && (st == IDLE || st == ERR2 || (st == ACCESS && pready === 1'b1 && pslverr !== 1'b1))) begin
        wr <= hwrite; a <= haddr; prot <= {~hprot[0], 1'b0, hprot[1]}; st <= SETUP;
      end
    end
  end
endmodule
Read included file: ahb_default.sv
module ahb_default (
  input  logic hclk,
  input  logic hresetn,
  input  logic hsel,
  input  logic [1:0] htrans,
  input  logic hready,
  output logic hreadyout,
  output logic hresp
);
  bit d_err = 0, e_two = 0;
  initial begin hreadyout = 1; hresp = 0; end
  always @(posedge hclk) begin
    if (!hresetn) begin
      d_err = 0; e_two = 0; hreadyout <= 1; hresp <= 0;
    end else begin
      if (d_err) begin
        if (!hreadyout) e_two = 1;
        else d_err = 0;
      end
      if (hsel && hready) begin d_err = htrans[1]; e_two = 0; end
      hreadyout <= !(d_err && !e_two);
      hresp <= d_err;
    end
  end
endmodule

Check Yourself

Predict the outputGPIO is silent: it never raises its ready. The test code watches eight edges. What is the last line the lab prints?
module tb;
  bit clk, reset_n;
  always #5 clk = !clk;
  logic [31:0] address = 0;
  logic [1:0] transfer = 0;
  wire [2:0] route, owner;
  wire [31:0] data;
  wire ready, error;
  read_slice #(.GPIO_OFFLINE(1)) bus(clk, reset_n, address, transfer, 16'h8042, 32'd1,
                 route, owner, data, ready, error);
  logic [31:0] reads [0:2];
  int next_read, edge_no;
  bit data_phase;
  initial begin
    bus.boot_rom[0] = 32'h00000013;
    bus.sram[0] = 32'hface0001;
    reads[0] = 32'h00000000;
    reads[1] = 32'h10001004;
    reads[2] = 32'h80000000;
    @(negedge clk);
    reset_n = 1;
    // Stop observing after eight edges even if the selected target stays silent.
    repeat (8) begin
      transfer = next_read < 3 ? 2'b10 : 2'b00;
      address = next_read < 3 ? reads[next_read] : 0;
      @(posedge clk);
      $display("e%0d addr=%08h active=%b route=%d owner=%d ready=%b error=%b data=%08h",
               edge_no, address, transfer[1], route, owner, ready, error, data);
      if (ready) begin
        if (data_phase && error) $fatal(1, "Read failed");
        if (data_phase && next_read == 3) $finish;
        data_phase = transfer[1];
        if (transfer[1]) next_read++;
      end
      edge_no++;
      @(negedge clk);
    end
    $display("Observation ended before all reads completed");
    $finish;
  end
endmodule
Read included file: read_slice.sv
// Read-only AHB slice with three regions and an optional expansion word.
module read_slice #(parameter bit ADD_ID = 0, GPIO_OFFLINE = 0) (
  input logic clk, reset_n,
  input logic [31:0] address,
  input logic [1:0] transfer,
  input logic [15:0] pins,
  input logic [31:0] wait_edges,
  output logic [2:0] route, owner,
  output logic [31:0] data,
  output logic ready, error
);
  localparam ROM=0, RAM=1, GPIO=2, MISS=3, ID_PAGE=4;
  logic [31:0] boot_rom [0:1023], sram [0:16383];
  logic [31:0] id_word, memory_word, bridge_data, paddr, pwdata, prdata;
  logic bridge_ready, bridge_error, miss_ready, miss_error;
  logic psel, penable, pwrite, pready, pslverr;
  logic [3:0] pstrb;

  always_comb begin
    if (address[31:12] == 20'h00000) route = ROM;
    else if (address[31:16] == 16'h8000) route = RAM;
    else if (address[31:12] == 20'h10001) route = GPIO;
    else if (ADD_ID && address[31:12] == 20'h40000) route = ID_PAGE;
    else route = MISS;
  end
  always_ff @(posedge clk) begin
    if (!reset_n) begin
      owner <= MISS;
      memory_word <= 0;
      id_word <= 0;
    end else if (ready) begin
      owner <= route;
      if (transfer[1] && route == ID_PAGE)
        id_word <= address[11:2] == 0 ? 32'h1d00cafe : 32'b0;
      if (transfer[1] && route == ROM) memory_word <= boot_rom[address[11:2]];
      if (transfer[1] && route == RAM) memory_word <= sram[address[15:2]];
    end
  end
  always_comb begin
    case (owner)
      ROM, RAM: {error, ready, data} = {2'b01, memory_word};
      ID_PAGE:  {error, ready, data} = {2'b01, id_word};
      GPIO:     {error, ready, data} = {bridge_error, bridge_ready, bridge_data};
      default:  {error, ready, data} = {miss_error, miss_ready, 32'b0};
    endcase
  end
  ahb_default fallback(clk, reset_n, route == MISS, transfer, ready, miss_ready, miss_error);
  ahb_apb_bridge bridge(
    .hclk(clk), .hresetn(reset_n), .hsel(route == GPIO), .haddr(address), .htrans(transfer),
    .hwrite(1'b0), .hprot(4'b0011), .hwdata(32'b0), .hwstrb(4'b0), .hready(ready),
    .hreadyout(bridge_ready), .hresp(bridge_error), .hrdata(bridge_data),
    .psel(psel), .penable(penable), .pwrite(pwrite), .paddr(paddr), .pwdata(pwdata),
    .pstrb(pstrb), .pprot(), .pready(pready && !GPIO_OFFLINE), .prdata(prdata), .pslverr(pslverr)
  );
  io_lab_regs registers(
    .pclk(clk), .presetn(reset_n), .psel(psel), .penable(penable), .pwrite(pwrite),
    .paddr(paddr), .pwdata(pwdata), .pstrb(pstrb), .pins(pins), .wait_edges(wait_edges),
    .pready(pready), .pslverr(pslverr), .prdata(prdata), .uart_selected(),
    .gpio_selected(), .tx_accepted(), .tx_byte()
  );
endmodule
Read included file: io_lab_regs.sv
// Two word registers for the S1 labs. Inputs have already crossed into PCLK.
// The UART model exposes accepted bytes; it does not model the serial shifter.
module io_lab_regs (
  input  logic        pclk, presetn,
  input  logic        psel, penable, pwrite,
  input  logic [31:0] paddr, pwdata,
  input  logic [3:0]  pstrb,
  input  logic [15:0] pins,
  input  logic [31:0] wait_edges,
  output logic        pready, pslverr,
  output logic [31:0] prdata,
  output logic        uart_selected, gpio_selected,
  output logic        tx_accepted,
  output logic [7:0]  tx_byte
);
  localparam logic [19:0] UART_PAGE = 20'h10000, GPIO_PAGE = 20'h10001;
  logic [31:0] elapsed;
  wire tx_word = uart_selected && paddr[11:0] == 12'h000 && pwrite;
  wire input_word = gpio_selected && paddr[11:0] == 12'h004 && !pwrite;

  assign uart_selected = psel && paddr[31:12] == UART_PAGE;
  assign gpio_selected = psel && paddr[31:12] == GPIO_PAGE;
  assign prdata = input_word ? {16'b0, pins} : 32'b0;
  assign pready = psel && penable && elapsed >= wait_edges;
  assign pslverr = pready && !(tx_word || input_word);
  assign tx_accepted = tx_word && pready && pstrb[0];
  assign tx_byte = pwdata[7:0];

  always_ff @(posedge pclk or negedge presetn) begin
    if (!presetn) elapsed <= 0;
    else if (psel && penable && !pready) elapsed <= elapsed + 1;
    else elapsed <= 0;
  end
endmodule
Read included file: ahb_apb_bridge.sv
`timescale 1ns/1ns
// ahb_apb_bridge.sv: an AHB subordinate that turns each accepted transfer into one APB transfer
// (read-only). The address phase is registered when HSEL, HREADY and a NONSEQ or SEQ transfer are
// all present. During SETUP and unfinished ACCESS the bridge keeps HREADYOUT low. A successful APB
// completion ends the AHB data phase with OKAY and HRDATA = PRDATA. A failed APB completion starts
// the two-cycle ERROR (HREADYOUT low then high, HRESP high in both). HWDATA and HWSTRB are data-phase signals the manager holds
// through the wait states, so PWDATA and PSTRB are driven from them directly; PADDR is the aligned
// word address (IHI 0024E 2.1.1 leaves an unaligned PADDR unpredictable). HPROT maps onto PPROT:
// privileged from HPROT[1], instruction from HPROT[0] = 0, and the access is Secure.
// A write therefore costs one setup and one access cycle: two cycles of data phase with no APB wait.
module ahb_apb_bridge (
  input  logic        hclk,
  input  logic        hresetn,
  input  logic        hsel,
  input  logic [31:0] haddr,
  input  logic [1:0]  htrans,
  input  logic        hwrite,
  input  logic [3:0]  hprot,
  input  logic [31:0] hwdata,
  input  logic [3:0]  hwstrb,
  input  logic        hready,
  output logic        hreadyout,
  output logic        hresp,
  output logic [31:0] hrdata,
  output logic        psel,
  output logic        penable,
  output logic        pwrite,
  output logic [31:0] paddr,
  output logic [31:0] pwdata,
  output logic [3:0]  pstrb,
  output logic [2:0]  pprot,
  input  logic        pready,
  input  logic [31:0] prdata,
  input  logic        pslverr);
  localparam int IDLE = 0, SETUP = 1, ACCESS = 2, ERR1 = 3, ERR2 = 4;
  bit trace = 0;
  int st = IDLE;
  logic        wr = 0;
  logic [31:0] a = 0;
  logic [2:0]  prot = 0;
  wire take = hsel && hready && htrans[1];
  assign psel      = (st == SETUP || st == ACCESS);
  assign penable   = (st == ACCESS);
  assign pwrite    = wr;
  assign paddr     = {a[31:2], 2'b00};     // APB leaves an unaligned PADDR unpredictable: the word, and PSTRB picks the bytes
  assign pwdata    = wr ? hwdata : 32'h0;
  assign pstrb     = wr ? hwstrb : 4'h0;
  assign pprot     = prot;
  assign hreadyout = (st == IDLE || st == ERR2) ? 1'b1 : (st == ACCESS) ? (pready === 1'b1 && pslverr !== 1'b1) : 1'b0;
  assign hresp     = (st == ERR1 || st == ERR2);
  assign hrdata    = (st == ACCESS) ? prdata : 32'h0;
  always @(posedge hclk or negedge hresetn) begin
    if (!hresetn) st <= IDLE;
    else begin
      case (st)
        SETUP:  st <= ACCESS;
        ACCESS: if (pready === 1'b1) begin
                  if (trace) $display("apb e%0d: %0s 0x%03h%0s", ($time - 5) / 10, wr ? "write" : "read", a[11:0], pslverr === 1'b1 ? " PSLVERR" : "");
                  st <= (pslverr === 1'b1) ? ERR1 : IDLE;
                end
        ERR1:   st <= ERR2;
        default: st <= IDLE;
      endcase
      // A new address phase can be taken whenever HREADY is 1: in IDLE, in the last cycle of ERROR,
      // or at the edge an access completes with OKAY.
      if (take && (st == IDLE || st == ERR2 || (st == ACCESS && pready === 1'b1 && pslverr !== 1'b1))) begin
        wr <= hwrite; a <= haddr; prot <= {~hprot[0], 1'b0, hprot[1]}; st <= SETUP;
      end
    end
  end
endmodule
Read included file: ahb_default.sv
module ahb_default (
  input  logic hclk,
  input  logic hresetn,
  input  logic hsel,
  input  logic [1:0] htrans,
  input  logic hready,
  output logic hreadyout,
  output logic hresp
);
  bit d_err = 0, e_two = 0;
  initial begin hreadyout = 1; hresp = 0; end
  always @(posedge hclk) begin
    if (!hresetn) begin
      d_err = 0; e_two = 0; hreadyout <= 1; hresp <= 0;
    end else begin
      if (d_err) begin
        if (!hreadyout) e_two = 1;
        else d_err = 0;
      end
      if (hsel && hready) begin d_err = htrans[1]; e_two = 0; end
      hreadyout <= !(d_err && !e_two);
      hresp <= d_err;
    end
  end
endmodule
ConceptWhich three answers pass through the return multiplexer together?
ConceptAt which edges does the saved select take a new value?
ConceptIn the recorded run, row e3 shows the SRAM address and the data 00008042. Whose word is that?
ConceptA selected subordinate never raises its ready. Why does the default subordinate not end the transfer?

Common Mistakes

Wrong Steer the return multiplexer with the decoder's present output.

The decoder already works on the next address.

Fix Steer it with the select saved when the address was taken.

Wrong Copy the select at every clock edge.

A wait keeps the same transfer in its data phase for several edges.

Fix Copy it only at an edge that sees HREADY at 1.

Wrong Take a steady word on the data wires as proof that the read is over.

The word can appear while ready is still 0.

Fix Find the edge with ready at 1, and read the data and the response there.

Remember

  • One return multiplexer carries HRDATA, HREADY and HRESP from one subordinate.
  • The decoder works in the address phase; the way back works in the data phase.
  • The select is saved at every edge with HREADY at 1, and holds through a wait.
  • Only the answering subordinate's ready reaches the manager.
  • A silent subordinate hangs the bus, and the default subordinate cannot help.

Practice

Report a problem with this page