|
| 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; |
0 commit comments