blob: 408d14ad1aba1059db675807bfd37328b73d51ad [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"},
18 {P10_10, "chip_data/chip_data_p10_10.cdb"},
19 {P10_20, "chip_data/chip_data_p10_20.cdb"},
Zane Shelley1be4c3c2020-04-17 15:55:07 -050020};
Zane Shelley11b89942019-11-07 11:07:28 -060021
22//------------------------------------------------------------------------------
23
24void SimulatorData::addChip(const Chip& i_chip)
25{
26 // First check if this entry already exists.
Zane Shelley8c093d82020-05-04 22:06:52 -050027 auto chip_itr = std::find(iv_chipList.begin(), iv_chipList.end(), i_chip);
28 ASSERT_EQ(iv_chipList.end(), chip_itr);
Zane Shelley11b89942019-11-07 11:07:28 -060029
30 // Add the new entry.
31 iv_chipList.push_back(i_chip);
32
Zane Shelley8c093d82020-05-04 22:06:52 -050033 // Check if this chip type has been initialized.
34 ChipType_t chipType = i_chip.getType();
35 auto type_itr = std::find(iv_typeList.begin(), iv_typeList.end(), chipType);
36 if (iv_typeList.end() != type_itr)
37 {
38 return; // No need to continue.
39 }
40
41 // Add the new entry.
42 iv_typeList.push_back(chipType);
43
Zane Shelley1be4c3c2020-04-17 15:55:07 -050044 // Look for the file path
Zane Shelley8c093d82020-05-04 22:06:52 -050045 auto itr2 = cv_chipPath.find(static_cast<SimChipType>(chipType));
Zane Shelley1be4c3c2020-04-17 15:55:07 -050046 ASSERT_NE(cv_chipPath.end(), itr2);
47 const char* path = itr2->second;
Zane Shelley11b89942019-11-07 11:07:28 -060048
Zane Shelley1be4c3c2020-04-17 15:55:07 -050049 // Open the Chip Data File
50 std::ifstream cdf{path, std::ifstream::binary};
51 ASSERT_TRUE(cdf.good());
52
53 // Get the length of file
54 cdf.seekg(0, cdf.end);
55 size_t sz_buffer = cdf.tellg();
56 cdf.seekg(0, cdf.beg);
57
58 // Allocate memory
59 char* buffer = new char[sz_buffer];
60
61 // Read data as a block
62 cdf.read(buffer, sz_buffer);
63
64 // Close the Chip Data File
65 cdf.close();
66
67 // Initilize the chip with this Chip Data File.
Zane Shelleydd109cc2020-04-30 21:41:41 -050068 initialize(buffer, sz_buffer);
Zane Shelley1be4c3c2020-04-17 15:55:07 -050069
70 // Clean up the buffer
71 delete[] buffer;
Zane Shelley11b89942019-11-07 11:07:28 -060072}
73
74//------------------------------------------------------------------------------
75
Zane Shelley2215d232023-04-07 14:43:40 -050076const char* __attn(AttentionType_t i_type)
77{
78 const char* str = "";
79 switch (i_type)
80 {
81 case ATTN_TYPE_CHIP_CS:
82 str = "CHIP_CS";
83 break;
84 case ATTN_TYPE_UNIT_CS:
85 str = "UNIT_CS";
86 break;
87 case ATTN_TYPE_RECOVERABLE:
88 str = "RECOVERABLE";
89 break;
90 case ATTN_TYPE_SP_ATTN:
91 str = "SP_ATTN";
92 break;
93 case ATTN_TYPE_HOST_ATTN:
94 str = "HOST_ATTN";
95 break;
96 default:
97 HEI_ERR("Unsupported attention type: %u", i_type);
98 assert(0);
99 }
100 return str;
101}
102
103//------------------------------------------------------------------------------
104
Zane Shelley11b89942019-11-07 11:07:28 -0600105void SimulatorData::endIteration()
106{
107 // Start by calling libhei::isolate().
108 IsolationData isoData{};
Zane Shelley229c1552020-05-04 22:44:15 -0500109 isolate(iv_chipList, isoData);
Zane Shelley11b89942019-11-07 11:07:28 -0600110
Zane Shelley88eb2022022-01-20 14:25:14 -0600111 /* TODO: Currently used for debug. Eventually, we want this written to file.
Zane Shelleydae1af62021-01-14 11:00:46 -0600112 for (const auto& e : isoData.getRegisterDump())
113 {
114 HEI_INF("Chip: %s", (const char*)e.first.getChip());
115
116 for (const auto& r : e.second)
117 {
118 HEI_INF(" Reg: 0x%06x %d 0x%016" PRIx64, r.regId, r.regInst,
119 r.data->getFieldRight(0, 64));
120 }
121 }
122 */
123
Zane Shelley11b89942019-11-07 11:07:28 -0600124 // Get the list of signatures found in isolation.
125 std::vector<Signature> givenSigList = isoData.getSignatureList();
126
Zane Shelley2215d232023-04-07 14:43:40 -0500127 // Print out the expected signature list and verify matches with given list.
128 HEI_INF("Signature summary:")
129 bool mismatch = false;
130 for (const auto& e : iv_expSigList)
Zane Shelley88eb2022022-01-20 14:25:14 -0600131 {
Zane Shelley2215d232023-04-07 14:43:40 -0500132 auto gItr = std::find(givenSigList.begin(), givenSigList.end(), e);
133 if (givenSigList.end() != gItr)
134 {
135 HEI_INF(" Match: %s 0x%04x %2u %2u %s",
136 (const char*)e.getChip().getChip(), e.getId(),
137 e.getInstance(), e.getBit(), __attn(e.getAttnType()));
Zane Shelley88eb2022022-01-20 14:25:14 -0600138
Zane Shelley2215d232023-04-07 14:43:40 -0500139 // Remove from given signature list so we can determine if there are
140 // any leftovers at the end.
141 givenSigList.erase(gItr);
142 }
143 else
144 {
145 HEI_INF(" No match: %s 0x%04x %2u %2u %s",
146 (const char*)e.getChip().getChip(), e.getId(),
147 e.getInstance(), e.getBit(), __attn(e.getAttnType()));
148
149 mismatch = true;
150 }
151 }
152
153 // Print out any leftover signatures from the given list.
154 for (const auto& g : givenSigList)
155 {
156 HEI_INF(" Unexpected: %s 0x%04x %2u %2u %s",
157 (const char*)g.getChip().getChip(), g.getId(), g.getInstance(),
158 g.getBit(), __attn(g.getAttnType()));
159
160 mismatch = true;
161 }
162
163 // Final check for mismatches.
164 ASSERT_FALSE(mismatch);
Zane Shelley11b89942019-11-07 11:07:28 -0600165
166 // The iteration is complete so we can flush the data.
167 flushIterationData();
168}
Zane Shelley1be4c3c2020-04-17 15:55:07 -0500169
170} // end namespace libhei