project-rules.mk 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 -relut
  8. NEXTPNR ?= nextpnr-ice40
  9. NEXTPNR_ARGS ?= --freq 50
  10. ICEPACK ?= icepack
  11. ICEPROG ?= iceprog
  12. IVERILOG ?= iverilog
  13. ICE40_LIBS ?= $(shell yosys-config --datdir/ice40/cells_sim.v)
  14. # Must be first rule and call it 'all' by convention
  15. all: synth
  16. # Root directory
  17. ROOT := $(abspath $(dir $(lastword $(MAKEFILE_LIST)))/..)
  18. # Temporary build-directory
  19. BUILD_TMP := $(abspath build-tmp)
  20. $(BUILD_TMP):
  21. mkdir -p $(BUILD_TMP)
  22. # Discover all cores
  23. $(foreach core_dir, $(wildcard $(ROOT)/cores/*), $(eval include $(core_dir)/core.mk))
  24. # Resolve dependency tree for project and collect sources
  25. $(BUILD_TMP)/proj-deps.mk: Makefile $(BUILD_TMP) $(addprefix $(BUILD_TMP)/deps-core-,$(PROJ_DEPS))
  26. @echo "include $(BUILD_TMP)/deps-core-*" > $@
  27. @echo "PROJ_ALL_DEPS := \$$(DEPS_SOLVE_TMP)" >> $@
  28. @echo "PROJ_ALL_RTL_SRCS := \$$(RTL_SRCS_SOLVE_TMP)" >> $@
  29. @echo "PROJ_ALL_SIM_SRCS := \$$(SIM_SRCS_SOLVE_TMP)" >> $@
  30. @echo "PROJ_ALL_PREREQ := \$$(PREREQ_SOLVE_TMP)" >> $@
  31. include $(BUILD_TMP)/proj-deps.mk
  32. # Make all sources absolute
  33. PROJ_RTL_SRCS := $(abspath $(PROJ_RTL_SRCS))
  34. PROJ_TOP_SRC := $(abspath $(PROJ_TOP_SRC))
  35. PIN_DEF ?= $(abspath data/$(PROJ_TOP_MOD)-$(BOARD).pcf)
  36. # Add those to the list
  37. PROJ_ALL_RTL_SRCS += $(PROJ_RTL_SRCS)
  38. PROJ_ALL_SIM_SRCS += $(PROJ_SIM_SRCS)
  39. PROJ_ALL_PREREQ += $(PROJ_PREREQ)
  40. # Include path
  41. PROJ_SYNTH_INCLUDES := -I$(abspath rtl/) $(addsuffix /rtl/, $(addprefix -I$(ROOT)/cores/, $(PROJ_ALL_DEPS)))
  42. PROJ_SIM_INCLUDES := -I$(abspath sim/) $(addsuffix /sim/, $(addprefix -I$(ROOT)/cores/, $(PROJ_ALL_DEPS)))
  43. # Synthesis & Place-n-route rules
  44. $(BUILD_TMP)/$(PROJ).ys: $(PROJ_TOP_SRC) $(PROJ_ALL_RTL_SRCS)
  45. @echo "read_verilog $(YOSYS_READ_ARGS) $(PROJ_SYNTH_INCLUDES) $(PROJ_TOP_SRC) $(PROJ_ALL_RTL_SRCS)" > $@
  46. @echo "synth_ice40 $(YOSYS_SYNTH_ARGS) -top $(PROJ_TOP_MOD) -json $(PROJ).json" >> $@
  47. $(BUILD_TMP)/$(PROJ).synth.rpt $(BUILD_TMP)/$(PROJ).json: $(PROJ_ALL_PREREQ) $(BUILD_TMP)/$(PROJ).ys $(PROJ_ALL_RTL_SRCS)
  48. cd $(BUILD_TMP) && \
  49. $(YOSYS) -s $(BUILD_TMP)/$(PROJ).ys \
  50. -l $(BUILD_TMP)/$(PROJ).synth.rpt
  51. $(BUILD_TMP)/$(PROJ).pnr.rpt $(BUILD_TMP)/$(PROJ).asc: $(BUILD_TMP)/$(PROJ).json $(PIN_DEF)
  52. $(NEXTPNR) $(NEXTPNR_ARGS) \
  53. --$(DEVICE) --package $(PACKAGE) \
  54. -l $(BUILD_TMP)/$(PROJ).pnr.rpt \
  55. --json $(BUILD_TMP)/$(PROJ).json \
  56. --pcf $(PIN_DEF) \
  57. --asc $@
  58. %.bin: %.asc
  59. $(ICEPACK) -s $< $@
  60. # Simulation
  61. $(BUILD_TMP)/%_tb: sim/%_tb.v $(ICE40_LIBS) $(PROJ_ALL_PREREQ) $(PROJ_ALL_RTL_SRCS) $(PROJ_ALL_SIM_SRCS)
  62. iverilog -Wall -DSIM=1 -o $@ \
  63. $(PROJ_SYNTH_INCLUDES) $(PROJ_SIM_INCLUDES) \
  64. $(addprefix -l, $(ICE40_LIBS) $(PROJ_ALL_RTL_SRCS) $(PROJ_ALL_SIM_SRCS)) \
  65. $<
  66. # Action targets
  67. synth: $(BUILD_TMP)/$(PROJ).bin
  68. sim: $(addprefix $(BUILD_TMP)/, $(PROJ_TESTBENCHES))
  69. prog: $(BUILD_TMP)/$(PROJ).bin
  70. $(ICEPROG) $<
  71. sudo-prog: $(BUILD_TMP)/$(PROJ).bin
  72. @echo 'Executing prog as root!!!'
  73. sudo $(ICEPROG) $<
  74. clean:
  75. @rm -Rf $(BUILD_TMP)
  76. .PHONY: all synth sim prog sudo-prog clean