blob: e9bd2aa416edd08b812f6c3b8a9bdbd5be4cbc7f [file] [log] [blame]
Zane Shelley11b89942019-11-07 11:07:28 -06001#include "simulator.hpp"
2
Zane Shelley1be4c3c2020-04-17 15:55:07 -05003#include <fstream> // std::ifstream
4
5namespace libhei
6{
7
8//------------------------------------------------------------------------------
9
10// Paths are relative from the build/ directory
Zane Shelley8c093d82020-05-04 22:06:52 -050011const std::map<SimulatorData::SimChipType, const char*>
Zane Shelley1be4c3c2020-04-17 15:55:07 -050012 SimulatorData::cv_chipPath = {
Zane Shelleyaadf3bf2020-04-30 21:25:29 -050013 {SAMPLE, "../test/simulator/sample_data/sample.cdb"},
Zane Shelleyd6826e52020-11-10 17:39:00 -060014 {EXPLORER_11, "chip_data_explorer_11.cdb"},
15 {EXPLORER_20, "chip_data_explorer_20.cdb"},
16 {P10_10, "chip_data_p10_10.cdb"},
Zane Shelleyf8a726b2020-12-16 21:29:32 -060017 {P10_20, "chip_data_p10_20.cdb"},
Zane Shelley1be4c3c2020-04-17 15:55:07 -050018};
Zane Shelley11b89942019-11-07 11:07:28 -060019
20//------------------------------------------------------------------------------
21
22void SimulatorData::addChip(const Chip& i_chip)
23{
24 // First check if this entry already exists.
Zane Shelley8c093d82020-05-04 22:06:52 -050025 auto chip_itr = std::find(iv_chipList.begin(), iv_chipList.end(), i_chip);
26 ASSERT_EQ(iv_chipList.end(), chip_itr);
Zane Shelley11b89942019-11-07 11:07:28 -060027
28 // Add the new entry.
29 iv_chipList.push_back(i_chip);
30
Zane Shelley8c093d82020-05-04 22:06:52 -050031 // 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 Shelley1be4c3c2020-04-17 15:55:07 -050042 // Look for the file path
Zane Shelley8c093d82020-05-04 22:06:52 -050043 auto itr2 = cv_chipPath.find(static_cast<SimChipType>(chipType));
Zane Shelley1be4c3c2020-04-17 15:55:07 -050044 ASSERT_NE(cv_chipPath.end(), itr2);
45 const char* path = itr2->second;
Zane Shelley11b89942019-11-07 11:07:28 -060046
Zane Shelley1be4c3c2020-04-17 15:55:07 -050047 // 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 Shelleydd109cc2020-04-30 21:41:41 -050066 initialize(buffer, sz_buffer);
Zane Shelley1be4c3c2020-04-17 15:55:07 -050067
68 // Clean up the buffer
69 delete[] buffer;
Zane Shelley11b89942019-11-07 11:07:28 -060070}
71
72//------------------------------------------------------------------------------
73
74void SimulatorData::endIteration()
75{
76 // Start by calling libhei::isolate().
77 IsolationData isoData{};
Zane Shelley229c1552020-05-04 22:44:15 -050078 isolate(iv_chipList, isoData);
Zane Shelley11b89942019-11-07 11:07:28 -060079
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 Shelley1be4c3c2020-04-17 15:55:07 -050095
96} // end namespace libhei