Makefile 12 KB

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