-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakefile
More file actions
152 lines (114 loc) · 4.92 KB
/
Makefile
File metadata and controls
152 lines (114 loc) · 4.92 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
# RV32I Processor Build System
# Usage:
# make - Build firmware hex
# make test-unit - Run all iverilog unit testbenches
# make test-system - Run full system testbench with firmware
# make test - Run all tests (unit + system)
# make lint - Verilator lint on all RTL
# make clean - Remove all build artifacts
SHELL := /bin/bash
# Toolchain: auto-detect prefix (Homebrew uses riscv64-elf-, others use riscv64-unknown-elf-)
TOOLCHAIN_PREFIX ?= $(shell which riscv64-unknown-elf-gcc >/dev/null 2>&1 && echo riscv64-unknown-elf- || echo riscv64-elf-)
PYTHON = python3
# RTL sources
RTL_SRCS = $(wildcard rtl/*.v)
RTL_NAMES = rv32i alu branchcomp controlunit regfile immgen loadstoreunit csr
# Firmware
TEST_OBJS = $(patsubst tests/%.S,build/tests/%.o,$(wildcard tests/*.S))
FIRMWARE_OBJS = build/firmware/start.o
# Build directory
BUILD = build
# Default target
all: firmware/firmware.hex
#############################################################################
# Firmware build
#############################################################################
firmware/firmware.hex: firmware/firmware.bin firmware/makehex.py
$(PYTHON) firmware/makehex.py $< 32768 > $@
firmware/firmware.bin: firmware/firmware.elf
$(TOOLCHAIN_PREFIX)objcopy -O binary $< $@
chmod -x $@
firmware/firmware.elf: $(FIRMWARE_OBJS) $(TEST_OBJS) firmware/sections.lds
$(TOOLCHAIN_PREFIX)gcc -Os -march=rv32i -mabi=ilp32 -ffreestanding -nostdlib -o $@ \
-Wl,-Bstatic,-T,firmware/sections.lds,-Map,firmware/firmware.map,--strip-debug \
$(FIRMWARE_OBJS) $(TEST_OBJS) -lgcc
chmod -x $@
build/firmware/start.o: firmware/start.S | $(BUILD)/firmware
$(TOOLCHAIN_PREFIX)gcc -c -march=rv32i -mabi=ilp32 -o $@ $<
build/tests/%.o: tests/%.S tests/riscv_test.h tests/test_macros.h | $(BUILD)/tests
$(TOOLCHAIN_PREFIX)gcc -c -march=rv32i -mabi=ilp32 -o $@ -DTEST_FUNC_NAME=$(notdir $(basename $<)) \
-DTEST_FUNC_TXT='"$(notdir $(basename $<))"' -DTEST_FUNC_RET=$(notdir $(basename $<))_ret $<
#############################################################################
# Unit testbenches (iverilog)
#############################################################################
UNIT_TBS = alu branchcomp controlunit regfile immgen loadstoreunit rv32i
# Build rules for unit testbench executables
$(BUILD)/alu: tb/alu_tb.v rtl/alu.v | $(BUILD)
iverilog -o $@ $^
$(BUILD)/branchcomp: tb/branchcomp_tb.v rtl/branchcomp.v | $(BUILD)
iverilog -o $@ $^
$(BUILD)/controlunit: tb/controlunit_tb.v rtl/controlunit.v | $(BUILD)
iverilog -o $@ $^
$(BUILD)/regfile: tb/regfile_tb.v rtl/regfile.v | $(BUILD)
iverilog -o $@ $^
$(BUILD)/immgen: tb/immgen_tb.v rtl/immgen.v | $(BUILD)
iverilog -o $@ $^
$(BUILD)/loadstoreunit: tb/loadstoreunit_tb.v rtl/loadstoreunit.v | $(BUILD)
iverilog -o $@ $^
$(BUILD)/rv32i: tb/rv32i_tb.v $(RTL_SRCS) | $(BUILD)
iverilog -o $@ $^
test-unit: $(addprefix $(BUILD)/,$(UNIT_TBS))
@pass=0; fail=0; \
for tb in $(UNIT_TBS); do \
echo "--- Running $$tb testbench ---"; \
output=$$($(BUILD)/$$tb 2>&1); \
echo "$$output"; \
if echo "$$output" | grep -q "0 errors"; then \
pass=$$((pass + 1)); \
else \
fail=$$((fail + 1)); \
fi; \
done; \
echo ""; \
echo "=============================="; \
echo "Unit tests: $$pass passed, $$fail failed"; \
echo "=============================="; \
if [ $$fail -ne 0 ]; then exit 1; fi
#############################################################################
# Full system testbench
#############################################################################
$(BUILD)/rv32i_hex: tb/testbench.v $(RTL_SRCS) | $(BUILD)
iverilog -o $@ $^
test-system: $(BUILD)/rv32i_hex firmware/firmware.hex
@echo "--- Running full system testbench ---"
$(BUILD)/rv32i_hex
#############################################################################
# Verilator + cocotb
#############################################################################
test-verilator:
$(MAKE) -C tb/cocotb
#############################################################################
# Lint
#############################################################################
lint:
verilator --lint-only -Wall --top-module rv32i $(RTL_SRCS)
#############################################################################
# Combined test target
#############################################################################
test: test-unit test-system
#############################################################################
# Clean
#############################################################################
clean:
rm -rf $(BUILD)
rm -f firmware/*.bin firmware/*.elf firmware/*.hex firmware/*.map
#############################################################################
# Directory creation
#############################################################################
$(BUILD):
mkdir -p $@
$(BUILD)/firmware:
mkdir -p $@
$(BUILD)/tests:
mkdir -p $@
.PHONY: all test test-unit test-system test-verilator lint clean