blob: 572809ef37b8bf6a98339acf41afeac6ed2a43fa [file] [log] [blame]
Zane Shelley11b89942019-11-07 11:07:28 -06001#include "simulator.hpp"
2
Zane Shelleydae1af62021-01-14 11:00:46 -06003#include <util/hei_includes.hpp>
4
Zane Shelley1be4c3c2020-04-17 15:55:07 -05005#include <fstream> // std::ifstream
6
7namespace libhei
8{
9
10//------------------------------------------------------------------------------
11
12// Paths are relative from the build/ directory
Zane Shelley8c093d82020-05-04 22:06:52 -050013const std::map<SimulatorData::SimChipType, const char*>
Zane Shelley1be4c3c2020-04-17 15:55:07 -050014 SimulatorData::cv_chipPath = {
Zane Shelleyaadf3bf2020-04-30 21:25:29 -050015 {SAMPLE, "../test/simulator/sample_data/sample.cdb"},
Zane Shelley2215d232023-04-07 14:43:40 -050016 {EXPLORER_11, "chip_data/chip_data_explorer_11.cdb"},
17 {EXPLORER_20, "chip_data/chip_data_explorer_20.cdb"},
Caleb Palmere4ad4e32024-08-07 09:48:14 -050018 {ODYSSEY_10, "chip_data/chip_data_odyssey_10.cdb"},
Zane Shelley2215d232023-04-07 14:43:40 -050019 {P10_10, "chip_data/chip_data_p10_10.cdb"},
20 {P10_20, "chip_data/chip_data_p10_20.cdb"},
Zane Shelley1be4c3c2020-04-17 15:55:07 -050021};
Zane Shelley11b89942019-11-07 11:07:28 -060022
23//------------------------------------------------------------------------------
24
Caleb Palmer446da092025-01-16 10:22:02 -060025void SimulatorData::addChip(const Chip& i_chip, const char* i_chipPath)
Zane Shelley11b89942019-11-07 11:07:28 -060026{
27 // First check if this entry already exists.
Zane Shelley8c093d82020-05-04 22:06:52 -050028 auto chip_itr = std::find(iv_chipList.begin(), iv_chipList.end(), i_chip);
29 ASSERT_EQ(iv_chipList.end(), chip_itr);
Zane Shelley11b89942019-11-07 11:07:28 -060030
31 // Add the new entry.
32 iv_chipList.push_back(i_chip);
33
Zane Shelley8c093d82020-05-04 22:06:52 -050034 // Check if this chip type has been initialized.
35 ChipType_t chipType = i_chip.getType();
36 auto type_itr = std::find(iv_typeList.begin(), iv_typeList.end(), chipType);
37 if (iv_typeList.end() != type_itr)
38 {
39 return; // No need to continue.
40 }
41
42 // Add the new entry.
43 iv_typeList.push_back(chipType);
44
Caleb Palmer446da092025-01-16 10:22:02 -060045 // Look for the file path, if a path was provided use that instead of one
46 // of the hardcoded values in the map.
47 ASSERT_TRUE(nullptr != i_chipPath);
48 const char* path = "";
49 if (0 != strcmp(i_chipPath, ""))
50 {
51 path = i_chipPath;
52 }
53 else
54 {
55 auto itr2 = cv_chipPath.find(static_cast<SimChipType>(chipType));
56 ASSERT_NE(cv_chipPath.end(), itr2);
57 path = itr2->second;
58 }
Zane Shelley11b89942019-11-07 11:07:28 -060059
Zane Shelley1be4c3c2020-04-17 15:55:07 -050060 // Open the Chip Data File
61 std::ifstream cdf{path, std::ifstream::binary};
62 ASSERT_TRUE(cdf.good());
63
64 // Get the length of file
65 cdf.seekg(0, cdf.end);
66 size_t sz_buffer = cdf.tellg();
67 cdf.seekg(0, cdf.beg);
68
69 // Allocate memory
70 char* buffer = new char[sz_buffer];
71
72 // Read data as a block
73 cdf.read(buffer, sz_buffer);
74
75 // Close the Chip Data File
76 cdf.close();
77
78 // Initilize the chip with this Chip Data File.
Zane Shelleydd109cc2020-04-30 21:41:41 -050079 initialize(buffer, sz_buffer);
Zane Shelley1be4c3c2020-04-17 15:55:07 -050080
81 // Clean up the buffer
82 delete[] buffer;
Zane Shelley11b89942019-11-07 11:07:28 -060083}
84
85//------------------------------------------------------------------------------
86
Zane Shelley2215d232023-04-07 14:43:40 -050087const char* __attn(AttentionType_t i_type)
88{
89 const char* str = "";
90 switch (i_type)
91 {
92 case ATTN_TYPE_CHIP_CS:
93 str = "CHIP_CS";
94 break;
95 case ATTN_TYPE_UNIT_CS:
96 str = "UNIT_CS";
97 break;
98 case ATTN_TYPE_RECOVERABLE:
99 str = "RECOVERABLE";
100 break;
101 case ATTN_TYPE_SP_ATTN:
102 str = "SP_ATTN";
103 break;
104 case ATTN_TYPE_HOST_ATTN:
105 str = "HOST_ATTN";
106 break;
107 default:
108 HEI_ERR("Unsupported attention type: %u", i_type);
109 assert(0);
110 }
111 return str;
112}
113
114//------------------------------------------------------------------------------
115
Caleb Palmer446da092025-01-16 10:22:02 -0600116void SimulatorData::simIsolate(IsolationData& o_isoData)
Zane Shelley11b89942019-11-07 11:07:28 -0600117{
118 // Start by calling libhei::isolate().
119 IsolationData isoData{};
Caleb Palmer446da092025-01-16 10:22:02 -0600120 isolate(iv_chipList, o_isoData);
Zane Shelley11b89942019-11-07 11:07:28 -0600121
Zane Shelley88eb2022022-01-20 14:25:14 -0600122 /* TODO: Currently used for debug. Eventually, we want this written to file.
Zane Shelleydae1af62021-01-14 11:00:46 -0600123 for (const auto& e : isoData.getRegisterDump())
124 {
125 HEI_INF("Chip: %s", (const char*)e.first.getChip());
126
127 for (const auto& r : e.second)
128 {
129 HEI_INF(" Reg: 0x%06x %d 0x%016" PRIx64, r.regId, r.regInst,
130 r.data->getFieldRight(0, 64));
131 }
132 }
133 */
134
Zane Shelley11b89942019-11-07 11:07:28 -0600135 // Get the list of signatures found in isolation.
Caleb Palmer446da092025-01-16 10:22:02 -0600136 std::vector<Signature> givenSigList = o_isoData.getSignatureList();
Zane Shelley11b89942019-11-07 11:07:28 -0600137
Zane Shelley2215d232023-04-07 14:43:40 -0500138 // Print out the expected signature list and verify matches with given list.
139 HEI_INF("Signature summary:")
140 bool mismatch = false;
141 for (const auto& e : iv_expSigList)
Zane Shelley88eb2022022-01-20 14:25:14 -0600142 {
Zane Shelley2215d232023-04-07 14:43:40 -0500143 auto gItr = std::find(givenSigList.begin(), givenSigList.end(), e);
144 if (givenSigList.end() != gItr)
145 {
146 HEI_INF(" Match: %s 0x%04x %2u %2u %s",
147 (const char*)e.getChip().getChip(), e.getId(),
148 e.getInstance(), e.getBit(), __attn(e.getAttnType()));
Zane Shelley88eb2022022-01-20 14:25:14 -0600149
Zane Shelley2215d232023-04-07 14:43:40 -0500150 // Remove from given signature list so we can determine if there are
151 // any leftovers at the end.
152 givenSigList.erase(gItr);
153 }
154 else
155 {
156 HEI_INF(" No match: %s 0x%04x %2u %2u %s",
157 (const char*)e.getChip().getChip(), e.getId(),
158 e.getInstance(), e.getBit(), __attn(e.getAttnType()));
159
160 mismatch = true;
161 }
162 }
163
164 // Print out any leftover signatures from the given list.
165 for (const auto& g : givenSigList)
166 {
167 HEI_INF(" Unexpected: %s 0x%04x %2u %2u %s",
168 (const char*)g.getChip().getChip(), g.getId(), g.getInstance(),
169 g.getBit(), __attn(g.getAttnType()));
170
171 mismatch = true;
172 }
173
174 // Final check for mismatches.
175 ASSERT_FALSE(mismatch);
Caleb Palmer446da092025-01-16 10:22:02 -0600176}
177
178//------------------------------------------------------------------------------
179
180void SimulatorData::endIteration()
181{
182 // First call simIsolate to isolate and check the expected signatures.
183 IsolationData isoData{};
184 simIsolate(isoData);
Zane Shelley11b89942019-11-07 11:07:28 -0600185
186 // The iteration is complete so we can flush the data.
187 flushIterationData();
188}
Zane Shelley1be4c3c2020-04-17 15:55:07 -0500189
190} // end namespace libhei