blob: f96109ad82d76630cdc145c33aa991655ea7a52b [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 {
Zane Shelleyd6826e52020-11-10 17:39:00 -060046 SAMPLE = 0xdeadbeef,
47 EXPLORER_11 = 0x60d20011,
48 EXPLORER_20 = 0x60d20020,
49 P10_10 = 0x20da0010,
Zane Shelleyf8a726b2020-12-16 21:29:32 -060050 P10_20 = 0x20da0020,
Zane Shelley1be4c3c2020-04-17 15:55:07 -050051 };
52
Zane Shelley11b89942019-11-07 11:07:28 -060053 private:
Zane Shelley1be4c3c2020-04-17 15:55:07 -050054 /** The Chip Data file paths for each support chip type. */
Zane Shelley8c093d82020-05-04 22:06:52 -050055 static const std::map<SimChipType, const char*> cv_chipPath;
Zane Shelley1be4c3c2020-04-17 15:55:07 -050056
Zane Shelley11b89942019-11-07 11:07:28 -060057 /** The list of configured chips used throughout a test case. */
58 std::vector<Chip> iv_chipList;
59
Zane Shelley8c093d82020-05-04 22:06:52 -050060 /** The list of configured chip types used throughout a test case. */
61 std::vector<ChipType_t> iv_typeList;
62
Zane Shelley11b89942019-11-07 11:07:28 -060063 /** The contents of all the SCOM registers used for an iteration of
64 * isolation. */
65 std::map<Chip, std::map<uint32_t, uint64_t>> iv_scomRegData;
66
67 /** The contents of all the Indirect SCOM registers used for an iteration of
68 * isolation. */
69 std::map<Chip, std::map<uint64_t, uint64_t>> iv_idScomRegData;
70
71 /** The list of expected signatures during an iteration of isolation. */
72 std::vector<Signature> iv_expSigList;
73
74 public:
75 /**
76 * @brief Adds a chip to the list of configured chips. Also, calls the main
77 * initialize() API which will initialize the isolator with the Chip
78 * Data File associated with this chip.
79 */
80 void addChip(const Chip& i_chip);
81
Paul Greenwoodc0919342019-12-10 15:36:17 -060082 /** @brief Retrieve ScomReg from map and return its value */
83 uint64_t getScomReg(const Chip& i_chip, uint32_t i_address)
84 {
85 return iv_scomRegData[i_chip][i_address];
86 }
87
88 /** @breif Retrieve idScomReg from map and return its value */
89 uint64_t getIdScomReg(const Chip& i_chip, uint64_t i_address)
90 {
91 return iv_idScomRegData[i_chip][i_address];
92 }
93
Zane Shelley11b89942019-11-07 11:07:28 -060094 /** @brief Adds a SCOM register to iv_scomRegData. */
95 void addScomReg(const Chip& i_chip, uint32_t i_address, uint64_t i_value)
96 {
97 // First check if this entry already exists.
98 auto chip_itr = iv_scomRegData.find(i_chip);
99 if (iv_scomRegData.end() != chip_itr)
100 {
101 auto addr_itr = chip_itr->second.find(i_address);
102 ASSERT_EQ(chip_itr->second.end(), addr_itr);
103 }
104
105 // Add the new entry.
106 iv_scomRegData[i_chip][i_address] = i_value;
107 }
108
109 /** @brief Adds a SCOM register to iv_idScomRegData. */
110 void addIdScomReg(const Chip& i_chip, uint64_t i_address, uint64_t i_value)
111 {
112 // First check if this entry already exists.
113 auto chip_itr = iv_idScomRegData.find(i_chip);
114 if (iv_idScomRegData.end() != chip_itr)
115 {
116 auto addr_itr = chip_itr->second.find(i_address);
117 ASSERT_EQ(chip_itr->second.end(), addr_itr);
118 }
119
120 // Add the new entry.
121 iv_idScomRegData[i_chip][i_address] = i_value;
122 }
123
124 /** @brief Adds a Signature to iv_expSigList. */
125 void addSignature(const Signature& i_signature)
126 {
127 // First check if this entry already exists.
128 auto itr =
129 std::find(iv_expSigList.begin(), iv_expSigList.end(), i_signature);
130 ASSERT_EQ(iv_expSigList.end(), itr);
131
132 // Add the new entry.
133 iv_expSigList.push_back(i_signature);
134 }
135
136 /**
137 * @brief Flushes register and expected signature lists used for a single
138 * isolation.
139 */
140 void flushIterationData()
141 {
142 iv_scomRegData.clear();
143 iv_idScomRegData.clear();
144 iv_expSigList.clear();
145 }
146
147 /** @brief Flushes all simulation data. */
148 void flushAll()
149 {
150 flushIterationData();
151 iv_chipList.clear();
152 }
153
154 /**
155 * @brief After an iteration is set up with registers and expected
156 * signatures, this is called to run the simulation and verify the
157 * expected signatures.
158 */
159 void endIteration();
160};
161
162} // end namespace libhei
163
164//------------------------------------------------------------------------------
165
166// clang-format off
167
168// The following macros can be used to simplify commonly used function for
169// simulation test cases. At the core of each test case is a Google Test (i.e.
170// gtest), which will do most of the error checking. Just like in gtest, a test
171// case file can contain more than one test. Also, remember that this is all C++
172// code. While it not likely to be used much, you can combine these macros with
173// C++ code to do more advanced test cases. For example, you can put the
174// iteration macros in a loop to walk through each bit of a register.
175
176/**
177 * This is the beginning of a test case. The NAME parameter must be valid C++
178 * identifier and must not contain any underscores (per gtest requirement). To
179 * end the test case use END_TEST_CASE. All contents of the test case must be
180 * contain in between these two macros.
181 */
182#define START_TEST_CASE(NAME) \
183 TEST(Simulator, NAME) \
184 { \
185 libhei::SimulatorData& simData = \
186 libhei::SimulatorData::getSingleton(); \
Zane Shelley1be4c3c2020-04-17 15:55:07 -0500187 simData.flushAll(); \
188 libhei::ChipType_t chipType;
Zane Shelley11b89942019-11-07 11:07:28 -0600189
190/**
191 * Use this to configure a chip object for the test case. There should be an
192 * instance of this macro for each chip required for the test case. Note that
193 * this will also call libhei::initialize() for each new chip type. The CHIP
194 * parameter must be valid C++ identifier because it will be used as the name of
195 * the chip variable. This same identifier will be re-used in several other
196 * macros.
197 */
198#define CHIP(CHIP, TYPE) \
Zane Shelley1be4c3c2020-04-17 15:55:07 -0500199 chipType = static_cast<libhei::ChipType_t>(libhei::SimulatorData::TYPE); \
200 libhei::Chip CHIP{#CHIP, chipType}; \
Zane Shelley11b89942019-11-07 11:07:28 -0600201 simData.addChip(CHIP);
202
203/**
204 * Once all of the chips have been configured, there can be one or more
205 * iterations defined in the test case. Use END_ITERATION to end the iteration.
206 * Note that register and signature information will be reset for each
207 * iteration, however, the same set of configure chips will be used for all
208 * iterations within the test case.
209 */
210#define START_ITERATION \
211 { \
212 simData.flushIterationData();
213
214/** This will add a SCOM register to the current iteration. */
215#define REG_SCOM(CHIP, ADDR, VAL) \
216 simData.addScomReg(CHIP, static_cast<uint32_t>(ADDR), \
217 static_cast<uint64_t>(VAL));
218
219/** This will add an Indirect SCOM register to the current iteration. */
220#define REG_IDSCOM(CHIP, ADDR, VAL) \
221 simData.addIdScomReg(CHIP, static_cast<uint64_t>(ADDR), \
222 static_cast<uint64_t>(VAL));
223
224/** This will add an expected signature to the current iteration. */
225#define EXP_SIG(CHIP, ID, INST, BIT, TYPE) \
226 simData.addSignature(libhei::Signature{ \
227 CHIP, static_cast<libhei::RegisterId_t>(ID), \
Zane Shelley13b182b2020-05-07 20:23:45 -0500228 static_cast<libhei::Instance_t>(INST), \
229 static_cast<libhei::BitPosition_t>(BIT), libhei::ATTN_TYPE_##TYPE});
Zane Shelley11b89942019-11-07 11:07:28 -0600230
231/**
232 * This is the end of an iteration that began with START_ITERATION. All of the
233 * register contents and expected signatures will have been stored in the
234 * simulation data. So, this will call libhei::isolate() with the list of
235 * configured chips. Using the register contents in the simulation data,
236 * libhei::isolate() will return a list of signatures (active attentions). That
237 * list will be compared against the expected list of signatures stored in the
238 * simulation data for test case verification.
239 *
240 * You will see that there are two gtest checks for failures:
241 * - The first check will look to see if any of the previous functions to add
242 * chips, registers, or signatures to the simulation data failed.
243 * - The second check will determine if isolation completed successfully and if
244 * all expected signatures have been verified.
245 * If either check fails, the test case will be aborted regardless if there are
246 * additional iterations in that test case. Note that failure in a test case
247 * will not have any impact on subsequent test cases. Therefore, all test cases
248 * in a file will at least be attempted even if there is a failure.
249 */
250#define END_ITERATION \
251 if (HasFailure()) { simData.flushAll(); return; } \
252 simData.endIteration(); \
253 if (HasFailure()) { simData.flushAll(); return; } \
254 }
255
256/**
257 * This is the end of the test case that started with START_TEST_CASE. It will
258 * call libhei::uninitialize() and clean up the simulation data.
259 */
260#define END_TEST_CASE \
261 libhei::uninitialize(); \
262 simData.flushAll(); \
263 }
264
265// clang-format on