-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhexsim.hpp
More file actions
376 lines (350 loc) · 11 KB
/
hexsim.hpp
File metadata and controls
376 lines (350 loc) · 11 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#ifndef HEX_SIM_HPP
#define HEX_SIM_HPP
#include <array>
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <exception>
#include <fmt/format.h>
#include <fstream>
#include <iostream>
#include <map>
#include "hex.hpp"
#include "hexsimio.hpp"
namespace hexsim {
class Processor {
// Constants.
static const size_t MEMORY_SIZE_WORDS = hex::MAX_MEMORY_SIZE_WORDS;
// State.
uint32_t pc;
uint32_t areg;
uint32_t breg;
uint32_t oreg;
uint32_t instr;
// Memory.
std::array<uint32_t, MEMORY_SIZE_WORDS> memory;
// IO.
hex::HexSimIO io;
// Control whether characters are sign extended into 32 bits. The behaviour of
// xhexb.x is for character values to be truncated on conversion to 32 bits.
// However, it is useful for testing to allow negative values.
bool truncateInputs;
// Logging.
std::ostream &out;
// Control.
bool running;
bool tracing;
int exitCode;
// State for tracing.
uint32_t lastPC;
size_t cycles;
size_t maxCycles;
hex::Instr instrEnum;
std::vector<std::pair<std::string, unsigned>> debugInfo;
std::map<std::string, unsigned> debugInfoMap;
/// Lookup a symbol name given the current PC.
const char *lookupSymbol() {
if (lastPC < debugInfo[0].second) {
return nullptr;
}
for (size_t i = 0; i < debugInfo.size(); i++) {
if (i == debugInfo.size() - 1 && lastPC >= debugInfo[i].second) {
return debugInfo[i].first.c_str();
}
if (lastPC >= debugInfo[i].second && lastPC < debugInfo[i + 1].second) {
return debugInfo[i].first.c_str();
}
}
return nullptr;
}
public:
Processor(std::istream &in, std::ostream &out, size_t maxCycles = 0)
: pc(0), areg(0), breg(0), oreg(0), io(in, out), truncateInputs(true),
out(out), running(true), tracing(false), lastPC(0), cycles(0),
maxCycles(maxCycles) {}
void setTracing(bool value) { tracing = value; }
void setTruncateInputs(bool value) { truncateInputs = value; }
void load(const char *filename, bool dumpContents = false) {
// Load the binary file.
std::streampos fileSize;
std::ifstream file(filename, std::ios::binary);
// Get length of file.
file.seekg(0, std::ios::end);
fileSize = file.tellg();
file.seekg(0, std::ios::beg);
// Check the file length matches.
unsigned remainingFileSize = static_cast<unsigned>(fileSize) - 4;
remainingFileSize =
(remainingFileSize + 3U) & ~3U; // Round up to multiple of 4.
unsigned programSize;
file.read(reinterpret_cast<char *>(&programSize), 4);
programSize <<= 2;
// Read the instructions into memory.
file.read(reinterpret_cast<char *>(memory.data()), programSize);
// Read debug data (if present).
if (remainingFileSize > programSize) {
// Strings.
uint32_t numStrings;
file.read(reinterpret_cast<char *>(&numStrings), sizeof(uint32_t));
// std::cout << std::to_string(numStrings) << " strings\n";
std::vector<std::string> strings;
for (size_t i = 0; i < numStrings; i++) {
char c = file.get();
std::string s;
while (c != '\0') {
s += c;
c = file.get();
}
strings.push_back(s);
}
// Symbols
uint32_t numSymbols;
file.read(reinterpret_cast<char *>(&numSymbols), sizeof(uint32_t));
// std::cout << std::to_string(numSymbols) << " symbols\n";
for (size_t i = 0; i < numSymbols; i++) {
uint32_t strIndex;
uint32_t byteOffset;
file.read(reinterpret_cast<char *>(&strIndex), sizeof(uint32_t));
file.read(reinterpret_cast<char *>(&byteOffset), sizeof(uint32_t));
// std::cout << "symbol " << strings[strIndex] << " " <<
// std::to_string(byteOffset) << "\n";
debugInfo.push_back(std::make_pair(strings[strIndex], byteOffset));
debugInfoMap[strings[strIndex]] = byteOffset;
}
}
// Print the contents of the binary.
if (dumpContents) {
out << "Read " << std::to_string(programSize) << " bytes\n";
for (size_t i = 0; i < (programSize / 4) + 1; i++) {
out << fmt::format("{:08d} {:08x}\n", i, memory[i]);
}
}
}
void traceSyscall() {
unsigned spWordIndex = memory[1];
switch (static_cast<hex::Syscall>(areg)) {
case hex::Syscall::EXIT:
out << fmt::format("exit {}\n", memory[spWordIndex + 2]);
break;
case hex::Syscall::WRITE:
out << fmt::format("write {} to simout({})\n", memory[spWordIndex + 2],
memory[spWordIndex + 3]);
break;
case hex::Syscall::READ:
out << fmt::format("read {} to mem[{:08x}]\n", memory[spWordIndex + 1],
(spWordIndex + 1));
break;
default:
break;
}
}
void trace(uint32_t instr, hex::Instr instrEnum) {
if (debugInfo.size()) {
auto symbolName = lookupSymbol();
std::string symbolInfo;
if (symbolName) {
auto symbolOffset = lastPC - debugInfoMap[symbolName];
symbolInfo = fmt::format("{}+{}", symbolName, symbolOffset);
}
out << fmt::format("{:<6d} {:<6d} {:<12} {:<4} {:<2d} ", cycles, lastPC,
symbolInfo, instrEnumToStr(instrEnum), (instr & 0xF));
} else {
out << fmt::format("{:<6d} {:<6d} {:<4} {:<2d} ", cycles, lastPC,
instrEnumToStr(instrEnum), (instr & 0xF));
}
switch (instrEnum) {
case hex::Instr::LDAM:
out << fmt::format("areg = mem[oreg ({:#08x})] ({})\n", oreg,
memory[oreg]);
break;
case hex::Instr::LDBM:
out << fmt::format("breg = mem[oreg ({:#08x})] ({})\n", oreg,
memory[oreg]);
break;
case hex::Instr::STAM:
out << fmt::format("mem[oreg ({:#08x})] = areg {}\n", oreg, areg);
break;
case hex::Instr::LDAC:
out << fmt::format("areg = oreg {}\n", oreg);
break;
case hex::Instr::LDBC:
out << fmt::format("breg = oreg {}\n", oreg);
break;
case hex::Instr::LDAP:
out << fmt::format("areg = pc ({}) + oreg ({}) {}\n", pc, oreg,
(pc + oreg));
break;
case hex::Instr::LDAI:
out << fmt::format("areg = mem[areg ({}) + oreg ({}) = {:#08x}] ({})\n",
areg, oreg, (areg + oreg), memory[areg + oreg]);
break;
case hex::Instr::LDBI:
out << fmt::format("breg = mem[breg ({}) + oreg ({}) = {:#08x}] ({})\n",
breg, oreg, (breg + oreg), memory[breg + oreg]);
break;
case hex::Instr::STAI:
out << fmt::format("mem[breg ({}) + oreg ({}) = {:#08x}] = areg ({})\n",
breg, oreg, (breg + oreg), areg);
break;
case hex::Instr::BR:
out << fmt::format("pc = pc + oreg ({}) ({:#08x})\n", oreg, (pc + oreg));
break;
case hex::Instr::BRZ:
out << fmt::format("pc = areg == zero ? pc + oreg ({}) ({:#08x}) : pc\n",
oreg, (pc + oreg));
break;
case hex::Instr::BRN:
out << fmt::format("pc = areg < zero ? pc + oreg ({}) ({:#08x}) : pc\n",
oreg, (pc + oreg));
break;
case hex::Instr::PFIX:
out << fmt::format("oreg = oreg ({}) << 4 ({:#08x})\n", oreg,
(oreg << 4));
break;
case hex::Instr::NFIX:
out << fmt::format("oreg = 0xFFFFFF00 | oreg ({}) << 4 ({:#08x})\n", oreg,
(0xFFFFFF00 | (oreg << 4)));
break;
case hex::Instr::OPR:
switch (static_cast<hex::OprInstr>(oreg)) {
case hex::OprInstr::BRB:
out << fmt::format("BRB pc = breg ({:#08x})\n", breg);
break;
case hex::OprInstr::ADD:
out << fmt::format("ADD areg = areg ({}) + breg ({}) ({})\n", areg,
breg, (areg + breg));
break;
case hex::OprInstr::SUB:
out << fmt::format("SUB areg = areg ({}) - breg ({}) ({})\n", areg,
breg, (areg - breg));
break;
case hex::OprInstr::SVC:
break;
};
break;
}
}
void syscall() {
unsigned spWordIndex = memory[1];
switch (static_cast<hex::Syscall>(areg)) {
case hex::Syscall::EXIT:
exitCode = memory[spWordIndex + 2];
running = false;
break;
case hex::Syscall::WRITE:
io.output(memory[spWordIndex + 2], memory[spWordIndex + 3]);
break;
case hex::Syscall::READ: {
auto value = io.input(memory[spWordIndex + 2]);
memory[spWordIndex + 1] = truncateInputs ? value & 0xFF : value;
break;
}
default:
throw std::runtime_error("invalid syscall: " + std::to_string(areg));
}
}
int run() {
while (running && (maxCycles > 0 ? cycles <= maxCycles : true)) {
instr = (memory[pc >> 2] >> ((pc & 0x3) << 3)) & 0xFF;
lastPC = pc;
pc = pc + 1;
oreg = oreg | (instr & 0xF);
instrEnum = static_cast<hex::Instr>((instr >> 4) & 0xF);
if (tracing) {
trace(instr, instrEnum);
}
switch (instrEnum) {
case hex::Instr::LDAM:
areg = memory[oreg];
oreg = 0;
break;
case hex::Instr::LDBM:
breg = memory[oreg];
oreg = 0;
break;
case hex::Instr::STAM:
memory[oreg] = areg;
oreg = 0;
break;
case hex::Instr::LDAC:
areg = oreg;
oreg = 0;
break;
case hex::Instr::LDBC:
breg = oreg;
oreg = 0;
break;
case hex::Instr::LDAP:
areg = pc + oreg;
oreg = 0;
break;
case hex::Instr::LDAI:
areg = memory[areg + oreg];
oreg = 0;
break;
case hex::Instr::LDBI:
breg = memory[breg + oreg];
oreg = 0;
break;
case hex::Instr::STAI:
memory[breg + oreg] = areg;
oreg = 0;
break;
case hex::Instr::BR:
pc = pc + oreg;
oreg = 0;
break;
case hex::Instr::BRZ:
if (areg == 0) {
pc = pc + oreg;
}
oreg = 0;
break;
case hex::Instr::BRN:
if ((int)areg < 0) {
pc = pc + oreg;
}
oreg = 0;
break;
case hex::Instr::PFIX:
oreg = oreg << 4;
break;
case hex::Instr::NFIX:
oreg = 0xFFFFFF00 | (oreg << 4);
break;
case hex::Instr::OPR:
switch (static_cast<hex::OprInstr>(oreg)) {
case hex::OprInstr::BRB:
pc = breg;
oreg = 0;
break;
case hex::OprInstr::ADD:
areg = areg + breg;
oreg = 0;
break;
case hex::OprInstr::SUB:
areg = areg - breg;
oreg = 0;
break;
case hex::OprInstr::SVC:
syscall();
if (tracing) {
traceSyscall();
}
break;
default:
throw std::runtime_error("invalid OPR: " + std::to_string(oreg));
};
oreg = 0;
break;
default:
throw std::runtime_error("invalid instruction");
}
cycles++;
}
return exitCode;
}
};
} // End namespace hexsim
#endif // HEX_SIM_HPP