|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""AUTO_MEMORIES drops the parameters from the instances of a |
| 3 | +parameterized memory module it blackboxes. |
| 4 | +
|
| 5 | +A black box keeps its instances' parameters, and the netlist writes them |
| 6 | +out (`Sram #(.NUM_ENTRIES(32)) u0`), which OpenROAD's Verilog reader |
| 7 | +refuses for a macro. After auto_memories_drop_parameters the instance is |
| 8 | +written as a plain instance of the macro, its ports unchanged. |
| 9 | +
|
| 10 | +Skipped when no yosys with read_slang is available: YOSYS_EXE or yosys on |
| 11 | +PATH, with the plugin from SLANG_PLUGIN_PATH when read_slang is not built |
| 12 | +in. |
| 13 | +""" |
| 14 | + |
| 15 | +import os |
| 16 | +import shutil |
| 17 | +import subprocess |
| 18 | +import tempfile |
| 19 | +import unittest |
| 20 | + |
| 21 | +DROP_TCL = os.path.join( |
| 22 | + os.path.dirname(os.path.abspath(__file__)), |
| 23 | + "..", |
| 24 | + "scripts", |
| 25 | + "memories", |
| 26 | + "drop_parameters.tcl", |
| 27 | +) |
| 28 | + |
| 29 | +DESIGN = """\ |
| 30 | +module Sram #(parameter NUM_ENTRIES = 64) ( |
| 31 | + input clk, input we, input [$clog2(NUM_ENTRIES)-1:0] addr, |
| 32 | + input [15:0] d, output logic [15:0] q); |
| 33 | + logic [15:0] mem [0:NUM_ENTRIES-1]; |
| 34 | + always_ff @(posedge clk) begin |
| 35 | + if (we) mem[addr] <= d; |
| 36 | + q <= mem[addr]; |
| 37 | + end |
| 38 | +endmodule |
| 39 | +module top (input clk, input we, input [4:0] addr, input [15:0] d, |
| 40 | + output [15:0] q0, q1); |
| 41 | + Sram #(.NUM_ENTRIES(32)) u0 (.clk, .we, .addr, .d, .q(q0)); |
| 42 | + Sram #(.NUM_ENTRIES(32)) u1 (.clk, .we, .addr, .d, .q(q1)); |
| 43 | +endmodule |
| 44 | +""" |
| 45 | + |
| 46 | + |
| 47 | +def yosys_command(): |
| 48 | + yosys = os.environ.get("YOSYS_EXE") or shutil.which("yosys") |
| 49 | + if not yosys: |
| 50 | + return None |
| 51 | + cmd = [yosys, "-q"] |
| 52 | + plugin = os.environ.get("SLANG_PLUGIN_PATH") |
| 53 | + if plugin: |
| 54 | + cmd += ["-m", plugin] |
| 55 | + probe = subprocess.run(cmd + ["-p", "help read_slang"], capture_output=True) |
| 56 | + return cmd if probe.returncode == 0 else None |
| 57 | + |
| 58 | + |
| 59 | +class TestAutoMemoriesParameters(unittest.TestCase): |
| 60 | + def netlist(self, drop): |
| 61 | + cmd = yosys_command() |
| 62 | + if cmd is None: |
| 63 | + self.skipTest("no yosys with read_slang") |
| 64 | + with tempfile.TemporaryDirectory() as tmp: |
| 65 | + design = os.path.join(tmp, "top.sv") |
| 66 | + with open(design, "w") as f: |
| 67 | + f.write(DESIGN) |
| 68 | + netlist = os.path.join(tmp, "out.v") |
| 69 | + script = os.path.join(tmp, "drop.tcl") |
| 70 | + with open(script, "w") as f: |
| 71 | + f.write( |
| 72 | + "yosys -import\n" |
| 73 | + f"source {{{DROP_TCL}}}\n" |
| 74 | + "yosys read_slang --keep-hierarchy --top top" |
| 75 | + f" --blackboxed-module Sram {{{design}}}\n" |
| 76 | + + ("auto_memories_drop_parameters [list Sram]\n" if drop else "") |
| 77 | + + "hierarchy -top top\n" |
| 78 | + f"write_verilog -noattr {{{netlist}}}\n" |
| 79 | + ) |
| 80 | + result = subprocess.run( |
| 81 | + cmd + ["-c", script], capture_output=True, text=True |
| 82 | + ) |
| 83 | + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) |
| 84 | + with open(netlist) as f: |
| 85 | + return f.read() |
| 86 | + |
| 87 | + def test_black_box_keeps_parameters(self): |
| 88 | + # The problem: without the proc the instance is parameterized. |
| 89 | + self.assertIn("Sram #(", self.netlist(drop=False)) |
| 90 | + |
| 91 | + def test_parameters_dropped(self): |
| 92 | + netlist = self.netlist(drop=True) |
| 93 | + self.assertNotIn("#(", netlist) |
| 94 | + self.assertIn("Sram u0", netlist) |
| 95 | + self.assertIn("Sram u1", netlist) |
| 96 | + self.assertIn(".addr(addr)", netlist) |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + unittest.main() |
0 commit comments