Skip to content

Commit 49c44b7

Browse files
committed
Allows passing Verilog parameters at synthesis time
See more at https://maker.itnerd.space/define-verilog-parameters-at-synthesis-time-yosys/
1 parent a9bc676 commit 49c44b7

File tree

5 files changed

+45
-9
lines changed

5 files changed

+45
-9
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@
66
*.bin
77
*.ivl
88
*.pnr
9-
*.log
9+
*.log
10+
top_wrapper.v
11+
build.config

verilog/Design.mk

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,22 @@ else ifeq ($(MODULE), hrmcpu)
3737

3838
endif
3939

40-
IVERILOG_MACRO=
40+
M4_OPTIONS=
41+
AUXFILES=
42+
43+
ifdef PROGRAM
44+
M4_OPTIONS += -D_PROGRAM_=$(PROGRAM)
45+
DEPS := top_wrapper.v $(filter-out top_wrapper.v,$(DEPS))
46+
AUXFILES += $(PROGRAM)
47+
endif
4148

49+
ifdef ROMFILE
50+
M4_OPTIONS += -D_ROMFILE_=$(ROMFILE)
51+
DEPS := top_wrapper.v $(filter-out top_wrapper.v,$(DEPS))
52+
AUXFILES += $(ROMFILE)
53+
endif
54+
55+
IVERILOG_MACRO=
4256
ifdef PROGRAM
4357
IVERILOG_MACRO:=$(IVERILOG_MACRO) -DPROGRAM=\"$(PROGRAM)\"
4458
endif

verilog/Makefile

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ gtkwave: $(MODULE).v $(DEPS) $(MODULE)_tb.v $(MODULE)_tb.vcd
2929

3030
gtkwave $(MODULE)_tb.vcd $(MODULE)_tb.gtkw &
3131

32-
$(MODULE).bin: $(MODULE).pcf $(MODULE).v $(DEPS)
32+
$(MODULE).bin: $(MODULE).pcf $(MODULE).v $(DEPS) $(AUXFILES) build.config
3333

3434
yosys -p "synth_ice40 -top $(MODULE) -blif $(MODULE).blif $(YOSYSOPT)" \
35-
-l $(MODULE).log -q $(MODULE).v $(DEPS)
35+
-l $(MODULE).log -q $(DEPS) $(MODULE).v
3636

3737
arachne-pnr -d $(MEMORY) -p $(MODULE).pcf $(MODULE).blif -o $(MODULE).pnr
3838

@@ -53,11 +53,18 @@ assets/$(MODULE)_dot.svg: $(MODULE).v $(DEPS)
5353
[ -f $(MODULE)_dot.dot ] && rm $(MODULE)_dot.dot
5454

5555
upload: $(MODULE).bin
56-
5756
iceprog $(MODULE).bin
5857

58+
# We save AUXFILES names to build.config. Force a rebuild if they have changed
59+
build.config: $(AUXFILES) .force
60+
@echo '$(AUXFILES)' | cmp -s - $@ || echo '$(AUXFILES)' > $@
61+
62+
top_wrapper.v: top_wrapper.m4 build.config
63+
m4 $(M4_OPTIONS) top_wrapper.m4 > top_wrapper.v
64+
5965
clean:
6066

61-
rm -f *.bin *.pnr *.blif *.out *.vcd *~
67+
rm -f *.bin *.pnr *.blif *.out *.vcd *~ top_wrapper.v build.config
6268

63-
.PHONY: all clean json svg sim dot
69+
.PHONY: all clean json svg bin sim dot .force
70+

verilog/top.v

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
`ifndef __TOP_MODULE__
2+
`define __TOP_MODULE__
3+
14
`default_nettype none
2-
//`include "const.vh"
35

46
// Use of `define allows override from iverilog using -Dkey=value
57
`ifndef PROGRAM
@@ -23,6 +25,12 @@ module top (
2325

2426
localparam baudsDivider=24'd104;
2527

28+
initial begin
29+
// will dump parameters values in the yosys log
30+
$display("PARAM PROGRAM: %s",`PROGRAM);
31+
$display("PARAM ROMFILE: %s",`ROMFILE);
32+
end
33+
2634
wire sw1_d; // pulse when sw pressed
2735
wire sw1_u; // pulse when sw released
2836
wire sw1_s; // sw state
@@ -150,4 +158,5 @@ module out2txCtl (
150158
end
151159
end
152160

153-
endmodule
161+
endmodule
162+
`endif // __TOP_MODULE__

verilog/top_wrapper.m4

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
changequote([,])dnl
2+
`define PROGRAM "_PROGRAM_"
3+
`define ROMFILE "_ROMFILE_"
4+
`include "top.v"

0 commit comments

Comments
 (0)