blob: b95027d74ac408f32cf9fca1ec2242a63b990edd [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 Shelley1be4c3c2020-04-17 15:55:07 -050014};
Zane Shelley11b89942019-11-07 11:07:28 -060015
16//------------------------------------------------------------------------------
17
18void SimulatorData::addChip(const Chip& i_chip)
19{
20 // First check if this entry already exists.
Zane Shelley8c093d82020-05-04 22:06:52 -050021 auto chip_itr = std::find(iv_chipList.begin(), iv_chipList.end(), i_chip);
22 ASSERT_EQ(iv_chipList.end(), chip_itr);
Zane Shelley11b89942019-11-07 11:07:28 -060023
24 // Add the new entry.
25 iv_chipList.push_back(i_chip);
26
Zane Shelley8c093d82020-05-04 22:06:52 -050027 // Check if this chip type has been initialized.
28 ChipType_t chipType = i_chip.getType();
29 auto type_itr = std::find(iv_typeList.begin(), iv_typeList.end(), chipType);
30 if (iv_typeList.end() != type_itr)
31 {
32 return; // No need to continue.
33 }
34
35 // Add the new entry.
36 iv_typeList.push_back(chipType);
37
Zane Shelley1be4c3c2020-04-17 15:55:07 -050038 // Look for the file path
Zane Shelley8c093d82020-05-04 22:06:52 -050039 auto itr2 = cv_chipPath.find(static_cast<SimChipType>(chipType));
Zane Shelley1be4c3c2020-04-17 15:55:07 -050040 ASSERT_NE(cv_chipPath.end(), itr2);
41 const char* path = itr2->second;
Zane Shelley11b89942019-11-07 11:07:28 -060042
Zane Shelley1be4c3c2020-04-17 15:55:07 -050043 // Open the Chip Data File
44 std::ifstream cdf{path, std::ifstream::binary};
45 ASSERT_TRUE(cdf.good());
46
47 // Get the length of file
48 cdf.seekg(0, cdf.end);
49 size_t sz_buffer = cdf.tellg();
50 cdf.seekg(0, cdf.beg);
51
52 // Allocate memory
53 char* buffer = new char[sz_buffer];
54
55 // Read data as a block
56 cdf.read(buffer, sz_buffer);
57
58 // Close the Chip Data File
59 cdf.close();
60
61 // Initilize the chip with this Chip Data File.
Zane Shelleydd109cc2020-04-30 21:41:41 -050062 initialize(buffer, sz_buffer);
Zane Shelley1be4c3c2020-04-17 15:55:07 -050063
64 // Clean up the buffer
65 delete[] buffer;
Zane Shelley11b89942019-11-07 11:07:28 -060066}
67
68//------------------------------------------------------------------------------
69
70void SimulatorData::endIteration()
71{
72 // Start by calling libhei::isolate().
73 IsolationData isoData{};
Zane Shelley229c1552020-05-04 22:44:15 -050074 isolate(iv_chipList, isoData);
Zane Shelley11b89942019-11-07 11:07:28 -060075
76 // Get the list of signatures found in isolation.
77 std::vector<Signature> givenSigList = isoData.getSignatureList();
78
79 // Verify the expected list and given list are the same.
80 ASSERT_EQ(iv_expSigList.size(), givenSigList.size());
81
82 std::sort(iv_expSigList.begin(), iv_expSigList.end());
83 std::sort(givenSigList.begin(), givenSigList.end());
84
85 ASSERT_TRUE(std::equal(givenSigList.begin(), givenSigList.end(),
86 iv_expSigList.begin()));
87
88 // The iteration is complete so we can flush the data.
89 flushIterationData();
90}
Zane Shelley1be4c3c2020-04-17 15:55:07 -050091
92} // end namespace libhei