blob: 46ec9f8019750f5a89fc7074e4e6592893ace2ec [file] [log] [blame]
lowRISC Contributors802543a2019-08-31 12:12:56 +01001#!/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
5r""" TileLink-Uncached Lightweight Xbar generator
6"""
7
8import argparse
9import logging as log
10import sys
Eunchan Kim837c7962020-04-30 12:15:49 -070011from pathlib import Path
lowRISC Contributors802543a2019-08-31 12:12:56 +010012
13import hjson
lowRISC Contributors802543a2019-08-31 12:12:56 +010014
15import tlgen
16
17
18def main():
19 parser = argparse.ArgumentParser(prog="tlgen")
20 parser.add_argument('--topcfg',
21 '-t',
22 metavar='file',
lowRISC Contributors802543a2019-08-31 12:12:56 +010023 type=argparse.FileType('r'),
24 help="`top_cfg.hjson` file.")
Eunchan Kim266b3a52019-10-09 14:31:57 -070025 parser.add_argument('--doc',
26 '-d',
27 action='store_true',
Philipp Wagner14a3fee2019-11-21 10:07:02 +000028 help='Generate self HTML document in stdout')
lowRISC Contributors802543a2019-08-31 12:12:56 +010029 parser.add_argument(
30 '--outdir',
31 '-o',
lowRISC Contributors802543a2019-08-31 12:12:56 +010032 help=
33 "Target directory. tlgen needs 'rtl/' and 'dv/' directory under the target dir"
34 )
Eunchan Kim837c7962020-04-30 12:15:49 -070035 parser.add_argument('--ip-path',
36 default="",
37 help='''
Weicai Yanga60ae7d2020-02-21 14:32:50 -080038 Additional path to generated rtl/ or dv/ folders: outdir/ip_path/rtl
39 Only needed when there are multiple xbar in outdir''')
lowRISC Contributors802543a2019-08-31 12:12:56 +010040 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 Kim266b3a52019-10-09 14:31:57 -070049 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 Contributors802543a2019-08-31 12:12:56 +010058 # 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 Yanga60ae7d2020-02-21 14:32:50 -080072 xbar.ip_path = args.ip_path
lowRISC Contributors802543a2019-08-31 12:12:56 +010073
74 if not tlgen.elaborate(xbar):
75 log.error("Elaboration failed." + repr(xbar))
76
77 # Generate
Eunchan Kim9191f262020-07-30 16:37:40 -070078 results = tlgen.generate(xbar)
lowRISC Contributors802543a2019-08-31 12:12:56 +010079
Weicai Yanga60ae7d2020-02-21 14:32:50 -080080 dv_path = Path(args.outdir) / args.ip_path / 'dv/autogen'
lowRISC Contributors802543a2019-08-31 12:12:56 +010081 dv_path.mkdir(parents=True, exist_ok=True)
82
Eunchan Kim9191f262020-07-30 16:37:40 -070083 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 Yanga495d202019-12-05 15:36:27 -080088
89 # generate TB
90 tlgen.generate_tb(xbar, dv_path)
lowRISC Contributors802543a2019-08-31 12:12:56 +010091
92
93if __name__ == "__main__":
94 main()