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.
- Model
- Memory
- Control / FSM
- Driver
- Bus
- Peripheral
- Pads
- Data
- Transaction / TLM
- Local work is free
Fetches and SRAM accesses stay in the model: one step, no bus.
- A device access is a call
The model calls a C function with an address and a size.
- The transactor
It holds the request as AHB NONSEQ, waits for HREADY, and returns the data and HRESP.
- 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.
| Question | |
|---|---|
| 1 | What is RTL in a hybrid, and what is a model? |
| 2 | How does a device access cross from the model into RTL? |
| 3 | Where does a hybrid spend its simulated time? |
| 4 | What does the instruction quantum trade? |
| 5 | Which 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
| Word | Meaning |
|---|---|
| Transactor | Synthesizable logic that takes one request and drives the bus protocol for it, then returns the reply. |
| Local access | A fetch or data access the model serves from its own memory, with no bus cycle. |
| Bus tick | One system clock edge spent inside a transactor request. |
| Step | One 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 model | What 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.
| Target | Edges for a read |
|---|---|
| one wait state (the slice's target registers its answer) | 4 |
| three wait states | 6 |
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
Rule 1: Serve what you can locally.
Fetches and SRAM stay in the model. Only devices, and faults, cross into RTL.
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.
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.
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.
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:
| Loop | Steps | Requests | Bus ticks | All ticks |
|---|---|---|---|---|
| SRAM checksum | 1,250,114 | 2 | 8 | 1,250,143 |
| SPI polling | 536,118 | 184,003 | 736,012 | 1,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:
#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;
}
| Platform | mtime ticks across 64 nops |
|---|---|
| Chip RTL | 65 |
| Virtual platform | 65 |
| Hybrid, IPC 1 | 68 |
| Hybrid, IPC 16 | 8 |
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.
| IPC | Instructions a second | Instructions from IRQ to trap |
|---|---|---|
| 1 | 468,515 | 0 |
| 4 | 1,965,268 | 3 |
| 16 | 6,765,140 | 15 |
| 64 | 19,454,268 | 63 |
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
This section looks ahead. Our hybrid joins one model core to one simulator.
Model core, emulated chip
Large teams join a virtual platform to RTL in an emulator through bus transactors, so both sides run fast.
Many transactors
Each bus or interface the model touches gets its own transactor: AXI, PCI Express, USB.
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?
write 00000004 size 2: 4 edges, OKAY read 00000005 size 0: 4 edges, OKAY, data 00000056 read dead0000 size 2: 5 edges, ERRORCheck Yourself
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.