The system
markov_system builds the object that holds everything: the
two kernels, the shared memory, the host, and the connections between them. It builds the same modules
two ways — joined directly, or across one bus — and the kernels and the host are the same code
in both; only the wiring differs. This page goes through it step by step, because the steps are the
general pattern for any system.
sysm = markov_system(default_jobs(4, 300), link="mm") # or link="direct"
results = sysm.run() # {job: {"x", "ones", "n", "t"}}
flowchart LR
host["host"] -- "commands (qcmd)" --> gen["generator"]
gen -- "draws u, credit back" --> chain["chain"]
chain -- "states x" --> mem[("memory")]
chain -- "responses (qresp)" --> host
host -- "reads x" --> mem
That picture is the same in both wirings. What changes is what each arrow is.
Step 1: one simulation, and its modules
def markov_system(jobs, link="mm", clk=None, gen_ii=1, xbar_latency=4, xbar_travel=2) -> HwSystem:
clk = clk if clk is not None else Clock(freq=100e6)
sim = Simulation()
gen = MarkovGen(name="gen", sim=sim, clk=clk, gen_ii=gen_ii)
chain = MarkovChain(name="chain", sim=sim, clk=clk)
mem = markov_memory(sim, clk)
host = MarkovHost(name="host", sim=sim, jobs=list(jobs), clk=clk)
host.mem = mem
Every module is constructed with the same Simulation — constructing it registers it, so there is
no separate “add” step — and the same clock. The kernels are built bare: their ports exist, bound to
nothing yet. markov_memory makes the shared memory, a MemoryMod: 512 64-bit words (MEM_SPAN = 4 KB), byte-addressed,
as a bus sees it.
Step 2: the memory regions the host hands out
for j in range(len(jobs)):
a = mem.alloc(REGION_BYTES // 8)
assert a == host._dst(j), (a, host._dst(j))
Each job’s states go to their own region. The memory allocates them, and the assert checks the
host’s arithmetic (_dst(j) = j * REGION_BYTES) names the same place — the host puts that address in
the command it sends, so the two must agree. A system that hands out buffers does this step; one whose
kernels only stream does not.
Step 3: the direct wiring
The simplest form, and worth having: it checks the kernels and the host with no bus in the way.
def _stream(sim, clk, name, master, slave, depth) -> StreamIF:
si = StreamIF(name=name, sim=sim, clk=clk, bitwidth=DW, depth=depth)
si.bind(ep_name="master", endpoint=master)
si.bind(ep_name="slave", endpoint=slave)
return si
def _wire_direct(sim, clk, gen, chain, mem, host) -> tuple[DutTop, dict]:
host.qcmd = StreamIFMaster(name="host_qcmd", sim=sim, bitwidth=DW, has_tlast=True)
host.qresp = StreamIFSlave(name="host_qresp", sim=sim, bitwidth=DW, has_tlast=False)
_stream(sim, clk, "k_cmd", host.qcmd, gen.s_cmd, CDEPTH)
_stream(sim, clk, "k_resp", chain.m_resp, host.qresp, RDEPTH)
u_link = CreditStreamIF(name="u", sim=sim, clk=clk, bitwidth=DW, depth=QDEPTH)
u_link.bind("master", gen.m_u)
u_link.bind("slave", chain.s_u)
mem_link = DirectMMIF(name="k_mem", sim=sim, clk=clk)
mem_link.bind("master", chain.m_mem)
mem_link.bind("slave", mem.s_mm)
dut = DutTop.of("markov_dut", sim, comps=[gen, chain, mem], ifs=[u_link, mem_link])
return dut, {"u_link": u_link, "mem_link": mem_link}
The pattern for every connection is make the interface, bind its two sides:
| connection | interface | sides |
|---|---|---|
| host → generator, chain → host | StreamIF, at the queue’s depth |
"master", "slave" |
| generator → chain | CreditStreamIF — the stream and its credit, one object |
the producer’s m_u, the consumer’s s_u |
| chain → memory | DirectMMIF |
the chain’s m_mem, the memory’s s_mm |
The host gets plain streams for qcmd and qresp, which the system creates; host.mem_bus_base
stays None, so the host reads x from the memory object directly. The last lines name the DUT —
the kernels and the memory, with the links between them (Declaring the DUT).
The bus wiring
Now every arrow crosses one AXI crossbar. The host is a bus master, and so are three things inside the
system; the kernels’ queues are reached through memory-mapped views. Steps 4 to 8 are the body of
_wire_mm(sim, clk, gen, chain, mem, host, xbar_latency, xbar_travel), in the order that works —
several of them depend on the one before.
Step 4: each kernel’s memory-mapped device
gen_dev = build_mm_device(gen, sim=sim, clk=clk, mem_dwidth=DW, prefix="gen_")
chain_dev = build_mm_device(chain, sim=sim, clk=clk, mem_dwidth=DW, prefix="chain_")
A kernel type declares how a bus reaches it, on its class, as its mm_views:
class MarkovGen(FreeRunMod):
mm_views = (QueueIn("qcmd", port="s_cmd", depth=CDEPTH), # the host writes commands here
CreditIn("u_crd", port="m_u")) # the chain writes credit here
class MarkovChain(FreeRunMod):
mm_views = (QueueIn("qu", port="s_u", depth=QDEPTH), # the generator writes draws here
QueueOut("qresp", port="m_resp", depth=RDEPTH)) # the host reads responses here
build_mm_device builds those views, joins each to the kernel’s named port, and puts them behind one
slave adaptor — one bus port for the kernel. prefix names
the view modules (gen_qcmd, chain_qresp, …), so two kernels’ views never collide. The kernel’s own
code is untouched: it still reads and writes streams.
What it returns is a device: a composite module (MmSlaveDevice) whose children are the kernel,
its views and its adaptor. A device is the unit the DUT is made of. It is one sub-top in the system’s
Verilog, and one IP when the system is packaged.
Step 5: the credit stream, routed
u_link = MmCreditStreamIF(name="u", sim=sim, clk=clk, bitwidth=DW, fwd_depth=FWD_DEPTH)
u_link.bind("master", gen.m_u)
u_link.bind("slave", chain.s_u)
The same two kernel endpoints as in the direct wiring, but the link now carries the stream across the
bus: it adds a queue writer (the generator’s draws → the chain’s qu view) and a credit writer
(the chain’s credit → the generator’s u_crd view), each a bus master, and a FIFO in front of the
queue writer (fwd_depth). Build the devices first: binding checks that each kernel’s other half
is already joined to its view. Why the link must be credit-based, and how it is sized, are
The credit link.
Step 6: the crossbar
masters = [host.m, *u_link.bus_masters(), chain.m_mem]
gs, gr = gen_dev.ranges(GEN_BASE)
cs, cr = chain_dev.ranges(CHAIN_BASE)
slaves = gs + cs + [mem.s_mm]
ranges = gr + cr + [(MEM_BASE, MEM_SPAN)]
xbar = AXIMMCrossBarIF(name="xbar", sim=sim, clk=clk, nports_master=len(masters),
nports_slave=len(slaves), bitwidth=DW, latency_init=xbar_latency,
latency_travel=xbar_travel)
for k, m in enumerate(masters):
xbar.bind(f"master_{k}", m)
for k, s in enumerate(slaves):
xbar.bind(f"slave_{k}", s)
assign_address_ranges(slaves, ranges)
Collect every bus master — the host’s, the credit link’s two writers, the chain’s memory writer —
and every bus slave — each device’s port, at the base you choose for it (dev.ranges(base) gives
the port and its span), and the memory. Bind them in order, then assign_address_ranges writes the
address map. This is the one place an address is chosen; everything else reads it back.
The order of the two lists is the crossbar’s port order. In pysim it only names the ports; at RTL it is
the order of the crossbar’s slots (the host is SI 0, the top’s AXI port), so keep it stable.
latency_init / latency_travel are the crossbar’s measured latency (4 cycles per request, 2 of them
in travel), part of pysim’s timing model.
Step 7: placing the credit link
gmap, cmap = GEN_LAYOUT.at(GEN_BASE), CHAIN_LAYOUT.at(CHAIN_BASE)
u_link.place(qin=cmap["qu"], crd_in=gmap["u_crd"])
The two writers must know their targets — the bus address of the chain’s qu view and of the
generator’s u_crd view. A layout (GEN_LAYOUT = MemSlaveLayout.of(MarkovGen), the view offsets of
the type) placed at an instance’s base (.at(GEN_BASE)) gives every view’s absolute address. Place
after the address map exists — that is why this is not part of Step 5.
Step 8: interrupts, and the host endpoints
for dev, name in ((gen_dev, "qcmd"), (chain_dev, "qresp")):
v = dev.views[name]
line = IrqIF(name=f"{v.name}_irq", sim=sim)
line.bind("source", v.m_irq)
line.bind("sink", host.irq[name])
poll = host.poll_cycles
host.qcmd = BoundMemSlaveAdaptor(gmap, host.m, poll_cycles=poll).stream_master(
"qcmd", irq=host.irq["qcmd"])
host.qresp = BoundMemSlaveAdaptor(cmap, host.m, poll_cycles=poll).stream_slave(
"qresp", irq=host.irq["qresp"])
host.mem_bus_base = MEM_BASE
Two things the host needs, both by view name:
- An interrupt line per queue it waits on. Each queue view drives one (
m_irq); anIrqIFjoins it to the host’sIrqIFSink(which the host owns — The host). - Its endpoints.
BoundMemSlaveAdaptor(map, host.m)is a kernel’s views as its host sees them: ask for a view by name and get the same kind of endpoint the direct wiring handed over —stream_master("qcmd")aStreamIFMaster,stream_slave("qresp")aStreamIFSlave— whose calls turn into bus reads and writes. Givenirq=, they sleep on the interrupt instead of polling. The host’s code does not change.
Last, mem_bus_base tells the host the memory is on the bus, at MEM_BASE: it now reads x with bus
reads.
Declaring the DUT
dut = DutTop.of("markov_dut", sim, comps=[gen_dev, chain_dev, mem], ifs=[xbar, u_link])
and the system is the simulation with that DUT named:
sysm = HwSystem(sim, dut, run_until=host.done, check=lambda s: host.results,
fresh=lambda: markov_system(jobs, link, clk, gen_ii, xbar_latency, xbar_travel))
sysm.gen, sysm.chain, sysm.mem, sysm.host = gen, chain, mem, host # and u_link, xbar, the devices
return sysm
HwSystem(sim, dut) is what every system flow takes: the simulation,
and the DUT, the one root that is synthesized. Here that is a tree: the two devices and the memory
are its children, and the crossbar and the routed credit stream are the links between them. Binding
the credit stream in Step 5 already put each of its bus writers in the device of the kernel it acts
for: the forward writer in gen_dev, the credit writer in chain_dev. Everything not in the tree,
which here is only the host, is the testbench — derived, never listed. The constructor checks the
rules: the DUT is a root of this simulation, every testbench participant can be lowered to the RTL
testbench (the host has a C++ twin), and no DUT boundary port is left unbound. The rest describes the
run: run_until ends it (the kernels never stop on their own), check is what run() returns, and
fresh rebuilds an unrun copy for a second pass (a calibration’s). The parts are kept as attributes
for the tests and the timing probes that name them. A build can draw another cut (inside=);
the DUT is the one it takes otherwise.
Step 9: run it
results = sysm.run() # sim.run_sim(until=host.done), then check(sysm)
run() writes the scenario the host plays (the same file the RTL testbench reads), then run_sim
calls every module’s pre_sim, starts every run_proc, and runs until the host says it is done. demo(link) wraps it and compares every job with the
golden; running both wirings and their results are Python simulation.
The pattern, in one list
- One
Simulation; construct every module on it, with one clock. - Allocate the data regions the host will hand out, where the memory says.
- Wire directly first: a
StreamIFper stream, aCreditStreamIFbetween kernels, aDirectMMIFto the memory; give the host plain stream endpoints. - For the bus:
build_mm_deviceper kernel (views declared on the kernel class), then any routed credit link (MmCreditStreamIF), then the crossbar — every master, every slave at its base,assign_address_ranges— thenplacethe link, then the interrupt lines, then the host’s endpoints fromBoundMemSlaveAdaptorby view name. - Declare the DUT: a
DutTopof the devices and memories, with the links between them, andHwSystem(sim, dut, run_until=host.done, check=...). run(): until the host’sdoneevent.
The same object is the input to the RTL simulation: the system top is walked from this graph and the host’s endpoints become the testbench’s — XSI testbench.