Build system changes.
[DuneNvme.git] / vivado / Vivado.mk
1 ################################################################################
2 #       Vivado.mk       General common Makefile targets
3 #                       T.Barnaby,      BEAM Ltd,       2020-02-18
4 ################################################################################
5 #
6 # Parameters
7 #  PROJECT:     The projects name
8 #  BOARD:       The board type if a specific standard board
9 #  FPGA_PART:   FPGA device (e.g. xcvu095-ffva2104-2-e)
10 #  FPGA_TOP:    Top module name
11 #  VIVADO_PATH: Path to Xilinx Vivado tools
12 #  VIVADO_TARGET: The Vivado FPGA prohram target
13 #  SYN_FILES:   Space-separated list of source files
14 #  INC_FILES:   Space-separated list of include files
15 #  XDC_FILES:   Space-separated list of timing constraint files
16 #  XCI_FILES:   Space-separated list of IP XCI files
17 #
18 # Targets
19 #  all:         Build everything
20 #  clean:       Remove output files and project files
21 #  distclean:   Clean all files
22 #
23
24 VIVADO_PATH     ?= /opt/Xilinx/Vivado/2019.2/bin
25 VIVADO_TARGET   ?= ""
26
27 export PATH     := ${VIVADO_PATH}:${PATH}
28
29 .PHONY: clean fpga
30
31 # Prevent make from deleting intermediate files and reports
32 .PRECIOUS: ${PROJECT}.xpr ${PROJECT}.bit ${PROJECT}.mcs ${PROJECT}.prm
33 .SECONDARY:
34
35 all: fpga
36
37 fpga: $(PROJECT).bit
38
39 clean:
40         -rm -rf *.log *.jou *.cache *.hw *.ip_user_files *.runs *.xpr *.html *.xml *.sim *.srcs *.str .Xil defines.v
41         -rm -rf create_project.tcl run_synth.tcl run_impl.tcl generate_bit.tcl
42         -rm -rf *.bit program.tcl generate_mcs.tcl *.mcs *.prm flash.tcl
43
44 distclean: clean
45         -rm -rf rev
46
47 # Vivado project file
48 ${PROJECT}.xpr: Makefile $(XCI_FILES)
49         rm -rf defines.v
50         touch defines.v
51         for x in $(DEFS); do echo '`define' $$x >> defines.v; done
52         echo "create_project -force -part $(FPGA_PART) ${PROJECT}" > create_project.tcl
53         if [ "${BOARD}" != "" ]; then echo "set_property board_part ${BOARD} [current_project]" >> create_project.tcl; fi
54         echo "set_property target_language VHDL [current_project]" >> create_project.tcl
55         echo "add_files -fileset sources_1 defines.v" >> create_project.tcl
56         for x in $(SYN_FILES); do echo "add_files -fileset sources_1 $$x" >> create_project.tcl; done
57         for x in $(XDC_FILES); do echo "add_files -fileset constrs_1 $$x" >> create_project.tcl; done
58         for x in $(XCI_FILES); do echo "import_ip $$x" >> create_project.tcl; done
59         echo "exit" >> create_project.tcl
60         vivado -nojournal -nolog -mode batch -source create_project.tcl
61
62 # Synthesis run
63 ${PROJECT}.runs/synth_1/${PROJECT}.dcp: ${PROJECT}.xpr $(SYN_FILES) $(INC_FILES) $(XDC_FILES)
64         rm -f $(PROJECT).bit
65         echo "open_project ${PROJECT}.xpr" > run_synth.tcl
66         echo "reset_run synth_1" >> run_synth.tcl
67         echo "launch_runs synth_1 -jobs 4" >> run_synth.tcl
68         echo "wait_on_run synth_1" >> run_synth.tcl
69         echo "set runStatus [ get_property STATUS [get_runs synth_1] ]" >> run_synth.tcl
70         #echo 'puts stderr "RunStatus: $${runStatus}"' >> run_synth.tcl
71         echo 'if { $${runStatus} != "synth_design Complete!"} {' >> run_synth.tcl
72         echo "  exit 1" >> run_synth.tcl
73         echo "}" >> run_synth.tcl
74         echo "exit 0" >> run_synth.tcl
75         vivado -nojournal -nolog -mode batch -source run_synth.tcl
76
77 # Implementation run
78 ${PROJECT}.runs/impl_1/${PROJECT}_routed.dcp: ${PROJECT}.runs/synth_1/${PROJECT}.dcp
79         rm -f $(PROJECT).bit
80         echo "open_project ${PROJECT}.xpr" > run_impl.tcl
81         echo "reset_run impl_1" >> run_impl.tcl
82         echo "launch_runs impl_1 -jobs 4" >> run_impl.tcl
83         #echo "launch_runs impl_1 -to_step write_bitstream -jobs 4" >> run_impl.tcl
84         echo "wait_on_run impl_1" >> run_impl.tcl
85         echo "set runStatus [ get_property STATUS [get_runs impl_1] ]" >> run_impl.tcl
86         echo 'puts stderr "RunStatus: $${runStatus}"' >> run_impl.tcl
87         echo 'if { $${runStatus} != "route_design Complete!"} {' >> run_impl.tcl
88         #echo 'if { $${runStatus} != "write_bitstream Complete!"} {' >> run_impl.tcl
89         echo "  exit 1" >> run_impl.tcl
90         echo "}" >> run_impl.tcl
91
92         echo "exit 0" >> run_impl.tcl
93         vivado -nojournal -nolog -mode batch -source run_impl.tcl
94
95 # Bit file
96 ${PROJECT}.bit: ${PROJECT}.runs/impl_1/${PROJECT}_routed.dcp
97         rm -f $(PROJECT).bit
98         echo "open_project ${PROJECT}.xpr" > generate_bit.tcl
99         echo "open_run impl_1" >> generate_bit.tcl
100         echo "report_utilization -hierarchical -file utilisation.txt" >> generate_bit.tcl
101         echo "write_bitstream -force ${PROJECT}.bit" >> generate_bit.tcl
102         echo "exit" >> generate_bit.tcl
103         time vivado -nojournal -nolog -mode batch -source generate_bit.tcl
104         mkdir -p rev
105         EXT=bit; COUNT=100; \
106         while [ -e rev/${PROJECT}_rev$$COUNT.$$EXT ]; \
107         do COUNT=$$((COUNT+1)); done; \
108         cp $@ rev/${PROJECT}_rev$$COUNT.$$EXT; \
109         echo "Output: rev/${PROJECT}_rev$$COUNT.$$EXT";
110
111 # Extras for flash etc
112 ${PROJECT}_primary.mcs ${PROJECT}_secondary.mcs ${PROJECT}_primary.prm ${PROJECT}_secondary.prm: ${PROJECT}.bit
113         echo "write_cfgmem -force -format mcs -size 256 -interface SPIx8 -loadbit {up 0x0000000 $*.bit} -checksum -file $*.mcs" > generate_mcs.tcl
114         echo "exit" >> generate_mcs.tcl
115         vivado -nojournal -nolog -mode batch -source generate_mcs.tcl
116         mkdir -p rev
117         COUNT=100; \
118         while [ -e rev/$*_rev$$COUNT.bit ]; \
119         do COUNT=$$((COUNT+1)); done; \
120         COUNT=$$((COUNT-1)); \
121         for x in _primary.mcs _secondary.mcs _primary.prm _secondary.prm; \
122         do cp $*$$x rev/$*_rev$$COUNT$$x; \
123         echo "Output: rev/$*_rev$$COUNT$$x"; done;
124
125 report: ${PROJECT}.runs/impl_1/${PROJECT}_routed.dcp
126         echo "open_project ${PROJECT}.xpr" > report.tcl
127         echo "open_run impl_1" >> report.tcl
128         echo "report_utilization -hierarchical -file utilisation.txt" >> report.tcl
129         echo "exit" >> report.tcl
130         vivado -nojournal -nolog -mode batch -source report.tcl
131
132 program: $(PROJECT).bit
133         echo "open_hw_manager" > program.tcl
134         echo "connect_hw_server ${FPGA_TARGET}" >> program.tcl
135         echo "open_hw_target" >> program.tcl
136         echo "current_hw_device [lindex [get_hw_devices] 0]" >> program.tcl
137         echo "refresh_hw_device -update_hw_probes false [current_hw_device]" >> program.tcl
138         echo "set_property PROGRAM.FILE {$(PROJECT).bit} [current_hw_device]" >> program.tcl
139         echo "program_hw_devices [current_hw_device]" >> program.tcl
140         echo "exit" >> program.tcl
141         vivado -nojournal -nolog -mode batch -source program.tcl
142         # cs_server sits around using 100% CPU!
143         killall cs_server
144
145 flash: $(PROJECT)_primary.mcs $(PROJECT)_secondary.mcs $(PROJECT)_primary.prm $(PROJECT)_secondary.prm
146         echo "open_hw" > flash.tcl
147         echo "connect_hw_server" >> flash.tcl
148         echo "open_hw_target" >> flash.tcl
149         echo "current_hw_device [lindex [get_hw_devices] 0]" >> flash.tcl
150         echo "refresh_hw_device -update_hw_probes false [current_hw_device]" >> flash.tcl
151         echo "create_hw_cfgmem -hw_device [current_hw_device] [lindex [get_cfgmem_parts {mt25qu01g-spi-x1_x2_x4_x8}] 0]" >> flash.tcl
152         echo "current_hw_cfgmem -hw_device [current_hw_device] [get_property PROGRAM.HW_CFGMEM [current_hw_device]]" >> flash.tcl
153         echo "set_property PROGRAM.FILES [list \"$(PROJECT)_primary.mcs\" \"$(PROJECT)_secondary.mcs\"] [current_hw_cfgmem]" >> flash.tcl
154         echo "set_property PROGRAM.PRM_FILES [list \"$(PROJECT)_primary.prm\" \"$(PROJECT)_secondary.prm\"] [current_hw_cfgmem]" >> flash.tcl
155         echo "set_property PROGRAM.ERASE 1 [current_hw_cfgmem]" >> flash.tcl
156         echo "set_property PROGRAM.CFG_PROGRAM 1 [current_hw_cfgmem]" >> flash.tcl
157         echo "set_property PROGRAM.VERIFY 1 [current_hw_cfgmem]" >> flash.tcl
158         echo "set_property PROGRAM.CHECKSUM 0 [current_hw_cfgmem]" >> flash.tcl
159         echo "set_property PROGRAM.ADDRESS_RANGE {use_file} [current_hw_cfgmem]" >> flash.tcl
160         echo "set_property PROGRAM.UNUSED_PIN_TERMINATION {pull-none} [current_hw_cfgmem]" >> flash.tcl
161         echo "create_hw_bitstream -hw_device [current_hw_device] [get_property PROGRAM.HW_CFGMEM_BITFILE [current_hw_device]]" >> flash.tcl
162         echo "program_hw_devices [current_hw_device]" >> flash.tcl
163         echo "refresh_hw_device [current_hw_device]" >> flash.tcl
164         echo "program_hw_cfgmem -hw_cfgmem [current_hw_cfgmem]" >> flash.tcl
165         echo "boot_hw_device [current_hw_device]" >> flash.tcl
166         echo "exit" >> flash.tcl
167         vivado -nojournal -nolog -mode batch -source flash.tcl