blob: e994eba1d7358341af7fe87f557c4e4c0950bc36 [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 Shelley1be4c3c2020-04-17 15:55:07 -050017};
Zane Shelley11b89942019-11-07 11:07:28 -060018
19//------------------------------------------------------------------------------
20
21void SimulatorData::addChip(const Chip& i_chip)
22{
23 // First check if this entry already exists.
Zane Shelley8c093d82020-05-04 22:06:52 -050024 auto chip_itr = std::find(iv_chipList.begin(), iv_chipList.end(), i_chip);
25 ASSERT_EQ(iv_chipList.end(), chip_itr);
Zane Shelley11b89942019-11-07 11:07:28 -060026
27 // Add the new entry.
28 iv_chipList.push_back(i_chip);
29
Zane Shelley8c093d82020-05-04 22:06:52 -050030 // 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 Shelley1be4c3c2020-04-17 15:55:07 -050041 // Look for the file path
Zane Shelley8c093d82020-05-04 22:06:52 -050042 auto itr2 = cv_chipPath.find(static_cast<SimChipType>(chipType));
Zane Shelley1be4c3c2020-04-17 15:55:07 -050043 ASSERT_NE(cv_chipPath.end(), itr2);
44 const char* path = itr2->second;
Zane Shelley11b89942019-11-07 11:07:28 -060045
Zane Shelley1be4c3c2020-04-17 15:55:07 -050046 // 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 Shelleydd109cc2020-04-30 21:41:41 -050065 initialize(buffer, sz_buffer);
Zane Shelley1be4c3c2020-04-17 15:55:07 -050066
67 // Clean up the buffer
68 delete[] buffer;
Zane Shelley11b89942019-11-07 11:07:28 -060069}
70
71//------------------------------------------------------------------------------
72
73void SimulatorData::endIteration()
74{
75 // Start by calling libhei::isolate().
76 IsolationData isoData{};
Zane Shelley229c1552020-05-04 22:44:15 -050077 isolate(iv_chipList, isoData);
Zane Shelley11b89942019-11-07 11:07:28 -060078
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 Shelley1be4c3c2020-04-17 15:55:07 -050094
95} // end namespace libhei