blob: 9c538c94bbb6ef70b15189c5540ca606c7af1c2b [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
11from pathlib import Path, PurePath
12
13import hjson
14import mako
15import pkg_resources
16
17import tlgen
18
19
20def main():
21 parser = argparse.ArgumentParser(prog="tlgen")
22 parser.add_argument('--topcfg',
23 '-t',
24 metavar='file',
lowRISC Contributors802543a2019-08-31 12:12:56 +010025 type=argparse.FileType('r'),
26 help="`top_cfg.hjson` file.")
Eunchan Kim266b3a52019-10-09 14:31:57 -070027 parser.add_argument('--doc',
28 '-d',
29 action='store_true',
Philipp Wagner14a3fee2019-11-21 10:07:02 +000030 help='Generate self HTML document in stdout')
lowRISC Contributors802543a2019-08-31 12:12:56 +010031 parser.add_argument(
32 '--outdir',
33 '-o',
lowRISC Contributors802543a2019-08-31 12:12:56 +010034 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 Kim266b3a52019-10-09 14:31:57 -070046 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 Contributors802543a2019-08-31 12:12:56 +010055 # 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
Weicai Yanga495d202019-12-05 15:36:27 -080074 out_rtl, out_pkg, out_core = tlgen.generate(xbar)
lowRISC Contributors802543a2019-08-31 12:12:56 +010075
Weicai Yange4315d22020-01-09 10:37:42 -080076 rtl_path = Path(args.outdir) / 'rtl/autogen'
lowRISC Contributors802543a2019-08-31 12:12:56 +010077 rtl_path.mkdir(parents=True, exist_ok=True)
Weicai Yange4315d22020-01-09 10:37:42 -080078 dv_path = Path(args.outdir) / 'dv/autogen'
lowRISC Contributors802543a2019-08-31 12:12:56 +010079 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
Weicai Yanga495d202019-12-05 15:36:27 -080091 core_filename = "xbar_%s.core" % (xbar.name)
92 core_filepath = rtl_path / core_filename
93 with core_filepath.open(mode='w', encoding='UTF-8') as fout:
94 fout.write(out_core)
95
96 # generate TB
97 tlgen.generate_tb(xbar, dv_path)
lowRISC Contributors802543a2019-08-31 12:12:56 +010098
99
100if __name__ == "__main__":
101 main()