9 from mibuild
.tools
import write_to_file
10 from migen
.util
.misc
import autotype
11 from migen
.fhdl
import simplify
13 from misoclib
.soc
import cpuif
14 from misoclib
.mem
.sdram
.phy
import initsequence
16 from misoc_import
import misoc_import
20 parser
= argparse
.ArgumentParser(formatter_class
=argparse
.RawDescriptionHelpFormatter
,
22 MiSoC - a high performance and small footprint SoC based on Migen.
24 This program builds and/or loads MiSoC components.
25 One or several actions can be specified:
27 clean delete previous build(s).
28 build-bitstream build FPGA bitstream. Implies build-bios on targets with
30 build-headers build software header files with CPU/CSR/IRQ/SDRAM_PHY definitions.
31 build-csr-csv save CSR map into CSV file.
32 build-bios build BIOS. Implies build-header.
34 load-bitstream load bitstream into volatile storage.
35 flash-bitstream load bitstream into non-volatile storage.
36 flash-bios load BIOS into non-volatile storage.
38 all clean, build-bitstream, build-bios, flash-bitstream, flash-bios.
40 Load/flash actions use the existing outputs, and do not trigger new builds.
43 parser
.add_argument("-t", "--target", default
="mlabs_video", help="SoC type to build")
44 parser
.add_argument("-s", "--sub-target", default
="", help="variant of the SoC type to build")
45 parser
.add_argument("-p", "--platform", default
=None, help="platform to build for")
46 parser
.add_argument("-Ot", "--target-option", default
=[], nargs
=2, action
="append", help="set target-specific option")
47 parser
.add_argument("-Op", "--platform-option", default
=[], nargs
=2, action
="append", help="set platform-specific option")
48 parser
.add_argument("-X", "--external", default
="", help="use external directory for targets, platforms and imports")
49 parser
.add_argument("--csr_csv", default
="csr.csv", help="CSV file to save the CSR map into")
51 parser
.add_argument("-d", "--decorate", default
=[], action
="append", help="apply simplification decorator to top-level")
52 parser
.add_argument("-Ob", "--build-option", default
=[], nargs
=2, action
="append", help="set build option")
53 parser
.add_argument("-f", "--flash-proxy-dir", default
=None, help="set search directory for flash proxy bitstreams")
55 parser
.add_argument("action", nargs
="+", help="specify an action")
57 return parser
.parse_args()
59 if __name__
== "__main__":
63 external_platform
= ""
65 external_target
= os
.path
.join(args
.external
, "targets")
66 external_platform
= os
.path
.join(args
.external
, "platforms")
67 sys
.path
.insert(0, os
.path
.abspath(args
.external
))
69 # create top-level SoC object
70 target_module
= misoc_import("targets", external_target
, args
.target
)
72 top_class
= getattr(target_module
, args
.sub_target
)
74 top_class
= target_module
.default_subtarget
76 if args
.platform
is None:
77 if hasattr(top_class
, "default_platform"):
78 platform_name
= top_class
.default_platform
80 raise ValueError("Target has no default platform, specify a platform with -p your_platform")
82 platform_name
= args
.platform
83 platform_module
= misoc_import("mibuild.platforms", external_platform
, platform_name
)
84 platform_kwargs
= dict((k
, autotype(v
)) for k
, v
in args
.platform_option
)
85 platform
= platform_module
.Platform(**platform_kwargs
)
87 platform
.soc_ext_path
= os
.path
.abspath(args
.external
)
89 build_name
= args
.target
+ "-" + top_class
.__name
__.lower() + "-" + platform_name
90 top_kwargs
= dict((k
, autotype(v
)) for k
, v
in args
.target_option
)
91 soc
= top_class(platform
, **top_kwargs
)
93 memory_regions
= soc
.get_memory_regions()
94 csr_regions
= soc
.get_csr_regions()
97 action_list
= ["clean", "build-bitstream", "build-headers", "build-csr-csv", "build-bios",
98 "load-bitstream", "flash-bitstream", "flash-bios", "all"]
99 actions
= {k
: False for k
in action_list
}
100 for action
in args
.action
:
101 if action
in actions
:
102 actions
[action
] = True
104 print("Unknown action: {}. Valid actions are:".format(action
))
105 for a
in action_list
:
111 / |/ / (_) / __/__ / ___/
112 / /|_/ / / / _\ \/ _ \/ /__
113 /_/ /_/ /_/ /___/\___/\___/
115 a high performance and small footprint SoC based on Migen
117 ====== Building for: ======
122 ===========================""".format(platform_name
, args
.target
, top_class
.__name
__, soc
.cpu_type
))
126 actions
["clean"] = True
127 actions
["build-bitstream"] = True
128 actions
["build-bios"] = True
129 if not actions
["load-bitstream"]:
130 actions
["flash-bitstream"] = True
131 if not soc
.integrated_rom_size
:
132 actions
["flash-bios"] = True
133 if actions
["build-bitstream"] and soc
.integrated_rom_size
:
134 actions
["build-bios"] = True
135 if actions
["build-bios"]:
136 actions
["build-headers"] = True
139 subprocess
.check_call("rm -rvf build/*", shell
=True) # Need shell for the build/* globbing
140 subprocess
.check_call(["make", "-C", os
.path
.join("software", "libcompiler-rt"), "clean"])
141 subprocess
.check_call(["make", "-C", os
.path
.join("software", "libbase"), "clean"])
142 subprocess
.check_call(["make", "-C", os
.path
.join("software", "libnet"), "clean"])
143 subprocess
.check_call(["make", "-C", os
.path
.join("software", "bios"), "clean"])
145 if actions
["build-headers"]:
153 """.format(platform_name
, args
.target
, top_class
.__name
__, soc
.cpu_type
)
154 genhdir
= os
.path
.join("software", "include", "generated")
155 if soc
.cpu_type
!= "none":
156 cpu_mak
= cpuif
.get_cpu_mak(soc
.cpu_type
)
157 write_to_file(os
.path
.join(genhdir
, "cpu.mak"), cpu_mak
)
158 linker_output_format
= cpuif
.get_linker_output_format(soc
.cpu_type
)
159 write_to_file(os
.path
.join(genhdir
, "output_format.ld"), linker_output_format
)
161 linker_regions
= cpuif
.get_linker_regions(memory_regions
)
162 write_to_file(os
.path
.join(genhdir
, "regions.ld"), boilerplate
+ linker_regions
)
164 for sdram_phy
in ["sdrphy", "ddrphy"]:
165 if hasattr(soc
, sdram_phy
):
166 sdram_phy_header
= initsequence
.get_sdram_phy_header(getattr(soc
, sdram_phy
).settings
)
167 write_to_file(os
.path
.join(genhdir
, "sdram_phy.h"), boilerplate
+ sdram_phy_header
)
168 mem_header
= cpuif
.get_mem_header(memory_regions
, getattr(soc
, "flash_boot_address", None))
169 write_to_file(os
.path
.join(genhdir
, "mem.h"), boilerplate
+ mem_header
)
170 csr_header
= cpuif
.get_csr_header(csr_regions
, soc
.get_constants())
171 write_to_file(os
.path
.join(genhdir
, "csr.h"), boilerplate
+ csr_header
)
173 if actions
["build-csr-csv"]:
174 csr_csv
= cpuif
.get_csr_csv(csr_regions
)
175 write_to_file(args
.csr_csv
, csr_csv
)
177 if actions
["build-bios"]:
178 ret
= subprocess
.call(["make", "-C", os
.path
.join("software", "bios")])
180 raise OSError("BIOS build failed")
182 bios_file
= os
.path
.join("software", "bios", "bios.bin")
184 if actions
["build-bitstream"]:
185 if soc
.integrated_rom_size
:
186 with
open(bios_file
, "rb") as boot_file
:
189 w
= boot_file
.read(4)
192 boot_data
.append(struct
.unpack(">I", w
)[0])
193 soc
.init_rom(boot_data
)
195 for decorator
in args
.decorate
:
196 soc
= getattr(simplify
, decorator
)(soc
)
197 build_kwargs
= dict((k
, autotype(v
)) for k
, v
in args
.build_option
)
198 vns
= platform
.build(soc
, build_name
=build_name
, **build_kwargs
)
201 if actions
["load-bitstream"]:
202 prog
= platform
.create_programmer()
203 prog
.load_bitstream(os
.path
.join("build", build_name
+ platform
.bitstream_ext
))
205 if actions
["flash-bitstream"]:
206 prog
= platform
.create_programmer()
207 prog
.set_flash_proxy_dir(args
.flash_proxy_dir
)
208 if prog
.needs_bitreverse
:
209 flashbit
= os
.path
.join("build", build_name
+ ".fpg")
210 subprocess
.check_call([os
.path
.join("tools", "byteswap"),
211 os
.path
.join("build", build_name
+ ".bin"),
214 flashbit
= os
.path
.join("build", build_name
+ ".bin")
215 prog
.flash(0, flashbit
)
217 if actions
["flash-bios"]:
218 prog
= platform
.create_programmer()
219 prog
.set_flash_proxy_dir(args
.flash_proxy_dir
)
220 prog
.flash(soc
.cpu_reset_address
, bios_file
)