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