|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import sys |
| 4 | +import os |
| 5 | +import argparse |
| 6 | +import subprocess |
| 7 | +import struct |
| 8 | +import importlib |
| 9 | + |
| 10 | +from mibuild.tools import write_to_file |
| 11 | +from migen.util.misc import autotype |
| 12 | +from migen.fhdl import verilog, edif |
| 13 | +from migen.fhdl.structure import _Fragment |
| 14 | +from migen.bank.description import CSRStatus |
| 15 | +from mibuild import tools |
| 16 | +from mibuild.xilinx.common import * |
| 17 | + |
| 18 | +from misoclib.soc import cpuif |
| 19 | +#from misoclib.lit.liteusb.common import * |
| 20 | + |
| 21 | + |
| 22 | +def _import(default, name): |
| 23 | + return importlib.import_module(default + "." + name) |
| 24 | + |
| 25 | + |
| 26 | +def _get_args(): |
| 27 | + parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, |
| 28 | + description="""\ |
| 29 | +LiteUSB - based on Migen. |
| 30 | +
|
| 31 | +This program builds and/or loads LiteUSB components. |
| 32 | +One or several actions can be specified: |
| 33 | +
|
| 34 | +clean delete previous build(s). |
| 35 | +build-rtl build verilog rtl. |
| 36 | +build-bitstream build-bitstream build FPGA bitstream. |
| 37 | +build-csr-csv save CSR map into CSV file. |
| 38 | +
|
| 39 | +load-bitstream load bitstream into volatile storage. |
| 40 | +
|
| 41 | +all clean, build-csr-csv, build-bitstream, load-bitstream. |
| 42 | +""") |
| 43 | + |
| 44 | + parser.add_argument("-t", "--target", default="simple", help="Core type to build") |
| 45 | + parser.add_argument("-s", "--sub-target", default="", help="variant of the Core 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("--csr_csv", default="./test/csr.csv", help="CSV file to save the CSR map into") |
| 50 | + |
| 51 | + parser.add_argument("action", nargs="+", help="specify an action") |
| 52 | + |
| 53 | + return parser.parse_args() |
| 54 | + |
| 55 | +# Note: misoclib need to be installed as a python library |
| 56 | + |
| 57 | +if __name__ == "__main__": |
| 58 | + args = _get_args() |
| 59 | + |
| 60 | + # create top-level Core object |
| 61 | + target_module = _import("targets", args.target) |
| 62 | + if args.sub_target: |
| 63 | + top_class = getattr(target_module, args.sub_target) |
| 64 | + else: |
| 65 | + top_class = target_module.default_subtarget |
| 66 | + |
| 67 | + if args.platform is None: |
| 68 | + if hasattr(top_class, "default_platform"): |
| 69 | + platform_name = top_class.default_platform |
| 70 | + else: |
| 71 | + raise ValueError("Target has no default platform, specify a platform with -p your_platform") |
| 72 | + else: |
| 73 | + platform_name = args.platform |
| 74 | + platform_module = _import("mibuild.platforms", platform_name) |
| 75 | + platform_kwargs = dict((k, autotype(v)) for k, v in args.platform_option) |
| 76 | + platform = platform_module.Platform(**platform_kwargs) |
| 77 | + |
| 78 | + build_name = top_class.__name__.lower() + "-" + platform_name |
| 79 | + top_kwargs = dict((k, autotype(v)) for k, v in args.target_option) |
| 80 | + soc = top_class(platform, **top_kwargs) |
| 81 | + soc.finalize() |
| 82 | + memory_regions = soc.get_memory_regions() |
| 83 | + csr_regions = soc.get_csr_regions() |
| 84 | + |
| 85 | + # decode actions |
| 86 | + action_list = ["clean", "build-csr-csv", "build-bitstream", "load-bitstream", "all"] |
| 87 | + actions = {k: False for k in action_list} |
| 88 | + for action in args.action: |
| 89 | + if action in actions: |
| 90 | + actions[action] = True |
| 91 | + else: |
| 92 | + print("Unknown action: "+action+". Valid actions are:") |
| 93 | + for a in action_list: |
| 94 | + print(" "+a) |
| 95 | + sys.exit(1) |
| 96 | + |
| 97 | + print(""" |
| 98 | + __ _ __ __ _________ |
| 99 | + / / (_) /____ / / / / __/ _ ) |
| 100 | + / /__/ / __/ -_) /_/ /\ \/ _ | |
| 101 | + /____/_/\__/\__/\____/___/____/ |
| 102 | +
|
| 103 | +
|
| 104 | + A small footprint and configurable USB core |
| 105 | + powered by Migen |
| 106 | +
|
| 107 | +====== Building parameters: ====== |
| 108 | +System Clk: {} MHz |
| 109 | +===============================""".format( |
| 110 | + soc.clk_freq/1000000)) |
| 111 | + |
| 112 | + # dependencies |
| 113 | + if actions["all"]: |
| 114 | + actions["build-csr-csv"] = True |
| 115 | + actions["build-bitstream"] = True |
| 116 | + actions["load-bitstream"] = True |
| 117 | + |
| 118 | + if actions["build-bitstream"]: |
| 119 | + actions["build-csr-csv"] = True |
| 120 | + actions["build-bitstream"] = True |
| 121 | + actions["load-bitstream"] = True |
| 122 | + |
| 123 | + if actions["clean"]: |
| 124 | + subprocess.call(["rm", "-rf", "build/*"]) |
| 125 | + |
| 126 | + if actions["build-csr-csv"]: |
| 127 | + csr_csv = cpuif.get_csr_csv(csr_regions) |
| 128 | + write_to_file(args.csr_csv, csr_csv) |
| 129 | + |
| 130 | + if actions["build-bitstream"]: |
| 131 | + vns = platform.build(soc, build_name=build_name, run=True) |
| 132 | + if hasattr(soc, "do_exit") and vns is not None: |
| 133 | + if hasattr(soc.do_exit, '__call__'): |
| 134 | + soc.do_exit(vns) |
| 135 | + |
| 136 | + if actions["load-bitstream"]: |
| 137 | + prog = platform.create_programmer() |
| 138 | + prog.load_bitstream("build/" + build_name + platform.bitstream_ext) |
0 commit comments