Coverage for pyVHDLModel/STD/EnvPkg.py: 100%

117 statements  

« prev     ^ index     » next       coverage.py v7.18.0, created at 2026-12-01 12:56 +0000

1# ==================================================================================================================== # 

2# __ ___ _ ____ _ __ __ _ _ # 

3# _ __ _ \ \ / / | | | _ \| | | \/ | ___ __| | ___| | # 

4# | '_ \| | | \ \ / /| |_| | | | | | | |\/| |/ _ \ / _` |/ _ \ | # 

5# | |_) | |_| |\ V / | _ | |_| | |___| | | | (_) | (_| | __/ | # 

6# | .__/ \__, | \_/ |_| |_|____/|_____|_| |_|\___/ \__,_|\___|_| # 

7# |_| |___/ # 

8# ==================================================================================================================== # 

9# Authors: # 

10# Patrick Lehmann # 

11# # 

12# License: # 

13# ==================================================================================================================== # 

14# Copyright 2017-2026 Patrick Lehmann - Boetzingen, Germany # 

15# Copyright 2016-2017 Patrick Lehmann - Dresden, Germany # 

16# # 

17# Licensed under the Apache License, Version 2.0 (the "License"); # 

18# you may not use this file except in compliance with the License. # 

19# You may obtain a copy of the License at # 

20# # 

21# http://www.apache.org/licenses/LICENSE-2.0 # 

22# # 

23# Unless required by applicable law or agreed to in writing, software # 

24# distributed under the License is distributed on an "AS IS" BASIS, # 

25# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # 

26# See the License for the specific language governing permissions and # 

27# limitations under the License. # 

28# # 

29# SPDX-License-Identifier: Apache-2.0 # 

30# ==================================================================================================================== # 

31# 

32"""This module contains the predefined package ``std.env`` of VHDL library ``STD``.""" 

33 

34from typing import Iterable, Optional as Nullable 

35 

36from pyTooling.Decorators import export 

37from pyTooling.Platform import CurrentPlatform 

38 

39from pyVHDLModel import VHDLVersion 

40from pyVHDLModel.Base import SimpleRange, Direction, Mode 

41from pyVHDLModel.Name import SimpleName 

42from pyVHDLModel.Symbol import SimpleSubtypeSymbol, ConstrainedScalarSubtypeSymbol, SimpleObjectOrFunctionCallSymbol 

43from pyVHDLModel.Expression import EnumerationLiteral, IntegerLiteral, StringLiteral, ConcatenationExpression 

44from pyVHDLModel.Type import EnumeratedType, ArrayType, AccessType, RecordType, RecordTypeElement 

45from pyVHDLModel.Object import Constant, DeferredConstant 

46from pyVHDLModel.Interface import ParameterConstantInterfaceItem, ParameterVariableInterfaceItem 

47from pyVHDLModel.Subprogram import Procedure, Function, Operator 

48from pyVHDLModel.Predefined import PredefinedPackage, PredefinedPackageBody 

49 

50 

51@export 

52class Env(PredefinedPackage): 

53 """ 

54 Predefined package ``std.env`` (IEEE Std 1076-2008, 16.5). 

55 

56 Its declarations, in the order of the standard: the procedures ``stop`` and ``finish``, each with and without a 

57 ``STATUS`` parameter, and the function ``resolution_limit``. 

58 

59 With :data:`VHDLVersion.VHDL2019 <pyVHDLModel.VHDLVersion.VHDL2019>`, it declares the additions of IEEE Std 

60 1076-2019, 16.5 after them, in the order of the standard: 

61 

62 * date and time: type ``dayofweek``, record type ``time_record``, functions like ``localtime`` and ``epoch``, 

63 the operators ``"+"`` and ``"-"`` of ``time_record`` and ``to_string`` 

64 * environment and tool: function ``getenv``, functions like ``vhdl_version`` and ``tool_name`` 

65 * directories and files: types ``directory_items`` and ``directory``, the status types like ``dir_open_status``, 

66 subprograms like ``dir_open`` and ``dir_createdir``, the deferred constant ``dir_separator`` 

67 * call path: types ``call_path_element``, ``call_path_vector`` and ``call_path_vector_ptr``, functions 

68 ``to_string``, ``get_call_path``, ``file_name``, ``file_path`` and ``file_line`` 

69 * assertions and PSL: subprograms like ``IsVhdlAssertFailed``, ``SetVhdlAssertEnable`` and ``ClearPslState`` 

70 

71 The declarations follow the package ``std/env.vhdl`` of the IEEE Std 1076-2019 packages, except where it 

72 contradicts the standard: a parameter of a type with a subelement of an access type is a variable (IEEE Std 

73 1076-2019, 6.5.2), e.g. ``Dir`` of ``dir_close`` (mode ``inout``, it deallocates the directory) and ``call_path`` 

74 of ``to_string``, and the operator ``"-"(DELTA, TREC)`` isn't declared. 

75 

76 .. seealso:: 

77 

78 Matching :class:`Package Body <pyVHDLModel.STD.EnvPkg.Env_Body>` declaration. 

79 """ 

80 

81 def __init__(self, vhdlVersion: VHDLVersion = VHDLVersion.VHDL2008) -> None: 

82 """ 

83 Initializes the ``env`` package. 

84 

85 :param vhdlVersion: Optional, the VHDL version of the declarations. Default: ``VHDLVersion.VHDL2008``. 

86 :raises ValueError: If parameter 'vhdlVersion' is None. 

87 :raises TypeError: If parameter 'vhdlVersion' is not of type :class:`~pyVHDLModel.VHDLVersion`. 

88 :raises ValueError: If parameter 'vhdlVersion' is not a supported VHDL version (VHDL-2008, VHDL-2019). 

89 """ 

90 super().__init__(vhdlVersion) 

91 

92 self._AddPackageClause(("work.textio.all",)) 

93 

94 for identifier in ("stop", "finish"): 

95 status = self._InParameter(("STATUS",), self._TypeMark("integer")) 

96 self._DeclareSubprogram(Procedure(identifier, parameterItems=(status,)), self._procedures) 

97 self._DeclareSubprogram(Procedure(identifier), self._procedures) 

98 

99 self._DeclareSubprogram(Function("resolution_limit", self._TypeMark("delay_length")), self._functions) 

100 

101 if vhdlVersion is VHDLVersion.VHDL2019: 

102 self._DeclareVHDL2019Additions() 

103 

104 def _DeclareVHDL2019Additions(self) -> None: 

105 """ 

106 Declare the additions of IEEE Std 1076-2019, 16.5, in the order of the standard. 

107 """ 

108 def function( 

109 identifier: str, 

110 returnType: SimpleSubtypeSymbol, 

111 parameterItems: Nullable[Iterable[ParameterConstantInterfaceItem | ParameterVariableInterfaceItem]] = None, 

112 isPure: bool = True 

113 ) -> None: 

114 """ 

115 Nested function declaring a function of this package. 

116 

117 :param identifier: The function's identifier. 

118 :param returnType: The type mark of the return type. 

119 :param parameterItems: Optional, the parameters, in declaration order. Default: ``None``, no parameters. 

120 :param isPure: Optional, ``False`` for an impure function. Default: ``True``, a pure function. 

121 """ 

122 self._DeclareSubprogram(Function(identifier, returnType, isPure, parameterItems=parameterItems), self._functions) 

123 

124 def procedure( 

125 identifier: str, 

126 parameterItems: Nullable[Iterable[ParameterConstantInterfaceItem | ParameterVariableInterfaceItem]] = None 

127 ) -> None: 

128 """ 

129 Nested function declaring a procedure of this package. 

130 

131 :param identifier: The procedure's identifier. 

132 :param parameterItems: Optional, the parameters, in declaration order. Default: ``None``, no parameters. 

133 """ 

134 self._DeclareSubprogram(Procedure(identifier, parameterItems=parameterItems), self._procedures) 

135 

136 def enumeration(identifier: str, literals: Iterable[str]) -> None: 

137 """ 

138 Nested function declaring an enumerated type of this package. 

139 

140 :param identifier: The type's identifier. 

141 :param literals: The identifiers of the enumeration literals, in declaration order. 

142 """ 

143 self._DeclareItem(EnumeratedType(identifier, [EnumerationLiteral(literal) for literal in literals]), self._types) 

144 

145 def integerRange(low: int, high: int) -> ConstrainedScalarSubtypeSymbol: 

146 """ 

147 Nested function creating the subtype indication ``INTEGER range low to high``. 

148 

149 :param low: The lower bound. 

150 :param high: The upper bound. 

151 :returns: The subtype indication. 

152 """ 

153 return ConstrainedScalarSubtypeSymbol( 

154 SimpleName("integer"), 

155 SimpleRange(IntegerLiteral(low), IntegerLiteral(high), Direction.To) 

156 ) 

157 

158 def literal(identifier: str) -> SimpleObjectOrFunctionCallSymbol: 

159 """ 

160 Nested function creating the name of an enumeration literal or a constant used as default value. 

161 

162 :param identifier: The literal's identifier, e.g. ``true``. 

163 :returns: The name. 

164 """ 

165 return SimpleObjectOrFunctionCallSymbol(SimpleName(identifier)) 

166 

167 # Date and time 

168 enumeration("dayofweek", ("sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday")) 

169 self._DeclareItem( 

170 RecordType("time_record", ( 

171 RecordTypeElement(("microsecond", ), integerRange(0, 999_999)), 

172 RecordTypeElement(("second", ), integerRange(0, 61)), 

173 RecordTypeElement(("minute", ), integerRange(0, 59)), 

174 RecordTypeElement(("hour", ), integerRange(0, 23)), 

175 RecordTypeElement(("day", ), integerRange(1, 31)), 

176 RecordTypeElement(("month", ), integerRange(0, 11)), 

177 RecordTypeElement(("year", ), integerRange(1, 4095)), 

178 RecordTypeElement(("weekday", ), self._TypeMark("dayofweek")), 

179 RecordTypeElement(("dayofyear", ), integerRange(0, 365)) 

180 )), 

181 self._types 

182 ) 

183 

184 function("localtime", self._TypeMark("time_record"), isPure=False) 

185 function("gmtime", self._TypeMark("time_record"), isPure=False) 

186 function("epoch", self._TypeMark("real"), isPure=False) 

187 for identifier in ("localtime", "gmtime"): 

188 function(identifier, self._TypeMark("time_record"), (self._InParameter(("TIMER", ), self._TypeMark("real")), )) 

189 

190 function("epoch", self._TypeMark("real"), (self._InParameter(("TREC", ), self._TypeMark("time_record")), )) 

191 for identifier in ("localtime", "gmtime"): 

192 function(identifier, self._TypeMark("time_record"), ( 

193 self._InParameter(("TREC", ), self._TypeMark("time_record")), 

194 )) 

195 

196 for operatorSymbol, left, leftType, right, rightType in ( 

197 ("+", "TREC", "time_record", "DELTA", "real"), 

198 ("+", "DELTA", "real", "TREC", "time_record"), 

199 ("-", "TREC", "time_record", "DELTA", "real") 

200 ): 

201 operator = Operator(operatorSymbol, self._TypeMark("time_record"), parameterItems=( 

202 self._InParameter((left, ), self._TypeMark(leftType)), 

203 self._InParameter((right, ), self._TypeMark(rightType)) 

204 )) 

205 self._DeclareSubprogram(operator, self._functions) 

206 

207 difference = Operator("-", self._TypeMark("real"), parameterItems=( 

208 self._InParameter(("TR1", "TR2"), self._TypeMark("time_record")), 

209 )) 

210 self._DeclareSubprogram(difference, self._functions) 

211 

212 function("time_to_seconds", self._TypeMark("real"), (self._InParameter(("TIME_VAL", ), self._TypeMark("time")), )) 

213 function("seconds_to_time", self._TypeMark("time"), (self._InParameter(("REAL_VAL", ), self._TypeMark("real")), )) 

214 function("to_string", self._TypeMark("string"), ( 

215 self._InParameter(("TREC", ), self._TypeMark("time_record")), 

216 ParameterConstantInterfaceItem(("FRAC_DIGITS", ), Mode.In, integerRange(0, 6), IntegerLiteral(0)) 

217 )) 

218 

219 # Environment and tool 

220 for returnType in ("string", "line"): 

221 function("getenv", self._TypeMark(returnType), ( 

222 self._InParameter(("Name", ), self._TypeMark("string")), 

223 ), isPure=False) 

224 

225 function("vhdl_version", self._TypeMark("string"), isPure=False) 

226 for identifier in ("tool_type", "tool_vendor", "tool_name", "tool_edition", "tool_version"): 

227 function(identifier, self._TypeMark("string")) 

228 

229 # Directories and files 

230 self._DeclareItem(AccessType("directory_items", self._TypeMark("line_vector")), self._types) 

231 self._DeclareItem( 

232 RecordType("directory", ( 

233 RecordTypeElement(("Name", ), self._TypeMark("line")), 

234 RecordTypeElement(("Items", ), self._TypeMark("directory_items")) 

235 )), 

236 self._types 

237 ) 

238 for identifier, literals in ( 

239 ("dir_open_status", ("status_ok", "status_not_found", "status_no_directory", "status_access_denied", 

240 "status_error")), 

241 ("dir_create_status", ("status_ok", "status_item_exists", "status_access_denied", "status_error")), 

242 ("dir_delete_status", ("status_ok", "status_no_directory", "status_not_empty", "status_access_denied", 

243 "status_error")), 

244 ("file_delete_status", ("status_ok", "status_no_file", "status_access_denied", "status_error")) 

245 ): 

246 enumeration(identifier, literals) 

247 

248 procedure("dir_open", ( 

249 self._OutParameter(("Dir", ), self._TypeMark("directory")), 

250 self._InParameter(("Path", ), self._TypeMark("string")), 

251 self._OutParameter(("Status", ), self._TypeMark("dir_open_status")) 

252 )) 

253 function("dir_open", self._TypeMark("dir_open_status"), ( 

254 self._OutParameter(("Dir", ), self._TypeMark("directory")), 

255 self._InParameter(("Path", ), self._TypeMark("string")) 

256 ), isPure=False) 

257 procedure("dir_close", (ParameterVariableInterfaceItem(("Dir", ), Mode.InOut, self._TypeMark("directory")), )) 

258 for identifier in ("dir_itemexists", "dir_itemisdir", "dir_itemisfile"): 

259 function(identifier, self._TypeMark("boolean"), ( 

260 self._InParameter(("Path", ), self._TypeMark("string")), 

261 ), isPure=False) 

262 

263 procedure("dir_workingdir", ( 

264 self._InParameter(("Path", ), self._TypeMark("string")), 

265 self._OutParameter(("Status", ), self._TypeMark("dir_open_status")) 

266 )) 

267 function("dir_workingdir", self._TypeMark("dir_open_status"), ( 

268 self._InParameter(("Path", ), self._TypeMark("string")), 

269 ), isPure=False) 

270 function("dir_workingdir", self._TypeMark("string"), isPure=False) 

271 

272 for identifier, option, status in ( 

273 ("dir_createdir", "Parents", "dir_create_status"), 

274 ("dir_deletedir", "Recursive", "dir_delete_status") 

275 ): 

276 procedure(identifier, ( 

277 self._InParameter(("Path", ), self._TypeMark("string")), 

278 self._OutParameter(("Status", ), self._TypeMark(status)) 

279 )) 

280 procedure(identifier, ( 

281 self._InParameter(("Path", ), self._TypeMark("string")), 

282 self._InParameter((option, ), self._TypeMark("boolean")), 

283 self._OutParameter(("Status", ), self._TypeMark(status)) 

284 )) 

285 function(identifier, self._TypeMark(status), ( 

286 self._InParameter(("Path", ), self._TypeMark("string")), 

287 self._InParameter((option, ), self._TypeMark("boolean"), literal("false")) 

288 ), isPure=False) 

289 

290 procedure("dir_deletefile", ( 

291 self._InParameter(("Path", ), self._TypeMark("string")), 

292 self._OutParameter(("Status", ), self._TypeMark("file_delete_status")) 

293 )) 

294 function("dir_deletefile", self._TypeMark("file_delete_status"), ( 

295 self._InParameter(("Path", ), self._TypeMark("string")), 

296 ), isPure=False) 

297 

298 dirSeparator = DeferredConstant(("dir_separator", ), self._TypeMark("string")) 

299 self._deferredConstants[dirSeparator._normalizedIdentifiers[0]] = dirSeparator 

300 self.AddDeclaredItem(dirSeparator) 

301 

302 # Call path 

303 self._DeclareItem( 

304 RecordType("call_path_element", ( 

305 RecordTypeElement(("name", ), self._TypeMark("line")), 

306 RecordTypeElement(("file_name", ), self._TypeMark("line")), 

307 RecordTypeElement(("file_path", ), self._TypeMark("line")), 

308 RecordTypeElement(("file_line", ), self._TypeMark("positive")) 

309 )), 

310 self._types 

311 ) 

312 function("to_string", self._TypeMark("string"), ( 

313 ParameterVariableInterfaceItem(("call_path", ), Mode.In, self._TypeMark("call_path_element")), 

314 ), isPure=False) 

315 for typeDefinition in ( 

316 ArrayType("call_path_vector", (self._TypeMark("natural"), ), self._TypeMark("call_path_element")), 

317 AccessType("call_path_vector_ptr", self._TypeMark("call_path_vector")) 

318 ): 

319 self._DeclareItem(typeDefinition, self._types) 

320 separator = ConcatenationExpression(StringLiteral(""), literal("lf")) 

321 function("to_string", self._TypeMark("string"), ( 

322 ParameterVariableInterfaceItem(("call_path", ), Mode.In, self._TypeMark(typeDefinition._identifier)), 

323 self._InParameter(("Separator", ), self._TypeMark("string"), separator) 

324 ), isPure=False) 

325 

326 function("get_call_path", self._TypeMark("call_path_vector_ptr"), isPure=False) 

327 for identifier, returnTypes in ( 

328 ("file_name", ("line", "string")), 

329 ("file_path", ("line", "string")), 

330 ("file_line", ("positive", "string")) 

331 ): 

332 for returnType in returnTypes: 

333 function(identifier, self._TypeMark(returnType), isPure=False) 

334 

335 # Assertions and PSL 

336 for identifier, returnType in (("IsVhdlAssertFailed", "boolean"), ("GetVhdlAssertCount", "natural")): 

337 function(identifier, self._TypeMark(returnType), isPure=False) 

338 function(identifier, self._TypeMark(returnType), ( 

339 self._InParameter(("Level", ), self._TypeMark("severity_level")), 

340 ), isPure=False) 

341 

342 procedure("ClearVhdlAssert") 

343 procedure("SetVhdlAssertEnable", (self._InParameter(("Enable", ), self._TypeMark("boolean"), literal("true")), )) 

344 procedure("SetVhdlAssertEnable", ( 

345 self._InParameter(("Level", ), self._TypeMark("severity_level"), literal("note")), 

346 self._InParameter(("Enable", ), self._TypeMark("boolean"), literal("true")) 

347 )) 

348 function("GetVhdlAssertEnable", self._TypeMark("boolean"), ( 

349 self._InParameter(("Level", ), self._TypeMark("severity_level"), literal("note")), 

350 ), isPure=False) 

351 procedure("SetVhdlAssertFormat", ( 

352 self._InParameter(("Level", ), self._TypeMark("severity_level")), 

353 self._InParameter(("Format", ), self._TypeMark("string")) 

354 )) 

355 procedure("SetVhdlAssertFormat", ( 

356 self._InParameter(("Level", ), self._TypeMark("severity_level")), 

357 self._InParameter(("Format", ), self._TypeMark("string")), 

358 self._OutParameter(("Valid", ), self._TypeMark("boolean")) 

359 )) 

360 function("GetVhdlAssertFormat", self._TypeMark("string"), ( 

361 self._InParameter(("Level", ), self._TypeMark("severity_level")), 

362 ), isPure=False) 

363 procedure("SetVhdlReadSeverity", ( 

364 self._InParameter(("Level", ), self._TypeMark("severity_level"), literal("failure")), 

365 )) 

366 function("GetVhdlReadSeverity", self._TypeMark("severity_level"), isPure=False) 

367 

368 function("PslAssertFailed", self._TypeMark("boolean"), isPure=False) 

369 function("PslIsCovered", self._TypeMark("boolean"), isPure=False) 

370 procedure("SetPslCoverAssert", (self._InParameter(("Enable", ), self._TypeMark("boolean"), literal("true")), )) 

371 function("GetPslCoverAssert", self._TypeMark("boolean"), isPure=False) 

372 function("PslIsAssertCovered", self._TypeMark("boolean"), isPure=False) 

373 procedure("ClearPslState") 

374 

375 

376@export 

377class Env_Body(PredefinedPackageBody): 

378 """ 

379 Predefined package body of package ``std.env``. 

380 

381 With :data:`VHDLVersion.VHDL2019 <pyVHDLModel.VHDLVersion.VHDL2019>`, it declares the full constant of the deferred 

382 constant ``dir_separator``, with the path separator of the host (``"\\"`` on Windows, ``"/"`` otherwise, read from 

383 :data:`CurrentPlatform.PathSeperator <pyTooling.Platform.Platform.PathSeperator>`); IEEE Std 1076-2019, 16.5.5 leaves 

384 the value to the implementation. 

385 

386 .. seealso:: 

387 

388 Matching :class:`Package <pyVHDLModel.STD.EnvPkg.Env>` declaration. 

389 """ 

390 

391 def __init__(self, vhdlVersion: VHDLVersion = VHDLVersion.VHDL2008) -> None: 

392 """ 

393 Initializes the ``env`` package body. 

394 

395 :param vhdlVersion: Optional, the VHDL version of the declarations. Default: ``VHDLVersion.VHDL2008``. 

396 :raises ValueError: If parameter 'vhdlVersion' is None. 

397 :raises TypeError: If parameter 'vhdlVersion' is not of type :class:`~pyVHDLModel.VHDLVersion`. 

398 :raises ValueError: If parameter 'vhdlVersion' is not a supported VHDL version (VHDL-2008, VHDL-2019). 

399 """ 

400 super().__init__(vhdlVersion) 

401 

402 if vhdlVersion is VHDLVersion.VHDL2019: 

403 separator = StringLiteral(CurrentPlatform.PathSeperator) 

404 dirSeparator = Constant(("dir_separator", ), self._TypeMark("string"), separator) 

405 self._constants[dirSeparator._normalizedIdentifiers[0]] = dirSeparator 

406 self.AddDeclaredItem(dirSeparator)