Skip to content

Commit f7b3fa3

Browse files
set up generic testbench targets in Makefile
1 parent 8c7b56e commit f7b3fa3

File tree

8 files changed

+68
-21
lines changed

8 files changed

+68
-21
lines changed

.gitignore

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
*.vvp
2-
*.vcd
3-
4-
!test/fetch_tb.vcd
5-
!test/beq_tb.vcd
62

73
.devcontainer/

Makefile

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,45 @@ OUTPUT := out/top.vvp
33
IVERILOG := iverilog
44
VVP := vvp
55

6+
TB_SRC_PATTERN := test/%_tb.sv
7+
TB_OUT_PATTERN := out/%_tb.vvp
8+
9+
TB_SRCS := $(wildcard $(subst %,*,$(TB_SRC_PATTERN)))
10+
TB_VVPS := $(patsubst $(TB_SRC_PATTERN),$(TB_OUT_PATTERN),$(TB_SRCS))
11+
TB_UTILS := test/utils.svh
12+
13+
TB_VCD_BASE_PATH := test/vcd
14+
615
all: $(OUTPUT)
716

8-
$(OUTPUT): $(SRCS)
17+
$(OUTPUT):
918
$(IVERILOG) -g2012 -o $(OUTPUT) -c src/top.cf
1019

1120
run: $(OUTPUT)
1221
$(VVP) $(OUTPUT)
1322

14-
# tmp
15-
fetch_tb:
16-
$(IVERILOG) -g2012 -o $(OUTPUT) $(SRCS) -c src/top.cf test/fetch_tb.sv
23+
$(TB_OUT_PATTERN): $(TB_SRC_PATTERN) $(TB_UTILS)
24+
$(IVERILOG) -g2012 -o $@ -c src/top.cf $<
25+
26+
build_tb: $(TB_VVPS)
1727

18-
beq_tb:
19-
$(IVERILOG) -g2012 -o $(OUTPUT) $(SRCS) -c src/top.cf test/beq_tb.sv
28+
run_tb: $(TB_VVPS)
29+
@failed=0; \
30+
for tb in $(TB_VVPS); do \
31+
echo "Running $$tb..."; \
32+
if ! $(VVP) -N $$tb +VCD_PATH=$(TB_VCD_BASE_PATH); then \
33+
echo "\033[31mFAILED: $$tb\033[0m"; \
34+
failed=1; \
35+
else \
36+
echo "\033[32mPASSED: $$tb\033[0m"; \
37+
fi; \
38+
echo ""; \
39+
done; \
40+
if [ $$failed -eq 1 ]; then \
41+
echo "\033[31mSome testbenches failed!\033[0m"; \
42+
exit 1; \
43+
else \
44+
echo "\033[32mAll testbenches passed!\033[0m"; \
45+
fi
2046

21-
.PHONY: all run
47+
.PHONY: all run testbenches run-tests

test/beq_tb.sv

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
`include "src/utils.svh"
44
`include "src/types.svh"
55

6+
`include "test/utils.svh"
7+
68
module beq_tb;
79
reg clk;
810
reg reset;
@@ -110,8 +112,6 @@ module beq_tb;
110112
$finish;
111113
end
112114

113-
initial begin
114-
$dumpfile("test/beq_tb.vcd");
115-
$dumpvars(0, beq_tb);
116-
end
115+
`SETUP_VCD_DUMP(beq_tb)
116+
117117
endmodule

test/fetch_tb.sv

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
`include "src/utils.svh"
44
`include "src/types.svh"
55

6+
`include "test/utils.svh"
7+
68
module fetch_tb;
79

810
reg clk;
@@ -184,8 +186,6 @@ module fetch_tb;
184186
$finish;
185187
end
186188

187-
initial begin
188-
$dumpfile("test/fetch_tb.vcd");
189-
$dumpvars(0, fetch_tb);
190-
end
189+
`SETUP_VCD_DUMP(fetch_tb)
190+
191191
endmodule

test/utils.svh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
`ifndef TEST__UTILS_SVH
2+
`define TEST__UTILS_SVH
3+
4+
`define SETUP_VCD_DUMP(module_name) \
5+
initial begin \
6+
string vcd_path; \
7+
string vcd_filename; \
8+
\
9+
if ($test$plusargs("VCD_PATH")) begin \
10+
if ($value$plusargs("VCD_PATH=%s", vcd_path) == 0) begin \
11+
$display("VCD_PATH not set, not dumping VCD"); \
12+
end \
13+
\
14+
vcd_filename = $sformatf("%s/%s.vcd", vcd_path, `"module_name`"); \
15+
\
16+
$dumpfile(vcd_filename); \
17+
$dumpvars(0, module_name); \
18+
end \
19+
end
20+
21+
`endif

test/vcd/.gitkeep

Whitespace-only changes.

test/beq_tb.vcd renamed to test/vcd/beq_tb.vcd

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
$date
2-
Thu Jun 5 04:15:59 2025
2+
Sun Jun 15 20:29:55 2025
33
$end
44
$version
55
Icarus Verilog
@@ -125,6 +125,8 @@ $var reg 32 o writeData [31:0] $end
125125
$upscope $end
126126
$upscope $end
127127
$upscope $end
128+
$scope begin $unm_blk_47 $end
129+
$upscope $end
128130
$upscope $end
129131
$enddefinitions $end
130132
$comment Show the parameter values. $end

test/fetch_tb.vcd renamed to test/vcd/fetch_tb.vcd

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
$date
2-
Thu Jun 5 04:15:54 2025
2+
Sun Jun 15 20:29:55 2025
33
$end
44
$version
55
Icarus Verilog
@@ -35,6 +35,8 @@ $upscope $end
3535
$scope begin $ivl_for_loop0 $end
3636
$var integer 32 0 i [31:0] $end
3737
$upscope $end
38+
$scope begin $unm_blk_48 $end
39+
$upscope $end
3840
$upscope $end
3941
$enddefinitions $end
4042
$comment Show the parameter values. $end

0 commit comments

Comments
 (0)