This repository has been archived by the owner on Dec 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscript.js
461 lines (323 loc) · 13 KB
/
script.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
// FUNÇÃO PARA TRANSFORMAR OS INPUTS EM BINÁRIO
var submit = document.querySelector("#submit");
var entrada1 = document.querySelector("#entrada1");
var entrada2 = document.querySelector("#entrada2");
var displayBin1 = document.querySelector("#displayBin1");
var displayBin2 = document.querySelector("#displayBin2");
var tds = document.querySelectorAll("td");
var selectFuncao = document.querySelector("#selectFuncao");
var selectRegister = document.querySelector("#selectRegister");
var regDisplay = document.querySelectorAll(".regDisplay");
var execute = document.querySelector("#execute");
var celulasMemoria = document.getElementById('celulasMemoria');
var ramValues = [];
var functionValues = [];
var regLabel = document.querySelectorAll(".regLabel");
var regValues = [];
var binValues = [];
var aluFuncao = document.querySelector('.aluFuncao');
var irFuncao = document.querySelector('.irFuncao');
var pcFuncao = document.querySelector('.pcFuncao');
// Função que está sendo executada no momento
var executando = document.querySelector('#executando');
// Variáveis das FLAGS
var flags = document.querySelector(".flags");
// Contador para pintar as células
var i = 0;
// Converte e mostra o display do INPUT1 (numero)
entrada1.addEventListener("input", function() {
var bin1 = (+entrada1.value).toString(2);
displayBin1.textContent = bin1;
});
// Converte e mostra o display do INPUT2 (selecionar registrador)
selectRegister.addEventListener("input", function() {
var bin2 = (+selectRegister[selectRegister.selectedIndex].value);
displayBin2.textContent = bin2;
});
// Pega os binários e salva
submit.addEventListener('submit', function (evento){
// Prevenir atualização da página
evento.preventDefault();
alert("Submit!");
var bin1 = (+entrada1.value).toString(2);
//** Pintar 3 células na RAM e colocar os valores quando for submitado um input **\\
// Seleciona o conteúdo da funcao selecionada e coloca na RAM
// Função sleep para dar efeito do delay
var now = new Date().getTime();
while ( new Date().getTime() < now + 2000 ){
tds[i].textContent = selectFuncao[selectFuncao.selectedIndex].textContent;
// Adiciona os conteúdos dos inputs na RAM
tds[i+1].textContent = bin1;
// Seleciona o valor do index selecionado do regitrador e coloca na RAM.
tds[i+2].textContent = selectRegister[selectRegister.selectedIndex].value;
}
// Ja cria o Array com os valores da RAM no momento do input
ramValues.push(selectFuncao[selectFuncao.selectedIndex].textContent);
ramValues.push(bin1);
ramValues.push(selectRegister[selectRegister.selectedIndex].value);
// Cria o array das funcoes para serem executadas
functionValues.push(selectFuncao[selectFuncao.selectedIndex].textContent);
// Cria o array do endereço da próxima instrução
regValues.push(selectRegister[selectRegister.selectedIndex].value);
// Cria o array do conteúdo do endereço
binValues.push(bin1);
// Contador para pintar 3
var count = 0;
while(count < 3){
tds[i].classList.add("bg-success");
i++;
count++;
}
});
// Desativar input do teclado quando digito em um input number
$("[type='number']").keypress(function (evt) {
evt.preventDefault();
});
execute.addEventListener('submit', function(evento) {
// Prevenir atualização da página
evento.preventDefault();
alert("Run!");
// DELAY
var now = new Date().getTime();
while ( new Date().getTime() < now + 1000 ){
// ULTRA MASTER GAMBIARRA SEM LOOP NA TORA MESMO
for(var j = 2; j <= 45; j+=3){
if(ramValues[j] == regDisplay[0].textContent){
regDisplay[0].textContent = ramValues[j-1];
regDisplay[0].classList.add("bg-success");
regLabel[0].classList.add("bg-warning");
}
if(ramValues[j] == regDisplay[1].textContent){
regDisplay[1].textContent = ramValues[j-1];
regDisplay[1].classList.add("bg-success");
regLabel[1].classList.add("bg-warning");
}
if(ramValues[j] == regDisplay[2].textContent){
regDisplay[2].textContent = ramValues[j-1];
regDisplay[2].classList.add("bg-success");
regLabel[2].classList.add("bg-warning");
}
if(ramValues[j] == regDisplay[3].textContent){
regDisplay[3].textContent = ramValues[j-1];
regDisplay[3].classList.add("bg-success");
regLabel[3].classList.add("bg-warning");
}
if(ramValues[j] == regDisplay[4].textContent){
regDisplay[4].textContent = ramValues[j-1];
regDisplay[4].classList.add("bg-success");
regLabel[4].classList.add("bg-warning");
}
if(ramValues[j] == regDisplay[5].textContent){
regDisplay[5].textContent = ramValues[j-1];
regDisplay[5].classList.add("bg-success");
regLabel[5].classList.add("bg-warning");
}
if(ramValues[j] == regDisplay[6].textContent){
regDisplay[6].textContent = ramValues[j-1];
regDisplay[6].classList.add("bg-success");
regLabel[6].classList.add("bg-warning");
}
if(ramValues[j] == regDisplay[7].textContent){
regDisplay[7].textContent = ramValues[j-1];
regDisplay[7].classList.add("bg-success");
regLabel[7].classList.add("bg-warning");
}
if(ramValues[j] == regDisplay[8].textContent){
regDisplay[8].textContent = ramValues[j-1];
regDisplay[8].classList.add("bg-success");
regLabel[8].classList.add("bg-warning");
}
if(ramValues[j] == regDisplay[9].textContent){
regDisplay[9].textContent = ramValues[j-1];
regDisplay[9].classList.add("bg-success");
regLabel[9].classList.add("bg-warning");
}
if(ramValues[j] == regDisplay[10].textContent){
regDisplay[10].textContent = ramValues[j-1];
regDisplay[10].classList.add("bg-success");
regLabel[10].classList.add("bg-warning");
}
if(ramValues[j] == regDisplay[11].textContent){
regDisplay[11].textContent = ramValues[j-1];
regDisplay[11].classList.add("bg-success");
regLabel[11].classList.add("bg-warning");
}
if(ramValues[j] == regDisplay[12].textContent){
regDisplay[12].textContent = ramValues[j-1];
regDisplay[12].classList.add("bg-success");
regLabel[12].classList.add("bg-warning");
}
if(ramValues[j] == regDisplay[13].textContent){
regDisplay[13].textContent = ramValues[j-1];
regDisplay[13].classList.add("bg-success");
regLabel[13].classList.add("bg-warning");
}
if(ramValues[j] == regDisplay[14].textContent){
regDisplay[14].textContent = ramValues[j-1];
regDisplay[14].classList.add("bg-success");
regLabel[14].classList.add("bg-warning");
}
}
}
aluFuncao.textContent = functionValues[0];
aluFuncao.parentNode.classList.add("bg-primary");
irFuncao.textContent = functionValues[1];
irFuncao.parentNode.classList.add("bg-info");
pcFuncao.textContent = regValues[1];
pcFuncao.parentNode.classList.add("bg-warning");
// FUNÇÕES ASSEMBLY
if (executando.textContent == "mult") {
regDisplay[(parseInt(regValues[0], 2)-1)].textContent = ((parseInt(binValues[1], 2) * parseInt(binValues[0], 2)).toString(2));
// CARRY FLAG
if((parseInt(binValues[0], 2) * parseInt(binValues[1], 2)) == 16){
flags.classList.add("bg-danger");
flags.childNodes[3].textContent = "CARRY";
}
// OVERFLOW FLAG
if((parseInt(binValues[0], 2) * parseInt(binValues[1], 2)) > 16){
flags.classList.add("bg-danger");
flags.childNodes[3].textContent = "OVERFLOW";
}
// ZERO FLAG
if((parseInt(binValues[0], 2) * parseInt(binValues[1], 2)) == 0){
flags.classList.add("bg-danger");
flags.childNodes[3].textContent = "ZERO";
}
// SIGN FLAG
if(parseInt(binValues[0], 2) * (parseInt(binValues[1], 2) ) < 0){
flags.classList.add("bg-danger");
flags.childNodes[3].textContent = "SIGN";
}
regValues.shift();
}
if (executando.textContent == "add") {
// console.log("pegou")
regDisplay[(parseInt(regValues[0], 2)-1)].textContent = ((parseInt(binValues[1], 2) + parseInt(binValues[0], 2)).toString(2));
// CARRY FLAG
if((parseInt(binValues[1], 2) + parseInt(binValues[0], 2)) == 16){
flags.classList.add("bg-danger");
flags.childNodes[3].textContent = "CARRY";
}
// OVERFLOW FLAG
if((parseInt(binValues[1], 2) + parseInt(binValues[0], 2)) > 16){
flags.classList.add("bg-danger");
flags.childNodes[3].textContent = "OVERFLOW";
}
// ZERO FLAG
if((parseInt(binValues[1], 2) + parseInt(binValues[0], 2)) == 0){
flags.classList.add("bg-danger");
flags.childNodes[3].textContent = "ZERO";
}
// SIGN FLAG
if((parseInt(binValues[1], 2) + parseInt(binValues[0], 2)) < 0){
flags.classList.add("bg-danger");
flags.childNodes[3].textContent = "ZERO";
}
regValues.shift();
}
if (executando.textContent == "sub") {
// console.log("pegou")
regDisplay[(parseInt(regValues[0], 2)-1)].textContent = ((parseInt(binValues[1], 2) - parseInt(binValues[0], 2)).toString(2));
// CARRY FLAG
if((parseInt(binValues[0], 2) - parseInt(binValues[1], 2)) == 16){
flags.classList.add("bg-danger");
flags.childNodes[3].textContent = "CARRY";
}
// OVERFLOW FLAG
if((parseInt(binValues[0], 2) - parseInt(binValues[1], 2)) > 16){
flags.classList.add("bg-danger");
flags.childNodes[3].textContent = "OVERFLOW";
}
// ZERO FLAG
if((parseInt(binValues[0], 2) - parseInt(binValues[1], 2)) == 0){
flags.classList.add("bg-danger");
flags.childNodes[3].textContent = "ZERO";
}
// SIGN FLAG
if(parseInt(binValues[0], 2) - (parseInt(binValues[1], 2) ) < 0){
flags.classList.add("bg-danger");
flags.childNodes[3].textContent = "SIGN";
}
regValues.shift();
}
if (executando.textContent == "div") {
// console.log("pegou")
regDisplay[(parseInt(regValues[0], 2)-1)].textContent = (parseInt((parseInt(binValues[1], 2) / parseInt(binValues[0], 2))).toString(2));
// CARRY FLAG
if((parseInt(binValues[0], 2) / parseInt(binValues[1], 2)) == 16){
flags.classList.add("bg-danger");
flags.childNodes[3].textContent = "CARRY";
}
// OVERFLOW FLAG
if((parseInt(binValues[0], 2) / parseInt(binValues[1], 2)) > 16){
flags.classList.add("bg-danger");
flags.childNodes[3].textContent = "OVERFLOW";
}
// ZERO FLAG
if((parseInt(binValues[0], 2) / parseInt(binValues[1], 2)) == 0){
flags.classList.add("bg-danger");
flags.childNodes[3].textContent = "ZERO";
}
// SIGN FLAG
if(parseInt(binValues[0], 2) / (parseInt(binValues[1], 2) ) < 0){
flags.classList.add("bg-danger");
flags.childNodes[3].textContent = "SIGN";
}
regValues.shift();
}
if (executando.textContent == "and") {
// console.log("pegou")
regDisplay[(parseInt(regValues[0], 2)-1)].textContent = (parseInt((parseInt(binValues[1], 2) && parseInt(binValues[0], 2))).toString(2));
regValues.shift();
}
if (executando.textContent == "or") {
// console.log("pegou")
regDisplay[(parseInt(regValues[0], 2)-1)].textContent = (parseInt((parseInt(binValues[1], 2) || parseInt(binValues[0], 2))).toString(2));
regValues.shift();
}
if (executando.textContent == "not") {
// console.log("pegou")
if ((parseInt(binValues[0], 2)).toString(2) == (parseInt(binValues[1], 2)).toString(2)) {
regDisplay[(parseInt(regValues[0], 2)-1)].textContent = regDisplay[(parseInt(regValues[0], 2)-1)].textContent * (- 1) + 1;
};
regValues.shift();
}
if(executando.textContent == "jmp=="){
if (regDisplay[(parseInt(regValues[0], 2)-1)].textContent != binValues[binValues.length-1]) {
alert("Erro de lógica. Impossível prosseguir.");
location.reload();
}
regValues.shift();
}
if(executando.textContent == "jmp>"){
if (regDisplay[(parseInt(regValues[0], 2)-1)].textContent > binValues[binValues.length-1]) {
alert("Erro de lógica. Impossível prosseguir.");
location.reload();
}
regValues.shift();
}
if(executando.textContent == "jmp<"){
if (regDisplay[(parseInt(regValues[0], 2)-1)].textContent < binValues[binValues.length-1]) {
alert("Erro de lógica. Impossível prosseguir.");
location.reload();
}
regValues.shift();
}
if(executando.textContent == "jmp!="){
if (regDisplay[(parseInt(regValues[0], 2)-1)].textContent == binValues[binValues.length-1]) {
alert("Erro de lógica. Impossível prosseguir.");
location.reload();
}
regValues.shift();
}
if(executando.textContent == "nop"){
alert("Sem operação!");
regValues.shift();
}
if(executando.textContent == "store"){
alert("Acesso de memória aleatória!");
regValues.shift();
}
// Não tirar nem fudendo
functionValues.shift();
});