-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcpu.js
69 lines (58 loc) · 1.98 KB
/
cpu.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
class DeviceInstructions {
constructor(program, starting_registers = { a: 0, b: 0 }, starting_instruction = 0) {
// Clone the arrays we pass in
this.program = JSON.parse(JSON.stringify(program));
this.registers = JSON.parse(JSON.stringify(starting_registers));
this.instruction = starting_instruction;
}
run() {
let line = this.program[this.instruction];
if (line) {
let { op, register, jump } = line;
return this[op](register, jump);
}
return false;
}
/**
* Opcodes
*/
// hlf r sets register r to half its current value, then continues with the next instruction.
hlf(register) {
this.registers[register] /= 2;
return ++this.instruction;
}
// tpl r sets register r to triple its current value, then continues with the next instruction.
tpl(register) {
this.registers[register] *= 3;
return ++this.instruction;
}
// inc r increments register r, adding 1 to it, then continues with the next instruction.
inc(register) {
this.registers[register] += 1;
return ++this.instruction;
}
// jmp offset is a jump; it continues with the instruction offset away relative to itself.
jmp(register, jump) {
this.instruction += jump;
return this.instruction;
}
// jie r, offset is like jmp, but only jumps if register r is even ("jump if even").
jie(register, jump) {
if (this.registers[register] % 2 === 0) {
this.instruction += jump;
} else {
this.instruction++;
}
return this.instruction;
}
// jio r, offset is like jmp, but only jumps if register r is 1 ("jump if one", not odd).
jio(register, jump) {
if (this.registers[register] === 1) {
this.instruction += jump;
} else {
this.instruction++;
}
return this.instruction;
}
}
module.exports = DeviceInstructions;