blob: 32eb31b1a2927797d8c779b7bc342fd958dccb67 [file] [log] [blame]
Zane Shelley11b89942019-11-07 11:07:28 -06001#include "simulator.hpp"
2
Zane Shelleydae1af62021-01-14 11:00:46 -06003#include <util/hei_includes.hpp>
4
Zane Shelley1be4c3c2020-04-17 15:55:07 -05005#include <fstream> // std::ifstream
6
7namespace libhei
8{
9
10//------------------------------------------------------------------------------
11
12// Paths are relative from the build/ directory
Zane Shelley8c093d82020-05-04 22:06:52 -050013const std::map<SimulatorData::SimChipType, const char*>
Zane Shelley1be4c3c2020-04-17 15:55:07 -050014 SimulatorData::cv_chipPath = {
Zane Shelleyaadf3bf2020-04-30 21:25:29 -050015 {SAMPLE, "../test/simulator/sample_data/sample.cdb"},
Zane Shelleyd6826e52020-11-10 17:39:00 -060016 {EXPLORER_11, "chip_data_explorer_11.cdb"},
17 {EXPLORER_20, "chip_data_explorer_20.cdb"},
18 {P10_10, "chip_data_p10_10.cdb"},
Zane Shelleyf8a726b2020-12-16 21:29:32 -060019 {P10_20, "chip_data_p10_20.cdb"},
Zane Shelley1be4c3c2020-04-17 15:55:07 -050020};
Zane Shelley11b89942019-11-07 11:07:28 -060021
22//------------------------------------------------------------------------------
23
24void SimulatorData::addChip(const Chip& i_chip)
25{
26 // First check if this entry already exists.
Zane Shelley8c093d82020-05-04 22:06:52 -050027 auto chip_itr = std::find(iv_chipList.begin(), iv_chipList.end(), i_chip);
28 ASSERT_EQ(iv_chipList.end(), chip_itr);
Zane Shelley11b89942019-11-07 11:07:28 -060029
30 // Add the new entry.
31 iv_chipList.push_back(i_chip);
32
Zane Shelley8c093d82020-05-04 22:06:52 -050033 // Check if this chip type has been initialized.
34 ChipType_t chipType = i_chip.getType();
35 auto type_itr = std::find(iv_typeList.begin(), iv_typeList.end(), chipType);
36 if (iv_typeList.end() != type_itr)
37 {
38 return; // No need to continue.
39 }
40
41 // Add the new entry.
42 iv_typeList.push_back(chipType);
43
Zane Shelley1be4c3c2020-04-17 15:55:07 -050044 // Look for the file path
Zane Shelley8c093d82020-05-04 22:06:52 -050045 auto itr2 = cv_chipPath.find(static_cast<SimChipType>(chipType));
Zane Shelley1be4c3c2020-04-17 15:55:07 -050046 ASSERT_NE(cv_chipPath.end(), itr2);
47 const char* path = itr2->second;
Zane Shelley11b89942019-11-07 11:07:28 -060048
Zane Shelley1be4c3c2020-04-17 15:55:07 -050049 // Open the Chip Data File
50 std::ifstream cdf{path, std::ifstream::binary};
51 ASSERT_TRUE(cdf.good());
52
53 // Get the length of file
54 cdf.seekg(0, cdf.end);
55 size_t sz_buffer = cdf.tellg();
56 cdf.seekg(0, cdf.beg);
57
58 // Allocate memory
59 char* buffer = new char[sz_buffer];
60
61 // Read data as a block
62 cdf.read(buffer, sz_buffer);
63
64 // Close the Chip Data File
65 cdf.close();
66
67 // Initilize the chip with this Chip Data File.
Zane Shelleydd109cc2020-04-30 21:41:41 -050068 initialize(buffer, sz_buffer);
Zane Shelley1be4c3c2020-04-17 15:55:07 -050069
70 // Clean up the buffer
71 delete[] buffer;
Zane Shelley11b89942019-11-07 11:07:28 -060072}
73
74//------------------------------------------------------------------------------
75
76void SimulatorData::endIteration()
77{
78 // Start by calling libhei::isolate().
79 IsolationData isoData{};
Zane Shelley229c1552020-05-04 22:44:15 -050080 isolate(iv_chipList, isoData);
Zane Shelley11b89942019-11-07 11:07:28 -060081
Zane Shelleydae1af62021-01-14 11:00:46 -060082 /* TODO: Currently used for debug. Eventually, we want this and the
83 * signature list written to file.
84 for (const auto& e : isoData.getRegisterDump())
85 {
86 HEI_INF("Chip: %s", (const char*)e.first.getChip());
87
88 for (const auto& r : e.second)
89 {
90 HEI_INF(" Reg: 0x%06x %d 0x%016" PRIx64, r.regId, r.regInst,
91 r.data->getFieldRight(0, 64));
92 }
93 }
94 */
95
Zane Shelley11b89942019-11-07 11:07:28 -060096 // Get the list of signatures found in isolation.
97 std::vector<Signature> givenSigList = isoData.getSignatureList();
98
99 // Verify the expected list and given list are the same.
100 ASSERT_EQ(iv_expSigList.size(), givenSigList.size());
101
102 std::sort(iv_expSigList.begin(), iv_expSigList.end());
103 std::sort(givenSigList.begin(), givenSigList.end());
104
105 ASSERT_TRUE(std::equal(givenSigList.begin(), givenSigList.end(),
106 iv_expSigList.begin()));
107
108 // The iteration is complete so we can flush the data.
109 flushIterationData();
110}
Zane Shelley1be4c3c2020-04-17 15:55:07 -0500111
112} // end namespace libhei