|
| 1 | + |
| 2 | +RISCV_GNU_TOOLCHAIN_GIT_REVISION = 411d134 |
| 3 | +RISCV_GNU_TOOLCHAIN_INSTALL_PREFIX = /opt/riscv32 |
| 4 | + |
| 5 | +# Give the user some easy overrides for local configuration quirks. |
| 6 | +# If you change one of these and it breaks, then you get to keep both pieces. |
| 7 | +SHELL = bash |
| 8 | +PYTHON = python3 |
| 9 | +VERILATOR = verilator |
| 10 | +ICARUS_SUFFIX = |
| 11 | +IVERILOG = iverilog$(ICARUS_SUFFIX) |
| 12 | +VVP = vvp$(ICARUS_SUFFIX) |
| 13 | + |
| 14 | +TEST_OBJS = $(addsuffix .o,$(basename $(wildcard tests/*.S))) |
| 15 | +FIRMWARE_OBJS = firmware/start.o firmware/irq.o firmware/print.o firmware/hello.o firmware/sieve.o firmware/multest.o firmware/stats.o |
| 16 | +GCC_WARNS = -Werror -Wall -Wextra -Wshadow -Wundef -Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings |
| 17 | +GCC_WARNS += -Wredundant-decls -Wstrict-prototypes -Wmissing-prototypes -pedantic # -Wconversion |
| 18 | +TOOLCHAIN_PREFIX = $(RISCV_GNU_TOOLCHAIN_INSTALL_PREFIX)i/bin/riscv32-unknown-elf- |
| 19 | +COMPRESSED_ISA = C |
| 20 | + |
| 21 | +# Add things like "export http_proxy=... https_proxy=..." here |
| 22 | +GIT_ENV = true |
| 23 | + |
| 24 | +test_mod: testbench_mod.vvp firmware/firmware.hex |
| 25 | + $(VVP) -N $< |
| 26 | + |
| 27 | +test: testbench.vvp firmware/firmware.hex |
| 28 | + $(VVP) -N $< |
| 29 | + |
| 30 | +test_vcd: testbench.vvp firmware/firmware.hex |
| 31 | + $(VVP) -N $< +vcd +trace +noerror |
| 32 | + |
| 33 | +test_rvf: testbench_rvf.vvp firmware/firmware.hex |
| 34 | + $(VVP) -N $< +vcd +trace +noerror |
| 35 | + |
| 36 | +test_wb: testbench_wb.vvp firmware/firmware.hex |
| 37 | + $(VVP) -N $< |
| 38 | + |
| 39 | +test_wb_vcd: testbench_wb.vvp firmware/firmware.hex |
| 40 | + $(VVP) -N $< +vcd +trace +noerror |
| 41 | + |
| 42 | +test_ez: testbench_ez.vvp |
| 43 | + $(VVP) -N $< |
| 44 | + |
| 45 | +test_ez_vcd: testbench_ez.vvp |
| 46 | + $(VVP) -N $< +vcd |
| 47 | + |
| 48 | +test_sp: testbench_sp.vvp firmware/firmware.hex |
| 49 | + $(VVP) -N $< |
| 50 | + |
| 51 | +test_axi: testbench.vvp firmware/firmware.hex |
| 52 | + $(VVP) -N $< +axi_test |
| 53 | + |
| 54 | +test_synth: testbench_synth.vvp firmware/firmware.hex |
| 55 | + $(VVP) -N $< |
| 56 | + |
| 57 | +test_verilator: testbench_verilator firmware/firmware.hex |
| 58 | + ./testbench_verilator |
| 59 | + |
| 60 | +testbench.vvp: testbench.v picorv32.v |
| 61 | + $(IVERILOG) -o $@ $(subst C,-DCOMPRESSED_ISA,$(COMPRESSED_ISA)) $^ |
| 62 | + chmod -x $@ |
| 63 | + |
| 64 | +testbench_mod.vvp: testbench_mod.v axi4_mem_periph.v picorv32.v ROW_IDCT.v COL_IDCT.v |
| 65 | + $(IVERILOG) -o $@ $(subst C,-DCOMPRESSED_ISA,$(COMPRESSED_ISA)) $^ |
| 66 | + chmod -x $@ |
| 67 | + |
| 68 | +testbench_rvf.vvp: testbench.v picorv32.v rvfimon.v |
| 69 | + $(IVERILOG) -o $@ -D RISCV_FORMAL $(subst C,-DCOMPRESSED_ISA,$(COMPRESSED_ISA)) $^ |
| 70 | + chmod -x $@ |
| 71 | + |
| 72 | +testbench_wb.vvp: testbench_wb.v picorv32.v |
| 73 | + $(IVERILOG) -o $@ $(subst C,-DCOMPRESSED_ISA,$(COMPRESSED_ISA)) $^ |
| 74 | + chmod -x $@ |
| 75 | + |
| 76 | +testbench_ez.vvp: testbench_ez.v picorv32.v |
| 77 | + $(IVERILOG) -o $@ $(subst C,-DCOMPRESSED_ISA,$(COMPRESSED_ISA)) $^ |
| 78 | + chmod -x $@ |
| 79 | + |
| 80 | +testbench_sp.vvp: testbench.v picorv32.v |
| 81 | + $(IVERILOG) -o $@ $(subst C,-DCOMPRESSED_ISA,$(COMPRESSED_ISA)) -DSP_TEST $^ |
| 82 | + chmod -x $@ |
| 83 | + |
| 84 | +testbench_synth.vvp: testbench.v synth.v |
| 85 | + $(IVERILOG) -o $@ -DSYNTH_TEST $^ |
| 86 | + chmod -x $@ |
| 87 | + |
| 88 | +testbench_verilator: testbench_mod.v picorv32.v axi4_mem_periph.v testbench.cc |
| 89 | + $(VERILATOR) --cc --exe -Wno-lint -trace --top-module picorv32_wrapper testbench_mod.v picorv32.v axi4_mem_periph.v testbench.cc \ |
| 90 | + $(subst C,-DCOMPRESSED_ISA,$(COMPRESSED_ISA)) --Mdir testbench_verilator_dir |
| 91 | + $(MAKE) -C testbench_verilator_dir -f Vpicorv32_wrapper.mk |
| 92 | + cp testbench_verilator_dir/Vpicorv32_wrapper testbench_verilator |
| 93 | + |
| 94 | +check: check-yices |
| 95 | + |
| 96 | +check-%: check.smt2 |
| 97 | + yosys-smtbmc -s $(subst check-,,$@) -t 30 --dump-vcd check.vcd check.smt2 |
| 98 | + yosys-smtbmc -s $(subst check-,,$@) -t 25 --dump-vcd check.vcd -i check.smt2 |
| 99 | + |
| 100 | +check.smt2: picorv32.v |
| 101 | + yosys -v2 -p 'read_verilog -formal picorv32.v' \ |
| 102 | + -p 'prep -top picorv32 -nordff' \ |
| 103 | + -p 'assertpmux -noinit; opt -fast' \ |
| 104 | + -p 'write_smt2 -wires check.smt2' |
| 105 | + |
| 106 | +synth.v: picorv32.v scripts/yosys/synth_sim.ys |
| 107 | + yosys -qv3 -l synth.log scripts/yosys/synth_sim.ys |
| 108 | + |
| 109 | +# Changing below to use 512k RAM |
| 110 | +firmware/firmware.hex: firmware/firmware.bin firmware/makehex.py |
| 111 | + $(PYTHON) firmware/makehex.py $< 524288 > $@ |
| 112 | + |
| 113 | +firmware/firmware.bin: firmware/firmware.elf |
| 114 | + $(TOOLCHAIN_PREFIX)objcopy -O binary $< $@ |
| 115 | + chmod -x $@ |
| 116 | + |
| 117 | +firmware/firmware.elf: $(FIRMWARE_OBJS) $(TEST_OBJS) firmware/sections.lds firmware/nanojpeg.c |
| 118 | + $(TOOLCHAIN_PREFIX)gcc -Os -ffreestanding -nostdlib -o $@ \ |
| 119 | + -Wl,-Bstatic,-T,firmware/sections.lds,-Map,firmware/firmware.map,--strip-debug \ |
| 120 | + $(FIRMWARE_OBJS) $(TEST_OBJS) -lgcc |
| 121 | + chmod -x $@ |
| 122 | + |
| 123 | +firmware/start.o: firmware/start.S |
| 124 | + $(TOOLCHAIN_PREFIX)gcc -c -march=rv32im$(subst C,c,$(COMPRESSED_ISA)) -o $@ $< |
| 125 | + |
| 126 | +firmware/%.o: firmware/%.c |
| 127 | + $(TOOLCHAIN_PREFIX)gcc -c -march=rv32i$(subst C,c,$(COMPRESSED_ISA)) -Os --std=c99 $(GCC_WARNS) -ffreestanding -nostdlib -o $@ $< |
| 128 | + |
| 129 | +tests/%.o: tests/%.S tests/riscv_test.h tests/test_macros.h |
| 130 | + $(TOOLCHAIN_PREFIX)gcc -c -march=rv32im -o $@ -DTEST_FUNC_NAME=$(notdir $(basename $<)) \ |
| 131 | + -DTEST_FUNC_TXT='"$(notdir $(basename $<))"' -DTEST_FUNC_RET=$(notdir $(basename $<))_ret $< |
| 132 | + |
| 133 | +download-tools: |
| 134 | + sudo bash -c 'set -ex; mkdir -p /var/cache/distfiles; $(GIT_ENV); \ |
| 135 | + $(foreach REPO,riscv-gnu-toolchain riscv-binutils-gdb riscv-gcc riscv-glibc riscv-newlib, \ |
| 136 | + if ! test -d /var/cache/distfiles/$(REPO).git; then rm -rf /var/cache/distfiles/$(REPO).git.part; \ |
| 137 | + git clone --bare https://github.com/riscv/$(REPO) /var/cache/distfiles/$(REPO).git.part; \ |
| 138 | + mv /var/cache/distfiles/$(REPO).git.part /var/cache/distfiles/$(REPO).git; else \ |
| 139 | + (cd /var/cache/distfiles/$(REPO).git; git fetch https://github.com/riscv/$(REPO)); fi;)' |
| 140 | + |
| 141 | +define build_tools_template |
| 142 | +build-$(1)-tools: |
| 143 | + @read -p "This will remove all existing data from $(RISCV_GNU_TOOLCHAIN_INSTALL_PREFIX)$(subst riscv32,,$(1)). Type YES to continue: " reply && [[ "$$$$reply" == [Yy][Ee][Ss] || "$$$$reply" == [Yy] ]] |
| 144 | + sudo bash -c "set -ex; rm -rf $(RISCV_GNU_TOOLCHAIN_INSTALL_PREFIX)$(subst riscv32,,$(1)); mkdir -p $(RISCV_GNU_TOOLCHAIN_INSTALL_PREFIX)$(subst riscv32,,$(1)); chown $$$${USER}: $(RISCV_GNU_TOOLCHAIN_INSTALL_PREFIX)$(subst riscv32,,$(1))" |
| 145 | + +$(MAKE) build-$(1)-tools-bh |
| 146 | + |
| 147 | +build-$(1)-tools-bh: |
| 148 | + +set -ex; $(GIT_ENV); \ |
| 149 | + if [ -d /var/cache/distfiles/riscv-gnu-toolchain.git ]; then reference_riscv_gnu_toolchain="--reference /var/cache/distfiles/riscv-gnu-toolchain.git"; else reference_riscv_gnu_toolchain=""; fi; \ |
| 150 | + if [ -d /var/cache/distfiles/riscv-binutils-gdb.git ]; then reference_riscv_binutils_gdb="--reference /var/cache/distfiles/riscv-binutils-gdb.git"; else reference_riscv_binutils_gdb=""; fi; \ |
| 151 | + if [ -d /var/cache/distfiles/riscv-gcc.git ]; then reference_riscv_gcc="--reference /var/cache/distfiles/riscv-gcc.git"; else reference_riscv_gcc=""; fi; \ |
| 152 | + if [ -d /var/cache/distfiles/riscv-glibc.git ]; then reference_riscv_glibc="--reference /var/cache/distfiles/riscv-glibc.git"; else reference_riscv_glibc=""; fi; \ |
| 153 | + if [ -d /var/cache/distfiles/riscv-newlib.git ]; then reference_riscv_newlib="--reference /var/cache/distfiles/riscv-newlib.git"; else reference_riscv_newlib=""; fi; \ |
| 154 | + rm -rf riscv-gnu-toolchain-$(1); git clone $$$$reference_riscv_gnu_toolchain https://github.com/riscv/riscv-gnu-toolchain riscv-gnu-toolchain-$(1); \ |
| 155 | + cd riscv-gnu-toolchain-$(1); git checkout $(RISCV_GNU_TOOLCHAIN_GIT_REVISION); \ |
| 156 | + git submodule update --init $$$$reference_riscv_binutils_gdb riscv-binutils; \ |
| 157 | + git submodule update --init $$$$reference_riscv_binutils_gdb riscv-gdb; \ |
| 158 | + git submodule update --init $$$$reference_riscv_gcc riscv-gcc; \ |
| 159 | + git submodule update --init $$$$reference_riscv_glibc riscv-glibc; \ |
| 160 | + git submodule update --init $$$$reference_riscv_newlib riscv-newlib; \ |
| 161 | + mkdir build; cd build; ../configure --with-arch=$(2) --prefix=$(RISCV_GNU_TOOLCHAIN_INSTALL_PREFIX)$(subst riscv32,,$(1)); make |
| 162 | + |
| 163 | +.PHONY: build-$(1)-tools |
| 164 | +endef |
| 165 | + |
| 166 | +$(eval $(call build_tools_template,riscv32i,rv32i)) |
| 167 | +$(eval $(call build_tools_template,riscv32ic,rv32ic)) |
| 168 | +$(eval $(call build_tools_template,riscv32im,rv32im)) |
| 169 | +$(eval $(call build_tools_template,riscv32imc,rv32imc)) |
| 170 | + |
| 171 | +build-tools: |
| 172 | + @echo "This will remove all existing data from $(RISCV_GNU_TOOLCHAIN_INSTALL_PREFIX)i, $(RISCV_GNU_TOOLCHAIN_INSTALL_PREFIX)ic, $(RISCV_GNU_TOOLCHAIN_INSTALL_PREFIX)im, and $(RISCV_GNU_TOOLCHAIN_INSTALL_PREFIX)imc." |
| 173 | + @read -p "Type YES to continue: " reply && [[ "$$reply" == [Yy][Ee][Ss] || "$$reply" == [Yy] ]] |
| 174 | + sudo bash -c "set -ex; rm -rf $(RISCV_GNU_TOOLCHAIN_INSTALL_PREFIX){i,ic,im,imc}; mkdir -p $(RISCV_GNU_TOOLCHAIN_INSTALL_PREFIX){i,ic,im,imc}; chown $${USER}: $(RISCV_GNU_TOOLCHAIN_INSTALL_PREFIX){i,ic,im,imc}" |
| 175 | + +$(MAKE) build-riscv32i-tools-bh |
| 176 | + +$(MAKE) build-riscv32ic-tools-bh |
| 177 | + +$(MAKE) build-riscv32im-tools-bh |
| 178 | + +$(MAKE) build-riscv32imc-tools-bh |
| 179 | + |
| 180 | +toc: |
| 181 | + gawk '/^-+$$/ { y=tolower(x); gsub("[^a-z0-9]+", "-", y); gsub("-$$", "", y); printf("- [%s](#%s)\n", x, y); } { x=$$0; }' README.md |
| 182 | + |
| 183 | +clean: |
| 184 | + rm -rf riscv-gnu-toolchain-riscv32i riscv-gnu-toolchain-riscv32ic \ |
| 185 | + riscv-gnu-toolchain-riscv32im riscv-gnu-toolchain-riscv32imc |
| 186 | + rm -vrf $(FIRMWARE_OBJS) $(TEST_OBJS) check.smt2 check.vcd synth.v synth.log \ |
| 187 | + firmware/firmware.elf firmware/firmware.bin firmware/firmware.hex firmware/firmware.map \ |
| 188 | + testbench.vvp testbench_sp.vvp testbench_synth.vvp testbench_ez.vvp \ |
| 189 | + testbench_rvf.vvp testbench_wb.vvp testbench.vcd testbench.trace \ |
| 190 | + testbench_verilator testbench_verilator_dir |
| 191 | + |
| 192 | +.PHONY: test test_vcd test_sp test_axi test_wb test_wb_vcd test_ez test_ez_vcd test_synth download-tools build-tools toc clean |
0 commit comments