fpgamakelib.mk 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. ######################################################
  2. # FPGA make library #
  3. # Original author: Michael Buesch <m@bues.ch> #
  4. # This code is Public Domain. #
  5. # Version 1.0 #
  6. ######################################################
  7. # Tools.
  8. YOSYS := yosys
  9. NEXTPNR := nextpnr
  10. ICEPACK := icepack
  11. ICETIME := icetime
  12. ICEPLL := icepll
  13. TINYPROG := tinyprog
  14. PYTHON := python3
  15. CAT := cat
  16. GREP := grep
  17. PRINTF := printf
  18. RM := rm
  19. FALSE := false
  20. TR := tr
  21. TEST := test
  22. TOUCH := touch
  23. GIT := git
  24. # Target name transformations.
  25. TARGET_NAME := $(TARGET)_$(NAME)
  26. TARGET_LOWER := $(shell $(PRINTF) '%s' '$(TARGET)' | $(TR) A-Z a-z)
  27. TARGET_UPPER := $(shell $(PRINTF) '%s' '$(TARGET)' | $(TR) a-z A-Z)
  28. # Target specific options and commands.
  29. ifeq ($(TARGET_LOWER),tinyfpga_bx)
  30. YOSYS_SYNTH_CMD = 'synth_ice40 -top $(TOP_MODULE) -json $(patsubst %.blif,%.json,$@) -blif $@'
  31. PNR_PACKAGE := cm81
  32. NEXTPNR_ARCH := ice40
  33. DEVICE := lp8k
  34. else
  35. $(error TARGET $(TARGET) is unknown)
  36. endif
  37. # Log files.
  38. YOSYS_LOG := $(TARGET_NAME)_yosys.log
  39. NEXTPNR_LOG := $(TARGET_NAME)_nextpnr.log
  40. ICEPACK_LOG := $(TARGET_NAME)_icepack.log
  41. ICETIME_LOG := $(TARGET_NAME)_icetime.log
  42. LOG = >$(1) 2>&1 || ( $(CAT) $(1); $(FALSE) )
  43. # PLL sanity check.
  44. ifneq ($(PLL_MOD_V_FILE),)
  45. ifeq ($(PLL_HZ),)
  46. $(error PLL_HZ is not defined)
  47. endif
  48. endif
  49. # Clock speeds.
  50. ifeq ($(TARGET_LOWER),tinyfpga_bx)
  51. CLK_HZ := 16000000
  52. ifeq ($(PLL_MOD_V_FILE),)
  53. PLL_HZ := $(CLK_HZ)
  54. else
  55. CLK_MHZ := $(shell expr $(CLK_HZ) / 1000000)
  56. PLL_MHZ := $(shell expr $(PLL_HZ) / 1000000)
  57. $(PLL_MOD_V_FILE):
  58. $(ICEPLL) -q -f $@ -m -n pll_module -i $(CLK_MHZ) -o $(PLL_MHZ)
  59. endif
  60. endif
  61. # MyHDL to Verilog
  62. %.v: %.py $(wildcard *.py) $(EXTRA_DEP_PY)
  63. $(PYTHON) -B $<
  64. # Synthesis
  65. %.blif: $(TOP_FILE) $(wildcard *.v) $(GENERATED_V) $(PLL_MOD_V_FILE) $(EXTRA_DEP_V)
  66. $(YOSYS) -p 'read_verilog -DTARGET_$(TARGET_UPPER)=1 -DCLK_HZ=$(CLK_HZ) -DPLL_HZ=$(PLL_HZ) $(if $(filter-out 0,$(DEBUG)),-DDEBUG=1) $<' \
  67. -p $(YOSYS_SYNTH_CMD) \
  68. $(call LOG,$(YOSYS_LOG))
  69. # Place and route
  70. %.asc: $(PCF_FILE) %.blif
  71. $(NEXTPNR)-$(NEXTPNR_ARCH) --$(DEVICE) --package $(PNR_PACKAGE) --json $(TARGET_NAME).json --pcf $< --asc $@ \
  72. $(call LOG,$(NEXTPNR_LOG))
  73. # Binary packing
  74. %.bin: %.asc
  75. ifeq ($(TARGET_LOWER),tinyfpga_bx)
  76. $(ICEPACK) $< $@ $(call LOG,$(ICEPACK_LOG))
  77. endif
  78. # Report generation
  79. %.rpt: %.asc %.bin
  80. ifeq ($(TARGET_LOWER),tinyfpga_bx)
  81. $(ICETIME) -d $(DEVICE) -p $(PCF_FILE) -m -t -r $@ $< \
  82. $(call LOG,$(ICETIME_LOG))
  83. -@$(PRINTF) '\n'
  84. endif
  85. -@$(GREP) -i -e 'Total' $@
  86. -@$(GREP) -i -Ee 'Max frequency|Max delay' $(NEXTPNR_LOG)
  87. -@$(PRINTF) '\n'
  88. -@$(GREP) --color=auto -H -n -i -e 'Warning' $(YOSYS_LOG) $(NEXTPNR_LOG) $(ICEPACK_LOG)
  89. -@$(PRINTF) '\n'
  90. -@$(GREP) -A6 -i -e 'Device utilisation' $(NEXTPNR_LOG)
  91. # Flashing
  92. install: $(TARGET_NAME).bin
  93. ifeq ($(TARGET_LOWER),tinyfpga_bx)
  94. $(TINYPROG) -p $<
  95. endif
  96. # Rebooting
  97. boot:
  98. ifeq ($(TARGET_LOWER),tinyfpga_bx)
  99. $(TINYPROG) -b
  100. endif
  101. # Cleanup
  102. clean:
  103. $(RM) -rf *.blif *.json *.asc *.bin *.rpt __pycache__ $(YOSYS_LOG) $(NEXTPNR_LOG) $(ICEPACK_LOG) $(ICETIME_LOG) $(GENERATED_V) $(PLL_MOD_V_FILE) $(CLEAN_FILES)
  104. # Default goal
  105. all: $(TARGET_NAME).bin $(TARGET_NAME).rpt
  106. .DEFAULT_GOAL := all
  107. .PHONY: all install boot clean
  108. .PRECIOUS: %.json %.blif %.asc $(GENERATED_V)