-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.html
More file actions
458 lines (386 loc) · 13 KB
/
index.html
File metadata and controls
458 lines (386 loc) · 13 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
<html>
<head>
<!-- DCPU16 -->
<script type="text/javascript" src="src/common.js"></script>
<script type="text/javascript" src="src/parser.js"></script>
<script type="text/javascript" src="src/asm.js"></script>
<script type="text/javascript" src="src/emulator.js"></script>
<script type="text/javascript" src="src/hw/ne_lem1802.js"></script>
<script type="text/javascript" src="src/hw/keyboard.js"></script>
<script type="text/javascript" src="src/hw/clock.js"></script>
<!-- Gimmicks -->
<script type="text/javascript" src="src/base64.js"></script>
<script type="text/javascript" src="src/io-web.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/ace/0.2.0/ace.js"></script>
<script type="text/javascript" src="src/ace-dasm16.js"></script>
<script type="text/javascript" src="themes/theme-merbivore.js"></script>
<script type="text/javascript" src="themes/theme-vibrant_ink.js"></script>
<script type="text/javascript" src="themes/theme-vibrant_ink_mod.js"></script>
<style type="text/css">
.markline {
position: absolute;
background-color: #641d1d;
z-index: 5;
}
</style>
</head>
<body>
<p><a href="https://github.com/migerh/dcpu16.js">GitHub repo</a></p>
<span style="font-size: 11px;">Click the line numbers to toggle breakpoints.<!-- Drop asm source files or rom files over the editor to load them.--></span>
<div style="height: 500px;">
<div id="src" style="width: 700px; height: 500px; float: left;"></div>
<canvas id="out" width="512" height="384" style="margin-left: 710px; margin-top: 0px; border: 16px solid black;">Canvas is not supported.</canvas>
</div>
<p>
<!--button onclick="asm();">Assemble</button-->
<button onclick="step();">Step</button>
<button onclick="start();">Start</button>
<button onclick="stop();">Stop</button>
<button onclick="_clear();">Clear</button>
<button onclick="downloadRom();">Download rom</button>
<span id="summary"></span>
</p>
<button onclick="gotoRam();">Go to</button> <input type="text" value="0" id="jumpto" />
<button onclick="gotoRam('SP');" onmouseover="highlightRam('SP');" onmousemove="highlightRam('SP');" onmouseout="highlightRam('none');">SP</button>
<button onclick="gotoRam('PC');" onmouseover="highlightRam('PC');" onmousemove="highlightRam('PC');" onmouseout="highlightRam('none');">PC</button><br />
<pre id="ram" style="margin-top: 0; width: 520px; height: 170px; font-size: 14px; overflow-x: hidden; overflow-y: scrollbar; float: left;"></pre>
<pre id="status" style="margin-left: 530px; width: 200px; height: 400px;"></pre>
<script type="text/javascript">
var PC, assembly, editor, marker, lastUpdate = 0, canvas, screen, kbd, clock,
Range = require("ace/range").Range,
load = function (what) {
editor.getSession().setValue($('#' + what).html());
},
markLine = function (line) {
range = new Range(line-1, 0, line, 0);
editor.getSession().removeMarker(marker);
marker = editor.getSession().addMarker(range, "markline", "line");
//editor.gotoLine(line);
},
newPC = function () {
if (assembly) {
PC = new DCPU16.PC();
screen = new DCPU16.NE_LEM1802(PC);
screen.on('border', function (color) {
$('#out').css('border-color', color);
});
kbd = new DCPU16.Keyboard(PC);
clock = new DCPU16.Clock(PC);
PC.load(assembly.bc, assembly.base);
$('#jumpto').val(DCPU16.printHex(assembly.base));
PC.ram.PC = assembly.base;
printStatus();
}
},
asm = function () {
var src = editor.getSession().getValue(),
cursor = editor.getCursorPosition(),
ann = [], i;
editor.getSession().clearAnnotations();
$('#summary').html('');
try {
assembly = DCPU16.asm(src);
for (i = 0; i < assembly.warnings.length; i++) {
ann.push({
row: assembly.warnings[i].line-1,
column: 0,
text: assembly.warnings[i].message,
type: 'warning'
});
}
editor.getSession().setAnnotations(ann);
if (ann.length > 0) {
$('#summary').html('There are ' + ann.length + ' warnings. <button onclick="editor.gotoLine(' + (ann[0].row+1) + ');">Jump to first one</button>.');
}
newPC();
} catch (e) {
editor.getSession().removeMarker(marker);
assembly = null;
editor.getSession().setAnnotations([{
row: e.line-1,
column: 0,
text: e.toString(),
type: 'error'
}]);
//editor.gotoLine(e.line-1);
$('#summary').html('There has been an error. <button onclick="editor.gotoLine(' + (e.line) + ');">Jump to the error</button>.');
}
//editor.moveCursorToPosition(cursor);
},
step = function () {
PC.step();
printStatus();
editor.gotoLine(assembly.addr2line[PC.ram.PC]);
},
start = function () {
PC.start();
printStatus(true);
},
stop = function () {
PC.stop();
},
_clear = function () {
newPC();
},
printStatus = function (timeout) {
var out = [];
if (!PC) {
return;
}
markLine(assembly.addr2line[PC.ram.PC - assembly.base]);
out.push('A = ' + DCPU16.printHex(PC.ram.A) + ' ');
out.push('B = ' + DCPU16.printHex(PC.ram.B) + '<br />');
out.push('C = ' + DCPU16.printHex(PC.ram.C) + ' ');
out.push('X = ' + DCPU16.printHex(PC.ram.X) + '<br />');
out.push('Y = ' + DCPU16.printHex(PC.ram.Y) + ' ');
out.push('Z = ' + DCPU16.printHex(PC.ram.Z) + '<br />');
out.push('I = ' + DCPU16.printHex(PC.ram.I) + ' ');
out.push('J = ' + DCPU16.printHex(PC.ram.J) + '<br /><br />');
out.push('PC = ' + DCPU16.printHex(PC.ram.PC) + ' ');
out.push('SP = ' + DCPU16.printHex(PC.ram.SP) + '<br />');
out.push('IA = ' + DCPU16.printHex(PC.ram.IA) + ' ');
out.push('EX = ' + DCPU16.printHex(PC.ram.EX) + '<br /><br />');
out.push('#interrupts = ' + PC.interrupts.length + '<br /><br />');
out.push('Stack: <br />');
if (PC.ram.SP > 0xFF00) {
for (i = PC.ram.SP; i < 0x10000; i++ ) {
out.push('[' + DCPU16.printHex(PC.getWord(i)) + ']<br />');
}
}
$('#status').html(out.join(''));
screen.drawScreen(canvas, 512, 384);
gotoRam();
if (timeout && PC.isRunning) {
setTimeout(function () {
printStatus(true)
}, 75);
}
},
printRam = function (offset) {
var i, ram = [];
for (i = 0; i < 80; i++) {
if (i%8 === 0) {
ram.push((i !== 0 ? '</span>\n' : '') + '<span id="ram-line-' + ((offset + i)/8) + '" class="ram-line">' + DCPU16.printHex(offset + i) + ' ');
}
ram.push('<span id="ram-word-' + (offset + i) + '" class="ram-word">' + DCPU16.printHex(PC.getWord(offset + i)) + '</span>');
}
$('#ram').html(ram.join(' '));
},
gotoRam = function (where) {
if (where) {
where = PC.ram[where];
} else {
where = DCPU16.parseInt($('#jumpto').val()) || 0;
}
where = Math.min(Math.floor(where/8), 0xffb0/8),
printRam(where*8);
},
highlightRam = function (where) {
$('.ram-word').css('background-color', 'white');
if (where == 'none') {
return;
}
if (where) {
where = PC.ram[where];
} else {
where = DCPU16.parseInt($('#jumpto').val());
}
$('#ram-word-' + where).css('background-color', 'pink');
},
downloadRom = function () {
window.open('data:application/octet-stream;base64,' + Base64.encode(assembly.bc), '_blank', 'height=100,width=100');
},
getGist = function (id) {
var params = id.split('|'),
uri = 'https://api.github.com/gists/' + params[0],
file,
callback = function (data) {
var i;
if (data.status === 200) {
if (data.data.files[params[1]]) {
file = params[1];
} else {
for (i in data.data.files) {
if (data.data.files.hasOwnProperty(i) && data.data.files[i].language === 'DCPU-16 ASM') {
file = i;
break;
}
}
}
editor.getSession().setValue(data.data.files[file].content);
asm();
}
};
$.ajax({
type: 'GET',
dataType: 'jsonp',
jsonp: 'callback',
url: uri,
success: function (data) {
callback(data);
}
});
};
window.onload = function () {
var gist, dasm16 = require('ace/mode/dasm16').Mode;
editor = ace.edit("src");
editor.setTheme('ace/theme/vibrant_ink_mod');
editor.session.setUseWorker(false);
editor.getSession().setMode(new dasm16());
canvas = document.getElementById('out').getContext('2d');
editor.getSession().selection.on('changeCursor', function (e) {
if (!assembly) {
return;
}
var curline = editor.getSession().getSelection().getCursor().row + 1,
totallines = editor.getSession().getValue().split("\n").length,
start = assembly.line2addr[curline] + assembly.base,
end, i;
for (i = curline + 1; i < totallines; i++) {
if (end = assembly.line2addr[i] + assembly.base) {
break;
}
}
$('.ram-word').css('background-color', 'white');
for (i = start; i < end; i++) {
$('#ram-word-' + i).css('background-color', 'pink');
}
});
$('#src > div.ace_gutter').on('click', function (e) {
var line = $(e.toElement),
lineNumber = line.html().match(/\d*$/);
if (assembly.line2addr[lineNumber] + assembly.base) {
line.html((PC.toggleBreakpoint(assembly.line2addr[lineNumber] + assembly.base) ? '<span style="color: red; font-weight: bolder;">•</span> ' : '') + lineNumber);
}
});
$('#src > .ace_sb').on('scroll', function (e) {
$('#src div.ace_gutter-cell').each(function (i, line) {
var lineNumber = $(line).html().match(/\d*$/),
lineAddr = assembly.line2addr[lineNumber] + assembly.base;
if (PC.breakpoints[lineAddr]) {
$(line).html('<span style="color: red; font-weight: bolder;">•</span> ' + lineNumber);
}
});
});
$('#jumpto').on('keyup', function (e) {
gotoRam();
});
document.addEventListener('dragover', function (e) {
e.stopPropagation();
e.preventDefault();
e.dataTransfer.dropEffect = 'none';
}, false);
document.getElementById('src').addEventListener('dragover', function (e) {
e.stopPropagation();
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
}, false);
document.getElementById('src').addEventListener('drop', function (e) {
var files = e.dataTransfer.files, f,
firstTry,
reader = new FileReader();
e.stopPropagation();
e.preventDefault();
if (files.length > 0) {
f = files[0];
reader.onloadend = function (result) {
var i, rom = [];
if (firstTry) {
if (firstTry == result.target.result) {
editor.getSession().setValue(firstTry);
asm();
} else {
for (i = 0; i < firstTry.length; i++) {
rom.push(firstTry.charCodeAt(i));
}
alert('Disassembling is currently deactivated, sorry.');
return;
assembly = DCPU16.dasm(rom);
editor.getSession().setValue(assembly.src);
newPC();
}
} else {
firstTry = result.target.result;
reader.readAsText(f);
}
};
reader.onerror = function (result) {
alert('Error while reading file.');
};
reader.readAsBinaryString(f);
}
}, false);
gist = window.location.href.split('#gist-');
if (gist[1]) {
getGist(gist[1]);
} else if (localStorage.getItem('file')) {
editor.getSession().setValue(localStorage.getItem('file'));
asm();
} else {
load('example-features');
asm();
}
editor.getSession().on('change', function (e) {
try {
localStorage.setItem('file', editor.getSession().getValue());
} catch (e) {}
asm();
});
};
</script>
<!-- example -->
<script type="text/dcpu-asm" id="example-features">
; If there is only a comment in the first line, make sure there are no whitespaces before.
; This is a known issue: https://github.com/migerh/dcpu16.js/issues/6
; start at 0x1000
; not at the moment, sorry.
; .org 0x1000
; Macros can not be used currently.
; Labels inside macros are not resolved properly yet.
; #macro push(value) {
; SET PUSH, value
; ; Equivalent
; ; SET [--SP], value
;}
; Everything from the specification should work
; http://0x10c.com/doc/dcpu-16.txt
SET A, 0x30
; Use a macro; see above
; push(0xDEAD)
; Sugar for asm veterans. Synonym to SET.
MOV Z, 0xBEEF
; Simple expressions are resolved: +,-,*,/ and brackets are allowed.
; Expressions are not shortened.
SET [0x1000], (9+7)*2
; There can be a register involved
SUB a, [0x2000/2+I]
; Mnemonics and registers are case insensitive.
; Labels and macros are case sensitive.
IfN a, 0x10
SET Pc, crash
; Do a loopy thing
SET I, 10
SET A, 0x2000
:loop
SET [0x2000+I], [A]
SUB I, 1
IFN I, 0
SET PC, loop
; Call a subroutine
SET X, 0x4
JSR testsub
SET PC, crash
:testsub
SHL X, 4
SET PC, POP
; Hang forever. X should now be 0x40 if everything went right.
:crash
SET PC, crash
; DAT is supported, too.
; You can use labels, numbers, expressions, and string literals here.
:data
dat 0x170, "Hello, World!", 0x65, 73, "w0,,0t!", 0xDEAD+20
</script>
</body>
</html>