-
Notifications
You must be signed in to change notification settings - Fork 3
/
lmsm.html
230 lines (226 loc) · 9.22 KB
/
lmsm.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LMSM Emulator</title>
<link rel="stylesheet" href="https://unpkg.com/[email protected]">
<link rel="stylesheet" href="https://unpkg.com/[email protected]/prism">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.11/codemirror.min.css" integrity="sha512-uf06llspW44/LZpHzHT6qBOIVODjWtv4MxCricRxkzvopAlSWnTf6hpZTFxuuZcuNE9CBQhqE0Seu1CoRk84nQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="lmsm.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.11/codemirror.min.js" integrity="sha512-rdFIN28+neM8H8zNsjRClhJb1fIYby2YCNmoqwnqBDEvZgpcp7MJiX8Wd+Oi6KcJOMOuvGztjrsI59rly9BsVQ==" crossorigin="anonymous" referrerpolicy="no-referrer" defer></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.11/addon/mode/simple.min.js" integrity="sha512-CGM6DWPHs250F/m90YZ9NEiEUhd9a4+u8wAzeKC6uHzZbYyt9/e2dLC5BGGB6Y0HtEdZQdSDYjDsoTyNGdMrMA==" crossorigin="anonymous" referrerpolicy="no-referrer" defer></script>
<script src="https://unpkg.com/[email protected]"></script>
<script src="//unpkg.com/alpinejs" defer></script>
<script src="lmsm.js"></script>
</head>
<body class="padding riobeachonsummer" x-data="{ lmsm: new LittleManStackMachine() }">
<div class="grid">
<div id="titleBox" class="box center" data-cols="1 3" data-rows="1">
<h2 class="allcaps">Little Man Stack Machine Emulator</h2>
</div>
<div id="codeEditorBox" class="box" data-cols="1" data-rows="2 5" x-init="
CodeMirror.defineSimpleMode('lmsm-assembly', {
start: [
{
regex: /(?:DAT|ADD|SUB|STA|LDI|LDA|BRA|BRZ|BRP|NOOP|INP|OUT|SPUSH|SPOP|SDUP|SDROP|SSWAP|SADD|SSUB|SMUL|SDIV|SMAX|SMIN|JAL|RET|HLT|dat|add|sub|sta|ldi|lda|bra|brz|brp|noop|inp|out|spush|spop|sdup|sdrop|sswap|sadd|ssub|smul|sdiv|smax|smin|jal|ret|hlt)\b/,
token: 'keyword'
},
{
regex: /\d\d/,
token: 'number',
},
],
});
CodeMirror.defineSimpleMode('firth', {
start: [
{
regex: /(?:def|loop|get|else|stop|var|return|swap|end|dup|do)\b/,
token: 'keyword'
},
{
regex: /zero\?/,
token: 'keyword',
},
{
regex: /asm/,
token: 'keyword',
mode: {spec: 'lmsm-assembly', end: /end/}
},
{
regex: /\*/,
token: 'operator'
},
{
regex: /\d\d/,
token: 'number',
},
],
});
window.editor = CodeMirror(document.querySelector('#codeEditor'), {
lineNumbers: true,
tabSize: 2,
});
window.editor.setSize('100%', '50em');
window.editor.setOption('mode', mode);
" x-data="{
mode: 'firth',
currentLineNumber: -1,
setMode() {
window.editor.setOption('mode', this.mode);
},
}">
<strong class="allcaps block titlebar">IDE</strong>
<label for="mode">
<select name="mode" x-model="mode" @change="setMode()">
<option value="lmsm-assembly">Assembly</option>
<option value="firth">Firth</option>
</select>
</label>
<section class="tool-bar" x-data="{
compile() {
this.lmsm = new LittleManStackMachine();
if(this.currentLineNumber !== -1) {
window.editor.getDoc().removeLineClass(this.currentLineNumber, 'background', 'markCode');
}
this.currentLineNumber = -1;
code = window.editor.getValue();
compiled = this.lmsm.compile(code);
assembled = this.lmsm.assemble(compiled);
this.lmsm.load(assembled);
},
assemble() {
this.lmsm = new LittleManStackMachine();
if(this.currentLineNumber !== -1) {
window.editor.getDoc().removeLineClass(this.currentLineNumber, 'background', 'markCode');
}
this.currentLineNumber = -1;
code = window.editor.getValue();
assembled = this.lmsm.assemble(code);
this.lmsm.load(assembled);
},
run() {
this.lmsm.run();
},
}">
<label x-show="mode === 'firth'">
<input type="button" @click="compile()" value="Compile" />
</label>
<label x-show="mode === 'lmsm-assembly'">
<input type="button" @click="assemble()" value="Assemble" />
</label>
<label>
<input type="button" @click="run()" value="Run" />
</label>
<label x-show="mode === 'lmsm-assembly'">
<input type="button" x-data="{
assembleAndRun() {
this.lmsm = new LittleManStackMachine();
if(this.currentLineNumber !== -1) {
window.editor.getDoc().removeLineClass(this.currentLineNumber, 'background', 'markCode');
}
this.currentLineNumber = -1;
code = window.editor.getValue();
this.lmsm.assembleAndRun(window.editor.getValue());
},
}" @click="assembleAndRun()" value="Load & Run" />
</label>
<label x-show="mode === 'lmsm-assembly'">
<input type="button" x-data="{
step() {
if(this.currentLineNumber !== -1) {
window.editor.getDoc()
.removeLineClass(this.currentLineNumber, 'background', 'markCode');
this.lmsm.step();
}
this.currentLineNumber++;
window.editor.getDoc()
.addLineClass(this.currentLineNumber, 'background', 'markCode');
}
}" @click="step()" value="Step" />
</label>
</section>
<div id="codeEditor"></div>
</div>
<div id="registerBox" class="box" data-cols="2" data-rows="2">
<strong class="allcaps block titlebar">Register File</strong>
<div class="margin grid">
<div class="box bad" data-cols="1">
<label for="pc" class="bold">PC:
<input name="pc" class="register" type="text" size="3" :value="lmsm.registers.program_counter" />
</label>
</div>
<div class="box info" data-cols="2">
<label for="acc" class="bold">ACC:
<input name="acc" class="register" size="3" type="text" :value="lmsm.registers.accumulator" />
</label>
</div>
<div class="box info" data-cols="3">
<label for="ci" class="bold">CI:
<input name="ci" class="register" size="3" type="text" :value="lmsm.registers.current_instruction" />
</label>
</div>
<div class="box ok" data-cols="4">
<label for="sp" class="bold">SP:
<input name="sp" class="register" size="3" type="text" :value="lmsm.registers.stack_pointer" />
</label>
</div>
<div class="box warn" data-cols="5">
<label for="rap" class="bold">RAP:
<input name="rap" class="register" size="3" type="text" :value="lmsm.registers.return_address_pointer" />
</label>
</div>
</div>
</div>
<div id="memoryBox" class="box" data-cols="2" data-rows="3 5">
<strong class="allcaps block titlebar">Memory Layout</strong>
<div class="center">
<table>
<caption></caption>
<thead>
<th></th>
<template x-for="i in 10">
<th x-text="i - 1"></th>
</template>
</thead>
<tbody>
<template x-for="i in 20">
<tr>
<td x-text="(i - 1) * 10" class="bold"></td>
<template x-for="j in 10">
<td class="memorySlot">
<label>
<input size="3" type="text" :class="cellClass()" x-model="lmsm.memory[mempos]" x-data="{
mempos: ((i-1) * 10) + (j - 1),
cellClass() {
if(this.lmsm.registers.program_counter == this.mempos)
return 'memory_slot program_counter'
else if (this.lmsm.registers.stack_pointer === this.mempos)
return 'memory_slot stack_pointer'
else if(this.lmsm.registers.return_address_pointer === this.mempos)
return 'memory_slot return_address_pointer'
else return 'memory_slot';
}
}" />
</label>
</td>
</template>
</tr>
</template>
</tbody>
</table>
</div>
</div>
<div id="outputBox" class="box" data-cols="3" data-rows="2 5">
<strong class="allcaps block titlebar">Output</strong>
<input type="button" @click="lmsm.clearOutput()" value="Clear" />
<div id="outputPane" class="box pane" _="on mutation of childList me.scroll({top: my scrollHeight, left: 0, behavior: 'smooth'})">
<template x-for="line in lmsm.output">
<div x-text="line"></div>
</template>
</div>
</div>
</div>
</body>
</html>