-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
400 lines (378 loc) · 13.3 KB
/
main.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
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
document.querySelectorAll("button").forEach(b => {
b.disabled = true;
});
const memory = new Memory();
const rom = new ROM();
const bus = new Bus();
let offset = 0x800;
bus.addComponent("ram", 0, 0x800, memory);
bus.addComponent("rom", 0x800, 0xFFFF, rom);
const cpu = new Processor(bus);
cpu.reset();
const DEFAULT_CODE = `; Welcome to Atom Fluorine assembly editor!
jmp main ; your program starts here
jmp maskable_interrupt ; if maskable interrupt is triggered, jump to the maskable interrupt handler
jmp non_maskable_interrupt ; if maskable interrupt is triggered, jump to the maskable non interrupt handler
main: ; insert your code here
hlt
maskable_interrupt: ; handles a maskable interrupt (interrupts that can be ignored, normally triggered by the program using the INT instruction)
reti
non_maskable_interrupt: ; handles a non maskable interrupt (interrupts that can't be ignored, normally triggered because of a error (not implemented in MkII) or hardware specific implementations)
reti`;
const noUpdateStackEl = document.querySelector("#noUpdateStack");
const noUpdateMemoryEl = document.querySelector("#noUpdateMemory");
const intervalTimeout = document.querySelector("#interval");
const stepTimes = document.querySelector("#stepTimes");
const ramViewer = document.querySelector("#ram-viewer");
const regel = document.querySelectorAll("#rvalues td");
const noUpdateInfo = document.querySelector("#noUpdateInfo");
const stackEl = document.querySelector("#stack");
const sregsEl = document.querySelectorAll("#srvalues td");
const uploadEl = document.querySelector("input[type=\"file\"]");
const downloadEl = document.querySelector("#download");
const enableDarkTheme = document.querySelector("#dark-theme");
const statusInfo = document.querySelector("#status");
const registersTable = document.querySelector("#registers");
const sregsTable = document.querySelector("#sregs");
let saveName = "atom2";
let editor;
function updateRegistersColors(){
const regnames = document.querySelector("#registers tr").children;
for(let i=0;i<regnames.length;i++){
const reg = regnames[i];
reg.style.backgroundColor = registersColors[i];
}
}
function updateRegisters(){
regel[0].textContent = cpu.registers[0];
regel[1].textContent = cpu.registers[1];
regel[2].textContent = cpu.registers[2];
regel[3].textContent = cpu.registers[3];
regel[4].textContent = cpu.registers[4].toString(2).padStart(8,"0");
}
function updateMemory(){
let section = cpu.ip & 0xFF00;
let k = 0;
while(ramViewer.firstChild)
ramViewer.firstChild.remove(); // forEach or for loops are glitched
for(let i=0;i<16;i++){
const tr = document.createElement("tr");
for(let j=0;j<16;j++){
const td = document.createElement("td");
td.innerText = bus.read(section | k++);
if(((cpu.ip & 0xFF) == k-1)){
td.style.backgroundColor = "#44a";
td.style.color = "#eee";
}
tr.append(td);
}
ramViewer.append(tr);
}
}
/*const clearRightTrash = arr => {
arr.reverse();
while(!arr[0] && arr.length > 0) arr.shift();
arr.reverse();
}*/
function updateStack(){
while(stackEl.firstChild)
stackEl.firstChild.remove();// forEach or for loops are glitched
const stack = [];
for(let i=255;i>=0;i--) stack.push(bus.read(i));
const okStack = stack.slice(0,255 - cpu.registers[3]);
for(let i=0;i<stack.length;i++){
const tr = document.createElement("tr");
const td = document.createElement("td");
td.textContent = stack[i];
tr.append(td);
if((okStack.length - 1) === i)
td.style.backgroundColor = "#0000ee";
stackEl.append(tr);
}
}
function updateSRegs(){
sregsEl[0].textContent = !!cpu.getFlag(PROCESSOR_FLAGS.HALTED);
sregsEl[1].textContent = !!cpu.getFlag(PROCESSOR_FLAGS.INTERRUPT);
sregsEl[2].textContent = !!cpu.getFlag(PROCESSOR_FLAGS.ZERO);
sregsEl[3].textContent = !!cpu.getFlag(PROCESSOR_FLAGS.CARRY);
}
function instruction2string(){
let ip = cpu.ip;
const byte = cpu.readFrom(ip);
const instruction_name = PROCESSOR_OPERATIONS[byte]?.name;
if(!instruction_name){
return `DB ${byte}`;
}
let instruction = instruction_name;
const model = OPMODELS[instruction_name];
ip++;
for(const type of model){
if(type === 0){
instruction += ` ${cpu.readFrom(ip)}`;
ip++;
}else if(type === 1){
const val1 = cpu.readFrom(ip);
ip++;
const val2 = cpu.readFrom(ip);
ip++;
const value = (val1 << 8) | val2;
instruction += ` 0x${value.toString("16").padStart(4,"0")}`;
}
}
return instruction;
}
function updateInfo(){
try{
statusInfo.innerText = `IP: ${cpu.ip}\nCurrent instruction: ${instruction2string()}`;
}catch(e){
statusInfo.innerText = `Error getting information about the processor.`;
console.error(e);
}
updateRegisters();
updateSRegs();
}
function updateAll(){
if(!noUpdateInfo.checked) updateInfo();
if(!noUpdateMemoryEl.checked) updateMemory();
if(!noUpdateStackEl.checked) updateStack();
}
function reset(dontResetBus){
cpu.reset();
if(!dontResetBus) bus.reset();
updateAll();
}
function compile_str(){
try{
const compiled = asm2code(editor.getValue(),offset);
rom.loadData(compiled, offset);
reset();
}catch(e){
console.info(e);
const splitted = e.message.split(" ");
if(e.constructor == SyntaxError){
alert(`No op named ${splitted[0]} in line ${splitted[1]}`);
editor.revealLine(parseInt(splitted[1]), 1);
editor.setPosition(new monaco.Position(parseInt(splitted[1]), 0));
editor.setSelection(new monaco.Selection(parseInt(splitted[1]), 0, parseInt(splitted[1]), Infinity));
}else{
alert(`No label ${splitted[0]}`);
editor.revealLine(parseInt(splitted[1]), 1);
editor.setPosition(new monaco.Position(parseInt(splitted[1]), 0));
editor.setSelection(new monaco.Selection(parseInt(splitted[1]), 0, parseInt(splitted[1]), Infinity));
}
}
}
function downloadCode(){
const url = "data:text/plain;charset=utf-8,"+encodeURIComponent(editor.getValue());
downloadEl.download = "code.aasm";
downloadEl.href = url;
downloadEl.click();
}
function clearRightTrash2(arr){
for(let i=arr.length-1;i>0;i--){
if(arr[i]) break;
arr.pop();
}
}
function downloadBin(){
const romCleaned = Array.from(rom);
clearRightTrash2(romCleaned);
const romVal = [];
for(const val of romCleaned)
romVal.push(String.fromCharCode(val));
console.log(romCleaned, romVal.map(s => s.charCodeAt(0)));
const url = "data:application/octet-stream,"+encodeURIComponent(romVal.join(""));
downloadEl.download = "rom.bin";
downloadEl.href = url;
downloadEl.click();
}
function getFiles(){
return new Promise(r => {
uploadEl.onchange = e => r(e.target.files);
uploadEl.click();
});
}
async function uploadCode(){
const file = (await getFiles())[0];
editor.setValue(await file.text());
compile_str();
updateAll();
}
async function uploadBin(){
const file = (await getFiles())[0];
const text = await file.text();
reset();
rom.loadROM(text.split("").map(s => s.charCodeAt(0)));
updateAll();
}
function makeStep(){
for(let i=0;i<stepTimes.value;i++){
if(cpu.halted) break;
cpu.step();
}
updateAll();
}
function intervalClear(){
clearInterval(interval);
interval = undefined;
}
function intervalStep(){
if(interval) intervalClear();
interval = setInterval(makeStep, parseInt(intervalTimeout.value));
}
function updateInfoDisplay(updateChecked){
if(window.localStorage.getItem("info-disabled")){
statusInfo.classList.add("hidden");
registersTable.classList.add("hidden");
sregsTable.classList.add("hidden");
}else{
statusInfo.classList.remove("hidden");
registersTable.classList.remove("hidden");
sregsTable.classList.remove("hidden");
updateInfo();
}
if(updateChecked) noUpdateInfo.checked = window.localStorage.getItem("info-disabled");
}
noUpdateInfo.addEventListener("change", () => {
window.localStorage.setItem("info-disabled", noUpdateInfo.checked ? "1" : "0");
updateInfoDisplay();
});
function updateStackDisplay(updateChecked){
if(window.localStorage.getItem("stack-disabled"))
while(stackEl.firstChild)
stackEl.firstChild.remove();// forEach or for loops are glitched
if(updateChecked) noUpdateStackEl.checked = window.localStorage.getItem("stack-disabled");
}
noUpdateStackEl.addEventListener("change", () => {
window.localStorage.setItem("stack-disabled", noUpdateStackEl.checked ? "1" : "0");
updateStackDisplay();
});
function updateMemoryDisplay(updateChecked){
if(window.localStorage.getItem("memory-disabled"))
while(ramViewer.firstChild)
ramViewer.firstChild.remove();// forEach or for loops are glitched
if(updateChecked) noUpdateMemoryEl.checked = window.localStorage.getItem("memory-disabled");
}
noUpdateMemoryEl.addEventListener("change", () => {
window.localStorage.setItem("memory-disabled", noUpdateMemoryEl.checked ? "1" : "0");
updateMemoryDisplay();
});
function saveCode(){
localStorage.setItem(saveName, editor.getValue());
}
function loadCode(){
const code = localStorage.getItem(saveName);
editor.setValue(code ? code : DEFAULT_CODE);
}
function isFirstTime(){
return !window.localStorage.getItem("alreadyVisited");
}
function restore(){
updateStackDisplay();
updateMemoryDisplay();
updateInfoDisplay();
}
function load(){
monaco.languages.register({
id: "assembly"
});
monaco.languages.setMonarchTokensProvider("assembly", {
ignoreCase: true,
defaultToken: "invalid",
tokenizer: {
root: [
{include: "@comment"},
{include: "@variables"},
{include: "@numbers"},
{include: "@string"},
{include: "@defineConstants"},
{include: "@controllers"},
{include: "@math"},
{include: "@memory"},
{include: "@stack"},
{include: "@flag"}
//[/((lda)|(ldx)|(ldy)|(sta)|(stx)|(sty))/i, "memory"],
//[/((pusha)|(pushx)|(pushy)|(popa)|(popx)|(popy))/i, "stack"],
//[/(ada)|(adx)|(ady)|(sua)|(sux)|(suy)|(nana)|(nanx)|(nany)|(cma)|(cmx)|(cmy)/i, "math"],
//[/(ccf)|(czf)|(cif)|(scf)|(szf)|(sif)/i, "flag"]
],
comment: [
[/;.+/, "comment"]
],
variables: [
[/.+:/, "variable.name"],
],
numbers: [
[/0x[0-9a-f]+/i, "number"],
[/0b[0-1]+/i, "number"],
[/\d+/, "number"],
],
string: [
[/"/, "string", "@stringEnd"]
],
stringEnd: [
[/\\"/, "string.escape"],
[/"/, "string", "@pop"],
[/./, "string"]
],
defineConstants: [
[/db|alc/i, "constant"]
],
controllers: [
[/^(int)|(reti)|(ret)|(hlt)\b/i, "keyword"],
[/^(jmp)|(call)|(ret)|(jz)|(jc)|(jnz)|(jnc)|(rst)\b/i, "keyword", "@controllerVariable"]
],
controllerVariable: [
[/.+|((?=;))/, "variable.name", "@pop"]
],
math: [
[/^(ada)|(adx)|(ady)|(sua)|(sux)|(suy)|(nana)|(nanx)|(nany)|(cma)|(cmx)|(cmy)\b/i, "attribute.value"]
],
memory: [
[/^((lda)|(ldx)|(ldy)|(ldia)|(ldix)|(ldiy)|(sta)|(stx)|(sty))\b/i, "type"]
],
stack: [
[/(pusha)|(pushx)|(pushy)|(popa)|(popx)|(popy)/i, "delimiter"]
],
flag: [
[/(ccf)|(czf)|(cif)|(scf)|(szf)|(sif)/i, "type"]
]
}
});
editor = monaco.editor.create(document.querySelector("#code"), {
theme: "hc-black",
language: "assembly",
value: "; Loading..."
});
function updateTheme(){
if(enableDarkTheme.checked){
document.documentElement.classList.add("dark-theme");
window.localStorage.setItem("dark-theme", "true");
monaco.editor.setTheme("hc-black");
}else{
document.documentElement.classList.remove("dark-theme");
window.localStorage.removeItem("dark-theme");
monaco.editor.setTheme("hc-light");
}
}
enableDarkTheme.addEventListener("change", updateTheme);
document.querySelectorAll("button").forEach(b => {
b.disabled = false;
});
if(isFirstTime()){
window.localStorage.setItem("alreadyVisited", "1");
window.localStorage.setItem("dark-theme", "true");
window.localStorage.setItem("stack-disabled", "1");
}
if(window.localStorage.getItem("dark-theme")){
enableDarkTheme.checked = true;
updateTheme();
}
updateInfoDisplay(true);
updateMemoryDisplay(true);
updateStackDisplay(true);
updateTheme();
loadCode();
compile_str();
updateAll();
}