Files
mlkem-sync/test_framework/lib/vector_gen.py
FallenSigh 8fdf944555 feat: init mlkem project with Verilator test framework
- sync_rtl/common/: skid_buffer, pipeline_reg, defines (valid/ready)
- sync_rtl/mod_add/: modular adder example with Verilator C++ TB
- test_framework/: Python-driven Verilator compile/sim/compare pipeline
- test_framework/modules/mod_add/: 50-vector test plan, full鏈路 PASS
- .trellis/spec/: RTL and test_framework conventions documented
2026-06-24 19:43:29 +08:00

62 lines
2.2 KiB
Python

"""vector_gen.py - Base class for vector generators.
Each module provides a gen_vectors.py that subclasses VectorGenerator.
The generate_one() method is abstract and must be overridden.
The write_hex_file() method writes vectors as hex-formatted files.
"""
import os
from abc import ABC, abstractmethod
class VectorGenerator(ABC):
"""Base class for test vector generators."""
@abstractmethod
def generate_one(self, params: dict) -> dict:
"""Generate a single test vector.
Args:
params: Module-specific parameters for vector generation.
Returns:
dict with 'input' and 'expected' keys, each containing a dict
of signal_name -> value.
"""
...
def write_hex_file(self, vectors: list[dict], filepath: str) -> None:
"""Write vectors to a hex file.
Subclasses should override this to match their module's input format.
Default: writes each vector on one line, hex values separated by spaces.
Args:
vectors: List of result dicts from generate_one().
filepath: Path to write the hex file.
"""
os.makedirs(os.path.dirname(filepath), exist_ok=True)
with open(filepath, 'w') as f:
for v in vectors:
# Default: write input values as space-separated hex
inputs = v.get('input', {})
hex_vals = [format(val, 'X') for val in inputs.values()]
f.write(' '.join(hex_vals) + '\n')
def write_expected_file(self, vectors: list[dict], filepath: str) -> None:
"""Write expected results to a hex file.
Subclasses should override this to match their module's expected format.
Default: writes each expected value on one line.
Args:
vectors: List of result dicts from generate_one().
filepath: Path to write the expected hex file.
"""
os.makedirs(os.path.dirname(filepath), exist_ok=True)
with open(filepath, 'w') as f:
for v in vectors:
expected = v.get('expected', {})
hex_vals = [format(val, 'X') for val in expected.values()]
f.write(' '.join(hex_vals) + '\n')