-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemu.cpp
More file actions
319 lines (295 loc) · 8.96 KB
/
emu.cpp
File metadata and controls
319 lines (295 loc) · 8.96 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*****************************************************************************
TITLE: Claims
AUTHOR: Hemant Kumar (2301CS20)
Declaration of Authorship
This emu.cpp file, is part of the miniproject assignment of course CS2102 at CSE Dept. IIT Patna
*****************************************************************************/
#include <bits/stdc++.h>
using namespace std;
class EmulatorState
{
private:
vector<uint32_t> program_memory;
unordered_map<string, pair<uint8_t, uint8_t>> instruction_set;
uint32_t accumulator;
uint32_t backup_reg;
uint32_t stack_ptr;
uint32_t program_counter;
size_t instruction_count;
struct MemoryOperation
{
uint32_t address;
uint32_t old_value;
MemoryOperation() : address(0), old_value(0) {} // Initialize to prevent warnings
} last_mem_op;
public:
EmulatorState() : accumulator(0),
backup_reg(0),
stack_ptr(0),
program_counter(0),
instruction_count(0)
{
initialize_instruction_set();
}
void initialize_instruction_set()
{
// Instruction format: name, opcode, operand_type
// operand_type: 0=none, 1=value, 2=offset
const vector<tuple<string, uint8_t, uint8_t>> instructions = {
make_tuple("ldc", 0x00, 1),
make_tuple("adc", 0x01, 1),
make_tuple("ldl", 0x02, 2),
make_tuple("stl", 0x03, 2),
make_tuple("ldnl", 0x04, 2),
make_tuple("stnl", 0x05, 2),
make_tuple("add", 0x06, 0),
make_tuple("sub", 0x07, 0),
make_tuple("shl", 0x08, 0),
make_tuple("shr", 0x09, 0),
make_tuple("adj", 0x0A, 1),
make_tuple("a2sp", 0x0B, 0),
make_tuple("sp2a", 0x0C, 0),
make_tuple("call", 0x0D, 2),
make_tuple("return", 0x0E, 0),
make_tuple("brz", 0x0F, 2),
make_tuple("brlz", 0x10, 2),
make_tuple("br", 0x11, 2),
make_tuple("HALT", 0x12, 0)};
for (const auto &inst : instructions)
{
instruction_set[get<0>(inst)] = make_pair(get<1>(inst), get<2>(inst));
}
}
bool load_program(const string &filename)
{
ifstream input(filename.c_str(), ios::binary); // Use c_str() for C++11
if (!input)
return false;
uint32_t instruction;
while (input.read(reinterpret_cast<char *>(&instruction), sizeof(uint32_t)))
{
program_memory.push_back(instruction);
}
return true;
}
void execute_instruction(uint32_t instruction, bool trace_mode)
{
uint8_t opcode = instruction & 0xFF;
int32_t operand = static_cast<int32_t>(instruction) >> 8;
instruction_count++;
switch (opcode)
{
case 0x00: // push_const
backup_reg = accumulator;
accumulator = static_cast<uint32_t>(operand);
break;
case 0x01: // add_const
accumulator = static_cast<uint32_t>(static_cast<int32_t>(accumulator) + operand);
break;
case 0x02: // load_local
backup_reg = accumulator;
accumulator = program_memory[stack_ptr + static_cast<uint32_t>(operand)];
last_mem_op.address = stack_ptr + static_cast<uint32_t>(operand);
last_mem_op.old_value = 0;
break;
case 0x03: // store_local
{
uint32_t addr = stack_ptr + static_cast<uint32_t>(operand);
last_mem_op.address = addr;
last_mem_op.old_value = program_memory[addr];
program_memory[addr] = accumulator;
accumulator = backup_reg;
}
break;
case 0x04: // load_indirect
{
uint32_t addr = accumulator + static_cast<uint32_t>(operand);
accumulator = program_memory[addr];
last_mem_op.address = addr;
last_mem_op.old_value = 0;
}
break;
case 0x05: // store_indirect
{
uint32_t addr = accumulator + static_cast<uint32_t>(operand);
last_mem_op.address = addr;
last_mem_op.old_value = program_memory[addr];
program_memory[addr] = backup_reg;
}
break;
case 0x06: // sum
accumulator = static_cast<uint32_t>(static_cast<int32_t>(accumulator) + static_cast<int32_t>(backup_reg));
break;
case 0x07: // difference
accumulator = static_cast<uint32_t>(static_cast<int32_t>(backup_reg) - static_cast<int32_t>(accumulator));
break;
case 0x08: // shift_left
accumulator = backup_reg << (accumulator & 0x1F); // Prevent undefined behavior
break;
case 0x09: // shift_right
accumulator = backup_reg >> (accumulator & 0x1F); // Prevent undefined behavior
break;
case 0x0A: // stack_adjust
stack_ptr = static_cast<uint32_t>(static_cast<int32_t>(stack_ptr) + operand);
break;
case 0x0B: // acc_to_stack
stack_ptr = accumulator;
accumulator = backup_reg;
break;
case 0x0C: // stack_to_acc
backup_reg = accumulator;
accumulator = stack_ptr;
break;
case 0x0D: // procedure_call
backup_reg = accumulator;
accumulator = program_counter;
program_counter = static_cast<uint32_t>(static_cast<int32_t>(program_counter) + operand);
break;
case 0x0E: // procedure_return
program_counter = accumulator;
accumulator = backup_reg;
break;
case 0x0F: // branch_zero
if (accumulator == 0)
{
program_counter = static_cast<uint32_t>(static_cast<int32_t>(program_counter) + operand);
}
break;
case 0x10: // branch_negative
if (static_cast<int32_t>(accumulator) < 0)
{
program_counter = static_cast<uint32_t>(static_cast<int32_t>(program_counter) + operand);
}
break;
case 0x11: // branch_always
program_counter = static_cast<uint32_t>(static_cast<int32_t>(program_counter) + operand);
break;
case 0x12: // terminate
program_counter = static_cast<uint32_t>(program_memory.size());
break;
default:
break;
}
if (trace_mode)
{
print_trace(opcode, operand);
}
}
string format_hex(uint32_t value) const
{
ostringstream ss;
ss << uppercase << setfill('0') << setw(8) << hex << value;
return ss.str();
}
void print_trace(uint8_t opcode, int32_t operand) const
{
cout << "PC=" << format_hex(program_counter - 1)
<< " SP=" << format_hex(stack_ptr)
<< " A=" << format_hex(accumulator)
<< " B=" << format_hex(backup_reg) << " ";
for (const auto &pair : instruction_set)
{
if (pair.second.first == opcode)
{
cout << pair.first;
if (pair.second.second > 0)
{
cout << " " << format_hex(static_cast<uint32_t>(operand));
}
break;
}
}
cout << endl;
}
void memory_dump() const
{
for (size_t i = 0; i < program_memory.size(); i += 4)
{
cout << format_hex(static_cast<uint32_t>(i)) << " ";
for (size_t j = i; j < min(program_memory.size(), i + 4); j++)
{
cout << format_hex(program_memory[j]) << " ";
}
cout << endl;
}
}
void execute(const string &mode)
{
bool trace = (mode == "-trace");
bool read_mode = (mode == "-read");
bool write_mode = (mode == "-write");
while (program_counter < program_memory.size())
{
uint32_t current = program_memory[program_counter++];
execute_instruction(current, trace);
uint8_t opcode = current & 0xFF;
if (read_mode && (opcode == 0x02 || opcode == 0x04))
{
cout << "Reading memory[" << format_hex(last_mem_op.address)
<< "] finds " << format_hex(accumulator) << endl;
}
if (write_mode && (opcode == 0x03 || opcode == 0x05))
{
cout << "Writing memory[" << format_hex(last_mem_op.address)
<< "] was " << format_hex(last_mem_op.old_value)
<< " now " << format_hex(program_memory[last_mem_op.address]) << endl;
}
if (opcode >= 0x12)
break;
}
}
void reset()
{
accumulator = 0;
backup_reg = 0;
stack_ptr = 0;
program_counter = 0;
}
size_t get_instruction_count() const
{
return instruction_count;
}
};
int main(int argc, char *argv[])
{
if (argc <= 2)
{
cout << "Usage: " << argv[0] << " [mode] filename.o\n"
<< "Modes:\n"
<< " -trace Show instruction trace\n"
<< " -read Show memory reads\n"
<< " -write Show memory writes\n"
<< " -before Show memory dump before execution\n"
<< " -after Show memory dump after execution\n"
<< " -wipe Reset registers before execution\n";
return 1;
}
EmulatorState emu;
string mode(argv[1]);
string filename(argv[2]);
if (!emu.load_program(filename))
{
cout << "Failed to load program file\n";
return 1;
}
if (mode == "-before")
{
cout << "memory dump before execution\n";
emu.memory_dump();
}
else if (mode == "-wipe")
{
emu.reset();
}
else
{
emu.execute(mode);
}
if (mode == "-after")
{
cout << "memory dump after execution\n";
emu.memory_dump();
}
cout << emu.get_instruction_count() << " instructions executed\n";
return 0;
}