Source gaps and sink backpressure

Send a frame through the one-entry register in stream_dut.vhd. The source’s generator permits two cycles of new offers, then pauses for one. The sink’s list permits one cycle of reception out of every three. The test checks that the register accepted and forwarded every value in order.

from collections.abc import Iterator

import cocotb
from cocotb.clock import Clock
from cocotb.types import LogicArray
from cocotb.handle import HierarchyObject
from cocotb.triggers import FallingEdge

from coaxi import AxiStreamBus, AxiStreamSink, AxiStreamSource, to_unsigned


def input_gaps() -> Iterator[bool]:
	while True:
		yield False
		yield False
		yield True


@cocotb.test(timeout_time=1000, timeout_unit="ns")
async def backpressure(dut: HierarchyObject) -> None:
	dut.wipe_registers.value = 1
	dut.allow_input.value = 0
	clock = dut.beat_clock
	cocotb.start_soon(Clock(clock, 10, unit="ns").start(start_high=False))

	source = AxiStreamSource(AxiStreamBus.bind(dut, prefix="s", last=True), clock)
	sink = AxiStreamSink(AxiStreamBus.bind(dut, prefix="m", t_prefix=False, last=True), clock)
	await source.initialise()
	await sink.initialise()
	await FallingEdge(clock)
	dut.wipe_registers.value = 0
	dut.allow_input.value = 1

	source.set_pause_generator(input_gaps())
	sink.set_pause_generator([True, True, False])
	values = [10, 20, 30, 40]
	sending = cocotb.start_soon(source.send_frame(values))
	frame = await sink.recv_frame(max_transfers=len(values))
	await sending

	received = []
	for beat in frame:
		assert isinstance(beat.data, LogicArray)
		received.append(to_unsigned(beat.data))
	assert received == values
	assert int(dut.accepted.value) == int(dut.retired.value) == len(values)

	await source.close()
	await sink.close()
uv run --locked pytest -q tests/test_simulation.py -k backpressure

Both patterns advance while the endpoints are idle. A paused source still holds a beat it has already offered until the peer accepts it. The sink needs ready to stall the register. Keep the test timeout: a pattern that never permits traffic can leave an operation waiting indefinitely.