-
Notifications
You must be signed in to change notification settings - Fork 3
/
cpu-emulator.js
151 lines (138 loc) · 3.62 KB
/
cpu-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
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
const { parseParts } = require('./utils.js');
function getSigned(binStr) {
binStr =
binStr.length >= 8 && binStr[0] === '1'
? binStr.padStart(32, '1')
: binStr.padStart(32, '0');
return parseInt(binStr, 2) >> 0;
}
const MAX_CYCLES = 10000000;
let cycles = 0;
exports.cpuEmulator = function(program, { returnClock = false } = {}) {
let distribution = {};
let counter = 0;
let clock = 0;
const state = {
D: 0,
A: 0,
RAM: [],
M(value) {
if (this.A < 0 || this.A > 24576) {
throw new Error(`Address out of range: ${this.A} (line ${counter}) `);
}
if (value === undefined) {
return this.RAM[this.A];
} else {
this.RAM[this.A] = value;
}
},
};
while (true) {
clock += 1;
distribution[counter] = distribution[counter] || 0;
distribution[counter] += 1;
if (cycles++ > MAX_CYCLES) {
throw new Error('max clock cycles exceeded');
}
let line = program[counter];
if (!line || !line.trim()) break;
line = line.replace(/\/\/.*$/, '').trim();
if (line === 'BREAK') {
// console.log('BREAK');
break;
} else if (line[0] === '@') {
const symbol = line.slice(1, line.length);
state.A = Number(symbol);
} else {
const { comp, dest, jump } = parseParts(line);
const compHandler = {
'0': () => 0,
'1': () => 1,
'-1': () => -1,
D: () => state.D,
A: () => state.A,
M: () => state.M(),
'!D': () => ~state.D,
'!A': () => ~state.A,
'!M': () => ~state.M(),
'-D': () => -state.D,
'-A': () => -state.A,
'-M': () => -state.M(),
'D+1': () => state.D + 1,
'A+1': () => state.A + 1,
'M+1': () => state.M() + 1,
'D-1': () => state.D - 1,
'A-1': () => state.A - 1,
'M-1': () => state.M() - 1,
'D+A': () => state.D + state.A,
'D+M': () => state.D + state.M(),
'D-A': () => state.D - state.A,
'D-M': () => state.D - state.M(),
'A-D': () => state.A - state.D,
'M-D': () => state.M() - state.D,
'D&A': () => state.D & state.A,
'D&M': () => state.D & state.M(),
'D|M': () => state.D | state.M(),
};
const handler = compHandler[comp];
if (!handler) {
throw new Error('Invalid comp: ' + comp + ', line: "' + line + '"');
}
const result = handler();
if (dest.indexOf('A') > -1) state.A = result;
if (dest.indexOf('D') > -1) state.D = result;
if (dest.indexOf('M') > -1) state.M(result);
// console.log({ counter, comp, dest, jump, result, state });
switch (jump) {
case 'JGT':
if (result > 0) {
counter = state.A;
continue;
}
break;
case 'JEQ':
if (result === 0) {
counter = state.A;
continue;
}
break;
case 'JGE':
if (result >= 0) {
counter = state.A;
continue;
}
break;
case 'JLT':
if (result < 0) {
counter = state.A;
continue;
}
break;
case 'JNE':
if (result !== 0) {
counter = state.A;
continue;
}
break;
case 'JLE':
if (result <= 0) {
counter = state.A;
continue;
}
break;
case 'JMP':
counter = state.A;
continue;
break;
}
}
counter += 1;
}
if (returnClock) {
return {
...state,
clock,
};
}
return state;
};