forked from pulp-training/sw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regtool.py
executable file
·235 lines (209 loc) · 7.68 KB
/
regtool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env python3
# Copyright lowRISC contributors.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0
r"""Command-line tool to validate and convert register hjson
"""
import argparse
import logging as log
import re
import sys
from pathlib import PurePath
import hjson
from reggen import (gen_cheader, gen_ctheader, gen_dv, gen_html, gen_json,
gen_rtl, gen_fpv, gen_selfdoc, validate, version)
DESC = """regtool, generate register info from Hjson source"""
USAGE = '''
regtool [options]
regtool [options] <input>
regtool (-h | --help)
regtool (-V | --version)
'''
def main():
format = 'hjson'
verbose = 0
parser = argparse.ArgumentParser(
prog="regtool",
formatter_class=argparse.RawDescriptionHelpFormatter,
usage=USAGE,
description=DESC)
parser.add_argument('input',
nargs='?',
metavar='file',
type=argparse.FileType('r'),
default=sys.stdin,
help='input file in Hjson type')
parser.add_argument('-d',
action='store_true',
help='Output register documentation (html)')
parser.add_argument('--cdefines',
'-D',
action='store_true',
help='Output C defines header')
parser.add_argument('--ctdefines',
'-T',
action='store_true',
help='Output C defines header (Titan style)')
parser.add_argument('--doc',
action='store_true',
help='Output source file documentation (gfm)')
parser.add_argument('-j',
action='store_true',
help='Output as formatted JSON')
parser.add_argument('-c', action='store_true', help='Output as JSON')
parser.add_argument('-r',
action='store_true',
help='Output as SystemVerilog RTL')
parser.add_argument('-s',
action='store_true',
help='Output as UVM Register class')
parser.add_argument('-f',
action='store_true',
help='Output as FPV CSR rw assertion module')
parser.add_argument('--outdir',
'-t',
help='Target directory for generated RTL; '
'tool uses ../rtl if blank.')
parser.add_argument('--outfile',
'-o',
type=argparse.FileType('w'),
default=sys.stdout,
help='Target filename for json, html, gfm.')
parser.add_argument('--verbose',
'-v',
action='store_true',
help='Verbose and run validate twice')
parser.add_argument('--param',
'-p',
type=str,
default="",
help='''Change the Parameter values.
Only integer value is supported.
You can add multiple param arguments.
Format: ParamA=ValA;ParamB=ValB
''')
parser.add_argument('--version',
'-V',
action='store_true',
help='Show version')
parser.add_argument('--novalidate',
action='store_true',
help='Skip validate, just output json')
args = parser.parse_args()
if args.version:
version.show_and_exit(__file__, ["Hjson", "Mako"])
verbose = args.verbose
if args.j:
format = 'json'
elif args.c:
format = 'compact'
elif args.d:
format = 'html'
elif args.doc:
format = 'doc'
elif args.r:
format = 'rtl'
elif args.s:
format = 'dv'
elif args.f:
format = 'fpv'
elif args.cdefines:
format = 'cdh'
elif args.ctdefines:
format = 'cth'
if (verbose):
log.basicConfig(format="%(levelname)s: %(message)s", level=log.DEBUG)
else:
log.basicConfig(format="%(levelname)s: %(message)s")
outfile = args.outfile
infile = args.input
params = args.param.split(';')
if format == 'rtl':
if args.outdir:
outdir = args.outdir
elif infile != sys.stdin:
outdir = str(PurePath(infile.name).parents[1].joinpath("rtl"))
else:
# Using sys.stdin. not possible to generate RTL
log.error("-r option cannot be used with pipe or stdin")
elif format == 'dv':
if args.outdir:
outdir = args.outdir
elif infile != sys.stdin:
outdir = str(PurePath(infile.name).parents[1].joinpath("dv"))
else:
# Using sys.stdin. not possible to generate RTL
log.error("-s option cannot be used with pipe or stdin")
elif format == 'fpv':
if args.outdir:
outdir = args.outdir
elif infile != sys.stdin:
outdir = str(PurePath(infile.name).parents[1].joinpath("fpv/vip"))
else:
# Using sys.stdin. not possible to generate RTL
log.error("-s option cannot be used with pipe or stdin")
else:
# Ignore
outdir = "."
if format == 'doc':
with outfile:
gen_selfdoc.document(outfile)
exit(0)
with infile:
try:
srcfull = infile.read()
obj = hjson.loads(srcfull,
use_decimal=True,
object_pairs_hook=validate.checking_dict)
except ValueError:
raise SystemExit(sys.exc_info()[1])
if args.novalidate:
with outfile:
gen_json.gen_json(obj, outfile, format)
outfile.write('\n')
elif (validate.validate(obj, params=params) == 0):
if (verbose):
log.info("Second validate pass (should show added optional keys)")
validate.validate(obj, params=params)
if format == 'rtl':
gen_rtl.gen_rtl(obj, outdir)
return 0
if format == 'dv':
gen_dv.gen_dv(obj, outdir)
return 0
if format == 'fpv':
gen_fpv.gen_fpv(obj, outdir)
return 0
src_lic = None
src_copy = ''
found_spdx = None
found_lunder = None
copy = re.compile(r'.*(copyright.*)|(.*\(c\).*)', re.IGNORECASE)
spdx = re.compile(r'.*(SPDX-License-Identifier:.+)')
lunder = re.compile(r'.*(Licensed under.+)', re.IGNORECASE)
for line in srcfull.splitlines():
mat = copy.match(line)
if mat is not None:
src_copy += mat.group(1)
mat = spdx.match(line)
if mat is not None:
found_spdx = mat.group(1)
mat = lunder.match(line)
if mat is not None:
found_lunder = mat.group(1)
if found_lunder:
src_lic = found_lunder
if found_spdx:
src_lic += '\n' + found_spdx
with outfile:
if format == 'html':
gen_html.gen_html(obj, outfile)
elif format == 'cdh':
gen_cheader.gen_cdefines(obj, outfile, src_lic, src_copy)
elif format == 'cth':
gen_ctheader.gen_cdefines(obj, outfile, src_lic, src_copy)
else:
gen_json.gen_json(obj, outfile, format)
outfile.write('\n')
if __name__ == '__main__':
main()