vunit-python-bridge 0.3.0__py3-none-any.whl → 0.4.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- vunit_python_bridge/__init__.py +9 -24
- vunit_python_bridge/bin/vunit_python_bridge-cp310-win_amd64.dll +0 -0
- vunit_python_bridge/bin/vunit_python_bridge-cp311-win_amd64.dll +0 -0
- vunit_python_bridge/bin/vunit_python_bridge-cp312-win_amd64.dll +0 -0
- vunit_python_bridge/bin/vunit_python_bridge-cp313-win_amd64.dll +0 -0
- vunit_python_bridge/bin/vunit_python_bridge-cp314-win_amd64.dll +0 -0
- vunit_python_bridge/bin/vunit_python_bridge-cp315-win_amd64.dll +0 -0
- vunit_python_bridge/bridge.py +112 -16
- vunit_python_bridge/hdl/src/python_bridge_pkg.vhd.in +24 -19
- vunit_python_bridge/hdl/src/python_ffi_pkg_bridge.vhd +98 -28
- vunit_python_bridge/native_library.py +10 -2
- vunit_python_bridge/runtime.py +85 -26
- vunit_python_bridge/simulator_hooks.py +44 -15
- {vunit_python_bridge-0.3.0.dist-info → vunit_python_bridge-0.4.1.dist-info}/METADATA +5 -1
- {vunit_python_bridge-0.3.0.dist-info → vunit_python_bridge-0.4.1.dist-info}/RECORD +18 -18
- {vunit_python_bridge-0.3.0.dist-info → vunit_python_bridge-0.4.1.dist-info}/WHEEL +0 -0
- {vunit_python_bridge-0.3.0.dist-info → vunit_python_bridge-0.4.1.dist-info}/licenses/LICENSE +0 -0
- {vunit_python_bridge-0.3.0.dist-info → vunit_python_bridge-0.4.1.dist-info}/top_level.txt +0 -0
vunit_python_bridge/__init__.py
CHANGED
|
@@ -14,7 +14,8 @@ language interface implementing them for the selected simulator:
|
|
|
14
14
|
|
|
15
15
|
the VUnit Python bridge, a small C library (native/*.c) embedding CPython in the simulator
|
|
16
16
|
process, called through VHPIDIRECT (NVC, GHDL), the FLI (Questa/ModelSim) or VHPI
|
|
17
|
-
(Riviera-PRO).
|
|
17
|
+
(Riviera-PRO). A simulator provided by a VUnit package names its interface itself, see
|
|
18
|
+
bridge.bridge_simulators.
|
|
18
19
|
|
|
19
20
|
Modules:
|
|
20
21
|
|
|
@@ -40,20 +41,6 @@ from .file_base import set_relative_file_base as set_relative_file_base
|
|
|
40
41
|
|
|
41
42
|
VHDL_PATH = Path(__file__).parent.resolve() / "hdl" / "src"
|
|
42
43
|
|
|
43
|
-
# The foreign language interface implementing the package for a simulator. VUnit says which
|
|
44
|
-
# simulator was selected, the package says how it is served: NVC and GHDL both go through
|
|
45
|
-
# VHPIDIRECT but bind the library differently, Questa/ModelSim goes through the FLI and
|
|
46
|
-
# Riviera-PRO through VHPI.
|
|
47
|
-
FOREIGN_LANGUAGE_INTERFACES = {
|
|
48
|
-
"nvc": "VHPIDIRECT_NVC",
|
|
49
|
-
"ghdl": "VHPIDIRECT_GHDL",
|
|
50
|
-
"modelsim": "FLI",
|
|
51
|
-
"rivierapro": "VHPI",
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
# The simulators of the table, named as a user knows them
|
|
55
|
-
SUPPORTED_SIMULATORS = "NVC, GHDL, Questa/ModelSim or Riviera-PRO"
|
|
56
|
-
|
|
57
44
|
|
|
58
45
|
def setup(context):
|
|
59
46
|
"""
|
|
@@ -63,21 +50,18 @@ def setup(context):
|
|
|
63
50
|
foreign language interface of the selected simulator.
|
|
64
51
|
"""
|
|
65
52
|
# pylint: disable=import-outside-toplevel
|
|
66
|
-
from .bridge import setup as setup_bridge
|
|
53
|
+
from .bridge import bridge_simulators, select_simulator, setup as setup_bridge
|
|
67
54
|
from . import simulator_hooks
|
|
68
55
|
|
|
69
56
|
simulator_name = context.simulator_name
|
|
57
|
+
simulators = bridge_simulators(simulator_name)
|
|
70
58
|
if simulator_name is None:
|
|
71
59
|
raise RuntimeError(
|
|
72
|
-
f"The vunit-python-bridge package requires {
|
|
60
|
+
f"The vunit-python-bridge package requires {', '.join(simulators)} but no simulator was found"
|
|
73
61
|
)
|
|
74
62
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
raise RuntimeError(
|
|
78
|
-
f"The vunit-python-bridge package requires {SUPPORTED_SIMULATORS}, "
|
|
79
|
-
f"it has no foreign language interface for {simulator_name}"
|
|
80
|
-
)
|
|
63
|
+
# Before anything is built, so a simulator the bridge cannot serve fails first
|
|
64
|
+
interface = select_simulator(simulators, simulator_name).interface
|
|
81
65
|
|
|
82
66
|
add_file_base_package(context)
|
|
83
67
|
|
|
@@ -87,8 +71,9 @@ def setup(context):
|
|
|
87
71
|
simulator_name,
|
|
88
72
|
context.simulator_prefix,
|
|
89
73
|
context.simulator_backend,
|
|
74
|
+
interface,
|
|
90
75
|
)
|
|
91
76
|
|
|
92
77
|
context.add_source_files(context.library.name, list(bridge.vhdl_files))
|
|
93
78
|
|
|
94
|
-
simulator_hooks.register(context, bridge)
|
|
79
|
+
simulator_hooks.register(context, bridge, simulators)
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
vunit_python_bridge/bridge.py
CHANGED
|
@@ -13,7 +13,9 @@ import os
|
|
|
13
13
|
import re
|
|
14
14
|
import sys
|
|
15
15
|
from pathlib import Path
|
|
16
|
-
from typing import List, NamedTuple, Optional
|
|
16
|
+
from typing import Callable, Dict, List, NamedTuple, Optional
|
|
17
|
+
|
|
18
|
+
from vunit.sim_if.factory import SIMULATOR_FACTORY
|
|
17
19
|
|
|
18
20
|
from .native_library import (
|
|
19
21
|
LIBRARY_DIR_VARIABLE,
|
|
@@ -31,10 +33,20 @@ VHDL_SOURCE_PATH = PACKAGE_PATH / "hdl" / "src"
|
|
|
31
33
|
BRIDGE_PACKAGE_TEMPLATE = VHDL_SOURCE_PATH / "python_bridge_pkg.vhd.in"
|
|
32
34
|
CONFIG_FILE_NAME = "vunit_python_bridge.cfg"
|
|
33
35
|
|
|
34
|
-
# The
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
# The foreign language interface of each built-in VUnit simulator. VHPIDIRECT names the library
|
|
37
|
+
# by file name and the simulator hooks make the simulator load it, which GHDL does in its own way.
|
|
38
|
+
BUILTIN_SIMULATORS = {
|
|
39
|
+
"nvc": "VHPIDIRECT",
|
|
40
|
+
"ghdl": "VHPIDIRECT_GHDL",
|
|
41
|
+
"modelsim": "FLI",
|
|
42
|
+
"rivierapro": "VHPI",
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
# What the interface class of a simulator from a VUnit package defines instead, see
|
|
46
|
+
# docs/simulator_packages.rst
|
|
47
|
+
INTERFACE_ATTRIBUTE = "foreign_language_interface"
|
|
48
|
+
HOOKS_ATTRIBUTE = "native_library_hooks"
|
|
49
|
+
PACKAGE_INTERFACES = ("VHPIDIRECT", "FLI", "VHPI")
|
|
38
50
|
|
|
39
51
|
# GHDL backends that link the design ahead of time. For these the library
|
|
40
52
|
# token in the VHPIDIRECT attribute is passed to the linker instead of being
|
|
@@ -47,6 +59,16 @@ FOREIGN_PATTERN = re.compile(r"\{foreign:(\w+)\}")
|
|
|
47
59
|
SUBPROGRAM_PATTERN = re.compile(r"^ (impure function|procedure) (\w+)(\([^)]*\))?( return \w+)?;", re.MULTILINE)
|
|
48
60
|
|
|
49
61
|
|
|
62
|
+
class BridgeSimulator(NamedTuple):
|
|
63
|
+
"""
|
|
64
|
+
How the bridge serves a simulator: its foreign language interface and, for a simulator from a
|
|
65
|
+
VUnit package, the native_library_hooks that make it load the library (None for a built-in one).
|
|
66
|
+
"""
|
|
67
|
+
|
|
68
|
+
interface: str
|
|
69
|
+
native_library_hooks: Optional[Callable]
|
|
70
|
+
|
|
71
|
+
|
|
50
72
|
class PythonBridge(NamedTuple):
|
|
51
73
|
"""
|
|
52
74
|
A prepared bridge: the native library and the generated VHDL.
|
|
@@ -63,6 +85,7 @@ def setup(
|
|
|
63
85
|
simulator_name: Optional[str] = None,
|
|
64
86
|
simulator_prefix: Optional[str] = None,
|
|
65
87
|
simulator_backend: Optional[str] = None,
|
|
88
|
+
interface: Optional[str] = None,
|
|
66
89
|
) -> PythonBridge:
|
|
67
90
|
"""
|
|
68
91
|
Prepare the Python bridge for a project. Called by the package setup function.
|
|
@@ -73,19 +96,18 @@ def setup(
|
|
|
73
96
|
:param simulator_prefix: The path its executables were found in.
|
|
74
97
|
:param simulator_backend: How the installation found there was built, which for GHDL
|
|
75
98
|
is the code generator deciding how the library is bound.
|
|
99
|
+
:param interface: The foreign language interface, see bridge_simulators. None takes it from
|
|
100
|
+
there by simulator name.
|
|
76
101
|
:returns: The bridge. Its vhdl_files are to be added to the package library.
|
|
77
102
|
"""
|
|
78
|
-
if
|
|
79
|
-
|
|
80
|
-
f"The vunit-python-bridge package requires NVC, GHDL, Questa/ModelSim or Riviera-PRO, "
|
|
81
|
-
f"it is not supported for {simulator_name}"
|
|
82
|
-
)
|
|
103
|
+
if interface is None and simulator_name is not None:
|
|
104
|
+
interface = select_simulator(bridge_simulators(simulator_name), simulator_name).interface
|
|
83
105
|
|
|
84
106
|
check_python_build()
|
|
85
107
|
check_windows_64bit_simulator(simulator_name, simulator_prefix)
|
|
86
108
|
|
|
87
|
-
is_fli =
|
|
88
|
-
is_vhpi =
|
|
109
|
+
is_fli = interface == "FLI"
|
|
110
|
+
is_vhpi = interface == "VHPI"
|
|
89
111
|
if (is_fli or is_vhpi) and simulator_prefix is None:
|
|
90
112
|
raise PythonBridgeError(
|
|
91
113
|
f"The {'FLI' if is_fli else 'VHPI'} variant of the bridge is built against the {simulator_name} "
|
|
@@ -96,6 +118,7 @@ def setup(
|
|
|
96
118
|
front_end = "fli" if is_fli else "vhpi" if is_vhpi else None
|
|
97
119
|
library_file = _prepare_library(Path(simulator_prefix) if front_end else None, front_end)
|
|
98
120
|
run_script_dir = str(Path.cwd() if run_script_path is None else Path(run_script_path).resolve().parent)
|
|
121
|
+
_make_pythonpath_absolute()
|
|
99
122
|
# The configuration belongs to the project, the library may be shared. The simulator
|
|
100
123
|
# hooks tell the library where the configuration is.
|
|
101
124
|
config_file = root / CONFIG_FILE_NAME
|
|
@@ -105,13 +128,13 @@ def setup(
|
|
|
105
128
|
# and the library, by absolute path since Questa accepts it, for the FLI; the library by
|
|
106
129
|
# absolute path without its suffix, which Riviera-PRO adds, and the entry point registered
|
|
107
130
|
# by native/vhpi.c for VHPI; the VHPIDIRECT library token and the entry point itself for
|
|
108
|
-
#
|
|
131
|
+
# VHPIDIRECT.
|
|
109
132
|
if is_fli:
|
|
110
133
|
foreign = f"fli_{{entry_point}} {library_file!s}"
|
|
111
134
|
elif is_vhpi:
|
|
112
135
|
foreign = f"VHPI {_vhpi_library_token(library_file)} {{entry_point}}"
|
|
113
136
|
else:
|
|
114
|
-
foreign = f"VHPIDIRECT {_vhpidirect_token(
|
|
137
|
+
foreign = f"VHPIDIRECT {_vhpidirect_token(interface, simulator_backend, library_file)} {{entry_point}}"
|
|
115
138
|
bridge_package = root / "vhdl" / "python_bridge_pkg.vhd"
|
|
116
139
|
_write_if_changed(bridge_package, _render_bridge_package(foreign))
|
|
117
140
|
|
|
@@ -125,6 +148,79 @@ def setup(
|
|
|
125
148
|
)
|
|
126
149
|
|
|
127
150
|
|
|
151
|
+
def bridge_simulators(selected: Optional[str] = None) -> Dict[str, BridgeSimulator]:
|
|
152
|
+
"""
|
|
153
|
+
The simulators the bridge serves, by name: the built-in ones and every simulator VUnit knows,
|
|
154
|
+
from a simulator package, whose interface class defines foreign_language_interface and
|
|
155
|
+
native_library_hooks. The simulator packages are added to VUnit before the bridge. Only the
|
|
156
|
+
attributes of the selected simulator are checked, a class defining just one of them is
|
|
157
|
+
otherwise not a simulator the bridge serves.
|
|
158
|
+
"""
|
|
159
|
+
simulators = {name: BridgeSimulator(interface, None) for name, interface in BUILTIN_SIMULATORS.items()}
|
|
160
|
+
for simulator_class in SIMULATOR_FACTORY.supported_simulators():
|
|
161
|
+
interface = getattr(simulator_class, INTERFACE_ATTRIBUTE, None)
|
|
162
|
+
hooks = getattr(simulator_class, HOOKS_ATTRIBUTE, None)
|
|
163
|
+
if interface is None and hooks is None:
|
|
164
|
+
continue
|
|
165
|
+
if simulator_class.name == selected:
|
|
166
|
+
_check_attributes(simulator_class.name, interface, hooks)
|
|
167
|
+
elif interface is None or hooks is None:
|
|
168
|
+
continue
|
|
169
|
+
simulators[simulator_class.name] = BridgeSimulator(interface, hooks)
|
|
170
|
+
return simulators
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def select_simulator(simulators: Dict[str, BridgeSimulator], name: str) -> BridgeSimulator:
|
|
174
|
+
"""
|
|
175
|
+
The simulator of the given name from bridge_simulators.
|
|
176
|
+
"""
|
|
177
|
+
if name not in simulators:
|
|
178
|
+
raise PythonBridgeError(
|
|
179
|
+
f"The vunit-python-bridge package requires {', '.join(simulators)}, or a simulator whose interface "
|
|
180
|
+
f"class says how it loads a native library, which {name} does not. Its simulator interface class must "
|
|
181
|
+
f"define {INTERFACE_ATTRIBUTE} ({', '.join(map(repr, PACKAGE_INTERFACES))}) and "
|
|
182
|
+
f"{HOOKS_ATTRIBUTE}(library_file), see 'Simulator packages' in the vunit-python-bridge documentation."
|
|
183
|
+
)
|
|
184
|
+
return simulators[name]
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def _check_attributes(name: str, interface, hooks) -> None:
|
|
188
|
+
"""
|
|
189
|
+
Check the attributes a simulator interface class defines to be served by the bridge.
|
|
190
|
+
"""
|
|
191
|
+
for defined, missing, value in (
|
|
192
|
+
(INTERFACE_ATTRIBUTE, HOOKS_ATTRIBUTE, hooks),
|
|
193
|
+
(HOOKS_ATTRIBUTE, INTERFACE_ATTRIBUTE, interface),
|
|
194
|
+
):
|
|
195
|
+
if value is None:
|
|
196
|
+
raise PythonBridgeError(
|
|
197
|
+
f"The {name} simulator interface defines {defined} but not {missing}, "
|
|
198
|
+
"the vunit-python-bridge package needs both. Define it in the simulator interface class."
|
|
199
|
+
)
|
|
200
|
+
if interface not in PACKAGE_INTERFACES:
|
|
201
|
+
raise PythonBridgeError(
|
|
202
|
+
f"{INTERFACE_ATTRIBUTE} of the {name} simulator interface is {interface!r}, "
|
|
203
|
+
f"the vunit-python-bridge package knows {', '.join(map(repr, PACKAGE_INTERFACES))}"
|
|
204
|
+
)
|
|
205
|
+
if not callable(hooks):
|
|
206
|
+
raise PythonBridgeError(
|
|
207
|
+
f"{HOOKS_ATTRIBUTE} of the {name} simulator interface is not callable, it must be a static or "
|
|
208
|
+
"class method taking the library file"
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def _make_pythonpath_absolute() -> None:
|
|
213
|
+
"""
|
|
214
|
+
Resolve relative PYTHONPATH entries against the current directory, as python does. VUnit
|
|
215
|
+
starts vsim (Questa/ModelSim, Riviera-PRO) in the test output directory, where they would
|
|
216
|
+
resolve differently.
|
|
217
|
+
"""
|
|
218
|
+
# Not covered: a relative PYTHONPATH set after add_package, which needs the cwd in the config
|
|
219
|
+
value = os.environ.get("PYTHONPATH")
|
|
220
|
+
if value:
|
|
221
|
+
os.environ["PYTHONPATH"] = os.pathsep.join(os.path.abspath(entry) for entry in value.split(os.pathsep))
|
|
222
|
+
|
|
223
|
+
|
|
128
224
|
def _prepare_library(simulator_prefix: Optional[Path], front_end: Optional[str]) -> Path:
|
|
129
225
|
"""
|
|
130
226
|
Prepare the library in the library directory, failing with a pointer to the variable
|
|
@@ -141,12 +237,12 @@ def _prepare_library(simulator_prefix: Optional[Path], front_end: Optional[str])
|
|
|
141
237
|
) from exc
|
|
142
238
|
|
|
143
239
|
|
|
144
|
-
def _vhpidirect_token(
|
|
240
|
+
def _vhpidirect_token(interface: Optional[str], simulator_backend: Optional[str], library_file: Path) -> str:
|
|
145
241
|
"""
|
|
146
242
|
The library token of the VHPIDIRECT attributes: the file name the simulator dlopen()s,
|
|
147
243
|
or the linker flag of the GHDL backends that link the design ahead of time.
|
|
148
244
|
"""
|
|
149
|
-
if
|
|
245
|
+
if interface == "VHPIDIRECT_GHDL" and simulator_backend in GHDL_LINKING_BACKENDS:
|
|
150
246
|
return "-lvunit_python_bridge"
|
|
151
247
|
return library_file.name
|
|
152
248
|
|
|
@@ -16,76 +16,81 @@
|
|
|
16
16
|
--
|
|
17
17
|
-- Only scalars and constrained arrays cross the foreign interface since their
|
|
18
18
|
-- ABI is identical for NVC and GHDL on all platforms and is translated to the
|
|
19
|
-
-- FLI parameter passing conventions by native/fli.c.
|
|
19
|
+
-- FLI parameter passing conventions by native/fli.c. The integers are c_int32_t,
|
|
20
|
+
-- which is the int32_t of the native library whatever the width of integer:
|
|
21
|
+
-- integer can be 64 bits with VHDL-2019, and its results then cross as two
|
|
22
|
+
-- halves, see python_ffi_pkg.
|
|
20
23
|
|
|
21
24
|
package python_bridge_pkg is
|
|
22
25
|
constant chunk_length : positive := 1024;
|
|
23
26
|
subtype string_chunk_t is string(1 to chunk_length);
|
|
24
|
-
|
|
27
|
+
type c_int32_t is range -2147483647 - 1 to 2147483647;
|
|
28
|
+
type c_int32_vector_t is array (natural range <>) of c_int32_t;
|
|
29
|
+
subtype integer_chunk_t is c_int32_vector_t(0 to chunk_length - 1);
|
|
25
30
|
subtype real_chunk_t is real_vector(0 to chunk_length - 1);
|
|
26
31
|
|
|
27
32
|
-- Start the interpreter. Idempotent.
|
|
28
|
-
impure function vpy_setup return
|
|
33
|
+
impure function vpy_setup return c_int32_t;
|
|
29
34
|
attribute foreign of vpy_setup : function is "{foreign:vpy_setup}";
|
|
30
35
|
|
|
31
36
|
-- Flush Python's streams and drop the last result and the staged values.
|
|
32
37
|
-- The interpreter is not finalized: later operations keep working.
|
|
33
|
-
impure function vpy_cleanup return
|
|
38
|
+
impure function vpy_cleanup return c_int32_t;
|
|
34
39
|
attribute foreign of vpy_cleanup : function is "{foreign:vpy_cleanup}";
|
|
35
40
|
|
|
36
|
-
impure function vpy_buffer_clear return
|
|
41
|
+
impure function vpy_buffer_clear return c_int32_t;
|
|
37
42
|
attribute foreign of vpy_buffer_clear : function is "{foreign:vpy_buffer_clear}";
|
|
38
43
|
|
|
39
|
-
impure function vpy_buffer_append(chunk : string_chunk_t; length :
|
|
44
|
+
impure function vpy_buffer_append(chunk : string_chunk_t; length : c_int32_t) return c_int32_t;
|
|
40
45
|
attribute foreign of vpy_buffer_append : function is "{foreign:vpy_buffer_append}";
|
|
41
46
|
|
|
42
47
|
-- Start an operation in the session named by the buffer ("default" is __main__).
|
|
43
|
-
impure function vpy_begin return
|
|
48
|
+
impure function vpy_begin return c_int32_t;
|
|
44
49
|
attribute foreign of vpy_begin : function is "{foreign:vpy_begin}";
|
|
45
50
|
|
|
46
51
|
-- Execute the buffer as Python source (is_file = 0) or as a file name (is_file = 1).
|
|
47
|
-
impure function vpy_execute(is_file :
|
|
52
|
+
impure function vpy_execute(is_file : c_int32_t) return c_int32_t;
|
|
48
53
|
attribute foreign of vpy_execute : function is "{foreign:vpy_execute}";
|
|
49
54
|
|
|
50
55
|
-- Evaluate the buffer as a Python expression and convert the value to the
|
|
51
56
|
-- VHDL type given by kind. width is the length of a std_ulogic_vector,
|
|
52
57
|
-- signed or unsigned result, -1 when it is not known.
|
|
53
|
-
impure function vpy_eval(kind, width :
|
|
58
|
+
impure function vpy_eval(kind, width : c_int32_t) return c_int32_t;
|
|
54
59
|
attribute foreign of vpy_eval : function is "{foreign:vpy_eval}";
|
|
55
60
|
|
|
56
|
-
impure function vpy_push_array(length, width, height, depth, bit_width, is_signed :
|
|
61
|
+
impure function vpy_push_array(length, width, height, depth, bit_width, is_signed : c_int32_t) return c_int32_t;
|
|
57
62
|
attribute foreign of vpy_push_array : function is "{foreign:vpy_push_array}";
|
|
58
63
|
|
|
59
|
-
impure function vpy_array_write(chunk : integer_chunk_t; length :
|
|
64
|
+
impure function vpy_array_write(chunk : integer_chunk_t; length : c_int32_t) return c_int32_t;
|
|
60
65
|
attribute foreign of vpy_array_write : function is "{foreign:vpy_array_write}";
|
|
61
66
|
|
|
62
67
|
-- Stage the pushed array and return its id (>= 1), -1 on error. Python
|
|
63
68
|
-- source code refers to the value as __vunit__.staged(<id>).
|
|
64
|
-
impure function vpy_stage return
|
|
69
|
+
impure function vpy_stage return c_int32_t;
|
|
65
70
|
attribute foreign of vpy_stage : function is "{foreign:vpy_stage}";
|
|
66
71
|
|
|
67
|
-
impure function vpy_result_integer return
|
|
72
|
+
impure function vpy_result_integer return c_int32_t;
|
|
68
73
|
attribute foreign of vpy_result_integer : function is "{foreign:vpy_result_integer}";
|
|
69
74
|
|
|
70
75
|
impure function vpy_result_real return real;
|
|
71
76
|
attribute foreign of vpy_result_real : function is "{foreign:vpy_result_real}";
|
|
72
77
|
|
|
73
|
-
impure function vpy_result_meta(idx :
|
|
78
|
+
impure function vpy_result_meta(idx : c_int32_t) return c_int32_t;
|
|
74
79
|
attribute foreign of vpy_result_meta : function is "{foreign:vpy_result_meta}";
|
|
75
80
|
|
|
76
|
-
impure function vpy_error_length return
|
|
81
|
+
impure function vpy_error_length return c_int32_t;
|
|
77
82
|
attribute foreign of vpy_error_length : function is "{foreign:vpy_error_length}";
|
|
78
83
|
|
|
79
|
-
procedure vpy_result_read_string(chunk : out string_chunk_t; offset, length :
|
|
84
|
+
procedure vpy_result_read_string(chunk : out string_chunk_t; offset, length : c_int32_t);
|
|
80
85
|
attribute foreign of vpy_result_read_string : procedure is "{foreign:vpy_result_read_string}";
|
|
81
86
|
|
|
82
|
-
procedure vpy_result_read_integers(chunk : out integer_chunk_t; offset, length :
|
|
87
|
+
procedure vpy_result_read_integers(chunk : out integer_chunk_t; offset, length : c_int32_t);
|
|
83
88
|
attribute foreign of vpy_result_read_integers : procedure is "{foreign:vpy_result_read_integers}";
|
|
84
89
|
|
|
85
|
-
procedure vpy_result_read_reals(chunk : out real_chunk_t; offset, length :
|
|
90
|
+
procedure vpy_result_read_reals(chunk : out real_chunk_t; offset, length : c_int32_t);
|
|
86
91
|
attribute foreign of vpy_result_read_reals : procedure is "{foreign:vpy_result_read_reals}";
|
|
87
92
|
|
|
88
|
-
procedure vpy_error_read(chunk : out string_chunk_t; offset, length :
|
|
93
|
+
procedure vpy_error_read(chunk : out string_chunk_t; offset, length : c_int32_t);
|
|
89
94
|
attribute foreign of vpy_error_read : procedure is "{foreign:vpy_error_read}";
|
|
90
95
|
end package;
|
|
91
96
|
|
|
@@ -252,14 +252,14 @@ package body python_ffi_pkg is
|
|
|
252
252
|
-- Transfers through the bridge
|
|
253
253
|
-----------------------------------------------------------------------------
|
|
254
254
|
impure function p_error_text return string is
|
|
255
|
-
constant len : natural := vpy_error_length;
|
|
255
|
+
constant len : natural := integer(vpy_error_length);
|
|
256
256
|
variable result : string(1 to len);
|
|
257
257
|
variable chunk : string_chunk_t;
|
|
258
258
|
variable offset, n : natural := 0;
|
|
259
259
|
begin
|
|
260
260
|
while offset < len loop
|
|
261
261
|
n := minimum(chunk_length, len - offset);
|
|
262
|
-
vpy_error_read(chunk, offset, n);
|
|
262
|
+
vpy_error_read(chunk, c_int32_t(offset), c_int32_t(n));
|
|
263
263
|
result(offset + 1 to offset + n) := chunk(1 to n);
|
|
264
264
|
offset := offset + n;
|
|
265
265
|
end loop;
|
|
@@ -277,17 +277,51 @@ package body python_ffi_pkg is
|
|
|
277
277
|
return false;
|
|
278
278
|
end;
|
|
279
279
|
|
|
280
|
+
-- The width of integer: 32 bits, or 64 bits as with VHDL-2019. The
|
|
281
|
+
-- foreign interface is 32 bits, so a 64-bit integer result crosses it as two
|
|
282
|
+
-- halves, see _integer_halves in runtime.py.
|
|
283
|
+
constant p_integer_width : positive := 32 + 32 * boolean'pos(integer'high > 2147483647);
|
|
284
|
+
|
|
285
|
+
-- A 64-bit integer from its halves, low taken as unsigned. Only called with a
|
|
286
|
+
-- 64-bit integer since 2**32 does not fit in a 32-bit one.
|
|
287
|
+
function p_from_halves(low, high : c_int32_t) return integer is
|
|
288
|
+
variable half_range : integer := 2**16;
|
|
289
|
+
begin
|
|
290
|
+
half_range := half_range * half_range;
|
|
291
|
+
if low < 0 then
|
|
292
|
+
return integer(high) * half_range + (integer(low) + half_range);
|
|
293
|
+
end if;
|
|
294
|
+
return integer(high) * half_range + integer(low);
|
|
295
|
+
end;
|
|
296
|
+
|
|
297
|
+
-- The width vpy_eval takes: the width of integer for an integer, integer_vector
|
|
298
|
+
-- or dict_t result, else the width of the result
|
|
299
|
+
function p_result_width(kind, width : integer) return integer is
|
|
300
|
+
begin
|
|
301
|
+
if kind = p_kind_integer or kind = p_kind_integer_vector or kind = p_kind_dict then
|
|
302
|
+
return p_integer_width;
|
|
303
|
+
end if;
|
|
304
|
+
return width;
|
|
305
|
+
end;
|
|
306
|
+
|
|
307
|
+
impure function p_succeeded(
|
|
308
|
+
status : c_int32_t; operation : string; logger : logger_t := python_logger
|
|
309
|
+
) return boolean is
|
|
310
|
+
begin
|
|
311
|
+
return p_succeeded(integer(status), operation, logger);
|
|
312
|
+
end;
|
|
313
|
+
|
|
280
314
|
impure function p_send(text : string) return integer is
|
|
281
315
|
alias normalized : string(1 to text'length) is text;
|
|
282
316
|
variable chunk : string_chunk_t;
|
|
283
317
|
variable offset, n : natural := 0;
|
|
284
318
|
variable status : integer;
|
|
285
319
|
begin
|
|
286
|
-
status := vpy_buffer_clear;
|
|
320
|
+
status := integer(vpy_buffer_clear);
|
|
287
321
|
while offset < text'length and status = 0 loop
|
|
288
322
|
n := minimum(chunk_length, text'length - offset);
|
|
289
323
|
chunk(1 to n) := normalized(offset + 1 to offset + n);
|
|
290
|
-
status := vpy_buffer_append(chunk, n);
|
|
324
|
+
status := integer(vpy_buffer_append(chunk, c_int32_t(n)));
|
|
291
325
|
offset := offset + n;
|
|
292
326
|
end loop;
|
|
293
327
|
return status;
|
|
@@ -313,7 +347,7 @@ package body python_ffi_pkg is
|
|
|
313
347
|
begin
|
|
314
348
|
return p_begin(session, operation)
|
|
315
349
|
and p_succeeded(p_send(text), operation, logger)
|
|
316
|
-
and p_succeeded(vpy_execute(is_file), operation, logger);
|
|
350
|
+
and p_succeeded(vpy_execute(c_int32_t(is_file)), operation, logger);
|
|
317
351
|
end;
|
|
318
352
|
|
|
319
353
|
impure function p_exec_file(
|
|
@@ -334,15 +368,24 @@ package body python_ffi_pkg is
|
|
|
334
368
|
begin
|
|
335
369
|
return p_begin(session, operation)
|
|
336
370
|
and p_succeeded(p_send(expr), operation, logger)
|
|
337
|
-
and p_succeeded(vpy_eval(kind, width), operation, logger);
|
|
371
|
+
and p_succeeded(vpy_eval(c_int32_t(kind), c_int32_t(p_result_width(kind, width))), operation, logger);
|
|
338
372
|
end;
|
|
339
373
|
|
|
340
374
|
-----------------------------------------------------------------------------
|
|
341
375
|
-- Results
|
|
342
376
|
-----------------------------------------------------------------------------
|
|
377
|
+
-- With a 64-bit integer, the high half is the first metadata value
|
|
343
378
|
impure function p_result_integer return integer is
|
|
344
379
|
begin
|
|
345
|
-
|
|
380
|
+
if p_integer_width = 64 then
|
|
381
|
+
return p_from_halves(vpy_result_integer, vpy_result_meta(0));
|
|
382
|
+
end if;
|
|
383
|
+
return integer(vpy_result_integer);
|
|
384
|
+
end;
|
|
385
|
+
|
|
386
|
+
impure function p_result_meta(idx : natural) return integer is
|
|
387
|
+
begin
|
|
388
|
+
return integer(vpy_result_meta(c_int32_t(idx)));
|
|
346
389
|
end;
|
|
347
390
|
|
|
348
391
|
impure function p_result_real return real is
|
|
@@ -351,44 +394,52 @@ package body python_ffi_pkg is
|
|
|
351
394
|
end;
|
|
352
395
|
|
|
353
396
|
impure function p_result_string return string is
|
|
354
|
-
constant len : natural :=
|
|
397
|
+
constant len : natural := p_result_meta(0);
|
|
355
398
|
variable result : string(1 to len);
|
|
356
399
|
variable chunk : string_chunk_t;
|
|
357
400
|
variable offset, n : natural := 0;
|
|
358
401
|
begin
|
|
359
402
|
while offset < len loop
|
|
360
403
|
n := minimum(chunk_length, len - offset);
|
|
361
|
-
vpy_result_read_string(chunk, offset, n);
|
|
404
|
+
vpy_result_read_string(chunk, c_int32_t(offset), c_int32_t(n));
|
|
362
405
|
result(offset + 1 to offset + n) := chunk(1 to n);
|
|
363
406
|
offset := offset + n;
|
|
364
407
|
end loop;
|
|
365
408
|
return result;
|
|
366
409
|
end;
|
|
367
410
|
|
|
411
|
+
-- With a 64-bit integer, an element is its low and high halves
|
|
368
412
|
impure function p_result_integer_vector return integer_vector is
|
|
369
|
-
constant len : natural :=
|
|
413
|
+
constant len : natural := p_result_meta(0);
|
|
414
|
+
constant halves : positive := p_integer_width / 32;
|
|
370
415
|
variable result : integer_vector(0 to len - 1);
|
|
371
416
|
variable chunk : integer_chunk_t;
|
|
372
417
|
variable offset, n : natural := 0;
|
|
373
418
|
begin
|
|
374
419
|
while offset < len loop
|
|
375
|
-
n := minimum(chunk_length, len - offset);
|
|
376
|
-
vpy_result_read_integers(chunk, offset, n);
|
|
377
|
-
|
|
420
|
+
n := minimum(chunk_length / halves, len - offset);
|
|
421
|
+
vpy_result_read_integers(chunk, c_int32_t(offset * halves), c_int32_t(n * halves));
|
|
422
|
+
for idx in 0 to n - 1 loop
|
|
423
|
+
if halves = 2 then
|
|
424
|
+
result(offset + idx) := p_from_halves(chunk(2 * idx), chunk(2 * idx + 1));
|
|
425
|
+
else
|
|
426
|
+
result(offset + idx) := integer(chunk(idx));
|
|
427
|
+
end if;
|
|
428
|
+
end loop;
|
|
378
429
|
offset := offset + n;
|
|
379
430
|
end loop;
|
|
380
431
|
return result;
|
|
381
432
|
end;
|
|
382
433
|
|
|
383
434
|
impure function p_result_real_vector return real_vector is
|
|
384
|
-
constant len : natural :=
|
|
435
|
+
constant len : natural := p_result_meta(0);
|
|
385
436
|
variable result : real_vector(0 to len - 1);
|
|
386
437
|
variable chunk : real_chunk_t;
|
|
387
438
|
variable offset, n : natural := 0;
|
|
388
439
|
begin
|
|
389
440
|
while offset < len loop
|
|
390
441
|
n := minimum(chunk_length, len - offset);
|
|
391
|
-
vpy_result_read_reals(chunk, offset, n);
|
|
442
|
+
vpy_result_read_reals(chunk, c_int32_t(offset), c_int32_t(n));
|
|
392
443
|
result(offset to offset + n - 1) := chunk(0 to n - 1);
|
|
393
444
|
offset := offset + n;
|
|
394
445
|
end loop;
|
|
@@ -396,22 +447,22 @@ package body python_ffi_pkg is
|
|
|
396
447
|
end;
|
|
397
448
|
|
|
398
449
|
impure function p_result_integer_array return integer_array_t is
|
|
399
|
-
constant len : natural :=
|
|
450
|
+
constant len : natural := p_result_meta(0);
|
|
400
451
|
variable result : integer_array_t := new_3d(
|
|
401
|
-
width =>
|
|
402
|
-
height =>
|
|
403
|
-
depth =>
|
|
404
|
-
bit_width =>
|
|
405
|
-
is_signed =>
|
|
452
|
+
width => p_result_meta(1),
|
|
453
|
+
height => p_result_meta(2),
|
|
454
|
+
depth => p_result_meta(3),
|
|
455
|
+
bit_width => p_result_meta(4),
|
|
456
|
+
is_signed => p_result_meta(5) /= 0
|
|
406
457
|
);
|
|
407
458
|
variable chunk : integer_chunk_t;
|
|
408
459
|
variable offset, n : natural := 0;
|
|
409
460
|
begin
|
|
410
461
|
while offset < len loop
|
|
411
462
|
n := minimum(chunk_length, len - offset);
|
|
412
|
-
vpy_result_read_integers(chunk, offset, n);
|
|
463
|
+
vpy_result_read_integers(chunk, c_int32_t(offset), c_int32_t(n));
|
|
413
464
|
for idx in 0 to n - 1 loop
|
|
414
|
-
set(result, offset + idx, chunk(idx));
|
|
465
|
+
set(result, offset + idx, integer(chunk(idx)));
|
|
415
466
|
end loop;
|
|
416
467
|
offset := offset + n;
|
|
417
468
|
end loop;
|
|
@@ -435,8 +486,27 @@ package body python_ffi_pkg is
|
|
|
435
486
|
);
|
|
436
487
|
return -1;
|
|
437
488
|
end if;
|
|
489
|
+
-- The elements cross as 32 bits, which a 64-bit integer, and so an
|
|
490
|
+
-- integer_array_t with a bit_width of 32, can exceed
|
|
491
|
+
if p_integer_width = 64 then
|
|
492
|
+
for idx in 0 to len - 1 loop
|
|
493
|
+
if get(arr, idx) < integer(c_int32_t'low) or get(arr, idx) > integer(c_int32_t'high) then
|
|
494
|
+
failure(
|
|
495
|
+
python_logger,
|
|
496
|
+
operation & " failed:" & LF &
|
|
497
|
+
"Element " & integer'image(idx) & " (" & integer'image(get(arr, idx)) & ") of the integer_array_t " &
|
|
498
|
+
"does not fit in 32 bits (" & integer'image(integer(c_int32_t'low)) & " to " &
|
|
499
|
+
integer'image(integer(c_int32_t'high)) & ")."
|
|
500
|
+
);
|
|
501
|
+
return -1;
|
|
502
|
+
end if;
|
|
503
|
+
end loop;
|
|
504
|
+
end if;
|
|
438
505
|
if not p_succeeded(
|
|
439
|
-
vpy_push_array(
|
|
506
|
+
vpy_push_array(
|
|
507
|
+
c_int32_t(len), c_int32_t(width(arr)), c_int32_t(height(arr)), c_int32_t(depth(arr)),
|
|
508
|
+
c_int32_t(bit_width(arr)), c_int32_t(boolean'pos(is_signed(arr)))
|
|
509
|
+
),
|
|
440
510
|
operation
|
|
441
511
|
) then
|
|
442
512
|
return -1;
|
|
@@ -445,15 +515,15 @@ package body python_ffi_pkg is
|
|
|
445
515
|
while offset < len loop
|
|
446
516
|
n := minimum(chunk_length, len - offset);
|
|
447
517
|
for idx in 0 to n - 1 loop
|
|
448
|
-
chunk(idx) := get(arr, offset + idx);
|
|
518
|
+
chunk(idx) := c_int32_t(get(arr, offset + idx));
|
|
449
519
|
end loop;
|
|
450
|
-
if not p_succeeded(vpy_array_write(chunk, n), operation) then
|
|
520
|
+
if not p_succeeded(vpy_array_write(chunk, c_int32_t(n)), operation) then
|
|
451
521
|
return -1;
|
|
452
522
|
end if;
|
|
453
523
|
offset := offset + n;
|
|
454
524
|
end loop;
|
|
455
525
|
|
|
456
|
-
staged_id := vpy_stage;
|
|
526
|
+
staged_id := integer(vpy_stage);
|
|
457
527
|
if staged_id < 1 then
|
|
458
528
|
failure(python_logger, operation & " failed:" & LF & p_error_text);
|
|
459
529
|
return -1;
|
|
@@ -485,7 +555,7 @@ package body python_ffi_pkg is
|
|
|
485
555
|
impure function eval_integer(expr : string; session : python_session_t := default_session) return integer is
|
|
486
556
|
begin
|
|
487
557
|
if p_eval(expr, p_kind_integer, -1, p_eval_operation(expr, session), session) then
|
|
488
|
-
return
|
|
558
|
+
return p_result_integer;
|
|
489
559
|
end if;
|
|
490
560
|
return integer'low;
|
|
491
561
|
end;
|
|
@@ -162,6 +162,8 @@ def _check_windows_python() -> None:
|
|
|
162
162
|
# Machine field values of the PE (Windows executable) header
|
|
163
163
|
_PE_MACHINE_X64 = 0x8664
|
|
164
164
|
_PE_MACHINE_NAMES = {_PE_MACHINE_X64: "64-bit x86 (x64)", 0x014C: "32-bit x86", 0xAA64: "ARM64"}
|
|
165
|
+
# The environment variables VUnit reads for the executable of NVC and GHDL
|
|
166
|
+
EXECUTABLE_VARIABLES = {"nvc": "NVC", "ghdl": "GHDL"}
|
|
165
167
|
|
|
166
168
|
|
|
167
169
|
def pe_machine(path: Path) -> Optional[int]:
|
|
@@ -192,8 +194,14 @@ def check_windows_64bit_simulator(simulator_name: str, simulator_prefix: Optiona
|
|
|
192
194
|
if simulator_prefix is None:
|
|
193
195
|
return
|
|
194
196
|
# NVC and GHDL name their executable, which VUnit lets the NVC and GHDL environment variables
|
|
195
|
-
# override, the Questa/ModelSim and Aldec interfaces use vsim
|
|
196
|
-
|
|
197
|
+
# override, the Questa/ModelSim and Aldec interfaces use vsim. The executable of a simulator
|
|
198
|
+
# from a VUnit package is not known here.
|
|
199
|
+
if simulator_name in EXECUTABLE_VARIABLES:
|
|
200
|
+
name = os.environ.get(EXECUTABLE_VARIABLES[simulator_name], simulator_name)
|
|
201
|
+
elif simulator_name in ("modelsim", "rivierapro"):
|
|
202
|
+
name = "vsim"
|
|
203
|
+
else:
|
|
204
|
+
return
|
|
197
205
|
executable = Path(simulator_prefix) / (name if name.lower().endswith(".exe") else f"{name}.exe")
|
|
198
206
|
machine = pe_machine(executable)
|
|
199
207
|
if machine is not None and machine != _PE_MACHINE_X64:
|
vunit_python_bridge/runtime.py
CHANGED
|
@@ -59,6 +59,11 @@ VHDL_TYPE_NAMES = {
|
|
|
59
59
|
STD_ULOGIC_VALUES = frozenset("UX01ZWLH-")
|
|
60
60
|
INTEGER_LOW = -(2**31)
|
|
61
61
|
INTEGER_HIGH = 2**31 - 1
|
|
62
|
+
# The width of VHDL integer: 32 bits, or 64 bits as with VHDL-2019. VHDL
|
|
63
|
+
# gives it as the width of an integer, integer_vector or dict_t result, -1 meaning
|
|
64
|
+
# 32. A 64-bit integer crosses the foreign interface as two int32 halves, see
|
|
65
|
+
# _integer_halves.
|
|
66
|
+
INTEGER_WIDTHS = (32, 64)
|
|
62
67
|
TEXT_ENCODING = "utf-8"
|
|
63
68
|
TEXT_ERRORS = "surrogateescape"
|
|
64
69
|
|
|
@@ -583,24 +588,68 @@ class Runtime: # pylint: disable=too-many-instance-attributes
|
|
|
583
588
|
if kind == KIND_INTEGER_ARRAY:
|
|
584
589
|
return self._array_result(value, array_meta)
|
|
585
590
|
if kind == KIND_DICT:
|
|
586
|
-
return self._dict_result(value, array_meta)
|
|
591
|
+
return self._dict_result(value, array_meta, width)
|
|
587
592
|
if kind not in VHDL_TYPE_NAMES:
|
|
588
593
|
raise RuntimeError(f"Internal error: unknown result kind {kind}")
|
|
589
594
|
converter = "bits" if kind in (KIND_SIGNED, KIND_UNSIGNED) else VHDL_TYPE_NAMES[kind]
|
|
590
595
|
return getattr(self, f"_{converter}_result")(kind, value, width)
|
|
591
596
|
|
|
592
597
|
@staticmethod
|
|
593
|
-
def
|
|
598
|
+
def _integer_width(width):
|
|
594
599
|
"""
|
|
595
|
-
|
|
600
|
+
The width of VHDL integer given by VHDL, see INTEGER_WIDTHS.
|
|
601
|
+
"""
|
|
602
|
+
if width == -1:
|
|
603
|
+
return 32
|
|
604
|
+
if width not in INTEGER_WIDTHS:
|
|
605
|
+
raise RuntimeError(f"Internal error: unsupported width of VHDL integer {width}")
|
|
606
|
+
return width
|
|
607
|
+
|
|
608
|
+
@staticmethod
|
|
609
|
+
def _integer_range(width):
|
|
610
|
+
"""
|
|
611
|
+
The range of VHDL integer of a width.
|
|
612
|
+
"""
|
|
613
|
+
if width == 64:
|
|
614
|
+
return -(2**63), 2**63 - 1
|
|
615
|
+
return INTEGER_LOW, INTEGER_HIGH
|
|
616
|
+
|
|
617
|
+
@staticmethod
|
|
618
|
+
def _integer_halves(value):
|
|
619
|
+
"""
|
|
620
|
+
The low and high int32 halves of a 64-bit integer, low as a signed int32.
|
|
621
|
+
VHDL computes the value as high * 2**32 + low, low taken as unsigned.
|
|
622
|
+
"""
|
|
623
|
+
low = value & 0xFFFFFFFF
|
|
624
|
+
return low - (1 << 32) if low & 0x80000000 else low, value >> 32
|
|
625
|
+
|
|
626
|
+
def _integer_result(self, kind, value, width):
|
|
627
|
+
"""
|
|
628
|
+
Convert an int result. With a 64-bit integer, the high half is the first
|
|
629
|
+
metadata value.
|
|
596
630
|
"""
|
|
597
631
|
if not _is_integer(value):
|
|
598
632
|
raise _type_error(kind, value, "int")
|
|
633
|
+
width = self._integer_width(width)
|
|
634
|
+
low, high = self._integer_range(width)
|
|
599
635
|
value = int(value)
|
|
600
|
-
if not
|
|
601
|
-
raise OverflowError(f"{value} is outside the range of VHDL integer ({
|
|
636
|
+
if not low <= value <= high:
|
|
637
|
+
raise OverflowError(f"{value} is outside the range of VHDL integer ({low} to {high})")
|
|
638
|
+
if width == 64:
|
|
639
|
+
value, high_half = self._integer_halves(value)
|
|
640
|
+
return (value, 0.0, b"", (high_half,))
|
|
602
641
|
return (value, 0.0, b"", ())
|
|
603
642
|
|
|
643
|
+
def _integer_vector_data(self, integers, width):
|
|
644
|
+
"""
|
|
645
|
+
integer_vector data: native int32, or with a 64-bit integer the low and
|
|
646
|
+
high int32 halves of each element.
|
|
647
|
+
"""
|
|
648
|
+
if width == 64:
|
|
649
|
+
halves = [half for value in integers for half in self._integer_halves(value)]
|
|
650
|
+
return struct.pack(f"={len(halves)}i", *halves)
|
|
651
|
+
return struct.pack(f"={len(integers)}i", *integers)
|
|
652
|
+
|
|
604
653
|
@staticmethod
|
|
605
654
|
def _real_result(kind, value, _width):
|
|
606
655
|
"""
|
|
@@ -688,11 +737,13 @@ class Runtime: # pylint: disable=too-many-instance-attributes
|
|
|
688
737
|
raise _type_error(kind, value, "a list or tuple")
|
|
689
738
|
return value
|
|
690
739
|
|
|
691
|
-
def _integer_vector_result(self, kind, value,
|
|
740
|
+
def _integer_vector_result(self, kind, value, width):
|
|
692
741
|
"""
|
|
693
|
-
Convert a list/tuple of int to integer_vector data
|
|
742
|
+
Convert a list/tuple of int to integer_vector data, see _integer_vector_data.
|
|
694
743
|
"""
|
|
695
744
|
values = self._sequence(kind, value)
|
|
745
|
+
width = self._integer_width(width)
|
|
746
|
+
integer_low, integer_high = self._integer_range(width)
|
|
696
747
|
integers = []
|
|
697
748
|
for index, element in enumerate(values):
|
|
698
749
|
if not _is_integer(element):
|
|
@@ -701,14 +752,13 @@ class Runtime: # pylint: disable=too-many-instance-attributes
|
|
|
701
752
|
f"a {_type_name(element)} ({element!r:.100}), to a VHDL integer; expected int"
|
|
702
753
|
)
|
|
703
754
|
element = int(element)
|
|
704
|
-
if not
|
|
755
|
+
if not integer_low <= element <= integer_high:
|
|
705
756
|
raise OverflowError(
|
|
706
757
|
f"Element {index} ({element}) is outside the range of VHDL integer "
|
|
707
|
-
f"({
|
|
758
|
+
f"({integer_low} to {integer_high})"
|
|
708
759
|
)
|
|
709
760
|
integers.append(element)
|
|
710
|
-
|
|
711
|
-
return (0, 0.0, data, (len(integers),))
|
|
761
|
+
return (0, 0.0, self._integer_vector_data(integers, width), (len(integers),))
|
|
712
762
|
|
|
713
763
|
def _real_vector_result(self, kind, value, _width):
|
|
714
764
|
"""
|
|
@@ -729,15 +779,15 @@ class Runtime: # pylint: disable=too-many-instance-attributes
|
|
|
729
779
|
data = struct.pack(f"={len(reals)}d", *reals)
|
|
730
780
|
return (0, 0.0, data, (len(reals),))
|
|
731
781
|
|
|
732
|
-
def _dict_result(self, value, array_meta):
|
|
782
|
+
def _dict_result(self, value, array_meta, width):
|
|
733
783
|
"""
|
|
734
784
|
Convert a dict and keep its entries for VHDL to fetch, see dict_kinds.
|
|
735
|
-
The result is the handle of the dict.
|
|
785
|
+
The result is the handle of the dict. width is the width of VHDL integer.
|
|
736
786
|
"""
|
|
737
787
|
self._dicts = []
|
|
738
|
-
return (self._convert_dict(value, array_meta), 0.0, b"", ())
|
|
788
|
+
return (self._convert_dict(value, array_meta, self._integer_width(width)), 0.0, b"", ())
|
|
739
789
|
|
|
740
|
-
def _convert_dict(self, value, array_meta):
|
|
790
|
+
def _convert_dict(self, value, array_meta, width):
|
|
741
791
|
"""
|
|
742
792
|
Convert the entries of a dict, and of its nested dicts, and return its handle.
|
|
743
793
|
"""
|
|
@@ -747,18 +797,28 @@ class Runtime: # pylint: disable=too-many-instance-attributes
|
|
|
747
797
|
for key, item in value.items():
|
|
748
798
|
if not isinstance(key, str):
|
|
749
799
|
raise TypeError(f"Cannot convert the dict key {key!r:.100}, a {_type_name(key)}; expected str")
|
|
750
|
-
entries.append((key,) + self._dict_value(key, item, array_meta))
|
|
800
|
+
entries.append((key,) + self._dict_value(key, item, array_meta, width))
|
|
751
801
|
self._dicts.append(entries)
|
|
752
802
|
return len(self._dicts) - 1
|
|
753
803
|
|
|
754
|
-
def _dict_value(self, key, item, array_meta):
|
|
804
|
+
def _dict_value(self, key, item, array_meta, width):
|
|
755
805
|
"""
|
|
756
806
|
The result kind and the converted value of a dict entry.
|
|
757
807
|
"""
|
|
758
808
|
if _is_bool(item):
|
|
759
809
|
return KIND_BOOLEAN, (int(bool(item)), 0.0, b"", ())
|
|
760
810
|
if _is_integer(item):
|
|
761
|
-
|
|
811
|
+
value = self._dict_integer(key, item, width)
|
|
812
|
+
if not INTEGER_LOW <= value <= INTEGER_HIGH:
|
|
813
|
+
# set_integer encodes the value in 32 bits whatever the width of integer
|
|
814
|
+
raise OverflowError(
|
|
815
|
+
f"{value} of dict key {key!r:.100} is outside the range of a dict_t integer, which VUnit "
|
|
816
|
+
f"stores in 32 bits ({INTEGER_LOW} to {INTEGER_HIGH})"
|
|
817
|
+
)
|
|
818
|
+
if width == 64:
|
|
819
|
+
low, high = self._integer_halves(value)
|
|
820
|
+
return KIND_INTEGER, (low, 0.0, b"", (high,))
|
|
821
|
+
return KIND_INTEGER, (value, 0.0, b"", ())
|
|
762
822
|
if _is_float(item):
|
|
763
823
|
if not math.isfinite(item):
|
|
764
824
|
raise ValueError(f"{item} of dict key {key!r:.100} cannot be represented as VHDL real")
|
|
@@ -766,27 +826,26 @@ class Runtime: # pylint: disable=too-many-instance-attributes
|
|
|
766
826
|
if isinstance(item, str):
|
|
767
827
|
return KIND_STRING, self._string_result(KIND_STRING, item, -1)
|
|
768
828
|
if isinstance(item, dict):
|
|
769
|
-
return KIND_DICT, (self._convert_dict(item, array_meta), 0.0, b"", ())
|
|
829
|
+
return KIND_DICT, (self._convert_dict(item, array_meta, width), 0.0, b"", ())
|
|
770
830
|
if isinstance(item, numpy.ndarray):
|
|
771
831
|
try:
|
|
772
832
|
return KIND_INTEGER_ARRAY, self._array_result(item, array_meta)
|
|
773
833
|
except (TypeError, ValueError, OverflowError) as error:
|
|
774
834
|
raise type(error)(f"dict key {key!r:.100}: {error}") from None
|
|
775
835
|
if isinstance(item, list) and all(_is_integer(element) for element in item):
|
|
776
|
-
integers = [self._dict_integer(key, element) for element in item]
|
|
777
|
-
return KIND_INTEGER_VECTOR, (0, 0.0,
|
|
836
|
+
integers = [self._dict_integer(key, element, width) for element in item]
|
|
837
|
+
return KIND_INTEGER_VECTOR, (0, 0.0, self._integer_vector_data(integers, width), (len(integers),))
|
|
778
838
|
raise TypeError(
|
|
779
839
|
f"Cannot convert the value of dict key {key!r:.100}, a {_type_name(item)} ({item!r:.100}); "
|
|
780
840
|
"expected int, float, str, bool, a list of int, a NumPy array of integers or a dict"
|
|
781
841
|
)
|
|
782
842
|
|
|
783
|
-
|
|
784
|
-
|
|
843
|
+
def _dict_integer(self, key, value, width):
|
|
844
|
+
low, high = self._integer_range(width)
|
|
785
845
|
value = int(value)
|
|
786
|
-
if not
|
|
846
|
+
if not low <= value <= high:
|
|
787
847
|
raise OverflowError(
|
|
788
|
-
f"{value} of dict key {key!r:.100} is outside the range of VHDL integer "
|
|
789
|
-
f"({INTEGER_LOW} to {INTEGER_HIGH})"
|
|
848
|
+
f"{value} of dict key {key!r:.100} is outside the range of VHDL integer ({low} to {high})"
|
|
790
849
|
)
|
|
791
850
|
return value
|
|
792
851
|
|
|
@@ -21,7 +21,8 @@ import sys
|
|
|
21
21
|
from pathlib import Path
|
|
22
22
|
from typing import Dict, List, Optional
|
|
23
23
|
|
|
24
|
-
from .bridge import GHDL_LINKING_BACKENDS, PythonBridge
|
|
24
|
+
from .bridge import GHDL_LINKING_BACKENDS, HOOKS_ATTRIBUTE, BridgeSimulator, PythonBridge
|
|
25
|
+
from .native_library import PythonBridgeError
|
|
25
26
|
|
|
26
27
|
# The vsim flag keeping the runtime libraries Questa bundles out of the dynamic library
|
|
27
28
|
# search path, and the help category listing every vsim flag.
|
|
@@ -31,32 +32,60 @@ VSIM_HELP_ARGUMENTS = ("-help", "all")
|
|
|
31
32
|
# Read by native/config.c, keep in sync
|
|
32
33
|
CONFIG_FILE_VARIABLE = "VUNIT_PYTHON_BRIDGE_CONFIG"
|
|
33
34
|
|
|
35
|
+
# The keyword arguments of register_simulator_hooks of the VUnit package context
|
|
36
|
+
HOOK_KINDS = ("elab_flags", "run_flags", "process_flags", "run_env")
|
|
34
37
|
|
|
35
|
-
|
|
38
|
+
|
|
39
|
+
def register(context, bridge: PythonBridge, simulators: Dict[str, BridgeSimulator]) -> None:
|
|
36
40
|
"""
|
|
37
|
-
Register the hooks of every simulator
|
|
41
|
+
Register the hooks of every built-in simulator with the package context. The hooks the selected
|
|
42
|
+
simulator interface gives by native_library_hooks replace the built-in ones.
|
|
38
43
|
"""
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
"ghdl",
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
)
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
)
|
|
44
|
+
hooks = {
|
|
45
|
+
"nvc": dict(run_flags=_nvc_run_flags(bridge), run_env=_config_run_env(bridge)),
|
|
46
|
+
"ghdl": dict(elab_flags=_ghdl_elab_flags(bridge), run_env=_ghdl_run_env(bridge)),
|
|
47
|
+
"modelsim": dict(process_flags=_modelsim_process_flags(), run_env=_config_run_env(bridge)),
|
|
48
|
+
}
|
|
49
|
+
selected = simulators.get(context.simulator_name)
|
|
50
|
+
if selected is not None and selected.native_library_hooks is not None:
|
|
51
|
+
hooks[context.simulator_name] = _package_hooks(context.simulator_name, selected.native_library_hooks, bridge)
|
|
52
|
+
for simulator_name, kinds in hooks.items():
|
|
53
|
+
context.register_simulator_hooks(simulator_name, **kinds)
|
|
48
54
|
if context.simulator_name == "rivierapro":
|
|
49
55
|
# Its interface has no run environment hook, but starts vsim with the environment of this
|
|
50
56
|
# process, after the setup
|
|
51
57
|
os.environ[CONFIG_FILE_VARIABLE] = str(bridge.config_file)
|
|
52
58
|
|
|
53
59
|
|
|
54
|
-
def
|
|
60
|
+
def _package_hooks(simulator_name: str, native_library_hooks, bridge: PythonBridge) -> Dict:
|
|
61
|
+
"""
|
|
62
|
+
The hooks of a simulator interface loading the library, with the configuration added to its
|
|
63
|
+
run environment.
|
|
64
|
+
"""
|
|
65
|
+
hooks = native_library_hooks(bridge.library_file)
|
|
66
|
+
if not isinstance(hooks, dict):
|
|
67
|
+
raise PythonBridgeError(
|
|
68
|
+
f"{HOOKS_ATTRIBUTE} of the {simulator_name} simulator interface returned {type(hooks).__name__}, "
|
|
69
|
+
f"it must return a dict with any of the keys {', '.join(HOOK_KINDS)}"
|
|
70
|
+
)
|
|
71
|
+
unknown = sorted(str(kind) for kind in hooks if kind not in HOOK_KINDS)
|
|
72
|
+
if unknown:
|
|
73
|
+
raise PythonBridgeError(
|
|
74
|
+
f"{HOOKS_ATTRIBUTE} of the {simulator_name} simulator interface returned the unknown keys "
|
|
75
|
+
f"{', '.join(unknown)}, the keys are {', '.join(HOOK_KINDS)}"
|
|
76
|
+
)
|
|
77
|
+
return dict(hooks, run_env=_config_run_env(bridge, hooks.get("run_env")))
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _config_run_env(bridge: PythonBridge, simulator_run_env=None):
|
|
55
81
|
"""
|
|
56
|
-
Tell the bridge library where the configuration of the project is
|
|
82
|
+
Tell the bridge library where the configuration of the project is, on top of the environment
|
|
83
|
+
a simulator_run_env hook gives.
|
|
57
84
|
"""
|
|
58
85
|
|
|
59
|
-
def hook(simulator_interface, env: Dict[str, str]) -> Dict[str, str]:
|
|
86
|
+
def hook(simulator_interface, env: Dict[str, str]) -> Dict[str, str]:
|
|
87
|
+
if simulator_run_env is not None:
|
|
88
|
+
env = simulator_run_env(simulator_interface, env)
|
|
60
89
|
env = dict(env)
|
|
61
90
|
env[CONFIG_FILE_VARIABLE] = str(bridge.config_file)
|
|
62
91
|
return env
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: vunit-python-bridge
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.1
|
|
4
4
|
Summary: A VUnit package making Python callable from VHDL.
|
|
5
5
|
Author-email: Lars Asplund <lars.anders.asplund@gmail.com>
|
|
6
6
|
License-Expression: MPL-2.0
|
|
@@ -96,6 +96,10 @@ check(call_integer_vector("gain", arg(integer_vector'(1, 2))) = integer_vector'(
|
|
|
96
96
|
| Questa/ModelSim | FLI | Tested manually on Linux; the Windows build is untested |
|
|
97
97
|
| Riviera-PRO | VHPI | Tested manually on Linux and Windows |
|
|
98
98
|
|
|
99
|
+
A simulator provided by a VUnit package says how it loads the bridge
|
|
100
|
+
itself, by two attributes of its simulator interface class: see
|
|
101
|
+
[Simulator packages](https://vunit-python-bridge.readthedocs.io/en/latest/simulator_packages.html).
|
|
102
|
+
|
|
99
103
|
## Requirements
|
|
100
104
|
|
|
101
105
|
- VUnit 5.0.0.dev15 or later (`vunit_hdl` on PyPI), which pip installs with the package.
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
vunit_python_bridge/__init__.py,sha256=
|
|
2
|
-
vunit_python_bridge/bridge.py,sha256=
|
|
1
|
+
vunit_python_bridge/__init__.py,sha256=3YLbTXiMC0KSBhayw_q2rXNi3O1GMaviINSAnHi7EeU,3036
|
|
2
|
+
vunit_python_bridge/bridge.py,sha256=MrbyRVbZhuyWUQmFtMaQ1kokjzs4ciirVSMj5ESb5xI,13347
|
|
3
3
|
vunit_python_bridge/file_base.py,sha256=yWWN-z7GWPnWMLuqIyUMSRWfax_6W0i92pA0nrh2qTY,4730
|
|
4
|
-
vunit_python_bridge/native_library.py,sha256=
|
|
5
|
-
vunit_python_bridge/runtime.py,sha256=
|
|
6
|
-
vunit_python_bridge/simulator_hooks.py,sha256=
|
|
4
|
+
vunit_python_bridge/native_library.py,sha256=DUH4MYaPSHO-FtvX7_1UsVIpgLSWArPOU8Urv1rGa20,25340
|
|
5
|
+
vunit_python_bridge/runtime.py,sha256=07RCnOGHt7ZXe8V1Y0Yq-77EYew2QYLPEoIBf9gF4II,35598
|
|
6
|
+
vunit_python_bridge/simulator_hooks.py,sha256=syBITFpv5pE7ZKbHIM4vTwo8wGhF9AfhNa-Obs0xNdI,7599
|
|
7
7
|
vunit_python_bridge/vunit_pkg.toml,sha256=8AO9wJFWqIGbWU8FLWAHL8vtfeYuD3D0FllilXVXmVE,660
|
|
8
|
-
vunit_python_bridge/bin/vunit_python_bridge-cp310-win_amd64.dll,sha256=
|
|
9
|
-
vunit_python_bridge/bin/vunit_python_bridge-cp311-win_amd64.dll,sha256=
|
|
10
|
-
vunit_python_bridge/bin/vunit_python_bridge-cp312-win_amd64.dll,sha256=
|
|
11
|
-
vunit_python_bridge/bin/vunit_python_bridge-cp313-win_amd64.dll,sha256=
|
|
12
|
-
vunit_python_bridge/bin/vunit_python_bridge-cp314-win_amd64.dll,sha256=
|
|
13
|
-
vunit_python_bridge/bin/vunit_python_bridge-cp315-win_amd64.dll,sha256=
|
|
14
|
-
vunit_python_bridge/hdl/src/python_bridge_pkg.vhd.in,sha256=
|
|
8
|
+
vunit_python_bridge/bin/vunit_python_bridge-cp310-win_amd64.dll,sha256=zvdj_6TOozIp_MOMzAG35BiobSZcWKumcWfmNdM1bDA,26624
|
|
9
|
+
vunit_python_bridge/bin/vunit_python_bridge-cp311-win_amd64.dll,sha256=gMreqwiChLm81bb_vJZb7CORikEvI-RxuwjDraCcZdU,26624
|
|
10
|
+
vunit_python_bridge/bin/vunit_python_bridge-cp312-win_amd64.dll,sha256=a6aL02meAWdpBZr_rXXd5e4hVziYdehub6eiXYJgqFY,26624
|
|
11
|
+
vunit_python_bridge/bin/vunit_python_bridge-cp313-win_amd64.dll,sha256=_0iBkCjm5dFO264nzNiiAd337kYg3k8n0kHN61spTHo,26624
|
|
12
|
+
vunit_python_bridge/bin/vunit_python_bridge-cp314-win_amd64.dll,sha256=4OY1LIWqFB3wmKk-lS_r-KpDNdMo0O6RDYsTnYHkSUA,26624
|
|
13
|
+
vunit_python_bridge/bin/vunit_python_bridge-cp315-win_amd64.dll,sha256=fOw-XZpwOPSiItxJUU8bWO1OED9Sja87r8f8zMGj_qY,26624
|
|
14
|
+
vunit_python_bridge/hdl/src/python_bridge_pkg.vhd.in,sha256=cykEPH0A0asOWPHkZ-xClYzMHm9sQFG4065MmkygaGI,5194
|
|
15
15
|
vunit_python_bridge/hdl/src/python_context.vhd,sha256=IE9dVRfPiA9tDVPW82hYltu5z3ZUczJIC4S3Hk_z9z4,522
|
|
16
|
-
vunit_python_bridge/hdl/src/python_ffi_pkg_bridge.vhd,sha256=
|
|
16
|
+
vunit_python_bridge/hdl/src/python_ffi_pkg_bridge.vhd,sha256=kvJFEs5hhLf3q_3tQKKIKa3NxwkJ85owBf9XFYBbpB8,22889
|
|
17
17
|
vunit_python_bridge/hdl/src/python_object_pkg.vhd,sha256=rvKiOMf2YZbjUssbbBGbUUsXi_Wc5y_aO77lgVx95WQ,34182
|
|
18
18
|
vunit_python_bridge/hdl/src/python_pkg.vhd,sha256=GvF7LUZk7lu8a_nMI-dNJ5FkQaXxW1FcSLeRhF2nJJE,56242
|
|
19
19
|
vunit_python_bridge/hdl/tools/generate_python_pkg.py,sha256=l_YPDW22TxGORH_ydefvKJ-HUXPfBYvOdczWxVf549Q,22731
|
|
@@ -27,8 +27,8 @@ vunit_python_bridge/native/fli.c,sha256=Half7fS8BA9X3n1jjpRAsUOoNDWEqhUT4WVfn_d-
|
|
|
27
27
|
vunit_python_bridge/native/interpreter.c,sha256=Ey6S6NZdCGs32G43jnOnAoS04RhSwJZoGGFGlaG0CBw,8222
|
|
28
28
|
vunit_python_bridge/native/operations.c,sha256=8hDC4iHcYOcy9Ijh564OKEx80eoJ7GDnlbPJX4hII1g,7271
|
|
29
29
|
vunit_python_bridge/native/vhpi.c,sha256=jnoHXIavPWC8wmAZwUvM-w7sDiptXxlHWlntVgmie2s,9208
|
|
30
|
-
vunit_python_bridge-0.
|
|
31
|
-
vunit_python_bridge-0.
|
|
32
|
-
vunit_python_bridge-0.
|
|
33
|
-
vunit_python_bridge-0.
|
|
34
|
-
vunit_python_bridge-0.
|
|
30
|
+
vunit_python_bridge-0.4.1.dist-info/licenses/LICENSE,sha256=Pz2eACSxkhsGfW9_iN60pgy-enjnbGTj8df8O3ebnQQ,16726
|
|
31
|
+
vunit_python_bridge-0.4.1.dist-info/METADATA,sha256=YGNrArKETqspCKDczuEzGycYPTtsBCj6KeOjdiScM0I,5741
|
|
32
|
+
vunit_python_bridge-0.4.1.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
33
|
+
vunit_python_bridge-0.4.1.dist-info/top_level.txt,sha256=abj4noIVQMKaPr2m8kPPxH6RSYadeXKc0Ho2uilmlEg,20
|
|
34
|
+
vunit_python_bridge-0.4.1.dist-info/RECORD,,
|
|
File without changes
|
{vunit_python_bridge-0.3.0.dist-info → vunit_python_bridge-0.4.1.dist-info}/licenses/LICENSE
RENAMED
|
File without changes
|
|
File without changes
|