Two kernels on a bus
In the memory-mapped FIR the only bus master is the host. Here a kernel is a bus master too: a generator kernel writes another kernel’s input queue across the same shared bus the host uses. That changes one thing that matters. A stream’s usual back-pressure – the consumer simply not taking the next word – does not work across a bus: a write into a full queue stalls the bus, and can deadlock it. The producer has to know there is room before it writes. That is credit-based flow control, and this example is the worked design for it.
The computation is a toy on purpose: a two-state Markov chain driven by a pseudo-random generator. Each kernel is a few lines; what the example is about is everything between them.
Learning objectives
What this adds to A memory-mapped FIR and the bus example before it, in the order the pages teach it:
- Model a two-state Markov chain – its transition probabilities, its stationary distribution – and simulate it with a pseudo-random generator, in integer arithmetic that Python and the RTL agree on bit for bit.
- Make a kernel a bus master that writes another kernel’s input queue, through an
MM-stream with a reverse credit stream
(
MmCreditStreamIF), its queue and credit mapped to views on the receiving kernel’s slave adaptor. - Batch the credit (one bus write per 32 words) without losing liveness (
max_write). - Size a credit link: a FIFO in front of a store-and-forward writer, and a credit window that covers the link’s bandwidth-delay product.
- Make a response mean “done”, not “seen”: the chain’s response is forwarded by its memory writer only after the results are stored.
- Write both kernel bodies as loops, one step per cycle – a recurrence included – and check credit between chunks, not inside the loop.
- Run the same kernels in two wirings of one system: joined directly, and over the bus.
- Put four bus masters on one crossbar at RTL, and account for its cycles with timing probes.
- Generate a docs figure as a build step, from the golden model.
Pages
- Theory — what a Markov chain is and where it is used, the two-state chain’s math, how one step is simulated, and the two modules.
- Protocol — every message and the order it flows in: command, forwarded command and draws, credit, states to memory, the response.
- Python model — the schemas, the golden model, the generator and the chain (a composite with the framework’s memory writer).
- The host —
MarkovHost, step by step: what a host owns, what the system hands it, its two processes, and a checklist for writing your own. - The system —
markov_system, step by step: the modules, the memory regions, the direct wiring, and the bus wiring (devices, the routed credit link, the crossbar, interrupts, the host’s endpoints). - The credit link — how the generator’s stream reaches the chain across the bus: the views, the routing, the numbers that size it, and what the RTL taught.
- Python simulation — both wirings, the results, what the gates check, the chain’s output, and how close the timing is.
- Build flow — the build DAG (
markov_build.py): which steps are the example’s (codegen and the scenario), which are the framework’s, the CLI, and what a second run skips. - Code generation — what each pysim object generates (Fig. 1), the four tops, the two hand-written bodies as loops, and the timing they close at.
- Synthesis — csynth of the four tops, the crossbar IP, and the Verilog top walked from the graph (Fig. 2).
- XSI testbench — the RTL under a testbench (Fig. 3): the host’s C++ twin, the generated harness, the run against pysim, reading the traces back, and the gates.
- RTL simulation — the four-master system under XSI, the gates, and finding the time with probes.
The code is in examples/markov.
Table of contents
- Theory - What a Markov chain is and where it is used (traffic and queueing models, bursty channel errors, Monte Carlo); the two-state chain's transition matrix and its stationary distribution; how one step is simulated from a uniform draw with two integer compares; why each step depends on the last; and the two modules the example splits it into -- a pseudo-random generator and the chain.
- Protocol - Every message in the system and the order it goes in: the host's command to the generator, the command forwarded ahead of the draws to the chain, credit returned to the generator, the states written to memory, and the response -- forwarded only once the states are stored -- back to the host, which waits on interrupts and keeps at most two jobs in flight.
- Python model - The Markov example in Python: the command and response schemas, the golden model (xorshift32 and the chain), the generator kernel, and the chain -- a composite of the chain core and the framework's in-band memory writer, so the response goes out only once the states are stored. Each kernel's run_iter is one job, the twin of its HLS body. The host and the system have pages of their own.
- The host - MarkovHost, read as a recipe for writing a host program: what a host is (a module outside the synthesized cut that talks to the system only through endpoints, never addresses), the endpoints it owns and the ones the system hands it, its jobs, its two processes -- a writer that admits at most two jobs and a reader that takes each response and reads the job's results back from memory -- how both sleep on interrupts instead of polling, and how the run ends. A checklist for writing your own, and which parts only matter once the host has a C++ twin.
- The system - markov_system, step by step, as the general pattern for assembling a system: one Simulation; the kernels, the memory and the host; the memory regions the host hands out; then a wiring. The direct wiring joins the host to the kernels with plain streams and the kernels with a CreditStreamIF. The bus wiring builds each kernel's memory-mapped device from its declared views, routes the credit stream over the crossbar, binds every master and slave, assigns the address map, places the credit link, runs each queue's interrupt to the host, and hands the host its endpoints by view name. The ordering constraints between those steps; then HwSystem(sim, dut) names the DUT, and how to run it.
- The credit link - How the generator's stream reaches the chain across the shared bus: a credit stream routed over the crossbar (MmCreditStreamIF). The views each kernel declares, the endpoints, the routing code, the numbers that size it (a 128-word queue, credit every 32 words, a 32-word FIFO in front of the store-and-forward writer), what the gates check, and the two sizing lessons the RTL taught.
- Python simulation - The whole system simulated in Python, two ways -- the kernels joined directly, and routed over one crossbar with four bus masters -- with the same kernel code. Bit-exact against the golden in both; what the gates check (no bus stall, batched credit, no host polling, at most two jobs in flight); the chain's output and its stationary probability; and how close pysim's timing is to the RTL.
- Build flow - The example's whole build -- from its generated sources to the RTL trace gate -- as one BuildDag in markov_build.py. Only codegen and the scenario (system(): four jobs of 300 steps) are the example's; add_system_steps adds the framework's csynth (one step per HLS top, skipped while its source stamp matches), system_rtl (the crossbar IP and the Verilog top), scenario, pysim, system_xsi and compare. The CLI (--through, --status, --synth check, --probes), what a second run skips and why, and where a run's results land. The next three pages take the steps in order: Code generation, Synthesis, XSI testbench.
- Code generation - The codegen step: which pysim objects become C++ and what each generates (Fig. 1). What becomes Vitis HLS and how: four free-running tops -- the generator, the chain (its core plus the framework's in-band memory writer), and the credit link's two bus writers -- with every header and top generated and only the two kernel bodies hand-written. Both bodies are straight-line loops per job, one step per cycle at II=1, with credit handled between chunks; the timing they close at, and the two timing fixes their first versions needed.
- Synthesis - The csynth and system_rtl steps: how the system becomes Verilog (Fig. 2). csynth runs Vitis on the four HLS tops codegen wrote -- each re-run only when its source stamp no longer matches, built or only checked (synth=build|check). system_rtl generates the crossbar IP with create_ip (cached by its configuration) and walks the pysim graph, cut at the two kernels and the memory, to markov_top.v: the csynth'd modules, the framework's adaptor and memory Verilog, the crossbar, the stream nets and FIFO, the writers' targets, and the host's bus and interrupts as top ports. rtl.json lists every file the top compiles.
- XSI testbench - The last four steps of the build flow -- scenario, pysim, system_xsi and compare -- put the RTL from Synthesis under a testbench and check it against pysim (Fig. 3). The host's C++ twin in markov_host.h -- two threads on generated endpoints, the same program as MarkovHost -- runs from the same scenario file as the pysim host; the generated harness binds it to the top's ports; XSI compiles and runs it; and every host endpoint's trace must be byte-identical between the two runs. Reading the results back from report.json and the traces; the gates, which run the DAG with synth=check; and, only after it works, the timing probes. A checklist for doing the same for another system.
- RTL simulation - The whole system as RTL under XSI: AMD's crossbar with four masters and three slaves, the two kernels' adaptors, a BRAM as the shared memory, the two csynth'd kernels and the credit link's two writers under one generated top, driven by the pysim host on C++ endpoints. The gates (bit-exact, no polls, 1870 cycles, pysim within 5%), the timing probes, and how they took the system from 2356 cycles to 1865 and pysim from 28% off to 3%.