-
Notifications
You must be signed in to change notification settings - Fork 43
/
instr.js
121 lines (120 loc) · 1.93 KB
/
instr.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
var INSTR = {};
INSTR[0] = function () {
STAT = 'HLT';
//print("Program halted");
};
INSTR[1] = function () {
//NOP
};
INSTR[2] = function () {
REG[this.rB] = REG[this.rA];
};
INSTR[3] = function () {
REG[this.rB] = this.V;
};
INSTR[4] = function () {
var valA = REG[this.rA],
valB = REG[this.rB],
valE = valB + this.D;
ST(valE, valA, 4);
};
INSTR[5] = function () {
var valB = REG[this.rB],
valE = valB + this.D;
REG[this.rA] = LD(valE);
};
INSTR[6] = function () {
var valA = REG[this.rA],
valB = REG[this.rB];
switch(this.fn) {
case 0:
REG[this.rB] += REG[this.rA];
break;
case 1:
REG[this.rB] -= REG[this.rA];
break;
case 2:
REG[this.rB] = REG[this.rA] & REG[this.rB];
break;
case 3:
REG[this.rB] = REG[this.rA] ^ REG[this.rB];
break;
}
SF = REG[this.rB] < 0 ? 1 : 0;
ZF = REG[this.rB] === 0 ? 1 : 0;
OF = 0;
};
INSTR[7] = function () {
switch(this.fn) {
case 0:
// JMP
PC = this.Dest;
break;
case 1:
// JLE
if (SF === 1 || ZF === 1) {
PC = this.Dest;
}
break;
case 2:
// JL
if (SF === 1) {
PC = this.Dest;
}
break;
case 3:
// JE
if (ZF === 1) {
PC = this.Dest;
}
break;
case 4:
// JNE
if (ZF === 0) {
PC = this.Dest;
}
break;
case 5:
// JGE
if (ZF === 0 || ZF === 1) {
PC = this.Dest;
}
break;
case 6:
// JG
if (SF === 0 && ZF === 0) {
PC = this.Dest;
}
break;
}
};
INSTR[8] = function () {
var valB = REG[4],
valE = valB - 4;
ST(valE, PC, 4);
REG[4] = valE;
PC = this.Dest;
};
INSTR[9] = function () {
var valA = REG[4],
valB = REG[4],
valE = valB + 4,
valM = LD(valA);
REG[4] = valE;
PC = valM;
};
INSTR[10] = function () {
var valA = REG[this.rA],
valB = REG[4],
valE = valB - 4;
ST(valE, valA, 4);
REG[4] = valE;
};
INSTR[11] = function () {
var valA = REG[4],
valB = REG[4],
valE = valB + 4,
valM = LD(valA);
REG[4] = valE;
REG[this.rA] = valM;
};