-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisa_simulator.cpp
More file actions
102 lines (90 loc) · 2.91 KB
/
Copy pathisa_simulator.cpp
File metadata and controls
102 lines (90 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <vector>
#include <iomanip>
struct ISA {
std::string raw_hex;
uint8_t opcode;
uint8_t pointer;
bool read;
bool write;
uint16_t row_addr;
static ISA decode(const std::string& hex) {
ISA i;
i.raw_hex = hex;
uint32_t instr = std::stoul(hex, nullptr, 16);
i.opcode = (instr >> 22) & 0x3;
i.pointer = (instr >> 16) & 0x3F;
i.read = (instr >> 15) & 0x1;
i.write = (instr >> 14) & 0x1;
i.row_addr = (instr >> 5) & 0x1FF;
return i;
}
std::string describe() const {
std::stringstream ss;
ss << raw_hex << " ";
switch (opcode) {
case 0b00:
if (read) ss << "READ";
else if (write) ss << "WRITE";
else ss << "NOP";
ss << " row=" << row_addr << " PE=" << int(pointer);
break;
case 0b01: ss << "PROG PE=" << int(pointer); break;
case 0b10: ss << "EXE PE=" << int(pointer); break;
case 0b11: ss << "END PE=" << int(pointer); break;
}
return ss.str();
}
};
int main() {
std::ifstream isaFile("program_parallel.isa");
std::ifstream memFile("memory_init.txt");
std::ofstream report("report.txt");
std::map<int, int> memory;
std::map<uint8_t, std::vector<ISA>> perPE;
std::string line;
while (std::getline(memFile, line)) {
std::istringstream iss(line);
int addr, val;
iss >> addr >> val;
memory[addr] = val;
}
while (std::getline(isaFile, line)) {
if (line.empty()) continue;
ISA instr = ISA::decode(line);
perPE[instr.pointer].push_back(instr);
}
report << "==== Parallel ISA Simulation ====\n\n";
for (auto &[pe, program] : perPE) {
report << "[PE " << int(pe) << "]\n";
int regA = 0, regB = 0, regC = 0;
int regIndex = 0;
for (const auto &instr : program) {
report << instr.describe() << "\n";
if (instr.opcode == 0b00 && instr.read) {
int val = memory[instr.row_addr];
if (regIndex == 0) regA = val;
else if (regIndex == 1) regB = val;
else regC = val;
regIndex = (regIndex + 1) % 3;
} else if (instr.opcode == 0b10) {
regC += regA * regB;
} else if (instr.opcode == 0b00 && instr.write) {
memory[instr.row_addr] = regC;
regA = regB = regC = 0;
regIndex = 0;
}
}
report << "\n";
}
report << "\n==== Final Memory (C matrix only) ====\n";
for (auto &[addr, val] : memory) {
if (addr >= 300)
report << "Row " << addr << " = " << val << "\n";
}
std::cout << "Simulation complete. See report.txt for results.\n";
return 0;
}