Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 1 | #include "simulator.hpp" |
| 2 | |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 3 | #include <fstream> // std::ifstream |
| 4 | |
| 5 | namespace libhei |
| 6 | { |
| 7 | |
| 8 | //------------------------------------------------------------------------------ |
| 9 | |
| 10 | // Paths are relative from the build/ directory |
Zane Shelley | 8c093d8 | 2020-05-04 22:06:52 -0500 | [diff] [blame] | 11 | const std::map<SimulatorData::SimChipType, const char*> |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 12 | SimulatorData::cv_chipPath = { |
Zane Shelley | aadf3bf | 2020-04-30 21:25:29 -0500 | [diff] [blame] | 13 | {SAMPLE, "../test/simulator/sample_data/sample.cdb"}, |
Zane Shelley | d6826e5 | 2020-11-10 17:39:00 -0600 | [diff] [blame] | 14 | {EXPLORER_11, "chip_data_explorer_11.cdb"}, |
| 15 | {EXPLORER_20, "chip_data_explorer_20.cdb"}, |
| 16 | {P10_10, "chip_data_p10_10.cdb"}, |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 17 | }; |
Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 18 | |
| 19 | //------------------------------------------------------------------------------ |
| 20 | |
| 21 | void SimulatorData::addChip(const Chip& i_chip) |
| 22 | { |
| 23 | // First check if this entry already exists. |
Zane Shelley | 8c093d8 | 2020-05-04 22:06:52 -0500 | [diff] [blame] | 24 | auto chip_itr = std::find(iv_chipList.begin(), iv_chipList.end(), i_chip); |
| 25 | ASSERT_EQ(iv_chipList.end(), chip_itr); |
Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 26 | |
| 27 | // Add the new entry. |
| 28 | iv_chipList.push_back(i_chip); |
| 29 | |
Zane Shelley | 8c093d8 | 2020-05-04 22:06:52 -0500 | [diff] [blame] | 30 | // Check if this chip type has been initialized. |
| 31 | ChipType_t chipType = i_chip.getType(); |
| 32 | auto type_itr = std::find(iv_typeList.begin(), iv_typeList.end(), chipType); |
| 33 | if (iv_typeList.end() != type_itr) |
| 34 | { |
| 35 | return; // No need to continue. |
| 36 | } |
| 37 | |
| 38 | // Add the new entry. |
| 39 | iv_typeList.push_back(chipType); |
| 40 | |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 41 | // Look for the file path |
Zane Shelley | 8c093d8 | 2020-05-04 22:06:52 -0500 | [diff] [blame] | 42 | auto itr2 = cv_chipPath.find(static_cast<SimChipType>(chipType)); |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 43 | ASSERT_NE(cv_chipPath.end(), itr2); |
| 44 | const char* path = itr2->second; |
Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 45 | |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 46 | // Open the Chip Data File |
| 47 | std::ifstream cdf{path, std::ifstream::binary}; |
| 48 | ASSERT_TRUE(cdf.good()); |
| 49 | |
| 50 | // Get the length of file |
| 51 | cdf.seekg(0, cdf.end); |
| 52 | size_t sz_buffer = cdf.tellg(); |
| 53 | cdf.seekg(0, cdf.beg); |
| 54 | |
| 55 | // Allocate memory |
| 56 | char* buffer = new char[sz_buffer]; |
| 57 | |
| 58 | // Read data as a block |
| 59 | cdf.read(buffer, sz_buffer); |
| 60 | |
| 61 | // Close the Chip Data File |
| 62 | cdf.close(); |
| 63 | |
| 64 | // Initilize the chip with this Chip Data File. |
Zane Shelley | dd109cc | 2020-04-30 21:41:41 -0500 | [diff] [blame] | 65 | initialize(buffer, sz_buffer); |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 66 | |
| 67 | // Clean up the buffer |
| 68 | delete[] buffer; |
Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | //------------------------------------------------------------------------------ |
| 72 | |
| 73 | void SimulatorData::endIteration() |
| 74 | { |
| 75 | // Start by calling libhei::isolate(). |
| 76 | IsolationData isoData{}; |
Zane Shelley | 229c155 | 2020-05-04 22:44:15 -0500 | [diff] [blame] | 77 | isolate(iv_chipList, isoData); |
Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 78 | |
| 79 | // Get the list of signatures found in isolation. |
| 80 | std::vector<Signature> givenSigList = isoData.getSignatureList(); |
| 81 | |
| 82 | // Verify the expected list and given list are the same. |
| 83 | ASSERT_EQ(iv_expSigList.size(), givenSigList.size()); |
| 84 | |
| 85 | std::sort(iv_expSigList.begin(), iv_expSigList.end()); |
| 86 | std::sort(givenSigList.begin(), givenSigList.end()); |
| 87 | |
| 88 | ASSERT_TRUE(std::equal(givenSigList.begin(), givenSigList.end(), |
| 89 | iv_expSigList.begin())); |
| 90 | |
| 91 | // The iteration is complete so we can flush the data. |
| 92 | flushIterationData(); |
| 93 | } |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 94 | |
| 95 | } // end namespace libhei |