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