From e02b87982eefc7dfd0d0e5a235b30377662b6537 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Harboe?= Date: Wed, 23 Sep 2026 11:19:55 +0200 Subject: [PATCH] riscv32i: declare dmem's procedurally assigned signals as regs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit dmem.v assigns we_mem and mem_out inside always @* blocks while declaring we_mem a wire and leaving mem_out an implicit net. A net cannot be the target of a procedural assignment (IEEE 1364-2005 6.1.2, IEEE 1800-2023 6.5). yosys's Verilog frontend accepts it and infers the variable; slang rejects it, nine times: dmem.v:33:10: error: cannot assign to a net within a procedural context so riscv32i cannot be read with SYNTH_HDL_FRONTEND=slang. The declarations are fixed, not the code: `reg [3:0] we_mem` and `output reg [31:0] mem_out`, which is what both frontends build. The always blocks and drivers are unchanged. The file keeps its CRLF line endings. flow/test/test_riscv32i_slang.py reads the riscv32i sources with read_slang, a black-box module standing in for the fakeram7_256x32 macro the flow takes from liberty, and asserts slang accepts them. It fails before this change, and skips where no yosys with read_slang is available (YOSYS_EXE or yosys on PATH, SLANG_PLUGIN_PATH for the plugin). Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Øyvind Harboe --- flow/designs/src/riscv32i/dmem.v | 4 +- flow/test/test_riscv32i_slang.py | 70 ++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 flow/test/test_riscv32i_slang.py diff --git a/flow/designs/src/riscv32i/dmem.v b/flow/designs/src/riscv32i/dmem.v index 6cda09216e..bbc1a16741 100755 --- a/flow/designs/src/riscv32i/dmem.v +++ b/flow/designs/src/riscv32i/dmem.v @@ -13,10 +13,10 @@ module dmem (clk, r_w, mem_addr, mem_data, mem_out); input r_w; input [31:0] mem_addr; input [31:0] mem_data; - output [31:0] mem_out; + output reg [31:0] mem_out; wire [1:0] sel_mem; wire [3:0] ce_mem; - wire [3:0] we_mem; + reg [3:0] we_mem; wire [31:0] inter_dmem0; wire [31:0] inter_dmem1; wire [31:0] inter_dmem2; diff --git a/flow/test/test_riscv32i_slang.py b/flow/test/test_riscv32i_slang.py new file mode 100644 index 0000000000..cc386a7804 --- /dev/null +++ b/flow/test/test_riscv32i_slang.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 +"""riscv32i's sources read under slang, which is stricter than yosys's own +Verilog frontend: a signal assigned in an always block must be declared a +variable (reg), not a net (IEEE 1364-2005 6.1.2, IEEE 1800-2023 6.5). + +The SRAM macro comes from its liberty file in the flow; here a black-box +module with the same ports stands in for it. Skipped when no yosys with +read_slang is available: YOSYS_EXE or yosys on PATH, with the plugin from +SLANG_PLUGIN_PATH when read_slang is not built in. +""" + +import glob +import os +import shutil +import subprocess +import tempfile +import unittest + +SRC_DIR = os.path.join( + os.path.dirname(os.path.abspath(__file__)), "..", "designs", "src", "riscv32i" +) + +FAKERAM_STUB = """\ +(* blackbox *) +module fakeram7_256x32 ( + input clk, + input [31:0] wd_in, + input ce_in, + input we_in, + output [31:0] rd_out, + input [7:0] addr_in +); +endmodule +""" + + +def yosys_command(): + yosys = os.environ.get("YOSYS_EXE") or shutil.which("yosys") + if not yosys: + return None + cmd = [yosys, "-q"] + plugin = os.environ.get("SLANG_PLUGIN_PATH") + if plugin: + cmd += ["-m", plugin] + probe = subprocess.run(cmd + ["-p", "help read_slang"], capture_output=True) + return cmd if probe.returncode == 0 else None + + +class TestRiscv32iSlang(unittest.TestCase): + def test_sources_read_under_slang(self): + cmd = yosys_command() + if cmd is None: + self.skipTest("no yosys with read_slang") + with tempfile.TemporaryDirectory() as tmp: + stub = os.path.join(tmp, "fakeram7_256x32.v") + with open(stub, "w") as f: + f.write(FAKERAM_STUB) + sources = sorted(glob.glob(os.path.join(SRC_DIR, "*.v"))) + [stub] + result = subprocess.run( + cmd + ["-p", f"read_slang {' '.join(sources)} --top riscv_top"], + capture_output=True, + text=True, + ) + output = result.stdout + result.stderr + self.assertNotIn("cannot assign to a net", output) + self.assertEqual(result.returncode, 0, output) + + +if __name__ == "__main__": + unittest.main()