Skip to content

Commit d3bf541

Browse files
committed
Add support package for VCs.
1 parent 4dd66f0 commit d3bf541

File tree

2 files changed

+245
-0
lines changed

2 files changed

+245
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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-2024, Lars Asplund [email protected]
6+
--
7+
-- This package contains common functionality for VCs.
8+
9+
context work.vunit_context;
10+
context work.com_context;
11+
12+
package vc_pkg is
13+
type unexpected_msg_type_policy_t is (fail, ignore);
14+
15+
type std_cfg_t is record
16+
p_id : id_t;
17+
p_actor : actor_t;
18+
p_logger : logger_t;
19+
p_checker : checker_t;
20+
p_unexpected_msg_type_policy : unexpected_msg_type_policy_t;
21+
end record;
22+
23+
constant null_std_cfg : std_cfg_t := (
24+
p_id => null_id,
25+
p_actor => null_actor,
26+
p_logger => null_logger,
27+
p_checker => null_checker,
28+
p_unexpected_msg_type_policy => ignore
29+
);
30+
31+
-- Creates a standard VC configuration with an id, an actor, a logger, a
32+
-- checker, and an unexpected message type policy.
33+
--
34+
-- If id = null_id, the id will be assigned the name provider:vc_name:n where n is 1
35+
-- for the first instance and increasing with one for every additional instance.
36+
--
37+
-- The id must not have an associated actor before the call as that may indicate
38+
-- several users of the same actor.
39+
--
40+
-- If a logger exist for the id, it will be reused. If not, a new logger is created.
41+
-- A new checker is created that reports to the logger.
42+
impure function create_std_cfg(
43+
id : id_t := null_id;
44+
provider : string := "";
45+
vc_name : string := "";
46+
unexpected_msg_type_policy : unexpected_msg_type_policy_t := fail
47+
) return std_cfg_t;
48+
49+
-- These functions extracts information from the standard VC configuration
50+
impure function get_id(std_cfg : std_cfg_t) return id_t;
51+
impure function get_actor(std_cfg : std_cfg_t) return actor_t;
52+
impure function get_logger(std_cfg : std_cfg_t) return logger_t;
53+
impure function get_checker(std_cfg : std_cfg_t) return checker_t;
54+
impure function unexpected_msg_type_policy(std_cfg : std_cfg_t) return unexpected_msg_type_policy_t;
55+
56+
-- Handle messages with unexpected message type according to the standard configuration
57+
procedure unexpected_msg_type(msg_type : msg_type_t; std_cfg : std_cfg_t);
58+
59+
end package;
60+
61+
package body vc_pkg is
62+
constant vc_pkg_logger : logger_t := get_logger("vunit_lib:vc_pkg");
63+
constant vc_pkg_checker : checker_t := new_checker(vc_pkg_logger);
64+
65+
impure function create_std_cfg(
66+
id : id_t := null_id;
67+
provider : string := "";
68+
vc_name : string := "";
69+
unexpected_msg_type_policy : unexpected_msg_type_policy_t := fail
70+
) return std_cfg_t is
71+
variable result : std_cfg_t;
72+
variable provider_id : id_t;
73+
variable vc_id : id_t;
74+
begin
75+
if id /= null_id then
76+
result.p_id := id;
77+
else
78+
if provider = "" then
79+
check_failed(vc_pkg_checker, "A provider must be provided.");
80+
81+
-- Simplifies testing when vc_pkg_checker logger is mocked
82+
return null_std_cfg;
83+
end if;
84+
85+
if vc_name = "" then
86+
check_failed(vc_pkg_checker, "A VC name must be provided.");
87+
88+
-- Simplifies testing when vc_pkg_checker logger is mocked
89+
return null_std_cfg;
90+
end if;
91+
92+
provider_id := get_id(provider);
93+
vc_id := get_id(vc_name, parent => provider_id);
94+
result.p_id := get_id(to_string(num_children(vc_id) + 1), parent => vc_id);
95+
end if;
96+
97+
result.p_unexpected_msg_type_policy := unexpected_msg_type_policy;
98+
99+
if find(result.p_id, enable_deferred_creation => false) /= null_actor then
100+
check_failed(vc_pkg_checker, "An actor already exists for " & full_name(result.p_id) & ".");
101+
else
102+
result.p_actor := new_actor(result.p_id);
103+
end if;
104+
105+
result.p_logger := get_logger(result.p_id);
106+
result.p_checker := new_checker(result.p_logger);
107+
108+
return result;
109+
end;
110+
111+
impure function get_id(std_cfg : std_cfg_t) return id_t is
112+
begin
113+
return std_cfg.p_id;
114+
end;
115+
116+
impure function get_actor(std_cfg : std_cfg_t) return actor_t is
117+
begin
118+
return std_cfg.p_actor;
119+
end;
120+
121+
impure function get_logger(std_cfg : std_cfg_t) return logger_t is
122+
begin
123+
return std_cfg.p_logger;
124+
end;
125+
126+
impure function get_checker(std_cfg : std_cfg_t) return checker_t is
127+
begin
128+
return std_cfg.p_checker;
129+
end;
130+
131+
impure function unexpected_msg_type_policy(std_cfg : std_cfg_t) return unexpected_msg_type_policy_t is
132+
begin
133+
return std_cfg.p_unexpected_msg_type_policy;
134+
end;
135+
136+
procedure unexpected_msg_type(msg_type : msg_type_t;
137+
std_cfg : std_cfg_t) is
138+
begin
139+
if unexpected_msg_type_policy(std_cfg) = fail then
140+
unexpected_msg_type(msg_type, get_logger(std_cfg));
141+
end if;
142+
end;
143+
end package body;
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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-2024, Lars Asplund [email protected]
6+
7+
library vunit_lib;
8+
context vunit_lib.vunit_context;
9+
context work.com_context;
10+
use work.vc_pkg.all;
11+
12+
entity tb_vc_pkg is
13+
generic(runner_cfg : string);
14+
end entity;
15+
16+
architecture a of tb_vc_pkg is
17+
begin
18+
19+
main : process
20+
variable std_cfg : std_cfg_t;
21+
variable id : id_t;
22+
variable actor : actor_t;
23+
24+
constant vc_pkg_logger : logger_t := get_logger("vunit_lib:vc_pkg");
25+
constant unknown_msg_type : msg_type_t := new_msg_type("unknown_msg");
26+
begin
27+
test_runner_setup(runner, runner_cfg);
28+
while test_suite loop
29+
if run("Test that provider must be supplied with null_id") then
30+
mock(vc_pkg_logger, error);
31+
std_cfg := create_std_cfg(vc_name => "my_vc");
32+
check_only_log(vc_pkg_logger, "A provider must be provided.", error);
33+
unmock(vc_pkg_logger);
34+
35+
elsif run("Test that vc_name must be supplied with null_id") then
36+
mock(vc_pkg_logger, error);
37+
std_cfg := create_std_cfg(provider => "provider");
38+
check_only_log(vc_pkg_logger, "A VC name must be provided.", error);
39+
unmock(vc_pkg_logger);
40+
41+
elsif run("Test standard config with specified id") then
42+
id := get_id("id");
43+
std_cfg := create_std_cfg(id => id);
44+
check(std_cfg.p_id = id);
45+
check(std_cfg.p_actor = find(id, enable_deferred_creation => false));
46+
check(std_cfg.p_logger = get_logger(id));
47+
check(get_logger(std_cfg.p_checker) = get_logger(id));
48+
check(std_cfg.p_unexpected_msg_type_policy = fail);
49+
50+
elsif run("Test standard config with null_id") then
51+
for instance in 1 to 3 loop
52+
std_cfg := create_std_cfg(provider => "provider", vc_name => "vc_name");
53+
id := std_cfg.p_id;
54+
check(id = get_id("provider:vc_name:" & to_string(instance)));
55+
check(std_cfg.p_actor = find(id, enable_deferred_creation => false));
56+
check(std_cfg.p_logger = get_logger(id));
57+
check(get_logger(std_cfg.p_checker) = get_logger(id));
58+
check(std_cfg.p_unexpected_msg_type_policy = fail);
59+
end loop;
60+
61+
elsif run("Test standard config with specified unexpected message type policy") then
62+
std_cfg := create_std_cfg(
63+
provider => "provider",
64+
vc_name => "vc_name",
65+
unexpected_msg_type_policy => ignore
66+
);
67+
id := std_cfg.p_id;
68+
check(id = get_id("provider:vc_name:1"));
69+
check(std_cfg.p_actor = find(id, enable_deferred_creation => false));
70+
check(std_cfg.p_logger = get_logger(id));
71+
check(get_logger(std_cfg.p_checker) = get_logger(id));
72+
check(std_cfg.p_unexpected_msg_type_policy = ignore);
73+
74+
elsif run("Test failing on reused actor") then
75+
mock(vc_pkg_logger, error);
76+
id := get_id("foo:bar");
77+
actor := new_actor(id);
78+
std_cfg := create_std_cfg(id => id);
79+
check_only_log(vc_pkg_logger, "An actor already exists for foo:bar.", error);
80+
unmock(vc_pkg_logger);
81+
82+
elsif run("Test failing on unexpected message") then
83+
id := get_id("id");
84+
std_cfg := create_std_cfg(id => id);
85+
mock(get_logger(id), failure);
86+
unexpected_msg_type(unknown_msg_type, std_cfg);
87+
check_only_log(get_logger(id), "Got unexpected message unknown_msg", failure);
88+
unmock(get_logger(id));
89+
90+
elsif run("Test ignoring unexpected message") then
91+
id := get_id("id");
92+
std_cfg := create_std_cfg(id => id, unexpected_msg_type_policy => ignore);
93+
mock(get_logger(id), failure);
94+
unexpected_msg_type(unknown_msg_type, std_cfg);
95+
check_no_log;
96+
unmock(get_logger(id));
97+
98+
end if;
99+
end loop;
100+
test_runner_cleanup(runner, fail_on_warning => true);
101+
end process;
102+
end architecture;

0 commit comments

Comments
 (0)