Skip to content

Commit eb0f15f

Browse files
committed
feat: add external models to string_ptr; add byte_vector* as an alias
1 parent 82f5d55 commit eb0f15f

12 files changed

Lines changed: 860 additions & 215 deletions

vunit/builtins.py

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,61 @@ def _add_files(self, pattern):
6868

6969
self._vunit_lib.add_source_file(file_name)
7070

71-
def _add_data_types(self):
71+
def _add_data_types(self, use_external=None, impls=None):
7272
"""
7373
Add data types packages
74+
75+
:param use_external: list of Booleans, to select whether to enable external models for string and/or
76+
integer_vector, respectively.
77+
:param impls: optional list of lists containing alternative implementations for external models.
7478
"""
7579
self._add_files(join(VHDL_PATH, "data_types", "src", "types", "*.vhd"))
7680
self._add_files(join(VHDL_PATH, "data_types", "src", "*.vhd"))
7781

82+
# Add sources corresponding to VHPIDIRECT arrays (or their placeholders)
83+
84+
from vunit.test.common import simulator_check
85+
86+
use_ext = [False, False]
87+
files = [None, None]
88+
89+
if use_external is not None:
90+
for ind, val in enumerate(use_external):
91+
use_ext[ind] = val
92+
if impls is not None:
93+
for ind, val in enumerate(impls):
94+
files[ind] = val
95+
96+
for val in use_ext:
97+
if val and simulator_check(lambda simclass: not simclass.supports_vhpi()):
98+
raise RuntimeError("the selected simulator does not support VHPI; must use non-VHPI packages...")
99+
100+
ext_path = join(VHDL_PATH, "data_types", "src", "external")
101+
102+
def default_pkg(cond, type_str):
103+
"""
104+
Return name of VHDL file with default VHPIDIRECT foreign declarations.
105+
"""
106+
nostr = 'no'
107+
if cond:
108+
nostr = ''
109+
return join(ext_path, 'external_' + type_str + '-' + nostr + 'vhpi.vhd')
110+
111+
if not files[0]:
112+
files[0] = [
113+
default_pkg(use_ext[0], 'string'),
114+
join(ext_path, "external_string-body.vhd")
115+
]
116+
if not files[1]:
117+
files[1] = [
118+
default_pkg(use_ext[1], 'integer_vector'),
119+
join(ext_path, "external_integer_vector-body.vhd")
120+
]
121+
122+
for _, flist in enumerate(files):
123+
for name in flist:
124+
self._add_files(name)
125+
78126
def _add_array_util(self):
79127
"""
80128
Add array utility
@@ -168,11 +216,15 @@ def add_verilog_builtins(self):
168216
"""
169217
self._vunit_lib.add_source_files(join(VERILOG_PATH, "vunit_pkg.sv"))
170218

171-
def add_vhdl_builtins(self):
219+
def add_vhdl_builtins(self, use_external=None, impls=None):
172220
"""
173221
Add vunit VHDL builtin libraries
222+
223+
:param use_external: list of Booleans, to select whether to enable external models for string and/or
224+
integer_vector, respectively.
225+
:param impls: optional list of lists containing alternative implementations for external models.
174226
"""
175-
self._add_data_types()
227+
self._add_data_types(use_external=use_external, impls=impls)
176228
self._add_files(join(VHDL_PATH, "*.vhd"))
177229
for path in ("core", "logging", "string_ops", "check", "dictionary", "run", "path"):
178230
self._add_files(join(VHDL_PATH, path, "src", "*.vhd"))

vunit/ui.py

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
A list of PLI file names.
119119
120120
``ghdl.flags``
121-
Extra arguments passed to ``ghdl --elab-run`` command *before* executable specific flags. Must be a list of strings.
121+
Extra arguments passed to ``ghdl --elab-run`` command *before* executable specific flags.
122122
Must be a list of strings.
123123
124124
``incisive.irun_sim_flags``
@@ -290,12 +290,17 @@ class VUnit(object): # pylint: disable=too-many-instance-attributes, too-many-p
290290
"""
291291

292292
@classmethod
293-
def from_argv(cls, argv=None, compile_builtins=True, vhdl_standard=None):
293+
def from_argv(cls, argv=None, compile_builtins=True, vhdl_standard=None, use_external=None, impls=None):
294294
"""
295295
Create VUnit instance from command line arguments.
296296
297297
:param argv: Use explicit argv instead of actual command line argument
298298
:param compile_builtins: Do not compile builtins. Used for VUnit internal testing.
299+
:param vhdl_standard: The VHDL standard used to compile files into this library,
300+
if None the VUNIT_VHDL_STANDARD environment variable is used
301+
:param use_external: list of Booleans, to select whether to enable external models for string and/or
302+
integer_vector, respectively; if None, all of them are disabled
303+
:param impls: optional list of lists containing alternative implementations for external models
299304
:returns: A :class:`.VUnit` object instance
300305
301306
:example:
@@ -307,10 +312,16 @@ def from_argv(cls, argv=None, compile_builtins=True, vhdl_standard=None):
307312
308313
"""
309314
args = VUnitCLI().parse_args(argv=argv)
310-
return cls.from_args(args, compile_builtins=compile_builtins, vhdl_standard=vhdl_standard)
315+
return cls.from_args(
316+
args,
317+
compile_builtins=compile_builtins,
318+
vhdl_standard=vhdl_standard,
319+
use_external=use_external,
320+
impls=impls
321+
)
311322

312323
@classmethod
313-
def from_args(cls, args, compile_builtins=True, vhdl_standard=None):
324+
def from_args(cls, args, compile_builtins=True, vhdl_standard=None, use_external=None, impls=None):
314325
"""
315326
Create VUnit instance from args namespace.
316327
Intended for users who adds custom command line options.
@@ -319,12 +330,23 @@ def from_args(cls, args, compile_builtins=True, vhdl_standard=None):
319330
320331
:param args: The parsed argument namespace object
321332
:param compile_builtins: Do not compile builtins. Used for VUnit internal testing.
333+
:param vhdl_standard: The VHDL standard used to compile files into this library,
334+
if None the VUNIT_VHDL_STANDARD environment variable is used
335+
:param use_external: list of Booleans, to select whether to enable external models for string and/or
336+
integer_vector, respectively; if None, all of them are disabled
337+
:param impls: optional list of lists containing alternative implementations for external models
322338
:returns: A :class:`.VUnit` object instance
323-
"""
324339
325-
return cls(args, compile_builtins=compile_builtins, vhdl_standard=vhdl_standard)
340+
"""
341+
return cls(
342+
args,
343+
compile_builtins=compile_builtins,
344+
vhdl_standard=vhdl_standard,
345+
use_external=use_external,
346+
impls=impls
347+
)
326348

327-
def __init__(self, args, compile_builtins=True, vhdl_standard=None):
349+
def __init__(self, args, compile_builtins=True, vhdl_standard=None, use_external=None, impls=None):
328350
self._args = args
329351
self._configure_logging(args.log_level)
330352
self._output_path = abspath(args.output_path)
@@ -371,8 +393,9 @@ def test_filter(name, attribute_names):
371393
self._test_bench_list = TestBenchList(database=database)
372394

373395
self._builtins = Builtins(self, self._vhdl_standard, simulator_class)
396+
self._compile_builtins = compile_builtins
374397
if compile_builtins:
375-
self.add_builtins()
398+
self.add_builtins(use_external=use_external, impls=impls)
376399

377400
def _create_database(self):
378401
"""
@@ -858,7 +881,6 @@ def _main(self, post_run):
858881
"""
859882
Base vunit main function without performing exit
860883
"""
861-
862884
if self._args.export_json is not None:
863885
return self._main_export_json(self._args.export_json)
864886

@@ -1056,11 +1078,15 @@ def _run_test(self, test_cases, report):
10561078
no_color=self._args.no_color)
10571079
runner.run(test_cases)
10581080

1059-
def add_builtins(self):
1081+
def add_builtins(self, use_external=None, impls=None):
10601082
"""
10611083
Add vunit VHDL builtin libraries
1084+
1085+
:param use_external: list of Booleans, to select whether to enable external models for string and/or
1086+
integer_vector, respectively.
1087+
:param impls: optional list of lists containing alternative implementations for external models.
10621088
"""
1063-
self._builtins.add_vhdl_builtins()
1089+
self._builtins.add_vhdl_builtins(use_external=use_external, impls=impls)
10641090

10651091
def add_com(self):
10661092
"""
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
-- This Source Code Form is subject to the terms of the Mozilla Public
2+
-- License, v. 2.0. If a copy of the MPL was not distributed with this file,
3+
-- You can obtain one at http://mozilla.org/MPL/2.0/.
4+
--
5+
-- Copyright (c) 2014-2019, Lars Asplund lars.anders.asplund@gmail.com
6+
--
7+
-- The purpose of this package is to provide a byte vector access type (pointer)
8+
-- that can itself be used in arrays and returned from functions unlike a
9+
-- real access type. This is achieved by letting the actual value be a handle
10+
-- into a singleton datastructure of string access types.
11+
--
12+
13+
use work.byte_vector_pkg.all;
14+
use work.string_ptr_pkg.all;
15+
16+
package byte_vector_ptr_pkg is
17+
18+
alias byte_vector_ptr_t is string_ptr_t;
19+
alias null_byte_vector_ptr is null_string_ptr;
20+
21+
alias new_byte_vector_ptr is new_string_ptr[natural, integer, val_t return ptr_t];
22+
alias new_byte_vector_ptr is new_string_ptr[natural, integer, natural return ptr_t];
23+
alias new_byte_vector_ptr is new_string_ptr[string, integer return ptr_t];
24+
25+
alias is_external is is_external[ptr_t return boolean];
26+
alias deallocate is deallocate[ptr_t];
27+
alias length is length[ptr_t return integer];
28+
alias set is set[ptr_t, natural, natural];
29+
alias get is get[ptr_t, natural return natural];
30+
alias reallocate is reallocate[ptr_t, natural, natural];
31+
alias resize is resize[ptr_t, natural, natural, natural];
32+
33+
-- alias write_byte is write_char;
34+
-- alias read_byte is read_char;
35+
-- alias byte_vector_access_t is string_access_t;
36+
37+
end package;

vunit/vhdl/data_types/src/data_types_context.vhd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
context data_types_context is
88
library vunit_lib;
9+
use vunit_lib.byte_vector_pkg.all;
10+
use vunit_lib.byte_vector_ptr_pkg.all;
911
use vunit_lib.integer_vector_ptr_pkg.all;
1012
use vunit_lib.integer_vector_ptr_pool_pkg.all;
1113
use vunit_lib.integer_array_pkg.all;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
-- This Source Code Form is subject to the terms of the Mozilla Public
2+
-- License, v. 2.0. If a copy of the MPL was not distributed with this file,
3+
-- You can obtain one at http://mozilla.org/MPL/2.0/.
4+
--
5+
-- Copyright (c) 2014-2019, Lars Asplund lars.anders.asplund@gmail.com
6+
7+
package body external_string_pkg is
8+
procedure
9+
write_char(
10+
id : integer;
11+
i : integer;
12+
v : character
13+
)is begin
14+
assert false report "VHPI write_char" severity failure;
15+
end;
16+
17+
impure function
18+
read_char(
19+
id : integer;
20+
i : integer
21+
) return character is begin
22+
assert false report "VHPI read_char" severity failure;
23+
end;
24+
25+
impure function
26+
get_ptr(
27+
id : integer
28+
) return extstring_access_t is begin
29+
assert false report "VHPI get_string_ptr" severity failure;
30+
end;
31+
end package body;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
-- This Source Code Form is subject to the terms of the Mozilla Public
2+
-- License, v. 2.0. If a copy of the MPL was not distributed with this file,
3+
-- You can obtain one at http://mozilla.org/MPL/2.0/.
4+
--
5+
-- Copyright (c) 2014-2019, Lars Asplund lars.anders.asplund@gmail.com
6+
7+
use work.string_pkg.all;
8+
9+
package external_string_pkg is
10+
procedure
11+
write_char(
12+
id : integer;
13+
i : integer;
14+
v : character
15+
);
16+
17+
impure function
18+
read_char(
19+
id : integer;
20+
i : integer
21+
) return character;
22+
23+
impure function
24+
get_ptr(
25+
id : integer
26+
) return extstring_access_t;
27+
end package;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
-- This Source Code Form is subject to the terms of the Mozilla Public
2+
-- License, v. 2.0. If a copy of the MPL was not distributed with this file,
3+
-- You can obtain one at http://mozilla.org/MPL/2.0/.
4+
--
5+
-- Copyright (c) 2014-2019, Lars Asplund lars.anders.asplund@gmail.com
6+
7+
use work.string_pkg.all;
8+
9+
package external_string_pkg is
10+
procedure
11+
write_char(
12+
id : integer;
13+
i : integer;
14+
v : character
15+
);
16+
attribute foreign of write_char : procedure is "VHPIDIRECT write_char";
17+
18+
impure function
19+
read_char(
20+
id : integer;
21+
i : integer
22+
) return character;
23+
attribute foreign of read_char : function is "VHPIDIRECT read_char";
24+
25+
impure function
26+
get_ptr(
27+
id : integer
28+
) return extstring_access_t;
29+
attribute foreign of get_ptr : function is "VHPIDIRECT get_string_ptr";
30+
end package;

0 commit comments

Comments
 (0)