10 from mibuild
.tools
import write_to_file
11 from migen
.util
.misc
import autotype
12 from migen
.fhdl
import simplify
14 from misoclib
.soc
import cpuif
15 from misoclib
.mem
.sdram
.phy
import initsequence
17 from misoc_import
import misoc_import
21 parser
= argparse
.ArgumentParser(formatter_class
=argparse
.RawDescriptionHelpFormatter
,
23 MiSoC - a high performance and small footprint SoC based on Migen.
25 This program builds and/or loads MiSoC components.
26 One or several actions can be specified:
28 clean delete previous build(s).
29 build-bitstream build FPGA bitstream. Implies build-bios on targets with
31 build-headers build software header files with CPU/CSR/IRQ/SDRAM_PHY definitions.
32 build-csr-csv save CSR map into CSV file.
33 build-bios build BIOS. Implies build-header.
35 load-bitstream load bitstream into volatile storage.
36 flash-bitstream load bitstream into non-volatile storage.
37 flash-bios load BIOS into non-volatile storage.
39 all clean, build-bitstream, build-bios, flash-bitstream, flash-bios.
41 Load/flash actions use the existing outputs, and do not trigger new builds.
44 parser
.add_argument("-t", "--target", default
="mlabs_video", help="SoC type to build")
45 parser
.add_argument("-s", "--sub-target", default
="", help="variant of the SoC type to build")
46 parser
.add_argument("-p", "--platform", default
=None, help="platform to build for")
47 parser
.add_argument("-Ot", "--target-option", default
=[], nargs
=2, action
="append", help="set target-specific option")
48 parser
.add_argument("-Op", "--platform-option", default
=[], nargs
=2, action
="append", help="set platform-specific option")
49 parser
.add_argument("-X", "--external", default
="", help="use external directory for targets, platforms and imports")
50 parser
.add_argument("--csr_csv", default
="csr.csv", help="CSV file to save the CSR map into")
52 parser
.add_argument("-d", "--decorate", default
=[], action
="append", help="apply simplification decorator to top-level")
53 parser
.add_argument("-Ob", "--build-option", default
=[], nargs
=2, action
="append", help="set build option")
54 parser
.add_argument("-f", "--flash-proxy-dir", default
=None, help="set search directory for flash proxy bitstreams")
56 parser
.add_argument("action", nargs
="+", help="specify an action")
58 return parser
.parse_args()
60 if __name__
== "__main__":
64 external_platform
= ""
66 external_target
= os
.path
.join(args
.external
, "targets")
67 external_platform
= os
.path
.join(args
.external
, "platforms")
68 sys
.path
.insert(0, os
.path
.abspath(args
.external
))
70 # create top-level SoC object
71 target_module
= misoc_import("targets", external_target
, args
.target
)
73 top_class
= getattr(target_module
, args
.sub_target
)
75 top_class
= target_module
.default_subtarget
77 if args
.platform
is None:
78 if hasattr(top_class
, "default_platform"):
79 platform_name
= top_class
.default_platform
81 raise ValueError("Target has no default platform, specify a platform with -p your_platform")
83 platform_name
= args
.platform
84 platform_module
= misoc_import("mibuild.platforms", external_platform
, platform_name
)
85 platform_kwargs
= dict((k
, autotype(v
)) for k
, v
in args
.platform_option
)
86 platform
= platform_module
.Platform(**platform_kwargs
)
88 platform
.soc_ext_path
= os
.path
.abspath(args
.external
)
90 build_name
= args
.target
+ "-" + top_class
.__name
__.lower() + "-" + platform_name
91 top_kwargs
= dict((k
, autotype(v
)) for k
, v
in args
.target_option
)
92 soc
= top_class(platform
, **top_kwargs
)
94 memory_regions
= soc
.get_memory_regions()
95 csr_regions
= soc
.get_csr_regions()
98 action_list
= ["clean", "build-bitstream", "build-headers", "build-csr-csv", "build-bios",
99 "load-bitstream", "flash-bitstream", "flash-bios", "all"]
100 actions
= {k
: False for k
in action_list
}
101 for action
in args
.action
:
102 if action
in actions
:
103 actions
[action
] = True
105 print("Unknown action: {}. Valid actions are:".format(action
))
106 for a
in action_list
:
112 / |/ / (_) / __/__ / ___/
113 / /|_/ / / / _\ \/ _ \/ /__
114 /_/ /_/ /_/ /___/\___/\___/
116 a high performance and small footprint SoC based on Migen
118 ====== Building for: ======
123 ===========================""".format(platform_name
, args
.target
, top_class
.__name
__, soc
.cpu_type
))
127 actions
["clean"] = True
128 actions
["build-bitstream"] = True
129 actions
["build-bios"] = True
130 if not actions
["load-bitstream"]:
131 actions
["flash-bitstream"] = True
132 if not soc
.integrated_rom_size
:
133 actions
["flash-bios"] = True
134 if actions
["build-bitstream"] and soc
.integrated_rom_size
:
135 actions
["build-bios"] = True
136 if actions
["build-bios"]:
137 actions
["build-headers"] = True
140 shutil
.rmtree("build") # Need shell for the build/* globbing
142 subprocess
.check_call(["make", "-C", os
.path
.join("software", "libcompiler-rt"), "clean"])
143 subprocess
.check_call(["make", "-C", os
.path
.join("software", "libbase"), "clean"])
144 subprocess
.check_call(["make", "-C", os
.path
.join("software", "libnet"), "clean"])
145 subprocess
.check_call(["make", "-C", os
.path
.join("software", "bios"), "clean"])
147 if actions
["build-headers"]:
155 """.format(platform_name
, args
.target
, top_class
.__name
__, soc
.cpu_type
)
156 genhdir
= os
.path
.join("software", "include", "generated")
157 if soc
.cpu_type
!= "none":
158 cpu_mak
= cpuif
.get_cpu_mak(soc
.cpu_type
)
159 write_to_file(os
.path
.join(genhdir
, "cpu.mak"), cpu_mak
)
160 linker_output_format
= cpuif
.get_linker_output_format(soc
.cpu_type
)
161 write_to_file(os
.path
.join(genhdir
, "output_format.ld"), linker_output_format
)
163 linker_regions
= cpuif
.get_linker_regions(memory_regions
)
164 write_to_file(os
.path
.join(genhdir
, "regions.ld"), boilerplate
+ linker_regions
)
166 for sdram_phy
in ["sdrphy", "ddrphy"]:
167 if hasattr(soc
, sdram_phy
):
168 sdram_phy_header
= initsequence
.get_sdram_phy_header(getattr(soc
, sdram_phy
).settings
)
169 write_to_file(os
.path
.join(genhdir
, "sdram_phy.h"), boilerplate
+ sdram_phy_header
)
170 mem_header
= cpuif
.get_mem_header(memory_regions
, getattr(soc
, "flash_boot_address", None))
171 write_to_file(os
.path
.join(genhdir
, "mem.h"), boilerplate
+ mem_header
)
172 csr_header
= cpuif
.get_csr_header(csr_regions
, soc
.get_constants())
173 write_to_file(os
.path
.join(genhdir
, "csr.h"), boilerplate
+ csr_header
)
175 if actions
["build-csr-csv"]:
176 csr_csv
= cpuif
.get_csr_csv(csr_regions
)
177 write_to_file(args
.csr_csv
, csr_csv
)
179 if actions
["build-bios"]:
180 ret
= subprocess
.call(["make", "-C", os
.path
.join("software", "bios")])
182 raise OSError("BIOS build failed")
184 bios_file
= os
.path
.join("software", "bios", "bios.bin")
186 if actions
["build-bitstream"]:
187 if soc
.integrated_rom_size
:
188 with
open(bios_file
, "rb") as boot_file
:
191 w
= boot_file
.read(4)
194 boot_data
.append(struct
.unpack(">I", w
)[0])
195 soc
.init_rom(boot_data
)
197 for decorator
in args
.decorate
:
198 soc
= getattr(simplify
, decorator
)(soc
)
199 build_kwargs
= dict((k
, autotype(v
)) for k
, v
in args
.build_option
)
200 vns
= platform
.build(soc
, build_name
=build_name
, **build_kwargs
)
203 if actions
["load-bitstream"]:
204 prog
= platform
.create_programmer()
205 prog
.load_bitstream(os
.path
.join("build", build_name
+ platform
.bitstream_ext
))
207 if actions
["flash-bitstream"]:
208 prog
= platform
.create_programmer()
209 prog
.set_flash_proxy_dir(args
.flash_proxy_dir
)
210 if prog
.needs_bitreverse
:
211 flashbit
= os
.path
.join("build", build_name
+ ".fpg")
212 subprocess
.check_call([os
.path
.join("tools", "byteswap"),
213 os
.path
.join("build", build_name
+ ".bin"),
216 flashbit
= os
.path
.join("build", build_name
+ ".bin")
217 prog
.flash(0, flashbit
)
219 if actions
["flash-bios"]:
220 prog
= platform
.create_programmer()
221 prog
.set_flash_proxy_dir(args
.flash_proxy_dir
)
222 prog
.flash(soc
.cpu_reset_address
, bios_file
)