-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem-a.html
More file actions
502 lines (422 loc) · 61.8 KB
/
system-a.html
File metadata and controls
502 lines (422 loc) · 61.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
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
494
495
496
497
498
499
500
501
502
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2022-W11-4 02:13 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>system/a</title>
<meta name="author" content="Inanna" />
<meta name="description" content="A simple memory-to-memory virtual machine implemented in C++. (nonfunctional)" />
<meta name="generator" content="Org Mode" />
<link href="/site.css" rel="stylesheet" type="text/css" /><link href="images/website-icon.png" rel="icon" />
</head>
<body>
<div id="preamble" class="status">
<div><a href="/index.html"><img alt="An abstract logo representing a series of three assembly line stamping machines with the words CONS, DEV, and embalzoned in white on each machine." id="site-logo" src="/images/website-logo.png" /></a></div>
</div>
<div id="content" class="content">
<h1 class="title">system/a
<br />
<span class="subtitle">A slightly-less modern computing system.</span>
</h1>
<div id="table-of-contents" role="doc-toc">
<h2>Table of Contents</h2>
<div id="text-table-of-contents" role="doc-toc">
<ul>
<li><a href="#org3e79207">Introduction</a></li>
<li><a href="#org110d859">Source Code</a>
<ul>
<li><a href="#orgfecdeb4">main.cpp</a></li>
<li><a href="#orge5a2cad">systema.h</a></li>
<li><a href="#org812041e">assembler.h</a></li>
<li><a href="#org0f73502">instructions.h</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div id="org604bc86" class="figure">
<p><img src="images/system-a-logo.jpeg" alt="An image showing the system/a logo with the text 'general computing platform' to the right and a core dump of a system/a program below it." />
</p>
</div>
<div id="outline-container-org3e79207" class="outline-2">
<h2 id="org3e79207">Introduction</h2>
<div class="outline-text-2" id="text-org3e79207">
<p>
This was, in essence, the thing that made me realize the beauty of computer science. Yes, it looks incredibly ugly and doesn't work right now, <i>but when it did work</i> it was beyond beautiful. Why? Because in learning how a basic memory-to-memory virtual machine was implemented I realized how elegant and simple computer science is.
</p>
<p>
Yes, there are instruction pipelines, yes, there are other complex things going on, but even with the added complexity the simplicity and beauty of the interfaces stood out. With a continuous block of memory and simple rules you could simulate anything and it was interesting to experience and discover how programming languages worked as well.
</p>
<p>
You can see the fancy manual I made for it <a href="other/operating-manual.pdf">here</a>.
</p>
<p>
Please note that all of this code is terrible and possibly non-functional. I have it here simply because I have fond memories about it.
</p>
</div>
</div>
<div id="outline-container-org110d859" class="outline-2">
<h2 id="org110d859">Source Code</h2>
<div class="outline-text-2" id="text-org110d859">
</div>
<div id="outline-container-orgfecdeb4" class="outline-3">
<h3 id="orgfecdeb4">main.cpp</h3>
<div class="outline-text-3" id="text-orgfecdeb4">
<div class="org-src-container">
<pre class="src src-C++"><span style="color: #000; font-weight: bold;">#include</span> <span style="color: #494949;"><</span><span style="color: #494949;">iostream</span><span style="color: #494949;">></span>
<span style="color: #000; font-weight: bold;">#include</span> <span style="color: #494949;"><</span><span style="color: #494949;">string</span><span style="color: #494949;">></span>
<span style="color: #000; font-weight: bold;">#include</span> <span style="color: #494949;">"assembler.h"</span>
<span style="color: #000; font-weight: bold;">#include</span> <span style="color: #494949;">"systema.h"</span>
<span style="color: #000; font-weight: bold;">#include</span> <span style="color: #494949;">"instructions.h"</span>
<span style="color: #a4a4a4; font-style: italic;">/*</span>
<span style="color: #a4a4a4; font-style: italic;"> system/a</span>
<span style="color: #a4a4a4; font-style: italic;"> this is the program that contains the code for simulating a</span>
<span style="color: #a4a4a4; font-style: italic;"> system/a computer and includes a translator for</span>
<span style="color: #a4a4a4; font-style: italic;"> output, a complete system/a instruction set and a full</span>
<span style="color: #a4a4a4; font-style: italic;"> assembler for converting ASM to machine code.</span>
<span style="color: #a4a4a4; font-style: italic;">*/</span>
<span style="color: #E53935;">int</span> <span style="font-weight: bold; font-style: italic;">main</span><span style="color: #494949;">()</span>
<span style="color: #494949;">{</span>
assembleFile<span style="color: #bbb;">(</span><span style="color: #494949;">"test.saa"</span>, <span style="color: #494949;">"test.sam"</span><span style="color: #bbb;">)</span>;
runSystemAProgram<span style="color: #bbb;">(</span><span style="color: #494949;">"test.sam"</span><span style="color: #bbb;">)</span>;
<span style="color: #E53935;">std</span>::cin.get<span style="color: #bbb;">()</span>;
<span style="color: #E53935; font-style: italic;">return</span> 0;
<span style="color: #494949;">}</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orge5a2cad" class="outline-3">
<h3 id="orge5a2cad">systema.h</h3>
<div class="outline-text-3" id="text-orge5a2cad">
<div class="org-src-container">
<pre class="src src-C++"><span style="color: #000; font-weight: bold;">#pragma</span> once
<span style="color: #000; font-weight: bold;">#include</span> <span style="color: #494949;"><</span><span style="color: #494949;">string</span><span style="color: #494949;">></span>
<span style="color: #000; font-weight: bold;">#include</span> <span style="color: #494949;"><</span><span style="color: #494949;">iostream</span><span style="color: #494949;">></span>
<span style="color: #000; font-weight: bold;">#include</span> <span style="color: #494949;"><</span><span style="color: #494949;">fstream</span><span style="color: #494949;">></span>
<span style="color: #000; font-weight: bold;">#include</span> <span style="color: #494949;"><</span><span style="color: #494949;">sstream</span><span style="color: #494949;">></span>
<span style="color: #000; font-weight: bold;">#include</span> <span style="color: #494949;"><</span><span style="color: #494949;">cmath</span><span style="color: #494949;">></span>
<span style="color: #000; font-weight: bold;">#include</span> <span style="color: #494949;">"instructions.h"</span>
<span style="color: #E53935;">void</span> <span style="font-weight: bold; font-style: italic;">readFileIntoMemory</span><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">std</span>::<span style="color: #E53935;">string</span> <span style="font-style: italic;">inputFile</span>, <span style="color: #E53935;">bool</span><span style="color: #bbb;">(</span>&<span style="font-style: italic;">memory</span><span style="color: #bbb;">)[</span>256<span style="color: #bbb;">][</span>32<span style="color: #bbb;">]</span><span style="color: #494949;">)</span>;
<span style="color: #E53935;">void</span> <span style="font-weight: bold; font-style: italic;">coreDumpToConsole</span><span style="color: #494949;">(</span><span style="color: #E53935;">bool</span> <span style="font-style: italic;">memory</span><span style="color: #bbb;">[</span>256<span style="color: #bbb;">][</span>32<span style="color: #bbb;">]</span>, <span style="color: #E53935;">int</span> <span style="font-style: italic;">programCounter</span><span style="color: #494949;">)</span>;
<span style="color: #E53935;">void</span> <span style="font-weight: bold; font-style: italic;">processCycle</span><span style="color: #494949;">(</span><span style="color: #E53935;">int</span> &<span style="font-style: italic;">programCounter</span>, <span style="color: #E53935;">bool</span><span style="color: #bbb;">(</span><span style="font-style: italic;">inputBuffer</span><span style="color: #bbb;">)[</span>32<span style="color: #bbb;">]</span>, <span style="color: #E53935;">bool</span><span style="color: #bbb;">(</span>&<span style="font-style: italic;">outputBuffer</span><span style="color: #bbb;">)[</span>32<span style="color: #bbb;">]</span>, <span style="color: #E53935;">bool</span><span style="color: #bbb;">(</span>&<span style="font-style: italic;">memory</span><span style="color: #bbb;">)[</span>256<span style="color: #bbb;">][</span>32<span style="color: #bbb;">]</span><span style="color: #494949;">)</span>;
<span style="color: #E53935;">int</span> <span style="font-weight: bold; font-style: italic;">toAddr</span><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">int</span> <span style="font-style: italic;">addrPos</span>, <span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">int</span> <span style="font-style: italic;">programCounter</span>, <span style="color: #E53935;">bool</span> <span style="font-style: italic;">memory</span><span style="color: #bbb;">[</span>256<span style="color: #bbb;">][</span>32<span style="color: #bbb;">]</span><span style="color: #494949;">)</span>;
<span style="color: #E53935;">int</span> <span style="font-weight: bold; font-style: italic;">addrToDec</span><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">int</span> <span style="font-style: italic;">addr</span>, <span style="color: #E53935;">bool</span> <span style="font-style: italic;">memory</span><span style="color: #bbb;">[</span>256<span style="color: #bbb;">][</span>32<span style="color: #bbb;">]</span><span style="color: #494949;">)</span>;
<span style="color: #E53935;">void</span> <span style="font-weight: bold; font-style: italic;">readFileIntoMemory</span><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">std</span>::<span style="color: #E53935;">string</span> <span style="font-style: italic;">inputFile</span>, <span style="color: #E53935;">bool</span><span style="color: #bbb;">(</span>&<span style="font-style: italic;">memory</span><span style="color: #bbb;">)[</span>256<span style="color: #bbb;">][</span>32<span style="color: #bbb;">]</span><span style="color: #494949;">)</span>
<span style="color: #494949;">{</span>
<span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">int</span> <span style="font-style: italic;">ASCIIOffset</span> = 48;
<span style="color: #E53935;">int</span> <span style="font-style: italic;">lineNum</span> = 0;
<span style="color: #E53935;">std</span>::<span style="color: #E53935;">ifstream</span> <span style="font-style: italic;">machineCode</span><span style="color: #bbb;">(</span>inputFile<span style="color: #bbb;">)</span>;
<span style="color: #E53935;">std</span>::<span style="color: #E53935;">string</span> <span style="font-style: italic;">line</span>;
<span style="color: #E53935; font-style: italic;">for</span> <span style="color: #bbb;">(</span><span style="color: #E53935;">int</span> <span style="font-style: italic;">lineNum</span>= 0; lineNum < 256; lineNum++<span style="color: #bbb;">)</span>
<span style="color: #bbb;">{</span>
<span style="color: #E53935;">std</span>::getline<span style="color: #494949;">(</span>machineCode, line<span style="color: #494949;">)</span>;
<span style="color: #E53935; font-style: italic;">for</span> <span style="color: #494949;">(</span><span style="color: #E53935;">int</span> <span style="font-style: italic;">CharcterNum</span> = 0; CharcterNum < line.length<span style="color: #bbb;">()</span>; CharcterNum++<span style="color: #494949;">)</span>
<span style="color: #494949;">{</span>
<span style="color: #E53935;">int</span> <span style="font-style: italic;">numChar</span> = line<span style="color: #bbb;">[</span>CharcterNum<span style="color: #bbb;">]</span>;
numChar = numChar - ASCIIOffset;
<span style="color: #E53935;">bool</span> <span style="font-style: italic;">slot</span> = numChar;
memory <span style="color: #bbb;">[</span>lineNum<span style="color: #bbb;">][</span>CharcterNum<span style="color: #bbb;">]</span> = slot;
<span style="color: #494949;">}</span>
<span style="color: #bbb;">}</span>
<span style="color: #494949;">}</span>
<span style="color: #a4a4a4; font-style: italic;">//</span><span style="color: #a4a4a4; font-style: italic;">this prints the state of the machine to the console</span>
<span style="color: #E53935;">void</span> <span style="font-weight: bold; font-style: italic;">coreDumpToConsole</span><span style="color: #494949;">(</span><span style="color: #E53935;">bool</span> <span style="font-style: italic;">memory</span><span style="color: #bbb;">[</span>256<span style="color: #bbb;">][</span>32<span style="color: #bbb;">]</span>, <span style="color: #E53935;">int</span> <span style="font-style: italic;">programCounter</span><span style="color: #494949;">)</span>
<span style="color: #494949;">{</span>
<span style="color: #E53935;">std</span>::cout << <span style="color: #494949;">"CURRENT INSTRUCTION ADDR :"</span> << programCounter << <span style="color: #E53935;">std</span>::endl;
<span style="color: #E53935;">std</span>::cout << <span style="color: #494949;">"CORE DUMP OUTPUT : "</span> << <span style="color: #E53935;">std</span>::endl;
<span style="color: #E53935; font-style: italic;">for</span> <span style="color: #bbb;">(</span><span style="color: #E53935;">int</span> <span style="font-style: italic;">rowNum</span> = 0; rowNum < 16; rowNum++<span style="color: #bbb;">)</span>
<span style="color: #bbb;">{</span>
<span style="color: #E53935; font-style: italic;">for</span> <span style="color: #494949;">(</span><span style="color: #E53935;">int</span> <span style="font-style: italic;">columnNum</span> = 0; columnNum < 32; columnNum++<span style="color: #494949;">)</span>
<span style="color: #494949;">{</span>
<span style="color: #E53935;">int</span> <span style="font-style: italic;">slot</span> = memory<span style="color: #bbb;">[</span>rowNum<span style="color: #bbb;">][</span>columnNum<span style="color: #bbb;">]</span>;
<span style="color: #E53935;">std</span>::cout << slot;
<span style="color: #494949;">}</span>
<span style="color: #E53935;">std</span>::cout << <span style="color: #E53935;">std</span>::endl;
<span style="color: #bbb;">}</span>
<span style="color: #494949;">}</span>
<span style="color: #E53935;">void</span> <span style="font-weight: bold; font-style: italic;">printOutBuf</span><span style="color: #494949;">(</span><span style="color: #E53935;">bool</span> <span style="font-style: italic;">outputBuffer</span><span style="color: #bbb;">[</span>32<span style="color: #bbb;">]</span><span style="color: #494949;">)</span>
<span style="color: #494949;">{</span>
<span style="color: #E53935;">long</span> <span style="color: #E53935;">long</span> <span style="font-style: italic;">value</span> = 0;
<span style="color: #E53935;">long</span> <span style="color: #E53935;">long</span> <span style="font-style: italic;">positionVal</span> = 0;
<span style="color: #E53935; font-style: italic;">for</span> <span style="color: #bbb;">(</span><span style="color: #E53935;">int</span> <span style="font-style: italic;">bit</span> = 31; bit > 0; bit--<span style="color: #bbb;">)</span>
<span style="color: #bbb;">{</span>
value += outputBuffer<span style="color: #494949;">[</span>bit<span style="color: #494949;">]</span> ? pow<span style="color: #494949;">(</span>2, positionVal<span style="color: #494949;">)</span> : 0;
positionVal++;
<span style="color: #bbb;">}</span>
<span style="color: #E53935;">std</span>::cout << value;
<span style="color: #494949;">}</span>
<span style="color: #E53935;">void</span> <span style="font-weight: bold; font-style: italic;">runSystemAProgram</span><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">std</span>::<span style="color: #E53935;">string</span> <span style="font-style: italic;">inputFile</span><span style="color: #494949;">)</span>
<span style="color: #494949;">{</span>
<span style="color: #E53935;">bool</span> <span style="font-style: italic;">inputBuffer</span> <span style="color: #bbb;">[</span>32<span style="color: #bbb;">]</span>;
<span style="color: #E53935;">bool</span> <span style="font-style: italic;">outputBuffer</span> <span style="color: #bbb;">[</span>32<span style="color: #bbb;">]</span>;
<span style="color: #E53935;">bool</span> <span style="font-style: italic;">memory</span> <span style="color: #bbb;">[</span>256<span style="color: #bbb;">][</span>32<span style="color: #bbb;">]</span>;
<span style="color: #E53935;">bool</span> <span style="font-style: italic;">STP</span> = <span style="color: #E53935;">false</span>;
<span style="color: #E53935;">int</span> <span style="font-style: italic;">programCounter</span> = 0;
readFileIntoMemory<span style="color: #bbb;">(</span>inputFile, memory<span style="color: #bbb;">)</span>;
<span style="color: #E53935; font-style: italic;">while</span> <span style="color: #bbb;">(</span>STP == <span style="color: #E53935;">false</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">{</span>
processCycle<span style="color: #494949;">(</span>programCounter, inputBuffer, outputBuffer, memory<span style="color: #494949;">)</span>;
<span style="color: #E53935; font-style: italic;">if</span> <span style="color: #494949;">(</span>programCounter < 0<span style="color: #494949;">)</span>
STP = <span style="color: #E53935;">true</span>;
coreDumpToConsole<span style="color: #494949;">(</span>memory, programCounter<span style="color: #494949;">)</span>; <span style="color: #E53935;">std</span>::cin.get<span style="color: #494949;">()</span>;
<span style="color: #bbb;">}</span>
printOutBuf<span style="color: #bbb;">(</span>outputBuffer<span style="color: #bbb;">)</span>;
<span style="color: #494949;">}</span>
<span style="color: #a4a4a4; font-style: italic;">// </span><span style="color: #a4a4a4; font-style: italic;">this simulates a operating cycle of system/a </span>
<span style="color: #E53935;">void</span> <span style="font-weight: bold; font-style: italic;">processCycle</span><span style="color: #494949;">(</span><span style="color: #E53935;">int</span> &<span style="font-style: italic;">programCounter</span>, <span style="color: #E53935;">bool</span> <span style="color: #bbb;">(</span><span style="font-style: italic;">inputBuffer</span><span style="color: #bbb;">)[</span>32<span style="color: #bbb;">]</span>, <span style="color: #E53935;">bool</span> <span style="color: #bbb;">(</span>&<span style="font-style: italic;">outputBuffer</span><span style="color: #bbb;">)[</span>32<span style="color: #bbb;">]</span>, <span style="color: #E53935;">bool</span> <span style="color: #bbb;">(</span>&<span style="font-style: italic;">memory</span><span style="color: #bbb;">)[</span>256<span style="color: #bbb;">][</span>32<span style="color: #bbb;">]</span><span style="color: #494949;">)</span>
<span style="color: #494949;">{</span>
<span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">bool</span> <span style="font-style: italic;">iP0</span> = memory<span style="color: #bbb;">[</span>programCounter<span style="color: #bbb;">][</span>4<span style="color: #bbb;">]</span>;
<span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">bool</span> <span style="font-style: italic;">iP1</span> = memory<span style="color: #bbb;">[</span>programCounter<span style="color: #bbb;">][</span>5<span style="color: #bbb;">]</span>;
<span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">bool</span> <span style="font-style: italic;">iP2</span> = memory<span style="color: #bbb;">[</span>programCounter<span style="color: #bbb;">][</span>6<span style="color: #bbb;">]</span>;
<span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">bool</span> <span style="font-style: italic;">iP3</span> = memory<span style="color: #bbb;">[</span>programCounter<span style="color: #bbb;">][</span>7<span style="color: #bbb;">]</span>;
<span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">int</span> <span style="font-style: italic;">argAddr1</span> = toAddr<span style="color: #bbb;">(</span>0, programCounter, memory<span style="color: #bbb;">)</span>;
<span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">int</span> <span style="font-style: italic;">argAddr2</span> = toAddr<span style="color: #bbb;">(</span>1, programCounter, memory<span style="color: #bbb;">)</span>;
<span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">int</span> <span style="font-style: italic;">tgtAddr</span> = toAddr<span style="color: #bbb;">(</span>2, programCounter, memory<span style="color: #bbb;">)</span>;
<span style="color: #E53935;">std</span>::cout << <span style="color: #494949;">"ProgCount : "</span> << programCounter << <span style="color: #494949;">" AA1 : "</span> << argAddr1 << <span style="color: #494949;">" AA2 : "</span> << argAddr2 << <span style="color: #494949;">" TGA : "</span> << tgtAddr << <span style="color: #494949;">" iP0 : "</span> << iP0 << <span style="color: #494949;">" iP1 : "</span> << iP1 << <span style="color: #494949;">" iP2 : "</span> << iP2 << <span style="color: #494949;">" iP3 : "</span> << iP3 << <span style="color: #E53935;">std</span>::endl;
<span style="color: #a4a4a4; font-style: italic;">// </span><span style="color: #a4a4a4; font-style: italic;">FLP 00001101</span>
<span style="color: #E53935; font-style: italic;">if</span> <span style="color: #bbb;">(</span>iP0 && iP1 && <span style="color: #000; font-weight: bold;">!</span>iP2 && iP3<span style="color: #bbb;">)</span>
FLP<span style="color: #bbb;">(</span>argAddr1, programCounter, memory<span style="color: #bbb;">)</span>;
<span style="color: #a4a4a4; font-style: italic;">// </span><span style="color: #a4a4a4; font-style: italic;">ADD 00000010</span>
<span style="color: #E53935; font-style: italic;">else</span> <span style="color: #E53935; font-style: italic;">if</span> <span style="color: #bbb;">(</span><span style="color: #000; font-weight: bold;">!</span>iP0 && <span style="color: #000; font-weight: bold;">!</span>iP1 && iP2 && <span style="color: #000; font-weight: bold;">!</span>iP3<span style="color: #bbb;">)</span>
ADD<span style="color: #bbb;">(</span>argAddr1, argAddr2, tgtAddr, programCounter, memory<span style="color: #bbb;">)</span>;
<span style="color: #a4a4a4; font-style: italic;">// </span><span style="color: #a4a4a4; font-style: italic;">JMP 00001010</span>
<span style="color: #E53935; font-style: italic;">else</span> <span style="color: #E53935; font-style: italic;">if</span> <span style="color: #bbb;">(</span>iP0 && <span style="color: #000; font-weight: bold;">!</span>iP1 && iP2 && <span style="color: #000; font-weight: bold;">!</span>iP3<span style="color: #bbb;">)</span>
JMP<span style="color: #bbb;">(</span>tgtAddr, programCounter, memory<span style="color: #bbb;">)</span>;
<span style="color: #a4a4a4; font-style: italic;">// </span><span style="color: #a4a4a4; font-style: italic;">JIE 00000101</span>
<span style="color: #E53935; font-style: italic;">else</span> <span style="color: #E53935; font-style: italic;">if</span> <span style="color: #bbb;">(</span><span style="color: #000; font-weight: bold;">!</span>iP0 && iP1 && <span style="color: #000; font-weight: bold;">!</span>iP2 && iP3<span style="color: #bbb;">)</span>
JIE<span style="color: #bbb;">(</span>argAddr1, argAddr2, tgtAddr, programCounter, memory<span style="color: #bbb;">)</span>;
<span style="color: #a4a4a4; font-style: italic;">// </span><span style="color: #a4a4a4; font-style: italic;">WRT 00000011</span>
<span style="color: #E53935; font-style: italic;">else</span> <span style="color: #E53935; font-style: italic;">if</span> <span style="color: #bbb;">(</span><span style="color: #000; font-weight: bold;">!</span>iP0 && <span style="color: #000; font-weight: bold;">!</span>iP1 && iP2 && iP3<span style="color: #bbb;">)</span>
WRT<span style="color: #bbb;">(</span>tgtAddr, outputBuffer, programCounter, memory<span style="color: #bbb;">)</span>;
<span style="color: #a4a4a4; font-style: italic;">// </span><span style="color: #a4a4a4; font-style: italic;">RED 00001100</span>
<span style="color: #E53935; font-style: italic;">else</span> <span style="color: #E53935; font-style: italic;">if</span> <span style="color: #bbb;">(</span>iP0 && iP1 && <span style="color: #000; font-weight: bold;">!</span>iP2 && <span style="color: #000; font-weight: bold;">!</span>iP3<span style="color: #bbb;">)</span>
RED<span style="color: #bbb;">(</span>tgtAddr, inputBuffer, programCounter, memory<span style="color: #bbb;">)</span>;
<span style="color: #a4a4a4; font-style: italic;">// </span><span style="color: #a4a4a4; font-style: italic;">MOV 00000100 -> this moves the </span>
<span style="color: #E53935; font-style: italic;">else</span> <span style="color: #E53935; font-style: italic;">if</span> <span style="color: #bbb;">(</span><span style="color: #000; font-weight: bold;">!</span>iP0 && iP1 && <span style="color: #000; font-weight: bold;">!</span>iP2 && <span style="color: #000; font-weight: bold;">!</span>iP3<span style="color: #bbb;">)</span>
MOV<span style="color: #bbb;">(</span>argAddr1, tgtAddr, programCounter, memory<span style="color: #bbb;">)</span>;
<span style="color: #a4a4a4; font-style: italic;">// </span><span style="color: #a4a4a4; font-style: italic;">STP 00001111 -> this stops the machine</span>
<span style="color: #E53935; font-style: italic;">else</span> <span style="color: #E53935; font-style: italic;">if</span> <span style="color: #bbb;">(</span>iP0 && iP1 && iP2 && iP3<span style="color: #bbb;">)</span> <span style="color: #bbb;">{</span>
STP<span style="color: #494949;">(</span>programCounter<span style="color: #494949;">)</span>;
<span style="color: #bbb;">}</span>
<span style="color: #a4a4a4; font-style: italic;">// </span><span style="color: #a4a4a4; font-style: italic;">if given illegal opcode the system ignores it and increments the program counter</span>
<span style="color: #E53935; font-style: italic;">else</span> <span style="color: #bbb;">{</span>
programCounter++;
<span style="color: #bbb;">}</span>
<span style="color: #494949;">}</span>
<span style="color: #E53935;">int</span> <span style="font-weight: bold; font-style: italic;">toAddr</span><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">int</span> <span style="font-style: italic;">addrPos</span>, <span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">int</span> <span style="font-style: italic;">programCounter</span>, <span style="color: #E53935;">bool</span> <span style="font-style: italic;">memory</span><span style="color: #bbb;">[</span>256<span style="color: #bbb;">][</span>32<span style="color: #bbb;">]</span><span style="color: #494949;">)</span>
<span style="color: #494949;">{</span>
<span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">int</span> <span style="font-style: italic;">addrPosF</span> = <span style="color: #bbb;">(</span>addrPos * 8<span style="color: #bbb;">)</span> + 8;
<span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">int</span> <span style="font-style: italic;">p0</span> = <span style="color: #bbb;">(</span>memory<span style="color: #494949;">[</span>programCounter<span style="color: #494949;">][</span>addrPosF + 0<span style="color: #494949;">]</span><span style="color: #bbb;">)</span> ? 128 : 0;
<span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">int</span> <span style="font-style: italic;">p1</span> = <span style="color: #bbb;">(</span>memory<span style="color: #494949;">[</span>programCounter<span style="color: #494949;">][</span>addrPosF + 1<span style="color: #494949;">]</span><span style="color: #bbb;">)</span> ? 64 : 0;
<span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">int</span> <span style="font-style: italic;">p2</span> = <span style="color: #bbb;">(</span>memory<span style="color: #494949;">[</span>programCounter<span style="color: #494949;">][</span>addrPosF + 2<span style="color: #494949;">]</span><span style="color: #bbb;">)</span> ? 32 : 0;
<span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">int</span> <span style="font-style: italic;">p3</span> = <span style="color: #bbb;">(</span>memory<span style="color: #494949;">[</span>programCounter<span style="color: #494949;">][</span>addrPosF + 3<span style="color: #494949;">]</span><span style="color: #bbb;">)</span> ? 16 : 0;
<span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">int</span> <span style="font-style: italic;">p4</span> = <span style="color: #bbb;">(</span>memory<span style="color: #494949;">[</span>programCounter<span style="color: #494949;">][</span>addrPosF + 4<span style="color: #494949;">]</span><span style="color: #bbb;">)</span> ? 8 : 0;
<span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">int</span> <span style="font-style: italic;">p5</span> = <span style="color: #bbb;">(</span>memory<span style="color: #494949;">[</span>programCounter<span style="color: #494949;">][</span>addrPosF + 5<span style="color: #494949;">]</span><span style="color: #bbb;">)</span> ? 4 : 0;
<span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">int</span> <span style="font-style: italic;">p6</span> = <span style="color: #bbb;">(</span>memory<span style="color: #494949;">[</span>programCounter<span style="color: #494949;">][</span>addrPosF + 6<span style="color: #494949;">]</span><span style="color: #bbb;">)</span> ? 2 : 0;
<span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">int</span> <span style="font-style: italic;">p7</span> = <span style="color: #bbb;">(</span>memory<span style="color: #494949;">[</span>programCounter<span style="color: #494949;">][</span>addrPosF + 7<span style="color: #494949;">]</span><span style="color: #bbb;">)</span> ? 1 : 0;
<span style="color: #E53935; font-style: italic;">return</span> p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7;
<span style="color: #494949;">}</span>
<span style="color: #E53935;">int</span> <span style="font-weight: bold; font-style: italic;">addrToDec</span><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">int</span> <span style="font-style: italic;">addr</span>, <span style="color: #E53935;">bool</span> <span style="font-style: italic;">memory</span><span style="color: #bbb;">[</span>256<span style="color: #bbb;">][</span>32<span style="color: #bbb;">]</span><span style="color: #494949;">)</span>
<span style="color: #494949;">{</span>
<span style="color: #E53935;">long</span> <span style="color: #E53935;">long</span> <span style="font-style: italic;">value</span> = 0;
<span style="color: #E53935; font-style: italic;">for</span> <span style="color: #bbb;">(</span><span style="color: #E53935;">int</span> <span style="font-style: italic;">bit</span> = 31; bit > 0; bit--<span style="color: #bbb;">)</span>
<span style="color: #bbb;">{</span>
value += memory<span style="color: #494949;">[</span>addr<span style="color: #494949;">][</span>bit<span style="color: #494949;">]</span> ? pow<span style="color: #494949;">(</span>2, bit<span style="color: #494949;">)</span> : 0;
<span style="color: #bbb;">}</span>
<span style="color: #E53935; font-style: italic;">return</span> value;
<span style="color: #494949;">}</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org812041e" class="outline-3">
<h3 id="org812041e">assembler.h</h3>
<div class="outline-text-3" id="text-org812041e">
<div class="org-src-container">
<pre class="src src-C++"><span style="color: #000; font-weight: bold;">#pragma</span> once
<span style="color: #000; font-weight: bold;">#include</span> <span style="color: #494949;"><</span><span style="color: #494949;">string</span><span style="color: #494949;">></span>
<span style="color: #000; font-weight: bold;">#include</span> <span style="color: #494949;"><</span><span style="color: #494949;">fstream</span><span style="color: #494949;">></span>
<span style="color: #000; font-weight: bold;">#include</span> <span style="color: #494949;"><</span><span style="color: #494949;">sstream</span><span style="color: #494949;">></span>
<span style="color: #000; font-weight: bold;">#include</span> <span style="color: #494949;"><</span><span style="color: #494949;">bitset</span><span style="color: #494949;">></span>
<span style="color: #000; font-weight: bold;">#include</span> <span style="color: #494949;"><</span><span style="color: #494949;">map</span><span style="color: #494949;">></span>
<span style="color: #E53935;">int</span> <span style="font-weight: bold; font-style: italic;">assembleFile</span><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">std</span>::<span style="color: #E53935;">string</span> <span style="font-style: italic;">inputFile</span>, <span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">std</span>::<span style="color: #E53935;">string</span> <span style="font-style: italic;">outputFile</span><span style="color: #494949;">)</span>;
<span style="color: #E53935;">std</span>::<span style="color: #E53935;">string</span> <span style="font-weight: bold; font-style: italic;">assembleLine</span><span style="color: #494949;">(</span><span style="color: #E53935;">std</span>::<span style="color: #E53935;">string</span> <span style="font-style: italic;">asmLine</span><span style="color: #494949;">)</span>;
<span style="color: #E53935;">int</span> <span style="font-weight: bold; font-style: italic;">hexToDec</span><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">std</span>::<span style="color: #E53935;">string</span> <span style="font-style: italic;">hexNum</span><span style="color: #494949;">)</span>;
<span style="color: #a4a4a4; font-style: italic;">// </span><span style="color: #a4a4a4; font-style: italic;">this assembles a system/a assembly file and converts it into a system/a machine code file</span>
<span style="color: #E53935;">int</span> <span style="font-weight: bold; font-style: italic;">assembleFile</span><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">std</span>::<span style="color: #E53935;">string</span> <span style="font-style: italic;">inputFile</span>, <span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">std</span>::<span style="color: #E53935;">string</span> <span style="font-style: italic;">outputFile</span><span style="color: #494949;">)</span>
<span style="color: #494949;">{</span>
<span style="color: #E53935;">std</span>::<span style="color: #E53935;">string</span> <span style="font-style: italic;">machineLine</span>, <span style="font-style: italic;">machineCode</span>;
<span style="color: #E53935;">std</span>::<span style="color: #E53935;">ifstream</span> <span style="font-style: italic;">assembly</span><span style="color: #bbb;">(</span>inputFile<span style="color: #bbb;">)</span>;
<span style="color: #E53935; font-style: italic;">for</span> <span style="color: #bbb;">(</span><span style="color: #E53935;">std</span>::<span style="color: #E53935;">string</span> <span style="font-style: italic;">line</span>; <span style="color: #E53935;">std</span>::getline<span style="color: #494949;">(</span>assembly, line<span style="color: #494949;">)</span>;<span style="color: #bbb;">)</span> <span style="color: #bbb;">{</span>
machineLine = assembleLine<span style="color: #494949;">(</span>line<span style="color: #494949;">)</span>;
machineCode += <span style="color: #494949;">(</span>machineLine + <span style="color: #494949;">"\n"</span><span style="color: #494949;">)</span>;
<span style="color: #bbb;">}</span>
<span style="color: #E53935;">std</span>::<span style="color: #E53935;">ofstream</span> <span style="font-style: italic;">machineCodeFile</span>;
machineCodeFile.open<span style="color: #bbb;">(</span>outputFile<span style="color: #bbb;">)</span>;
machineCodeFile << machineCode;
machineCodeFile.close<span style="color: #bbb;">()</span>;
<span style="color: #E53935; font-style: italic;">return</span> 0;
<span style="color: #494949;">}</span>
<span style="color: #a4a4a4; font-style: italic;">// </span><span style="color: #a4a4a4; font-style: italic;">this reads lines of fixed-width assembly and converts them into binary machine code</span>
<span style="color: #E53935;">std</span>::<span style="color: #E53935;">string</span> <span style="font-weight: bold; font-style: italic;">assembleLine</span><span style="color: #494949;">(</span><span style="color: #E53935;">std</span>::<span style="color: #E53935;">string</span> <span style="font-style: italic;">asmLine</span><span style="color: #494949;">)</span>
<span style="color: #494949;">{</span>
<span style="color: #E53935;">int</span> <span style="font-style: italic;">position</span> = 0;
<span style="color: #E53935;">std</span>::<span style="color: #E53935;">stringstream</span> <span style="font-style: italic;">asmStream</span><span style="color: #bbb;">(</span>asmLine<span style="color: #bbb;">)</span>;
<span style="color: #E53935;">std</span>::<span style="color: #E53935;">string</span> <span style="font-style: italic;">token</span>;
<span style="color: #E53935;">std</span>::<span style="color: #E53935;">string</span> <span style="font-style: italic;">machineLine</span>;
<span style="color: #E53935;">bool</span> <span style="font-style: italic;">NUMMarker</span> = <span style="color: #E53935;">false</span>;
<span style="color: #E53935;">std</span>::<span style="color: #E53935;">map</span><span style="color: #bbb;"><</span><span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">std</span>::<span style="color: #E53935;">string</span>, <span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">std</span>::<span style="color: #E53935;">string</span><span style="color: #bbb;">></span> <span style="font-style: italic;">instructions</span> = <span style="color: #bbb;">{</span>
<span style="color: #494949;">{</span> <span style="color: #494949;">"FLP"</span>, <span style="color: #494949;">"00001101"</span> <span style="color: #494949;">}</span>,<span style="color: #494949;">{</span> <span style="color: #494949;">"ADD"</span>, <span style="color: #494949;">"00000010"</span> <span style="color: #494949;">}</span>,<span style="color: #494949;">{</span> <span style="color: #494949;">"JMP"</span>, <span style="color: #494949;">"000010100000000000000000"</span> <span style="color: #494949;">}</span>,<span style="color: #494949;">{</span> <span style="color: #494949;">"JIE"</span> , <span style="color: #494949;">"00000101"</span> <span style="color: #494949;">}</span>,
<span style="color: #494949;">{</span> <span style="color: #494949;">"WRT"</span> , <span style="color: #494949;">"000000110000000000000000"</span> <span style="color: #494949;">}</span>,<span style="color: #494949;">{</span> <span style="color: #494949;">"RED"</span> , <span style="color: #494949;">"000011000000000000000000"</span> <span style="color: #494949;">}</span>,
<span style="color: #494949;">{</span> <span style="color: #494949;">"MOV"</span>, <span style="color: #494949;">"00000100"</span> <span style="color: #494949;">}</span>,<span style="color: #494949;">{</span> <span style="color: #494949;">"STP"</span>, <span style="color: #494949;">"00001111000000000000000000000000"</span> <span style="color: #494949;">}</span> <span style="color: #bbb;">}</span>;
<span style="color: #E53935; font-style: italic;">while</span> <span style="color: #bbb;">(</span>getline<span style="color: #494949;">(</span>asmStream, token, <span style="color: #494949;">' '</span><span style="color: #494949;">)</span> && <span style="color: #494949;">(</span>machineLine.length<span style="color: #bbb;">()</span> < 32<span style="color: #494949;">)</span> && <span style="color: #494949;">(</span>token != <span style="color: #494949;">";"</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">{</span>
<span style="color: #E53935; font-style: italic;">if</span> <span style="color: #494949;">(</span>NUMMarker == <span style="color: #E53935;">true</span><span style="color: #494949;">)</span> <span style="color: #494949;">{</span>
<span style="color: #E53935;">std</span>::<span style="color: #E53935;">bitset</span><span style="color: #bbb;"><</span>32<span style="color: #bbb;">></span> <span style="font-style: italic;">Binary</span><span style="color: #bbb;">(</span>hexToDec<span style="color: #494949;">(</span>token<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>;
machineLine += Binary.to_string<span style="color: #bbb;">()</span>;
<span style="color: #494949;">}</span>
<span style="color: #E53935; font-style: italic;">else</span> <span style="color: #E53935; font-style: italic;">if</span> <span style="color: #494949;">(</span>instructions.find<span style="color: #bbb;">(</span>token<span style="color: #bbb;">)</span> != instructions.end<span style="color: #bbb;">()</span><span style="color: #494949;">)</span>
machineLine += instructions.find<span style="color: #494949;">(</span>token<span style="color: #494949;">)</span>->second;
<span style="color: #E53935; font-style: italic;">else</span> <span style="color: #E53935; font-style: italic;">if</span> <span style="color: #494949;">(</span>token == <span style="color: #494949;">"NUM"</span><span style="color: #494949;">)</span>
NUMMarker = <span style="color: #E53935;">true</span>;
<span style="color: #E53935; font-style: italic;">else</span> <span style="color: #E53935; font-style: italic;">if</span> <span style="color: #494949;">(</span>token != <span style="color: #494949;">""</span><span style="color: #494949;">)</span> <span style="color: #494949;">{</span>
<span style="color: #E53935;">std</span>::<span style="color: #E53935;">bitset</span><span style="color: #bbb;"><</span>8<span style="color: #bbb;">></span> <span style="font-style: italic;">Binary</span><span style="color: #bbb;">(</span>hexToDec<span style="color: #494949;">(</span>token<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>;
machineLine += Binary.to_string<span style="color: #bbb;">()</span>;
<span style="color: #494949;">}</span>
<span style="color: #bbb;">}</span>
<span style="color: #E53935; font-style: italic;">while</span> <span style="color: #bbb;">(</span>machineLine.length<span style="color: #494949;">()</span> < 32<span style="color: #bbb;">)</span>
<span style="color: #bbb;">{</span>
machineLine += <span style="color: #494949;">"00000000"</span>;
<span style="color: #bbb;">}</span>
<span style="color: #E53935; font-style: italic;">return</span> machineLine;
<span style="color: #494949;">}</span>
<span style="color: #a4a4a4; font-style: italic;">// </span><span style="color: #a4a4a4; font-style: italic;">this converts intigers to decimal values</span>
<span style="color: #E53935;">int</span> <span style="font-weight: bold; font-style: italic;">hexToDec</span><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">const</span> <span style="color: #E53935;">std</span>::<span style="color: #E53935;">string</span> <span style="font-style: italic;">hexNum</span><span style="color: #494949;">)</span>
<span style="color: #494949;">{</span>
<span style="color: #E53935;">std</span>::<span style="color: #E53935;">stringstream</span> <span style="font-style: italic;">numStream</span>;
numStream << <span style="color: #E53935;">std</span>::hex << hexNum;
<span style="color: #E53935;">int</span> <span style="font-style: italic;">decimalNum</span>;
numStream >> decimalNum;
<span style="color: #E53935; font-style: italic;">return</span> decimalNum;
<span style="color: #494949;">}</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org0f73502" class="outline-3">
<h3 id="org0f73502">instructions.h</h3>
<div class="outline-text-3" id="text-org0f73502">
<div class="org-src-container">
<pre class="src src-C++">once
<span style="color: #a4a4a4; font-style: italic;">// </span><span style="color: #a4a4a4; font-style: italic;">this flips the bits of a memory address</span>
<span style="color: #E53935;">void</span> <span style="font-weight: bold; font-style: italic;">FLP</span><span style="color: #494949;">(</span><span style="color: #E53935;">int</span> <span style="font-style: italic;">argAddr1</span>, <span style="color: #E53935;">int</span> &<span style="font-style: italic;">programCounter</span>, <span style="color: #E53935;">bool</span> <span style="color: #bbb;">(</span>&<span style="font-style: italic;">memory</span><span style="color: #bbb;">)[</span>256<span style="color: #bbb;">][</span>32<span style="color: #bbb;">]</span><span style="color: #494949;">)</span>
<span style="color: #494949;">{</span>
<span style="color: #E53935; font-style: italic;">for</span> <span style="color: #bbb;">(</span><span style="color: #E53935;">int</span> <span style="font-style: italic;">bit</span> = 0; bit < 32; bit++<span style="color: #bbb;">)</span>
<span style="color: #bbb;">{</span>
memory<span style="color: #494949;">[</span>argAddr1<span style="color: #494949;">][</span>bit<span style="color: #494949;">]</span> = <span style="color: #000; font-weight: bold;">!</span>memory<span style="color: #494949;">[</span>argAddr1<span style="color: #494949;">][</span>bit<span style="color: #494949;">]</span>;
<span style="color: #bbb;">}</span>
programCounter++;
<span style="color: #494949;">}</span>
<span style="color: #a4a4a4; font-style: italic;">// </span><span style="color: #a4a4a4; font-style: italic;">this adds the number in argAddr1 to argAddr2 and places it in the tgtAddr</span>
<span style="color: #E53935;">void</span> <span style="font-weight: bold; font-style: italic;">ADD</span><span style="color: #494949;">(</span><span style="color: #E53935;">int</span> <span style="font-style: italic;">argAddr1</span>, <span style="color: #E53935;">int</span> <span style="font-style: italic;">argAddr2</span>, <span style="color: #E53935;">int</span> <span style="font-style: italic;">tgtAddr</span>, <span style="color: #E53935;">int</span> &<span style="font-style: italic;">programCounter</span>, <span style="color: #E53935;">bool</span><span style="color: #bbb;">(</span>&<span style="font-style: italic;">memory</span><span style="color: #bbb;">)[</span>256<span style="color: #bbb;">][</span>32<span style="color: #bbb;">]</span><span style="color: #494949;">)</span>
<span style="color: #494949;">{</span>
<span style="color: #E53935;">bool</span> <span style="font-style: italic;">carry</span> = <span style="color: #E53935;">false</span>;
<span style="color: #E53935;">bool</span> <span style="font-style: italic;">contArgAddr1</span>;
<span style="color: #E53935;">bool</span> <span style="font-style: italic;">contArgAddr2</span>;
<span style="color: #E53935; font-style: italic;">for</span> <span style="color: #bbb;">(</span><span style="color: #E53935;">int</span> <span style="font-style: italic;">bit</span> = 31; bit > 0; bit--<span style="color: #bbb;">)</span>
<span style="color: #bbb;">{</span>
contArgAddr1 = memory<span style="color: #494949;">[</span>argAddr1<span style="color: #494949;">][</span>bit<span style="color: #494949;">]</span>;
contArgAddr2 = memory<span style="color: #494949;">[</span>argAddr2<span style="color: #494949;">][</span>bit<span style="color: #494949;">]</span>;
<span style="color: #E53935; font-style: italic;">if</span> <span style="color: #494949;">(</span>contArgAddr1 && contArgAddr2 && carry<span style="color: #494949;">)</span>
<span style="color: #494949;">{</span>
memory<span style="color: #bbb;">[</span>tgtAddr<span style="color: #bbb;">][</span>bit<span style="color: #bbb;">]</span> = 1;
carry = 1;
<span style="color: #494949;">}</span>
<span style="color: #E53935; font-style: italic;">else</span> <span style="color: #E53935; font-style: italic;">if</span> <span style="color: #494949;">(</span><span style="color: #bbb;">(</span><span style="color: #494949;">(</span>contArgAddr1 && contArgAddr2<span style="color: #494949;">)</span> && <span style="color: #000; font-weight: bold;">!</span>carry<span style="color: #bbb;">)</span> || <span style="color: #bbb;">(</span><span style="color: #494949;">(</span>contArgAddr1 && carry<span style="color: #494949;">)</span> && <span style="color: #000; font-weight: bold;">!</span>contArgAddr2<span style="color: #bbb;">)</span> || <span style="color: #bbb;">(</span><span style="color: #494949;">(</span>contArgAddr2 && carry<span style="color: #494949;">)</span> && <span style="color: #000; font-weight: bold;">!</span>contArgAddr1<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">{</span>
memory<span style="color: #bbb;">[</span>tgtAddr<span style="color: #bbb;">][</span>bit<span style="color: #bbb;">]</span> = 0;
carry = 1;
<span style="color: #494949;">}</span>
<span style="color: #E53935; font-style: italic;">else</span> <span style="color: #E53935; font-style: italic;">if</span> <span style="color: #494949;">(</span><span style="color: #bbb;">(</span>contArgAddr1 && <span style="color: #000; font-weight: bold;">!</span>contArgAddr2 && <span style="color: #000; font-weight: bold;">!</span>carry<span style="color: #bbb;">)</span> || <span style="color: #bbb;">(</span><span style="color: #000; font-weight: bold;">!</span>contArgAddr1 && contArgAddr2 && <span style="color: #000; font-weight: bold;">!</span>carry<span style="color: #bbb;">)</span> || <span style="color: #bbb;">(</span><span style="color: #000; font-weight: bold;">!</span>contArgAddr1 && <span style="color: #000; font-weight: bold;">!</span>contArgAddr2 && carry<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">{</span>
memory<span style="color: #bbb;">[</span>tgtAddr<span style="color: #bbb;">][</span>bit<span style="color: #bbb;">]</span> = 1;
carry = 0;
<span style="color: #494949;">}</span>
<span style="color: #E53935; font-style: italic;">else</span>
<span style="color: #494949;">{</span>
memory<span style="color: #bbb;">[</span>tgtAddr<span style="color: #bbb;">][</span>bit<span style="color: #bbb;">]</span> = 0;
carry = 0;
<span style="color: #494949;">}</span>
<span style="color: #bbb;">}</span>
programCounter++;
<span style="color: #494949;">}</span>
<span style="color: #a4a4a4; font-style: italic;">// </span><span style="color: #a4a4a4; font-style: italic;">this unconditionally sets the program counter to the target address</span>
<span style="color: #E53935;">void</span> <span style="font-weight: bold; font-style: italic;">JMP</span><span style="color: #494949;">(</span><span style="color: #E53935;">int</span> <span style="font-style: italic;">tgtAddr</span>, <span style="color: #E53935;">int</span> &<span style="font-style: italic;">programCounter</span>, <span style="color: #E53935;">bool</span><span style="color: #bbb;">(</span>&<span style="font-style: italic;">memory</span><span style="color: #bbb;">)[</span>256<span style="color: #bbb;">][</span>32<span style="color: #bbb;">]</span><span style="color: #494949;">)</span>
<span style="color: #494949;">{</span>
programCounter = tgtAddr;
<span style="color: #494949;">}</span>
<span style="color: #a4a4a4; font-style: italic;">// </span><span style="color: #a4a4a4; font-style: italic;">this sets the program counter to the target address if argAddr1 and argAddr2 is equal</span>
<span style="color: #E53935;">void</span> <span style="font-weight: bold; font-style: italic;">JIE</span><span style="color: #494949;">(</span><span style="color: #E53935;">int</span> <span style="font-style: italic;">argAddr1</span>, <span style="color: #E53935;">int</span> <span style="font-style: italic;">argAddr2</span>, <span style="color: #E53935;">int</span> <span style="font-style: italic;">tgtAddr</span>, <span style="color: #E53935;">int</span> &<span style="font-style: italic;">programCounter</span>, <span style="color: #E53935;">bool</span><span style="color: #bbb;">(</span>&<span style="font-style: italic;">memory</span><span style="color: #bbb;">)[</span>256<span style="color: #bbb;">][</span>32<span style="color: #bbb;">]</span><span style="color: #494949;">)</span>
<span style="color: #494949;">{</span>
<span style="color: #E53935;">bool</span> <span style="font-style: italic;">equal</span> = <span style="color: #E53935;">true</span>;
<span style="color: #E53935;">int</span> <span style="font-style: italic;">bit</span> = 0;
<span style="color: #E53935; font-style: italic;">while</span> <span style="color: #bbb;">(</span><span style="color: #494949;">(</span>bit < 32<span style="color: #494949;">)</span> && equal<span style="color: #bbb;">)</span>
<span style="color: #bbb;">{</span>
<span style="color: #E53935; font-style: italic;">if</span> <span style="color: #494949;">(</span>memory<span style="color: #bbb;">[</span>argAddr1<span style="color: #bbb;">][</span>bit<span style="color: #bbb;">]</span> != memory<span style="color: #bbb;">[</span>argAddr2<span style="color: #bbb;">][</span>bit<span style="color: #bbb;">]</span><span style="color: #494949;">)</span>
equal = <span style="color: #E53935;">false</span>;
bit++;
<span style="color: #bbb;">}</span>
<span style="color: #E53935; font-style: italic;">if</span> <span style="color: #bbb;">(</span>equal<span style="color: #bbb;">)</span>
programCounter = tgtAddr;
<span style="color: #E53935; font-style: italic;">else</span>
programCounter++;
<span style="color: #494949;">}</span>
<span style="color: #a4a4a4; font-style: italic;">// </span><span style="color: #a4a4a4; font-style: italic;">this writes the contents of tgtAddr to the output buffer</span>
<span style="color: #E53935;">void</span> <span style="font-weight: bold; font-style: italic;">WRT</span><span style="color: #494949;">(</span><span style="color: #E53935;">int</span> <span style="font-style: italic;">tgtAddr</span>, <span style="color: #E53935;">bool</span> <span style="color: #bbb;">(</span>&<span style="font-style: italic;">outputBuffer</span><span style="color: #bbb;">)[</span>32<span style="color: #bbb;">]</span>, <span style="color: #E53935;">int</span> &<span style="font-style: italic;">programCounter</span>, <span style="color: #E53935;">bool</span><span style="color: #bbb;">(</span>&<span style="font-style: italic;">memory</span><span style="color: #bbb;">)[</span>256<span style="color: #bbb;">][</span>32<span style="color: #bbb;">]</span><span style="color: #494949;">)</span>
<span style="color: #494949;">{</span>
<span style="color: #E53935; font-style: italic;">for</span> <span style="color: #bbb;">(</span><span style="color: #E53935;">int</span> <span style="font-style: italic;">bit</span> = 0; bit < 32; bit++<span style="color: #bbb;">)</span>
<span style="color: #bbb;">{</span>
outputBuffer<span style="color: #494949;">[</span>bit<span style="color: #494949;">]</span> = memory<span style="color: #494949;">[</span>tgtAddr<span style="color: #494949;">]</span> <span style="color: #494949;">[</span>bit<span style="color: #494949;">]</span>;
<span style="color: #bbb;">}</span>
programCounter++;
<span style="color: #494949;">}</span>
<span style="color: #a4a4a4; font-style: italic;">// </span><span style="color: #a4a4a4; font-style: italic;">this writes the contents of the input buffer to tgtAddr</span>
<span style="color: #E53935;">void</span> <span style="font-weight: bold; font-style: italic;">RED</span><span style="color: #494949;">(</span><span style="color: #E53935;">int</span> <span style="font-style: italic;">tgtAddr</span>, <span style="color: #E53935;">bool</span> <span style="color: #bbb;">(</span><span style="font-style: italic;">inputBuffer</span><span style="color: #bbb;">)[</span>32<span style="color: #bbb;">]</span>, <span style="color: #E53935;">int</span> &<span style="font-style: italic;">programCounter</span>, <span style="color: #E53935;">bool</span><span style="color: #bbb;">(</span>&<span style="font-style: italic;">memory</span><span style="color: #bbb;">)[</span>256<span style="color: #bbb;">][</span>32<span style="color: #bbb;">]</span><span style="color: #494949;">)</span>
<span style="color: #494949;">{</span>
<span style="color: #E53935; font-style: italic;">for</span> <span style="color: #bbb;">(</span><span style="color: #E53935;">int</span> <span style="font-style: italic;">bit</span> = 0; bit < 32; bit++<span style="color: #bbb;">)</span>
<span style="color: #bbb;">{</span>
memory<span style="color: #494949;">[</span>tgtAddr<span style="color: #494949;">][</span>bit<span style="color: #494949;">]</span> = inputBuffer<span style="color: #494949;">[</span>bit<span style="color: #494949;">]</span>;
<span style="color: #E53935;">int</span> <span style="font-style: italic;">bitVal</span> = memory<span style="color: #494949;">[</span>tgtAddr<span style="color: #494949;">][</span>bit<span style="color: #494949;">]</span>;
memory<span style="color: #494949;">[</span>tgtAddr<span style="color: #494949;">][</span>bit<span style="color: #494949;">]</span> = 0;
<span style="color: #bbb;">}</span>
programCounter++;
<span style="color: #494949;">}</span>
<span style="color: #a4a4a4; font-style: italic;">// </span><span style="color: #a4a4a4; font-style: italic;">this copies the value contained in argAddr1 to tgtAddr</span>
<span style="color: #E53935;">void</span> <span style="font-weight: bold; font-style: italic;">MOV</span><span style="color: #494949;">(</span><span style="color: #E53935;">int</span> <span style="font-style: italic;">argAddr1</span>, <span style="color: #E53935;">int</span> <span style="font-style: italic;">tgtAddr</span>, <span style="color: #E53935;">int</span> &<span style="font-style: italic;">programCounter</span>, <span style="color: #E53935;">bool</span><span style="color: #bbb;">(</span>&<span style="font-style: italic;">memory</span><span style="color: #bbb;">)[</span>256<span style="color: #bbb;">][</span>32<span style="color: #bbb;">]</span><span style="color: #494949;">)</span>
<span style="color: #494949;">{</span>
<span style="color: #E53935; font-style: italic;">for</span> <span style="color: #bbb;">(</span><span style="color: #E53935;">int</span> <span style="font-style: italic;">bit</span> = 0; bit < 32; bit++<span style="color: #bbb;">)</span>
<span style="color: #bbb;">{</span>
memory<span style="color: #494949;">[</span>tgtAddr<span style="color: #494949;">][</span>bit<span style="color: #494949;">]</span> = memory<span style="color: #494949;">[</span>argAddr1<span style="color: #494949;">][</span>bit<span style="color: #494949;">]</span>;
<span style="color: #bbb;">}</span>
programCounter++;
<span style="color: #494949;">}</span>
<span style="color: #a4a4a4; font-style: italic;">// </span><span style="color: #a4a4a4; font-style: italic;">this sets the program counter to an illegal value, causing the computer to stop and return the STP code</span>
<span style="color: #E53935;">void</span> <span style="font-weight: bold; font-style: italic;">STP</span><span style="color: #494949;">(</span><span style="color: #E53935;">int</span> &<span style="font-style: italic;">programCounter</span><span style="color: #494949;">)</span>
<span style="color: #494949;">{</span>
programCounter = -1;
<span style="color: #494949;">}</span>
</pre>
</div>
</div>
</div>
</div>
</div>
<div id="postamble" class="status">
<p class="date">Last Modified: 2021-W52-2 00:21</p><p class="creator">Generated Using: <a href="https://www.gnu.org/software/emacs/">Emacs</a> 27.2 (<a href="https://orgmode.org">Org</a> mode 9.4.6)</p><p class="license">Except where otherwise noted content on <a href="https://cons.dev">cons.dev</a> is licensed under a <a href="https://creativecommons.org/licenses/by-sa/4.0/" rel="license">Creative Commons Attribution-ShareAlike 4.0 International License</a>.</p>
</div>
</body>
</html>