mem_init.mk 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #########################################################################
  2. ####### M E M I N I T M A K E F I L E C O N T E N T ######
  3. #########################################################################
  4. #########################################################################
  5. # This file is intended to be included by public.mk
  6. #
  7. #
  8. # The following variables must be defined before including this file:
  9. # - ELF
  10. #
  11. # The following variables may be defined to override the default behavior:
  12. # - HDL_SIM_DIR
  13. # - HDL_SIM_INSTALL_DIR
  14. # - MEM_INIT_DIR
  15. # - MEM_INIT_INSTALL_DIR
  16. # - QUARTUS_PROJECT_DIR
  17. # - SOPC_NAME
  18. # - SIM_OPTIMIZE
  19. # - RESET_ADDRESS
  20. #
  21. #########################################################################
  22. ifeq ($(MEM_INIT_FILE),)
  23. # MEM_INIT_FILE should be set equal to the working relative path to this
  24. # mem_init.mk makefile fragment
  25. MEM_INIT_FILE := $(wildcard $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)))
  26. endif
  27. ifeq ($(ELF2DAT),)
  28. ELF2DAT := elf2dat
  29. endif
  30. ifeq ($(ELF2HEX),)
  31. ELF2HEX := elf2hex
  32. endif
  33. ifeq ($(ELF2FLASH),)
  34. ELF2FLASH := elf2flash
  35. endif
  36. ifeq ($(FLASH2DAT),)
  37. FLASH2DAT := flash2dat
  38. endif
  39. ifeq ($(ALT_FILE_CONVERT),)
  40. ALT_FILE_CONVERT := alt-file-convert
  41. endif
  42. ifeq ($(NM),)
  43. NM := nios2-elf-nm
  44. endif
  45. ifeq ($(MKDIR),)
  46. MKDIR := mkdir -p
  47. endif
  48. ifeq ($(RM),)
  49. RM := rm -f
  50. endif
  51. ifeq ($(CP),)
  52. CP := cp
  53. endif
  54. ifeq ($(ECHO),)
  55. ECHO := echo
  56. endif
  57. MEM_INIT_DIR ?= mem_init
  58. HDL_SIM_DIR ?= $(MEM_INIT_DIR)/hdl_sim
  59. ifdef QUARTUS_PROJECT_DIR
  60. MEM_INIT_INSTALL_DIR ?= $(patsubst %/,%,$(QUARTUS_PROJECT_DIR))
  61. ifdef SOPC_NAME
  62. HDL_SIM_INSTALL_DIR ?= $(patsubst %/,%,$(QUARTUS_PROJECT_DIR))/$(SOPC_NAME)_sim
  63. endif
  64. endif
  65. MEM_INIT_DESCRIPTOR_FILE ?= $(MEM_INIT_DIR)/meminit.spd
  66. MEM_INIT_QIP_FILE ?= $(MEM_INIT_DIR)/meminit.qip
  67. #-------------------------------------
  68. # Default Flash Boot Loaders
  69. #-------------------------------------
  70. BOOT_LOADER_PATH ?= $(SOPC_KIT_NIOS2)/components/altera_nios2
  71. BOOT_LOADER_CFI ?= $(BOOT_LOADER_PATH)/boot_loader_cfi.srec
  72. BOOT_LOADER_CFI_BE ?= $(BOOT_LOADER_PATH)/boot_loader_cfi_be.srec
  73. #-------------------------------------
  74. # Default Target
  75. #-------------------------------------
  76. .PHONY: default_mem_init
  77. ifeq ($(QSYS),1)
  78. default_mem_init: mem_init_generate
  79. else
  80. default_mem_init: mem_init_install
  81. endif
  82. #-------------------------------------
  83. # Runtime Macros
  84. #-------------------------------------
  85. define post-process-info
  86. @echo Post-processing to create $@...
  87. endef
  88. target_stem = $(notdir $(basename $@))
  89. mem_start_address = $($(target_stem)_START)
  90. mem_end_address = $($(target_stem)_END)
  91. mem_span = $($(target_stem)_SPAN)
  92. mem_width = $($(target_stem)_WIDTH)
  93. mem_hex_width = $($(target_stem)_HEX_DATA_WIDTH)
  94. mem_endianness = $($(target_stem)_ENDIANNESS)
  95. mem_create_lanes = $($(target_stem)_CREATE_LANES)
  96. mem_pad_flag = $($(target_stem)_PAD_FLAG)
  97. mem_reloc_input_flag = $($(target_stem)_RELOC_INPUT_FLAG)
  98. mem_no_zero_fill_flag = $($(target_stem)_NO_ZERO_FILL_FLAG)
  99. flash_mem_epcs_flag = $($(target_stem)_EPCS_FLAGS)
  100. flash_mem_cfi_flag = $($(target_stem)_CFI_FLAGS)
  101. flash_mem_boot_loader_flag = $($(target_stem)_BOOT_LOADER_FLAG)
  102. elf2dat_extra_args = $(mem_pad_flag)
  103. elf2hex_extra_args = $(mem_no_zero_fill_flag)
  104. elf2flash_extra_args = $(flash_mem_cfi_flag) $(flash_mem_epcs_flag) $(flash_mem_boot_loader_flag)
  105. flash2dat_extra_args = $(mem_pad_flag) $(mem_reloc_input_flag)
  106. #------------------------------------------------------------------------------
  107. # BSP SPECIFIC CONTENT
  108. #
  109. # The content below is controlled by the BSP and SOPC System
  110. #------------------------------------------------------------------------------
  111. #START OF BSP SPECIFIC
  112. #-------------------------------------
  113. # Global Settings
  114. #-------------------------------------
  115. # The following TYPE comment allows tools to identify the 'type' of target this
  116. # makefile is associated with.
  117. # TYPE: BSP_MEMINIT_MAKEFILE
  118. # This following VERSION comment indicates the version of the tool used to
  119. # generate this makefile. A makefile variable is provided for VERSION as well.
  120. # ACDS_VERSION: 18.1
  121. ACDS_VERSION := 18.1
  122. # This following BUILD_NUMBER comment indicates the build number of the tool
  123. # used to generate this makefile.
  124. # BUILD_NUMBER: 646
  125. # Optimize for simulation
  126. SIM_OPTIMIZE ?= 0
  127. # The CPU reset address as needed by elf2flash
  128. RESET_ADDRESS ?= 0x00040000
  129. # The specific Nios II ELF file format to use.
  130. NIOS2_ELF_FORMAT ?= elf32-littlenios2
  131. #-------------------------------------
  132. # Pre-Initialized Memory Descriptions
  133. #-------------------------------------
  134. # Memory: onchip_memory2
  135. MEM_0 := nios2_uc_onchip_memory2
  136. $(MEM_0)_NAME := onchip_memory2
  137. $(MEM_0)_MEM_INIT_FILE_PARAM_NAME := INIT_FILE
  138. HEX_FILES += $(MEM_INIT_DIR)/$(MEM_0).hex
  139. MEM_INIT_INSTALL_FILES += $(MEM_INIT_INSTALL_DIR)/$(MEM_0).hex
  140. DAT_FILES += $(HDL_SIM_DIR)/$(MEM_0).dat
  141. HDL_SIM_INSTALL_FILES += $(HDL_SIM_INSTALL_DIR)/$(MEM_0).dat
  142. SYM_FILES += $(HDL_SIM_DIR)/$(MEM_0).sym
  143. HDL_SIM_INSTALL_FILES += $(HDL_SIM_INSTALL_DIR)/$(MEM_0).sym
  144. $(MEM_0)_START := 0x00040000
  145. $(MEM_0)_END := 0x00071fff
  146. $(MEM_0)_SPAN := 0x00032000
  147. $(MEM_0)_HIERARCHICAL_PATH := onchip_memory2
  148. $(MEM_0)_WIDTH := 32
  149. $(MEM_0)_HEX_DATA_WIDTH := 32
  150. $(MEM_0)_ENDIANNESS := --little-endian-mem
  151. $(MEM_0)_CREATE_LANES := 0
  152. .PHONY: onchip_memory2
  153. onchip_memory2: check_elf_exists $(MEM_INIT_DIR)/$(MEM_0).hex $(HDL_SIM_DIR)/$(MEM_0).dat $(HDL_SIM_DIR)/$(MEM_0).sym
  154. #END OF BSP SPECIFIC
  155. #-------------------------------------
  156. # Pre-Initialized Memory Targets
  157. #-------------------------------------
  158. .PHONY: mem_init_install mem_init_generate mem_init_clean
  159. ifeq ($(QSYS),1)
  160. # Target mem_init_install is deprecated for QSys based systems
  161. # To initialize onchip memories for Quartus II Synthesis with Qsys based systems:
  162. # 1) Use "make mem_init_genearate"
  163. # 2) Add the generated mem_init/meminit.qip file to your Quartus II Project
  164. #
  165. mem_init_install:
  166. $(error Deprecated Makefile Target: '$@'. Use target 'mem_init_generate' and then add $(MEM_INIT_QIP_FILE) to your Quartus II Project)
  167. else # QSYS != 1, if SopcBuilder based system
  168. ifneq ($(MEM_INIT_INSTALL_DIR),)
  169. mem_init_install: $(MEM_INIT_INSTALL_FILES)
  170. endif
  171. ifneq ($(HDL_SIM_INSTALL_DIR),)
  172. mem_init_install: $(HDL_SIM_INSTALL_FILES)
  173. endif
  174. mem_init_install: mem_init_generate
  175. ifeq ($(MEM_INIT_INSTALL_DIR),)
  176. @echo "WARNING: MEM_INIT_INSTALL_DIR not set. Set your QUARTUS_PROJECT_DIR environment variable."
  177. endif
  178. ifeq ($(HDL_SIM_INSTALL_DIR),)
  179. @echo "WARNING: HDL_SIM_INSTALL_DIR not set. Set your QUARTUS_PROJECT_DIR and SOPC_NAME environment variable."
  180. endif
  181. $(MEM_INIT_INSTALL_FILES): $(MEM_INIT_INSTALL_DIR)/%: $(MEM_INIT_DIR)/%
  182. @$(MKDIR) $(@D)
  183. @$(CP) -v $< $@
  184. $(HDL_SIM_INSTALL_FILES): $(HDL_SIM_INSTALL_DIR)/%: $(HDL_SIM_DIR)/%
  185. @$(MKDIR) $(@D)
  186. @$(CP) -v $< $@
  187. endif # QSYS == 1
  188. mem_init_generate: hex dat sym flash $(MEM_INIT_DESCRIPTOR_FILE) $(MEM_INIT_QIP_FILE)
  189. mem_init_clean:
  190. @$(RM) -r $(MEM_INIT_DIR) $(HDL_SIM_DIR) $(FLASH_FILES)
  191. .PHONY: hex dat sym flash
  192. hex: check_elf_exists $(HEX_FILES)
  193. dat: check_elf_exists $(DAT_FILES)
  194. sym: check_elf_exists $(SYM_FILES)
  195. flash: check_elf_exists $(FLASH_FILES)
  196. #-------------------------------------
  197. # Pre-Initialized Memory Rules
  198. #-------------------------------------
  199. .PHONY: check_elf_exists
  200. check_elf_exists: $(ELF)
  201. ifeq ($(ELF),)
  202. $(error ELF var not set in mem_init.mk)
  203. endif
  204. $(filter-out $(FLASH_DAT_FILES),$(DAT_FILES)): %.dat: $(ELF)
  205. $(post-process-info)
  206. @$(MKDIR) $(@D)
  207. $(ELF2DAT) --infile=$< --outfile=$@ \
  208. --base=$(mem_start_address) --end=$(mem_end_address) --width=$(mem_width) \
  209. $(mem_endianness) --create-lanes=$(mem_create_lanes) $(elf2dat_extra_args)
  210. $(foreach i,0 1 2 3 4 5 6 7,%_lane$(i).dat): %.dat
  211. @true
  212. ELF_TO_HEX_CMD_NO_BOOTLOADER = $(ELF2HEX) $< $(mem_start_address) $(mem_end_address) --width=$(mem_hex_width) \
  213. $(mem_endianness) --create-lanes=$(mem_create_lanes) $(elf2hex_extra_args) $@
  214. ELF_TO_HEX_CMD_WITH_BOOTLOADER = $(ALT_FILE_CONVERT) -I $(NIOS2_ELF_FORMAT) -O hex --input=$< --output=$@ \
  215. --base=$(mem_start_address) --end=$(mem_end_address) --reset=$(RESET_ADDRESS) \
  216. --out-data-width=$(mem_hex_width) $(flash_mem_boot_loader_flag)
  217. ELF_TO_HEX_CMD = $(strip $(if $(flash_mem_boot_loader_flag), \
  218. $(ELF_TO_HEX_CMD_WITH_BOOTLOADER), \
  219. $(ELF_TO_HEX_CMD_NO_BOOTLOADER) \
  220. ))
  221. $(HEX_FILES): %.hex: $(ELF)
  222. $(post-process-info)
  223. @$(MKDIR) $(@D)
  224. $(ELF_TO_HEX_CMD)
  225. $(SYM_FILES): %.sym: $(ELF)
  226. $(post-process-info)
  227. @$(MKDIR) $(@D)
  228. $(NM) -n $< > $@
  229. $(FLASH_FILES): %.flash: $(ELF)
  230. $(post-process-info)
  231. @$(MKDIR) $(@D)
  232. $(ELF2FLASH) --input=$< --outfile=$@ --sim_optimize=$(SIM_OPTIMIZE) $(mem_endianness) \
  233. $(elf2flash_extra_args)
  234. #
  235. # Function generate_spd_entry
  236. # Arg1: path to the memory initialization file
  237. # Arg2: Type HEX or DAT
  238. # Arg3: Output spd file to append
  239. gen_spd_entry.BASE_FILE = $(basename $(notdir $1))
  240. gen_spd_entry.PARAM_NAME = $($(gen_spd_entry.BASE_FILE)_MEM_INIT_FILE_PARAM_NAME)
  241. gen_spd_entry.MEM_PATH = $($(gen_spd_entry.BASE_FILE)_HIERARCHICAL_PATH)
  242. gen_spd_entry.SETTINGS = $(strip \
  243. path=\"$1\" \
  244. type=\"$2\" \
  245. $(if $(gen_spd_entry.PARAM_NAME),initParamName=\"$(gen_spd_entry.PARAM_NAME)\") \
  246. $(if $(gen_spd_entry.MEM_PATH),memoryPath=\"$(gen_spd_entry.MEM_PATH)\") \
  247. )
  248. define gen_spd_entry
  249. $(ECHO) "<file $(gen_spd_entry.SETTINGS) />" >> $3
  250. endef
  251. $(MEM_INIT_DESCRIPTOR_FILE).DAT_FILESET := $(patsubst $(dir $(MEM_INIT_DESCRIPTOR_FILE))%,%,$(DAT_FILES))
  252. $(MEM_INIT_DESCRIPTOR_FILE).HEX_FILESET := $(patsubst $(dir $(MEM_INIT_DESCRIPTOR_FILE))%,%,$(HEX_FILES))
  253. $(MEM_INIT_DESCRIPTOR_FILE): %.spd: $(MEM_INIT_FILE)
  254. $(post-process-info)
  255. @$(MKDIR) $(@D)
  256. @$(RM) $@
  257. @$(ECHO) "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > $@
  258. @$(ECHO) "<simPackage>" >> $@
  259. @$(foreach dat_file,$($@.DAT_FILESET),$(call gen_spd_entry,$(dat_file),DAT,$@) &&)true
  260. @$(foreach hex_file,$($@.HEX_FILESET),$(call gen_spd_entry,$(hex_file),HEX,$@) &&)true
  261. @$(ECHO) "</simPackage>" >> $@
  262. .DELETE_ON_ERROR: $(MEM_INIT_DESCRIPTOR_FILE)
  263. $(MEM_INIT_QIP_FILE): %.qip: $(MEM_INIT_FILE)
  264. $(post-process-info)
  265. @$(MKDIR) $(@D)
  266. @$(RM) $@
  267. @$(ECHO) "set_global_assignment -name SEARCH_PATH $$::quartus(qip_path)" > $@
  268. .DELETE_ON_ERROR: $(MEM_INIT_QIP_FILE)