project-rules.mk 3.5 KB

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