-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
307 lines (268 loc) · 10.9 KB
/
Makefile
File metadata and controls
307 lines (268 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
.PHONY: all
all: simv
SIMV_SRCS = \
risc-v.srcs/sources_1/new/tb_riscv_cpu.sv \
risc-v.srcs/sources_1/new/cpu.sv \
risc-v.srcs/sources_1/new/clk_wiz_0_stub.sv
SIMV_DEPS = $(SIMV_SRCS) risc-v.srcs/sources_1/new/axi_uartlite_simple.sv
simv: $(SIMV_DEPS)
iverilog -g2012 -Wall -DIVERILOG -I risc-v.srcs/sources_1/new -o $@ $(SIMV_SRCS)
# Build program/main_prog.hex from program/source/main.S with comments
.PHONY: mainhex
mainhex: program/main_prog.hex
program/hex/%_prog.hex: program/source/%.S scripts/asm_to_hex.py
python3 scripts/asm_to_hex.py --input $< --output $@
# -------------------------------------------------------------------
# Build simple "tohost" ELFs from program/source/*_elf.S for Spike-compare flow
# -------------------------------------------------------------------
# Keep disassembly dumps (GNU make may otherwise treat them as intermediate and delete them)
.PRECIOUS: build/asm-elf/%.dump
build/asm-elf/%.elf: program/source/%_elf.S
@mkdir -p build/asm-elf
/opt/riscv/bin/riscv64-unknown-elf-gcc \
-march=rv64ia_zicsr_zifencei -mabi=lp64 \
-nostdlib -nostartfiles \
-Wl,--no-relax -Wl,-e,_start -Wl,-Tprogram/riscv_test.ld \
-o $@ $<
build/asm-elf/%.dump: build/asm-elf/%.elf
/opt/riscv/bin/riscv64-unknown-elf-objdump -d -s -j .text -j .data -M numeric $< > $@
# Generic rules for self-tests (matches hazard-elf, load-elf, etc.)
%-elf: build/asm-elf/%.elf build/asm-elf/%.dump
@:
%-spike: %-elf
$(MAKE) riscv-test ELF=build/asm-elf/$*.elf
%-xsim: %-elf
$(MAKE) xsim-test ELF=build/asm-elf/$*.elf
# Auto-discover all *_elf.S tests
ASM_ELF_SRCS := $(wildcard program/source/*_elf.S)
ASM_ELF_TARGETS := $(patsubst program/source/%_elf.S, %-elf, $(ASM_ELF_SRCS))
.PHONY: spike-selftests
spike-selftests: verilator $(ASM_ELF_TARGETS)
python3 scripts/run_riscv_tests_suite.py \
--dir "$(PWD)/build/asm-elf" \
--pattern "*.elf" \
--outdir "$(PWD)/build/spike-selftests" \
--isa "rv64ia" \
--spike-max-instructions "$${SPIKE_MAX_INSN:-200000}" \
--max-cycles "$${MAX_CYCLES:-200000}" \
$$( [ "$${FAIL_FAST:-0}" = "1" ] && echo --fail-fast ) \
$$( [ -n "$${LIMIT:-}" ] && echo --limit "$${LIMIT}" )
.PHONY: xsim-selftests
xsim-selftests: $(ASM_ELF_TARGETS)
python3 scripts/run_xsim_tests_suite.py \
--dir "$(PWD)/build/asm-elf" \
--pattern "*.elf" \
--outdir "$(PWD)/build/xsim-selftests" \
--isa "rv64ia" \
--vivado-path "$(VIVADO_PATH)" \
--spike-max-instructions "$${SPIKE_MAX_INSN:-200000}" \
--max-cycles "$${MAX_CYCLES:-200000}" \
$$( [ "$${FAIL_FAST:-0}" = "1" ] && echo --fail-fast ) \
$$( [ -n "$${LIMIT:-}" ] && echo --limit "$${LIMIT}" )
test: all
make build-hex-all
make test-all
.PHONY: bootrom FORCE
FORCE:
bootrom: rtl/bootrom.hex
rtl/bootrom.hex: program/source/uart-hello.S FORCE
/opt/riscv/bin/riscv64-unknown-elf-gcc -march=rv64i_zicsr_zifencei -mabi=lp64 -nostdlib -nostartfiles -Wl,-Ttext=0 -o uart-hello.elf $<
/opt/riscv/bin/riscv64-unknown-elf-objcopy -O binary uart-hello.elf uart-hello.bin
python3 scripts/to_hex.py uart-hello.bin > $@
cp $@ risc-v.sim/sim_1/behav/xsim/
clean:
rm -f simv
rm -f program/hex/*_prog.hex
rm -f rtl/bootrom.hex risc-v.sim/sim_1/behav/xsim/bootrom.hex
rm -rf build
rm -rf obj_dir
# -------------------------------------------------------------------
# Verilator (Spike-compare) flow for riscv-tests-style ELFs
# -------------------------------------------------------------------
.PHONY: verilator
verilator:
verilator --binary --timing -Wall -Wno-fatal -DIVERILOG -I$(PWD)/rtl \
--top-module tb_verilator $(PWD)/sim/tb_verilator.sv -o simv_verilator
.PHONY: riscv-test
# Usage:
# make riscv-test ELF=rv32ui-p-add
riscv-test: verilator
@test -n "$(ELF)" || (echo "Usage: make riscv-test ELF=<path-to-elf>"; exit 2)
@elf_path="$(ELF)"; case "$$elf_path" in /*) ;; *) elf_path="$(PWD)/$$elf_path";; esac; \
outdir="$(PWD)/build/riscv-tests"; mkdir -p "$$outdir"; \
sections=$$(/opt/riscv/bin/riscv64-unknown-elf-objdump -h "$$elf_path" | awk '/^[[:space:]]*[0-9]+/ {print $$2}' | grep -E '^\.(text(\.init)?|data)$$'); \
dump_cmd="/opt/riscv/bin/riscv64-unknown-elf-objdump -d -s -M numeric"; \
for s in $$sections; do dump_cmd="$$dump_cmd -j $$s"; done; \
$$dump_cmd "$$elf_path" > "$$outdir/$$(basename "$$elf_path").dump" 2>&1 || \
/opt/riscv/bin/riscv64-unknown-elf-objdump -d -s -M numeric "$$elf_path" > "$$outdir/$$(basename "$$elf_path").dump"; \
python3 scripts/run_riscv_test_compare_spike.py --elf "$$elf_path" --outdir "$$outdir"
.PHONY: riscv-tests
# Usage:
# make riscv-tests
# make riscv-tests PATTERN=rv32ui-p-* DIR=/opt/riscv/target/share/riscv-tests/isa
# make riscv-tests LIMIT=10 FAIL_FAST=1
riscv-tests: verilator
python3 scripts/run_riscv_tests_suite.py \
--dir "$${DIR:-/opt/riscv/target/share/riscv-tests/isa}" \
--pattern "$${PATTERN:-rv32ui-p-*}" \
--outdir "$(PWD)/build/riscv-tests" \
--isa "$${ISA:-rv64ia}" \
--spike-max-instructions "$${SPIKE_MAX_INSN:-200000}" \
--max-cycles "$${MAX_CYCLES:-200000}" \
$$( [ "$${FAIL_FAST:-0}" = "1" ] && echo --fail-fast ) \
$$( [ -n "$${LIMIT:-}" ] && echo --limit "$${LIMIT}" )
# Parametric single test run: make run TEST=<name>
# expects program/<name>_prog.hex and optional program/<name>.exp
# will build the program if it doesn't exist
run:
make all
make program/hex/$(TEST)_prog.hex
vvp ./simv +HEX=program/hex/$(TEST)_prog.hex +EXP=program/exp/$(TEST).exp +CASE=$(TEST)
# Concise single test run: make run-quiet TEST=<name>
# Prints one line: "RESULT <name> PASS" or "RESULT <name> FAIL <n>"
.PHONY: run-quiet
run-quiet:
make -s all
make -s program/hex/$(TEST)_prog.hex
@out=$$(vvp ./simv +HEX=program/hex/$(TEST)_prog.hex +EXP=program/exp/$(TEST).exp +CASE=$(TEST) +QUIET +SUMMARY | tail -n 2); \
echo "$$out" | grep "RESULT"; \
echo "$$out" | grep -q "RESULT $(TEST) PASS"
.PHONY: run-uart
run-uart:
make -s all
make -s program/hex/$(TEST)_prog.hex
vvp ./simv +HEX=program/hex/$(TEST)_prog.hex +EXP=program/exp/$(TEST).exp +CASE=$(TEST) +QUIET +SUMMARY
# Run all tests and print per-test PASS/FAIL summary
.PHONY: test-summary
test-summary: all build-hex-all
@pass=0; fail=0; without_exp=0; \
for f in program/hex/*_prog.hex; do \
n=$${f##*/}; n=$${n%_prog.hex}; \
res=$$(make -s run-quiet TEST=$$n || true); \
echo "$$res"; \
if echo "$$res" | grep -q "PASS"; then \
pass=$$((pass+1)); \
elif echo "$$res" | grep -q "without exp file"; then \
without_exp=$$((without_exp+1)); \
else \
fail=$$((fail+1)); \
fi; \
done; \
echo ""; \
echo "Summary: PASS=$$pass FAIL=$$fail WITHOUT_EXP=$$without_exp TOTAL=$$((pass+fail+without_exp))"; \
test $$fail -eq 0
# Run all tests that have *_prog.hex in program/ and matching .exp files if present
test-all:
@set -e; for f in program/hex/*_prog.hex; do \
n=$${f##*/}; n=$${n%_prog.hex}; \
echo "== $$n =="; \
make run TEST=$$n; \
done
build-hex-all:
@set -e; for f in program/source/*.S; do \
n=$${f##*/}; n=$${n%.S}; \
if [ -n "$(V)" ]; then \
echo "== $$n =="; \
$(MAKE) program/hex/$${n}_prog.hex; \
else \
$(MAKE) -s program/hex/$${n}_prog.hex >/dev/null; \
fi; \
done
# -------------------------------------------------------------------
# Vivado XSim Simulation Flow
# -------------------------------------------------------------------
# Vivado installation path
VIVADO_PATH := $(HOME)/tools/Xilinx/2025.2/Vivado
VIVADO_BIN := $(VIVADO_PATH)/bin
XSIM_SCRIPT := risc-v.sim/sim_1/behav/xsim/xsim/tb_design_1.sh
# Run XSim simulation using exported script
# Usage: make xsim
# Note: Run 'make xsim-export' first if simulation scripts don't exist
.PHONY: xsim
xsim:
@if [ ! -f "$(XSIM_SCRIPT)" ]; then \
echo "Simulation scripts not found. Running xsim-export first..."; \
$(MAKE) xsim-export; \
fi
@echo "=== Running Vivado XSim Simulation ==="
cd $$(dirname $(XSIM_SCRIPT)) && \
export PATH="$(VIVADO_BIN):$$PATH" && \
bash tb_design_1.sh
# Run only compile step
.PHONY: xsim-compile
xsim-compile:
@echo "=== Compiling design for XSim ==="
cd $$(dirname $(XSIM_SCRIPT)) && \
export PATH="$(VIVADO_BIN):$$PATH" && \
bash tb_design_1.sh -step compile
# Run compile + elaborate
.PHONY: xsim-elaborate
xsim-elaborate:
@echo "=== Elaborating design for XSim ==="
cd $$(dirname $(XSIM_SCRIPT)) && \
export PATH="$(VIVADO_BIN):$$PATH" && \
bash tb_design_1.sh -step elaborate
# Run simulation only (requires prior compile + elaborate)
.PHONY: xsim-run
xsim-run:
@echo "=== Running XSim simulation ==="
cd $$(dirname $(XSIM_SCRIPT)) && \
export PATH="$(VIVADO_BIN):$$PATH" && \
bash tb_design_1.sh -step simulate
# Generate simulation scripts from Vivado project
# Run this first in CI, or when Block Design / IP changes
# This generates: tb_design_1.sh and all required support files
.PHONY: xsim-export
xsim-export:
@echo "=== Generating IP outputs and exporting simulation scripts ==="
$(VIVADO_BIN)/vivado -mode batch -nojournal -nolog -source scripts/export_sim.tcl
# Full Vivado batch simulation via TCL (regenerates all scripts)
# Usage: make vivado-sim
# Usage: make vivado-sim SIM_TIME=5000ns
SIM_TIME ?= 1000ns
.PHONY: vivado-sim
vivado-sim:
@echo "=== Running Vivado Simulation (batch mode) ==="
$(VIVADO_BIN)/vivado -mode batch -nojournal -nolog -source scripts/run_sim.tcl -tclargs $(SIM_TIME)
# -------------------------------------------------------------------
# XSim + Spike Comparison Flow (similar to Verilator flow but uses BRAM)
# -------------------------------------------------------------------
# Single test: make xsim-test ELF=/opt/riscv/target/share/riscv-tests/isa/rv32ui-p-add
.PHONY: xsim-test
xsim-test:
@if [ -n "$(ELF)" ]; then \
elf_path="$(ELF)"; case "$$elf_path" in /*) ;; *) elf_path="$(PWD)/$$elf_path";; esac; \
python3 scripts/run_xsim_test_compare_spike.py \
--elf "$$elf_path" \
--outdir "$(PWD)/build/xsim-tests" \
--isa "$${ISA:-rv64ia}" \
--vivado-path "$(VIVADO_PATH)" \
--spike-max-instructions "$${SPIKE_MAX_INSN:-200000}" \
--max-cycles "$${MAX_CYCLES:-200000}"; \
else \
$(MAKE) xsim-tests; \
fi
.PHONY: xsim-tests
# Usage:
# make xsim-tests
# make xsim-tests PATTERN=rv32ui-p-* DIR=/opt/riscv/target/share/riscv-tests/isa
# make xsim-tests LIMIT=10 FAIL_FAST=1
xsim-tests:
python3 scripts/run_xsim_tests_suite.py \
--dir "$${DIR:-/opt/riscv/target/share/riscv-tests/isa}" \
--pattern "$${PATTERN:-rv32ui-p-*}" \
--outdir "$(PWD)/build/xsim-tests" \
--isa "$${ISA:-rv64ia}" \
--vivado-path "$(VIVADO_PATH)" \
--spike-max-instructions "$${SPIKE_MAX_INSN:-200000}" \
--max-cycles "$${MAX_CYCLES:-200000}" \
$$( [ "$${FAIL_FAST:-0}" = "1" ] && echo --fail-fast ) \
$$( [ -n "$${LIMIT:-}" ] && echo --limit "$${LIMIT}" )
# Check if BRAM is correctly inferred (synthesis check)
.PHONY: check-bram
check-bram:
@echo "=== Checking BRAM Inference ==="
$(VIVADO_BIN)/vivado -mode batch -nojournal -nolog -source scripts/check_bram_inference.tcl
# Alias for xsim-test (kept for backward compatibility)
.PHONY: xsim-test-quick
xsim-test-quick: xsim-test