From 3787ea19cd06ba7f0e6078342c16f5d099b05989 Mon Sep 17 00:00:00 2001 From: Krystine Sherwin <93062060+KrystalDelusion@users.noreply.github.com> Date: Fri, 14 Nov 2025 11:47:40 +1300 Subject: [PATCH] tests/memfile: Test dump_meminit Change tests/memfile to a mktest, moving the prior run-test.sh contents into a new read_dir.sh. Add dump.ys (and friends) for testing dump_meminit. --- Makefile | 2 +- tests/memfile/.gitignore | 1 + tests/memfile/dump.ys | 62 ++++++++++++++++++++++++++++++++++++++ tests/memfile/dump_gate.v | 19 ++++++++++++ tests/memfile/dump_gold.il | 17 +++++++++++ tests/memfile/read_dir.sh | 49 ++++++++++++++++++++++++++++++ tests/memfile/run-test.sh | 51 ++----------------------------- 7 files changed, 152 insertions(+), 49 deletions(-) create mode 100644 tests/memfile/dump.ys create mode 100644 tests/memfile/dump_gate.v create mode 100644 tests/memfile/dump_gold.il create mode 100755 tests/memfile/read_dir.sh diff --git a/Makefile b/Makefile index 6f2797646..349d76c4b 100644 --- a/Makefile +++ b/Makefile @@ -914,6 +914,7 @@ MK_TEST_DIRS += tests/verific endif endif MK_TEST_DIRS += tests/verilog +MK_TEST_DIRS += tests/memfile # Tests that don't generate .mk SH_TEST_DIRS = @@ -935,7 +936,6 @@ SH_TEST_DIRS += tests/proc SH_TEST_DIRS += tests/blif SH_TEST_DIRS += tests/arch SH_TEST_DIRS += tests/rpc -SH_TEST_DIRS += tests/memfile SH_TEST_DIRS += tests/fmt SH_TEST_DIRS += tests/cxxrtl SH_TEST_DIRS += tests/liberty diff --git a/tests/memfile/.gitignore b/tests/memfile/.gitignore index 61b0d4264..bf81114e6 100644 --- a/tests/memfile/.gitignore +++ b/tests/memfile/.gitignore @@ -1 +1,2 @@ temp* +*.mem diff --git a/tests/memfile/dump.ys b/tests/memfile/dump.ys new file mode 100644 index 000000000..330e12a03 --- /dev/null +++ b/tests/memfile/dump.ys @@ -0,0 +1,62 @@ +# create sub dir +!mkdir -p temp +# remove output files +!rm -f gold.m.mem +!rm -f temp/gold.m.mem + +# load test design +read_rtlil dump_gold.il +design -save gold + +# dump_meminit sets INIT_FILE and unsets INIT +select -assert-any t:$__MEMORY r:INIT %i +select -assert-none t:$__MEMORY r:INIT_FILE %i +dump_meminit +select -assert-none t:$__MEMORY r:INIT %i +select -assert-any t:$__MEMORY r:INIT_FILE %i +# memory written to file +!test -f gold.m.mem +# file name in INIT_FILE +select -assert-any t:$__MEMORY r:INIT_FILE=gold.m.mem %i + +design -load gold +dump_meminit -prefix temp/ +# memory written to sub directory +!test -f temp/gold.m.mem +# sub directory in INIT_FILE +select -assert-any t:$__MEMORY r:INIT_FILE=temp/gold.m.mem %i + +# dump_gate.v calls $readmemh with memory output from dump_gold.il +design -reset +read_verilog dump_gate.v +proc +design -save gate_proc + +# infer memory for gate +memory -nomap +select -assert-any t:$mem_v2 +design -save gate_memory + +# memory read from output of dump_meminit has the same value as the RTLIL +select -assert-any t:$mem_v2 r:INIT=32'hdeadbeef %i + +design -load gold +select -assert-any t:$__MEMORY r:INIT=32'hdeadbeef %i + +# dump_meminit does nothing if there are no cells with INIT +design -load gate_proc +select -assert-none r:INIT +dump_meminit +select -assert-none r:INIT_FILE + +# change cell type since INIT_FILE param is invalid on $mem_v2 +design -load gate_memory +chtype -set $__MEMORY gate/m + +# dump_meminit on parsed memory +select -assert-none r:INIT_FILE +dump_meminit +select -assert-any r:INIT_FILE=gate.m.mem + +# gold and gate dumps are identical +!diff -s gate.m.mem gold.m.mem diff --git a/tests/memfile/dump_gate.v b/tests/memfile/dump_gate.v new file mode 100644 index 000000000..4134a569a --- /dev/null +++ b/tests/memfile/dump_gate.v @@ -0,0 +1,19 @@ +module gate ( + input clk, wen, + input [2:0] addr, + input [3:0] wdata, + output [3:0] rdata +); + +reg [3:0] m [7:0]; +initial + $readmemh("gold.m.mem", m); + +always @(posedge clk) begin + if (wen) + m[addr] <= wdata; + else + rdata <= m[addr]; +end + +endmodule \ No newline at end of file diff --git a/tests/memfile/dump_gold.il b/tests/memfile/dump_gold.il new file mode 100644 index 000000000..53f361f15 --- /dev/null +++ b/tests/memfile/dump_gold.il @@ -0,0 +1,17 @@ +module \gold + wire input 1 \clk + wire input 1 \wen + wire input 3 \addr + wire input 4 \wdata + wire input 4 \rdata + cell $__MEMORY \m + parameter \WIDTH 4 + parameter \ABITS 3 + parameter \INIT 32'11011110101011011011111011101111 + connect \PORT_A_CLK \clk + connect \PORT_A_ADDR \addr + connect \PORT_A_WR_DATA \wdata + connect \PORT_A_WR_EN \wen + connect \PORT_A_RD_DATA \rdata + end +end diff --git a/tests/memfile/read_dir.sh b/tests/memfile/read_dir.sh new file mode 100755 index 000000000..db0ec54ee --- /dev/null +++ b/tests/memfile/read_dir.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash + +set -e + +mkdir -p temp +cp content1.dat temp/content2.dat + +cd .. + +echo "Running from the parent directory with content1.dat" +../yosys -qp "read_verilog -defer memfile/memory.v; chparam -set MEMFILE \"content1.dat\" memory" +echo "Running from the parent directory with temp/content2.dat" +../yosys -qp "read_verilog -defer memfile/memory.v; chparam -set MEMFILE \"temp/content2.dat\" memory" +echo "Running from the parent directory with memfile/temp/content2.dat" +../yosys -qp "read_verilog -defer memfile/memory.v; chparam -set MEMFILE \"memfile/temp/content2.dat\" memory" + +cd memfile + +echo "Running from the same directory with content1.dat" +../../yosys -qp "read_verilog -defer memory.v; chparam -set MEMFILE \"content1.dat\" memory" +echo "Running from the same directory with temp/content2.dat" +../../yosys -qp "read_verilog -defer memory.v; chparam -set MEMFILE \"temp/content2.dat\" memory" + +cd temp + +echo "Running from a child directory with content1.dat" +../../../yosys -qp "read_verilog -defer ../memory.v; chparam -set MEMFILE \"content1.dat\" memory" +echo "Running from a child directory with temp/content2.dat" +../../../yosys -qp "read_verilog -defer ../memory.v; chparam -set MEMFILE \"temp/content2.dat\" memory" +echo "Running from a child directory with content2.dat" +../../../yosys -qp "read_verilog -defer ../memory.v; chparam -set MEMFILE \"temp/content2.dat\" memory" + +cd .. + +echo "Checking a failure when zero length filename is provided" +if ../../yosys -qp "read_verilog memory.v"; then + echo "The execution should fail but it didn't happen, which is WRONG." + exit 1 +else + echo "Execution failed, which is OK." +fi + +echo "Checking a failure when not existing filename is provided" +if ../../yosys -qp "read_verilog -defer memory.v; chparam -set MEMFILE \"content3.dat\" memory"; then + echo "The execution should fail but it didn't happen, which is WRONG." + exit 1 +else + echo "Execution failed, which is OK." +fi diff --git a/tests/memfile/run-test.sh b/tests/memfile/run-test.sh index db0ec54ee..c5567825f 100755 --- a/tests/memfile/run-test.sh +++ b/tests/memfile/run-test.sh @@ -1,49 +1,4 @@ #!/usr/bin/env bash - -set -e - -mkdir -p temp -cp content1.dat temp/content2.dat - -cd .. - -echo "Running from the parent directory with content1.dat" -../yosys -qp "read_verilog -defer memfile/memory.v; chparam -set MEMFILE \"content1.dat\" memory" -echo "Running from the parent directory with temp/content2.dat" -../yosys -qp "read_verilog -defer memfile/memory.v; chparam -set MEMFILE \"temp/content2.dat\" memory" -echo "Running from the parent directory with memfile/temp/content2.dat" -../yosys -qp "read_verilog -defer memfile/memory.v; chparam -set MEMFILE \"memfile/temp/content2.dat\" memory" - -cd memfile - -echo "Running from the same directory with content1.dat" -../../yosys -qp "read_verilog -defer memory.v; chparam -set MEMFILE \"content1.dat\" memory" -echo "Running from the same directory with temp/content2.dat" -../../yosys -qp "read_verilog -defer memory.v; chparam -set MEMFILE \"temp/content2.dat\" memory" - -cd temp - -echo "Running from a child directory with content1.dat" -../../../yosys -qp "read_verilog -defer ../memory.v; chparam -set MEMFILE \"content1.dat\" memory" -echo "Running from a child directory with temp/content2.dat" -../../../yosys -qp "read_verilog -defer ../memory.v; chparam -set MEMFILE \"temp/content2.dat\" memory" -echo "Running from a child directory with content2.dat" -../../../yosys -qp "read_verilog -defer ../memory.v; chparam -set MEMFILE \"temp/content2.dat\" memory" - -cd .. - -echo "Checking a failure when zero length filename is provided" -if ../../yosys -qp "read_verilog memory.v"; then - echo "The execution should fail but it didn't happen, which is WRONG." - exit 1 -else - echo "Execution failed, which is OK." -fi - -echo "Checking a failure when not existing filename is provided" -if ../../yosys -qp "read_verilog -defer memory.v; chparam -set MEMFILE \"content3.dat\" memory"; then - echo "The execution should fail but it didn't happen, which is WRONG." - exit 1 -else - echo "Execution failed, which is OK." -fi +set -eu +source ../gen-tests-makefile.sh +generate_mk --bash --yosys-scripts \ No newline at end of file