project-rules.mk 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. # Root directory
  21. ROOT := $(abspath $(dir $(lastword $(MAKEFILE_LIST)))/..)
  22. # Temporary build-directory
  23. BUILD_TMP := $(abspath build-tmp)
  24. $(BUILD_TMP):
  25. mkdir -p $(BUILD_TMP)
  26. # Discover all cores
  27. $(foreach core_dir, $(wildcard $(ROOT)/cores/*), $(eval include $(core_dir)/core.mk))
  28. # Resolve dependency tree for project and collect sources
  29. $(BUILD_TMP)/proj-deps.mk: Makefile $(BUILD_TMP) $(addprefix $(BUILD_TMP)/deps-core-,$(PROJ_DEPS))
  30. @echo "include $(BUILD_TMP)/deps-core-*" > $@
  31. @echo "PROJ_ALL_DEPS := \$$(DEPS_SOLVE_TMP)" >> $@
  32. @echo "PROJ_ALL_RTL_SRCS := \$$(RTL_SRCS_SOLVE_TMP)" >> $@
  33. @echo "PROJ_ALL_SIM_SRCS := \$$(SIM_SRCS_SOLVE_TMP)" >> $@
  34. @echo "PROJ_ALL_PREREQ := \$$(PREREQ_SOLVE_TMP)" >> $@
  35. include $(BUILD_TMP)/proj-deps.mk
  36. # Make all sources absolute
  37. PROJ_RTL_SRCS := $(abspath $(PROJ_RTL_SRCS))
  38. PROJ_TOP_SRC := $(abspath $(PROJ_TOP_SRC))
  39. # Board config
  40. PIN_DEF ?= $(abspath data/$(PROJ_TOP_MOD)-$(BOARD).pcf)
  41. BOARD_DEFINE=BOARD_$(shell echo $(BOARD) | tr a-z\- A-Z_)
  42. YOSYS_READ_ARGS += -D$(BOARD_DEFINE)=1
  43. # Add those to the list
  44. PROJ_ALL_RTL_SRCS += $(PROJ_RTL_SRCS)
  45. PROJ_ALL_SIM_SRCS += $(PROJ_SIM_SRCS)
  46. PROJ_ALL_PREREQ += $(PROJ_PREREQ)
  47. # Include path
  48. PROJ_SYNTH_INCLUDES := -I$(abspath rtl/) $(addsuffix /rtl/, $(addprefix -I$(ROOT)/cores/, $(PROJ_ALL_DEPS)))
  49. PROJ_SIM_INCLUDES := -I$(abspath sim/) $(addsuffix /sim/, $(addprefix -I$(ROOT)/cores/, $(PROJ_ALL_DEPS)))
  50. # Synthesis & Place-n-route rules
  51. $(BUILD_TMP)/$(PROJ).ys: $(PROJ_TOP_SRC) $(PROJ_ALL_RTL_SRCS)
  52. @echo "read_verilog $(YOSYS_READ_ARGS) $(PROJ_SYNTH_INCLUDES) $(PROJ_TOP_SRC) $(PROJ_ALL_RTL_SRCS)" > $@
  53. @echo "synth_ice40 $(YOSYS_SYNTH_ARGS) -top $(PROJ_TOP_MOD) -json $(PROJ).json" >> $@
  54. $(BUILD_TMP)/$(PROJ).synth.rpt $(BUILD_TMP)/$(PROJ).json: $(PROJ_ALL_PREREQ) $(BUILD_TMP)/$(PROJ).ys $(PROJ_ALL_RTL_SRCS)
  55. cd $(BUILD_TMP) && \
  56. $(YOSYS) -s $(BUILD_TMP)/$(PROJ).ys \
  57. -l $(BUILD_TMP)/$(PROJ).synth.rpt
  58. $(BUILD_TMP)/$(PROJ).pnr.rpt $(BUILD_TMP)/$(PROJ).asc: $(BUILD_TMP)/$(PROJ).json $(PIN_DEF)
  59. $(NEXTPNR) $(NEXTPNR_ARGS) $(NEXTPNR_SYS_ARGS) \
  60. --$(DEVICE) --package $(PACKAGE) \
  61. -l $(BUILD_TMP)/$(PROJ).pnr.rpt \
  62. --json $(BUILD_TMP)/$(PROJ).json \
  63. --pcf $(PIN_DEF) \
  64. --asc $@
  65. %.bin: %.asc
  66. $(ICEPACK) -s $< $@
  67. # Simulation
  68. $(BUILD_TMP)/%_tb: sim/%_tb.v $(ICE40_LIBS) $(PROJ_ALL_PREREQ) $(PROJ_ALL_RTL_SRCS) $(PROJ_ALL_SIM_SRCS)
  69. $(IVERILOG) -Wall -Wno-portbind -Wno-timescale -DSIM=1 -D$(BOARD_DEFINE)=1 -o $@ \
  70. $(PROJ_SYNTH_INCLUDES) $(PROJ_SIM_INCLUDES) \
  71. $(addprefix -l, $(ICE40_LIBS) $(PROJ_ALL_RTL_SRCS) $(PROJ_ALL_SIM_SRCS)) \
  72. $<
  73. # Action targets
  74. synth: $(BUILD_TMP)/$(PROJ).bin
  75. sim: $(addprefix $(BUILD_TMP)/, $(PROJ_TESTBENCHES))
  76. prog: $(BUILD_TMP)/$(PROJ).bin
  77. $(ICEPROG) $<
  78. sudo-prog: $(BUILD_TMP)/$(PROJ).bin
  79. @echo 'Executing prog as root!!!'
  80. sudo $(ICEPROG) $<
  81. dfuprog: $(BUILD_TMP)/$(PROJ).bin
  82. ifeq ($(DFU_SERIAL),)
  83. @echo "[!] DFU_SERIAL not defined"
  84. else
  85. $(DFU_UTIL) -e -S $(DFU_SERIAL) -a 0 -D $<
  86. endif
  87. clean:
  88. @rm -Rf $(BUILD_TMP)
  89. .PHONY: all synth sim prog sudo-prog clean