forked from IchigoJam/asm15
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreadmcode.js
66 lines (61 loc) · 2.12 KB
/
readmcode.js
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
"use strict";
function parseBasInt(s) {
if (s.substr(0, 1) === "#") return parseInt(s.substr(1), 16);
else if (s.substr(0, 1) === "`") return parseInt(s.substr(1), 2);
else return parseInt(s, 10);
}
function readMachineCode(codeStr) {
const s = codeStr.split("\n");
let n = 0;
const memWrites = [];
for (let i = 0; i < s.length; i++) {
let basCode = s[i].replace(/\s/g, ""); // erase spaces
basCode = basCode.replace(/^\d+/, ""); // erase line number
basCode = basCode.replace(/'.*$/, ""); // erase comment
const basCodes = basCode.split(":");
let basValid = false;
let matched;
for (let j = 0; j < basCodes.length; j++) {
const code = basCodes[j];
if ((matched = /^poke(#[0-9a-fA-F]+|[0-9]+|`[01]+),/i.exec(code))) {
let addr = parseBasInt(matched[1]);
const data = code.split(",");
for (let k = 1; k < data.length; k++) {
memWrites.push([addr, parseBasInt(data[k]), "poke"]);
addr++;
}
basValid = true;
} else if ((matched = /^\[(#[0-9a-fA-F]+|[0-9]+|`[01]+)\]=(#[0-9a-fA-F]+|[0-9]+|`[01]+)$/i.exec(code))) {
const idx = parseBasInt(matched[1]);
const value = parseBasInt(matched[2]);
memWrites.push([0x800 + idx * 2, value & 0xff, "array"]);
memWrites.push([0x800 + idx * 2 + 1, (value >> 8) & 0xff, "array"]);
basValid = true;
} else if ((matched = /^let\[(#[0-9a-fA-F]+|[0-9]+|`[01]+)\],/i.exec(code))) {
let idx = parseBasInt(matched[1]);
const data = code.split(",");
for (let k = 1; k < data.length; k++) {
const value = parseBasInt(data[k]);
memWrites.push([0x800 + idx * 2, value & 0xff, "array"]);
memWrites.push([0x800 + idx * 2 + 1, (value >> 8) & 0xff, "array"]);
idx++;
}
basValid = true;
}
}
if (!basValid) {
if ((matched = /^\*\*\* (-?[0-9]+)-byte gap \*\*\*$/.exec(s[i]))) {
n += parseInt(matched[1]) >> 1;
} else {
const binCode = s[i].replace(/[^01]/g, "");
if (binCode.length == 16) {
const binValue = parseInt(binCode, 2);
memWrites.push([n * 2, binValue & 0xff, "bin"]);
memWrites.push([n * 2 + 1, (binValue >> 8) & 0xff, "bin"]);
n++;
}
}
}
}
return memWrites;
}