bafbb8220c0191179ff7c11f840492d64820a835
[microwatt.git] / Makefile
1 GHDL ?= ghdl
2 GHDLFLAGS=--std=08 -frelaxed
3 CFLAGS=-O3 -Wall
4
5 GHDLSYNTH ?= ghdl.so
6 YOSYS ?= yosys
7 NEXTPNR ?= nextpnr-ecp5
8 ECPPACK ?= ecppack
9 OPENOCD ?= openocd
10
11 # We need a version of GHDL built with either the LLVM or gcc backend.
12 # Fedora provides this, but other distros may not. Another option is to use
13 # the Docker image.
14 DOCKER ?= 0
15 PODMAN ?= 0
16
17 ifeq ($(DOCKER), 1)
18 DOCKERBIN=docker
19 USE_DOCKER=1
20 endif
21
22 ifeq ($(PODMAN), 1)
23 DOCKERBIN=podman
24 USE_DOCKER=1
25 endif
26
27 ifeq ($(USE_DOCKER), 1)
28 PWD = $(shell pwd)
29 DOCKERARGS = run --rm -v $(PWD):/src:z -w /src
30 GHDL = $(DOCKERBIN) $(DOCKERARGS) ghdl/ghdl:buster-llvm-7 ghdl
31 CC = $(DOCKERBIN) $(DOCKERARGS) ghdl/ghdl:buster-llvm-7 gcc
32 GHDLSYNTH = ghdl
33 YOSYS = $(DOCKERBIN) $(DOCKERARGS) ghdl/synth:beta yosys
34 NEXTPNR = $(DOCKERBIN) $(DOCKERARGS) ghdl/synth:nextpnr-ecp5 nextpnr-ecp5
35 ECPPACK = $(DOCKERBIN) $(DOCKERARGS) ghdl/synth:trellis ecppack
36 OPENOCD = $(DOCKERBIN) $(DOCKERARGS) --device /dev/bus/usb ghdl/synth:prog openocd
37 endif
38
39 all = core_tb icache_tb dcache_tb multiply_tb dmi_dtm_tb divider_tb \
40 rotator_tb countzero_tb wishbone_bram_tb soc_reset_tb
41
42 all: $(all)
43
44 core_files = decode_types.vhdl common.vhdl wishbone_types.vhdl fetch1.vhdl \
45 fetch2.vhdl utils.vhdl plru.vhdl cache_ram.vhdl icache.vhdl \
46 decode1.vhdl helpers.vhdl insn_helpers.vhdl gpr_hazard.vhdl \
47 cr_hazard.vhdl control.vhdl decode2.vhdl register_file.vhdl \
48 cr_file.vhdl crhelpers.vhdl ppc_fx_insns.vhdl rotator.vhdl \
49 logical.vhdl countzero.vhdl multiply.vhdl divider.vhdl execute1.vhdl \
50 loadstore1.vhdl mmu.vhdl dcache.vhdl writeback.vhdl core_debug.vhdl \
51 core.vhdl
52
53 soc_files = wishbone_arbiter.vhdl wishbone_bram_wrapper.vhdl sync_fifo.vhdl \
54 wishbone_debug_master.vhdl xics.vhdl syscon.vhdl soc.vhdl
55
56 soc_sim_files = sim_console.vhdl sim_uart.vhdl sim_bram_helpers.vhdl \
57 sim_bram.vhdl sim_jtag_socket.vhdl sim_jtag.vhdl dmi_dtm_xilinx.vhdl
58
59 unisim_lib = sim-unisim/unisim-obj08.cf
60 unisim_lib_files = sim-unisim/BSCANE2.vhdl sim-unisim/BUFG.vhdl \
61 sim-unisim/unisim_vcomponents.vhdl
62 $(unisim_lib): $(unisim_lib_files)
63 ghdl -i --std=08 --work=unisim --workdir=sim-unisim $^
64
65 soc_sim_c_files = sim_vhpi_c.c sim_bram_helpers_c.c sim_console_c.c \
66 sim_jtag_socket_c.c
67
68 soc_sim_obj_files=$(soc_sim_c_files:.c=.o)
69 comma := ,
70 soc_sim_link=$(patsubst %,-Wl$(comma)%,$(soc_sim_obj_files))
71
72 core_tbs = multiply_tb divider_tb rotator_tb countzero_tb
73 soc_tbs = core_tb icache_tb dcache_tb dmi_dtm_tb wishbone_bram_tb
74 soc_dram_tbs = dram_tb core_dram_tb
75
76 $(soc_tbs): %: $(core_files) $(soc_files) $(soc_sim_files) $(soc_sim_obj_files) $(unisim_lib) %.vhdl
77 $(GHDL) -c $(GHDLFLAGS) -Psim-unisim $(soc_sim_link) $(core_files) $(soc_files) $(soc_sim_files) $@.vhdl -e $@
78
79 $(core_tbs): %: $(core_files) glibc_random.vhdl glibc_random_helpers.vhdl %.vhdl
80 $(GHDL) -c $(GHDLFLAGS) $(core_files) glibc_random.vhdl glibc_random_helpers.vhdl $@.vhdl -e $@
81
82 soc_reset_tb: fpga/soc_reset_tb.vhdl fpga/soc_reset.vhdl
83 $(GHDL) -c $(GHDLFLAGS) fpga/soc_reset_tb.vhdl fpga/soc_reset.vhdl -e $@
84
85 # LiteDRAM sim
86 VERILATOR_ROOT=$(shell verilator -getenv VERILATOR_ROOT 2>/dev/null)
87 ifeq (, $(VERILATOR_ROOT))
88 $(soc_dram_tbs):
89 $(error "Verilator is required to make this target !")
90 else
91
92 VERILATOR_CFLAGS=-O3
93 VERILATOR_FLAGS=-O3
94 verilated_dram: litedram/generated/sim/litedram_core.v
95 verilator $(VERILATOR_FLAGS) -CFLAGS $(VERILATOR_CFLAGS) -Wno-fatal --cc $< --trace
96 make -C obj_dir -f ../litedram/extras/sim_dram_verilate.mk VERILATOR_ROOT=$(VERILATOR_ROOT)
97
98 SIM_DRAM_CFLAGS = -I. -Iobj_dir -Ilitedram/generated/sim -I$(VERILATOR_ROOT)/include -I$(VERILATOR_ROOT)/include/vltstd
99 SIM_DRAM_CFLAGS += -DVM_COVERAGE=0 -DVM_SC=0 -DVM_TRACE=1 -DVL_PRINTF=printf -faligned-new
100 sim_litedram_c.o: litedram/extras/sim_litedram_c.cpp verilated_dram
101 $(CC) $(CPPFLAGS) $(SIM_DRAM_CFLAGS) $(CFLAGS) -c $< -o $@
102
103 soc_dram_files = $(soc_files) litedram/extras/litedram-wrapper-l2.vhdl litedram/generated/sim/litedram-initmem.vhdl
104 soc_dram_sim_files = $(soc_sim_files) litedram/extras/sim_litedram.vhdl
105 soc_dram_sim_obj_files = $(soc_sim_obj_files) sim_litedram_c.o
106 dram_link_files=-Wl,obj_dir/Vlitedram_core__ALL.a -Wl,obj_dir/verilated.o -Wl,obj_dir/verilated_vcd_c.o -Wl,-lstdc++
107 soc_dram_sim_link=$(patsubst %,-Wl$(comma)%,$(soc_dram_sim_obj_files)) $(dram_link_files)
108
109 $(soc_dram_tbs): %: $(core_files) $(soc_dram_files) $(soc_dram_sim_files) $(soc_dram_sim_obj_files) $(unisim_lib) %.vhdl
110 $(GHDL) -c $(GHDLFLAGS) -Psim-unisim $(soc_dram_sim_link) $(core_files) $(soc_dram_files) $(soc_dram_sim_files) $@.vhdl -e $@
111 endif
112
113 # Hello world
114 MEMORY_SIZE=8192
115 RAM_INIT_FILE=hello_world/hello_world.hex
116
117 # Micropython
118 #MEMORY_SIZE=393216
119 #RAM_INIT_FILE=micropython/firmware.hex
120
121 # OrangeCrab with ECP85
122 RESET_LOW=true
123 CLK_INPUT=50000000
124 CLK_FREQUENCY=50000000
125 LPF=constraints/orange-crab.lpf
126 PACKAGE=CSFBGA285
127 NEXTPNR_FLAGS=--um5g-85k --freq 50
128 OPENOCD_JTAG_CONFIG=openocd/olimex-arm-usb-tiny-h.cfg
129 OPENOCD_DEVICE_CONFIG=openocd/LFE5UM5G-85F.cfg
130
131 # ECP5-EVN
132 #RESET_LOW=true
133 #CLK_INPUT=12000000
134 #CLK_FREQUENCY=12000000
135 #LPF=constraints/ecp5-evn.lpf
136 #PACKAGE=CABGA381
137 #NEXTPNR_FLAGS=--um5g-85k --freq 12
138 #OPENOCD_JTAG_CONFIG=openocd/ecp5-evn.cfg
139 #OPENOCD_DEVICE_CONFIG=openocd/LFE5UM5G-85F.cfg
140
141 GHDL_IMAGE_GENERICS=-gMEMORY_SIZE=$(MEMORY_SIZE) -gRAM_INIT_FILE=$(RAM_INIT_FILE) \
142 -gRESET_LOW=$(RESET_LOW) -gCLK_INPUT=$(CLK_INPUT) -gCLK_FREQUENCY=$(CLK_FREQUENCY)
143
144 clkgen=fpga/clk_gen_bypass.vhd
145 toplevel=fpga/top-generic.vhdl
146 dmi_dtm=dmi_dtm_dummy.vhdl
147
148 fpga_files = $(core_files) $(soc_files) fpga/soc_reset.vhdl \
149 fpga/pp_fifo.vhd fpga/pp_soc_uart.vhd fpga/main_bram.vhdl
150
151 synth_files = $(core_files) $(soc_files) $(fpga_files) $(clkgen) $(toplevel) $(dmi_dtm)
152
153 microwatt.json: $(synth_files)
154 $(YOSYS) -m $(GHDLSYNTH) -p "ghdl --std=08 $(GHDL_IMAGE_GENERICS) $(GHDL_TARGET_GENERICS) $(synth_files) -e toplevel; synth_ecp5 -json $@"
155
156 microwatt.v: $(synth_files)
157 $(YOSYS) -m $(GHDLSYNTH) -p "ghdl --std=08 $(GHDL_IMAGE_GENERICS) $(GHDL_TARGET_GENERICS) $(synth_files) -e toplevel; write_verilog $@"
158
159 # Need to investigate why yosys is hitting verilator warnings, and eventually turn on -Wall
160 microwatt-verilator: microwatt.v verilator/microwatt-verilator.cpp verilator/uart-verilator.c
161 verilator -O3 -CFLAGS "-DCLK_FREQUENCY=$(CLK_FREQUENCY)" --assert --cc microwatt.v --exe verilator/microwatt-verilator.cpp verilator/uart-verilator.c -o $@ -Wno-CASEOVERLAP -Wno-UNOPTFLAT #--trace
162 make -C obj_dir -f Vmicrowatt.mk
163 @cp -f obj_dir/microwatt-verilator microwatt-verilator
164
165 microwatt_out.config: microwatt.json $(LPF)
166 $(NEXTPNR) --json $< --lpf $(LPF) --textcfg $@ $(NEXTPNR_FLAGS) --package $(PACKAGE)
167
168 microwatt.bit: microwatt_out.config
169 $(ECPPACK) --svf microwatt.svf $< $@
170
171 microwatt.svf: microwatt.bit
172
173 prog: microwatt.svf
174 $(OPENOCD) -f $(OPENOCD_JTAG_CONFIG) -f $(OPENOCD_DEVICE_CONFIG) -c "transport select jtag; init; svf $<; exit"
175
176 tests = $(sort $(patsubst tests/%.out,%,$(wildcard tests/*.out)))
177 tests_console = $(sort $(patsubst tests/%.console_out,%,$(wildcard tests/*.console_out)))
178
179 tests_console: $(tests_console)
180
181 check: $(tests) tests_console test_micropython test_micropython_long tests_unit
182
183 check_light: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 test_micropython test_micropython_long tests_console tests_unit
184
185 $(tests): core_tb
186 @./scripts/run_test.sh $@
187
188 $(tests_console): core_tb
189 @./scripts/run_test_console.sh $@
190
191 test_micropython: core_tb
192 @./scripts/test_micropython.py
193
194 test_micropython_long: core_tb
195 @./scripts/test_micropython_long.py
196
197 tests_core_tb = $(patsubst %_tb,%_tb_test,$(core_tbs))
198 tests_soc_tb = $(patsubst %_tb,%_tb_test,$(soc_tbs))
199
200 %_test: %
201 ./$< --assert-level=error > /dev/null
202
203 tests_core: $(tests_core_tb)
204
205 tests_soc: $(tests_soc_tb)
206
207 # FIXME SOC tests have bit rotted, so disable for now
208 #tests_unit: tests_core tests_soc
209 tests_unit: tests_core
210
211 TAGS:
212 find . -name '*.vhdl' | xargs ./scripts/vhdltags
213
214 .PHONY: TAGS
215
216 _clean:
217 rm -f *.o *.cf $(all)
218 rm -f fpga/*.o fpga/*.cf
219 rm -f sim-unisim/*.o sim-unisim/*.cf
220 rm -f litedram/extras/*.o
221 rm -f TAGS
222 rm -f scripts/mw_debug/*.o
223 rm -f scripts/mw_debug/mw_debug
224 rm -f microwatt.bin microwatt.json microwatt.svf microwatt_out.config
225 rm -f microwatt.v microwatt-verilator
226 rm -rf obj_dir/
227
228 clean: _clean
229 make -f scripts/mw_debug/Makefile clean
230 make -f hello_world/Makefile clean
231
232 distclean: _clean
233 rm -f *~ fpga/*~ lib/*~ console/*~ include/*~
234 rm -rf litedram/build
235 rm -f litedram/extras/*~
236 rm -f litedram/gen-src/*~
237 rm -f litedram/gen-src/sdram_init/*~
238 make -f scripts/mw_debug/Makefile distclean
239 make -f hello_world/Makefile distclean
240
241 .PHONY: all prog check check_light clean distclean
242 .PRECIOUS: microwatt.json microwatt_out.config microwatt.bit