lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright lowRISC contributors. |
| 3 | # Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | r""" TileLink-Uncached Lightweight Xbar generator |
| 6 | """ |
| 7 | |
| 8 | import argparse |
| 9 | import logging as log |
| 10 | import sys |
| 11 | from pathlib import Path, PurePath |
| 12 | |
| 13 | import hjson |
| 14 | import mako |
| 15 | import pkg_resources |
| 16 | |
| 17 | import tlgen |
| 18 | |
| 19 | |
| 20 | def main(): |
| 21 | parser = argparse.ArgumentParser(prog="tlgen") |
| 22 | parser.add_argument('--topcfg', |
| 23 | '-t', |
| 24 | metavar='file', |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 25 | type=argparse.FileType('r'), |
| 26 | help="`top_cfg.hjson` file.") |
Eunchan Kim | 266b3a5 | 2019-10-09 14:31:57 -0700 | [diff] [blame] | 27 | parser.add_argument('--doc', |
| 28 | '-d', |
| 29 | action='store_true', |
| 30 | help='Generate self html document in stdout') |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 31 | parser.add_argument( |
| 32 | '--outdir', |
| 33 | '-o', |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 34 | help= |
| 35 | "Target directory. tlgen needs 'rtl/' and 'dv/' directory under the target dir" |
| 36 | ) |
| 37 | parser.add_argument('--verbose', '-v', action='store_true', help='Verbose') |
| 38 | |
| 39 | args = parser.parse_args() |
| 40 | |
| 41 | if args.verbose: |
| 42 | log.basicConfig(format="%(levelname)s: %(message)s", level=log.DEBUG) |
| 43 | else: |
| 44 | log.basicConfig(format="%(levelname)s: %(message)s") |
| 45 | |
Eunchan Kim | 266b3a5 | 2019-10-09 14:31:57 -0700 | [diff] [blame] | 46 | if args.doc: |
| 47 | # Generate Doc and return |
| 48 | sys.stdout.write(tlgen.selfdoc(heading=3, cmd='tlgen.py --doc')) |
| 49 | return |
| 50 | |
| 51 | # Check if topcfg defined |
| 52 | if not args.topcfg or not args.outdir: |
| 53 | log.error("--topcfg option is mandatory to generate codes.") |
| 54 | |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 55 | # Check if outdir exists. If not, show error and exit |
| 56 | if not Path(args.outdir).is_dir(): |
| 57 | log.error("'--outdir' should point to writable directory") |
| 58 | |
| 59 | # Load contents of top_cfg |
| 60 | # Skip this part and use internal structure at this time |
| 61 | try: |
| 62 | obj = hjson.load(args.topcfg, use_decimal=True) |
| 63 | except ValueError: |
| 64 | raise SystemExit(sys.exc_info()[1]) |
| 65 | |
| 66 | log.info(obj) |
| 67 | |
| 68 | xbar = tlgen.validate(obj) |
| 69 | |
| 70 | if not tlgen.elaborate(xbar): |
| 71 | log.error("Elaboration failed." + repr(xbar)) |
| 72 | |
| 73 | # Generate |
| 74 | out_rtl, out_pkg, out_dv = tlgen.generate(xbar) |
| 75 | |
| 76 | rtl_path = Path(args.outdir) / 'rtl' |
| 77 | rtl_path.mkdir(parents=True, exist_ok=True) |
| 78 | dv_path = Path(args.outdir) / 'dv' |
| 79 | dv_path.mkdir(parents=True, exist_ok=True) |
| 80 | |
| 81 | rtl_filename = "xbar_%s.sv" % (xbar.name) |
| 82 | rtl_filepath = rtl_path / rtl_filename |
| 83 | with rtl_filepath.open(mode='w', encoding='UTF-8') as fout: |
| 84 | fout.write(out_rtl) |
| 85 | |
| 86 | pkg_filename = "tl_%s_pkg.sv" % (xbar.name) |
| 87 | pkg_filepath = rtl_path / pkg_filename |
| 88 | with pkg_filepath.open(mode='w', encoding='UTF-8') as fout: |
| 89 | fout.write(out_pkg) |
| 90 | |
| 91 | dv_filename = "xbar_%s_tb.sv" % (xbar.name) |
| 92 | dv_filepath = dv_path / dv_filename |
| 93 | with dv_filepath.open(mode='w', encoding='UTF-8') as fout: |
| 94 | fout.write(out_dv) |
| 95 | |
| 96 | |
| 97 | if __name__ == "__main__": |
| 98 | main() |