Tests/cocotb (#19)
* wip * reorg * update sv int * apb4 working * apb3 working * version bump + ignore runner warning * remove redundant check * adding log on failure * cleaning up verilator version issue * devcontainer * Fix missing libpython in GitHub Actions CI environment (#21) * Initial plan * Install libpython in GitHub Actions for cocotb tests Co-authored-by: arnavsacheti <36746504+arnavsacheti@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: arnavsacheti <36746504+arnavsacheti@users.noreply.github.com> --------- Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -1,3 +1,10 @@
|
||||
from pathlib import Path
|
||||
"""Manifest of SystemRDL sources used by the cocotb simulations."""
|
||||
|
||||
rdls = map(Path, ["simple.rdl", "multiple_reg.rdl"])
|
||||
RDL_CASES: list[tuple[str, str]] = [
|
||||
("simple.rdl", "simple_test"),
|
||||
("multiple_reg.rdl", "multi_reg"),
|
||||
("deep_hierarchy.rdl", "deep_hierarchy"),
|
||||
("wide_status.rdl", "wide_status"),
|
||||
("variable_layout.rdl", "variable_layout"),
|
||||
("asymmetric_bus.rdl", "asymmetric_bus"),
|
||||
]
|
||||
|
||||
69
tests/cocotb_lib/handle_utils.py
Normal file
69
tests/cocotb_lib/handle_utils.py
Normal file
@@ -0,0 +1,69 @@
|
||||
"""Utilities for resolving cocotb signal handles across simulators."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Iterable
|
||||
|
||||
|
||||
class SignalHandle:
|
||||
"""
|
||||
Wrapper that resolves array elements even when the simulator does not expose
|
||||
unpacked arrays through ``handle[idx]``.
|
||||
"""
|
||||
|
||||
def __init__(self, dut, name: str) -> None:
|
||||
self._dut = dut
|
||||
self._name = name
|
||||
self._base = getattr(dut, name, None)
|
||||
self._cache: dict[tuple[int, ...], Any] = {}
|
||||
|
||||
def resolve(self, indices: tuple[int, ...]):
|
||||
if not indices:
|
||||
return self._base if self._base is not None else self._lookup(tuple())
|
||||
|
||||
if indices not in self._cache:
|
||||
self._cache[indices] = self._direct_or_lookup(indices)
|
||||
return self._cache[indices]
|
||||
|
||||
def _direct_or_lookup(self, indices: tuple[int, ...]):
|
||||
if self._base is not None:
|
||||
ref = self._base
|
||||
try:
|
||||
for idx in indices:
|
||||
ref = ref[idx]
|
||||
return ref
|
||||
except (IndexError, TypeError, AttributeError):
|
||||
pass
|
||||
|
||||
return self._lookup(indices)
|
||||
|
||||
def _lookup(self, indices: tuple[int, ...]):
|
||||
suffix = "".join(f"[{idx}]" for idx in indices)
|
||||
path = f"{self._name}{suffix}"
|
||||
|
||||
try:
|
||||
return getattr(self._dut, path)
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
errors: list[Exception] = []
|
||||
for extended in (False, True):
|
||||
try:
|
||||
return self._dut._id(path, extended=extended)
|
||||
except (AttributeError, ValueError) as exc:
|
||||
errors.append(exc)
|
||||
|
||||
raise AttributeError(f"Unable to resolve handle '{path}' via dut._id") from errors[-1]
|
||||
|
||||
|
||||
def resolve_handle(handle, indices: Iterable[int]):
|
||||
"""Resolve either a regular cocotb handle or a ``SignalHandle`` wrapper."""
|
||||
index_tuple = tuple(indices)
|
||||
|
||||
if isinstance(handle, SignalHandle):
|
||||
return handle.resolve(index_tuple)
|
||||
|
||||
ref = handle
|
||||
for idx in index_tuple:
|
||||
ref = ref[idx]
|
||||
return ref
|
||||
105
tests/cocotb_lib/rdl/asymmetric_bus.rdl
Normal file
105
tests/cocotb_lib/rdl/asymmetric_bus.rdl
Normal file
@@ -0,0 +1,105 @@
|
||||
regfile port_rf {
|
||||
reg {
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x0;
|
||||
} port_enable[0:0];
|
||||
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x0;
|
||||
} port_speed[3:1];
|
||||
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x0;
|
||||
} port_width[8:4];
|
||||
} control @ 0x0;
|
||||
|
||||
reg {
|
||||
field {
|
||||
sw = r;
|
||||
hw = w;
|
||||
reset = 0x0;
|
||||
} error_count[15:0];
|
||||
|
||||
field {
|
||||
sw = r;
|
||||
hw = w;
|
||||
reset = 0x0;
|
||||
} retry_count[31:16];
|
||||
} counters @ 0x4;
|
||||
|
||||
reg {
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x0;
|
||||
} qos[7:0];
|
||||
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x0;
|
||||
} virtual_channel[9:8];
|
||||
} qos @ 0x8;
|
||||
};
|
||||
|
||||
addrmap asymmetric_bus {
|
||||
reg {
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x0;
|
||||
} control[3:0];
|
||||
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x0;
|
||||
} id[15:4];
|
||||
} control @ 0x0;
|
||||
|
||||
reg {
|
||||
field {
|
||||
sw = r;
|
||||
hw = w;
|
||||
reset = 0x0;
|
||||
} status_flags[19:0];
|
||||
} status @ 0x4;
|
||||
|
||||
reg {
|
||||
regwidth = 64;
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x00abcdef;
|
||||
} timestamp_low[31:0];
|
||||
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x00123456;
|
||||
} timestamp_high[55:32];
|
||||
} timestamp @ 0x8;
|
||||
|
||||
reg {
|
||||
regwidth = 128;
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x0;
|
||||
} extended_id[63:0];
|
||||
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x1;
|
||||
} parity[64:64];
|
||||
} extended @ 0x10;
|
||||
|
||||
port_rf port[6] @ 0x40 += 0x20;
|
||||
};
|
||||
115
tests/cocotb_lib/rdl/deep_hierarchy.rdl
Normal file
115
tests/cocotb_lib/rdl/deep_hierarchy.rdl
Normal file
@@ -0,0 +1,115 @@
|
||||
addrmap deep_hierarchy {
|
||||
regfile context_rf {
|
||||
reg {
|
||||
field {
|
||||
sw = rw;
|
||||
hw = r;
|
||||
reset = 0x1;
|
||||
} enable[7:0];
|
||||
|
||||
field {
|
||||
sw = r;
|
||||
hw = w;
|
||||
onread = rclr;
|
||||
reset = 0x0;
|
||||
} status[15:8];
|
||||
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x55;
|
||||
} mode[23:16];
|
||||
} command @ 0x0;
|
||||
|
||||
reg {
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x1234;
|
||||
} threshold[15:0];
|
||||
} threshold @ 0x4;
|
||||
|
||||
reg {
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x0;
|
||||
} counter[31:0];
|
||||
} counter @ 0x8;
|
||||
};
|
||||
|
||||
regfile engine_rf {
|
||||
context_rf context[3] @ 0x0;
|
||||
|
||||
reg {
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x0;
|
||||
} timeout[15:0];
|
||||
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x1;
|
||||
} priority[19:16];
|
||||
} config @ 0x30;
|
||||
|
||||
reg {
|
||||
field {
|
||||
sw = r;
|
||||
hw = w;
|
||||
onread = rclr;
|
||||
reset = 0x0;
|
||||
} error[31:0];
|
||||
} error_log @ 0x34;
|
||||
};
|
||||
|
||||
addrmap fabric_slice {
|
||||
engine_rf engines[4] @ 0x0;
|
||||
|
||||
regfile monitor_rf {
|
||||
reg {
|
||||
field {
|
||||
sw = r;
|
||||
hw = w;
|
||||
reset = 0x0;
|
||||
} perf_count[31:0];
|
||||
} perf @ 0x0;
|
||||
|
||||
reg {
|
||||
field {
|
||||
sw = r;
|
||||
hw = w;
|
||||
reset = 0x0;
|
||||
} last_error[31:0];
|
||||
} last_error @ 0x4;
|
||||
};
|
||||
|
||||
monitor_rf monitor @ 0x400;
|
||||
|
||||
reg {
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0xdeadbeef;
|
||||
} fabric_ctrl[31:0];
|
||||
} fabric_ctrl @ 0x500;
|
||||
};
|
||||
|
||||
fabric_slice slices[2] @ 0x0 += 0x800;
|
||||
|
||||
reg {
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x1;
|
||||
} global_enable[0:0];
|
||||
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x4;
|
||||
} debug_level[3:1];
|
||||
} global_control @ 0x1000;
|
||||
};
|
||||
156
tests/cocotb_lib/rdl/variable_layout.rdl
Normal file
156
tests/cocotb_lib/rdl/variable_layout.rdl
Normal file
@@ -0,0 +1,156 @@
|
||||
reg ctrl_reg_t {
|
||||
desc = "Control register shared across channels.";
|
||||
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x1;
|
||||
} enable[0:0];
|
||||
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x2;
|
||||
} mode[3:1];
|
||||
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x0;
|
||||
} prescale[11:4];
|
||||
};
|
||||
|
||||
regfile channel_rf {
|
||||
ctrl_reg_t control @ 0x0;
|
||||
|
||||
reg {
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x0;
|
||||
} gain[11:0];
|
||||
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x200;
|
||||
} offset[23:12];
|
||||
} calibrate @ 0x4;
|
||||
|
||||
reg {
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x0;
|
||||
} sample_count[15:0];
|
||||
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x0;
|
||||
} error_count[31:16];
|
||||
} counters @ 0x8;
|
||||
};
|
||||
|
||||
regfile slice_rf {
|
||||
reg {
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x0;
|
||||
} slope[15:0];
|
||||
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x0;
|
||||
} intercept[31:16];
|
||||
} calibration @ 0x0;
|
||||
|
||||
reg {
|
||||
regwidth = 64;
|
||||
field {
|
||||
sw = r;
|
||||
hw = w;
|
||||
reset = 0x0;
|
||||
} min_val[31:0];
|
||||
|
||||
field {
|
||||
sw = r;
|
||||
hw = w;
|
||||
reset = 0x0;
|
||||
} max_val[63:32];
|
||||
} range @ 0x4;
|
||||
};
|
||||
|
||||
regfile tile_rf {
|
||||
channel_rf channel[3] @ 0x0;
|
||||
|
||||
reg {
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x0;
|
||||
} tile_mode[1:0];
|
||||
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x0;
|
||||
} tile_enable[2:2];
|
||||
} tile_ctrl @ 0x100;
|
||||
|
||||
slice_rf slice[2] @ 0x200;
|
||||
};
|
||||
|
||||
regfile summary_rf {
|
||||
reg {
|
||||
field {
|
||||
sw = r;
|
||||
hw = w;
|
||||
reset = 0x0;
|
||||
} total_errors[31:0];
|
||||
} errors @ 0x0;
|
||||
|
||||
reg {
|
||||
field {
|
||||
sw = r;
|
||||
hw = w;
|
||||
reset = 0x0;
|
||||
} total_samples[31:0];
|
||||
} samples @ 0x4;
|
||||
|
||||
reg {
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x0;
|
||||
} interrupt_enable[7:0];
|
||||
} interrupt_enable @ 0x8;
|
||||
};
|
||||
|
||||
addrmap variable_layout {
|
||||
tile_rf tiles[2] @ 0x0;
|
||||
|
||||
reg {
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x0;
|
||||
} watchdog_enable[0:0];
|
||||
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x100;
|
||||
} watchdog_timeout[16:1];
|
||||
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x0;
|
||||
} watchdog_mode[18:17];
|
||||
} watchdog @ 0x2000;
|
||||
|
||||
summary_rf summary @ 0x3000;
|
||||
};
|
||||
69
tests/cocotb_lib/rdl/wide_status.rdl
Normal file
69
tests/cocotb_lib/rdl/wide_status.rdl
Normal file
@@ -0,0 +1,69 @@
|
||||
reg status_reg_t {
|
||||
regwidth = 64;
|
||||
desc = "Status register capturing wide flags and sticky bits.";
|
||||
|
||||
field {
|
||||
sw = r;
|
||||
hw = w;
|
||||
onread = rclr;
|
||||
reset = 0x0;
|
||||
} flags[62:0];
|
||||
|
||||
field {
|
||||
sw = rw;
|
||||
hw = r;
|
||||
reset = 0x1;
|
||||
} sticky_bit[63:63];
|
||||
};
|
||||
|
||||
reg metrics_reg_t {
|
||||
regwidth = 64;
|
||||
desc = "Metrics register pairing counters with thresholds.";
|
||||
|
||||
field {
|
||||
sw = r;
|
||||
hw = w;
|
||||
reset = 0x0;
|
||||
} count[31:0];
|
||||
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x0;
|
||||
} threshold[63:32];
|
||||
};
|
||||
|
||||
addrmap wide_status {
|
||||
status_reg_t status_blocks[16] @ 0x0;
|
||||
|
||||
metrics_reg_t metrics[4] @ 0x400;
|
||||
|
||||
reg {
|
||||
regwidth = 128;
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x0;
|
||||
} configuration[127:0];
|
||||
} configuration @ 0x800;
|
||||
|
||||
reg {
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x0;
|
||||
} version_major[7:0];
|
||||
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x1;
|
||||
} version_minor[15:8];
|
||||
|
||||
field {
|
||||
sw = rw;
|
||||
hw = rw;
|
||||
reset = 0x0100;
|
||||
} build[31:16];
|
||||
} version @ 0x900;
|
||||
};
|
||||
@@ -1,9 +1,13 @@
|
||||
"""Common utilities for cocotb testbenches."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections import defaultdict
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from systemrdl import RDLCompiler
|
||||
from systemrdl.node import AddressableNode, AddrmapNode, RegNode
|
||||
|
||||
from peakrdl_busdecoder.cpuif.base_cpuif import BaseCpuif
|
||||
from peakrdl_busdecoder.exporter import BusDecoderExporter
|
||||
@@ -65,3 +69,206 @@ def get_verilog_sources(module_path: Path, package_path: Path, intf_files: list[
|
||||
# Add module file
|
||||
sources.append(str(module_path))
|
||||
return sources
|
||||
|
||||
|
||||
def prepare_cpuif_case(
|
||||
rdl_source: str,
|
||||
top_name: str,
|
||||
output_dir: Path,
|
||||
cpuif_cls: type[BaseCpuif],
|
||||
*,
|
||||
control_signal: str,
|
||||
max_samples_per_master: int = 3,
|
||||
exporter_kwargs: dict[str, Any] | None = None,
|
||||
) -> tuple[Path, Path, dict[str, Any]]:
|
||||
"""
|
||||
Compile SystemRDL, export the CPUIF, and build a configuration payload for cocotb tests.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
rdl_source:
|
||||
Path to the SystemRDL source file.
|
||||
top_name:
|
||||
Name of the top-level addrmap to elaborate.
|
||||
output_dir:
|
||||
Directory where generated HDL will be written.
|
||||
cpuif_cls:
|
||||
CPUIF implementation class to use during export.
|
||||
control_signal:
|
||||
Name of the control signal used to identify master ports
|
||||
(``"PSEL"`` for APB, ``"AWVALID"`` for AXI4-Lite, etc.).
|
||||
max_samples_per_master:
|
||||
Limit for the number of register addresses sampled per master in the test matrix.
|
||||
exporter_kwargs:
|
||||
Optional keyword overrides passed through to :class:`BusDecoderExporter`.
|
||||
|
||||
Returns
|
||||
-------
|
||||
tuple
|
||||
``(module_path, package_path, config_dict)``, where the configuration dictionary
|
||||
is JSON-serializable and describes masters, indices, and sampled transactions.
|
||||
"""
|
||||
compiler = RDLCompiler()
|
||||
compiler.compile_file(rdl_source)
|
||||
root = compiler.elaborate(top_name)
|
||||
top_node = root.top # type: ignore[assignment]
|
||||
|
||||
export_kwargs: dict[str, Any] = {"cpuif_cls": cpuif_cls}
|
||||
if exporter_kwargs:
|
||||
export_kwargs.update(exporter_kwargs)
|
||||
|
||||
exporter = BusDecoderExporter()
|
||||
exporter.export(root, str(output_dir), **export_kwargs)
|
||||
|
||||
module_name = export_kwargs.get("module_name", top_name)
|
||||
package_name = export_kwargs.get("package_name", f"{top_name}_pkg")
|
||||
|
||||
module_path = Path(output_dir) / f"{module_name}.sv"
|
||||
package_path = Path(output_dir) / f"{package_name}.sv"
|
||||
|
||||
config = _build_case_config(
|
||||
top_node,
|
||||
exporter.cpuif,
|
||||
control_signal,
|
||||
max_samples_per_master=max_samples_per_master,
|
||||
)
|
||||
|
||||
config["address_width"] = exporter.cpuif.addr_width
|
||||
config["data_width"] = exporter.cpuif.data_width
|
||||
config["byte_width"] = exporter.cpuif.data_width // 8
|
||||
|
||||
return module_path, package_path, config
|
||||
|
||||
|
||||
def _build_case_config(
|
||||
top_node: AddrmapNode,
|
||||
cpuif: BaseCpuif,
|
||||
control_signal: str,
|
||||
*,
|
||||
max_samples_per_master: int,
|
||||
) -> dict[str, Any]:
|
||||
master_entries: dict[str, dict[str, Any]] = {}
|
||||
|
||||
for child in cpuif.addressable_children:
|
||||
signal = cpuif.signal(control_signal, child)
|
||||
# Example: m_apb_tiles_PSEL[N_TILESS] -> m_apb_tiles
|
||||
base = signal.split("[", 1)[0]
|
||||
suffix = f"_{control_signal}"
|
||||
if not base.endswith(suffix):
|
||||
raise ValueError(f"Unable to derive port prefix from '{signal}'")
|
||||
port_prefix = base[: -len(suffix)]
|
||||
|
||||
master_entries[child.inst_name] = {
|
||||
"inst_name": child.inst_name,
|
||||
"port_prefix": port_prefix,
|
||||
"is_array": bool(child.is_array),
|
||||
"dimensions": list(child.array_dimensions or []),
|
||||
"indices": set(),
|
||||
}
|
||||
|
||||
# Map each register to its top-level master and collect addresses
|
||||
groups: dict[tuple[str, tuple[int, ...]], list[tuple[int, str]]] = defaultdict(list)
|
||||
|
||||
def visit(node: AddressableNode) -> None:
|
||||
if isinstance(node, RegNode):
|
||||
master = node # type: AddressableNode
|
||||
while master.parent is not top_node:
|
||||
parent = master.parent
|
||||
if not isinstance(parent, AddressableNode):
|
||||
raise RuntimeError("Encountered unexpected hierarchy while resolving master node")
|
||||
master = parent
|
||||
|
||||
inst_name = master.inst_name
|
||||
if inst_name not in master_entries:
|
||||
# Handles cases where the register itself is the master (direct child of top)
|
||||
signal = cpuif.signal(control_signal, master)
|
||||
base = signal.split("[", 1)[0]
|
||||
suffix = f"_{control_signal}"
|
||||
if not base.endswith(suffix):
|
||||
raise ValueError(f"Unable to derive port prefix from '{signal}'")
|
||||
port_prefix = base[: -len(suffix)]
|
||||
master_entries[inst_name] = {
|
||||
"inst_name": inst_name,
|
||||
"port_prefix": port_prefix,
|
||||
"is_array": bool(master.is_array),
|
||||
"dimensions": list(master.array_dimensions or []),
|
||||
"indices": set(),
|
||||
}
|
||||
|
||||
idx_tuple = tuple(master.current_idx or [])
|
||||
master_entries[inst_name]["indices"].add(idx_tuple)
|
||||
|
||||
relative_addr = int(node.absolute_address) - int(top_node.absolute_address)
|
||||
full_path = node.get_path()
|
||||
label = full_path.split(".", 1)[1] if "." in full_path else full_path
|
||||
groups[(inst_name, idx_tuple)].append((relative_addr, label))
|
||||
|
||||
for child in node.children(unroll=True):
|
||||
if isinstance(child, AddressableNode):
|
||||
visit(child)
|
||||
|
||||
visit(top_node)
|
||||
|
||||
masters_list = []
|
||||
for entry in master_entries.values():
|
||||
indices = entry["indices"] or {()}
|
||||
entry["indices"] = [list(idx) for idx in sorted(indices)]
|
||||
masters_list.append(
|
||||
{
|
||||
"inst_name": entry["inst_name"],
|
||||
"port_prefix": entry["port_prefix"],
|
||||
"is_array": entry["is_array"],
|
||||
"dimensions": entry["dimensions"],
|
||||
"indices": entry["indices"],
|
||||
}
|
||||
)
|
||||
|
||||
transactions = []
|
||||
for (inst_name, idx_tuple), items in groups.items():
|
||||
addresses = sorted({addr for addr, _ in items})
|
||||
samples = _sample_addresses(addresses, max_samples_per_master)
|
||||
for addr in samples:
|
||||
label = next(lbl for candidate, lbl in items if candidate == addr)
|
||||
transactions.append(
|
||||
{
|
||||
"address": addr,
|
||||
"master": inst_name,
|
||||
"index": list(idx_tuple),
|
||||
"label": label,
|
||||
}
|
||||
)
|
||||
|
||||
transactions.sort(key=lambda item: (item["master"], item["index"], item["address"]))
|
||||
|
||||
masters_list.sort(key=lambda item: item["inst_name"])
|
||||
|
||||
return {
|
||||
"masters": masters_list,
|
||||
"transactions": transactions,
|
||||
}
|
||||
|
||||
|
||||
def _sample_addresses(addresses: list[int], max_samples: int) -> list[int]:
|
||||
if len(addresses) <= max_samples:
|
||||
return addresses
|
||||
|
||||
samples: list[int] = []
|
||||
samples.append(addresses[0])
|
||||
if len(addresses) > 1:
|
||||
samples.append(addresses[-1])
|
||||
|
||||
if len(addresses) > 2:
|
||||
mid = addresses[len(addresses) // 2]
|
||||
if mid not in samples:
|
||||
samples.append(mid)
|
||||
|
||||
idx = 1
|
||||
while len(samples) < max_samples:
|
||||
pos = (len(addresses) * idx) // max_samples
|
||||
candidate = addresses[min(pos, len(addresses) - 1)]
|
||||
if candidate not in samples:
|
||||
samples.append(candidate)
|
||||
idx += 1
|
||||
|
||||
samples.sort()
|
||||
return samples
|
||||
|
||||
Reference in New Issue
Block a user