|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""SYNTH_MEMORY_MAX_BITS stops synthesis on a read/write memory over the |
| 3 | +limit, and lets a ROM of the same size through. |
| 4 | +
|
| 5 | +mem_dump.py reads the memories yosys inferred (mem.json) and exits with |
| 6 | +an error when one is over the limit. A ROM, a $mem_v2 with no write |
| 7 | +port, holds constants that synthesis maps to logic: it is listed, marked |
| 8 | +as a ROM, and not counted. |
| 9 | +""" |
| 10 | + |
| 11 | +import json |
| 12 | +import os |
| 13 | +import subprocess |
| 14 | +import sys |
| 15 | +import tempfile |
| 16 | +import unittest |
| 17 | + |
| 18 | +MEM_DUMP = os.path.join( |
| 19 | + os.path.dirname(os.path.abspath(__file__)), "..", "scripts", "mem_dump.py" |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +def mem_json(wr_ports, size=64, width=128): |
| 24 | + def b(n): |
| 25 | + return format(n, "032b") |
| 26 | + |
| 27 | + return { |
| 28 | + "modules": { |
| 29 | + "top": { |
| 30 | + "attributes": {}, |
| 31 | + "cells": { |
| 32 | + "mem": { |
| 33 | + "type": "$mem_v2", |
| 34 | + "attributes": {}, |
| 35 | + "parameters": { |
| 36 | + "SIZE": b(size), |
| 37 | + "WIDTH": b(width), |
| 38 | + "WR_PORTS": b(wr_ports), |
| 39 | + }, |
| 40 | + } |
| 41 | + }, |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | +class TestMemDump(unittest.TestCase): |
| 48 | + def run_mem_dump(self, data, max_bits): |
| 49 | + with tempfile.TemporaryDirectory() as tmp: |
| 50 | + path = os.path.join(tmp, "mem.json") |
| 51 | + with open(path, "w") as f: |
| 52 | + json.dump(data, f) |
| 53 | + return subprocess.run( |
| 54 | + [sys.executable, MEM_DUMP, "--max-bits", str(max_bits), path], |
| 55 | + capture_output=True, |
| 56 | + text=True, |
| 57 | + ) |
| 58 | + |
| 59 | + def test_ram_over_limit_stops(self): |
| 60 | + # 64 x 128 = 8192 bits, one write port |
| 61 | + result = self.run_mem_dump(mem_json(wr_ports=1), 4096) |
| 62 | + self.assertNotEqual(result.returncode, 0, result.stdout) |
| 63 | + self.assertIn("exceeds SYNTH_MEMORY_MAX_BITS", result.stderr) |
| 64 | + |
| 65 | + def test_rom_over_limit_passes(self): |
| 66 | + # the same 8192 bits with no write port |
| 67 | + result = self.run_mem_dump(mem_json(wr_ports=0), 4096) |
| 68 | + self.assertEqual(result.returncode, 0, result.stderr) |
| 69 | + self.assertIn("ROM top", result.stdout) |
| 70 | + |
| 71 | + def test_ram_under_limit_passes(self): |
| 72 | + result = self.run_mem_dump(mem_json(wr_ports=1), 8192) |
| 73 | + self.assertEqual(result.returncode, 0, result.stderr) |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + unittest.main() |
0 commit comments