-
Notifications
You must be signed in to change notification settings - Fork 0
/
readWord.ts
138 lines (128 loc) · 3.51 KB
/
readWord.ts
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
import { OpCodeNames, OpCodeModes } from "./OpCodes"; // Import the generated OpCodeNames and OpCodeModes arrays
const logOpCodes = false;
const logOpModes = false;
enum OpMode {
None = 0b00000,
A = 0b10000,
AB = 0b11000,
AC = 0b10100,
ABC = 0b11100,
AD = 0b10010,
D = 0b00010,
//AE = 0b10001,
E = 0b00001,
}
export interface Instruction {
A: number | undefined;
B: number | undefined;
C: number | undefined;
D: number | undefined;
E: number | undefined;
Aux: Buffer | undefined;
OpCode: number;
}
export function HasAux(opcode: number)
{
let OpMode: number = OpCodeModes[opcode];
if (OpMode == undefined)
{
console.warn("Invalid opcode " + opcode);
return false;
}
return ((OpMode & 0b0000010) == 0b0000010);
}
export function ReadOpCode(word: Buffer): Instruction
{
let opCode = word.readUInt8(0);
if (logOpCodes)
console.log("Opcode: " + OpCodeNames[opCode], "Mode: " + OpMode[OpCodeModes[opCode]>>2]);
// ABC
// AD
function InsA(): number {
return word.readUInt8(1);
}
// b is always behind a and c
function InsB(): number {
return word.readUInt8(2);
}
function InsC(): number {
return word.readUInt8(3);
}
// d is a 16-bit value, its behind a
// AD mode
function InsD(): number {
return word.readInt16LE(2);
}
// E - least-significant byte for the opcode, followed by E (24-bit integer). E is a signed integer that commonly specifies a jump offset
// E
function InsE(): number {
let first8 = word.readUInt8(1);
let last16 = word.readInt16LE(2);
return (last16 << 8) | first8;
}
let A,B,C,D,Aux
switch(true)
{
case OpMode[OpCodeModes[opCode]>>2] == "A":
A = InsA();
if (logOpModes)
console.log("A: " + A);
break;
case OpMode[OpCodeModes[opCode]>>2] == "AB":
A = InsA();
B = InsB();
if (logOpModes)
console.log("A: " + A, "B: " + B);
break;
case OpMode[OpCodeModes[opCode]>>2] == "AC":
A = InsA();
C = InsC();
if (logOpModes)
console.log("A: " + A, "C: " + C);
break;
case OpMode[OpCodeModes[opCode]>>2] == "ABC":
A = InsA();
B = InsB();
C = InsC();
if (logOpModes)
console.log("A: " + A, "B: " + B, "C: " + C);
break;
case OpMode[OpCodeModes[opCode]>>2] == "AD":
A = InsA();
D = InsD();
if (logOpModes)
console.log("A: " + A, "D: " + D);
break;
case OpMode[OpCodeModes[opCode]>>2] == "D":
D = InsD();
if (logOpModes)
console.log("D: " + D);
break;
case OpMode[OpCodeModes[opCode]>>2] == "E":
D = InsE();
if (logOpModes)
console.log("E: " + D);
break;
case OpMode[OpCodeModes[opCode]>>2] == "None":
break;
default:
console.warn("Error reading OpCode words" + OpCodeNames[opCode]);
break;
}
if (HasAux(opCode))
{
// slice buffer
Aux = word.slice(4, 8);
if (logOpModes)
console.log("Aux:", Aux);
}
return {
A: A,
B: B,
C: C,
D: D,
E: D,
Aux: Aux,
OpCode: opCode
} as Instruction;
}