-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassembler.htm
More file actions
308 lines (236 loc) · 9.04 KB
/
assembler.htm
File metadata and controls
308 lines (236 loc) · 9.04 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
<html>
<head>
<link href='https://schickt.de/font/px8x8.css' rel='stylesheet'>
<link rel="stylesheet" type="text/css" href="style/style.css" media="screen" />
<style>
#container-top {
height: 75vh;
max-height: 75vh;
width: 100vw;
overflow: hidden;
}
#editor,
#editor-nol,
#assembler_output,
#preprocessor_output {
height: calc(75vh - 16px);
max-height: calc(75 - 16px);
}
#editor-nol {
float: left;
width: 40px;
text-align: right;
padding-right: 8px;
}
#blank-out {
bottom: 25%;
width: 48px;
height: 12px;
background: #202020;
position: absolute;
}
#editor {
overflow: scroll;
white-space: nowrap;
width: calc(100% - 48px);
}
#preprocessor_output,
#assembler_output {
overflow: scroll;
width: 100%;
padding-left: 1px;
}
#log {
clear: both;
height: calc(25% - 30px); /* oddness */
overflow: scroll;
background: #101010;
}
.local-width {
width: calc(20% - 16px);
}
</style>
<script src='rc-core.js'></script>
<script src='rc-math.js'></script>
<script src='rc-instruction-set.js'></script>
<script src='rc-address-modes.js'></script>
<script src='rc-preprocessor.js'></script>
<script src='rc-assembler.js'></script>
<script src='rc-classes.js'></script>
<script src='rc-instruction.js'></script>
<script src='ui-common.js'></script>
<script>
var modified = false
var verbose = true
var environment = new Environment()
function preprocess() {
var code = document.getElementById('editor').value
var prepro = new Preprocessor()
var output = prepro.preprocess(code, environment)
var out = verbose ? output.verbose_code : output.code
var content = number_text(out, true, false)
document.getElementById('preprocessor_output').innerHTML = content
html = '<span>'
if (output.errors.length) {
html += `<b>PREPROCESSOR ERRORS:</b>\n${output.errors.join('\n').html()}</b>\n\n`
}
if (output.warnings.length) {
html += `<i>PREPROCESSOR WARNINGS:</i>\n${output.warnings.join('\n').html()}`
}
html += '</span>'
document.getElementById('log').innerHTML = html
return output
}
function assemble(code) {
var assembler = new Assembler()
var output = assembler.assemble(code, environment)
var content = number_assembly(output.listing, true)
document.getElementById('assembler_output').innerHTML = content
var html = '<br><span>'
if (output.errors.length) {
html += `\n<b>ASSEMBLER ERRORS:</b>\n${output.errors.join('\n').html()}</b>\n\n`
}
if (output.warnings.length) {
html += `<i>ASSEMBLER WARNINGS:</i>\n${output.warnings.join('\n').html()}`
}
html += '</span>'
document.getElementById('log').innerHTML += html
return output
}
function changed() {
var p = preprocess()
if (p.errors.length == 0) {
var a = assemble(p.code)
if (a.errors.length == 0) {
modified = true
document.getElementById('log').innerHTML += '<span class="yay">ASSEMBLED SUCCESSFULLY.</span><br>'
}
document.getElementById('save_button').hidden = (a.errors.length > 0)
} else {
document.getElementById('assembler_output').innerHTML = '<b>Preprocessor errors prevented assembly.</b>'
document.getElementById('save_button').hidden = true
}
renumber_lines()
}
function renumber_lines() {
var n = document.getElementById('editor').value.split('\n').length
var s = ''
for (var i = 1; i <= n; i++) {
if (i % 10 == 0) {
s += `<div>${i}</div>`
}
else {
s += `${i}<br>`
}
}
document.getElementById('editor-nol').innerHTML = s
}
function save() {
var code = document.getElementById('editor').value
var name = document.getElementById('name').value
name = '#WRRR_' + name.replace(/ /g, '_')
localStorage.setItem(name, code)
document.getElementById('save_button').hidden = true
}
function scrolled() {
var y = document.getElementById('editor')
var e = document.getElementById('editor-nol')
e.style.height = y.scrollHeight
e.style.marginTop = `-${y.scrollTop}px`
if (verbose) {
var p = document.getElementById('preprocessor_output')
p.scrollTop = y.scrollTop
}
}
function output_scrolled() {
if (verbose) {
var p = document.getElementById('preprocessor_output')
var y = document.getElementById('editor')
y.scrollTop = p.scrollTop
}
}
function init() {
// listen to messages from parent window
window.addEventListener('message', message_handler, false)
document.getElementById('editor').addEventListener('keydown', function(e) {
if (e.keyCode === 9) {
// get caret position/selection
var start = this.selectionStart;
var end = this.selectionEnd;
var value = e.target.value
// set textarea value to: text before caret + tab + text after caret
e.target.value = `${value.substring(0, start)}\t${value.substring(end)}`
// put caret at right position again (add one for the tab)
this.selectionStart = this.selectionEnd = start + 1;
// prevent the focus loss
e.preventDefault();
}
}, false);
changed()
}
function debug() {
const code = btoa(document.getElementById('editor').value)
window.parent.postMessage(`debugger::launch::${code}`, '*')
}
function message_handler(event) {
items = event.data.split('::')
action = items.shift(0)
switch (action) {
case 'load':
document.getElementById('editor').value = atob(items[0])
changed()
break
case 'environment':
environment = JSON.parse(atob(items[0]))
changed()
break
}
}
function toggle_verbose() {
verbose = !verbose
document.getElementById('checkbox-verbose').innerHTML = `[${verbose ? 'X' : ' '}] verbose`
changed()
scrolled()
}
</script>
</head>
<body onload='init()'>
<div class='top' style='background:red;'>
<div id='container-top' class='code'>
<div class='left50'>
<h1>Source code</h1>
<div class='no-overflow'>
<div id='editor-nol' class='code num'></div>
<div id='blank-out' class='code'></div>
<textarea id='editor' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' class='code' onkeyup='changed()'
onscroll='scrolled()'>
;redcode-88
;author your_name_here
org start
start mov $1, $1
dummy dat #0, #0</textarea>
</div>
</div>
<div class='v-spacer'></div>
<div class='left30'>
<h1>Preprocessor output
<span id='checkbox-verbose' onclick='toggle_verbose()'>[X] verbose</span>
</h1>
<pre id='preprocessor_output' onscroll='output_scrolled()' class='code'></pre>
</div>
<div class='v-spacer'></div>
<div class='left local-width'>
<h1>Assembly listing</h1>
<pre id='assembler_output' class='code'></pre>
</div>
</div>
<pre id='log'></pre>
</div>
<div class='toolbar'>
<input type='button' value='ASSEMBLE' onclick='changed()'>
<input type='button' value='WRITE TO LOCAL STORAGE' onclick='save()' id='save_button'>
<input type='text' placeholder='WARRIOR NAME' id='name'>
<input type='button' value='SELECTED to DEBUGGER' onclick='debug()' class='right'>
</div>
</body>
</html>