Hybrid: A Model Core with RTL Peripherals

Keep the peripherals as RTL, replace only the core with a fast model, and join them with a transactor. Software runs faster, devices stay real. The price is paid at every device access, and in time.

You Will Be Able To

  • Explain how a hybrid serves fetches, local data and device accesses.
  • Read a hybrid's counters and say where its simulated time goes.
  • Explain the instruction quantum, and what it does to timers and interrupt latency.
  • Say which timing a hybrid keeps and which it gives up.
Lesson
V13.3
Time
27 min
Runs on
Your browser
Check
5 questions

The virtual platform passed the driver, and RTL takes all night. The team wants the real SPI controller and the real pads, with software at more than a hundred thousand instructions a second.

One Function Call Becomes Bus CyclesThe model core calls a function for a device access. The transactor turns it into AHB phases in the RTL.
  • Model
  • Memory
  • Control / FSM
  • Driver
  • Bus
  • Peripheral
  • Pads
  • Data
  • Transaction / TLM
One Function Call Becomes Bus CyclesThe model core calls a function for a device access. The transactor turns it into AHB phases in the RTL. model ap_iss runs instructions in C memory Local ROM, SRAM host byte arrays control read(addr, size) a C callback driver Transactor synthesizable RTL bus AHB fabric RTL, real wait states peripheral RTL peripherals UART, SPI, timers,PLIC pads Pads RTL pin mux no busdevice accessrequestaddress, data phaseAPB, AHBpins
  1. model ap_issruns instructions in C
    • no bus → Local ROM, SRAM
    • device access → read(addr, size)
  2. memory Local ROM, SRAMhost byte arrays
  3. control read(addr, size)a C callback
    • request → Transactor
  4. driver Transactorsynthesizable RTL
    • address, data phase → AHB fabric
  5. bus AHB fabricRTL, real wait states
    • APB, AHB → RTL peripherals
  6. peripheral RTL peripheralsUART, SPI, timers, PLIC
    • pins → Pads
  7. pads PadsRTL pin mux
  1. Local work is free

    Fetches and SRAM accesses stay in the model: one step, no bus.

  2. A device access is a call

    The model calls a C function with an address and a size.

  3. The transactor

    It holds the request as AHB NONSEQ, waits for HREADY, and returns the data and HRESP.

  4. Real devices, real pads

    From the fabric on, everything is the chip's RTL.

Read the diagram as text
  • ap_iss: runs instructions in C
  • Local ROM, SRAM: host byte arrays
  • read(addr, size): a C callback
  • Transactor: synthesizable RTL
  • AHB fabric: RTL, real wait states
  • RTL peripherals: UART, SPI, timers, PLIC
  • Pads: RTL pin mux
  • ap_iss → Local ROM, SRAM: no bus
  • ap_iss → read(addr, size): device access
  • read(addr, size) → Transactor: request
  • Transactor → AHB fabric: address, data phase
  • AHB fabric → RTL peripherals: APB, AHB
  • RTL peripherals → Pads: pins

tools/soc/fast-platforms: hybrid.cpp (the model side, C++ around Verilator), transactor.sv (one outstanding request), hybrid_top.sv. The kit manual, "The hybrid boundary".

Before You Start

Read Virtual Platforms first. A hybrid joins C and SystemVerilog. DPI is taught in Your First DPI-C Call and Calling Back Into the Testbench, not here.

Five questions this page answers
Question
1What is RTL in a hybrid, and what is a model?
2How does a device access cross from the model into RTL?
3Where does a hybrid spend its simulated time?
4What does the instruction quantum trade?
5Which timing does a hybrid give up?

The Idea: Cut the Chip at the Core's Bus

The core is the busiest part of a simulation: it does something on every clock. Its behaviour, instruction by instruction, is also the easiest to model exactly. So a hybrid replaces only the core with an instruction-level model.

Everything else stays RTL: the fabric, the peripherals, the interrupt controller, the pads. A device access is the only place where the model and the RTL meet. A transactor sits there and turns a call into bus cycles.

Local work, such as fetching code and touching SRAM, stays inside the model and costs one step. A device access costs every bus cycle it takes. So a hybrid is fast on code that computes and slower on code that talks to devices.

New Words

New words on this page
WordMeaning
TransactorSynthesizable logic that takes one request and drives the bus protocol for it, then returns the reply.
Local accessA fetch or data access the model serves from its own memory, with no bus cycle.
Bus tickOne system clock edge spent inside a transactor request.
StepOne instruction opportunity of the model; at IPC 1 it advances the RTL by one edge.
Instruction quantum (IPC)How many local instructions share one RTL edge. IPC 1 is the default.
Timing modelWhat a platform says about when things happen. The hybrid's is coarse for the core and exact for devices.

The Smallest Example: The Cost of One Request

The transactor holds one request at a time. An edge takes the request; the address phase waits for HREADY; the data phase waits for HREADY again; the reply is registered. Count edges for a target with no wait states, then with two.

Edges for one request (the slice below, transactor.sv unchanged)
TargetEdges for a read
one wait state (the slice's target registers its answer)4
three wait states6

Each wait state adds one edge. A target with none at all would need three: capture, address and data, the minimum the kit manual gives. On ap_soc's own fabric the hybrid's SPI loop averaged 4 bus ticks per request, as the lab below shows.

The Rules

  1. Rule 1: Serve what you can locally.

    Fetches and SRAM stay in the model. Only devices, and faults, cross into RTL.

  2. Rule 2: One request at a time, and wait for the reply.

    The transactor holds one request and returns its data and HRESP. An ERROR becomes an access fault in the model.

  3. Rule 3: Count where the time goes.

    The hybrid reports steps, requests and bus ticks. Device-heavy code spends most of its ticks inside requests.

  4. Rule 4: A quantum trades time for speed.

    At IPC N, N local instructions share one edge. Timers advance less per instruction, and an interrupt can wait up to N - 1 instructions.

  5. Rule 5: Expect the core's timing to be gone.

    Pipeline stalls, fetch timing and the core's clock gating are not modelled. Device timing is RTL and stays exact.

Sources for These Rules

The boundary, the transactor, the counters and the quantum: tools/soc/fast-platforms/README.md, "The hybrid boundary"; facts 27.14.

A virtual platform joined to hardware through bus transactors: MEHTA-DV 12.4 (there with an emulator; here with RTL in a simulator).

The DMA case: with +rtl-sram every SRAM access crosses into the one RTL SRAM, so another manager sees it (the kit manual).

The Lab: The Hybrid's Counters

The ladder run of V13.1 kept the hybrid's own counters for its two steady loops:

LoopStepsRequestsBus ticksAll ticks
SRAM checksum1,250,114281,250,143
SPI polling536,118184,003736,0121,272,151

Rules 1 and 3 at work. The checksum made 2 requests in 1,250,114 steps: only the mailbox crossed. The SPI loop made a request every few steps, at 4 bus ticks each. It spent 57 per cent of its ticks inside them. That is why its rate fell.

Next, one image that reads the machine timer, runs 64 nops, and reads it again:

The lab image
#include "hal.h"
#include "tb.h"

int main(void)
{
    unsigned start = reg_read32(CLINT_MTIME_LOW);
    __asm__ volatile (".rept 64\nnop\n.endr");
    unsigned ticks = reg_read32(CLINT_MTIME_LOW) - start;
    tb_puts("mtime ticks across 64 nops: "); tb_put_hex(ticks); tb_putc('\n');
    return 0;
}
Platformmtime ticks across 64 nops
Chip RTL65
Virtual platform65
Hybrid, IPC 168
Hybrid, IPC 168

Rule 4 at work: at IPC 16 the same 64 nops took 8 ticks, against 68 at IPC 1. Sixteen local instructions share each edge. Rule 5: none of these is the chip's pipeline; RTL gave 65.

The quantum sweep (checksum loop; timer interrupt witness)
IPCInstructions a secondInstructions from IRQ to trap
1468,5150
41,965,2683
166,765,14015
6419,454,26863

Speed rose with the quantum, and so did the interrupt latency: exactly N - 1 instructions at each setting.

Why It Is Built This Way

Why cut at the core's bus? Because that is where the chip's own specification draws a line: the AHB protocol. The transactor speaks that protocol, so the RTL behind it sees ordinary AHB transfers, as it would from the core.

Why one request at a time? The model executes one instruction at a time and needs each load's data before it can go on. A deeper queue would need the model to guess.

Why offer a quantum at all? Local code is limited by how often the model must stop and step the RTL clock. Sharing one edge among N instructions removes most of those stops, at a cost in timing that rule 4 states.

Traps

The first trap is firmware that measures time in instructions. A delay loop of 1,000 instructions is 1,000 edges at IPC 1 and about 63 at IPC 16. Firmware that needs a real delay should read a timer.

The second is DMA. A DMA engine in RTL writes the RTL SRAM; the model has its own copy. Unless the hybrid serves SRAM from RTL too, the model never sees what the DMA wrote. Our hybrid refuses a DMA start unless +rtl-sram was chosen before boot.

The third is believing the hybrid's cycle counts. They count edges of a model that has no pipeline. Use RTL for any check of cycles.

At Scale

ap_soc~12 blocks
Phone-class SoChundreds of blocks

This section looks ahead. Our hybrid joins one model core to one simulator.

  1. Model core, emulated chip

    Large teams join a virtual platform to RTL in an emulator through bus transactors, so both sides run fast.

  2. Many transactors

    Each bus or interface the model touches gets its own transactor: AXI, PCI Express, USB.

  3. Bandwidth and latency

    The link between the two sides becomes the bottleneck; the traffic across it is minimised.

Try It

Run it: three calls through the hybrid's transactor: a word write, a byte read and a read that answers ERROR.

The byte at 0x5 comes back right-justified. Which byte of the word written to 0x4 is it?

Change the target's wait states. Which counts change, and which does not?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: write 00000004 size 2: 4 edges, OKAY read 00000005 size 0: 4 edges, OKAY, data 00000056 read dead0000 size 2: 5 edges, ERROR

Check Yourself

Predict the outputThe target now adds two more wait states to each data phase. What is the last line printed?
// One function call becomes bus cycles: the transactor of the hybrid, unchanged, and a small AHB target.
// One outstanding request. Pulse start while idle. done, error and result are
// registered on the completing data edge. Data at the software boundary is right-justified.
module transactor (
  input logic clk, resetn, start, write_req,
  input logic [31:0] addr, value,
  input logic [2:0] size,
  output logic done, error,
  output logic [31:0] result,
  output logic [31:0] haddr, hwdata,
  output logic [1:0] htrans,
  output logic hwrite,
  output logic [2:0] hsize,
  input logic [31:0] hrdata,
  input logic hready, hresp
);
  typedef enum logic [1:0] {IDLE, ADDRESS, DATA} phase_t;
  phase_t phase;
  logic [31:0] shifted;
  assign htrans = phase == ADDRESS ? 2'b10 : 2'b00;
  assign shifted = hrdata >> {haddr[1:0], 3'b000};
  always_ff @(posedge clk or negedge resetn) begin
    if (!resetn) begin
      phase <= IDLE;
      done <= 0;
      error <= 0;
      result <= 0;
      haddr <= 0;
      hwrite <= 0;
      hsize <= 0;
      hwdata <= 0;
    end else begin
      done <= 0;
      case (phase)
        IDLE: if (start) begin
          haddr <= addr;
          hwdata <= value << {addr[1:0], 3'b000};
          hwrite <= write_req;
          hsize <= size;
          phase <= ADDRESS;
        end
        ADDRESS: if (hready) phase <= DATA;
        DATA: if (hready) begin
          result <= hsize == 0 ? {24'b0,shifted[7:0]} :
                    hsize == 1 ? {16'b0,shifted[15:0]} : shifted;
          error <= hresp;
          done <= 1;
          phase <= IDLE;
        end
        default: phase <= IDLE;
      endcase
    end
  end
endmodule

// A target that registers its answer, so every data phase has one wait state, plus 2 more.
// Address 0xdead0000 answers ERROR in two cycles.
module target #(parameter int WAITS = 2) (input logic clk, resetn,
  input logic [31:0] haddr, hwdata, input logic [1:0] htrans, input logic hwrite, input logic [2:0] hsize,
  output logic [31:0] hrdata, output logic hready, hresp);
  logic [31:0] mem [0:3];
  logic [31:0] a; logic w, busy, bad; int left;
  always_ff @(posedge clk or negedge resetn)
    if (!resetn) begin busy <= 0; hready <= 1; hresp <= 0; left <= 0; end
    else if (busy) begin
      if (bad && !hresp) hresp <= 1;                                         // first ERROR cycle: HREADY still low
      else if (bad) begin hready <= 1; busy <= 0; end                       // second ERROR cycle: HREADY high
      else if (left > 0) left <= left - 1;
      else begin
        if (w) mem[a[3:2]] <= hwdata; else hrdata <= mem[a[3:2]];
        hready <= 1; busy <= 0;
      end
    end else if (htrans == 2'b10) begin
      a <= haddr; w <= hwrite; bad <= haddr == 32'hdead0000; busy <= 1; left <= WAITS;
      hready <= 0; hresp <= 0;
    end else hresp <= 0;
endmodule

module tb;
  logic clk = 0, resetn = 0, start = 0, write_req = 0, done, error;
  logic [31:0] addr, value, result, haddr, hwdata, hrdata;
  logic [2:0] size, hsize; logic [1:0] htrans; logic hwrite, hready, hresp;
  int edges;
  transactor x(.*);
  target t(.clk, .resetn, .haddr, .hwdata, .htrans, .hwrite, .hsize, .hrdata, .hready, .hresp);
  always #5 clk = ~clk;
  // One call from the model's side: everything below this line is time the model does not see.
  task automatic call(input bit w, input logic [31:0] a, input logic [2:0] s, input logic [31:0] v);
    @(negedge clk); write_req = w; addr = a; size = s; value = v; start = 1;
    @(posedge clk); edges = 1;               // the edge that takes the request
    @(negedge clk); start = 0;
    do begin @(posedge clk); #1 edges++; end while (!done);   // #1: read done after the edge has updated it
    if (error) $display("%s %h size %0d: %0d edges, ERROR", w ? "write" : "read ", a, s, edges);
    else if (w) $display("write %h size %0d: %0d edges, OKAY", a, s, edges);
    else $display("read  %h size %0d: %0d edges, OKAY, data %h", a, s, edges, result);
  endtask
  initial begin
    repeat (2) @(posedge clk); resetn = 1;
    call(1, 32'h0000_0004, 2, 32'h1234_5678);
    call(0, 32'h0000_0005, 0, 0);
    call(0, 32'hdead_0000, 2, 0);
    $finish;
  end
endmodule
ConceptIn the hybrid, what is RTL?
ConceptHow does a device access cross into RTL?
ConceptWhere did the SPI loop spend most of its ticks?
ConceptWhat does IPC 16 do to interrupts?

Common Mistakes

Wrong Expect the hybrid to be fast on every program.

Each device access runs its bus cycles in RTL; device-heavy code is slow.

Fix Read the counters: steps, requests, bus ticks.

Wrong Time a delay by counting instructions.

The quantum changes how many edges an instruction is worth.

Fix Wait on a timer or a device status, never on an instruction count.

Wrong Check cycle-exact behaviour of the core on the hybrid.

The core is a model with no pipeline.

Fix Hand that check to RTL.

Remember

  • A hybrid replaces only the core with a model and keeps every device as RTL.
  • Local work costs one step; a device access costs its bus cycles through a transactor.
  • The instruction quantum buys speed with coarser time: timers and interrupt latency change.
  • Device timing stays exact; the core's pipeline timing is gone.

Practice

Report a problem with this page