3 import sys
, argparse
, importlib
, subprocess
, struct
5 from mibuild
.tools
import write_to_file
6 from migen
.util
.misc
import autotype
7 from migen
.fhdl
import simplify
9 from misoclib
.gensoc
import cpuif
10 from misoclib
.s6ddrphy
import initsequence
14 parser
= argparse
.ArgumentParser(formatter_class
=argparse
.RawDescriptionHelpFormatter
,
16 MiSoC - a high performance and small footprint SoC based on Migen.
18 This program builds and/or loads MiSoC components.
19 One or several actions can be specified:
21 build-bitstream build FPGA bitstream. Implies build-bios on targets with
23 build-headers build software header files with CSR/IRQ/SDRAM_PHY definitions.
24 build-csr-csv save CSR map into CSV file.
25 build-bios build BIOS. Implies build-header.
27 load-bitstream load bitstream into volatile storage.
28 flash-bitstream load bitstream into non-volatile storage.
29 flash-bios load BIOS into non-volatile storage.
31 all build-bitstream, build-bios, flash-bitstream, flash-bios.
33 Load/flash actions use the existing outputs, and do not trigger new builds.
36 parser
.add_argument("-p", "--platform", default
="mixxeo", help="platform to build for")
37 parser
.add_argument("-t", "--target", default
="mlabs_video", help="SoC type to build")
38 parser
.add_argument("-s", "--sub-target", default
="", help="variant of the SoC type to build")
39 parser
.add_argument("-Ot", "--target-option", default
=[], nargs
=2, action
="append", help="set target-specific option")
40 parser
.add_argument("-Xp", "--external-platform", default
="", help="use external platform file in the specified path")
41 parser
.add_argument("-Xt", "--external-target", default
="", help="use external target file in the specified path")
43 parser
.add_argument("-d", "--decorate", default
=[], action
="append", help="apply simplification decorator to top-level")
44 parser
.add_argument("-Ob", "--build-option", default
=[], nargs
=2, action
="append", help="set build option")
46 parser
.add_argument("action", nargs
="+", help="specify an action")
48 return parser
.parse_args()
50 def _misoc_import(default
, external
, name
):
53 del sys
.modules
[name
] # force external path search
56 loader
= importlib
.find_loader(name
, [external
])
58 raise ImportError("Module not found: "+name
)
59 return loader
.load_module()
61 return importlib
.import_module(default
+ "." + name
)
63 if __name__
== "__main__":
66 # create top-level SoC object
67 platform_module
= _misoc_import("mibuild.platforms", args
.external_platform
, args
.platform
)
68 target_module
= _misoc_import("targets", args
.external_target
, args
.target
)
69 platform
= platform_module
.Platform()
71 top_class
= getattr(target_module
, args
.sub_target
)
73 top_class
= target_module
.get_default_subtarget(platform
)
74 build_name
= top_class
.__name
__.lower() + "-" + args
.platform
75 top_kwargs
= dict((k
, autotype(v
)) for k
, v
in args
.target_option
)
76 soc
= top_class(platform
, **top_kwargs
)
80 action_list
= ["build-bitstream", "build-headers", "build-csr-csv", "build-bios",
81 "load-bitstream", "flash-bitstream", "flash-bios", "all"]
82 actions
= {k
: False for k
in action_list
}
83 for action
in args
.action
:
85 actions
[action
] = True
87 print("Unknown action: "+action
+". Valid actions are:")
94 actions
["build-bitstream"] = True
95 actions
["build-bios"] = True
96 actions
["flash-bitstream"] = True
97 actions
["flash-bios"] = True
98 if actions
["build-bitstream"] and hasattr(soc
, "init_bios_memory"):
99 actions
["build-bios"] = True
100 if actions
["build-bios"]:
101 actions
["build-headers"] = True
103 if actions
["build-headers"]:
110 """.format(args
.platform
, args
.target
, top_class
.__name
__)
111 linker_header
= cpuif
.get_linker_regions(soc
.cpu_memory_regions
)
112 write_to_file("software/include/generated/regions.ld", boilerplate
+ linker_header
)
113 csr_header
= cpuif
.get_csr_header(soc
.csr_base
, soc
.csrbankarray
, soc
.interrupt_map
)
114 write_to_file("software/include/generated/csr.h", boilerplate
+ csr_header
)
115 if hasattr(soc
, "ddrphy"):
116 sdram_phy_header
= initsequence
.get_sdram_phy_header(soc
.ddrphy
)
117 write_to_file("software/include/generated/sdram_phy.h", boilerplate
+ sdram_phy_header
)
119 if actions
["build-csr-csv"]:
120 csr_csv
= cpuif
.get_csr_csv(soc
.csr_base
, soc
.csrbankarray
)
121 write_to_file(args
.csr_csv
, csr_csv
)
123 if actions
["build-bios"]:
124 ret
= subprocess
.call(["make", "-C", "software/bios"])
126 raise OSError("BIOS build failed")
128 if hasattr(soc
, "init_bios_memory"):
129 with
open("software/bios/bios.bin", "rb") as bios_file
:
132 w
= bios_file
.read(4)
135 bios_data
.append(struct
.unpack(">I", w
)[0])
136 soc
.init_bios_memory(bios_data
)
138 if actions
["build-bitstream"]:
139 for decorator
in args
.decorate
:
140 soc
= getattr(simplify
, decorator
)(soc
)
141 build_kwargs
= dict((k
, autotype(v
)) for k
, v
in args
.build_option
)
142 platform
.build(soc
, build_name
=build_name
, **build_kwargs
)
144 if actions
["load-bitstream"] or actions
["flash-bitstream"] or actions
["flash-bios"]:
145 prog
= programmer
.create_programmer(platform
.name
, None)
146 if actions
["load-bitstream"]:
147 prog
.load_bitstream("build/" + build_name
+ ".bit")
148 if actions
["flash-bitstream"]:
149 if prog
.needs_bitreverse
:
150 flashbit
= "build/" + build_name
+ ".fpg"
151 subprocess
.call(["tools/byteswap",
152 "build/" + build_name
+ ".bin",
155 flashbit
= "build/" + build_name
+ ".bin"
156 prog
.flash(0, flashbit
)
157 if actions
["flash-bios"]:
158 prog
.flash(soc
.cpu_reset_address
, "software/bios/bios.bin")