Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 1 | #include "simulator.hpp" |
| 2 | |
Zane Shelley | dae1af6 | 2021-01-14 11:00:46 -0600 | [diff] [blame] | 3 | #include <util/hei_includes.hpp> |
| 4 | |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 5 | #include <fstream> // std::ifstream |
| 6 | |
| 7 | namespace libhei |
| 8 | { |
| 9 | |
| 10 | //------------------------------------------------------------------------------ |
| 11 | |
| 12 | // Paths are relative from the build/ directory |
Zane Shelley | 8c093d8 | 2020-05-04 22:06:52 -0500 | [diff] [blame] | 13 | const std::map<SimulatorData::SimChipType, const char*> |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 14 | SimulatorData::cv_chipPath = { |
Zane Shelley | aadf3bf | 2020-04-30 21:25:29 -0500 | [diff] [blame] | 15 | {SAMPLE, "../test/simulator/sample_data/sample.cdb"}, |
Zane Shelley | bcb4395 | 2021-07-08 22:13:57 -0500 | [diff] [blame] | 16 | {EXPLORER_11, "xml/chip_data_explorer_11.cdb"}, |
| 17 | {EXPLORER_20, "xml/chip_data_explorer_20.cdb"}, |
| 18 | {P10_10, "xml/chip_data_p10_10.cdb"}, |
| 19 | {P10_20, "xml/chip_data_p10_20.cdb"}, |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 20 | }; |
Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 21 | |
| 22 | //------------------------------------------------------------------------------ |
| 23 | |
| 24 | void SimulatorData::addChip(const Chip& i_chip) |
| 25 | { |
| 26 | // First check if this entry already exists. |
Zane Shelley | 8c093d8 | 2020-05-04 22:06:52 -0500 | [diff] [blame] | 27 | auto chip_itr = std::find(iv_chipList.begin(), iv_chipList.end(), i_chip); |
| 28 | ASSERT_EQ(iv_chipList.end(), chip_itr); |
Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 29 | |
| 30 | // Add the new entry. |
| 31 | iv_chipList.push_back(i_chip); |
| 32 | |
Zane Shelley | 8c093d8 | 2020-05-04 22:06:52 -0500 | [diff] [blame] | 33 | // 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 Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 44 | // Look for the file path |
Zane Shelley | 8c093d8 | 2020-05-04 22:06:52 -0500 | [diff] [blame] | 45 | auto itr2 = cv_chipPath.find(static_cast<SimChipType>(chipType)); |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 46 | ASSERT_NE(cv_chipPath.end(), itr2); |
| 47 | const char* path = itr2->second; |
Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 48 | |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 49 | // 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 Shelley | dd109cc | 2020-04-30 21:41:41 -0500 | [diff] [blame] | 68 | initialize(buffer, sz_buffer); |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 69 | |
| 70 | // Clean up the buffer |
| 71 | delete[] buffer; |
Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | //------------------------------------------------------------------------------ |
| 75 | |
| 76 | void SimulatorData::endIteration() |
| 77 | { |
| 78 | // Start by calling libhei::isolate(). |
| 79 | IsolationData isoData{}; |
Zane Shelley | 229c155 | 2020-05-04 22:44:15 -0500 | [diff] [blame] | 80 | isolate(iv_chipList, isoData); |
Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 81 | |
Zane Shelley | 88eb202 | 2022-01-20 14:25:14 -0600 | [diff] [blame] | 82 | /* TODO: Currently used for debug. Eventually, we want this written to file. |
Zane Shelley | dae1af6 | 2021-01-14 11:00:46 -0600 | [diff] [blame] | 83 | for (const auto& e : isoData.getRegisterDump()) |
| 84 | { |
| 85 | HEI_INF("Chip: %s", (const char*)e.first.getChip()); |
| 86 | |
| 87 | for (const auto& r : e.second) |
| 88 | { |
| 89 | HEI_INF(" Reg: 0x%06x %d 0x%016" PRIx64, r.regId, r.regInst, |
| 90 | r.data->getFieldRight(0, 64)); |
| 91 | } |
| 92 | } |
| 93 | */ |
| 94 | |
Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 95 | // Get the list of signatures found in isolation. |
| 96 | std::vector<Signature> givenSigList = isoData.getSignatureList(); |
| 97 | |
| 98 | // Verify the expected list and given list are the same. |
| 99 | ASSERT_EQ(iv_expSigList.size(), givenSigList.size()); |
| 100 | |
| 101 | std::sort(iv_expSigList.begin(), iv_expSigList.end()); |
| 102 | std::sort(givenSigList.begin(), givenSigList.end()); |
| 103 | |
Zane Shelley | 88eb202 | 2022-01-20 14:25:14 -0600 | [diff] [blame] | 104 | /* TODO: Currently used for debug. Eventually, we want this written to file. |
| 105 | for (const auto& s : givenSigList) |
| 106 | { |
| 107 | HEI_INF("Signature: %s 0x%04x %u %u %u", |
| 108 | (const char*)s.getChip().getChip(), s.getId(), s.getInstance(), |
| 109 | s.getBit(), s.getAttnType()); |
| 110 | } |
| 111 | */ |
| 112 | |
Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 113 | ASSERT_TRUE(std::equal(givenSigList.begin(), givenSigList.end(), |
| 114 | iv_expSigList.begin())); |
| 115 | |
| 116 | // The iteration is complete so we can flush the data. |
| 117 | flushIterationData(); |
| 118 | } |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 119 | |
| 120 | } // end namespace libhei |