-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstruction.js
More file actions
128 lines (104 loc) · 4 KB
/
instruction.js
File metadata and controls
128 lines (104 loc) · 4 KB
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
/*
A TYPE
#MOV [opcode 5 bits][imm 27 bits]
B TYPE
#ADD [opcode 5 bits][return reg 5 bits][r1 5 bits][r2 5 bits][imm val 12 bits] r0 = r1 + r2 + imm
#SUB [opcode 5 bits][return reg 5 bits][r1 5 bits][r2 5 bits][imm val 12 bits] r0 = r1 - r2 - imm
#MUL [opcode 5 bits][return reg 5 bits][r1 5 bits][r2 5 bits][imm val 12 bits] r0 = r1 * r2 * imm (if imm = 0, default to 1)
#DIV [opcode 5 bits][return reg 5 bits][r1 5 bits][r2 5 bits][imm val 12 bits] r0 = r1 / r2 / imm (if imm = 0, default to 1)
#AND [opcode 5 bits][return reg 5 bits][r1 5 bits][r2 5 bits][imm val 12 bits] if imm is 0 default to 1
#OR [opcode 5 bits][return reg 5 bits][r1 5 bits][r2 5 bits][imm val 12 bits] except imm doesnt default to 1
#NOR [opcode 5 bits][return reg 5 bits][r1 5 bits][r2 5 bits][imm val 12 bits] if imm is 0 default to 1
C TYPE
#SLL [opcode 5 bits][shift reg 5 bits][shift imm 22] (shift left logical)
#SRL [opcode 5 bits][shift reg 5 bits][shift imm 22] (shift right logical)
D TYPE
#SLT [opcode 5 bits][return reg 5 bits][reg1 5 bits][reg2 5 bits] (return true if one reg is less than another)
E TYPE
#JMP [opcode 5 bits][imm 27 bits]
F TYPE
#JMR [opcode 5 bits][reg 5 bits]
F TYPE
#BEQ [opcode 5 bits][dest reg 5 bits][r1 5 bits][r2 5 bits][offset 17 bits] branch if equal
#BNE [opcode 5 bits][dest reg 5 bits][r1 5 bits][r2 5 bits][offset 17 bits] branch if not equal
G TYPE
#BLEZ[opcode 5 bits][dest reg 5 bits][r1 5 bits][offset 22 bits] branch if less than or equal to 0
#BGTZ[opcode 5 bits][dest reg 5 bits][r1 5 bits][offset 22 bits] branch if greater than or equal to 0
H TYPE
#LDR [opcode 5 bits][return reg 5 bits][addr reg 5 bits][offset 17 bits]
#STR [opcode 5 bits][origin reg 5 bits][addr reg 5 bits][offset 17 bits]
I TYPE
#HLT [opcode 5 bits]
#NOP [opcode 5 bits]
*/
function Instruction(iValue){
return {
value: 0,
bitString: 0,
parts: null,
opType: null,
opCode: null,
decode: function(){
if(iValue > 0 && iValue<4294967296){
this.value = iValue;
this.bitString = iValue.toString(2);
if(this.bitString.length < 32){
this.bitString = new Array(33-this.bitString.length).join('0') + this.bitString;
}
this.parts = [];
this.parts = [this.bitString.substr(0,5), this.bitString.substr(5,5), this.bitString.substr(10, 5), this.bitString.substr(15)];
this.opCode = this.bitString.substr(0,5);
if(this.opCode === 0){
this.opType = "A";
}
else if(this.opCode > 0 && this.opCode <= 7){
this.opType = "B";
this.parts = [this.bitString.substr(0,5), this.bitString.substr(5,5), this.bitString.substr(10, 5), this.bitString.substr(15, 5), this.bitString.substr(20)];
}
else if(this.opCode > 7 && this.opCode <= 9){
this.opType = "C";
this.parts = [this.bitString.substr(0,5), this.bitString.substr(5,5), this.bitString.substr(22)];
}
else if(this.opCode === 10){
this.opType = "D";
this.parts = [this.bitString.substr(0,5), this.bitString.substr(5,5), this.bitString.substr(10, 5), this.bitString.substr(15, 5)];
}
else if(this.opCode === 11){
this.opType = "E";
this.parts = [this.bitString.substr(0,5), this.bitString.substr(5,5), this.bitString.substr(10, 5), this.bitString.substr(15, 5)];
}
else if(this.opCode === 12){
this.opType = "F";
}
else if(this.opCode > 12 && this.opCode <= 14{
this.opType = "G";
}
else if(this.opCode > 14 && this.opCode <= 16){
this.opType = "H";
}
else if(this.opCode > 16 && this.opCode < 18){
this.opType = "I";
}
else{
this.opType = "J";
}
return {value: this.value, bitString: this.bitString, parts: this.parts, opCode: this.opCode, opType: this.opType};
}
return false;
}
,
fetchReg: function(registers){
if(this.opType === null){
return false;
}
switch(this.opType){
case "A":
//do nothing..?
break;
case "B":
return {r1: registers[]}
break;
}
}
}
};