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"""Command-line tool to validate and convert register hjson |
| 6 | |
| 7 | """ |
| 8 | import argparse |
| 9 | import logging as log |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 10 | import re |
| 11 | import sys |
| 12 | from pathlib import PurePath |
| 13 | |
Rupert Swarbrick | 588819e | 2021-02-10 16:27:53 +0000 | [diff] [blame] | 14 | from reggen import (gen_cheader, gen_dv, gen_fpv, gen_html, |
Rupert Swarbrick | 269bb3d | 2021-02-23 15:41:56 +0000 | [diff] [blame] | 15 | gen_json, gen_rtl, gen_selfdoc, version) |
| 16 | from reggen.ip_block import IpBlock |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 17 | |
Philipp Wagner | 14a3fee | 2019-11-21 10:07:02 +0000 | [diff] [blame] | 18 | DESC = """regtool, generate register info from Hjson source""" |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 19 | |
| 20 | USAGE = ''' |
| 21 | regtool [options] |
| 22 | regtool [options] <input> |
| 23 | regtool (-h | --help) |
| 24 | regtool (-V | --version) |
| 25 | ''' |
| 26 | |
| 27 | |
| 28 | def main(): |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 29 | verbose = 0 |
| 30 | |
| 31 | parser = argparse.ArgumentParser( |
| 32 | prog="regtool", |
| 33 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 34 | usage=USAGE, |
| 35 | description=DESC) |
Eunchan Kim | 6ec3b89 | 2019-10-01 15:02:56 -0700 | [diff] [blame] | 36 | parser.add_argument('input', |
| 37 | nargs='?', |
| 38 | metavar='file', |
| 39 | type=argparse.FileType('r'), |
| 40 | default=sys.stdin, |
Philipp Wagner | 14a3fee | 2019-11-21 10:07:02 +0000 | [diff] [blame] | 41 | help='input file in Hjson type') |
Eunchan Kim | 6ec3b89 | 2019-10-01 15:02:56 -0700 | [diff] [blame] | 42 | 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 Kim | 6ec3b89 | 2019-10-01 15:02:56 -0700 | [diff] [blame] | 49 | 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 Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 55 | parser.add_argument('-c', action='store_true', help='Output as JSON') |
Eunchan Kim | 6ec3b89 | 2019-10-01 15:02:56 -0700 | [diff] [blame] | 56 | 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 Chen | 0bad783 | 2019-11-07 11:25:00 -0800 | [diff] [blame] | 62 | parser.add_argument('-f', |
| 63 | action='store_true', |
| 64 | help='Output as FPV CSR rw assertion module') |
Eunchan Kim | 0bd7566 | 2020-04-24 10:21:18 -0700 | [diff] [blame] | 65 | parser.add_argument('--outdir', |
| 66 | '-t', |
| 67 | help='Target directory for generated RTL; ' |
| 68 | 'tool uses ../rtl if blank.') |
Srikrishna Iyer | d2341c2 | 2021-01-11 22:31:18 -0800 | [diff] [blame] | 69 | 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 Kim | 6ec3b89 | 2019-10-01 15:02:56 -0700 | [diff] [blame] | 73 | 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 Kim | 276af5e | 2019-10-01 17:02:46 -0700 | [diff] [blame] | 85 | default="", |
Eunchan Kim | 6ec3b89 | 2019-10-01 15:02:56 -0700 | [diff] [blame] | 86 | 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 Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 99 | |
| 100 | args = parser.parse_args() |
| 101 | |
| 102 | if args.version: |
| 103 | version.show_and_exit(__file__, ["Hjson", "Mako"]) |
| 104 | |
| 105 | verbose = args.verbose |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 106 | 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 Swarbrick | 586b93f | 2020-11-10 16:25:39 +0000 | [diff] [blame] | 111 | # 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 Yang | 53b0d4d | 2020-11-30 15:28:33 -0800 | [diff] [blame] | 117 | arg_to_format = [('j', ('json', None)), ('c', ('compact', None)), |
| 118 | ('d', ('html', None)), ('doc', ('doc', None)), |
| 119 | ('r', ('rtl', 'rtl')), ('s', ('dv', 'dv')), |
Rupert Swarbrick | 588819e | 2021-02-10 16:27:53 +0000 | [diff] [blame] | 120 | ('f', ('fpv', 'fpv/vip')), ('cdefines', ('cdh', None))] |
Rupert Swarbrick | 586b93f | 2020-11-10 16:25:39 +0000 | [diff] [blame] | 121 | 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 Yang | 53b0d4d | 2020-11-30 15:28:33 -0800 | [diff] [blame] | 127 | 'command line ({} and {}).'.format(format, spec[0])) |
Rupert Swarbrick | 586b93f | 2020-11-10 16:25:39 +0000 | [diff] [blame] | 128 | sys.exit(1) |
| 129 | format, dirspec = spec |
| 130 | if format is None: |
| 131 | format = 'hjson' |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 132 | |
| 133 | infile = args.input |
Rupert Swarbrick | 269bb3d | 2021-02-23 15:41:56 +0000 | [diff] [blame] | 134 | |
| 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 Kim | 6ec3b89 | 2019-10-01 15:02:56 -0700 | [diff] [blame] | 146 | |
Rupert Swarbrick | 586b93f | 2020-11-10 16:25:39 +0000 | [diff] [blame] | 147 | # 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 Yang | 53b0d4d | 2020-11-30 15:28:33 -0800 | [diff] [blame] | 154 | 'not an output directory.'.format(format)) |
Rupert Swarbrick | 586b93f | 2020-11-10 16:25:39 +0000 | [diff] [blame] | 155 | sys.exit(1) |
| 156 | |
| 157 | outfile = args.outfile |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 158 | else: |
Rupert Swarbrick | 586b93f | 2020-11-10 16:25:39 +0000 | [diff] [blame] | 159 | if args.outfile is not sys.stdout: |
| 160 | log.error('The {} format expects an output directory, ' |
Weicai Yang | 53b0d4d | 2020-11-30 15:28:33 -0800 | [diff] [blame] | 161 | 'not an output file.'.format(format)) |
Rupert Swarbrick | 586b93f | 2020-11-10 16:25:39 +0000 | [diff] [blame] | 162 | 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 Yang | 53b0d4d | 2020-11-30 15:28:33 -0800 | [diff] [blame] | 170 | 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 Swarbrick | 586b93f | 2020-11-10 16:25:39 +0000 | [diff] [blame] | 175 | sys.exit(1) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 176 | |
| 177 | if format == 'doc': |
| 178 | with outfile: |
| 179 | gen_selfdoc.document(outfile) |
| 180 | exit(0) |
| 181 | |
Rupert Swarbrick | 269bb3d | 2021-02-23 15:41:56 +0000 | [diff] [blame] | 182 | srcfull = infile.read() |
| 183 | |
Rupert Swarbrick | a9fdc61 | 2020-11-10 16:28:58 +0000 | [diff] [blame] | 184 | try: |
Rupert Swarbrick | 269bb3d | 2021-02-23 15:41:56 +0000 | [diff] [blame] | 185 | obj = IpBlock.from_text(srcfull, params, infile.name) |
| 186 | except ValueError as err: |
| 187 | log.error(str(err)) |
| 188 | exit(1) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 189 | |
| 190 | if args.novalidate: |
| 191 | with outfile: |
| 192 | gen_json.gen_json(obj, outfile, format) |
| 193 | outfile.write('\n') |
Rupert Swarbrick | 269bb3d | 2021-02-23 15:41:56 +0000 | [diff] [blame] | 194 | else: |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 195 | if format == 'rtl': |
Rupert Swarbrick | 6880e21 | 2021-02-10 16:10:00 +0000 | [diff] [blame] | 196 | return gen_rtl.gen_rtl(obj, outdir) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 197 | if format == 'dv': |
Rupert Swarbrick | 6880e21 | 2021-02-10 16:10:00 +0000 | [diff] [blame] | 198 | return gen_dv.gen_dv(obj, args.dv_base_prefix, outdir) |
Cindy Chen | 0bad783 | 2019-11-07 11:25:00 -0800 | [diff] [blame] | 199 | if format == 'fpv': |
Rupert Swarbrick | 6880e21 | 2021-02-10 16:10:00 +0000 | [diff] [blame] | 200 | return gen_fpv.gen_fpv(obj, outdir) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 201 | 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 Kim | 0bd7566 | 2020-04-24 10:21:18 -0700 | [diff] [blame] | 210 | if mat is not None: |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 211 | src_copy += mat.group(1) |
| 212 | mat = spdx.match(line) |
Eunchan Kim | 0bd7566 | 2020-04-24 10:21:18 -0700 | [diff] [blame] | 213 | if mat is not None: |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 214 | found_spdx = mat.group(1) |
| 215 | mat = lunder.match(line) |
Eunchan Kim | 0bd7566 | 2020-04-24 10:21:18 -0700 | [diff] [blame] | 216 | if mat is not None: |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 217 | 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 Swarbrick | 6880e21 | 2021-02-10 16:10:00 +0000 | [diff] [blame] | 225 | return gen_html.gen_html(obj, outfile) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 226 | elif format == 'cdh': |
Rupert Swarbrick | 6880e21 | 2021-02-10 16:10:00 +0000 | [diff] [blame] | 227 | return gen_cheader.gen_cdefines(obj, outfile, src_lic, src_copy) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 228 | else: |
Rupert Swarbrick | 6880e21 | 2021-02-10 16:10:00 +0000 | [diff] [blame] | 229 | return gen_json.gen_json(obj, outfile, format) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 230 | |
| 231 | outfile.write('\n') |
| 232 | |
| 233 | |
| 234 | if __name__ == '__main__': |
Rupert Swarbrick | 6880e21 | 2021-02-10 16:10:00 +0000 | [diff] [blame] | 235 | sys.exit(main()) |