blob: e3b6bef4f05de637cb4655d9c8356a1d18a6cabd [file] [log] [blame]
Zane Shelley11b89942019-11-07 11:07:28 -06001#pragma once
2
3#include <hei_main.hpp>
4
Zane Shelley3a02e242020-05-08 16:25:36 -05005#include <algorithm>
6#include <map>
7#include <vector>
8
Zane Shelley11b89942019-11-07 11:07:28 -06009#include "gtest/gtest.h"
10
11namespace libhei
12{
13
14/**
15 * @brief Contains simulated chip objects and register contents used during
16 * isolation. Also contains the expected signatures to compare after
17 * isolation.
18 */
19class SimulatorData
20{
21 private: // This class cannot be instantiated. Use getSingleton() instead.
22 /** @brief Default constructor. */
23 SimulatorData() = default;
24
25 /** @brief Destructor. */
26 ~SimulatorData() = default;
27
28 /** @brief Copy constructor. */
29 SimulatorData(const SimulatorData&) = delete;
30
31 /** @brief Assignment operator. */
32 SimulatorData& operator=(const SimulatorData&) = delete;
33
34 public:
35 /** @brief Provides access to a singleton instance of this object. */
36 static SimulatorData& getSingleton()
37 {
38 static SimulatorData theSimData{};
39 return theSimData;
40 }
41
Zane Shelley1be4c3c2020-04-17 15:55:07 -050042 public:
43 /** The list of supported chip types for the simulator. */
Zane Shelley8c093d82020-05-04 22:06:52 -050044 enum SimChipType
Zane Shelley1be4c3c2020-04-17 15:55:07 -050045 {
Patrick Williams2f7537d2023-05-10 07:51:39 -050046 SAMPLE = 0xdeadbeef,
Zane Shelleyd6826e52020-11-10 17:39:00 -060047 EXPLORER_11 = 0x60d20011,
48 EXPLORER_20 = 0x60d20020,
Caleb Palmere4ad4e32024-08-07 09:48:14 -050049 ODYSSEY_10 = 0x60c00010,
Patrick Williams2f7537d2023-05-10 07:51:39 -050050 P10_10 = 0x20da0010,
51 P10_20 = 0x20da0020,
Zane Shelley1be4c3c2020-04-17 15:55:07 -050052 };
53
Zane Shelley11b89942019-11-07 11:07:28 -060054 private:
Zane Shelley1be4c3c2020-04-17 15:55:07 -050055 /** The Chip Data file paths for each support chip type. */
Zane Shelley8c093d82020-05-04 22:06:52 -050056 static const std::map<SimChipType, const char*> cv_chipPath;
Zane Shelley1be4c3c2020-04-17 15:55:07 -050057
Zane Shelley11b89942019-11-07 11:07:28 -060058 /** The list of configured chips used throughout a test case. */
59 std::vector<Chip> iv_chipList;
60
Zane Shelley8c093d82020-05-04 22:06:52 -050061 /** The list of configured chip types used throughout a test case. */
62 std::vector<ChipType_t> iv_typeList;
63
Zane Shelley11b89942019-11-07 11:07:28 -060064 /** The contents of all the SCOM registers used for an iteration of
65 * isolation. */
66 std::map<Chip, std::map<uint32_t, uint64_t>> iv_scomRegData;
67
68 /** The contents of all the Indirect SCOM registers used for an iteration of
69 * isolation. */
70 std::map<Chip, std::map<uint64_t, uint64_t>> iv_idScomRegData;
71
72 /** The list of expected signatures during an iteration of isolation. */
73 std::vector<Signature> iv_expSigList;
74
75 public:
76 /**
77 * @brief Adds a chip to the list of configured chips. Also, calls the main
78 * initialize() API which will initialize the isolator with the Chip
79 * Data File associated with this chip.
80 */
81 void addChip(const Chip& i_chip);
82
Paul Greenwoodc0919342019-12-10 15:36:17 -060083 /** @brief Retrieve ScomReg from map and return its value */
84 uint64_t getScomReg(const Chip& i_chip, uint32_t i_address)
85 {
86 return iv_scomRegData[i_chip][i_address];
87 }
88
89 /** @breif Retrieve idScomReg from map and return its value */
90 uint64_t getIdScomReg(const Chip& i_chip, uint64_t i_address)
91 {
92 return iv_idScomRegData[i_chip][i_address];
93 }
94
Zane Shelley11b89942019-11-07 11:07:28 -060095 /** @brief Adds a SCOM register to iv_scomRegData. */
96 void addScomReg(const Chip& i_chip, uint32_t i_address, uint64_t i_value)
97 {
98 // First check if this entry already exists.
99 auto chip_itr = iv_scomRegData.find(i_chip);
100 if (iv_scomRegData.end() != chip_itr)
101 {
102 auto addr_itr = chip_itr->second.find(i_address);
103 ASSERT_EQ(chip_itr->second.end(), addr_itr);
104 }
105
106 // Add the new entry.
107 iv_scomRegData[i_chip][i_address] = i_value;
108 }
109
110 /** @brief Adds a SCOM register to iv_idScomRegData. */
111 void addIdScomReg(const Chip& i_chip, uint64_t i_address, uint64_t i_value)
112 {
113 // First check if this entry already exists.
114 auto chip_itr = iv_idScomRegData.find(i_chip);
115 if (iv_idScomRegData.end() != chip_itr)
116 {
117 auto addr_itr = chip_itr->second.find(i_address);
118 ASSERT_EQ(chip_itr->second.end(), addr_itr);
119 }
120
121 // Add the new entry.
122 iv_idScomRegData[i_chip][i_address] = i_value;
123 }
124
125 /** @brief Adds a Signature to iv_expSigList. */
126 void addSignature(const Signature& i_signature)
127 {
128 // First check if this entry already exists.
Patrick Williams8db65db2024-08-16 15:22:30 -0400129 auto itr =
130 std::find(iv_expSigList.begin(), iv_expSigList.end(), i_signature);
Zane Shelley11b89942019-11-07 11:07:28 -0600131 ASSERT_EQ(iv_expSigList.end(), itr);
132
133 // Add the new entry.
134 iv_expSigList.push_back(i_signature);
135 }
136
137 /**
138 * @brief Flushes register and expected signature lists used for a single
139 * isolation.
140 */
141 void flushIterationData()
142 {
143 iv_scomRegData.clear();
144 iv_idScomRegData.clear();
145 iv_expSigList.clear();
146 }
147
148 /** @brief Flushes all simulation data. */
149 void flushAll()
150 {
151 flushIterationData();
152 iv_chipList.clear();
Zane Shelleydcf902b2021-07-15 22:18:35 -0500153 iv_typeList.clear();
Zane Shelley11b89942019-11-07 11:07:28 -0600154 }
155
156 /**
157 * @brief After an iteration is set up with registers and expected
158 * signatures, this is called to run the simulation and verify the
159 * expected signatures.
160 */
161 void endIteration();
162};
163
164} // end namespace libhei
165
166//------------------------------------------------------------------------------
167
168// clang-format off
169
170// The following macros can be used to simplify commonly used function for
171// simulation test cases. At the core of each test case is a Google Test (i.e.
172// gtest), which will do most of the error checking. Just like in gtest, a test
173// case file can contain more than one test. Also, remember that this is all C++
174// code. While it not likely to be used much, you can combine these macros with
175// C++ code to do more advanced test cases. For example, you can put the
176// iteration macros in a loop to walk through each bit of a register.
177
178/**
179 * This is the beginning of a test case. The NAME parameter must be valid C++
180 * identifier and must not contain any underscores (per gtest requirement). To
181 * end the test case use END_TEST_CASE. All contents of the test case must be
182 * contain in between these two macros.
183 */
184#define START_TEST_CASE(NAME) \
185 TEST(Simulator, NAME) \
186 { \
187 libhei::SimulatorData& simData = \
188 libhei::SimulatorData::getSingleton(); \
Zane Shelley1be4c3c2020-04-17 15:55:07 -0500189 simData.flushAll(); \
190 libhei::ChipType_t chipType;
Zane Shelley11b89942019-11-07 11:07:28 -0600191
192/**
193 * Use this to configure a chip object for the test case. There should be an
194 * instance of this macro for each chip required for the test case. Note that
195 * this will also call libhei::initialize() for each new chip type. The CHIP
196 * parameter must be valid C++ identifier because it will be used as the name of
197 * the chip variable. This same identifier will be re-used in several other
198 * macros.
199 */
200#define CHIP(CHIP, TYPE) \
Zane Shelley1be4c3c2020-04-17 15:55:07 -0500201 chipType = static_cast<libhei::ChipType_t>(libhei::SimulatorData::TYPE); \
202 libhei::Chip CHIP{#CHIP, chipType}; \
Zane Shelley11b89942019-11-07 11:07:28 -0600203 simData.addChip(CHIP);
204
205/**
206 * Once all of the chips have been configured, there can be one or more
207 * iterations defined in the test case. Use END_ITERATION to end the iteration.
208 * Note that register and signature information will be reset for each
209 * iteration, however, the same set of configure chips will be used for all
210 * iterations within the test case.
211 */
212#define START_ITERATION \
213 { \
214 simData.flushIterationData();
215
216/** This will add a SCOM register to the current iteration. */
217#define REG_SCOM(CHIP, ADDR, VAL) \
218 simData.addScomReg(CHIP, static_cast<uint32_t>(ADDR), \
219 static_cast<uint64_t>(VAL));
220
221/** This will add an Indirect SCOM register to the current iteration. */
222#define REG_IDSCOM(CHIP, ADDR, VAL) \
223 simData.addIdScomReg(CHIP, static_cast<uint64_t>(ADDR), \
224 static_cast<uint64_t>(VAL));
225
226/** This will add an expected signature to the current iteration. */
227#define EXP_SIG(CHIP, ID, INST, BIT, TYPE) \
228 simData.addSignature(libhei::Signature{ \
229 CHIP, static_cast<libhei::RegisterId_t>(ID), \
Zane Shelley13b182b2020-05-07 20:23:45 -0500230 static_cast<libhei::Instance_t>(INST), \
231 static_cast<libhei::BitPosition_t>(BIT), libhei::ATTN_TYPE_##TYPE});
Zane Shelley11b89942019-11-07 11:07:28 -0600232
233/**
234 * This is the end of an iteration that began with START_ITERATION. All of the
235 * register contents and expected signatures will have been stored in the
236 * simulation data. So, this will call libhei::isolate() with the list of
237 * configured chips. Using the register contents in the simulation data,
238 * libhei::isolate() will return a list of signatures (active attentions). That
239 * list will be compared against the expected list of signatures stored in the
240 * simulation data for test case verification.
241 *
242 * You will see that there are two gtest checks for failures:
243 * - The first check will look to see if any of the previous functions to add
244 * chips, registers, or signatures to the simulation data failed.
245 * - The second check will determine if isolation completed successfully and if
246 * all expected signatures have been verified.
247 * If either check fails, the test case will be aborted regardless if there are
248 * additional iterations in that test case. Note that failure in a test case
249 * will not have any impact on subsequent test cases. Therefore, all test cases
250 * in a file will at least be attempted even if there is a failure.
251 */
252#define END_ITERATION \
253 if (HasFailure()) { simData.flushAll(); return; } \
254 simData.endIteration(); \
255 if (HasFailure()) { simData.flushAll(); return; } \
256 }
257
258/**
259 * This is the end of the test case that started with START_TEST_CASE. It will
260 * call libhei::uninitialize() and clean up the simulation data.
261 */
262#define END_TEST_CASE \
263 libhei::uninitialize(); \
264 simData.flushAll(); \
265 }
266
267// clang-format on