StreamDemux

from waveflow.streams import StreamDemux, StreamDemuxStep

dmx = StreamDemux(name="dmx", sim=sim, n_out=2, bitwidth=64, frame_bursts=1)                  # by s_sel
hdr = StreamDemux(name="hdr", sim=sim, n_out=4, bitwidth=64, route="header", sel_lsb=56, sel_bits=2)

One framed input, s_in; n_out framed outputs, m_out_0 .. m_out_<n_out-1>. Each firing takes one frame – frame_bursts TLAST bursts – and writes it whole to one output. Which one is chosen one of two ways:

route the index ports
"sel" (default) one word on s_sel per frame, read before the frame s_in, s_sel, m_out_*
"header" bits sel_lsb .. sel_lsb + sel_bits - 1 of the frame’s first word, which is forwarded with the rest s_in, m_out_*

n_out is 2 to 16, frame_bursts 1 to 255, sel_bits 1 to 16. An index of n_out or more names no output: the frame is read and dropped, and dropped counts it in pysim (the HLS body drops it too). The block reports nothing, so a design that cannot lose a frame keeps its indices in range.

When to use it

As the return half of a shared kernel: a StreamArbiter merges the clients’ frames into K and writes each frame’s input on m_sel; the demux reads that as s_sel after K and sends each of K’s output frames back. That works for any K that keeps frames in order, one frame out per frame in.

Route by a header field when the frame already says where it goes, and nothing beside it does: a response whose header carries its client’s id, say.

The rule

The outputs do not move in lockstep, but a full one stalls them all. The block writes a frame to one output; while that output’s FIFO is full it waits, and the frames behind it – for every other output – wait too. Each client reads its output as it comes; a client that waits on something else first can stall its siblings.

In pysim

run_iter is one frame, as the body: its index (an s_sel word, or the frame’s first word’s field), then its bursts to that output, copied as their words arrive. It commits on the frame’s first word (begin_read(pending_ok=True), as StreamArbiter does) and copies with copy_burst: the output starts then and ends no earlier than the input’s last word – not once the whole frame is in, as it did before (plans/osfb_pysim_fidelity.md, cause H). Output order per output is the input’s, so pysim and RTL record the same words on every output.

Its decision cycles, on a calibrated platform. By default pysim decides at once. Name a calibrated platform and each frame first spends the cycles the RTL does deciding (waveflow.streams.timing, cause F):

dmx = StreamDemux(name="dmx", sim=sim, n_out=3, bitwidth=184, frame_bursts=2, route="header",
                  clk=Clock(freq=250e6),
                  platform_dir=packaged_platforms_dir() / "xczu48dr_250mhz_vitis2025_1")

The table is keyed by the outputs, the route, the width and the frame (n, hdr, w, fb). Measured with the demux alone at 4 ns on xczu48dr (Vitis HLS / xsim 2025.1, a backlog of 8-word bursts to each output in turn; python -m waveflow.calib.fixtures.stream_blocks --blocks demux): 3 idle cycles between frames at every point – 2, 4, 8 and 16 outputs by s_sel and by a header field at 32 bits with 1-burst frames, and examples/os_filterbank’s three-output demuxes by a header field at 32 bits with 1-burst frames and at 184 bits with 2-burst frames. pysim reproduces each (tests/streams/test_stream_timing.py; at RTL, -m xsi). A point not measured (5 outputs, or 64 bits) is refused when the block is built, and another clock when the simulation starts.

In HLS

The bodies, both in stream_demux_task.h, are copied into a build by StreamDemuxStep:

template <int W, int NB, typename TI, typename TS, typename... TO>
static void stream_demux_task(hls::stream<TI>& s_in, hls::stream<TS>& s_sel, hls::stream<TO>&... m_out);

template <int W, int NB, int LSB, int BITS, typename TI, typename... TO>
static void stream_demux_hdr_task(hls::stream<TI>& s_in, hls::stream<TO>&... m_out);

The outputs are the pack, last in the signature; the generator supplies every port’s word type, as for StreamBroadcast. One firing reads the index and the frame’s first word, then copies the rest, a word a cycle (II = 1).

Its gates

waveflow.streams.rtl builds the demux in its own harness (a driver on each input, a sink on each output) both ways, and the arbiter-demux pair; each output’s recording at RTL equals pysim’s, and none is empty.

python -m waveflow.streams.rtl C:/wfb/dmx --block demux --n 3 --frame-bursts 2
python -m waveflow.streams.rtl C:/wfb/dmxh --block demux_hdr --n 3

Measured (Vitis 2025.1, xc7z020 at 100 MHz, 3 outputs, 32 bits): by s_sel with 2-burst frames, 5.73 ns estimated, 253 FF and 492 LUT; by a header field, 3.18 ns, 261 FF and 422 LUT.

On the RFSoC at 250 MHz (xczu48dr-ffvg1517-2-e, 4 ns; Vitis HLS / Vivado 2025.1, out of context): by s_sel, 1.822 ns estimated, 2.474 ns routed, WNS +1.526 ns, 399 LUT / 669 FF; by a header field, 0.854 ns estimated, 2.792 ns routed, WNS +1.208 ns, 344 LUT / 588 FF. The header demux is csynth’s fastest and routes slower than the other: a csynth estimate well under budget does not rank the routed result (plans/shared_kernel_rfsoc_timing.md).

Its resources

Records for 2 and 4 outputs – 32, 64 and 128 bits, 1- and 2-burst frames, both routes – are filed in the packaged platform xczu48dr_250mhz_vitis2025_1 (csynth at 4 ns, Vitis HLS 2025.1; rebuilt by python -m waveflow.streams.calib demux). No DSP, no BRAM:

outputs, route LUT at 32 / 64 / 128 bits, 1 burst FF at 32 / 64 / 128 bits, 1 burst
2, by s_sel 447 / 479 / 543 213 / 397 / 765
2, by header 397 / 397 / 397 219 / 403 / 771
4, by s_sel 520 / 552 / 616 293 / 557 / 1,085
4, by header 470 / 470 / 470 303 / 567 / 1,095

A second burst a frame adds about 20 LUT. Routing by header costs the fewest LUT, and they do not grow with the width. At 16 outputs and 128 bits, by s_sel, it routes at 2,835 LUT and 7,436 FF (a standalone top, its port slices included) and meets 4 ns with WNS +0.874 ns (Vivado 2025.1).

See also