forked from Wellingborough/LMC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
framework.html
454 lines (407 loc) · 19 KB
/
framework.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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LMC Simulator v1.0</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tabulator/5.1.8/css/tabulator_midnight.min.css">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<script src="js/framework.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tabulator/5.1.8/js/tabulator.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.4.0/jspdf.umd.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.5.20/jspdf.plugin.autotable.min.js"></script>
<script src="https://kit.fontawesome.com/0b212d21ae.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="css/framework.css">
</head>
<body onresize="resizeEvent()" onload="filltable()">
<div class="header">
<p>LMC Simulator</p>
<div style="margin-left: auto;">
<button id="save-btn" class="icon-button" title="Save program to file" onclick="saveCode()"><i class="fa-solid fa-file-arrow-down fa-lg"></i></button>
<button id="hide-load-btn" class="icon-button" title="Load program from file" onclick="document.getElementById('load-btn').click()"><i class="fa-solid fa-file-arrow-up fa-lg"></i></button>
<button id="print-btn" class="icon-button" title="Print program to PDF" onclick="printCode()"><i class="fa-solid fa-print fa-lg"></i></button>
<button id="about-modal-btn" class="icon-button" title="About"><i class="fa-solid fa-circle-info fa-lg"></i></button>
<button id="settings-modal-btn" class="icon-button" title="Settings"><i class="fa-solid fa-gear fa-lg"></i></button>
</div>
</div>
<div class="codeEditor" id="codetableholder" style="display:flex; flex-direction:column; margin:0px; max-height:85vh;">
<div class="row content" id="code-table" style="flex: 0 1 auto;" ></div>
<div class="footer" id="code-table-buttons" class="footer">
<input id="load-btn" type="file" style="display:none;">
<button id="assemble-button" class="button" onclick="assembleCode()">Assemble</button>
<button id="clear-button" onclick="clearCode()" class="button">Clear</button>
</div>
</div>
<div class="cpuSchematic" id="processor-schematic">
<canvas id="processor-canvas">Your browser does not support the HTML5 canvas</canvas>
</div>
<div class="memory" id="memory-table">
<p>Memory</p>
</div>
<div class="controls">
<button id="run-btn" class="button" onclick="runCode()">Run</button>
<button id="stop-btn" class="button" onclick="allStop()">Stop</button>
<button id="resume-btn" class="button" onclick="allAhead()">Resume</button>
<button id="next-btn" class="button" onclick="stepCode()">Next</button>
<p id="state-label" style="margin:0px;">Ready</p>
</div>
<div class="ioConsole">
<div id="io-div" style="display:flex; flex-direction:column; margin:0px;">
<div id="input-div" style="background-color: #ee99ee;padding:8px; align-items: center;">
<p id="output-holder">
<span style="background-color: #ee99ee; color: #000000; padding: 8px;">Input:</span>
<input type="number" id="input-text" min="0" max="999" >
<button onclick="processInput()" id="input-button">Enter</button>
</p>
</div>
<div id="output-div" style="flex: 1; background-color: #ee99ee;padding:8px;"><p style="margin:0px;">
<span style="background-color: #ee99ee; color: #000000; padding: 8px;">Output:</span>
<textarea id="output-text" rows="8" readonly="true"></textarea>
</div>
</div>
<div id="log-div" style="background-color: #ffbbff;padding:8px;display:flex; flex-direction:column;">
<div style="margin:0px; height:100%; box-sizing: border-box;">
<span style="background-color: #ffbbff; color: #000000; padding: 8px;">Console:</span>
<textarea id="log-text" rows="4" readonly="true"></textarea>
</div>
</div>
</div>
<!-- Settings modal -->
<div class="modal" id="settings-modal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<i class="fa-solid fa-gear fa-2x" style="margin: 8px 8px;"></i>
<button class="icon-button" id="settings-close-btn" style="float: right;"onclick="cancelSettings()"><i class="fa-solid fa-circle-xmark fa-2x"></i></button>
<h4 class="modal-title" style="margin-left: 8px;">LMC Simulation Settings</h4>
</div>
<div class="modal-body">
<input type="checkbox" name="showDataFlows" value="1" checked id="setting-show-data-flows" style="margin:8px;">
<label for="showDataFlows">Show data flows around the CPU during program execution</label><br>
<input type="checkbox" name="showVariableValuesInCode" value="1" checked id="setting-show-variable-values" style="margin:8px;">
<label for="showDataFlows">Show variable values in the code table during program execution</label><br>
<input type="checkbox" name="showMemoryAccess" value="1" checked id="setting-show-memory-access" style="margin:8px;">
<label for="showMemoryAccess">Show memory read/write locations during program execution</label><br><br>
<input type="range" name="executionSpeed" id="setting-speed" min="0" max="4" step="1" value="2" onchange="changeSetting('speed')">
<label for="setting-speed">Execution Speed</label><br><br>
<p id="speed-display"><i class="fa-regular fa-hand"></i>Run at Medium speed - about 0.5 seconds per cycle</p>
</div>
<div class="modal-footer">
<button id="settings-cancel-btn" class="button" onclick="cancelSettings()">Cancel</button>
<button id="settings-save-btn" class="button" onclick="saveSettings()">Save</button>
</div>
</div>
</div>
</div>
<!-- Examples modal -->
<div class="modal" id="examples-modal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<i class="fa-solid fa-file-code fa-2x" style="margin: 8px 8px;"></i>
<button class="icon-button" id="examples-close-btn" style="float: right;"><i class="fa-solid fa-circle-xmark fa-2x"></i></button>
<h4 class="modal-title" style="margin-left: 8px;">LMC Example Programs</h4>
</div>
<div class="modal-body">
<p>Select an example to load:</p>
<div>
<input type="radio" id="example1" name="example_select" value=1 checked>
<label for="example1">Example 1 - Add two numbers together</label>
</div>
<div>
<input type="radio" id="example2" name="example_select" value=2>
<label for="example2">Example 2 - Output a pattern of 1s and 0s</label>
</div>
<div>
<input type="radio" id="example3" name="example_select" value=3>
<label for="example3">Example 3 - Calculate the square of a number</label>
</div>
<div>
<input type="radio" id="example4" name="example_select" value=4>
<label for="example4">Example 4 - Integer division</label>
</div>
</div>
<div class="modal-footer" style="margin-top:20px;">
<button id="examples-close-btn" class="button" onclick="closeExamples()">Cancel</button>
<button id="examples-load-btn" class="button" onclick="loadExample()">Load</button>
</div>
</div>
</div>
</div>
<!-- Help modal -->
<div class="modal" id="help-modal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<i class="fa-solid fa-pen-to-square fa-2x" style="margin: 8px 8px;"></i>
<button class="icon-button" id="help-close-btn" style="float: right;"><i class="fa-solid fa-circle-xmark fa-2x"></i></button>
<h4 class="modal-title" style="margin-left: 8px;">LMC Editor Help</h4>
</div>
<div class="modal-body">
<p>
The Code Editor is based on a grid system, with columns for you to enter
labels, operators and operands.
The left-most column is used to show which line of code is being executed when the program
is running, and the 'Line' column shows non-editable line numbers.
</p>
<p>
You can move between lines and columns by pressing the Tab key, or by using the cursor keys.
</p>
<p>
Ctrl-Z and Ctrl-Y will undo and redo the last edit, but only if you are not editing a cell in the Code Editor.
</p>
<p>
Select a line by left-clicking anywhere on the line. The line will be highlighted.
</p>
<p>
You can delete the current line using the Del or Delete key - all of the subsequent lines will be moved up.
</p>
<p>
Pressing the Ins or Insert key will create a new line above the current line, moving all subsequent lines down.
</p>
<p>
When you are happy with your code, press the Assemble button, and the LMC Simulator will check and
assemble your code into memory. If there are any errors, a message will appear in the Console window
indicating the line and problem.
</p>
<p>
The Load button can be used to load pre-written programs from your computer.
</p>
<p>
The Save button will save a text file on your computer containing the current program.
</p>
<p>
The Print button will create and download a PDF version of the current program.
</p>
</div>
<div class="modal-footer">
<button id="help-close-btn" class="button" onclick="closeHelp()">Close</button>
</div>
</div>
</div>
</div>
<!-- About modal -->
<div class="modal" id="about-modal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<i class="fa-solid fa-circle-info fa-2x" style="margin: 8px 8px;"></i>
<button class="icon-button" id="about-close-btn" style="float: right;"><i class="fa-solid fa-circle-xmark fa-2x"></i></button>
<h4 class="modal-title" style="margin-left: 8px;">About the Little Man Computer</h4>
</div>
<div class="modal-body">
<h3>An Introduction to the LMC</h3>
<p>
The Little Man Computer (LMC) is a simple model of a processor, based on von Neumann's architecture.
</p>
<h3>The LMC Simulator</h3>
<p>
This web app simulates the LMC, so that you can write programs in the LMC language
and then assemble and run them. The simulator will show you the state of the main
registers in the processor, and the contents of memory.
</p>
<p>
The flow of data around the LMC is shown, and you can pause and resume the execution of your
program at any time.
</p>
<p>
To learn more about how the editor works, just press the 'Help' button below the code window.
</p>
<p>
There are a number of example programs that you can try out - just press the 'Examples' button
below for more information.
</p>
</div>
<div class="modal-footer">
<button id="rabout-close-btn" class="button" onclick="closeAbout()">Close</button>
<button id="instruction-set-modal-btn" class="button">Instruction Set</button>
<button id="examples-modal-btn" class="button">Examples</button>
<button id="help-modal-btn" class="button">Editor Help</button>
</div>
</div>
</div>
</div>
<!-- Instruction Set modal -->
<div class="modal" id="instruction-set-modal">
<div class="modal-content">
<div class="modal-header">
<i class="fa-solid fa-table-list fa-2x" style="margin: 8px 8px;"></i>
<button class="icon-button" id="instruction-set-close-btn" style="float: right;"><i class="fa-solid fa-circle-xmark fa-2x"></i></button>
<h4 class="modal-title" style="margin-left: 8px;">Little Man Computer Instruction Set</h4>
</div>
<div>
<table id="instruction-set-table">
<tr>
<th>Instruction</th>
<th>Mnemonic</th>
<th>Machine Code</th>
<th>Explanation</th>
</tr>
<tr>
<td>Load Accumulator</td>
<td>LDA</td>
<td>5xx</td>
<td>Load the contents of the given memory address (xx) into the Accumulator</td>
</tr>
<tr>
<td>Store Accumulator</td>
<td>STA</td>
<td>3xx</td>
<td>Store the contents of the Accumulator at the given memory address (xx)</td>
</tr>
<tr>
<td>Add</td>
<td>ADD</td>
<td>1xx</td>
<td>Add the contents of the given memory address (xx) to the value in the Accumulator</td>
</tr>
<tr>
<td>Subtract</td>
<td>SUB</td>
<td>2xx</td>
<td>Subtract the contents of the given memory address (xx) from the value in the Accumulator</td>
</tr>
<tr>
<td>Input</td>
<td>INP</td>
<td>901</td>
<td>Copy the value from the Input 'mailbox' into the Accumulator</td>
</tr>
<tr>
<td>Output</td>
<td>OUT</td>
<td>902</td>
<td>Copy the value from the Accumulator into the Output 'mailbox'</td>
</tr>
<tr>
<td>Branch</td>
<td>BRA</td>
<td>6xx</td>
<td>Branch (jump) to the instruction at the given memory address (xx)</td>
</tr>
<tr>
<td>Branch if positive</td>
<td>BRP</td>
<td>8xx</td>
<td>If the value in the Accumulator is positive (including zero), then branch to the instruction at the given memory address (xx)</td>
</tr>
<tr>
<td>Branch if zero</td>
<td>BRZ</td>
<td>7xx</td>
<td>If the value in the Accumulator is zero, then branch to the instruction at the given memory address (xx)</td>
</tr>
<tr>
<td>Halt</td>
<td>HLT</td>
<td>000</td>
<td>Stop execution of the program</td>
</tr>
<tr>
<td>Data</td>
<td>DAT</td>
<td>value of the data</td>
<td>Indicates that the current memory address holds a data value</td>
</tr>
</table>
</div>
<div class="modal-footer">
<button id="rinstruction-set-close-btn" class="button">Close</button>
</div>
</div>
</div>
<script>
let settingsModalBtn = document.getElementById("settings-modal-btn");
let settingsModal = document.getElementById("settings-modal");
let settingsCloseBtn = document.getElementById("settings-close-btn");
settingsModalBtn.onclick = function(){
settingsModal.style.display = "block";
stashOldSettings();
}
settingsCloseBtn.onclick = function(){
settingsModal.style.display = "none";
}
window.onclick = function(e){
if(e.target == settingsModal){
settingsModal.style.display = "none";
} else if(e.target == aboutModal){
aboutModal.style.display = "none";
}
}
let examplesModalBtn = document.getElementById("examples-modal-btn");
let examplesModal = document.getElementById("examples-modal");
let examplesCloseBtn = document.getElementById("examples-close-btn");
examplesModalBtn.onclick = function(){
aboutModal.style.display = "none";
examplesModal.style.display = "block";
}
examplesCloseBtn.onclick = function(){
examplesModal.style.display = "none";
}
window.onclick = function(e){
if(e.target == settingsModal){
settingsModal.style.display = "none";
} else if(e.target == aboutModal){
aboutModal.style.display = "none";
} else if(e.target == examplesModal){
examplesModal.style.display = "none";
}
}
let instructionSetModalBtn = document.getElementById("instruction-set-modal-btn");
let instructionSetModal = document.getElementById("instruction-set-modal");
let instructionSetCloseBtn = document.getElementById("instruction-set-close-btn");
let instructionSetRCloseBtn = document.getElementById("rinstruction-set-close-btn");
instructionSetModalBtn.onclick = function(){
aboutModal.style.display = "none";
instructionSetModal.style.display = "block";
}
instructionSetCloseBtn.onclick = function(){
instructionSetModal.style.display = "none";
}
instructionSetRCloseBtn.onclick = function(){
instructionSetModal.style.display = "none";
}
//
// Foundation work for clickable processor schematic
// Hook the mouse event (should do touch too) and convert
// window coordinates to canvas coordinates
// Call canvasHitCheck() in the pile of JavaScript
// to figure out what they have clicked on
//
document.getElementById('processor-canvas').onclick=function(e){
let rect = document.getElementById('processor-canvas').getBoundingClientRect();
canvasHitCheck(e.clientX - rect.left, e.clientY - rect.top);
}
let aboutModalBtn = document.getElementById("about-modal-btn");
let aboutModal = document.getElementById("about-modal");
let aboutCloseBtn = document.getElementById("about-close-btn");
aboutModalBtn.onclick = function(){
aboutModal.style.display = "block";
}
aboutCloseBtn.onclick = function(){
aboutModal.style.display = "none";
}
let helpModalBtn = document.getElementById("help-modal-btn");
let helpModal = document.getElementById("help-modal");
let helpCloseBtn = document.getElementById("help-close-btn");
helpModalBtn.onclick = function(){
aboutModal.style.display = "none";
helpModal.style.display = "block";
}
helpCloseBtn.onclick = function(){
helpModal.style.display = "none";
}
<!-- Handle return being pressed in the Input box -->
document.getElementById('input-text').onkeypress=function(e){
if (e.keyCode==13){
document.getElementById('input-button').click();
}
}
<!-- also temp and doesn't seem to work! -->
let canvasElement = document.getElementById("processor-schematic")
canvasElement.addEventListener("resize", debouncedResize );
function debouncedResize() {
console.log("Event fired");
}
</script>
</body>
</html>