-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproc.js
243 lines (223 loc) · 7.59 KB
/
proc.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
Proc = function(){
this.flags = [];
this.regsVal = [];
this.regsList = [];
this.IP;
this.labels = [];
this.instrList = [];
}
Proc.prototype.initInstructionList = function()
{
this.instrList.push("MOV");//MOVE
this.instrList.push("ADD");//ADD
this.instrList.push("SUB");//SUBSTRACT
this.instrList.push("MUL");//MULTIPLY
this.instrList.push("DIV");//DIVIDE
this.instrList.push("RDV");//REST of DIVIIDING
this.instrList.push("DEC");//DECREMENT
this.instrList.push("INC");//INCREMENT
this.instrList.push("NEG");//to NEGATIVE
this.instrList.push("POS");//to POSITIVE
this.instrList.push("CMP");//COMPARE
this.instrList.push("JMP");//JUMP to
this.instrList.push("JPU");//JUMP if UNEQUAL
this.instrList.push("JPE");//JUMP if EQUAL
this.instrList.push("JPL");//JUMP if LESSER
this.instrList.push("JPG");//JUMP if GREATER
this.instrList.push("EXT");//EXIT
}
Proc.prototype.initRegsisters = function(nRegisters)
{
for (var i = 0; i < nRegisters; i++) {
var reg = "r" + i; //Создаются регистры видa r1, r2..., ri
this.regsList.push(reg);
this.regsVal[reg] = 0;
}
}
Proc.prototype.initFlags = function()
{
//Compare Unequal Flag - true - сравнение показало, что операнды не равны
this.flags["CMPUF"] = false;
//Compare Equal Flag - true - сравнение показало, что операнды равны
this.flags["CMPEF"] = false;
//Compare Lesser Flag - true - сравнение показало, что оп1 меньше чем оп2
this.flags["CMPLF"] = false;
//Compare Greater Flag - true - сравнение показало, что оп1 больше чем оп2
this.flags["CMPGF"] = false;
//Error Flag - true если была совершена ошибка в синтаксисе
this.flags["ERF"] = false;
}
Proc.prototype.initInstructions = function(nRegisters)
{
//null потому что метки не нужны в этих операциях
var binInstrCurry = procINSTR(null)(this.flags)(this.regsVal)(procBinaryInstr);
Proc.prototype.procADD = binInstrCurry(ADD);
Proc.prototype.procSUB = binInstrCurry(SUB);
Proc.prototype.procMUL = binInstrCurry(MUL);
Proc.prototype.procDIV = binInstrCurry(DIV);
Proc.prototype.procRDV = binInstrCurry(RDV);
Proc.prototype.procMOV = binInstrCurry(MOV);
//null потому что метки не нужны в этих операциях
var unaryInstrCurry = procINSTR(null)(this.flags)(this.regsVal)(procUnaryInstr);
Proc.prototype.procINC = unaryInstrCurry(INC);
Proc.prototype.procDEC = unaryInstrCurry(DEC);
Proc.prototype.procPOS = unaryInstrCurry(POS);
Proc.prototype.procNEG = unaryInstrCurry(NEG);
var cmpInstrCurry = procINSTR(null)(this.flags)(this.regsVal)(procCompareInstr);
Proc.prototype.procCMP = cmpInstrCurry(CMP);
//null потому что здесь не нужны регистры
var jumpInstrCurry = procINSTR(this.labels)(this.flags)(null)(procJumpInstr);
Proc.prototype.procJMP = jumpInstrCurry(JMP);
Proc.prototype.procJPU = jumpInstrCurry(JPU);
Proc.prototype.procJPE = jumpInstrCurry(JPE);
Proc.prototype.procJPL = jumpInstrCurry(JPL);
Proc.prototype.procJPG = jumpInstrCurry(JPG);
}
Proc.prototype.resetRegs = function()
{
for (var i = 0; i < this.regsList.length; i++) {
var reg = "r" + i; //Создаются регистры видa r1, r2..., ri
this.regsVal[reg] = 0;
}
}
Proc.prototype.initProc = function(nRegisters)
{
this.initRegsisters(nRegisters);
this.initFlags();
this.initInstructionList();
this.initInstructions();
this.IP = 0;
}
Proc.prototype.getLabels = function(codeStrings)
{
for (var i = 0; i < codeStrings.length; i++) {
var code = codeStrings[i].split(":");
if(code.length > 1){
this.labels[code[0]] = i;
console.log("Getting Label: " + code[0] + ", label number: " + i);
}
}
}
/* Формат строки: label:_INSTR_op1,op2_;comment
*строка сплитится по ';' часть с комментарием откидывается
*/
Proc.prototype.readMemory = function(memory)
{
var memStr = memory[this.IP];
var formatMemStr = [];
formatMemStr = memStr.split(";"); //Сплит по ; разделит комментарий от кода
formatMemStr.pop();//Выкидываем комментарий
console.log("Processor read memory: " + formatMemStr[0]);
return formatMemStr;
}
/*Проверяется существование инструкции, наличие регистров
*Выполнение инструкции
*/
Proc.prototype.applyInstr = function(instr, OpStr)
{
//Маленький костыль, но сплайс возвращает массив, поэтоу так
var instruction = instr[0].toUpperCase();
if(!this.instrList.some( function(item){ return item == instruction; } )){ //Определена ли передаваемая инструкция
alert("Instruction is not defined! "+instruction);
this.flags["ERF"] = true;
return;
}
//Длина массива операндов мб равна 0, в случае операторов без аргументов напр. EXT
if(OpStr.length != 0){
var arrOP = OpStr[0].split(',');//Операторы разделены запятой
console.log("IP: " + this.IP + " Instr: " + instruction+ " operands: " + arrOP);
}
switch(instruction.toUpperCase()){
case "ADD":
this.procADD(arrOP);
this.IP++;
break
case "SUB":
this.procSUB(arrOP);
this.IP++;
break
case "MOV":
this.procMOV(arrOP);
this.IP++;
break
case "DIV":
this.procDIV(arrOP);
this.IP++;
break
case "MUL":
this.procMUL(arrOP);
this.IP++;
break
case "RDV":
this.procRDV(arrOP);
this.IP++;
break
case "INC":
this.procINC(arrOP);
this.IP++;
break
case "DEC":
this.procDEC(arrOP);
this.IP++;
break
case "POS":
this.procPOS(arrOP);
this.IP++;
break
case "NEG":
this.procNEG(arrOP);
this.IP++;
break
case "CMP":
this.procCMP(arrOP);
this.IP++;
break
case "JMP":
var res = this.procJMP(arrOP);
(res == -1) ? this.IP++ : (this.IP = res);
break
case "JPU":
var res = this.procJPU(arrOP);
(res == -1) ? this.IP++ : (this.IP = res);
break
case "JPE":
var res = this.procJPE(arrOP);
(res == -1) ? this.IP++ : (this.IP = res);
break
case "JPL":
var res = this.procJPL(arrOP);
(res == -1) ? this.IP++ : (this.IP = res);
break
case "JPG":
var res = this.procJPG(arrOP);
(res == -1) ? this.IP++ : (this.IP = res);
break
//Костыль, для выхода меняем флаг ошибки на "true"
case "EXT":
console.log("IN EXIT");
this.flags["ERF"] =true;
break
default:
alert("There is no instr: "+instruction);
this.flags["ERF"]= true;
break
}
}
Proc.prototype.executeInstr = function(memStr)
{
var arrOfInstr = memStr.split(" ");
var isInstrDefined = this.instrList.some( function(item){ return item == arrOfInstr[0].toUpperCase(); } )
if(!isInstrDefined){//Если первая инструкция не определенная команда, то это должна быть метка
console.log("Deleted label: " + arrOfInstr[0]);
arrOfInstr.splice(0,1);//Первый эл.(метка в этом случае) выбрасывается
}
/*На этом этапе массив инструкий должен состоять только из
*инструкции и операнда(ов). В функцию выталкиывается
*инструкция, а оставшийся массив и есть операнды
*/
this.applyInstr(arrOfInstr.splice(0,1), arrOfInstr);
}
Proc.prototype.step = function(memStr)
{
this.executeInstr(memStr);
}