project-rules.mk 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #
  2. # project-rules.mk
  3. #
  4. # Default tools
  5. YOSYS ?= yosys
  6. YOSYS_READ_ARGS ?=
  7. YOSYS_SYNTH_ARGS ?= -dffe_min_ce_use 4
  8. NEXTPNR ?= nextpnr-ice40
  9. NEXTPNR_ARGS ?= --freq 50
  10. ICEPACK ?= icepack
  11. ICEPROG ?= iceprog
  12. IVERILOG ?= iverilog
  13. DFU_UTIL ?= dfu-util
  14. ifeq ($(PLACER),heap)
  15. NEXTPNR_SYS_ARGS += --placer heap
  16. endif
  17. ICE40_LIBS ?= $(shell yosys-config --datdir/ice40/cells_sim.v)
  18. # Must be first rule and call it 'all' by convention
  19. all: synth
  20. # Base directories
  21. ifeq ($(origin NO2BUILD_DIR), undefined)
  22. NO2BUILD_DIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
  23. endif
  24. ifeq ($(origin NO2CORES_DIR), undefined)
  25. NO2CORES_DIR := $(abspath $(NO2BUILD_DIR)/../cores)
  26. endif
  27. # Temporary build-directory
  28. BUILD_TMP := $(abspath build-tmp)
  29. $(BUILD_TMP):
  30. mkdir -p $(BUILD_TMP)
  31. # Discover all cores
  32. $(foreach core_def, $(wildcard $(NO2CORES_DIR)/*/no2core.mk), $(eval include $(core_def)))
  33. # Resolve dependency tree for project and collect sources
  34. $(BUILD_TMP)/proj-deps.mk: Makefile $(BUILD_TMP) $(addprefix $(BUILD_TMP)/deps-core-,$(PROJ_DEPS))
  35. @echo "SELF_DIR := \$$(dir \$$(lastword \$$(MAKEFILE_LIST)))" > $@
  36. @echo "include \$$(SELF_DIR)deps-core-*" >> $@
  37. @echo "PROJ_ALL_DEPS := \$$(DEPS_SOLVE_TMP)" >> $@
  38. @echo "PROJ_ALL_RTL_SRCS := \$$(RTL_SRCS_SOLVE_TMP)" >> $@
  39. @echo "PROJ_ALL_SIM_SRCS := \$$(SIM_SRCS_SOLVE_TMP)" >> $@
  40. @echo "PROJ_ALL_PREREQ := \$$(PREREQ_SOLVE_TMP)" >> $@
  41. include $(BUILD_TMP)/proj-deps.mk
  42. # Make all sources absolute
  43. PROJ_RTL_SRCS := $(abspath $(PROJ_RTL_SRCS))
  44. PROJ_TOP_SRC := $(abspath $(PROJ_TOP_SRC))
  45. # Board config
  46. PIN_DEF ?= $(abspath data/$(PROJ_TOP_MOD)-$(BOARD).pcf)
  47. BOARD_DEFINE=BOARD_$(shell echo $(BOARD) | tr a-z\- A-Z_)
  48. YOSYS_READ_ARGS += -D$(BOARD_DEFINE)=1
  49. # Add those to the list
  50. PROJ_ALL_RTL_SRCS += $(PROJ_RTL_SRCS)
  51. PROJ_ALL_SIM_SRCS += $(PROJ_SIM_SRCS)
  52. PROJ_ALL_PREREQ += $(PROJ_PREREQ)
  53. # Include path
  54. PROJ_SYNTH_INCLUDES := -I$(abspath rtl/) $(addsuffix /rtl/, $(addprefix -I$(NO2CORES_DIR)/, $(PROJ_ALL_DEPS)))
  55. PROJ_SIM_INCLUDES := -I$(abspath sim/) $(addsuffix /sim/, $(addprefix -I$(NO2CORES_DIR)/, $(PROJ_ALL_DEPS)))
  56. # Synthesis & Place-n-route rules
  57. $(BUILD_TMP)/$(PROJ).ys: $(PROJ_TOP_SRC) $(PROJ_ALL_RTL_SRCS)
  58. @echo "read_verilog $(YOSYS_READ_ARGS) $(PROJ_SYNTH_INCLUDES) $(PROJ_TOP_SRC) $(PROJ_ALL_RTL_SRCS)" > $@
  59. @echo "synth_ice40 $(YOSYS_SYNTH_ARGS) -top $(PROJ_TOP_MOD) -json $(PROJ).json" >> $@
  60. $(BUILD_TMP)/$(PROJ).synth.rpt $(BUILD_TMP)/$(PROJ).json: $(PROJ_ALL_PREREQ) $(BUILD_TMP)/$(PROJ).ys $(PROJ_ALL_RTL_SRCS)
  61. cd $(BUILD_TMP) && \
  62. $(YOSYS) -s $(BUILD_TMP)/$(PROJ).ys \
  63. -l $(BUILD_TMP)/$(PROJ).synth.rpt
  64. $(BUILD_TMP)/$(PROJ).pnr.rpt $(BUILD_TMP)/$(PROJ).asc: $(BUILD_TMP)/$(PROJ).json $(PIN_DEF)
  65. $(NEXTPNR) $(NEXTPNR_ARGS) $(NEXTPNR_SYS_ARGS) \
  66. --$(DEVICE) --package $(PACKAGE) \
  67. -l $(BUILD_TMP)/$(PROJ).pnr.rpt \
  68. --json $(BUILD_TMP)/$(PROJ).json \
  69. --pcf $(PIN_DEF) \
  70. --asc $@
  71. %.bin: %.asc
  72. $(ICEPACK) -s $< $@
  73. # Simulation
  74. $(BUILD_TMP)/%_tb: sim/%_tb.v $(ICE40_LIBS) $(PROJ_ALL_PREREQ) $(PROJ_ALL_RTL_SRCS) $(PROJ_ALL_SIM_SRCS)
  75. $(IVERILOG) -Wall -Wno-portbind -Wno-timescale -DSIM=1 -D$(BOARD_DEFINE)=1 -o $@ \
  76. $(PROJ_SYNTH_INCLUDES) $(PROJ_SIM_INCLUDES) \
  77. $(addprefix -l, $(ICE40_LIBS) $(PROJ_ALL_RTL_SRCS) $(PROJ_ALL_SIM_SRCS)) \
  78. $<
  79. # Action targets
  80. synth: $(BUILD_TMP)/$(PROJ).bin
  81. sim: $(addprefix $(BUILD_TMP)/, $(PROJ_TESTBENCHES))
  82. prog: $(BUILD_TMP)/$(PROJ).bin
  83. $(ICEPROG) $<
  84. sudo-prog: $(BUILD_TMP)/$(PROJ).bin
  85. @echo 'Executing prog as root!!!'
  86. sudo $(ICEPROG) $<
  87. dfuprog: $(BUILD_TMP)/$(PROJ).bin
  88. ifeq ($(DFU_SERIAL),)
  89. @echo "[!] DFU_SERIAL not defined"
  90. else
  91. $(DFU_UTIL) -e -S $(DFU_SERIAL) -a 0 -D $<
  92. endif
  93. clean:
  94. @rm -Rf $(BUILD_TMP)
  95. .PHONY: all synth sim prog sudo-prog clean