|
| 1 | +"""CI test for SRAM masked-write support.""" |
| 2 | + |
| 3 | +import re |
| 4 | + |
| 5 | +from assassyn.frontend import * |
| 6 | +from assassyn.test import run_test |
| 7 | + |
| 8 | + |
| 9 | +ADDR0 = 0x12 |
| 10 | +ADDR1 = 0x34 |
| 11 | + |
| 12 | +OPS = [ |
| 13 | + {"kind": "write", "addr": ADDR0, "wdata": 0x11223344, "wmask": 0xFFFFFFFF}, |
| 14 | + {"kind": "read", "addr": ADDR0, "expected": 0x11223344}, |
| 15 | + {"kind": "write", "addr": ADDR0, "wdata": 0xFFFFFFFF, "wmask": 0x00000000}, |
| 16 | + {"kind": "read", "addr": ADDR0, "expected": 0x11223344}, |
| 17 | + {"kind": "write", "addr": ADDR0, "wdata": 0x000000AA, "wmask": 0x000000FF}, |
| 18 | + {"kind": "read", "addr": ADDR0, "expected": 0x112233AA}, |
| 19 | + {"kind": "write", "addr": ADDR0, "wdata": 0x0000BB00, "wmask": 0x0000FF00}, |
| 20 | + {"kind": "read", "addr": ADDR0, "expected": 0x1122BBAA}, |
| 21 | + {"kind": "write", "addr": ADDR0, "wdata": 0x00CC0000, "wmask": 0x00FF0000}, |
| 22 | + {"kind": "read", "addr": ADDR0, "expected": 0x11CCBBAA}, |
| 23 | + {"kind": "write", "addr": ADDR0, "wdata": 0xDD000000, "wmask": 0xFF000000}, |
| 24 | + {"kind": "read", "addr": ADDR0, "expected": 0xDDCCBBAA}, |
| 25 | + {"kind": "write", "addr": ADDR0, "wdata": 0x0000EEFF, "wmask": 0x0000FFFF}, |
| 26 | + {"kind": "read", "addr": ADDR0, "expected": 0xDDCCEEFF}, |
| 27 | + {"kind": "write", "addr": ADDR0, "wdata": 0xA1B20000, "wmask": 0xFFFF0000}, |
| 28 | + {"kind": "read", "addr": ADDR0, "expected": 0xA1B2EEFF}, |
| 29 | + {"kind": "write", "addr": ADDR1, "wdata": 0x55667788, "wmask": 0xFFFFFFFF}, |
| 30 | + {"kind": "read", "addr": ADDR1, "expected": 0x55667788}, |
| 31 | + {"kind": "write", "addr": ADDR1, "wdata": 0x00990000, "wmask": 0x00FF0000}, |
| 32 | + {"kind": "read", "addr": ADDR1, "expected": 0x55997788}, |
| 33 | + {"kind": "write", "addr": ADDR1, "wdata": 0xAA5500CC, "wmask": 0x0F0F00F0}, |
| 34 | + {"kind": "read", "addr": ADDR1, "expected": 0x5A9577C8}, |
| 35 | + {"kind": "read", "addr": ADDR0, "expected": 0xA1B2EEFF}, |
| 36 | +] |
| 37 | + |
| 38 | + |
| 39 | +class ReadObserver(Module): |
| 40 | + |
| 41 | + def __init__(self): |
| 42 | + super().__init__( |
| 43 | + ports={ |
| 44 | + "step": Port(Bits(8)), |
| 45 | + "addr": Port(Bits(9)), |
| 46 | + } |
| 47 | + ) |
| 48 | + |
| 49 | + @module.combinational |
| 50 | + def build(self, rdata: RegArray): |
| 51 | + step, addr = self.pop_all_ports(True) |
| 52 | + log( |
| 53 | + "masked_read step={} addr=0x{:03x} data=0x{:08x}", |
| 54 | + step, |
| 55 | + addr, |
| 56 | + rdata[0].bitcast(Bits(32)), |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +class Launcher(Module): |
| 61 | + |
| 62 | + def __init__(self, target): |
| 63 | + super().__init__(ports={}) |
| 64 | + self.target = target |
| 65 | + |
| 66 | + @module.combinational |
| 67 | + def build(self): |
| 68 | + self.target.async_called() |
| 69 | + |
| 70 | + |
| 71 | +class MaskedDriver(Module): |
| 72 | + |
| 73 | + def __init__(self, observer): |
| 74 | + super().__init__(ports={}) |
| 75 | + self.name = "Driver" |
| 76 | + self.observer = observer |
| 77 | + |
| 78 | + @module.combinational |
| 79 | + def build(self): |
| 80 | + phase_bits = max(1, (len(OPS) + 1).bit_length()) |
| 81 | + phase = RegArray(UInt(phase_bits), 1, initializer=[0]) |
| 82 | + state = phase[0] |
| 83 | + next_state = state + UInt(phase_bits)(1) |
| 84 | + (phase & self)[0] <= next_state |
| 85 | + |
| 86 | + we = Bits(1)(0) |
| 87 | + re = Bits(1)(0) |
| 88 | + addr = Bits(9)(0) |
| 89 | + wdata = Bits(32)(0) |
| 90 | + wmask = Bits(32)(0) |
| 91 | + |
| 92 | + for idx, op in enumerate(OPS): |
| 93 | + is_step = state == UInt(phase_bits)(idx) |
| 94 | + addr_bits = Bits(9)(op["addr"]) |
| 95 | + addr = is_step.select(addr_bits, addr) |
| 96 | + |
| 97 | + if op["kind"] == "write": |
| 98 | + we = is_step.select(Bits(1)(1), we) |
| 99 | + wdata = is_step.select(Bits(32)(op["wdata"]), wdata) |
| 100 | + wmask = is_step.select(Bits(32)(op["wmask"]), wmask) |
| 101 | + else: |
| 102 | + re = is_step.select(Bits(1)(1), re) |
| 103 | + with Condition(is_step): |
| 104 | + self.observer.async_called(step=Bits(8)(idx), addr=addr_bits) |
| 105 | + |
| 106 | + sram = SRAM(32, 512, None) |
| 107 | + sram.build(we, re, addr, wdata, wmask) |
| 108 | + |
| 109 | + with Condition(state == UInt(phase_bits)(len(OPS))): |
| 110 | + finish() |
| 111 | + |
| 112 | + return sram |
| 113 | + |
| 114 | + |
| 115 | +READ_RE = re.compile( |
| 116 | + r"masked_read step=(\d+) addr=0x([0-9a-fA-F]+) data=0x([0-9a-fA-F]+)" |
| 117 | +) |
| 118 | + |
| 119 | + |
| 120 | +def check(raw): |
| 121 | + expected_reads = [ |
| 122 | + (idx, op["addr"], op["expected"]) |
| 123 | + for idx, op in enumerate(OPS) |
| 124 | + if op["kind"] == "read" |
| 125 | + ] |
| 126 | + |
| 127 | + actual_reads = [] |
| 128 | + for line in raw.splitlines(): |
| 129 | + if "[readobserver" not in line.lower(): |
| 130 | + continue |
| 131 | + match = READ_RE.search(line) |
| 132 | + assert match is not None, f"Unexpected ReadObserver log line: {line}" |
| 133 | + actual_reads.append( |
| 134 | + ( |
| 135 | + int(match.group(1)), |
| 136 | + int(match.group(2), 16), |
| 137 | + int(match.group(3), 16), |
| 138 | + ) |
| 139 | + ) |
| 140 | + |
| 141 | + assert actual_reads == expected_reads, ( |
| 142 | + f"Masked SRAM reads mismatch.\n" |
| 143 | + f"expected={expected_reads}\n" |
| 144 | + f"actual={actual_reads}" |
| 145 | + ) |
| 146 | + |
| 147 | + |
| 148 | +def test_sram_masked_write(): |
| 149 | + def top(): |
| 150 | + observer = ReadObserver() |
| 151 | + driver = MaskedDriver(observer) |
| 152 | + sram = driver.build() |
| 153 | + launcher = Launcher(driver) |
| 154 | + launcher.build() |
| 155 | + observer.build(sram.dout) |
| 156 | + |
| 157 | + run_test("sram_masked", top, check, sim_threshold=200, idle_threshold=200) |
| 158 | + |
| 159 | + |
| 160 | +if __name__ == "__main__": |
| 161 | + test_sram_masked_write() |
0 commit comments