blob: b68ec805005e8c2eba7f155dd59115cd9e70180e [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
25void SimulatorData::addChip(const Chip& i_chip)
26{
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
Zane Shelley1be4c3c2020-04-17 15:55:07 -050045 // Look for the file path
Zane Shelley8c093d82020-05-04 22:06:52 -050046 auto itr2 = cv_chipPath.find(static_cast<SimChipType>(chipType));
Zane Shelley1be4c3c2020-04-17 15:55:07 -050047 ASSERT_NE(cv_chipPath.end(), itr2);
48 const char* path = itr2->second;
Zane Shelley11b89942019-11-07 11:07:28 -060049
Zane Shelley1be4c3c2020-04-17 15:55:07 -050050 // Open the Chip Data File
51 std::ifstream cdf{path, std::ifstream::binary};
52 ASSERT_TRUE(cdf.good());
53
54 // Get the length of file
55 cdf.seekg(0, cdf.end);
56 size_t sz_buffer = cdf.tellg();
57 cdf.seekg(0, cdf.beg);
58
59 // Allocate memory
60 char* buffer = new char[sz_buffer];
61
62 // Read data as a block
63 cdf.read(buffer, sz_buffer);
64
65 // Close the Chip Data File
66 cdf.close();
67
68 // Initilize the chip with this Chip Data File.
Zane Shelleydd109cc2020-04-30 21:41:41 -050069 initialize(buffer, sz_buffer);
Zane Shelley1be4c3c2020-04-17 15:55:07 -050070
71 // Clean up the buffer
72 delete[] buffer;
Zane Shelley11b89942019-11-07 11:07:28 -060073}
74
75//------------------------------------------------------------------------------
76
Zane Shelley2215d232023-04-07 14:43:40 -050077const char* __attn(AttentionType_t i_type)
78{
79 const char* str = "";
80 switch (i_type)
81 {
82 case ATTN_TYPE_CHIP_CS:
83 str = "CHIP_CS";
84 break;
85 case ATTN_TYPE_UNIT_CS:
86 str = "UNIT_CS";
87 break;
88 case ATTN_TYPE_RECOVERABLE:
89 str = "RECOVERABLE";
90 break;
91 case ATTN_TYPE_SP_ATTN:
92 str = "SP_ATTN";
93 break;
94 case ATTN_TYPE_HOST_ATTN:
95 str = "HOST_ATTN";
96 break;
97 default:
98 HEI_ERR("Unsupported attention type: %u", i_type);
99 assert(0);
100 }
101 return str;
102}
103
104//------------------------------------------------------------------------------
105
Zane Shelley11b89942019-11-07 11:07:28 -0600106void SimulatorData::endIteration()
107{
108 // Start by calling libhei::isolate().
109 IsolationData isoData{};
Zane Shelley229c1552020-05-04 22:44:15 -0500110 isolate(iv_chipList, isoData);
Zane Shelley11b89942019-11-07 11:07:28 -0600111
Zane Shelley88eb2022022-01-20 14:25:14 -0600112 /* TODO: Currently used for debug. Eventually, we want this written to file.
Zane Shelleydae1af62021-01-14 11:00:46 -0600113 for (const auto& e : isoData.getRegisterDump())
114 {
115 HEI_INF("Chip: %s", (const char*)e.first.getChip());
116
117 for (const auto& r : e.second)
118 {
119 HEI_INF(" Reg: 0x%06x %d 0x%016" PRIx64, r.regId, r.regInst,
120 r.data->getFieldRight(0, 64));
121 }
122 }
123 */
124
Zane Shelley11b89942019-11-07 11:07:28 -0600125 // Get the list of signatures found in isolation.
126 std::vector<Signature> givenSigList = isoData.getSignatureList();
127
Zane Shelley2215d232023-04-07 14:43:40 -0500128 // Print out the expected signature list and verify matches with given list.
129 HEI_INF("Signature summary:")
130 bool mismatch = false;
131 for (const auto& e : iv_expSigList)
Zane Shelley88eb2022022-01-20 14:25:14 -0600132 {
Zane Shelley2215d232023-04-07 14:43:40 -0500133 auto gItr = std::find(givenSigList.begin(), givenSigList.end(), e);
134 if (givenSigList.end() != gItr)
135 {
136 HEI_INF(" Match: %s 0x%04x %2u %2u %s",
137 (const char*)e.getChip().getChip(), e.getId(),
138 e.getInstance(), e.getBit(), __attn(e.getAttnType()));
Zane Shelley88eb2022022-01-20 14:25:14 -0600139
Zane Shelley2215d232023-04-07 14:43:40 -0500140 // Remove from given signature list so we can determine if there are
141 // any leftovers at the end.
142 givenSigList.erase(gItr);
143 }
144 else
145 {
146 HEI_INF(" No match: %s 0x%04x %2u %2u %s",
147 (const char*)e.getChip().getChip(), e.getId(),
148 e.getInstance(), e.getBit(), __attn(e.getAttnType()));
149
150 mismatch = true;
151 }
152 }
153
154 // Print out any leftover signatures from the given list.
155 for (const auto& g : givenSigList)
156 {
157 HEI_INF(" Unexpected: %s 0x%04x %2u %2u %s",
158 (const char*)g.getChip().getChip(), g.getId(), g.getInstance(),
159 g.getBit(), __attn(g.getAttnType()));
160
161 mismatch = true;
162 }
163
164 // Final check for mismatches.
165 ASSERT_FALSE(mismatch);
Zane Shelley11b89942019-11-07 11:07:28 -0600166
167 // The iteration is complete so we can flush the data.
168 flushIterationData();
169}
Zane Shelley1be4c3c2020-04-17 15:55:07 -0500170
171} // end namespace libhei