-
Notifications
You must be signed in to change notification settings - Fork 3
/
i3c_core_config.py
220 lines (170 loc) · 6.58 KB
/
i3c_core_config.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
# SPDX-License-Identifier: Apache-2.0
# Command-line options logic imported from ibex_config script:
# https://github.com/lowRISC/ibex/blob/f8b6d468c336a0a96f6e774a70420fb370a68699/util/ibex_config.py
import argparse
import json
import os
import yaml
from jsonschema import ValidationError, validate
from py2svh import cfg2svh
from common import ConfigException, I3CCoreConfig, I3CGenericConfig, RegGenConfig
_DEFAULT_CONFIG_FILE = "i3c_core_configs.yaml"
_DEFAULT_OUT_DEFINES_FILE = "i3c_defines.svh"
_DEFAULT_I3C_CONFIG_NAME = "default"
def parse_and_validate_config(name: str, filename: str) -> I3CGenericConfig:
schema_path = os.path.join(os.path.dirname(__file__), "i3c_core_config.schema.json")
with open(filename) as config_file, open(schema_path, "r") as s:
try:
yml = yaml.load(config_file, Loader=yaml.SafeLoader)
except yaml.YAMLError as err:
raise ConfigException(f"Could not decode yaml: {err}")
if name not in yml:
raise ConfigException(f"{name} configuration not found in the {filename}.")
schema = json.load(s)
try:
validate(yml[name], schema)
except ValidationError as err:
raise ConfigException(
f"{filename!r}: Invalid I3C core configuration: {err.message}"
) from None
return I3CGenericConfig(yml[name], schema["properties"])
class BaseOpts:
def __init__(self, name, description) -> None:
self.name = name
self.description = description
def setup_args(self, subparser: argparse._SubParsersAction):
self.argparser = subparser.add_parser(self.name, help=(f"Produce {self.description}"))
self.argparser.set_defaults(output_fn=self.output)
def output(self, config: I3CGenericConfig):
raise NotImplementedError
class CmdLineOpts(BaseOpts):
def __init__(self, name, description, _set_param_func, _set_define_func, _hier_sep) -> None:
super().__init__(name, description)
self._set_param_func = _set_param_func
self._set_define_func = _set_define_func
self._hier_sep = _hier_sep
def setup_args(self, subparser: argparse._SubParsersAction):
super().setup_args(subparser)
self.argparser.add_argument(
"--ins-hier-path",
help=("Hierarchical path to the instance to set " "configuration parameters on"),
default="",
)
def output(self, config: I3CGenericConfig, args):
hier_path = args.ins_hier_path + self._hier_sep if args.ins_hier_path else ""
sim_opts = []
for name, value in config.items():
if isinstance(value, str):
sim_opts += self._set_define_func(name, value)
else:
assert type(value) in [bool, int]
# Explicitly convert booleans to 0/1:
val_as_int = int(value)
full_param = hier_path + name
sim_opts += self._set_param_func(full_param, str(val_as_int))
return " ".join(sim_opts)
class VCSOpts(CmdLineOpts):
def __init__(self) -> None:
super().__init__(
"vcs_opts",
"VCS compile command line options",
lambda p, v: ["-pvalue+" + p + "=" + v],
lambda d, v: ["+define+" + d + "=" + v],
".",
)
class QuestaSimOpts(CmdLineOpts):
def __init__(self) -> None:
super().__init__(
"questa_sim_opts",
"Questa simulation command line options",
lambda p, v: ["-g/" + p + "=" + v],
lambda *_: [],
"/",
)
class QuestaCompileOpts(CmdLineOpts):
def __init__(self) -> None:
super().__init__(
"questa_compile_opts",
"Questa compile options",
lambda *_: [],
lambda d, v: ["+define+" + d + "=" + v],
"/",
)
class RegGenOpts(CmdLineOpts):
"""
Produces argument line based on the given I3CGenericConfig to be passed
to the PeakRDL Regblock register generation.
"""
def __init__(self) -> None:
super().__init__(
"reg_gen_opts",
"Peakrdl regblock generator options",
lambda p, v: ["-P " + p + "=" + v],
lambda *_: [],
".",
)
def output(self, config: I3CGenericConfig, args):
return super().output(RegGenConfig(config), args)
class VerilatorSimOpts(CmdLineOpts):
def __init__(self) -> None:
super().__init__(
"verilator_opts",
"Verilator simulation options",
lambda p, v: ["-pvalue+" + p + "=" + v],
lambda d, v: ["+define+" + d + "=" + v],
".",
)
def output(self, config: I3CGenericConfig, args):
return super().output(I3CCoreConfig(config), args)
class SVHFile(BaseOpts):
def __init__(self) -> None:
super().__init__("svh_file", "I3C Core parameter definitions in a System Verilog file")
def setup_args(self, subparser: argparse._SubParsersAction):
super().setup_args(subparser)
self.argparser.add_argument(
"--output-file",
help=("Path to the output System Verilog file"),
default=_DEFAULT_OUT_DEFINES_FILE,
)
def output(self, config: I3CGenericConfig, args) -> None:
cfg2svh(config, args.output_file)
return f"Successfully saved configuration to {args.output_file}."
def main():
supported_targets = [
SVHFile(),
RegGenOpts(),
QuestaSimOpts(),
QuestaCompileOpts(),
VCSOpts(),
VerilatorSimOpts(),
]
argparser = argparse.ArgumentParser(
description=(
"Outputs I3C core configuration parameters in specified format "
"and the provided YAML configuration."
)
)
argparser.add_argument(
"config_name",
help="Name of the I3C Core configuration",
default=_DEFAULT_I3C_CONFIG_NAME,
)
argparser.add_argument(
"config_filename",
help="Name of the file containing `config_name` configuration",
default=_DEFAULT_CONFIG_FILE,
)
arg_subparser = argparser.add_subparsers(
help="Option format to be provided",
dest="output_fn",
metavar="opt_fmt",
)
for target in supported_targets:
target.setup_args(arg_subparser)
args = argparser.parse_args()
if args.output_fn is None:
raise ValueError("No target specified to output options for.")
cfg = parse_and_validate_config(args.config_name, args.config_filename)
print(args.output_fn(cfg, args))
if __name__ == "__main__":
main()