generated from roerohan/Template
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathemulator.js
54 lines (45 loc) · 1.31 KB
/
emulator.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
import Lexer from 'emulator/parser/lexer.js';
import CPU from 'emulator/cpu/core.js';
import Parser from 'emulator/parser/parser.js';
class Emulator {
constructor() {
this.cpu = new CPU();
}
loadCode(code) {
const tokens = (new Lexer(code)).tokenize();
const parsed = (new Parser(tokens)).parse();
this.cpu.loadCode(parsed);
}
getRegisters() {
const regs = {};
Object.entries(this.cpu.registers.regs).forEach((element) => {
if (['A', 'B', 'C', 'D'].includes(element[0][0])) {
regs[`${element[0][0]}H`] = element[1].get('H');
regs[`${element[0][0]}L`] = element[1].get('L');
}
regs[element[0]] = element[1].get();
});
return regs;
}
getMemory() {
return [...this.cpu.memory.mem];
}
getSerialisableMemory() {
return [...this.cpu.memory.mem].map((v) => {
if (typeof v === 'number') return v;
let op1;
let op2;
if (v.op1) {
op1 = v.op1.value;
}
if (v.op2) {
op2 = v.op2.value;
}
return `${v.mnemonic.value} ${op1} ${op2}`;
});
}
resetState() {
this.cpu = new CPU();
}
}
export default new Emulator();