RTL simulation

The system at RTL

The example’s build DAG, examples/markov/markov_build.py, synthesizes the four tops, builds one Verilog top around them and runs it under XSI:

flowchart LR
  host["C++ host<br/>(testbench)"] --> xbar
  qw["queue writer"] --> xbar
  cw["credit writer"] --> xbar
  mw["chain's memory writer"] --> xbar
  xbar(("AMD axi_crossbar<br/>4 SI x 3 MI"))
  xbar --> ga["generator's adaptor<br/>qcmd, u_crd"]
  xbar --> ca["chain's adaptor<br/>qu, qresp"]
  xbar --> mem[("BRAM view<br/>shared memory")]
  ga --> gen["markov_gen"]
  gen --> fifo["FIFO"] --> qw
  ca --> chain["markov_chain"]
  chain --> cw
  chain --> mw

The whole top is generated by walking the pysim system (waveflow/build/system_top.py); the example names only the cut – system_top_spec(sysm.xbar, [sysm.gen, sysm.chain, sysm.mem]) – and everything below follows from the graph:

  • The crossbar is generated from the pysim system’s own crossbar (AxiXbarConfig.from_crossbar): the same slaves at the same addresses, two ID bits so responses find their way back to four masters.
  • The adaptors are the hand-written view leaves joined by generated wiring – for the generator a queue in and a credit-in register, for the chain a queue in and a queue out.
  • The shared memory is a BRAM view behind its own front. (The testbench’s memory models do not echo AXI IDs, which a multi-master crossbar routes responses by; the front does.)
  • The kernels and writers are wired from the same TopSpec each was synthesized from, not from a table or a parse of their Verilog: an m_axi pin the crossbar has is joined to its slot, one it lacks is tied off. The writers come with the routed credit link; each one’s target – its peer view’s word address, from the address map – is a constant the top drives.
  • The forward FIFO sits between the generator and its queue writer, at the depth the pysim link declares (fwd_depth).

The host is MarkovHost’s C++ twin, MarkovHostModel in markov_host.h, run from the same scenario file as the pysim host and checked against it trace by trace. How the DAG builds all of this is three pages: Build flow (which steps are the example’s), Synthesis (the tops, the crossbar, the Verilog top) and XSI testbench (the host, the harness, the run).

Running it

python -m examples.markov.markov_build                      # codegen, csynth what is stale, pysim, RTL, compare
python -m examples.markov.markov_build --through pysim      # the pysim side alone (no Vivado)
python -m examples.markov.markov_build --status             # what is stale, and why
pytest tests/examples/test_markov_xsi.py -m xsi             # the gates (needs Vivado)

The csynth step re-synthesizes a top only when the sources its stamp recorded have changed, so after the first build a run costs the XSI simulation and seconds of generation. compare fails the run if the host’s RTL traces differ from pysim’s.

Results

  pysim RTL
4 jobs x 300 steps, 2 in flight 1924 cycles 1951 cycles
x bit-exact bit-exact
host reads other than responses and x 0 0

The gates (tests/examples/test_markov_xsi.py) check each job’s x and ones against the golden, that the host never reads a count, the cycle count, that pysim stays within 5% of it, and that the host’s three traces are identical between the two. They run the DAG with synth="check": a top whose RTL was not built from the sources on disk fails the gates, named, and is never synthesized by them.

Finding the time

The first RTL run took 2356 cycles; pysim said 1700. Instead of guessing, python -m examples.markov.markov_build --probes builds the top with one-bit probes on the handshake of every link – the generator’s words to its writer, each writer’s bursts and acknowledgements, the chain’s reads, credit offered and taken, responses – and the testbench prints the cycles each fired. Lined up with the same events from pysim, they showed where the time went:

step what the probes showed the change RTL cycles
– both bodies single-firing state machines – 2356
1 – both bodies rewritten as loops per job (Code generation) 2246
2 a chunk left the generator every 103 cycles for 64 draws: the queue writer gathers a whole chunk before bursting it and reads nothing while it bursts, and nothing buffered the generator a FIFO in front of the writer – fwd_depth, two chunks 2015
3 at every job start the generator stalled 70–110 cycles for credit, then the chain starved for ~60: the 63-word credit window was shorter than the link’s round trip the chain’s queue 64 -> 128 words 1865
4 the chain now the bottleneck at 79 cycles a chunk, the generator at 71: 64 steps plus a fixed cost per chunk pysim charges each kernel’s measured chunk_overhead (15 and 7) pysim 1700 -> 1926
5 – the credit handling moved into the framework’s credit::Producer / credit::Consumer – equivalent logic, scheduled slightly differently 1870
6 the memory writer reported each write ~25 cycles before the memory acknowledged it its completion waits for the write response (hls::fence): correctness, not speed 1951
7 traced, the writer’s response firing echoes 14 cycles after its last input though it writes nothing: the fence’s round trip, in every firing, on each job’s path to the host pysim charges it (MemWStream.fence_cycles) pysim 1854 -> 1924

Steps 2 and 3 were defects in the design – the RTL got faster – and step 4 a gap in the model. The design lessons are general, and are on the guide page MM-streams with credit: a store-and-forward stage needs a buffer in front of it the size of what it gathers, and a credit window must cover the link’s bandwidth-delay product. Applied here they are The credit link’s numbers.

The probes stay in the build: --probes (build_dag(probes=True), its own workspace xsi_work/markov_probes/) reproduces the gate’s cycle count exactly, so they can be switched on whenever the timing moves.