"""Reference models for the cocotb examples. Plain Python and NumPy only. They know nothing about the simulator, so they can be unit-tested on their own (see test_models.py) and reused by the cocotb tests. """ from __future__ import annotations from collections import deque from dataclasses import dataclass import numpy as np @dataclass class Accum8Model: """Cycle model of accum8.sv: call step() once per rising clock edge.""" acc: int = 0 carry: int = 0 def step(self, rst: int, clr: int, en: int, din: int) -> None: if rst or clr: self.acc, self.carry = 0, 0 elif en: total = self.acc + din self.acc, self.carry = total & 0xFF, total >> 8 def muladd_ref(a: np.ndarray, b: np.ndarray, c: np.ndarray) -> np.ndarray: """y = a * b + c, computed exactly in 64-bit integers.""" return a.astype(np.int64) * b.astype(np.int64) + c.astype(np.int64) class SeqDetectModel: """Model of the 1011 detector: detect is high when the last four bits clocked in were 1, 0, 1, 1 (overlapping matches allowed).""" PATTERN = (1, 0, 1, 1) def __init__(self) -> None: self.history: deque[int] = deque(maxlen=4) def reset(self) -> None: self.history.clear() def step(self, din: int) -> None: self.history.append(din) @property def detect(self) -> int: return int(tuple(self.history) == self.PATTERN)