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 | d6826e5 | 2020-11-10 17:39:00 -0600 | [diff] [blame] | 16 | {EXPLORER_11, "chip_data_explorer_11.cdb"}, |
| 17 | {EXPLORER_20, "chip_data_explorer_20.cdb"}, |
| 18 | {P10_10, "chip_data_p10_10.cdb"}, |
Zane Shelley | f8a726b | 2020-12-16 21:29:32 -0600 | [diff] [blame] | 19 | {P10_20, "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 | dae1af6 | 2021-01-14 11:00:46 -0600 | [diff] [blame] | 82 | /* TODO: Currently used for debug. Eventually, we want this and the |
| 83 | * signature list written to file. |
| 84 | for (const auto& e : isoData.getRegisterDump()) |
| 85 | { |
| 86 | HEI_INF("Chip: %s", (const char*)e.first.getChip()); |
| 87 | |
| 88 | for (const auto& r : e.second) |
| 89 | { |
| 90 | HEI_INF(" Reg: 0x%06x %d 0x%016" PRIx64, r.regId, r.regInst, |
| 91 | r.data->getFieldRight(0, 64)); |
| 92 | } |
| 93 | } |
| 94 | */ |
| 95 | |
Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 96 | // Get the list of signatures found in isolation. |
| 97 | std::vector<Signature> givenSigList = isoData.getSignatureList(); |
| 98 | |
| 99 | // Verify the expected list and given list are the same. |
| 100 | ASSERT_EQ(iv_expSigList.size(), givenSigList.size()); |
| 101 | |
| 102 | std::sort(iv_expSigList.begin(), iv_expSigList.end()); |
| 103 | std::sort(givenSigList.begin(), givenSigList.end()); |
| 104 | |
| 105 | ASSERT_TRUE(std::equal(givenSigList.begin(), givenSigList.end(), |
| 106 | iv_expSigList.begin())); |
| 107 | |
| 108 | // The iteration is complete so we can flush the data. |
| 109 | flushIterationData(); |
| 110 | } |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 111 | |
| 112 | } // end namespace libhei |