forked from uva-cs/pdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ibcm-simulator.cpp
479 lines (457 loc) · 16.8 KB
/
ibcm-simulator.cpp
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/* A program that will simulate an IBCM program, and it has a number
* of command line switches that control its execution. This program
* is useful for automating the execution of a number of IBCM programs
* without having to load each one into the web interface. It will
* also print out traces of the entire program execution, if desired.
* Run with the `-help` flag to see the full list of options.
*/
#ifndef IS_LITTLE_ENDIAN
#ifndef IS_BIG_ENDIAN
#error Must define IS_LITTLE_ENDIAN or IS_BIG_ENDIAN via the `-D` flag to the compiler
#define IS_LITTLE_ENDIAN
#endif
#endif
#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <stdint.h>
using namespace std;
int compState = 0;
int simState = 0;
int printState = 0;
int printStats = 0;
int dumpState = 0;
int verbose = 0;
unsigned int numinst = 0;
int printTicks = 0;
unsigned int maxticks = 0;
string outputFile = "ibcm.bin";
string inputFile;
uint16_t mem[4096];
void printHelp(char *name);
void checkEndian(void);
void compileCode (string infilename, string outfilename);
void simulateIBCM ();
void readBinaryIBCMFile (string infilename);
void dumpIBCMFile ();
char* decodeIBCMInstruction(uint16_t inst);
int main(int argc, char *argv[]) {
int i = 1;
checkEndian();
assert (sizeof(short) == 2);
ios::sync_with_stdio(); // Synchronize C++ and C I/O subsystems!
while (i < argc) {
if (strcmp(argv[i], "-comp")==0) {
compState=1;
inputFile=argv[i+1]; // TODO: check that this doesn't go past the array bounds
} else if (strcmp(argv[i], "-sim")==0) {
simState=1;
inputFile=argv[i+1]; // TODO: check that this doesn't go past the array bounds
} else if (strcmp(argv[i], "-dump")==0) {
dumpState=1;
inputFile=argv[i+1]; // TODO: check that this doesn't go past the array bounds
} else if (strcmp(argv[i], "-o")==0) {
outputFile=argv[i+1]; // TODO: check that this doesn't go past the array bounds
i++;
} else if (strcmp(argv[i], "-verbose")==0) {
verbose++;
} else if (strcmp(argv[i], "-help")==0) {
printHelp(argv[0]);
} else if (strcmp(argv[i], "-print")==0) {
printState = 1;
} else if (strcmp(argv[i], "-stats")==0) {
printStats = 1;
} else if (strcmp(argv[i], "-maxticks")==0) {
maxticks = atoi(argv[++i]); // TODO: check the format of the int
}
i++;
}
//Check for proper combination of params
if (simState==0 && compState==0 && inputFile=="") {
//cerr << "No option specified..." << endl;
printHelp(argv[0]);
return 1;
}
if ( compState )
compileCode (inputFile, outputFile);
if ( simState ) {
readBinaryIBCMFile (inputFile);
if ( printState ) {
dumpIBCMFile ();
cout << endl << endl;
}
simulateIBCM();
if ( printStats )
cout << "Total of " << numinst << " instructions executed." << endl;
if ( printState ) {
cout << endl << endl;
dumpIBCMFile ();
}
}
if ( dumpState ) {
readBinaryIBCMFile (inputFile);
dumpIBCMFile ();
}
return 0;
}
void printHelp(char *name) {
static bool helpPrinted; // Static values are initialized to 0 or 0-equivalent
if(helpPrinted) return;
helpPrinted = true;
cout << "Usage: " << name << " [option] ..." << endl;
cout << "Options:" << endl;
cout << "\t[-comp <inputfile>]\tSignals the program to compile the IBCM file speicfied by <inputfile>" << endl;
cout << "\t[-sim <inputfile>]\tSignals the program to simulate the IBCM program specified by <inputfile>" << endl;
cout << "\t[-o <outfile>]\t\tSpecify the name of the outfile produced by compiling <inputfile>" << endl
<< "\t\t\t\tThe defaut value for <outfile> is \"ibcm.bin.\"" << endl;
cout << "\t[-verbose]\t\tPrints useful debugging information while compiling and emulating" << endl
<< "\t\t\t\the specified IBCM program.";
cout << " Specify twice for more output." << endl;
cout << "\t[-dump <inputfile>]\tOutputs a binary IBCM file in text format.";
cout << " Also specify -verbose for decompilation." << endl;
cout << "\t[-print]\t\tPrints a listing of the program before and after the simulation." << endl;
cout << "\t[-maxticks <n>]\t\tSet the maximum number of ticks." << endl;
cout << "\t[-stats]\t\tPrints stats of the executed program, including the number of ticks." << endl;
cout << "\t[-help]\t\t\tPrints this help message." << endl;
}
void checkEndian(void) {
static int firsttime = 1;
if (firsttime) {
union {
char charword[4];
unsigned int intword;
} check;
check.charword[0] = 1;
check.charword[1] = 2;
check.charword[2] = 3;
check.charword[3] = 4;
#ifdef IS_BIG_ENDIAN
if (check.intword != 0x01020304) { /* big */
cerr << "ERROR: Host machine is not Big Endian.\nExiting." << endl;
exit(205);
}
#else
#ifdef IS_LITTLE_ENDIAN
if (check.intword != 0x04030201) { /* little */
cerr << "ERROR: Host machine is not Little Endian.\nExiting." << endl;
exit(206);
}
#else
#error ERROR: must define either IS_LITTLE_ENDIAN or IS_BIG_ENDIAN
#endif // IS_LITTLE_ENDIAN
#endif // IS_BIG_ENDIAN
firsttime = 0;
}
}
bool ishexdigit (int c) {
if ( isdigit(c) )
return true;
if ( (c >= 'a') && (c <= 'f') )
return true;
if ( (c >= 'A') && (c <= 'F') )
return true;
return false;
}
void dumpIBCMFile () {
// find last command to not print
int end;
for ( end = 4095; end >= 0; end-- ) {
if ( mem[end] != 0 )
break;
}
for ( int i = 0; i <= end; i++ ) {
printf ("%.4x\tpc = %.4x\t%s\n", mem[i], i,
decodeIBCMInstruction(mem[i]));
}
}
void compileCode (string infilename, string outfilename) {
string line;
int linenum = -1, outputlines = 0;
// open input file
ifstream infile(infilename.c_str());
if ( !infile.is_open() ) {
cerr << "Error: unable to open input file." << endl;
exit(200);
}
// open output file
ofstream outfile(outfilename.c_str());
if ( !outfile.is_open() ) {
cerr << "Error: unable to open output file." << endl;
exit(201);
}
// read input file, write to output file
while (!infile.eof()) {
linenum++;
getline (infile,line);
// skip blank lines
if ( line.size() == 0 )
continue;
if ( (line[0] == '/') && (line[1] == '/') )
continue;
// sanity check
if ( !ishexdigit(line[0]) || !ishexdigit(line[1]) ||
!ishexdigit(line[2]) || !ishexdigit(line[3]) ) {
cerr << "Error on line " << linenum << ": invalid hex digits" << endl;
}
line[4] = 0;
int hex;
sscanf (line.c_str(), "%x", &hex);
#ifdef IS_LITTLE_ENDIAN
char g = hex % 256;
outfile.write(&g,1);
g = hex / 256;
outfile.write(&g,1);
#else
#error Not yet working for non-little-endian machines
#endif
//cout << line[0] << line[1] << line[2] << line[3] << endl;
outputlines++;
}
infile.close();
// fill remaining space with 0's
for (; outputlines < 4096; outputlines++ ) {
char x = 0;
//cout << "0000" << endl;
outfile.write (&x,1);
outfile.write (&x,1);
}
outfile.close();
}
void readBinaryIBCMFile (string infilename) {
int cmdnum = 0;
// open input file
ifstream infile(infilename.c_str());
if ( !infile.is_open() ) {
cerr << "Error: unable to open input file." << endl;
exit(202);
}
// load up file into array
for ( int i = 0; i < 4096; i++ )
mem[i] = 0;
char buf[2];
while (!infile.eof()) {
infile.read(buf,2);
#ifdef IS_LITTLE_ENDIAN
mem[cmdnum] = (buf[1] & 0x000000ff) * 256 + (buf[0] & 0x000000ff);
#else
#error Not yet working for non-little-endian machines
#endif
cmdnum++;
}
//for ( int i = 0; i < 4096; i++ ) printf ("%.4x\n", mem[i] & 0x0000ffff);
}
union ibcm_instruction {
uint16_t buf;
#ifdef IS_BIG_ENDIAN //The IBCM is big endian
struct {
unsigned char high, low;
} bytes;
struct {
unsigned int op:4, unused:12;
} halt;
struct {
unsigned int op:4, ioopt:2, unused:10;
} io;
struct {
unsigned int op:4, shiftop:2, unused:5, shiftcount:5;
} shifts;
struct {
unsigned int op:4, address:12;
} others;
#else
#ifdef IS_LITTLE_ENDIAN //The IBCM is little endian
struct {
unsigned char low, high;
} bytes;
struct {
unsigned int unused:12, op:4;
} halt;
struct {
unsigned int unused:10, ioopt:2, op:4;
} io;
struct {
unsigned int shiftcount:5, unused:5, shiftop:2, op:4;
} shifts;
struct {
unsigned int address:12, op:4;
} others;
#else
#error Must define BIG_ENDIAN or LITTLE_ENDIAN
#endif // LITTLE_ENDIAN
#endif // BIG_ENDIAN
} ir, ir2;
char* decodeIBCMInstruction(uint16_t inst) {
static string instnames[] = {"halt", "I/O", "shifts", "load", "store", "add", "sub", "and",
"or", "xor", "not", "nop", "jmp", "jmpe", "jmpl", "brl"
};
static char buf[256], buf2[8];
ir2.buf = inst;
int op = ir2.others.op;
buf[0] = 0;
if ( (op <= 2) || (op == 10) || (op == 11) ) // halt, not, nop, I/O, and shift
strcpy (buf, instnames[op].c_str());
else if ( op >= 3 ) {
strcpy (buf, instnames[ir2.others.op].c_str());
strcat (buf, "\t");
sprintf (buf2, "%.3x", ir2.others.address);
strcat (buf, buf2);
}
return buf;
}
void simulateIBCM() {
int acc = 0, pc = 0, incpc;
string buf;
while (1) {
acc &= 0x0000ffff;
incpc = 1;
ir.buf = mem[pc];
if ( verbose >= 2 )
printf ("\npc = %.3x: ir = %.4x, ibcm = '%s'\n", pc, ir.buf, decodeIBCMInstruction(ir.buf));
numinst++;
switch (ir.others.op) {
case 0: // halt
if ( verbose >= 1 ) printf ("pc = %.3x: halt\n", pc);
return;
break;
case 1: // I/O
switch ( ir.io.ioopt ) {
case 0: // read hex
if ( verbose >= 1 ) printf ("pc = %.3x: I/O: read hex\n", pc);
printf ("Enter hex input: ");
fflush (stdout);
cin >> buf;
sscanf (buf.c_str(), "%x", &acc);
acc &= 0x0000ffff;
printf ("\n");
break;
case 1: // read ascii
if ( verbose >= 1 ) printf ("pc = %.3x: I/O: read ascii\n", pc);
printf ("Enter ascii input: ");
fflush (stdout);
cin >> buf;
acc = buf[0];
acc &= 0x0000ffff;
printf ("\n");
break;
case 2: // write hex
if ( verbose >= 1 ) printf ("pc = %.3x: I/O: write hex\n", pc);
if ( verbose >= 1 ) printf ("Hex output: ");
printf ("%.4x\n", acc);
break;
case 3: // write ascii
if ( verbose >= 1 ) printf ("pc = %.3x: I/O: write ascii\n", pc);
if ( verbose >= 1 ) printf ("Ascii output: ");
printf ("%c\n", acc & 0xff);
break;
}
break;
case 2: // shifts
switch ( ir.shifts.shiftop ) {
case 0: // shift left
acc = (acc << ir.shifts.shiftcount) & 0xffff;
acc &= 0x0000ffff;
if ( verbose >= 1 ) printf ("pc = %.3x: shift left by %d; result is %.4x\n", pc, ir.shifts.shiftop, acc);
break;
case 1: // shift right
acc = (acc >> ir.shifts.shiftcount) & (0xffff >> ir.shifts.shiftcount);
acc &= 0x0000ffff;
if ( verbose >= 1 ) printf ("pc = %.3x: shift right by %d; result is %.4x\n", pc, ir.shifts.shiftop, acc);
break;
case 2: // rotate left
acc = ((acc << ir.shifts.shiftcount) & 0xffff) |
((acc >> (16-ir.shifts.shiftcount)) & (0xffff >> (16-ir.shifts.shiftcount)));
acc &= 0x0000ffff;
if ( verbose >= 1 ) printf ("pc = %.3x: rotate left by %d; result is %.4x\n", pc, ir.shifts.shiftop, acc);
break;
case 3: // rotate right
acc = ((acc >> ir.shifts.shiftcount) & (0xffff >> ir.shifts.shiftcount)) |
((acc << (16-ir.shifts.shiftcount)) & 0xffff);
acc &= 0x0000ffff;
if ( verbose >= 1 ) printf ("pc = %.3x: rotate right by %d; result is %.4x\n", pc, ir.shifts.shiftop, acc);
break;
}
break;
case 3: // load
acc = mem[ir.others.address];
acc &= 0x0000ffff;
if ( verbose >= 1 ) printf ("pc = %.3x: load result (@ %.3x) is %.4x\n", pc, ir.others.address, acc);
break;
case 4: // store
acc &= 0x0000ffff;
mem[ir.others.address] = acc;
if ( verbose >= 1 ) printf ("pc = %.3x: store result (@ %.3x) is %.4x\n", pc, ir.others.address, mem[ir.others.address]);
break;
case 5: // add
acc += mem[ir.others.address];
acc &= 0x0000ffff;
if ( verbose >= 1 ) printf ("pc = %.3x: add result is %.4x\n", pc, acc);
break;
case 6: // sub
acc -= mem[ir.others.address];
acc &= 0x0000ffff;
if ( verbose >= 1 ) printf ("pc = %.3x: sub result is %.4x\n", pc, acc);
break;
case 7: // and
acc &= mem[ir.others.address];
acc &= 0x0000ffff;
if ( verbose >= 1 ) printf ("pc = %.3x: and result is %.4x\n", pc, acc);
break;
case 8: // or
acc |= mem[ir.others.address];
acc &= 0x0000ffff;
if ( verbose >= 1 ) printf ("pc = %.3x: or result is %.4x\n", pc, acc);
break;
case 9: // xor
acc ^= mem[ir.others.address];
acc &= 0x0000ffff;
if ( verbose >= 1 ) printf ("pc = %.3x: xor result is %.4x\n", pc, acc);
break;
case 10: // not
acc = ~acc;
acc &= 0x0000ffff;
if ( verbose >= 1 ) printf ("pc = %.3x: not result is %.4x\n", pc, acc);
break;
case 11: // nop
// do nothing
if ( verbose >= 1 ) printf ("pc = %.3x: nop\n", pc);
break;
case 12: // jmp
if ( verbose >= 1 ) printf ("pc = %.3x: jmp to %.4x\n", pc, ir.others.address);
pc = ir.others.address;
incpc = 0;
break;
case 13: // jmpe
if ( verbose >= 1 ) printf ("pc = %.3x: jmpe to %.4x\n", pc, ir.others.address);
if ( (acc & 0x0000ffff) == 0 ) {
pc = ir.others.address;
incpc = 0;
}
break;
case 14: // jmpl
if ( verbose >= 1 ) printf ("pc = %.3x: jmpl to %.3x\n", pc, ir.others.address);
if ( (acc & 0x00008000) != 0 ) {
pc = ir.others.address;
incpc = 0;
}
break;
case 15: // brl
acc = pc+1;
acc &= 0x0000ffff;
if ( verbose >= 1 ) printf ("pc = %.3x: brl from %.3x to %.4x\n", pc, acc, ir.others.address);
pc = ir.others.address;
incpc = 0;
break;
}
if ( incpc )
pc++;
if ( maxticks && (numinst >= maxticks) ) {
cerr << "Simulation reached the maximum number of " << maxticks << " instructions to execute." << endl;
cout << "Simulation reached the maximum number of " << maxticks << " instructions to execute." << endl;
break;
}
}
}