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