forked from aaronbloomfield/pdr
-
Notifications
You must be signed in to change notification settings - Fork 228
/
ibcm-simulator.cpp.html
493 lines (471 loc) · 95.9 KB
/
ibcm-simulator.cpp.html
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="GNU source-highlight
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite">
<title>ibcm-simulator.cpp</title>
</head>
<body style="background-color:white">
<pre><i><span style="color:#9A1900">/* A program that will simulate an IBCM program, and it has a number</span></i>
<i><span style="color:#9A1900"> * of command line switches that control its execution. This program</span></i>
<i><span style="color:#9A1900"> * is useful for automating the execution of a number of IBCM programs</span></i>
<i><span style="color:#9A1900"> * without having to load each one into the web interface. It will</span></i>
<i><span style="color:#9A1900"> * also print out traces of the entire program execution, if desired.</span></i>
<i><span style="color:#9A1900"> * Run with the `-help` flag to see the full list of options.</span></i>
<i><span style="color:#9A1900"> */</span></i>
<b><span style="color:#000080">#ifndef</span></b> IS_LITTLE_ENDIAN
<b><span style="color:#000080">#ifndef</span></b> IS_BIG_ENDIAN
<b><span style="color:#000080">#error</span></b> Must <span style="color:#008080">define</span> IS_LITTLE_ENDIAN <span style="color:#008080">or</span> IS_BIG_ENDIAN <span style="color:#008080">via</span> the `<span style="color:#990000">-</span>D` flag to the compiler
<b><span style="color:#000080">#define</span></b> IS_LITTLE_ENDIAN
<b><span style="color:#000080">#endif</span></b>
<b><span style="color:#000080">#endif</span></b>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><iostream></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><fstream></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><string></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><string.h></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><stdlib.h></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><assert.h></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><stdio.h></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><stdint.h></span>
<b><span style="color:#0000FF">using</span></b> <b><span style="color:#0000FF">namespace</span></b> std<span style="color:#990000">;</span>
<span style="color:#009900">int</span> compState <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#009900">int</span> simState <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#009900">int</span> printState <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#009900">int</span> printStats <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#009900">int</span> dumpState <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#009900">int</span> verbose <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#009900">unsigned</span> <span style="color:#009900">int</span> numinst <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#009900">int</span> printTicks <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#009900">unsigned</span> <span style="color:#009900">int</span> maxticks <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#008080">string</span> outputFile <span style="color:#990000">=</span> <span style="color:#FF0000">"ibcm.bin"</span><span style="color:#990000">;</span>
<span style="color:#008080">string</span> inputFile<span style="color:#990000">;</span>
<span style="color:#008080">uint16_t</span> mem<span style="color:#990000">[</span><span style="color:#993399">4096</span><span style="color:#990000">];</span>
<span style="color:#009900">void</span> <b><span style="color:#000000">printHelp</span></b><span style="color:#990000">(</span><span style="color:#009900">char</span> <span style="color:#990000">*</span>name<span style="color:#990000">);</span>
<span style="color:#009900">void</span> <b><span style="color:#000000">checkEndian</span></b><span style="color:#990000">(</span><span style="color:#009900">void</span><span style="color:#990000">);</span>
<span style="color:#009900">void</span> <b><span style="color:#000000">compileCode</span></b> <span style="color:#990000">(</span><span style="color:#008080">string</span> infilename<span style="color:#990000">,</span> <span style="color:#008080">string</span> outfilename<span style="color:#990000">);</span>
<span style="color:#009900">void</span> <b><span style="color:#000000">simulateIBCM</span></b> <span style="color:#990000">();</span>
<span style="color:#009900">void</span> <b><span style="color:#000000">readBinaryIBCMFile</span></b> <span style="color:#990000">(</span><span style="color:#008080">string</span> infilename<span style="color:#990000">);</span>
<span style="color:#009900">void</span> <b><span style="color:#000000">dumpIBCMFile</span></b> <span style="color:#990000">();</span>
<span style="color:#009900">char</span><span style="color:#990000">*</span> <b><span style="color:#000000">decodeIBCMInstruction</span></b><span style="color:#990000">(</span><span style="color:#008080">uint16_t</span> inst<span style="color:#990000">);</span>
<span style="color:#009900">int</span> <b><span style="color:#000000">main</span></b><span style="color:#990000">(</span><span style="color:#009900">int</span> argc<span style="color:#990000">,</span> <span style="color:#009900">char</span> <span style="color:#990000">*</span>argv<span style="color:#990000">[])</span> <span style="color:#FF0000">{</span>
<span style="color:#009900">int</span> i <span style="color:#990000">=</span> <span style="color:#993399">1</span><span style="color:#990000">;</span>
<b><span style="color:#000000">checkEndian</span></b><span style="color:#990000">();</span>
<b><span style="color:#000000">assert</span></b> <span style="color:#990000">(</span><b><span style="color:#0000FF">sizeof</span></b><span style="color:#990000">(</span><span style="color:#009900">short</span><span style="color:#990000">)</span> <span style="color:#990000">==</span> <span style="color:#993399">2</span><span style="color:#990000">);</span>
ios<span style="color:#990000">::</span><b><span style="color:#000000">sync_with_stdio</span></b><span style="color:#990000">();</span> <i><span style="color:#9A1900">// Synchronize C++ and C I/O subsystems!</span></i>
<b><span style="color:#0000FF">while</span></b> <span style="color:#990000">(</span>i <span style="color:#990000"><</span> argc<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span><b><span style="color:#000000">strcmp</span></b><span style="color:#990000">(</span>argv<span style="color:#990000">[</span>i<span style="color:#990000">],</span> <span style="color:#FF0000">"-comp"</span><span style="color:#990000">)==</span><span style="color:#993399">0</span><span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
compState<span style="color:#990000">=</span><span style="color:#993399">1</span><span style="color:#990000">;</span>
inputFile<span style="color:#990000">=</span>argv<span style="color:#990000">[</span>i<span style="color:#990000">+</span><span style="color:#993399">1</span><span style="color:#990000">];</span> <i><span style="color:#9A1900">// TODO: check that this doesn't go past the array bounds</span></i>
<span style="color:#FF0000">}</span> <b><span style="color:#0000FF">else</span></b> <b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span><b><span style="color:#000000">strcmp</span></b><span style="color:#990000">(</span>argv<span style="color:#990000">[</span>i<span style="color:#990000">],</span> <span style="color:#FF0000">"-sim"</span><span style="color:#990000">)==</span><span style="color:#993399">0</span><span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
simState<span style="color:#990000">=</span><span style="color:#993399">1</span><span style="color:#990000">;</span>
inputFile<span style="color:#990000">=</span>argv<span style="color:#990000">[</span>i<span style="color:#990000">+</span><span style="color:#993399">1</span><span style="color:#990000">];</span> <i><span style="color:#9A1900">// TODO: check that this doesn't go past the array bounds</span></i>
<span style="color:#FF0000">}</span> <b><span style="color:#0000FF">else</span></b> <b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span><b><span style="color:#000000">strcmp</span></b><span style="color:#990000">(</span>argv<span style="color:#990000">[</span>i<span style="color:#990000">],</span> <span style="color:#FF0000">"-dump"</span><span style="color:#990000">)==</span><span style="color:#993399">0</span><span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
dumpState<span style="color:#990000">=</span><span style="color:#993399">1</span><span style="color:#990000">;</span>
inputFile<span style="color:#990000">=</span>argv<span style="color:#990000">[</span>i<span style="color:#990000">+</span><span style="color:#993399">1</span><span style="color:#990000">];</span> <i><span style="color:#9A1900">// TODO: check that this doesn't go past the array bounds</span></i>
<span style="color:#FF0000">}</span> <b><span style="color:#0000FF">else</span></b> <b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span><b><span style="color:#000000">strcmp</span></b><span style="color:#990000">(</span>argv<span style="color:#990000">[</span>i<span style="color:#990000">],</span> <span style="color:#FF0000">"-o"</span><span style="color:#990000">)==</span><span style="color:#993399">0</span><span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
outputFile<span style="color:#990000">=</span>argv<span style="color:#990000">[</span>i<span style="color:#990000">+</span><span style="color:#993399">1</span><span style="color:#990000">];</span> <i><span style="color:#9A1900">// TODO: check that this doesn't go past the array bounds</span></i>
i<span style="color:#990000">++;</span>
<span style="color:#FF0000">}</span> <b><span style="color:#0000FF">else</span></b> <b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span><b><span style="color:#000000">strcmp</span></b><span style="color:#990000">(</span>argv<span style="color:#990000">[</span>i<span style="color:#990000">],</span> <span style="color:#FF0000">"-verbose"</span><span style="color:#990000">)==</span><span style="color:#993399">0</span><span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
verbose<span style="color:#990000">++;</span>
<span style="color:#FF0000">}</span> <b><span style="color:#0000FF">else</span></b> <b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span><b><span style="color:#000000">strcmp</span></b><span style="color:#990000">(</span>argv<span style="color:#990000">[</span>i<span style="color:#990000">],</span> <span style="color:#FF0000">"-help"</span><span style="color:#990000">)==</span><span style="color:#993399">0</span><span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#000000">printHelp</span></b><span style="color:#990000">(</span>argv<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">]);</span>
<span style="color:#FF0000">}</span> <b><span style="color:#0000FF">else</span></b> <b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span><b><span style="color:#000000">strcmp</span></b><span style="color:#990000">(</span>argv<span style="color:#990000">[</span>i<span style="color:#990000">],</span> <span style="color:#FF0000">"-print"</span><span style="color:#990000">)==</span><span style="color:#993399">0</span><span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
printState <span style="color:#990000">=</span> <span style="color:#993399">1</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span> <b><span style="color:#0000FF">else</span></b> <b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span><b><span style="color:#000000">strcmp</span></b><span style="color:#990000">(</span>argv<span style="color:#990000">[</span>i<span style="color:#990000">],</span> <span style="color:#FF0000">"-stats"</span><span style="color:#990000">)==</span><span style="color:#993399">0</span><span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
printStats <span style="color:#990000">=</span> <span style="color:#993399">1</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span> <b><span style="color:#0000FF">else</span></b> <b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span><b><span style="color:#000000">strcmp</span></b><span style="color:#990000">(</span>argv<span style="color:#990000">[</span>i<span style="color:#990000">],</span> <span style="color:#FF0000">"-maxticks"</span><span style="color:#990000">)==</span><span style="color:#993399">0</span><span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
maxticks <span style="color:#990000">=</span> <b><span style="color:#000000">atoi</span></b><span style="color:#990000">(</span>argv<span style="color:#990000">[++</span>i<span style="color:#990000">]);</span> <i><span style="color:#9A1900">// TODO: check the format of the int</span></i>
<span style="color:#FF0000">}</span>
i<span style="color:#990000">++;</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">//Check for proper combination of params</span></i>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span>simState<span style="color:#990000">==</span><span style="color:#993399">0</span> <span style="color:#990000">&&</span> compState<span style="color:#990000">==</span><span style="color:#993399">0</span> <span style="color:#990000">&&</span> inputFile<span style="color:#990000">==</span><span style="color:#FF0000">""</span><span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<i><span style="color:#9A1900">//cerr << "No option specified..." << endl;</span></i>
<b><span style="color:#000000">printHelp</span></b><span style="color:#990000">(</span>argv<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">]);</span>
<b><span style="color:#0000FF">return</span></b> <span style="color:#993399">1</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> compState <span style="color:#990000">)</span>
<b><span style="color:#000000">compileCode</span></b> <span style="color:#990000">(</span>inputFile<span style="color:#990000">,</span> outputFile<span style="color:#990000">);</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> simState <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#000000">readBinaryIBCMFile</span></b> <span style="color:#990000">(</span>inputFile<span style="color:#990000">);</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> printState <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#000000">dumpIBCMFile</span></b> <span style="color:#990000">();</span>
cout <span style="color:#990000"><<</span> endl <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<b><span style="color:#000000">simulateIBCM</span></b><span style="color:#990000">();</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> printStats <span style="color:#990000">)</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"Total of "</span> <span style="color:#990000"><<</span> numinst <span style="color:#990000"><<</span> <span style="color:#FF0000">" instructions executed."</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> printState <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> endl <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#000000">dumpIBCMFile</span></b> <span style="color:#990000">();</span>
<span style="color:#FF0000">}</span>
<span style="color:#FF0000">}</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> dumpState <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#000000">readBinaryIBCMFile</span></b> <span style="color:#990000">(</span>inputFile<span style="color:#990000">);</span>
<b><span style="color:#000000">dumpIBCMFile</span></b> <span style="color:#990000">();</span>
<span style="color:#FF0000">}</span>
<b><span style="color:#0000FF">return</span></b> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<span style="color:#009900">void</span> <b><span style="color:#000000">printHelp</span></b><span style="color:#990000">(</span><span style="color:#009900">char</span> <span style="color:#990000">*</span>name<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">static</span></b> <span style="color:#009900">bool</span> helpPrinted<span style="color:#990000">;</span> <i><span style="color:#9A1900">// Static values are initialized to 0 or 0-equivalent</span></i>
<b><span style="color:#0000FF">if</span></b><span style="color:#990000">(</span>helpPrinted<span style="color:#990000">)</span> <b><span style="color:#0000FF">return</span></b><span style="color:#990000">;</span>
helpPrinted <span style="color:#990000">=</span> <b><span style="color:#0000FF">true</span></b><span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"Usage: "</span> <span style="color:#990000"><<</span> name <span style="color:#990000"><<</span> <span style="color:#FF0000">" [option] ..."</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"Options:"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"</span><span style="color:#CC33CC">\t</span><span style="color:#FF0000">[-comp <inputfile>]</span><span style="color:#CC33CC">\t</span><span style="color:#FF0000">Signals the program to compile the IBCM file speicfied by <inputfile>"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"</span><span style="color:#CC33CC">\t</span><span style="color:#FF0000">[-sim <inputfile>]</span><span style="color:#CC33CC">\t</span><span style="color:#FF0000">Signals the program to simulate the IBCM program specified by <inputfile>"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"</span><span style="color:#CC33CC">\t</span><span style="color:#FF0000">[-o <outfile>]</span><span style="color:#CC33CC">\t\t</span><span style="color:#FF0000">Specify the name of the outfile produced by compiling <inputfile>"</span> <span style="color:#990000"><<</span> endl
<span style="color:#990000"><<</span> <span style="color:#FF0000">"</span><span style="color:#CC33CC">\t\t\t\t</span><span style="color:#FF0000">The defaut value for <outfile> is </span><span style="color:#CC33CC">\"</span><span style="color:#FF0000">ibcm.bin.</span><span style="color:#CC33CC">\"</span><span style="color:#FF0000">"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"</span><span style="color:#CC33CC">\t</span><span style="color:#FF0000">[-verbose]</span><span style="color:#CC33CC">\t\t</span><span style="color:#FF0000">Prints useful debugging information while compiling and emulating"</span> <span style="color:#990000"><<</span> endl
<span style="color:#990000"><<</span> <span style="color:#FF0000">"</span><span style="color:#CC33CC">\t\t\t\t</span><span style="color:#FF0000">he specified IBCM program."</span><span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">" Specify twice for more output."</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"</span><span style="color:#CC33CC">\t</span><span style="color:#FF0000">[-dump <inputfile>]</span><span style="color:#CC33CC">\t</span><span style="color:#FF0000">Outputs a binary IBCM file in text format."</span><span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">" Also specify -verbose for decompilation."</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"</span><span style="color:#CC33CC">\t</span><span style="color:#FF0000">[-print]</span><span style="color:#CC33CC">\t\t</span><span style="color:#FF0000">Prints a listing of the program before and after the simulation."</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"</span><span style="color:#CC33CC">\t</span><span style="color:#FF0000">[-maxticks <n>]</span><span style="color:#CC33CC">\t\t</span><span style="color:#FF0000">Set the maximum number of ticks."</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"</span><span style="color:#CC33CC">\t</span><span style="color:#FF0000">[-stats]</span><span style="color:#CC33CC">\t\t</span><span style="color:#FF0000">Prints stats of the executed program, including the number of ticks."</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"</span><span style="color:#CC33CC">\t</span><span style="color:#FF0000">[-help]</span><span style="color:#CC33CC">\t\t\t</span><span style="color:#FF0000">Prints this help message."</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<span style="color:#009900">void</span> <b><span style="color:#000000">checkEndian</span></b><span style="color:#990000">(</span><span style="color:#009900">void</span><span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">static</span></b> <span style="color:#009900">int</span> firsttime <span style="color:#990000">=</span> <span style="color:#993399">1</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span>firsttime<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">union</span></b> <span style="color:#FF0000">{</span>
<span style="color:#009900">char</span> charword<span style="color:#990000">[</span><span style="color:#993399">4</span><span style="color:#990000">];</span>
<span style="color:#009900">unsigned</span> <span style="color:#009900">int</span> intword<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span> check<span style="color:#990000">;</span>
check<span style="color:#990000">.</span>charword<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">]</span> <span style="color:#990000">=</span> <span style="color:#993399">1</span><span style="color:#990000">;</span>
check<span style="color:#990000">.</span>charword<span style="color:#990000">[</span><span style="color:#993399">1</span><span style="color:#990000">]</span> <span style="color:#990000">=</span> <span style="color:#993399">2</span><span style="color:#990000">;</span>
check<span style="color:#990000">.</span>charword<span style="color:#990000">[</span><span style="color:#993399">2</span><span style="color:#990000">]</span> <span style="color:#990000">=</span> <span style="color:#993399">3</span><span style="color:#990000">;</span>
check<span style="color:#990000">.</span>charword<span style="color:#990000">[</span><span style="color:#993399">3</span><span style="color:#990000">]</span> <span style="color:#990000">=</span> <span style="color:#993399">4</span><span style="color:#990000">;</span>
<b><span style="color:#000080">#ifdef</span></b> IS_BIG_ENDIAN
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span>check<span style="color:#990000">.</span>intword <span style="color:#990000">!=</span> <span style="color:#993399">0x01020304</span><span style="color:#990000">)</span> <span style="color:#FF0000">{</span> <i><span style="color:#9A1900">/* big */</span></i>
cerr <span style="color:#990000"><<</span> <span style="color:#FF0000">"ERROR: Host machine is not Big Endian.</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">Exiting."</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#000000">exit</span></b><span style="color:#990000">(</span><span style="color:#993399">205</span><span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
<b><span style="color:#000080">#else</span></b>
<b><span style="color:#000080">#ifdef</span></b> IS_LITTLE_ENDIAN
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span>check<span style="color:#990000">.</span>intword <span style="color:#990000">!=</span> <span style="color:#993399">0x04030201</span><span style="color:#990000">)</span> <span style="color:#FF0000">{</span> <i><span style="color:#9A1900">/* little */</span></i>
cerr <span style="color:#990000"><<</span> <span style="color:#FF0000">"ERROR: Host machine is not Little Endian.</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">Exiting."</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#000000">exit</span></b><span style="color:#990000">(</span><span style="color:#993399">206</span><span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
<b><span style="color:#000080">#else</span></b>
<b><span style="color:#000080">#error</span></b> ERROR<span style="color:#990000">:</span> must define <span style="color:#008080">either</span> IS_LITTLE_ENDIAN <span style="color:#008080">or</span> IS_BIG_ENDIAN
<b><span style="color:#000080">#endif</span></b> <i><span style="color:#9A1900">// IS_LITTLE_ENDIAN</span></i>
<b><span style="color:#000080">#endif</span></b> <i><span style="color:#9A1900">// IS_BIG_ENDIAN</span></i>
firsttime <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<span style="color:#FF0000">}</span>
<span style="color:#009900">bool</span> <b><span style="color:#000000">ishexdigit</span></b> <span style="color:#990000">(</span><span style="color:#009900">int</span> c<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <b><span style="color:#000000">isdigit</span></b><span style="color:#990000">(</span>c<span style="color:#990000">)</span> <span style="color:#990000">)</span>
<b><span style="color:#0000FF">return</span></b> <b><span style="color:#0000FF">true</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">(</span>c <span style="color:#990000">>=</span> <span style="color:#FF0000">'a'</span><span style="color:#990000">)</span> <span style="color:#990000">&&</span> <span style="color:#990000">(</span>c <span style="color:#990000"><=</span> <span style="color:#FF0000">'f'</span><span style="color:#990000">)</span> <span style="color:#990000">)</span>
<b><span style="color:#0000FF">return</span></b> <b><span style="color:#0000FF">true</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">(</span>c <span style="color:#990000">>=</span> <span style="color:#FF0000">'A'</span><span style="color:#990000">)</span> <span style="color:#990000">&&</span> <span style="color:#990000">(</span>c <span style="color:#990000"><=</span> <span style="color:#FF0000">'F'</span><span style="color:#990000">)</span> <span style="color:#990000">)</span>
<b><span style="color:#0000FF">return</span></b> <b><span style="color:#0000FF">true</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">return</span></b> <b><span style="color:#0000FF">false</span></b><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<span style="color:#009900">void</span> <b><span style="color:#000000">dumpIBCMFile</span></b> <span style="color:#990000">()</span> <span style="color:#FF0000">{</span>
<i><span style="color:#9A1900">// find last command to not print</span></i>
<span style="color:#009900">int</span> end<span style="color:#990000">;</span>
<b><span style="color:#0000FF">for</span></b> <span style="color:#990000">(</span> end <span style="color:#990000">=</span> <span style="color:#993399">4095</span><span style="color:#990000">;</span> end <span style="color:#990000">>=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span> end<span style="color:#990000">--</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> mem<span style="color:#990000">[</span>end<span style="color:#990000">]</span> <span style="color:#990000">!=</span> <span style="color:#993399">0</span> <span style="color:#990000">)</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<b><span style="color:#0000FF">for</span></b> <span style="color:#990000">(</span> <span style="color:#009900">int</span> i <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span> i <span style="color:#990000"><=</span> end<span style="color:#990000">;</span> i<span style="color:#990000">++</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"%.4x</span><span style="color:#CC33CC">\t</span><span style="color:#FF0000">pc = %.4x</span><span style="color:#CC33CC">\t</span><span style="color:#FF0000">%s</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> mem<span style="color:#990000">[</span>i<span style="color:#990000">],</span> i<span style="color:#990000">,</span>
<b><span style="color:#000000">decodeIBCMInstruction</span></b><span style="color:#990000">(</span>mem<span style="color:#990000">[</span>i<span style="color:#990000">]));</span>
<span style="color:#FF0000">}</span>
<span style="color:#FF0000">}</span>
<span style="color:#009900">void</span> <b><span style="color:#000000">compileCode</span></b> <span style="color:#990000">(</span><span style="color:#008080">string</span> infilename<span style="color:#990000">,</span> <span style="color:#008080">string</span> outfilename<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<span style="color:#008080">string</span> line<span style="color:#990000">;</span>
<span style="color:#009900">int</span> linenum <span style="color:#990000">=</span> <span style="color:#990000">-</span><span style="color:#993399">1</span><span style="color:#990000">,</span> outputlines <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<i><span style="color:#9A1900">// open input file</span></i>
<span style="color:#008080">ifstream</span> <b><span style="color:#000000">infile</span></b><span style="color:#990000">(</span>infilename<span style="color:#990000">.</span><b><span style="color:#000000">c_str</span></b><span style="color:#990000">());</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">!</span>infile<span style="color:#990000">.</span><b><span style="color:#000000">is_open</span></b><span style="color:#990000">()</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
cerr <span style="color:#990000"><<</span> <span style="color:#FF0000">"Error: unable to open input file."</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#000000">exit</span></b><span style="color:#990000">(</span><span style="color:#993399">200</span><span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// open output file</span></i>
<span style="color:#008080">ofstream</span> <b><span style="color:#000000">outfile</span></b><span style="color:#990000">(</span>outfilename<span style="color:#990000">.</span><b><span style="color:#000000">c_str</span></b><span style="color:#990000">());</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">!</span>outfile<span style="color:#990000">.</span><b><span style="color:#000000">is_open</span></b><span style="color:#990000">()</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
cerr <span style="color:#990000"><<</span> <span style="color:#FF0000">"Error: unable to open output file."</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#000000">exit</span></b><span style="color:#990000">(</span><span style="color:#993399">201</span><span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// read input file, write to output file</span></i>
<b><span style="color:#0000FF">while</span></b> <span style="color:#990000">(!</span>infile<span style="color:#990000">.</span><b><span style="color:#000000">eof</span></b><span style="color:#990000">())</span> <span style="color:#FF0000">{</span>
linenum<span style="color:#990000">++;</span>
<b><span style="color:#000000">getline</span></b> <span style="color:#990000">(</span>infile<span style="color:#990000">,</span>line<span style="color:#990000">);</span>
<i><span style="color:#9A1900">// skip blank lines</span></i>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> line<span style="color:#990000">.</span><b><span style="color:#000000">size</span></b><span style="color:#990000">()</span> <span style="color:#990000">==</span> <span style="color:#993399">0</span> <span style="color:#990000">)</span>
<b><span style="color:#0000FF">continue</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">(</span>line<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">]</span> <span style="color:#990000">==</span> <span style="color:#FF0000">'/'</span><span style="color:#990000">)</span> <span style="color:#990000">&&</span> <span style="color:#990000">(</span>line<span style="color:#990000">[</span><span style="color:#993399">1</span><span style="color:#990000">]</span> <span style="color:#990000">==</span> <span style="color:#FF0000">'/'</span><span style="color:#990000">)</span> <span style="color:#990000">)</span>
<b><span style="color:#0000FF">continue</span></b><span style="color:#990000">;</span>
<i><span style="color:#9A1900">// sanity check</span></i>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">!</span><b><span style="color:#000000">ishexdigit</span></b><span style="color:#990000">(</span>line<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">])</span> <span style="color:#990000">||</span> <span style="color:#990000">!</span><b><span style="color:#000000">ishexdigit</span></b><span style="color:#990000">(</span>line<span style="color:#990000">[</span><span style="color:#993399">1</span><span style="color:#990000">])</span> <span style="color:#990000">||</span>
<span style="color:#990000">!</span><b><span style="color:#000000">ishexdigit</span></b><span style="color:#990000">(</span>line<span style="color:#990000">[</span><span style="color:#993399">2</span><span style="color:#990000">])</span> <span style="color:#990000">||</span> <span style="color:#990000">!</span><b><span style="color:#000000">ishexdigit</span></b><span style="color:#990000">(</span>line<span style="color:#990000">[</span><span style="color:#993399">3</span><span style="color:#990000">])</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
cerr <span style="color:#990000"><<</span> <span style="color:#FF0000">"Error on line "</span> <span style="color:#990000"><<</span> linenum <span style="color:#990000"><<</span> <span style="color:#FF0000">": invalid hex digits"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
line<span style="color:#990000">[</span><span style="color:#993399">4</span><span style="color:#990000">]</span> <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#009900">int</span> hex<span style="color:#990000">;</span>
<b><span style="color:#000000">sscanf</span></b> <span style="color:#990000">(</span>line<span style="color:#990000">.</span><b><span style="color:#000000">c_str</span></b><span style="color:#990000">(),</span> <span style="color:#FF0000">"%x"</span><span style="color:#990000">,</span> <span style="color:#990000">&</span>hex<span style="color:#990000">);</span>
<b><span style="color:#000080">#ifdef</span></b> IS_LITTLE_ENDIAN
<span style="color:#009900">char</span> g <span style="color:#990000">=</span> hex <span style="color:#990000">%</span> <span style="color:#993399">256</span><span style="color:#990000">;</span>
outfile<span style="color:#990000">.</span><b><span style="color:#000000">write</span></b><span style="color:#990000">(&</span>g<span style="color:#990000">,</span><span style="color:#993399">1</span><span style="color:#990000">);</span>
g <span style="color:#990000">=</span> hex <span style="color:#990000">/</span> <span style="color:#993399">256</span><span style="color:#990000">;</span>
outfile<span style="color:#990000">.</span><b><span style="color:#000000">write</span></b><span style="color:#990000">(&</span>g<span style="color:#990000">,</span><span style="color:#993399">1</span><span style="color:#990000">);</span>
<b><span style="color:#000080">#else</span></b>
<b><span style="color:#000080">#error</span></b> Not yet working <span style="color:#008080">for</span> non<span style="color:#990000">-</span>little<span style="color:#990000">-</span>endian machines
<b><span style="color:#000080">#endif</span></b>
<i><span style="color:#9A1900">//cout << line[0] << line[1] << line[2] << line[3] << endl;</span></i>
outputlines<span style="color:#990000">++;</span>
<span style="color:#FF0000">}</span>
infile<span style="color:#990000">.</span><b><span style="color:#000000">close</span></b><span style="color:#990000">();</span>
<i><span style="color:#9A1900">// fill remaining space with 0's</span></i>
<b><span style="color:#0000FF">for</span></b> <span style="color:#990000">(;</span> outputlines <span style="color:#990000"><</span> <span style="color:#993399">4096</span><span style="color:#990000">;</span> outputlines<span style="color:#990000">++</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<span style="color:#009900">char</span> x <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<i><span style="color:#9A1900">//cout << "0000" << endl;</span></i>
outfile<span style="color:#990000">.</span><b><span style="color:#000000">write</span></b> <span style="color:#990000">(&</span>x<span style="color:#990000">,</span><span style="color:#993399">1</span><span style="color:#990000">);</span>
outfile<span style="color:#990000">.</span><b><span style="color:#000000">write</span></b> <span style="color:#990000">(&</span>x<span style="color:#990000">,</span><span style="color:#993399">1</span><span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
outfile<span style="color:#990000">.</span><b><span style="color:#000000">close</span></b><span style="color:#990000">();</span>
<span style="color:#FF0000">}</span>
<span style="color:#009900">void</span> <b><span style="color:#000000">readBinaryIBCMFile</span></b> <span style="color:#990000">(</span><span style="color:#008080">string</span> infilename<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<span style="color:#009900">int</span> cmdnum <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<i><span style="color:#9A1900">// open input file</span></i>
<span style="color:#008080">ifstream</span> <b><span style="color:#000000">infile</span></b><span style="color:#990000">(</span>infilename<span style="color:#990000">.</span><b><span style="color:#000000">c_str</span></b><span style="color:#990000">());</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">!</span>infile<span style="color:#990000">.</span><b><span style="color:#000000">is_open</span></b><span style="color:#990000">()</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
cerr <span style="color:#990000"><<</span> <span style="color:#FF0000">"Error: unable to open input file."</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#000000">exit</span></b><span style="color:#990000">(</span><span style="color:#993399">202</span><span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// load up file into array</span></i>
<b><span style="color:#0000FF">for</span></b> <span style="color:#990000">(</span> <span style="color:#009900">int</span> i <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span> i <span style="color:#990000"><</span> <span style="color:#993399">4096</span><span style="color:#990000">;</span> i<span style="color:#990000">++</span> <span style="color:#990000">)</span>
mem<span style="color:#990000">[</span>i<span style="color:#990000">]</span> <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#009900">char</span> buf<span style="color:#990000">[</span><span style="color:#993399">2</span><span style="color:#990000">];</span>
<b><span style="color:#0000FF">while</span></b> <span style="color:#990000">(!</span>infile<span style="color:#990000">.</span><b><span style="color:#000000">eof</span></b><span style="color:#990000">())</span> <span style="color:#FF0000">{</span>
infile<span style="color:#990000">.</span><b><span style="color:#000000">read</span></b><span style="color:#990000">(</span>buf<span style="color:#990000">,</span><span style="color:#993399">2</span><span style="color:#990000">);</span>
<b><span style="color:#000080">#ifdef</span></b> IS_LITTLE_ENDIAN
mem<span style="color:#990000">[</span>cmdnum<span style="color:#990000">]</span> <span style="color:#990000">=</span> <span style="color:#990000">(</span>buf<span style="color:#990000">[</span><span style="color:#993399">1</span><span style="color:#990000">]</span> <span style="color:#990000">&</span> <span style="color:#993399">0x000000ff</span><span style="color:#990000">)</span> <span style="color:#990000">*</span> <span style="color:#993399">256</span> <span style="color:#990000">+</span> <span style="color:#990000">(</span>buf<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">]</span> <span style="color:#990000">&</span> <span style="color:#993399">0x000000ff</span><span style="color:#990000">);</span>
<b><span style="color:#000080">#else</span></b>
<b><span style="color:#000080">#error</span></b> Not yet working <span style="color:#008080">for</span> non<span style="color:#990000">-</span>little<span style="color:#990000">-</span>endian machines
<b><span style="color:#000080">#endif</span></b>
cmdnum<span style="color:#990000">++;</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">//for ( int i = 0; i < 4096; i++ ) printf ("%.4x\n", mem[i] & 0x0000ffff);</span></i>
<span style="color:#FF0000">}</span>
<b><span style="color:#0000FF">union</span></b> <span style="color:#008080">ibcm_instruction</span> <span style="color:#FF0000">{</span>
<span style="color:#008080">uint16_t</span> buf<span style="color:#990000">;</span>
<b><span style="color:#000080">#ifdef</span></b> IS_BIG_ENDIAN <i><span style="color:#9A1900">//The IBCM is big endian</span></i>
<b><span style="color:#0000FF">struct</span></b> <span style="color:#FF0000">{</span>
<span style="color:#009900">unsigned</span> <span style="color:#009900">char</span> high<span style="color:#990000">,</span> low<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span> bytes<span style="color:#990000">;</span>
<b><span style="color:#0000FF">struct</span></b> <span style="color:#FF0000">{</span>
<span style="color:#009900">unsigned</span> <span style="color:#009900">int</span> op<span style="color:#990000">:</span><span style="color:#993399">4</span><span style="color:#990000">,</span> unused<span style="color:#990000">:</span><span style="color:#993399">12</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span> halt<span style="color:#990000">;</span>
<b><span style="color:#0000FF">struct</span></b> <span style="color:#FF0000">{</span>
<span style="color:#009900">unsigned</span> <span style="color:#009900">int</span> op<span style="color:#990000">:</span><span style="color:#993399">4</span><span style="color:#990000">,</span> ioopt<span style="color:#990000">:</span><span style="color:#993399">2</span><span style="color:#990000">,</span> unused<span style="color:#990000">:</span><span style="color:#993399">10</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span> io<span style="color:#990000">;</span>
<b><span style="color:#0000FF">struct</span></b> <span style="color:#FF0000">{</span>
<span style="color:#009900">unsigned</span> <span style="color:#009900">int</span> op<span style="color:#990000">:</span><span style="color:#993399">4</span><span style="color:#990000">,</span> shiftop<span style="color:#990000">:</span><span style="color:#993399">2</span><span style="color:#990000">,</span> unused<span style="color:#990000">:</span><span style="color:#993399">5</span><span style="color:#990000">,</span> shiftcount<span style="color:#990000">:</span><span style="color:#993399">5</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span> shifts<span style="color:#990000">;</span>
<b><span style="color:#0000FF">struct</span></b> <span style="color:#FF0000">{</span>
<span style="color:#009900">unsigned</span> <span style="color:#009900">int</span> op<span style="color:#990000">:</span><span style="color:#993399">4</span><span style="color:#990000">,</span> address<span style="color:#990000">:</span><span style="color:#993399">12</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span> others<span style="color:#990000">;</span>
<b><span style="color:#000080">#else</span></b>
<b><span style="color:#000080">#ifdef</span></b> IS_LITTLE_ENDIAN <i><span style="color:#9A1900">//The IBCM is little endian</span></i>
<b><span style="color:#0000FF">struct</span></b> <span style="color:#FF0000">{</span>
<span style="color:#009900">unsigned</span> <span style="color:#009900">char</span> low<span style="color:#990000">,</span> high<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span> bytes<span style="color:#990000">;</span>
<b><span style="color:#0000FF">struct</span></b> <span style="color:#FF0000">{</span>
<span style="color:#009900">unsigned</span> <span style="color:#009900">int</span> unused<span style="color:#990000">:</span><span style="color:#993399">12</span><span style="color:#990000">,</span> op<span style="color:#990000">:</span><span style="color:#993399">4</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span> halt<span style="color:#990000">;</span>
<b><span style="color:#0000FF">struct</span></b> <span style="color:#FF0000">{</span>
<span style="color:#009900">unsigned</span> <span style="color:#009900">int</span> unused<span style="color:#990000">:</span><span style="color:#993399">10</span><span style="color:#990000">,</span> ioopt<span style="color:#990000">:</span><span style="color:#993399">2</span><span style="color:#990000">,</span> op<span style="color:#990000">:</span><span style="color:#993399">4</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span> io<span style="color:#990000">;</span>
<b><span style="color:#0000FF">struct</span></b> <span style="color:#FF0000">{</span>
<span style="color:#009900">unsigned</span> <span style="color:#009900">int</span> shiftcount<span style="color:#990000">:</span><span style="color:#993399">5</span><span style="color:#990000">,</span> unused<span style="color:#990000">:</span><span style="color:#993399">5</span><span style="color:#990000">,</span> shiftop<span style="color:#990000">:</span><span style="color:#993399">2</span><span style="color:#990000">,</span> op<span style="color:#990000">:</span><span style="color:#993399">4</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span> shifts<span style="color:#990000">;</span>
<b><span style="color:#0000FF">struct</span></b> <span style="color:#FF0000">{</span>
<span style="color:#009900">unsigned</span> <span style="color:#009900">int</span> address<span style="color:#990000">:</span><span style="color:#993399">12</span><span style="color:#990000">,</span> op<span style="color:#990000">:</span><span style="color:#993399">4</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span> others<span style="color:#990000">;</span>
<b><span style="color:#000080">#else</span></b>
<b><span style="color:#000080">#error</span></b> Must <span style="color:#008080">define</span> BIG_ENDIAN <span style="color:#008080">or</span> LITTLE_ENDIAN
<b><span style="color:#000080">#endif</span></b> <i><span style="color:#9A1900">// LITTLE_ENDIAN</span></i>
<b><span style="color:#000080">#endif</span></b> <i><span style="color:#9A1900">// BIG_ENDIAN</span></i>
<span style="color:#FF0000">}</span> ir<span style="color:#990000">,</span> ir2<span style="color:#990000">;</span>
<span style="color:#009900">char</span><span style="color:#990000">*</span> <b><span style="color:#000000">decodeIBCMInstruction</span></b><span style="color:#990000">(</span><span style="color:#008080">uint16_t</span> inst<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">static</span></b> <span style="color:#008080">string</span> instnames<span style="color:#990000">[]</span> <span style="color:#990000">=</span> <span style="color:#FF0000">{</span><span style="color:#FF0000">"halt"</span><span style="color:#990000">,</span> <span style="color:#FF0000">"I/O"</span><span style="color:#990000">,</span> <span style="color:#FF0000">"shifts"</span><span style="color:#990000">,</span> <span style="color:#FF0000">"load"</span><span style="color:#990000">,</span> <span style="color:#FF0000">"store"</span><span style="color:#990000">,</span> <span style="color:#FF0000">"add"</span><span style="color:#990000">,</span> <span style="color:#FF0000">"sub"</span><span style="color:#990000">,</span> <span style="color:#FF0000">"and"</span><span style="color:#990000">,</span>
<span style="color:#FF0000">"or"</span><span style="color:#990000">,</span> <span style="color:#FF0000">"xor"</span><span style="color:#990000">,</span> <span style="color:#FF0000">"not"</span><span style="color:#990000">,</span> <span style="color:#FF0000">"nop"</span><span style="color:#990000">,</span> <span style="color:#FF0000">"jmp"</span><span style="color:#990000">,</span> <span style="color:#FF0000">"jmpe"</span><span style="color:#990000">,</span> <span style="color:#FF0000">"jmpl"</span><span style="color:#990000">,</span> <span style="color:#FF0000">"brl"</span>
<span style="color:#FF0000">}</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">static</span></b> <span style="color:#009900">char</span> buf<span style="color:#990000">[</span><span style="color:#993399">256</span><span style="color:#990000">],</span> buf2<span style="color:#990000">[</span><span style="color:#993399">8</span><span style="color:#990000">];</span>
ir2<span style="color:#990000">.</span>buf <span style="color:#990000">=</span> inst<span style="color:#990000">;</span>
<span style="color:#009900">int</span> op <span style="color:#990000">=</span> ir2<span style="color:#990000">.</span>others<span style="color:#990000">.</span>op<span style="color:#990000">;</span>
buf<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">]</span> <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">(</span>op <span style="color:#990000"><=</span> <span style="color:#993399">2</span><span style="color:#990000">)</span> <span style="color:#990000">||</span> <span style="color:#990000">(</span>op <span style="color:#990000">==</span> <span style="color:#993399">10</span><span style="color:#990000">)</span> <span style="color:#990000">||</span> <span style="color:#990000">(</span>op <span style="color:#990000">==</span> <span style="color:#993399">11</span><span style="color:#990000">)</span> <span style="color:#990000">)</span> <i><span style="color:#9A1900">// halt, not, nop, I/O, and shift</span></i>
<b><span style="color:#000000">strcpy</span></b> <span style="color:#990000">(</span>buf<span style="color:#990000">,</span> instnames<span style="color:#990000">[</span>op<span style="color:#990000">].</span><b><span style="color:#000000">c_str</span></b><span style="color:#990000">());</span>
<b><span style="color:#0000FF">else</span></b> <b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> op <span style="color:#990000">>=</span> <span style="color:#993399">3</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#000000">strcpy</span></b> <span style="color:#990000">(</span>buf<span style="color:#990000">,</span> instnames<span style="color:#990000">[</span>ir2<span style="color:#990000">.</span>others<span style="color:#990000">.</span>op<span style="color:#990000">].</span><b><span style="color:#000000">c_str</span></b><span style="color:#990000">());</span>
<b><span style="color:#000000">strcat</span></b> <span style="color:#990000">(</span>buf<span style="color:#990000">,</span> <span style="color:#FF0000">"</span><span style="color:#CC33CC">\t</span><span style="color:#FF0000">"</span><span style="color:#990000">);</span>
<b><span style="color:#000000">sprintf</span></b> <span style="color:#990000">(</span>buf2<span style="color:#990000">,</span> <span style="color:#FF0000">"%.3x"</span><span style="color:#990000">,</span> ir2<span style="color:#990000">.</span>others<span style="color:#990000">.</span>address<span style="color:#990000">);</span>
<b><span style="color:#000000">strcat</span></b> <span style="color:#990000">(</span>buf<span style="color:#990000">,</span> buf2<span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
<b><span style="color:#0000FF">return</span></b> buf<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<span style="color:#009900">void</span> <b><span style="color:#000000">simulateIBCM</span></b><span style="color:#990000">()</span> <span style="color:#FF0000">{</span>
<span style="color:#009900">int</span> acc <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">,</span> pc <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">,</span> incpc<span style="color:#990000">;</span>
<span style="color:#008080">string</span> buf<span style="color:#990000">;</span>
<b><span style="color:#0000FF">while</span></b> <span style="color:#990000">(</span><span style="color:#993399">1</span><span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
acc <span style="color:#990000">&=</span> <span style="color:#993399">0x0000ffff</span><span style="color:#990000">;</span>
incpc <span style="color:#990000">=</span> <span style="color:#993399">1</span><span style="color:#990000">;</span>
ir<span style="color:#990000">.</span>buf <span style="color:#990000">=</span> mem<span style="color:#990000">[</span>pc<span style="color:#990000">];</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> verbose <span style="color:#990000">>=</span> <span style="color:#993399">2</span> <span style="color:#990000">)</span>
<b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">pc = %.3x: ir = %.4x, ibcm = '%s'</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> pc<span style="color:#990000">,</span> ir<span style="color:#990000">.</span>buf<span style="color:#990000">,</span> <b><span style="color:#000000">decodeIBCMInstruction</span></b><span style="color:#990000">(</span>ir<span style="color:#990000">.</span>buf<span style="color:#990000">));</span>
numinst<span style="color:#990000">++;</span>
<b><span style="color:#0000FF">switch</span></b> <span style="color:#990000">(</span>ir<span style="color:#990000">.</span>others<span style="color:#990000">.</span>op<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">case</span></b> <span style="color:#993399">0</span><span style="color:#990000">:</span> <i><span style="color:#9A1900">// halt</span></i>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> verbose <span style="color:#990000">>=</span> <span style="color:#993399">1</span> <span style="color:#990000">)</span> <b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"pc = %.3x: halt</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> pc<span style="color:#990000">);</span>
<b><span style="color:#0000FF">return</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">case</span></b> <span style="color:#993399">1</span><span style="color:#990000">:</span> <i><span style="color:#9A1900">// I/O</span></i>
<b><span style="color:#0000FF">switch</span></b> <span style="color:#990000">(</span> ir<span style="color:#990000">.</span>io<span style="color:#990000">.</span>ioopt <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">case</span></b> <span style="color:#993399">0</span><span style="color:#990000">:</span> <i><span style="color:#9A1900">// read hex</span></i>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> verbose <span style="color:#990000">>=</span> <span style="color:#993399">1</span> <span style="color:#990000">)</span> <b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"pc = %.3x: I/O: read hex</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> pc<span style="color:#990000">);</span>
<b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"Enter hex input: "</span><span style="color:#990000">);</span>
<b><span style="color:#000000">fflush</span></b> <span style="color:#990000">(</span>stdout<span style="color:#990000">);</span>
cin <span style="color:#990000">>></span> buf<span style="color:#990000">;</span>
<b><span style="color:#000000">sscanf</span></b> <span style="color:#990000">(</span>buf<span style="color:#990000">.</span><b><span style="color:#000000">c_str</span></b><span style="color:#990000">(),</span> <span style="color:#FF0000">"%x"</span><span style="color:#990000">,</span> <span style="color:#990000">&</span>acc<span style="color:#990000">);</span>
acc <span style="color:#990000">&=</span> <span style="color:#993399">0x0000ffff</span><span style="color:#990000">;</span>
<b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">);</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">case</span></b> <span style="color:#993399">1</span><span style="color:#990000">:</span> <i><span style="color:#9A1900">// read ascii</span></i>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> verbose <span style="color:#990000">>=</span> <span style="color:#993399">1</span> <span style="color:#990000">)</span> <b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"pc = %.3x: I/O: read ascii</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> pc<span style="color:#990000">);</span>
<b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"Enter ascii input: "</span><span style="color:#990000">);</span>
<b><span style="color:#000000">fflush</span></b> <span style="color:#990000">(</span>stdout<span style="color:#990000">);</span>
cin <span style="color:#990000">>></span> buf<span style="color:#990000">;</span>
acc <span style="color:#990000">=</span> buf<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">];</span>
acc <span style="color:#990000">&=</span> <span style="color:#993399">0x0000ffff</span><span style="color:#990000">;</span>
<b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">);</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">case</span></b> <span style="color:#993399">2</span><span style="color:#990000">:</span> <i><span style="color:#9A1900">// write hex</span></i>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> verbose <span style="color:#990000">>=</span> <span style="color:#993399">1</span> <span style="color:#990000">)</span> <b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"pc = %.3x: I/O: write hex</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> pc<span style="color:#990000">);</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> verbose <span style="color:#990000">>=</span> <span style="color:#993399">1</span> <span style="color:#990000">)</span> <b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"Hex output: "</span><span style="color:#990000">);</span>
<b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"%.4x</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> acc<span style="color:#990000">);</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">case</span></b> <span style="color:#993399">3</span><span style="color:#990000">:</span> <i><span style="color:#9A1900">// write ascii</span></i>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> verbose <span style="color:#990000">>=</span> <span style="color:#993399">1</span> <span style="color:#990000">)</span> <b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"pc = %.3x: I/O: write ascii</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> pc<span style="color:#990000">);</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> verbose <span style="color:#990000">>=</span> <span style="color:#993399">1</span> <span style="color:#990000">)</span> <b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"Ascii output: "</span><span style="color:#990000">);</span>
<b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"%c</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> acc <span style="color:#990000">&</span> <span style="color:#993399">0xff</span><span style="color:#990000">);</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">case</span></b> <span style="color:#993399">2</span><span style="color:#990000">:</span> <i><span style="color:#9A1900">// shifts</span></i>
<b><span style="color:#0000FF">switch</span></b> <span style="color:#990000">(</span> ir<span style="color:#990000">.</span>shifts<span style="color:#990000">.</span>shiftop <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">case</span></b> <span style="color:#993399">0</span><span style="color:#990000">:</span> <i><span style="color:#9A1900">// shift left</span></i>
acc <span style="color:#990000">=</span> <span style="color:#990000">(</span>acc <span style="color:#990000"><<</span> ir<span style="color:#990000">.</span>shifts<span style="color:#990000">.</span>shiftcount<span style="color:#990000">)</span> <span style="color:#990000">&</span> <span style="color:#993399">0xffff</span><span style="color:#990000">;</span>
acc <span style="color:#990000">&=</span> <span style="color:#993399">0x0000ffff</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> verbose <span style="color:#990000">>=</span> <span style="color:#993399">1</span> <span style="color:#990000">)</span> <b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"pc = %.3x: shift left by %d; result is %.4x</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> pc<span style="color:#990000">,</span> ir<span style="color:#990000">.</span>shifts<span style="color:#990000">.</span>shiftop<span style="color:#990000">,</span> acc<span style="color:#990000">);</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">case</span></b> <span style="color:#993399">1</span><span style="color:#990000">:</span> <i><span style="color:#9A1900">// shift right</span></i>
acc <span style="color:#990000">=</span> <span style="color:#990000">(</span>acc <span style="color:#990000">>></span> ir<span style="color:#990000">.</span>shifts<span style="color:#990000">.</span>shiftcount<span style="color:#990000">)</span> <span style="color:#990000">&</span> <span style="color:#990000">(</span><span style="color:#993399">0xffff</span> <span style="color:#990000">>></span> ir<span style="color:#990000">.</span>shifts<span style="color:#990000">.</span>shiftcount<span style="color:#990000">);</span>
acc <span style="color:#990000">&=</span> <span style="color:#993399">0x0000ffff</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> verbose <span style="color:#990000">>=</span> <span style="color:#993399">1</span> <span style="color:#990000">)</span> <b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"pc = %.3x: shift right by %d; result is %.4x</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> pc<span style="color:#990000">,</span> ir<span style="color:#990000">.</span>shifts<span style="color:#990000">.</span>shiftop<span style="color:#990000">,</span> acc<span style="color:#990000">);</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">case</span></b> <span style="color:#993399">2</span><span style="color:#990000">:</span> <i><span style="color:#9A1900">// rotate left</span></i>
acc <span style="color:#990000">=</span> <span style="color:#990000">((</span>acc <span style="color:#990000"><<</span> ir<span style="color:#990000">.</span>shifts<span style="color:#990000">.</span>shiftcount<span style="color:#990000">)</span> <span style="color:#990000">&</span> <span style="color:#993399">0xffff</span><span style="color:#990000">)</span> <span style="color:#990000">|</span>
<span style="color:#990000">((</span>acc <span style="color:#990000">>></span> <span style="color:#990000">(</span><span style="color:#993399">16</span><span style="color:#990000">-</span>ir<span style="color:#990000">.</span>shifts<span style="color:#990000">.</span>shiftcount<span style="color:#990000">))</span> <span style="color:#990000">&</span> <span style="color:#990000">(</span><span style="color:#993399">0xffff</span> <span style="color:#990000">>></span> <span style="color:#990000">(</span><span style="color:#993399">16</span><span style="color:#990000">-</span>ir<span style="color:#990000">.</span>shifts<span style="color:#990000">.</span>shiftcount<span style="color:#990000">)));</span>
acc <span style="color:#990000">&=</span> <span style="color:#993399">0x0000ffff</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> verbose <span style="color:#990000">>=</span> <span style="color:#993399">1</span> <span style="color:#990000">)</span> <b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"pc = %.3x: rotate left by %d; result is %.4x</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> pc<span style="color:#990000">,</span> ir<span style="color:#990000">.</span>shifts<span style="color:#990000">.</span>shiftop<span style="color:#990000">,</span> acc<span style="color:#990000">);</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">case</span></b> <span style="color:#993399">3</span><span style="color:#990000">:</span> <i><span style="color:#9A1900">// rotate right</span></i>
acc <span style="color:#990000">=</span> <span style="color:#990000">((</span>acc <span style="color:#990000">>></span> ir<span style="color:#990000">.</span>shifts<span style="color:#990000">.</span>shiftcount<span style="color:#990000">)</span> <span style="color:#990000">&</span> <span style="color:#990000">(</span><span style="color:#993399">0xffff</span> <span style="color:#990000">>></span> ir<span style="color:#990000">.</span>shifts<span style="color:#990000">.</span>shiftcount<span style="color:#990000">))</span> <span style="color:#990000">|</span>
<span style="color:#990000">((</span>acc <span style="color:#990000"><<</span> <span style="color:#990000">(</span><span style="color:#993399">16</span><span style="color:#990000">-</span>ir<span style="color:#990000">.</span>shifts<span style="color:#990000">.</span>shiftcount<span style="color:#990000">))</span> <span style="color:#990000">&</span> <span style="color:#993399">0xffff</span><span style="color:#990000">);</span>
acc <span style="color:#990000">&=</span> <span style="color:#993399">0x0000ffff</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> verbose <span style="color:#990000">>=</span> <span style="color:#993399">1</span> <span style="color:#990000">)</span> <b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"pc = %.3x: rotate right by %d; result is %.4x</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> pc<span style="color:#990000">,</span> ir<span style="color:#990000">.</span>shifts<span style="color:#990000">.</span>shiftop<span style="color:#990000">,</span> acc<span style="color:#990000">);</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">case</span></b> <span style="color:#993399">3</span><span style="color:#990000">:</span> <i><span style="color:#9A1900">// load</span></i>
acc <span style="color:#990000">=</span> mem<span style="color:#990000">[</span>ir<span style="color:#990000">.</span>others<span style="color:#990000">.</span>address<span style="color:#990000">];</span>
acc <span style="color:#990000">&=</span> <span style="color:#993399">0x0000ffff</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> verbose <span style="color:#990000">>=</span> <span style="color:#993399">1</span> <span style="color:#990000">)</span> <b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"pc = %.3x: load result (@ %.3x) is %.4x</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> pc<span style="color:#990000">,</span> ir<span style="color:#990000">.</span>others<span style="color:#990000">.</span>address<span style="color:#990000">,</span> acc<span style="color:#990000">);</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">case</span></b> <span style="color:#993399">4</span><span style="color:#990000">:</span> <i><span style="color:#9A1900">// store</span></i>
acc <span style="color:#990000">&=</span> <span style="color:#993399">0x0000ffff</span><span style="color:#990000">;</span>
mem<span style="color:#990000">[</span>ir<span style="color:#990000">.</span>others<span style="color:#990000">.</span>address<span style="color:#990000">]</span> <span style="color:#990000">=</span> acc<span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> verbose <span style="color:#990000">>=</span> <span style="color:#993399">1</span> <span style="color:#990000">)</span> <b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"pc = %.3x: store result (@ %.3x) is %.4x</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> pc<span style="color:#990000">,</span> ir<span style="color:#990000">.</span>others<span style="color:#990000">.</span>address<span style="color:#990000">,</span> mem<span style="color:#990000">[</span>ir<span style="color:#990000">.</span>others<span style="color:#990000">.</span>address<span style="color:#990000">]);</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">case</span></b> <span style="color:#993399">5</span><span style="color:#990000">:</span> <i><span style="color:#9A1900">// add</span></i>
acc <span style="color:#990000">+=</span> mem<span style="color:#990000">[</span>ir<span style="color:#990000">.</span>others<span style="color:#990000">.</span>address<span style="color:#990000">];</span>
acc <span style="color:#990000">&=</span> <span style="color:#993399">0x0000ffff</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> verbose <span style="color:#990000">>=</span> <span style="color:#993399">1</span> <span style="color:#990000">)</span> <b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"pc = %.3x: add result is %.4x</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> pc<span style="color:#990000">,</span> acc<span style="color:#990000">);</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">case</span></b> <span style="color:#993399">6</span><span style="color:#990000">:</span> <i><span style="color:#9A1900">// sub</span></i>
acc <span style="color:#990000">-=</span> mem<span style="color:#990000">[</span>ir<span style="color:#990000">.</span>others<span style="color:#990000">.</span>address<span style="color:#990000">];</span>
acc <span style="color:#990000">&=</span> <span style="color:#993399">0x0000ffff</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> verbose <span style="color:#990000">>=</span> <span style="color:#993399">1</span> <span style="color:#990000">)</span> <b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"pc = %.3x: sub result is %.4x</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> pc<span style="color:#990000">,</span> acc<span style="color:#990000">);</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">case</span></b> <span style="color:#993399">7</span><span style="color:#990000">:</span> <i><span style="color:#9A1900">// and</span></i>
acc <span style="color:#990000">&=</span> mem<span style="color:#990000">[</span>ir<span style="color:#990000">.</span>others<span style="color:#990000">.</span>address<span style="color:#990000">];</span>
acc <span style="color:#990000">&=</span> <span style="color:#993399">0x0000ffff</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> verbose <span style="color:#990000">>=</span> <span style="color:#993399">1</span> <span style="color:#990000">)</span> <b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"pc = %.3x: and result is %.4x</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> pc<span style="color:#990000">,</span> acc<span style="color:#990000">);</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">case</span></b> <span style="color:#993399">8</span><span style="color:#990000">:</span> <i><span style="color:#9A1900">// or</span></i>
acc <span style="color:#990000">|=</span> mem<span style="color:#990000">[</span>ir<span style="color:#990000">.</span>others<span style="color:#990000">.</span>address<span style="color:#990000">];</span>
acc <span style="color:#990000">&=</span> <span style="color:#993399">0x0000ffff</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> verbose <span style="color:#990000">>=</span> <span style="color:#993399">1</span> <span style="color:#990000">)</span> <b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"pc = %.3x: or result is %.4x</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> pc<span style="color:#990000">,</span> acc<span style="color:#990000">);</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">case</span></b> <span style="color:#993399">9</span><span style="color:#990000">:</span> <i><span style="color:#9A1900">// xor</span></i>
acc <span style="color:#990000">^=</span> mem<span style="color:#990000">[</span>ir<span style="color:#990000">.</span>others<span style="color:#990000">.</span>address<span style="color:#990000">];</span>
acc <span style="color:#990000">&=</span> <span style="color:#993399">0x0000ffff</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> verbose <span style="color:#990000">>=</span> <span style="color:#993399">1</span> <span style="color:#990000">)</span> <b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"pc = %.3x: xor result is %.4x</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> pc<span style="color:#990000">,</span> acc<span style="color:#990000">);</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">case</span></b> <span style="color:#993399">10</span><span style="color:#990000">:</span> <i><span style="color:#9A1900">// not</span></i>
acc <span style="color:#990000">=</span> <span style="color:#990000">~</span>acc<span style="color:#990000">;</span>
acc <span style="color:#990000">&=</span> <span style="color:#993399">0x0000ffff</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> verbose <span style="color:#990000">>=</span> <span style="color:#993399">1</span> <span style="color:#990000">)</span> <b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"pc = %.3x: not result is %.4x</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> pc<span style="color:#990000">,</span> acc<span style="color:#990000">);</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">case</span></b> <span style="color:#993399">11</span><span style="color:#990000">:</span> <i><span style="color:#9A1900">// nop</span></i>
<i><span style="color:#9A1900">// do nothing</span></i>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> verbose <span style="color:#990000">>=</span> <span style="color:#993399">1</span> <span style="color:#990000">)</span> <b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"pc = %.3x: nop</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> pc<span style="color:#990000">);</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">case</span></b> <span style="color:#993399">12</span><span style="color:#990000">:</span> <i><span style="color:#9A1900">// jmp</span></i>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> verbose <span style="color:#990000">>=</span> <span style="color:#993399">1</span> <span style="color:#990000">)</span> <b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"pc = %.3x: jmp to %.4x</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> pc<span style="color:#990000">,</span> ir<span style="color:#990000">.</span>others<span style="color:#990000">.</span>address<span style="color:#990000">);</span>
pc <span style="color:#990000">=</span> ir<span style="color:#990000">.</span>others<span style="color:#990000">.</span>address<span style="color:#990000">;</span>
incpc <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">case</span></b> <span style="color:#993399">13</span><span style="color:#990000">:</span> <i><span style="color:#9A1900">// jmpe</span></i>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> verbose <span style="color:#990000">>=</span> <span style="color:#993399">1</span> <span style="color:#990000">)</span> <b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"pc = %.3x: jmpe to %.4x</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> pc<span style="color:#990000">,</span> ir<span style="color:#990000">.</span>others<span style="color:#990000">.</span>address<span style="color:#990000">);</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">(</span>acc <span style="color:#990000">&</span> <span style="color:#993399">0x0000ffff</span><span style="color:#990000">)</span> <span style="color:#990000">==</span> <span style="color:#993399">0</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
pc <span style="color:#990000">=</span> ir<span style="color:#990000">.</span>others<span style="color:#990000">.</span>address<span style="color:#990000">;</span>
incpc <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">case</span></b> <span style="color:#993399">14</span><span style="color:#990000">:</span> <i><span style="color:#9A1900">// jmpl</span></i>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> verbose <span style="color:#990000">>=</span> <span style="color:#993399">1</span> <span style="color:#990000">)</span> <b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"pc = %.3x: jmpl to %.3x</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> pc<span style="color:#990000">,</span> ir<span style="color:#990000">.</span>others<span style="color:#990000">.</span>address<span style="color:#990000">);</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">(</span>acc <span style="color:#990000">&</span> <span style="color:#993399">0x00008000</span><span style="color:#990000">)</span> <span style="color:#990000">!=</span> <span style="color:#993399">0</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
pc <span style="color:#990000">=</span> ir<span style="color:#990000">.</span>others<span style="color:#990000">.</span>address<span style="color:#990000">;</span>
incpc <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">case</span></b> <span style="color:#993399">15</span><span style="color:#990000">:</span> <i><span style="color:#9A1900">// brl</span></i>
acc <span style="color:#990000">=</span> pc<span style="color:#990000">+</span><span style="color:#993399">1</span><span style="color:#990000">;</span>
acc <span style="color:#990000">&=</span> <span style="color:#993399">0x0000ffff</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> verbose <span style="color:#990000">>=</span> <span style="color:#993399">1</span> <span style="color:#990000">)</span> <b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"pc = %.3x: brl from %.3x to %.4x</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> pc<span style="color:#990000">,</span> acc<span style="color:#990000">,</span> ir<span style="color:#990000">.</span>others<span style="color:#990000">.</span>address<span style="color:#990000">);</span>
pc <span style="color:#990000">=</span> ir<span style="color:#990000">.</span>others<span style="color:#990000">.</span>address<span style="color:#990000">;</span>
incpc <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> incpc <span style="color:#990000">)</span>
pc<span style="color:#990000">++;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> maxticks <span style="color:#990000">&&</span> <span style="color:#990000">(</span>numinst <span style="color:#990000">>=</span> maxticks<span style="color:#990000">)</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
cerr <span style="color:#990000"><<</span> <span style="color:#FF0000">"Simulation reached the maximum number of "</span> <span style="color:#990000"><<</span> maxticks <span style="color:#990000"><<</span> <span style="color:#FF0000">" instructions to execute."</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"Simulation reached the maximum number of "</span> <span style="color:#990000"><<</span> maxticks <span style="color:#990000"><<</span> <span style="color:#FF0000">" instructions to execute."</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<span style="color:#FF0000">}</span>
<span style="color:#FF0000">}</span>
</pre>
</body>
</html>