global: pep8 (E302)
[litex.git] / make.py
1 #!/usr/bin/env python3
2
3 import sys, os, argparse, subprocess, struct
4
5 from mibuild.tools import write_to_file
6 from migen.util.misc import autotype
7 from migen.fhdl import simplify
8
9 from misoclib.soc import cpuif
10 from misoclib.mem.sdram.phy import initsequence
11
12 from misoc_import import misoc_import
13
14
15 def _get_args():
16 parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
17 description="""\
18 MiSoC - a high performance and small footprint SoC based on Migen.
19
20 This program builds and/or loads MiSoC components.
21 One or several actions can be specified:
22
23 clean delete previous build(s).
24 build-bitstream build FPGA bitstream. Implies build-bios on targets with
25 integrated BIOS.
26 build-headers build software header files with CPU/CSR/IRQ/SDRAM_PHY definitions.
27 build-csr-csv save CSR map into CSV file.
28 build-bios build BIOS. Implies build-header.
29
30 load-bitstream load bitstream into volatile storage.
31 flash-bitstream load bitstream into non-volatile storage.
32 flash-bios load BIOS into non-volatile storage.
33
34 all clean, build-bitstream, build-bios, flash-bitstream, flash-bios.
35
36 Load/flash actions use the existing outputs, and do not trigger new builds.
37 """)
38
39 parser.add_argument("-t", "--target", default="mlabs_video", help="SoC type to build")
40 parser.add_argument("-s", "--sub-target", default="", help="variant of the SoC type to build")
41 parser.add_argument("-p", "--platform", default=None, help="platform to build for")
42 parser.add_argument("-Ot", "--target-option", default=[], nargs=2, action="append", help="set target-specific option")
43 parser.add_argument("-Op", "--platform-option", default=[], nargs=2, action="append", help="set platform-specific option")
44 parser.add_argument("-X", "--external", default="", help="use external directory for targets, platforms and imports")
45 parser.add_argument("--csr_csv", default="csr.csv", help="CSV file to save the CSR map into")
46
47 parser.add_argument("-d", "--decorate", default=[], action="append", help="apply simplification decorator to top-level")
48 parser.add_argument("-Ob", "--build-option", default=[], nargs=2, action="append", help="set build option")
49 parser.add_argument("-f", "--flash-proxy-dir", default=None, help="set search directory for flash proxy bitstreams")
50
51 parser.add_argument("action", nargs="+", help="specify an action")
52
53 return parser.parse_args()
54
55 if __name__ == "__main__":
56 args = _get_args()
57
58 external_target = ""
59 external_platform = ""
60 if args.external:
61 external_target = os.path.join(args.external, "targets")
62 external_platform = os.path.join(args.external, "platforms")
63 sys.path.insert(1, os.path.abspath(args.external))
64
65 # create top-level SoC object
66 target_module = misoc_import("targets", external_target, args.target)
67 if args.sub_target:
68 top_class = getattr(target_module, args.sub_target)
69 else:
70 top_class = target_module.default_subtarget
71
72 if args.platform is None:
73 if hasattr(top_class, "default_platform"):
74 platform_name = top_class.default_platform
75 else:
76 raise ValueError("Target has no default platform, specify a platform with -p your_platform")
77 else:
78 platform_name = args.platform
79 platform_module = misoc_import("mibuild.platforms", external_platform, platform_name)
80 platform_kwargs = dict((k, autotype(v)) for k, v in args.platform_option)
81 platform = platform_module.Platform(**platform_kwargs)
82 if args.external:
83 platform.soc_ext_path = os.path.abspath(args.external)
84
85 build_name = args.target + "-" + top_class.__name__.lower() + "-" + platform_name
86 top_kwargs = dict((k, autotype(v)) for k, v in args.target_option)
87 soc = top_class(platform, **top_kwargs)
88 soc.finalize()
89 memory_regions = soc.get_memory_regions()
90 csr_regions = soc.get_csr_regions()
91
92 # decode actions
93 action_list = ["clean", "build-bitstream", "build-headers", "build-csr-csv", "build-bios",
94 "load-bitstream", "flash-bitstream", "flash-bios", "all"]
95 actions = {k: False for k in action_list}
96 for action in args.action:
97 if action in actions:
98 actions[action] = True
99 else:
100 print("Unknown action: {}. Valid actions are:".format(action))
101 for a in action_list:
102 print(" "+a)
103 sys.exit(1)
104
105 print("""\
106 __ ___ _ ____ _____
107 / |/ / (_) / __/__ / ___/
108 / /|_/ / / / _\ \/ _ \/ /__
109 /_/ /_/ /_/ /___/\___/\___/
110
111 a high performance and small footprint SoC based on Migen
112
113 ====== Building for: ======
114 Platform: {}
115 Target: {}
116 Subtarget: {}
117 CPU type: {}
118 ===========================""".format(platform_name, args.target, top_class.__name__, soc.cpu_type))
119
120 # dependencies
121 if actions["all"]:
122 actions["clean"] = True
123 actions["build-bitstream"] = True
124 actions["build-bios"] = True
125 if not actions["load-bitstream"]:
126 actions["flash-bitstream"] = True
127 if not soc.integrated_rom_size:
128 actions["flash-bios"] = True
129 if actions["build-bitstream"] and soc.integrated_rom_size:
130 actions["build-bios"] = True
131 if actions["build-bios"]:
132 actions["build-headers"] = True
133
134 if actions["clean"]:
135 subprocess.call(["rm", "-rf", "build/*"])
136 subprocess.call(["make", "-C", os.path.join("software", "libcompiler-rt"), "clean"])
137 subprocess.call(["make", "-C", os.path.join("software", "libbase"), "clean"])
138 subprocess.call(["make", "-C", os.path.join("software", "libnet"), "clean"])
139 subprocess.call(["make", "-C", os.path.join("software", "bios"), "clean"])
140
141 if actions["build-headers"]:
142 boilerplate = """/*
143 * Platform: {}
144 * Target: {}
145 * Subtarget: {}
146 * CPU type: {}
147 */
148
149 """.format(platform_name, args.target, top_class.__name__, soc.cpu_type)
150 genhdir = os.path.join("software", "include", "generated")
151 if soc.cpu_type != "none":
152 cpu_mak = cpuif.get_cpu_mak(soc.cpu_type)
153 write_to_file(os.path.join(genhdir, "cpu.mak"), cpu_mak)
154 linker_output_format = cpuif.get_linker_output_format(soc.cpu_type)
155 write_to_file(os.path.join(genhdir, "output_format.ld"), linker_output_format)
156
157 linker_regions = cpuif.get_linker_regions(memory_regions)
158 write_to_file(os.path.join(genhdir, "regions.ld"), boilerplate + linker_regions)
159
160 for sdram_phy in ["sdrphy", "ddrphy"]:
161 if hasattr(soc, sdram_phy):
162 sdram_phy_header = initsequence.get_sdram_phy_header(getattr(soc, sdram_phy).settings)
163 write_to_file(os.path.join(genhdir, "sdram_phy.h"), boilerplate + sdram_phy_header)
164 mem_header = cpuif.get_mem_header(memory_regions, getattr(soc, "flash_boot_address", None))
165 write_to_file(os.path.join(genhdir, "mem.h"), boilerplate + mem_header)
166 csr_header = cpuif.get_csr_header(csr_regions, soc.get_constants())
167 write_to_file(os.path.join(genhdir, "csr.h"), boilerplate + csr_header)
168
169 if actions["build-csr-csv"]:
170 csr_csv = cpuif.get_csr_csv(csr_regions)
171 write_to_file(args.csr_csv, csr_csv)
172
173 if actions["build-bios"]:
174 ret = subprocess.call(["make", "-C", os.path.join("software", "bios")])
175 if ret:
176 raise OSError("BIOS build failed")
177
178 bios_file = os.path.join("software", "bios", "bios.bin")
179
180 if actions["build-bitstream"]:
181 if soc.integrated_rom_size:
182 with open(bios_file, "rb") as boot_file:
183 boot_data = []
184 while True:
185 w = boot_file.read(4)
186 if not w:
187 break
188 boot_data.append(struct.unpack(">I", w)[0])
189 soc.init_rom(boot_data)
190
191 for decorator in args.decorate:
192 soc = getattr(simplify, decorator)(soc)
193 build_kwargs = dict((k, autotype(v)) for k, v in args.build_option)
194 vns = platform.build(soc, build_name=build_name, **build_kwargs)
195 soc.do_exit(vns)
196
197 if actions["load-bitstream"]:
198 prog = platform.create_programmer()
199 prog.load_bitstream(os.path.join("build", build_name + platform.bitstream_ext))
200
201 if actions["flash-bitstream"]:
202 prog = platform.create_programmer()
203 prog.set_flash_proxy_dir(args.flash_proxy_dir)
204 if prog.needs_bitreverse:
205 flashbit = os.path.join("build", build_name + ".fpg")
206 subprocess.call([os.path.join("tools", "byteswap"),
207 os.path.join("build", build_name + ".bin"),
208 flashbit])
209 else:
210 flashbit = os.path.join("build", build_name + ".bin")
211 prog.flash(0, flashbit)
212
213 if actions["flash-bios"]:
214 prog = platform.create_programmer()
215 prog.set_flash_proxy_dir(args.flash_proxy_dir)
216 prog.flash(soc.cpu_reset_address, bios_file)