blob: aa264c57b22b224d8af12aea2aec46cece2f14de [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"""Command-line tool to validate and convert register hjson
6
7"""
8import argparse
9import logging as log
lowRISC Contributors802543a2019-08-31 12:12:56 +010010import re
11import sys
12from pathlib import PurePath
13
Rupert Swarbrick588819e2021-02-10 16:27:53 +000014from reggen import (gen_cheader, gen_dv, gen_fpv, gen_html,
Rupert Swarbrick269bb3d2021-02-23 15:41:56 +000015 gen_json, gen_rtl, gen_selfdoc, version)
16from reggen.ip_block import IpBlock
lowRISC Contributors802543a2019-08-31 12:12:56 +010017
Philipp Wagner14a3fee2019-11-21 10:07:02 +000018DESC = """regtool, generate register info from Hjson source"""
lowRISC Contributors802543a2019-08-31 12:12:56 +010019
20USAGE = '''
21 regtool [options]
22 regtool [options] <input>
23 regtool (-h | --help)
24 regtool (-V | --version)
25'''
26
27
28def main():
lowRISC Contributors802543a2019-08-31 12:12:56 +010029 verbose = 0
30
31 parser = argparse.ArgumentParser(
32 prog="regtool",
33 formatter_class=argparse.RawDescriptionHelpFormatter,
34 usage=USAGE,
35 description=DESC)
Eunchan Kim6ec3b892019-10-01 15:02:56 -070036 parser.add_argument('input',
37 nargs='?',
38 metavar='file',
39 type=argparse.FileType('r'),
40 default=sys.stdin,
Philipp Wagner14a3fee2019-11-21 10:07:02 +000041 help='input file in Hjson type')
Eunchan Kim6ec3b892019-10-01 15:02:56 -070042 parser.add_argument('-d',
43 action='store_true',
44 help='Output register documentation (html)')
45 parser.add_argument('--cdefines',
46 '-D',
47 action='store_true',
48 help='Output C defines header')
Eunchan Kim6ec3b892019-10-01 15:02:56 -070049 parser.add_argument('--doc',
50 action='store_true',
51 help='Output source file documentation (gfm)')
52 parser.add_argument('-j',
53 action='store_true',
54 help='Output as formatted JSON')
lowRISC Contributors802543a2019-08-31 12:12:56 +010055 parser.add_argument('-c', action='store_true', help='Output as JSON')
Eunchan Kim6ec3b892019-10-01 15:02:56 -070056 parser.add_argument('-r',
57 action='store_true',
58 help='Output as SystemVerilog RTL')
59 parser.add_argument('-s',
60 action='store_true',
61 help='Output as UVM Register class')
Cindy Chen0bad7832019-11-07 11:25:00 -080062 parser.add_argument('-f',
63 action='store_true',
64 help='Output as FPV CSR rw assertion module')
Eunchan Kim0bd75662020-04-24 10:21:18 -070065 parser.add_argument('--outdir',
66 '-t',
67 help='Target directory for generated RTL; '
68 'tool uses ../rtl if blank.')
Srikrishna Iyerd2341c22021-01-11 22:31:18 -080069 parser.add_argument('--dv-base-prefix',
70 default='dv_base',
71 help='Prefix for the DV register classes from which '
72 'the register models are derived.')
Eunchan Kim6ec3b892019-10-01 15:02:56 -070073 parser.add_argument('--outfile',
74 '-o',
75 type=argparse.FileType('w'),
76 default=sys.stdout,
77 help='Target filename for json, html, gfm.')
78 parser.add_argument('--verbose',
79 '-v',
80 action='store_true',
81 help='Verbose and run validate twice')
82 parser.add_argument('--param',
83 '-p',
84 type=str,
Eunchan Kim276af5e2019-10-01 17:02:46 -070085 default="",
Eunchan Kim6ec3b892019-10-01 15:02:56 -070086 help='''Change the Parameter values.
87 Only integer value is supported.
88 You can add multiple param arguments.
89
90 Format: ParamA=ValA;ParamB=ValB
91 ''')
92 parser.add_argument('--version',
93 '-V',
94 action='store_true',
95 help='Show version')
96 parser.add_argument('--novalidate',
97 action='store_true',
98 help='Skip validate, just output json')
lowRISC Contributors802543a2019-08-31 12:12:56 +010099
100 args = parser.parse_args()
101
102 if args.version:
103 version.show_and_exit(__file__, ["Hjson", "Mako"])
104
105 verbose = args.verbose
lowRISC Contributors802543a2019-08-31 12:12:56 +0100106 if (verbose):
107 log.basicConfig(format="%(levelname)s: %(message)s", level=log.DEBUG)
108 else:
109 log.basicConfig(format="%(levelname)s: %(message)s")
110
Rupert Swarbrick586b93f2020-11-10 16:25:39 +0000111 # Entries are triples of the form (arg, (format, dirspec)).
112 #
113 # arg is the name of the argument that selects the format. format is the
114 # name of the format. dirspec is None if the output is a single file; if
115 # the output needs a directory, it is a default path relative to the source
116 # file (used when --outdir is not given).
Weicai Yang53b0d4d2020-11-30 15:28:33 -0800117 arg_to_format = [('j', ('json', None)), ('c', ('compact', None)),
118 ('d', ('html', None)), ('doc', ('doc', None)),
119 ('r', ('rtl', 'rtl')), ('s', ('dv', 'dv')),
Rupert Swarbrick588819e2021-02-10 16:27:53 +0000120 ('f', ('fpv', 'fpv/vip')), ('cdefines', ('cdh', None))]
Rupert Swarbrick586b93f2020-11-10 16:25:39 +0000121 format = None
122 dirspec = None
123 for arg_name, spec in arg_to_format:
124 if getattr(args, arg_name):
125 if format is not None:
126 log.error('Multiple output formats specified on '
Weicai Yang53b0d4d2020-11-30 15:28:33 -0800127 'command line ({} and {}).'.format(format, spec[0]))
Rupert Swarbrick586b93f2020-11-10 16:25:39 +0000128 sys.exit(1)
129 format, dirspec = spec
130 if format is None:
131 format = 'hjson'
lowRISC Contributors802543a2019-08-31 12:12:56 +0100132
133 infile = args.input
Rupert Swarbrick269bb3d2021-02-23 15:41:56 +0000134
135 # Split parameters into key=value pairs.
136 raw_params = args.param.split(';') if args.param else []
137 params = []
138 for idx, raw_param in enumerate(raw_params):
139 tokens = raw_param.split('=')
140 if len(tokens) != 2:
141 raise ValueError('Entry {} in list of parameter defaults to '
142 'apply is {!r}, which is not of the form '
143 'param=value.'
144 .format(idx, raw_param))
145 params.append((tokens[0], tokens[1]))
Eunchan Kim6ec3b892019-10-01 15:02:56 -0700146
Rupert Swarbrick586b93f2020-11-10 16:25:39 +0000147 # Define either outfile or outdir (but not both), depending on the output
148 # format.
149 outfile = None
150 outdir = None
151 if dirspec is None:
152 if args.outdir is not None:
153 log.error('The {} format expects an output file, '
Weicai Yang53b0d4d2020-11-30 15:28:33 -0800154 'not an output directory.'.format(format))
Rupert Swarbrick586b93f2020-11-10 16:25:39 +0000155 sys.exit(1)
156
157 outfile = args.outfile
lowRISC Contributors802543a2019-08-31 12:12:56 +0100158 else:
Rupert Swarbrick586b93f2020-11-10 16:25:39 +0000159 if args.outfile is not sys.stdout:
160 log.error('The {} format expects an output directory, '
Weicai Yang53b0d4d2020-11-30 15:28:33 -0800161 'not an output file.'.format(format))
Rupert Swarbrick586b93f2020-11-10 16:25:39 +0000162 sys.exit(1)
163
164 if args.outdir is not None:
165 outdir = args.outdir
166 elif infile is not sys.stdin:
167 outdir = str(PurePath(infile.name).parents[1].joinpath(dirspec))
168 else:
169 # We're using sys.stdin, so can't infer an output directory name
Weicai Yang53b0d4d2020-11-30 15:28:33 -0800170 log.error(
171 'The {} format writes to an output directory, which '
172 'cannot be inferred automatically if the input comes '
173 'from stdin. Use --outdir to specify it manually.'.format(
174 format))
Rupert Swarbrick586b93f2020-11-10 16:25:39 +0000175 sys.exit(1)
lowRISC Contributors802543a2019-08-31 12:12:56 +0100176
177 if format == 'doc':
178 with outfile:
179 gen_selfdoc.document(outfile)
180 exit(0)
181
Rupert Swarbrick269bb3d2021-02-23 15:41:56 +0000182 srcfull = infile.read()
183
Rupert Swarbricka9fdc612020-11-10 16:28:58 +0000184 try:
Rupert Swarbrick269bb3d2021-02-23 15:41:56 +0000185 obj = IpBlock.from_text(srcfull, params, infile.name)
186 except ValueError as err:
187 log.error(str(err))
188 exit(1)
lowRISC Contributors802543a2019-08-31 12:12:56 +0100189
190 if args.novalidate:
191 with outfile:
192 gen_json.gen_json(obj, outfile, format)
193 outfile.write('\n')
Rupert Swarbrick269bb3d2021-02-23 15:41:56 +0000194 else:
lowRISC Contributors802543a2019-08-31 12:12:56 +0100195 if format == 'rtl':
Rupert Swarbrick6880e212021-02-10 16:10:00 +0000196 return gen_rtl.gen_rtl(obj, outdir)
lowRISC Contributors802543a2019-08-31 12:12:56 +0100197 if format == 'dv':
Rupert Swarbrick6880e212021-02-10 16:10:00 +0000198 return gen_dv.gen_dv(obj, args.dv_base_prefix, outdir)
Cindy Chen0bad7832019-11-07 11:25:00 -0800199 if format == 'fpv':
Rupert Swarbrick6880e212021-02-10 16:10:00 +0000200 return gen_fpv.gen_fpv(obj, outdir)
lowRISC Contributors802543a2019-08-31 12:12:56 +0100201 src_lic = None
202 src_copy = ''
203 found_spdx = None
204 found_lunder = None
205 copy = re.compile(r'.*(copyright.*)|(.*\(c\).*)', re.IGNORECASE)
206 spdx = re.compile(r'.*(SPDX-License-Identifier:.+)')
207 lunder = re.compile(r'.*(Licensed under.+)', re.IGNORECASE)
208 for line in srcfull.splitlines():
209 mat = copy.match(line)
Eunchan Kim0bd75662020-04-24 10:21:18 -0700210 if mat is not None:
lowRISC Contributors802543a2019-08-31 12:12:56 +0100211 src_copy += mat.group(1)
212 mat = spdx.match(line)
Eunchan Kim0bd75662020-04-24 10:21:18 -0700213 if mat is not None:
lowRISC Contributors802543a2019-08-31 12:12:56 +0100214 found_spdx = mat.group(1)
215 mat = lunder.match(line)
Eunchan Kim0bd75662020-04-24 10:21:18 -0700216 if mat is not None:
lowRISC Contributors802543a2019-08-31 12:12:56 +0100217 found_lunder = mat.group(1)
218 if found_lunder:
219 src_lic = found_lunder
220 if found_spdx:
221 src_lic += '\n' + found_spdx
222
223 with outfile:
224 if format == 'html':
Rupert Swarbrick6880e212021-02-10 16:10:00 +0000225 return gen_html.gen_html(obj, outfile)
lowRISC Contributors802543a2019-08-31 12:12:56 +0100226 elif format == 'cdh':
Rupert Swarbrick6880e212021-02-10 16:10:00 +0000227 return gen_cheader.gen_cdefines(obj, outfile, src_lic, src_copy)
lowRISC Contributors802543a2019-08-31 12:12:56 +0100228 else:
Rupert Swarbrick6880e212021-02-10 16:10:00 +0000229 return gen_json.gen_json(obj, outfile, format)
lowRISC Contributors802543a2019-08-31 12:12:56 +0100230
231 outfile.write('\n')
232
233
234if __name__ == '__main__':
Rupert Swarbrick6880e212021-02-10 16:10:00 +0000235 sys.exit(main())