"""gen_vectors.py - Test vector generator for storage (sd_bram) module.

Generates (addr, data) write pairs and expected read-back values.
Address space: 0..63 (6-bit) | Data width: 48-bit.
"""

import os
import sys

sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', 'lib'))

from vector_gen import VectorGenerator


class StorageVectorGenerator(VectorGenerator):
    """Generates write/read test vectors for sd_bram behavioral RAM."""

    def generate_one(self, params: dict) -> dict:
        """Generate a single (addr, data) write pair.

        Uses sequential addresses 0..N-1 and deterministic 48-bit data
        patterns for reproducible testing.

        Args:
            params: Unused for storage basic case.

        Returns:
            dict with 'input' and 'expected' keys.
        """
        # Sequential address assignment during generation
        # addr is determined by position; data is a deterministic pattern
        # The actual index is handled in write_hex_file for sequential addresses
        return {
            'data': 0  # placeholder — filled in write_hex_file by index
        }

    def write_hex_file(self, vectors: list[dict], filepath: str) -> None:
        """Write input vectors as "AA DDDDDDDDDDDD" hex lines.

        Uses sequential addresses 0..N-1 and deterministic 48-bit data:
        data = (addr * 0x100000000001 + 0xDEADBEEFCAFE) & 0xFFFF_FFFF_FFFF

        Args:
            vectors: List of vector 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 i in range(len(vectors)):
                addr = i
                # Deterministic 48-bit pattern: spreads bits across the word
                data = ((addr + 1) * 0x100000000001 + 0xDEADBEEFCAFE) & 0xFFFFFFFFFFFF
                f.write(f'{addr:02X} {data:012X}\n')

    def write_expected_file(self, vectors: list[dict], filepath: str) -> None:
        """Write expected read-back values (same patterns as written).

        Args:
            vectors: List of vector 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 i in range(len(vectors)):
                addr = i
                data = ((addr + 1) * 0x100000000001 + 0xDEADBEEFCAFE) & 0xFFFFFFFFFFFF
                f.write(f'{data:012X}\n')
