IQ samples as a dataclass

Send a frame of Python IQ samples through a VHDL loopback. Each sample has two signed 16-bit fields. The adapter lets the test compare IQ values directly. The default adapter can send the same record as a dictionary. The HDL wires the streams together without buffering, so send and receive must run concurrently.

VHDL: iq_loopback.vhd

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

package iq_types is
	type iq_t is record
		i : signed(15 downto 0);
		q : signed(15 downto 0);
	end record;
end package;

library ieee;
use ieee.std_logic_1164.all;
use work.iq_types.all;

entity iq_loopback is
	port (
		clk : in std_logic;
		s_tdata : in iq_t;
		s_tvalid, s_tlast : in std_logic;
		s_tready : out std_logic;
		m_tdata : out iq_t;
		m_tvalid, m_tlast : out std_logic;
		m_tready : in std_logic
	);
end entity;

architecture loopback of iq_loopback is
begin
	m_tdata <= s_tdata;
	m_tvalid <= s_tvalid;
	m_tlast <= s_tlast;
	s_tready <= m_tready;
end architecture;

Python: custom_payload.py

Bind both buses before constructing their adapters. Each IQPayload looks up i and q on its own data handle and takes the widths and indices from those signals. drive() converts both integers before writing either field. read() builds a new IQ and raises ValueError on unresolved bits. The source uses clear() to write zeros during initialisation and cleanup; the sink never writes the record.

AxiStreamSource[IQ] and AxiStreamSink[IQ] take the value type IQ. Pass an IQPayload instance as payload. If you annotate an endpoint attribute, include [IQ] there too: a bare AxiStreamSource or AxiStreamSink in an annotation means PayloadValue | None.

If the checker rejects the constructor, try adapter: Payload[IQ] = IQPayload(input_bus) to check the adapter on its own. Use drive(self, value: IQ) -> None and read(self) -> IQ: a different parameter name in drive(), such as sample, changes which keywords the method accepts.

from dataclasses import dataclass

import cocotb
from cocotb.clock import Clock
from cocotb.types import LogicArray
from cocotb.handle import HierarchyObject, LogicArrayObject, LogicObject

from coaxi import AxiStreamBus, AxiStreamSink, AxiStreamSource, Payload, to_signed


@dataclass(frozen=True)
class IQ:
	i: int
	q: int


class IQPayload(Payload[IQ]):
	def __init__(self, bus: AxiStreamBus) -> None:
		record = bus.data
		if not isinstance(record, HierarchyObject):
			raise TypeError("IQ data must be a record")
		i, q = record["i"], record["q"]
		if not isinstance(i, LogicArrayObject) or not isinstance(q, LogicArrayObject):
			raise TypeError("IQ fields must be logic vectors")
		self._i = i
		self._q = q

	def drive(self, value: IQ) -> None:
		# Convert both fields before writing either one.
		i = LogicArray.from_signed(value.i, self._i.range)
		q = LogicArray.from_signed(value.q, self._q.range)
		self._i.set(i)
		self._q.set(q)

	def read(self) -> IQ:
		return IQ(to_signed(self._i.value), to_signed(self._q.value))

	def clear(self) -> None:
		self.drive(IQ(0, 0))


@cocotb.test(timeout_time=1000, timeout_unit="ns")
async def custom_payload(dut: HierarchyObject) -> None:
	clock = dut.clk
	assert isinstance(clock, LogicObject)
	cocotb.start_soon(Clock(clock, 10, unit="ns").start(start_high=False))
	input_bus = AxiStreamBus.bind(dut, prefix="s", last=True)
	output_bus = AxiStreamBus.bind(dut, prefix="m", last=True)
	source = AxiStreamSource[IQ](input_bus, clock=clock, payload=IQPayload(input_bus))
	sink = AxiStreamSink[IQ](output_bus, clock=clock, payload=IQPayload(output_bus))
	await source.initialise()
	await sink.initialise()

	samples = [IQ(-32768, 34), IQ(56, -78), IQ(90, 32767)]
	sending = cocotb.start_soon(source.send_frame(samples))
	frame = await sink.recv_frame(max_transfers=len(samples))
	await sending
	assert [beat.data for beat in frame] == samples
	assert [beat.last for beat in frame] == [False, False, True]

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

In another cocotb project, compile iq_loopback.vhd as VHDL-2008 and select iq_loopback as the top level. Put custom_payload.py on the Python import path and set COCOTB_TEST_MODULES=custom_payload. For your DUT, change the bindings and the adapter’s field lookups and conversions. keep and strb require a byte-aligned vector; leave them unbound on a record bus.