| 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 |
| Michael Schaffner | 9af3053 | 2022-01-13 20:25:55 -0800 | [diff] [blame] | 12 | from pathlib import Path |
| lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 13 | |
| Srikrishna Iyer | d8aca09 | 2022-02-02 11:17:44 -0800 | [diff] [blame] | 14 | from reggen import (gen_cheader, gen_dv, gen_fpv, gen_html, gen_json, gen_rtl, |
| 15 | gen_rust, gen_sec_cm_testplan, gen_selfdoc, version) |
| Michael Schaffner | 9af3053 | 2022-01-13 20:25:55 -0800 | [diff] [blame] | 16 | from reggen.countermeasure import CounterMeasure |
| Srikrishna Iyer | d8aca09 | 2022-02-02 11:17:44 -0800 | [diff] [blame] | 17 | from reggen.ip_block import IpBlock |
| lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 18 | |
| Philipp Wagner | 14a3fee | 2019-11-21 10:07:02 +0000 | [diff] [blame] | 19 | DESC = """regtool, generate register info from Hjson source""" |
| lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 20 | |
| 21 | USAGE = ''' |
| 22 | regtool [options] |
| 23 | regtool [options] <input> |
| 24 | regtool (-h | --help) |
| 25 | regtool (-V | --version) |
| 26 | ''' |
| 27 | |
| 28 | |
| 29 | def main(): |
| lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 30 | verbose = 0 |
| 31 | |
| 32 | parser = argparse.ArgumentParser( |
| 33 | prog="regtool", |
| 34 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 35 | usage=USAGE, |
| 36 | description=DESC) |
| Eunchan Kim | 6ec3b89 | 2019-10-01 15:02:56 -0700 | [diff] [blame] | 37 | parser.add_argument('input', |
| 38 | nargs='?', |
| 39 | metavar='file', |
| 40 | type=argparse.FileType('r'), |
| 41 | default=sys.stdin, |
| Philipp Wagner | 14a3fee | 2019-11-21 10:07:02 +0000 | [diff] [blame] | 42 | help='input file in Hjson type') |
| Eunchan Kim | 6ec3b89 | 2019-10-01 15:02:56 -0700 | [diff] [blame] | 43 | parser.add_argument('-d', |
| 44 | action='store_true', |
| 45 | help='Output register documentation (html)') |
| 46 | parser.add_argument('--cdefines', |
| 47 | '-D', |
| 48 | action='store_true', |
| 49 | help='Output C defines header') |
| Chia-Chi Teng | a038790 | 2021-08-10 10:39:22 -0600 | [diff] [blame] | 50 | parser.add_argument('--rust', |
| 51 | '-R', |
| 52 | action='store_true', |
| 53 | help='Output Rust constants') |
| Eunchan Kim | 6ec3b89 | 2019-10-01 15:02:56 -0700 | [diff] [blame] | 54 | parser.add_argument('--doc', |
| 55 | action='store_true', |
| 56 | help='Output source file documentation (gfm)') |
| 57 | parser.add_argument('-j', |
| 58 | action='store_true', |
| 59 | help='Output as formatted JSON') |
| lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 60 | parser.add_argument('-c', action='store_true', help='Output as JSON') |
| Eunchan Kim | 6ec3b89 | 2019-10-01 15:02:56 -0700 | [diff] [blame] | 61 | parser.add_argument('-r', |
| 62 | action='store_true', |
| 63 | help='Output as SystemVerilog RTL') |
| Srikrishna Iyer | d8aca09 | 2022-02-02 11:17:44 -0800 | [diff] [blame] | 64 | parser.add_argument('--sec-cm-testplan', |
| 65 | action='store_true', |
| 66 | help='Generate security countermeasures testplan.') |
| Eunchan Kim | 6ec3b89 | 2019-10-01 15:02:56 -0700 | [diff] [blame] | 67 | parser.add_argument('-s', |
| 68 | action='store_true', |
| 69 | help='Output as UVM Register class') |
| Cindy Chen | 0bad783 | 2019-11-07 11:25:00 -0800 | [diff] [blame] | 70 | parser.add_argument('-f', |
| 71 | action='store_true', |
| 72 | help='Output as FPV CSR rw assertion module') |
| Eunchan Kim | 0bd7566 | 2020-04-24 10:21:18 -0700 | [diff] [blame] | 73 | parser.add_argument('--outdir', |
| 74 | '-t', |
| 75 | help='Target directory for generated RTL; ' |
| 76 | 'tool uses ../rtl if blank.') |
| Cindy Chen | e16009b | 2021-11-01 19:35:51 -0700 | [diff] [blame] | 77 | parser.add_argument('--dv-base-names', |
| Cindy Chen | c8389ed | 2021-11-04 10:53:07 -0700 | [diff] [blame] | 78 | nargs="+", |
| Cindy Chen | e16009b | 2021-11-01 19:35:51 -0700 | [diff] [blame] | 79 | help='Names or prefix for the DV register classes from which ' |
| Srikrishna Iyer | d2341c2 | 2021-01-11 22:31:18 -0800 | [diff] [blame] | 80 | 'the register models are derived.') |
| Eunchan Kim | 6ec3b89 | 2019-10-01 15:02:56 -0700 | [diff] [blame] | 81 | parser.add_argument('--outfile', |
| 82 | '-o', |
| 83 | type=argparse.FileType('w'), |
| 84 | default=sys.stdout, |
| 85 | help='Target filename for json, html, gfm.') |
| 86 | parser.add_argument('--verbose', |
| 87 | '-v', |
| 88 | action='store_true', |
| 89 | help='Verbose and run validate twice') |
| 90 | parser.add_argument('--param', |
| 91 | '-p', |
| 92 | type=str, |
| Eunchan Kim | 276af5e | 2019-10-01 17:02:46 -0700 | [diff] [blame] | 93 | default="", |
| Eunchan Kim | 6ec3b89 | 2019-10-01 15:02:56 -0700 | [diff] [blame] | 94 | help='''Change the Parameter values. |
| 95 | Only integer value is supported. |
| 96 | You can add multiple param arguments. |
| 97 | |
| 98 | Format: ParamA=ValA;ParamB=ValB |
| 99 | ''') |
| 100 | parser.add_argument('--version', |
| 101 | '-V', |
| 102 | action='store_true', |
| 103 | help='Show version') |
| 104 | parser.add_argument('--novalidate', |
| 105 | action='store_true', |
| 106 | help='Skip validate, just output json') |
| lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 107 | |
| 108 | args = parser.parse_args() |
| 109 | |
| 110 | if args.version: |
| 111 | version.show_and_exit(__file__, ["Hjson", "Mako"]) |
| 112 | |
| 113 | verbose = args.verbose |
| lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 114 | if (verbose): |
| 115 | log.basicConfig(format="%(levelname)s: %(message)s", level=log.DEBUG) |
| 116 | else: |
| 117 | log.basicConfig(format="%(levelname)s: %(message)s") |
| 118 | |
| Rupert Swarbrick | 586b93f | 2020-11-10 16:25:39 +0000 | [diff] [blame] | 119 | # Entries are triples of the form (arg, (format, dirspec)). |
| 120 | # |
| 121 | # arg is the name of the argument that selects the format. format is the |
| 122 | # name of the format. dirspec is None if the output is a single file; if |
| 123 | # the output needs a directory, it is a default path relative to the source |
| 124 | # file (used when --outdir is not given). |
| Weicai Yang | 53b0d4d | 2020-11-30 15:28:33 -0800 | [diff] [blame] | 125 | arg_to_format = [('j', ('json', None)), ('c', ('compact', None)), |
| 126 | ('d', ('html', None)), ('doc', ('doc', None)), |
| 127 | ('r', ('rtl', 'rtl')), ('s', ('dv', 'dv')), |
| Chia-Chi Teng | a038790 | 2021-08-10 10:39:22 -0600 | [diff] [blame] | 128 | ('f', ('fpv', 'fpv/vip')), ('cdefines', ('cdh', None)), |
| Srikrishna Iyer | d8aca09 | 2022-02-02 11:17:44 -0800 | [diff] [blame] | 129 | ('sec_cm_testplan', ('sec_cm_testplan', 'data')), |
| Chia-Chi Teng | a038790 | 2021-08-10 10:39:22 -0600 | [diff] [blame] | 130 | ('rust', ('rs', None))] |
| Rupert Swarbrick | 586b93f | 2020-11-10 16:25:39 +0000 | [diff] [blame] | 131 | format = None |
| 132 | dirspec = None |
| 133 | for arg_name, spec in arg_to_format: |
| 134 | if getattr(args, arg_name): |
| 135 | if format is not None: |
| 136 | log.error('Multiple output formats specified on ' |
| Weicai Yang | 53b0d4d | 2020-11-30 15:28:33 -0800 | [diff] [blame] | 137 | 'command line ({} and {}).'.format(format, spec[0])) |
| Rupert Swarbrick | 586b93f | 2020-11-10 16:25:39 +0000 | [diff] [blame] | 138 | sys.exit(1) |
| 139 | format, dirspec = spec |
| 140 | if format is None: |
| 141 | format = 'hjson' |
| lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 142 | |
| 143 | infile = args.input |
| Rupert Swarbrick | 269bb3d | 2021-02-23 15:41:56 +0000 | [diff] [blame] | 144 | |
| 145 | # Split parameters into key=value pairs. |
| 146 | raw_params = args.param.split(';') if args.param else [] |
| 147 | params = [] |
| 148 | for idx, raw_param in enumerate(raw_params): |
| 149 | tokens = raw_param.split('=') |
| 150 | if len(tokens) != 2: |
| 151 | raise ValueError('Entry {} in list of parameter defaults to ' |
| 152 | 'apply is {!r}, which is not of the form ' |
| Srikrishna Iyer | d8aca09 | 2022-02-02 11:17:44 -0800 | [diff] [blame] | 153 | 'param=value.'.format(idx, raw_param)) |
| Rupert Swarbrick | 269bb3d | 2021-02-23 15:41:56 +0000 | [diff] [blame] | 154 | params.append((tokens[0], tokens[1])) |
| Eunchan Kim | 6ec3b89 | 2019-10-01 15:02:56 -0700 | [diff] [blame] | 155 | |
| Rupert Swarbrick | 586b93f | 2020-11-10 16:25:39 +0000 | [diff] [blame] | 156 | # Define either outfile or outdir (but not both), depending on the output |
| 157 | # format. |
| 158 | outfile = None |
| 159 | outdir = None |
| 160 | if dirspec is None: |
| 161 | if args.outdir is not None: |
| 162 | log.error('The {} format expects an output file, ' |
| Weicai Yang | 53b0d4d | 2020-11-30 15:28:33 -0800 | [diff] [blame] | 163 | 'not an output directory.'.format(format)) |
| Rupert Swarbrick | 586b93f | 2020-11-10 16:25:39 +0000 | [diff] [blame] | 164 | sys.exit(1) |
| 165 | |
| 166 | outfile = args.outfile |
| lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 167 | else: |
| Rupert Swarbrick | 586b93f | 2020-11-10 16:25:39 +0000 | [diff] [blame] | 168 | if args.outfile is not sys.stdout: |
| 169 | log.error('The {} format expects an output directory, ' |
| Weicai Yang | 53b0d4d | 2020-11-30 15:28:33 -0800 | [diff] [blame] | 170 | 'not an output file.'.format(format)) |
| Rupert Swarbrick | 586b93f | 2020-11-10 16:25:39 +0000 | [diff] [blame] | 171 | sys.exit(1) |
| 172 | |
| 173 | if args.outdir is not None: |
| 174 | outdir = args.outdir |
| 175 | elif infile is not sys.stdin: |
| Michael Schaffner | 9af3053 | 2022-01-13 20:25:55 -0800 | [diff] [blame] | 176 | outdir = str(Path(infile.name).parents[1].joinpath(dirspec)) |
| Rupert Swarbrick | 586b93f | 2020-11-10 16:25:39 +0000 | [diff] [blame] | 177 | else: |
| 178 | # 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] | 179 | log.error( |
| 180 | 'The {} format writes to an output directory, which ' |
| 181 | 'cannot be inferred automatically if the input comes ' |
| 182 | 'from stdin. Use --outdir to specify it manually.'.format( |
| 183 | format)) |
| Rupert Swarbrick | 586b93f | 2020-11-10 16:25:39 +0000 | [diff] [blame] | 184 | sys.exit(1) |
| lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 185 | |
| 186 | if format == 'doc': |
| 187 | with outfile: |
| 188 | gen_selfdoc.document(outfile) |
| 189 | exit(0) |
| 190 | |
| Rupert Swarbrick | 269bb3d | 2021-02-23 15:41:56 +0000 | [diff] [blame] | 191 | srcfull = infile.read() |
| 192 | |
| Rupert Swarbrick | a9fdc61 | 2020-11-10 16:28:58 +0000 | [diff] [blame] | 193 | try: |
| Rupert Swarbrick | 269bb3d | 2021-02-23 15:41:56 +0000 | [diff] [blame] | 194 | obj = IpBlock.from_text(srcfull, params, infile.name) |
| 195 | except ValueError as err: |
| 196 | log.error(str(err)) |
| 197 | exit(1) |
| lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 198 | |
| Michael Schaffner | 9af3053 | 2022-01-13 20:25:55 -0800 | [diff] [blame] | 199 | # If this block has countermeasures, we grep for RTL annotations in all |
| 200 | # .sv implementation files and check whether they match up with what is |
| 201 | # defined inside the Hjson. |
| Srikrishna Iyer | a5566a4 | 2022-02-16 14:54:08 -0800 | [diff] [blame] | 202 | # Skip this check when generating DV code - its not needed. |
| 203 | if format != 'dv': |
| Olof Kindgren | f999d55 | 2022-03-10 10:34:05 +0000 | [diff] [blame] | 204 | sv_files = Path(infile.name).parent.joinpath('..').joinpath('rtl').glob('*.sv') |
| Srikrishna Iyer | c71e12d | 2022-02-15 09:56:36 -0800 | [diff] [blame] | 205 | rtl_names = CounterMeasure.search_rtl_files(sv_files) |
| 206 | obj.check_cm_annotations(rtl_names, infile.name) |
| Michael Schaffner | 9af3053 | 2022-01-13 20:25:55 -0800 | [diff] [blame] | 207 | |
| lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 208 | if args.novalidate: |
| 209 | with outfile: |
| 210 | gen_json.gen_json(obj, outfile, format) |
| 211 | outfile.write('\n') |
| Rupert Swarbrick | 269bb3d | 2021-02-23 15:41:56 +0000 | [diff] [blame] | 212 | else: |
| lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 213 | if format == 'rtl': |
| Rupert Swarbrick | 6880e21 | 2021-02-10 16:10:00 +0000 | [diff] [blame] | 214 | return gen_rtl.gen_rtl(obj, outdir) |
| Srikrishna Iyer | d8aca09 | 2022-02-02 11:17:44 -0800 | [diff] [blame] | 215 | if format == 'sec_cm_testplan': |
| 216 | return gen_sec_cm_testplan.gen_sec_cm_testplan(obj, outdir) |
| lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 217 | if format == 'dv': |
| Cindy Chen | e16009b | 2021-11-01 19:35:51 -0700 | [diff] [blame] | 218 | return gen_dv.gen_dv(obj, args.dv_base_names, outdir) |
| Cindy Chen | 0bad783 | 2019-11-07 11:25:00 -0800 | [diff] [blame] | 219 | if format == 'fpv': |
| Rupert Swarbrick | 6880e21 | 2021-02-10 16:10:00 +0000 | [diff] [blame] | 220 | return gen_fpv.gen_fpv(obj, outdir) |
| lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 221 | src_lic = None |
| 222 | src_copy = '' |
| 223 | found_spdx = None |
| 224 | found_lunder = None |
| 225 | copy = re.compile(r'.*(copyright.*)|(.*\(c\).*)', re.IGNORECASE) |
| 226 | spdx = re.compile(r'.*(SPDX-License-Identifier:.+)') |
| 227 | lunder = re.compile(r'.*(Licensed under.+)', re.IGNORECASE) |
| 228 | for line in srcfull.splitlines(): |
| 229 | mat = copy.match(line) |
| Eunchan Kim | 0bd7566 | 2020-04-24 10:21:18 -0700 | [diff] [blame] | 230 | if mat is not None: |
| lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 231 | src_copy += mat.group(1) |
| 232 | mat = spdx.match(line) |
| Eunchan Kim | 0bd7566 | 2020-04-24 10:21:18 -0700 | [diff] [blame] | 233 | if mat is not None: |
| lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 234 | found_spdx = mat.group(1) |
| 235 | mat = lunder.match(line) |
| Eunchan Kim | 0bd7566 | 2020-04-24 10:21:18 -0700 | [diff] [blame] | 236 | if mat is not None: |
| lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 237 | found_lunder = mat.group(1) |
| 238 | if found_lunder: |
| 239 | src_lic = found_lunder |
| 240 | if found_spdx: |
| 241 | src_lic += '\n' + found_spdx |
| 242 | |
| 243 | with outfile: |
| 244 | if format == 'html': |
| Rupert Swarbrick | 6880e21 | 2021-02-10 16:10:00 +0000 | [diff] [blame] | 245 | return gen_html.gen_html(obj, outfile) |
| lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 246 | elif format == 'cdh': |
| Srikrishna Iyer | d8aca09 | 2022-02-02 11:17:44 -0800 | [diff] [blame] | 247 | return gen_cheader.gen_cdefines(obj, outfile, src_lic, |
| 248 | src_copy) |
| Chia-Chi Teng | a038790 | 2021-08-10 10:39:22 -0600 | [diff] [blame] | 249 | elif format == 'rs': |
| 250 | return gen_rust.gen_rust(obj, outfile, src_lic, src_copy) |
| lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 251 | else: |
| Rupert Swarbrick | 6880e21 | 2021-02-10 16:10:00 +0000 | [diff] [blame] | 252 | return gen_json.gen_json(obj, outfile, format) |
| lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 253 | |
| 254 | outfile.write('\n') |
| 255 | |
| 256 | |
| 257 | if __name__ == '__main__': |
| Rupert Swarbrick | 6880e21 | 2021-02-10 16:10:00 +0000 | [diff] [blame] | 258 | sys.exit(main()) |