pyEDAA.OSVVM.Project.Procedures

pyEDAA/OSVVM/Project/Procedures.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
# ==================================================================================================================== #
#              _____ ____    _        _      ___  ______     ____     ____  __                                         #
#  _ __  _   _| ____|  _ \  / \      / \    / _ \/ ___\ \   / /\ \   / /  \/  |                                        #
# | '_ \| | | |  _| | | | |/ _ \    / _ \  | | | \___ \\ \ / /  \ \ / /| |\/| |                                        #
# | |_) | |_| | |___| |_| / ___ \  / ___ \ | |_| |___) |\ V /    \ V / | |  | |                                        #
# | .__/ \__, |_____|____/_/   \_\/_/   \_(_)___/|____/  \_/      \_/  |_|  |_|                                        #
# |_|    |___/                                                                                                         #
# ==================================================================================================================== #
# Authors:                                                                                                             #
#   Patrick Lehmann                                                                                                    #
#                                                                                                                      #
# License:                                                                                                             #
# ==================================================================================================================== #
# Copyright 2025-2026 Patrick Lehmann - Boetzingen, Germany                                                            #
#                                                                                                                      #
# Licensed under the Apache License, Version 2.0 (the "License");                                                      #
# you may not use this file except in compliance with the License.                                                     #
# You may obtain a copy of the License at                                                                              #
#                                                                                                                      #
#   http://www.apache.org/licenses/LICENSE-2.0                                                                         #
#                                                                                                                      #
# Unless required by applicable law or agreed to in writing, software                                                  #
# distributed under the License is distributed on an "AS IS" BASIS,                                                    #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.                                             #
# See the License for the specific language governing permissions and                                                  #
# limitations under the License.                                                                                       #
#                                                                                                                      #
# SPDX-License-Identifier: Apache-2.0                                                                                  #
# ==================================================================================================================== #
#
"""
This module implements OSVVM's TCL procedures (used in OSVVM's ``*.pro`` files) as Python functions.

These functions are then registered at the :class:`TCL processor <pyEDAA.OSVVM.Project.TCL.OsvvmProFileProcessor>`, so
procedure calls within TCL code get "redirected" to these Python functions. Each Python function has access to a global
variable :data:`~pyEDAA.OSVVM.Project.osvvmContext` to preserve its state or modify the context.

.. important::

   For passing Python exceptions through the TCL layer back into Python, every function in the module MUST follow the
   following scheme:

   .. code-block:: Python

      def myTclProcedure(....) -> ...:
        try:
          # do something

        except OSVVMException as ex:  # pragma: no cover
          raise ex
        except Exception as ex:       # pragma: no cover
          osvvmContext.RaiseException(ex)
"""
from pathlib               import Path
from typing                import Optional as Nullable

from pyTooling.Decorators  import export
from pyTooling.Common      import getFullyQualifiedName
from pyVHDLModel           import VHDLVersion

from pyEDAA.OSVVM          import OSVVMException
from pyEDAA.OSVVM.Project  import osvvmContext, VHDLSourceFile, GenericValue, ConstraintFile as OSVVM_ConstraintFile
from pyEDAA.OSVVM.Project  import XDCConstraintFile, ScopeToRef as OSVVM_ScopeToRef, ScopeToCell as OSVVM_ScopeToCell
from pyEDAA.OSVVM.Project  import BuildName as OSVVM_BuildName, NoNullRangeWarning as OSVVM_NoNullRangeWarning


@export
def BuildName(name: str) -> int:
	"""
	This function implements the behavior of OSVVM's ``BuildName`` procedure.

	Create and register a :class:`~pyEDAA.OSVVM.Project.BuildName` option and return the options unique ID.

	:param name: Name of the build.
	:returns:    The option's unique ID.
	"""
	try:
		buildName = OSVVM_BuildName(name)
		return osvvmContext.AddOption(buildName)
	except OSVVMException as ex:  # pragma: no cover
		raise ex
	except Exception as ex:       # pragma: no cover
		osvvmContext.RaiseException(ex)


@export
def build(file: str, *options: int) -> None:
	"""
	This function implements the behavior of OSVVM's ``build`` procedure.

	The current directory of the currently active context is preserved	while the referenced ``*.pro`` file is processed.
	After processing that file, the context's current directory is restored.

	The referenced file gets appended to a list of included files maintained by the context.

	.. rubric:: pro-file discovery algorithm:

	1. If the path explicitly references a ``*.pro`` file, this file is used.
	2. If the path references a directory, it checks implicitly for a ``build.pro`` file, otherwise
	3. it checks implicitly for a ``<path>.pro`` file, named like the directories name.

	.. rubric:: inferring the build name:

	1. The option :class:`~pyEDAA.OSVVM.Project.BuildName` was gives (indirectly via option ID) as parameter.
	2. It's derived from the current directory name.

	:param file:            Explicit path to a ``*.pro`` file or a directory containing an implicitly searched ``*.pro``
	                        file.
	:param options:         Optional, list of option IDs.
	:raises OSVVMException: If parameter 'options' contains unknown option IDS.
	:raises OSVVMException: If parameter 'options' contains an option not of type :class:`~pyEDAA.OSVVM.Project.BuildName`.

	.. seealso::

	   * :func:`BuildName`
	   * :func:`include`
	   * :func:`ChangeWorkingDirectory`
	"""
	try:
		file = Path(file)
		buildName = None

		# Preserve current directory
		currentDirectory = osvvmContext._currentDirectory

		for optionID in options:
			try:
				option = osvvmContext._options[int(optionID)]
			except KeyError as e:  # pragma: no cover
				ex = OSVVMException(f"Option {optionID} not found in option dictionary.")
				ex.__cause__ = e
				osvvmContext.RaiseException(ex)

			if isinstance(option, OSVVM_BuildName):
				buildName = option.Name
			else:  # pragma: no cover
				ex = OSVVMException(f"Option {optionID} is not a BuildName.")
				ex.__cause__ = TypeError()
				osvvmContext.LastException = ex
				raise ex

		# If no build name was specified, derive a name from *.pro file.
		if buildName is None:
			buildName = file.stem

		osvvmContext.BeginBuild(buildName)
		includeFile = osvvmContext.IncludeFile(file)
		osvvmContext.EvaluateFile(includeFile)
		osvvmContext.EndBuild()

		# Restore current directory after recursively evaluating *.pro files.
		osvvmContext._currentDirectory = currentDirectory

	except OSVVMException as ex:  # pragma: no cover
		raise ex
	except Exception as ex:       # pragma: no cover
		osvvmContext.RaiseException(ex)


@export
def include(file: str) -> None:
	"""
	This function implements the behavior of OSVVM's ``include`` procedure.

	The current directory of the currently active context is preserved	while the referenced ``*.pro`` file is processed.
	After processing that file, the context's current directory is restored.

	The referenced file gets appended to a list of included files maintained by the context.

	.. rubric:: pro-file discovery algorithm:

	1. If the path explicitly references a ``*.pro`` file, this file is used.
	2. If the path references a directory, it checks implicitly for a ``build.pro`` file, otherwise
	3. it checks implicitly for a ``<path>.pro`` file, named like the directories name.

	:param file:            Explicit path to a ``*.pro`` file or a directory containing an implicitly searched ``*.pro``
	                        file.

	.. seealso::

	   * :func:`build`
	   * :func:`ChangeWorkingDirectory`
	"""
	try:
		# Preserve current directory
		currentDirectory = osvvmContext._currentDirectory

		includeFile = osvvmContext.IncludeFile(Path(file))
		osvvmContext.EvaluateFile(includeFile)

		# Restore current directory after recursively evaluating *.pro files.
		osvvmContext._currentDirectory = currentDirectory

	except OSVVMException as ex:  # pragma: no cover
		raise ex
	except Exception as ex:       # pragma: no cover
		osvvmContext.RaiseException(ex)


@export
def library(libraryName: str, libraryPath: Nullable[str] = None) -> None:
	"""
	This function implements the behavior of OSVVM's ``library`` procedure.

	It sets the currently active VHDL library to the specified VHDL library. If no VHDL library with that name exist, a
	new VHDL library is created and set as active VHDL library.

	.. hint::

	   All following ``analyze`` calls will use this library as the VHDL source file's VHDL library.

	.. caution::

	   Parameter `libraryPath` is not yet implemented.

	:param libraryName:          Name of the VHDL library.
	:param libraryPath:          Optional, path where to create that VHDL library.
	:raises NotImplementedError: When parameter 'libraryPath' is not None.

	.. seealso::

	   * :func:`LinkLibrary`
	   * :func:`LinkLibraryDirectory`
	"""
	try:
		if libraryPath is not None:
			raise NotImplementedError(f"Optional parameter 'libraryPath' not yet supported.")

		osvvmContext.SetLibrary(libraryName)

	except OSVVMException as ex:  # pragma: no cover
		raise ex
	except Exception as ex:       # pragma: no cover
		osvvmContext.RaiseException(ex)


@export
def NoNullRangeWarning() -> int:
	"""
	This function implements the behavior of OSVVM's ``NoNullRangeWarning`` procedure.

	Create and register a :class:`~pyEDAA.OSVVM.Project.NoNullRangeWarning` option and return the options unique ID.

	:returns: The option's unique ID.
	"""
	try:
		option = OSVVM_NoNullRangeWarning()
		return osvvmContext.AddOption(option)
	except OSVVMException as ex:  # pragma: no cover
		raise ex
	except Exception as ex:       # pragma: no cover
		osvvmContext.RaiseException(ex)


@export
def analyze(file: str, *options: int) -> None:
	"""
	This function implements the behavior of OSVVM's ``analyze`` procedure.

	Analyze an HDL source file.

  .. rubric:: Supported options:

  * :func:`NoNullRangeWarning` - disable null-range warnings when analyzing.
  * :func:`ConstraintFile` - associated constraint file

	:param file:            Path of the VHDL source file.
	:param options:         Optional, list of option IDs.
	:raises OSVVMException: When the referenced source file doesn't exist.
	:raises OSVVMException: When the referenced source file isn't an ``*.vhd`` or ``*.vhdl`` file.
	:raises OSVVMException: When parameter 'options' contains an unknown option ID.
	:raises OSVVMException: When referenced option is not a :class:`~pyEDAA.OSVVM.Project.NoNullRangeWarning` or
	                        :class:`~pyEDAA.OSVVM.Project.ConstraintFile`.

	.. seealso::

	   * :func:`NoNullRangeWarning`
	   * :func:`SetCoverageAnalyzeEnable`
	   * :func:`ConstraintFile`
	"""
	try:
		file = Path(file)
		fullPath = (osvvmContext._currentDirectory / file).resolve()

		noNullRangeWarning = None
		associatedConstraintFiles = []
		for optionID in options:
			try:
				option = osvvmContext._options[int(optionID)]
			except KeyError as ex:  # pragma: no cover
				osvvmContext.RaiseException(OSVVMException(f"Option {optionID} not found in option dictionary."), ex)

			if isinstance(option, OSVVM_NoNullRangeWarning):
				noNullRangeWarning = True
			elif isinstance(option, OSVVM_ConstraintFile):
				associatedConstraintFiles.append(XDCConstraintFile(
					option.Path,
					option.ScopeToRef,
					option.ScopeToCell
				))
			else:  # pragma: no cover
				ex = TypeError(f"Option {optionID} is not a NoNullRangeWarning or ConstraintFile.")
				ex.add_note(f"Got type '{getFullyQualifiedName(option)}'.")
				osvvmContext.RaiseException(OSVVMException(f"Dereferenced option ID is not a NoNullRangeWarning or ConstraintFile object"), ex)

		if not fullPath.exists():  # pragma: no cover
			osvvmContext.RaiseException(OSVVMException(f"Path '{fullPath}' can't be analyzed."), FileNotFoundError(fullPath))

		if fullPath.suffix in (".vhd", ".vhdl"):
			vhdlFile = VHDLSourceFile(
				fullPath.relative_to(osvvmContext._workingDirectory, walk_up=True),
				noNullRangeWarning=noNullRangeWarning,
				associatedFiles=associatedConstraintFiles
			)
			osvvmContext.AddVHDLFile(vhdlFile)
		else:  # pragma: no cover
			osvvmContext.RaiseException(OSVVMException(f"Path '{fullPath}' is no VHDL file (*.vhd, *.vhdl)."))

	except OSVVMException as ex:  # pragma: no cover
		raise ex
	except Exception as ex:       # pragma: no cover
		osvvmContext.RaiseException(ex)


@export
def simulate(toplevelName: str, *options: int) -> None:
	"""
	This function implements the behavior of OSVVM's ``simulate`` procedure.

	Simulate a given toplevel entity or configuration name.

  .. rubric:: Supported options:

  * :func:`generic` - specify generic values.

	:param toplevelName:    Name of the toplevel.
	:param options:         Optional, list of option IDs.
	:raises ValueError:     When parameter 'toplevelName' is empty.
	:raises OSVVMException: When parameter 'options' contains an unknown option ID.
	:raises OSVVMException: When referenced option is not a :class:`~pyEDAA.OSVVM.Project.GenericValue`.

	.. seealso::

	   * :func:`generic`
	   * :func:`RunTest`
	"""
	try:
		if toplevelName == "":
			raise ValueError(f"Parameter 'toplevelName' is empty.")

		testcase = osvvmContext.SetTestcaseToplevel(toplevelName)
		for optionID in options:
			try:
				option = osvvmContext._options[int(optionID)]
			except KeyError as ex:  # pragma: no cover
				osvvmContext.RaiseException(OSVVMException(f"Option {optionID} not found in option dictionary."), ex)

			if isinstance(option, GenericValue):
				testcase.AddGeneric(option)
			else:  # pragma: no cover
				ex = TypeError(f"Option {optionID} is not a GenericValue.")
				ex.add_note(f"Got type '{getFullyQualifiedName(option)}'.")
				osvvmContext.RaiseException(OSVVMException(f"Dereferenced option ID is not a GenericValue object"), ex)

	except OSVVMException as ex:  # pragma: no cover
		raise ex
	except Exception as ex:       # pragma: no cover
		osvvmContext.RaiseException(ex)


@export
def generic(name: str, value: str) -> int:
	"""
	This function implements the behavior of OSVVM's ``generic`` procedure.

	Create and register a :class:`~pyEDAA.OSVVM.Project.GenericValue` option and return the options unique ID.

	:param name:  Name of the generic.
	:param value: Value of the generic.
	:returns:     The option's unique ID.
	"""
	try:
		genericValue = GenericValue(name, value)
		return osvvmContext.AddOption(genericValue)
	except OSVVMException as ex:  # pragma: no cover
		raise ex
	except Exception as ex:       # pragma: no cover
		osvvmContext.RaiseException(ex)


@export
def TestSuite(name: str) -> None:
	"""
	This function implements the behavior of OSVVM's ``TestSuite`` procedure.

	Set or create the currently active :class:`~pyEDAA.OSVVM.Project.Testsuite`.

	:param name: Name of the OSVVM testsuite.
	"""
	try:
		osvvmContext.SetTestsuite(name)
	except OSVVMException as ex:  # pragma: no cover
		raise ex
	except Exception as ex:       # pragma: no cover
		osvvmContext.RaiseException(ex)


@export
def TestName(name: str) -> None:
	"""
	This function implements the behavior of OSVVM's ``TestName`` procedure.

	Create a new :class:`~pyEDAA.OSVVM.Project.Testcase`.

	:param name: Name of the OSVVM testcase.
	"""
	try:
		osvvmContext.AddTestcase(name)
	except OSVVMException as ex:  # pragma: no cover
		raise ex
	except Exception as ex:       # pragma: no cover
		osvvmContext.RaiseException(ex)


@export
def RunTest(file: str, *options: int) -> None:
	"""
	This function implements the behavior of OSVVM's ``RunTest`` procedure.

	Simulate a given toplevel entity or configuration name. Infer testcase name from filename.

  .. rubric:: Supported options:

  * :func:`generic` - specify generic values.

	:param file:            Path of the VHDL source file containing the toplevel.
	:param options:         Optional, list of option IDs.
	:raises OSVVMException: When the referenced source file doesn't exist.
	:raises OSVVMException: When the referenced source file isn't an ``*.vhd`` or ``*.vhdl`` file.
	:raises OSVVMException: When parameter 'options' contains an unknown option ID.
	:raises OSVVMException: When referenced option is not a :class:`~pyEDAA.OSVVM.Project.GenericValue`.

	.. seealso::

	   * :func:`generic`
	   * :func:`simulate`
	"""
	try:
		file = Path(file)
		testName = file.stem

		# Analyze file
		fullPath = (osvvmContext._currentDirectory / file).resolve()

		if not fullPath.exists():  # pragma: no cover
			osvvmContext.RaiseException(OSVVMException(f"Path '{fullPath}' can't be analyzed."), FileNotFoundError(fullPath))

		if fullPath.suffix in (".vhd", ".vhdl"):
			vhdlFile = VHDLSourceFile(fullPath.relative_to(osvvmContext._workingDirectory, walk_up=True))
			osvvmContext.AddVHDLFile(vhdlFile)
		else:  # pragma: no cover
			osvvmContext.RaiseException(OSVVMException(f"Path '{fullPath}' is no VHDL file (*.vhd, *.vhdl)."))

		# Add testcase
		testcase = osvvmContext.AddTestcase(testName)
		testcase.SetToplevel(testName)
		for optionID in options:
			try:
				option = osvvmContext._options[int(optionID)]
			except KeyError as ex:  # pragma: no cover
				osvvmContext.RaiseException(OSVVMException(f"Option {optionID} not found in option dictionary."), ex)

			if isinstance(option, GenericValue):
				testcase.AddGeneric(option)
			else:  # pragma: no cover
				ex = TypeError(f"Option {optionID} is not a GenericValue.")
				ex.add_note(f"Got type '{getFullyQualifiedName(option)}'.")
				osvvmContext.RaiseException(OSVVMException(f"Dereferenced option ID is not a GenericValue object"), ex)

	except OSVVMException as ex:  # pragma: no cover
		raise ex
	except Exception as ex:       # pragma: no cover
		osvvmContext.RaiseException(ex)


@export
def LinkLibrary(libraryName: str, libraryPath: Nullable[str] = None):
	"""
	Not implemented by pyEDAA.OSVVM.
	"""
	osvvmContext.RaiseException(NotImplementedError(f"Procedure 'LinkLibrary' is not implemented."))


@export
def LinkLibraryDirectory(libraryDirectory: str):
	"""
	Not implemented by pyEDAA.OSVVM.
	"""
	osvvmContext.RaiseException(NotImplementedError(f"Procedure 'LinkLibraryDirectory' is not implemented."))


@export
def SetVHDLVersion(value: str) -> None:
	"""
	This function implements the behavior of OSVVM's ``SetVHDLVersion`` procedure.

	Set the used VHDL language revision.

	.. hint::

	   All following ``analyze`` calls will use this VHDL revision.

	:param value:           The VHDL language revision's release year.
	:raises ValueError:     When parameter 'value' is not an integer value.
	:raises OSVVMException: When parameter 'value' is not a known VHDL revision's release year.

	.. seealso::

	   * :func:`GetVHDLVersion`
	"""
	try:
		try:
			value = int(value)
		except ValueError as e:  # pragma: no cover
			ex = ValueError(f"Parameter 'value' is not an integer value.")
			ex.add_note(f"Got '{value}'.")
			osvvmContext.RaiseException(ex, e)

		match value:
			case 1987:
				osvvmContext.VHDLVersion = VHDLVersion.VHDL87
			case 1993:
				osvvmContext.VHDLVersion = VHDLVersion.VHDL93
			case 2002:
				osvvmContext.VHDLVersion = VHDLVersion.VHDL2002
			case 2008:
				osvvmContext.VHDLVersion = VHDLVersion.VHDL2008
			case 2019:
				osvvmContext.VHDLVersion = VHDLVersion.VHDL2019
			case _:  # pragma: no cover
				osvvmContext.RaiseException(OSVVMException(f"Unsupported VHDL version '{value}'."))

	except OSVVMException as ex:  # pragma: no cover
		raise ex
	except Exception as ex:       # pragma: no cover
		osvvmContext.RaiseException(ex)


@export
def GetVHDLVersion() -> int:
	"""
	This function implements the behavior of OSVVM's ``GetVHDLVersion`` procedure.

	Returns the currently set VHDL language revision.

	:returns:               The VHDL language revision's release year.
	:raises OSVVMException: When the currently set VHDL language revision is unknown in this decoding function.

	.. seealso::

	   * :func:`SetVHDLVersion`
	"""
	try:
		if osvvmContext.VHDLVersion is VHDLVersion.VHDL87:
			return 1987
		elif osvvmContext.VHDLVersion is VHDLVersion.VHDL93:
			return 1993
		elif osvvmContext.VHDLVersion is VHDLVersion.VHDL2002:
			return 2002
		elif osvvmContext.VHDLVersion is VHDLVersion.VHDL2008:
			return 2008
		elif osvvmContext.VHDLVersion is VHDLVersion.VHDL2019:
			return 2019
		else:  # pragma: no cover
			osvvmContext.RaiseException(OSVVMException(f"Unsupported VHDL version '{osvvmContext.VHDLVersion}'."))

	except OSVVMException as ex:  # pragma: no cover
		raise ex
	except Exception as ex:       # pragma: no cover
		osvvmContext.RaiseException(ex)


@export
def SetCoverageAnalyzeEnable(value: bool) -> None:
	"""
	Not implemented by pyEDAA.OSVVM.
	"""
	osvvmContext.RaiseException(NotImplementedError(f"Procedure 'SetCoverageAnalyzeEnable' is not implemented."))


@export
def SetCoverageSimulateEnable(value: bool) -> None:
	"""
	Not implemented by pyEDAA.OSVVM.
	"""
	osvvmContext.RaiseException(NotImplementedError(f"Procedure 'SetCoverageSimulateEnable' is not implemented."))


@export
def FileExists(file: str) -> bool:
	"""
	This function implements the behavior of OSVVM's ``FileExists`` procedure.

	Check if the given file exists.

	:param file:        File name.
	:returns:           True, if file exists, otherwise False.
	:raises ValueError: When parameter 'file' is empty.

	.. seealso::

	   * :func:`DirectoryExists`
	"""
	try:
		if file == "":
			raise ValueError(f"Parameter 'file' is empty.")

		return (osvvmContext._currentDirectory / file).is_file()

	except OSVVMException as ex:  # pragma: no cover
		raise ex
	except Exception as ex:       # pragma: no cover
		osvvmContext.RaiseException(ex)


@export
def DirectoryExists(directory: str) -> bool:
	"""
	This function implements the behavior of OSVVM's ``DirectoryExists`` procedure.

	Check if the given directory exists.

	:param directory:   Directory name.
	:returns:           True, if directory exists, otherwise False.
	:raises ValueError: When parameter 'directory' is empty.

	.. seealso::

	   * :func:`FileExists`
	"""
	try:
		if directory == "":
			raise ValueError(f"Parameter 'directory' is empty.")

		return (osvvmContext._currentDirectory / directory).is_dir()

	except OSVVMException as ex:  # pragma: no cover
		raise ex
	except Exception as ex:       # pragma: no cover
		osvvmContext.RaiseException(ex)


@export
def ChangeWorkingDirectory(directory: str) -> None:
	"""
	This function implements the behavior of OSVVM's ``ChangeWorkingDirectory`` procedure.

	Change the current directory (virtual working directory) to the given directory.

	:param directory:       Directory name.
	:raises ValueError:     When parameter 'directory' is empty.
	:raises OSVVMException: When the referenced directory doesn't exist.

	.. seealso::

	   * :func:`build`
	   * :func:`include`
	"""
	try:
		if directory == "":
			raise ValueError(f"Parameter 'directory' is empty.")

		osvvmContext._currentDirectory = (newDirectory := osvvmContext._currentDirectory / directory)
		if not newDirectory.is_dir():  # pragma: no cover
			osvvmContext.RaiseException(OSVVMException(f"Directory '{newDirectory}' doesn't exist."), NotADirectoryError(newDirectory))

	except OSVVMException as ex:  # pragma: no cover
		raise ex
	except Exception as ex:       # pragma: no cover
		osvvmContext.RaiseException(ex)


@export
def FindOsvvmSettingsDirectory(*args) -> None:
	"""
	Not implemented by pyEDAA.OSVVM.
	"""
	osvvmContext.RaiseException(NotImplementedError(f"Procedure 'FindOsvvmSettingsDirectory' is not implemented."))


@export
def CreateOsvvmScriptSettingsPkg(*args) -> None:
	"""
	Not implemented by pyEDAA.OSVVM.
	"""
	osvvmContext.RaiseException(NotImplementedError(f"Procedure 'CreateOsvvmScriptSettingsPkg' is not implemented."))


@export
def noop(*args) -> None:
	"""
	A no-operation dummy procedure accepting any positional arguments.

	:param args: Any arguments
	"""


@export
def ConstraintFile(file: str, *options: int) -> int:
	"""
	This function implements the behavior of pyEDAA's ``ConstraintFile`` procedure.

	Create and register a :class:`~pyEDAA.OSVVM.Project.ConstraintFile` option and return the options unique ID.

	:param file:            Path to the constraint file.
	:param options:         Optional, list of option IDs.
	:returns:               The option's unique ID.
	:raises OSVVMException: When parameter 'options' contains an unknown option ID.
	:raises OSVVMException: When referenced option is not a :class:`~pyEDAA.OSVVM.Project.ScopeToRef` or
	                        :class:`~pyEDAA.OSVVM.Project.ScopeToCell`.
	:raises OSVVMException: When the referenced constraint file doesn't exist.
	:raises OSVVMException: When the referenced constraint file isn't an ``*.sdc`` or ``*.xdc`` file.
	"""
	try:
		file = Path(file)
		fullPath = (osvvmContext._currentDirectory / file).resolve()

		properties = {}
		for optionID in options:
			try:
				option = osvvmContext._options[int(optionID)]
			except KeyError as ex:  # pragma: no cover
				osvvmContext.RaiseException(OSVVMException(f"Option {optionID} not found in option dictionary."), ex)

			if isinstance(option, OSVVM_ScopeToRef):
				properties["scopeToRef"] = option.Reference
			elif isinstance(option, OSVVM_ScopeToCell):
				properties["scopeToCell"] = option.Cell
			else:  # pragma: no cover
				ex = TypeError(f"Option {optionID} is not a ScopeToRef or ScopeToCell.")
				ex.add_note(f"Got type '{getFullyQualifiedName(option)}'.")
				osvvmContext.RaiseException(OSVVMException(f"Dereferenced option ID is not a ScopeToRef or ScopeToCell object"), ex)

		if not fullPath.exists():  # pragma: no cover
			osvvmContext.RaiseException(OSVVMException(f"Constraint file '{fullPath}' can't be found."), FileNotFoundError(fullPath))

		if not fullPath.suffix in (".sdc", ".xdc"):
			osvvmContext.RaiseException(OSVVMException(f"Path '{fullPath}' is no constraint file (*.sdc, *.xdc)."))

		constraint = OSVVM_ConstraintFile(Path(file), **properties)
		return osvvmContext.AddOption(constraint)

	except OSVVMException as ex:  # pragma: no cover
		raise ex
	except Exception as ex:       # pragma: no cover
		osvvmContext.RaiseException(ex)


@export
def ScopeToRef(refName: str) -> int:
	"""
	This function implements the behavior of pyEDAA's ``ScopeToRef`` procedure.

	Create and register a :class:`~pyEDAA.OSVVM.Project.ScopeToRef` option and return the options unique ID.

	:param refName:     Reference name.
	:returns:           The option's unique ID.
	:raises ValueError: When parameter 'refName' is empty.
	"""
	try:
		if refName == "":
			raise ValueError("Parameter 'refName' is a empty string.")

		ref = OSVVM_ScopeToRef(refName)
		return osvvmContext.AddOption(ref)
	except OSVVMException as ex:  # pragma: no cover
		raise ex
	except Exception as ex:       # pragma: no cover
		osvvmContext.RaiseException(ex)


@export
def ScopeToCell(cellName: str) -> int:
	"""
	This function implements the behavior of pyEDAA's ``ScopeToCell`` procedure.

	Create and register a :class:`~pyEDAA.OSVVM.Project.ScopeToCell` option and return the options unique ID.

	:param cellName:    Cell name.
	:returns:           The option's unique ID.
	:raises ValueError: When parameter 'cellName' is empty.
	"""
	try:
		if cellName == "":
			raise ValueError("Parameter 'cellName' is a empty string.")

		ref = OSVVM_ScopeToCell(cellName)
		return osvvmContext.AddOption(ref)
	except OSVVMException as ex:  # pragma: no cover
		raise ex
	except Exception as ex:       # pragma: no cover
		osvvmContext.RaiseException(ex)