2013-07-04 13:19:39 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2014-08-09 02:39:38 -04:00
|
|
|
import sys, os, argparse, subprocess, struct
|
2013-07-04 13:19:39 -04:00
|
|
|
|
|
|
|
from mibuild.tools import write_to_file
|
2013-12-01 12:19:51 -05:00
|
|
|
from migen.util.misc import autotype
|
2013-12-12 11:37:46 -05:00
|
|
|
from migen.fhdl import simplify
|
2013-07-04 13:19:39 -04:00
|
|
|
|
2013-11-24 04:30:02 -05:00
|
|
|
from misoclib.gensoc import cpuif
|
2014-04-17 13:38:13 -04:00
|
|
|
from misoclib.sdramphy import initsequence
|
2013-07-04 13:19:39 -04:00
|
|
|
|
2014-08-09 02:39:38 -04:00
|
|
|
from misoc_import import misoc_import
|
|
|
|
|
2013-11-24 07:37:32 -05:00
|
|
|
def _get_args():
|
2014-02-15 08:01:50 -05:00
|
|
|
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
|
|
description="""\
|
|
|
|
MiSoC - a high performance and small footprint SoC based on Migen.
|
|
|
|
|
|
|
|
This program builds and/or loads MiSoC components.
|
|
|
|
One or several actions can be specified:
|
|
|
|
|
2014-04-14 11:21:34 -04:00
|
|
|
clean delete previous build(s).
|
2014-02-15 08:01:50 -05:00
|
|
|
build-bitstream build FPGA bitstream. Implies build-bios on targets with
|
|
|
|
integrated BIOS.
|
2014-05-14 04:24:56 -04:00
|
|
|
build-headers build software header files with CPU/CSR/IRQ/SDRAM_PHY definitions.
|
2014-02-15 08:01:50 -05:00
|
|
|
build-csr-csv save CSR map into CSV file.
|
|
|
|
build-bios build BIOS. Implies build-header.
|
|
|
|
|
|
|
|
load-bitstream load bitstream into volatile storage.
|
|
|
|
flash-bitstream load bitstream into non-volatile storage.
|
|
|
|
flash-bios load BIOS into non-volatile storage.
|
|
|
|
|
2014-04-14 11:21:34 -04:00
|
|
|
all clean, build-bitstream, build-bios, flash-bitstream, flash-bios.
|
2014-02-15 08:01:50 -05:00
|
|
|
|
|
|
|
Load/flash actions use the existing outputs, and do not trigger new builds.
|
|
|
|
""")
|
2013-11-24 07:37:32 -05:00
|
|
|
|
|
|
|
parser.add_argument("-t", "--target", default="mlabs_video", help="SoC type to build")
|
|
|
|
parser.add_argument("-s", "--sub-target", default="", help="variant of the SoC type to build")
|
2014-02-16 08:51:52 -05:00
|
|
|
parser.add_argument("-p", "--platform", default=None, help="platform to build for")
|
2013-12-01 12:19:51 -05:00
|
|
|
parser.add_argument("-Ot", "--target-option", default=[], nargs=2, action="append", help="set target-specific option")
|
2014-06-07 07:43:23 -04:00
|
|
|
parser.add_argument("-Op", "--platform-option", default=[], nargs=2, action="append", help="set platform-specific option")
|
2014-02-16 08:51:52 -05:00
|
|
|
parser.add_argument("-X", "--external", default="", help="use external directory for targets, platforms and imports")
|
2014-04-24 16:08:33 -04:00
|
|
|
parser.add_argument("--csr_csv", default="csr.csv", help="CSV file to save the CSR map into")
|
2014-10-17 05:14:35 -04:00
|
|
|
|
2013-12-12 11:37:46 -05:00
|
|
|
parser.add_argument("-d", "--decorate", default=[], action="append", help="apply simplification decorator to top-level")
|
2013-12-01 12:19:51 -05:00
|
|
|
parser.add_argument("-Ob", "--build-option", default=[], nargs=2, action="append", help="set build option")
|
2014-02-15 08:13:25 -05:00
|
|
|
parser.add_argument("-f", "--flash-proxy-dir", default=None, help="set search directory for flash proxy bitstreams")
|
2013-07-04 13:19:39 -04:00
|
|
|
|
2014-02-15 08:01:50 -05:00
|
|
|
parser.add_argument("action", nargs="+", help="specify an action")
|
2013-07-04 13:19:39 -04:00
|
|
|
|
2013-11-24 07:37:32 -05:00
|
|
|
return parser.parse_args()
|
2013-07-04 13:19:39 -04:00
|
|
|
|
2014-02-15 08:01:50 -05:00
|
|
|
if __name__ == "__main__":
|
2013-11-24 07:37:32 -05:00
|
|
|
args = _get_args()
|
2013-07-04 13:19:39 -04:00
|
|
|
|
2014-02-16 08:51:52 -05:00
|
|
|
external_target = ""
|
|
|
|
external_platform = ""
|
|
|
|
if args.external:
|
|
|
|
external_target = os.path.join(args.external, "targets")
|
|
|
|
external_platform = os.path.join(args.external, "platforms")
|
|
|
|
sys.path.insert(1, os.path.abspath(args.external))
|
|
|
|
|
2014-02-15 08:01:50 -05:00
|
|
|
# create top-level SoC object
|
2014-08-09 02:39:38 -04:00
|
|
|
target_module = misoc_import("targets", external_target, args.target)
|
2013-11-24 07:37:32 -05:00
|
|
|
if args.sub_target:
|
|
|
|
top_class = getattr(target_module, args.sub_target)
|
|
|
|
else:
|
2014-02-16 08:51:52 -05:00
|
|
|
top_class = target_module.default_subtarget
|
|
|
|
|
|
|
|
if args.platform is None:
|
|
|
|
platform_name = top_class.default_platform
|
|
|
|
else:
|
|
|
|
platform_name = args.platform
|
2014-08-09 02:39:38 -04:00
|
|
|
platform_module = misoc_import("mibuild.platforms", external_platform, platform_name)
|
2014-06-07 07:43:23 -04:00
|
|
|
platform_kwargs = dict((k, autotype(v)) for k, v in args.platform_option)
|
|
|
|
platform = platform_module.Platform(**platform_kwargs)
|
2014-04-18 07:51:56 -04:00
|
|
|
if args.external:
|
|
|
|
platform.soc_ext_path = os.path.abspath(args.external)
|
2014-02-16 08:51:52 -05:00
|
|
|
|
|
|
|
build_name = top_class.__name__.lower() + "-" + platform_name
|
2013-12-01 12:19:51 -05:00
|
|
|
top_kwargs = dict((k, autotype(v)) for k, v in args.target_option)
|
2013-11-24 07:59:15 -05:00
|
|
|
soc = top_class(platform, **top_kwargs)
|
2013-11-24 16:14:46 -05:00
|
|
|
soc.finalize()
|
|
|
|
|
2014-02-15 08:01:50 -05:00
|
|
|
# decode actions
|
2014-04-14 11:21:34 -04:00
|
|
|
action_list = ["clean", "build-bitstream", "build-headers", "build-csr-csv", "build-bios",
|
2014-02-15 08:01:50 -05:00
|
|
|
"load-bitstream", "flash-bitstream", "flash-bios", "all"]
|
|
|
|
actions = {k: False for k in action_list}
|
|
|
|
for action in args.action:
|
|
|
|
if action in actions:
|
|
|
|
actions[action] = True
|
|
|
|
else:
|
|
|
|
print("Unknown action: "+action+". Valid actions are:")
|
|
|
|
for a in action_list:
|
|
|
|
print(" "+a)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2014-02-16 08:51:52 -05:00
|
|
|
print("""\
|
|
|
|
__ ___ _ ____ _____
|
|
|
|
/ |/ / (_) / __/__ / ___/
|
|
|
|
/ /|_/ / / / _\ \/ _ \/ /__
|
|
|
|
/_/ /_/ /_/ /___/\___/\___/
|
|
|
|
|
|
|
|
a high performance and small footprint SoC based on Migen
|
|
|
|
|
|
|
|
====== Building for: ======
|
|
|
|
Platform: {}
|
|
|
|
Target: {}
|
|
|
|
Subtarget: {}
|
2014-05-14 04:24:56 -04:00
|
|
|
CPU type: {}
|
|
|
|
===========================""".format(platform_name, args.target, top_class.__name__, soc.cpu_type))
|
2014-02-16 08:51:52 -05:00
|
|
|
|
2014-02-15 08:01:50 -05:00
|
|
|
# dependencies
|
|
|
|
if actions["all"]:
|
2014-04-14 11:21:34 -04:00
|
|
|
actions["clean"] = True
|
2014-02-15 08:01:50 -05:00
|
|
|
actions["build-bitstream"] = True
|
|
|
|
actions["build-bios"] = True
|
|
|
|
actions["flash-bitstream"] = True
|
|
|
|
actions["flash-bios"] = True
|
|
|
|
if actions["build-bitstream"] and hasattr(soc, "init_bios_memory"):
|
|
|
|
actions["build-bios"] = True
|
|
|
|
if actions["build-bios"]:
|
|
|
|
actions["build-headers"] = True
|
|
|
|
|
2014-04-14 11:21:34 -04:00
|
|
|
if actions["clean"]:
|
|
|
|
subprocess.call(["rm", "-rf", "build/*"])
|
2014-05-14 04:24:56 -04:00
|
|
|
subprocess.call(["make", "-C", "software/libcompiler-rt", "clean"])
|
|
|
|
subprocess.call(["make", "-C", "software/libbase", "clean"])
|
|
|
|
subprocess.call(["make", "-C", "software/libnet", "clean"])
|
|
|
|
subprocess.call(["make", "-C", "software/bios", "clean"])
|
2014-04-14 11:21:34 -04:00
|
|
|
|
2014-02-15 08:01:50 -05:00
|
|
|
if actions["build-headers"]:
|
2013-11-24 07:37:32 -05:00
|
|
|
boilerplate = """/*
|
2014-02-16 08:51:52 -05:00
|
|
|
* Platform: {}
|
|
|
|
* Target: {}
|
2013-11-24 07:37:32 -05:00
|
|
|
* Subtarget: {}
|
2014-05-14 04:24:56 -04:00
|
|
|
* CPU type: {}
|
2013-11-24 07:37:32 -05:00
|
|
|
*/
|
|
|
|
|
2014-05-14 04:24:56 -04:00
|
|
|
""".format(platform_name, args.target, top_class.__name__, soc.cpu_type)
|
|
|
|
cpu_mak = cpuif.get_cpu_mak(soc.cpu_type)
|
|
|
|
write_to_file("software/include/generated/cpu.mak", cpu_mak)
|
|
|
|
linker_output_format = cpuif.get_linker_output_format(soc.cpu_type)
|
|
|
|
write_to_file("software/include/generated/output_format.ld", linker_output_format)
|
|
|
|
|
|
|
|
linker_regions = cpuif.get_linker_regions(soc.cpu_memory_regions)
|
|
|
|
write_to_file("software/include/generated/regions.ld", boilerplate + linker_regions)
|
2014-02-21 11:55:05 -05:00
|
|
|
try:
|
|
|
|
flash_boot_address = soc.flash_boot_address
|
|
|
|
except AttributeError:
|
|
|
|
flash_boot_address = None
|
|
|
|
mem_header = cpuif.get_mem_header(soc.cpu_memory_regions, flash_boot_address)
|
|
|
|
write_to_file("software/include/generated/mem.h", boilerplate + mem_header)
|
2013-10-20 18:04:26 -04:00
|
|
|
csr_header = cpuif.get_csr_header(soc.csr_base, soc.csrbankarray, soc.interrupt_map)
|
2013-11-24 13:50:17 -05:00
|
|
|
write_to_file("software/include/generated/csr.h", boilerplate + csr_header)
|
2014-04-17 13:38:13 -04:00
|
|
|
for sdram_phy in ["sdrphy", "ddrphy"]:
|
|
|
|
if hasattr(soc, sdram_phy):
|
|
|
|
sdram_phy_header = initsequence.get_sdram_phy_header(getattr(soc, sdram_phy))
|
|
|
|
write_to_file("software/include/generated/sdram_phy.h", boilerplate + sdram_phy_header)
|
2014-02-15 08:01:50 -05:00
|
|
|
|
|
|
|
if actions["build-csr-csv"]:
|
2013-10-20 18:04:26 -04:00
|
|
|
csr_csv = cpuif.get_csr_csv(soc.csr_base, soc.csrbankarray)
|
2013-11-24 07:37:32 -05:00
|
|
|
write_to_file(args.csr_csv, csr_csv)
|
2013-07-04 13:19:39 -04:00
|
|
|
|
2014-02-15 08:01:50 -05:00
|
|
|
if actions["build-bios"]:
|
2013-11-24 16:14:46 -05:00
|
|
|
ret = subprocess.call(["make", "-C", "software/bios"])
|
|
|
|
if ret:
|
|
|
|
raise OSError("BIOS build failed")
|
|
|
|
|
2014-02-15 08:01:50 -05:00
|
|
|
if actions["build-bitstream"]:
|
2014-05-19 13:49:32 -04:00
|
|
|
if hasattr(soc, "init_bios_memory"):
|
|
|
|
with open("software/bios/bios.bin", "rb") as bios_file:
|
|
|
|
bios_data = []
|
|
|
|
while True:
|
|
|
|
w = bios_file.read(4)
|
|
|
|
if not w:
|
|
|
|
break
|
|
|
|
bios_data.append(struct.unpack(">I", w)[0])
|
|
|
|
soc.init_bios_memory(bios_data)
|
|
|
|
|
2014-02-15 08:01:50 -05:00
|
|
|
for decorator in args.decorate:
|
|
|
|
soc = getattr(simplify, decorator)(soc)
|
2013-12-01 12:19:51 -05:00
|
|
|
build_kwargs = dict((k, autotype(v)) for k, v in args.build_option)
|
|
|
|
platform.build(soc, build_name=build_name, **build_kwargs)
|
2013-07-04 13:19:39 -04:00
|
|
|
|
2014-02-15 08:01:50 -05:00
|
|
|
if actions["load-bitstream"] or actions["flash-bitstream"] or actions["flash-bios"]:
|
2014-07-31 12:17:32 -04:00
|
|
|
prog = platform.create_programmer()
|
2014-02-15 08:01:50 -05:00
|
|
|
if actions["load-bitstream"]:
|
2014-04-14 11:23:04 -04:00
|
|
|
prog.load_bitstream("build/" + build_name + platform.bitstream_ext)
|
2014-02-15 08:01:50 -05:00
|
|
|
if actions["flash-bitstream"]:
|
2014-08-09 02:38:56 -04:00
|
|
|
prog.set_flash_proxy_dir(args.flash_proxy_dir)
|
2014-02-15 08:01:50 -05:00
|
|
|
if prog.needs_bitreverse:
|
|
|
|
flashbit = "build/" + build_name + ".fpg"
|
|
|
|
subprocess.call(["tools/byteswap",
|
|
|
|
"build/" + build_name + ".bin",
|
|
|
|
flashbit])
|
|
|
|
else:
|
|
|
|
flashbit = "build/" + build_name + ".bin"
|
|
|
|
prog.flash(0, flashbit)
|
|
|
|
if actions["flash-bios"]:
|
2014-08-21 07:32:32 -04:00
|
|
|
prog.set_flash_proxy_dir(args.flash_proxy_dir)
|
2014-02-15 08:01:50 -05:00
|
|
|
prog.flash(soc.cpu_reset_address, "software/bios/bios.bin")
|