|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import re |
| 5 | +import io |
| 6 | +import sys |
| 7 | +import unittest |
| 8 | + |
| 9 | +sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "utils"))) |
| 10 | +from single_port_ram_verilog_exporter import SinglePortRAMVerilogExporter |
| 11 | +from memory_factory import MemoryFactory |
| 12 | +from class_process import Process |
| 13 | +from timing_data import TimingData |
| 14 | +from test_utils import TestUtils |
| 15 | + |
| 16 | + |
| 17 | +class SVBBoxDataTest(unittest.TestCase): |
| 18 | + """Unit test for SystemVerilog blackbox class""" |
| 19 | + |
| 20 | + def setUp(self): |
| 21 | + """Sets up base_data with example config data""" |
| 22 | + self._process = Process(TestUtils.get_base_process_data()) |
| 23 | + self._timing_data = TimingData() |
| 24 | + self._bus_re = re.compile( |
| 25 | + "^\s*\S+\s+(?:reg)?\s*\[\s*(\d+)\:(\d+)\\s*]\s*(\S+)\s*\," |
| 26 | + ) |
| 27 | + |
| 28 | + def _extract_bus_msb(self, content): |
| 29 | + msb_map = {} |
| 30 | + in_strm = io.StringIO(content) |
| 31 | + |
| 32 | + for line in in_strm: |
| 33 | + result = self._bus_re.match(line) |
| 34 | + if result: |
| 35 | + msb = int(result.group(1)) |
| 36 | + bus_name = result.group(3) |
| 37 | + msb_map[bus_name] = msb |
| 38 | + return msb_map |
| 39 | + |
| 40 | + def _check_bus_msb(self, mem_name, bus_name, bus_msb_map, exp_msb): |
| 41 | + self.assertIn(bus_name, bus_msb_map) |
| 42 | + self.assertEqual( |
| 43 | + bus_msb_map[bus_name], |
| 44 | + exp_msb, |
| 45 | + f"msb's don't match for {bus_name} on {mem_name}: {bus_msb_map[bus_name]} {exp_msb}", |
| 46 | + ) |
| 47 | + |
| 48 | + def _check_bbox(self, words, depth): |
| 49 | + name = f"fakeram_16_{depth}" |
| 50 | + mem = MemoryFactory.create( |
| 51 | + name, words, depth, 4, "RAM", "SP", self._process, self._timing_data |
| 52 | + ) |
| 53 | + exporter = SinglePortRAMVerilogExporter(mem) |
| 54 | + strm = io.StringIO() |
| 55 | + exporter.export_blackbox(strm) |
| 56 | + content = strm.getvalue() |
| 57 | + strm.close() |
| 58 | + bus_msb_map = self._extract_bus_msb(content) |
| 59 | + self._check_bus_msb(name, "rd_out", bus_msb_map, mem.get_data_bus_msb()) |
| 60 | + self._check_bus_msb(name, "wd_in", bus_msb_map, mem.get_data_bus_msb()) |
| 61 | + self._check_bus_msb(name, "addr_in", bus_msb_map, mem.get_addr_bus_msb()) |
| 62 | + |
| 63 | + def test_spsram_bbox(self): |
| 64 | + """ |
| 65 | + Tests that the spsram rd_out and wd_in indices are the same and |
| 66 | + correct |
| 67 | + """ |
| 68 | + |
| 69 | + words = 16 |
| 70 | + depths = [32, 64, 256] |
| 71 | + for depth in depths: |
| 72 | + self._check_bbox(words, depth) |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == "__main__": |
| 76 | + unittest.main() |
0 commit comments