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 |
Eunchan Kim | 837c796 | 2020-04-30 12:15:49 -0700 | [diff] [blame] | 11 | from pathlib import Path |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 12 | |
| 13 | import hjson |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 14 | |
| 15 | import tlgen |
| 16 | |
| 17 | |
| 18 | def main(): |
| 19 | parser = argparse.ArgumentParser(prog="tlgen") |
| 20 | parser.add_argument('--topcfg', |
| 21 | '-t', |
| 22 | metavar='file', |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 23 | type=argparse.FileType('r'), |
| 24 | help="`top_cfg.hjson` file.") |
Eunchan Kim | 266b3a5 | 2019-10-09 14:31:57 -0700 | [diff] [blame] | 25 | parser.add_argument('--doc', |
| 26 | '-d', |
| 27 | action='store_true', |
Philipp Wagner | 14a3fee | 2019-11-21 10:07:02 +0000 | [diff] [blame] | 28 | help='Generate self HTML document in stdout') |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 29 | parser.add_argument( |
| 30 | '--outdir', |
| 31 | '-o', |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 32 | help= |
| 33 | "Target directory. tlgen needs 'rtl/' and 'dv/' directory under the target dir" |
| 34 | ) |
Eunchan Kim | 837c796 | 2020-04-30 12:15:49 -0700 | [diff] [blame] | 35 | parser.add_argument('--ip-path', |
| 36 | default="", |
| 37 | help=''' |
Weicai Yang | a60ae7d | 2020-02-21 14:32:50 -0800 | [diff] [blame] | 38 | Additional path to generated rtl/ or dv/ folders: outdir/ip_path/rtl |
| 39 | Only needed when there are multiple xbar in outdir''') |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 40 | parser.add_argument('--verbose', '-v', action='store_true', help='Verbose') |
| 41 | |
| 42 | args = parser.parse_args() |
| 43 | |
| 44 | if args.verbose: |
| 45 | log.basicConfig(format="%(levelname)s: %(message)s", level=log.DEBUG) |
| 46 | else: |
| 47 | log.basicConfig(format="%(levelname)s: %(message)s") |
| 48 | |
Eunchan Kim | 266b3a5 | 2019-10-09 14:31:57 -0700 | [diff] [blame] | 49 | if args.doc: |
| 50 | # Generate Doc and return |
| 51 | sys.stdout.write(tlgen.selfdoc(heading=3, cmd='tlgen.py --doc')) |
| 52 | return |
| 53 | |
| 54 | # Check if topcfg defined |
| 55 | if not args.topcfg or not args.outdir: |
| 56 | log.error("--topcfg option is mandatory to generate codes.") |
| 57 | |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 58 | # Check if outdir exists. If not, show error and exit |
| 59 | if not Path(args.outdir).is_dir(): |
| 60 | log.error("'--outdir' should point to writable directory") |
| 61 | |
| 62 | # Load contents of top_cfg |
| 63 | # Skip this part and use internal structure at this time |
| 64 | try: |
| 65 | obj = hjson.load(args.topcfg, use_decimal=True) |
| 66 | except ValueError: |
| 67 | raise SystemExit(sys.exc_info()[1]) |
| 68 | |
| 69 | log.info(obj) |
| 70 | |
| 71 | xbar = tlgen.validate(obj) |
Weicai Yang | a60ae7d | 2020-02-21 14:32:50 -0800 | [diff] [blame] | 72 | xbar.ip_path = args.ip_path |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 73 | |
| 74 | if not tlgen.elaborate(xbar): |
| 75 | log.error("Elaboration failed." + repr(xbar)) |
| 76 | |
| 77 | # Generate |
Eunchan Kim | 9191f26 | 2020-07-30 16:37:40 -0700 | [diff] [blame] | 78 | results = tlgen.generate(xbar) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 79 | |
Weicai Yang | a60ae7d | 2020-02-21 14:32:50 -0800 | [diff] [blame] | 80 | dv_path = Path(args.outdir) / args.ip_path / 'dv/autogen' |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 81 | dv_path.mkdir(parents=True, exist_ok=True) |
| 82 | |
Eunchan Kim | 9191f26 | 2020-07-30 16:37:40 -0700 | [diff] [blame] | 83 | for filename, filecontent in results: |
| 84 | filepath = Path(args.outdir) / args.ip_path / filename |
| 85 | filepath.parent.mkdir(parents=True, exist_ok=True) |
| 86 | with filepath.open(mode='w', encoding='UTF-8') as fout: |
| 87 | fout.write(filecontent) |
Weicai Yang | a495d20 | 2019-12-05 15:36:27 -0800 | [diff] [blame] | 88 | |
| 89 | # generate TB |
| 90 | tlgen.generate_tb(xbar, dv_path) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 91 | |
| 92 | |
| 93 | if __name__ == "__main__": |
| 94 | main() |