-
Notifications
You must be signed in to change notification settings - Fork 0
/
processador.c
703 lines (560 loc) · 20.8 KB
/
processador.c
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
/* Processador do simulador MIPS32 */
/* Autor: Bruno Cesar, bcesar.g6@gmail.com */
#include "processador.h"
#include <stdio.h>
#include <stdlib.h>
static unsigned int IR; // instruction Register
unsigned int process; //Variável de controle do processo
int jump; // Variável de controle de saltos (Trava a busca e decodificação)
barr_aux* barr_Lo = NULL; //Barramento auxiliar do registrador Lo
barr_aux* barr_Hi = NULL; //Barramento auxiliar do registrador Hi
estacao_reserva* er_add1 = NULL;
estacao_reserva* er_add2 = NULL;
estacao_reserva* er_add3 = NULL;
estacao_reserva* er_mult1 = NULL;
estacao_reserva* er_mult2 = NULL;
estacao_reserva* er_load1 = NULL;
estacao_reserva* er_load2 = NULL;
estacao_reserva* er_load3 = NULL;
estacao_reserva* er_load4 = NULL;
estacao_reserva* er_load5 = NULL;
estacao_reserva* er_store1 = NULL;
estacao_reserva* er_store2 = NULL;
estacao_reserva* er_store3 = NULL;
estacao_reserva* er_store4 = NULL;
estacao_reserva* er_store5 = NULL;
/* Simula função de controle: Salto inconcidional */
void inconditionalJump(){
inst* ptr_instruction = pegaDaFila(1);
if(ptr_instruction->J.op == 3){
setReg(getPC(), REG_RA); //(JAL) link
if(get_flag(FLAG_VERBOSE)) printf("JAL -> RA = pc\t");
}
setPC(ptr_instruction->J.target);
if(get_flag(FLAG_VERBOSE)) printf("Salto inconcidional para %d\n", getPC());
flushCache(CACHE_TEXTO);
jump = 0; //Reseta o controle de salto
}
/* Se identificar um salto condicional altera o controle de saltos para pular a etapa de busca */
void checkConditionalJump(inst instruction){
// Opcodes de Branch
// 4 1-1 1-17 7 6 1-0 1-16 5
switch (instruction.I.op) {
case 1:
if(instruction.I.rt == 0 || instruction.I.rt == 1 ||
instruction.I.rt == 16 || instruction.I.rt == 17){
jump = 1;
}
break;
case 4:
case 5:
case 6:
case 7:
jump = 1;
break;
default:
break;
}
}
/* Etapa de busca */
/* Nesta etapa do pipeline uma instrução, na forma de palavra, */
/* é buscada na memoria na posição do PC atual */
/* Ela é então copiada para o registrador de instrução IR */
void busca(){
endereco addr;
addr.i = getPC();
IR = cRead(CACHE_TEXTO, addr);
if (get_flag(FLAG_VERBOSE)){
printf("\nPalavra lida da cache: ");
printaBinario(&IR, 0, NULL);
printf("\n");
}
updatePC();
}
/* Etapa de decoficação */
/* Decodifica uma palavra identificando seu tipo e atribuindo */
/* seus respectivos dados aos atributos da estrutura inst */
/* Params = word w : A palavra a ser decodificada */
/* Return = inst : A instrução decodificada e preenchida */
void decodifica(){
inst instruction;
unsigned int opcode = 0;
unsigned int func = 0;
word aux = malloc(sizeof(unsigned int));
memcpy(aux, &IR, 4);
*aux = *aux >> 26;
opcode = *aux;
switch (opcode) {
case 0:
case 28:
memcpy(aux, &IR, 4);
*aux = *aux << 26;
*aux = *aux >> 26;
func = *aux;
//Instrução R
instruction.R.op = opcode;
instruction.R.func = func;
memcpy(aux, &IR, 4);
*aux = *aux << 6;
*aux = *aux >> 27;
instruction.R.rs = *aux;
memcpy(aux, &IR, 4);
*aux = *aux << 11;
*aux = *aux >> 27;
instruction.R.rt = *aux;
memcpy(aux, &IR, 4);
*aux = *aux << 16;
*aux = *aux >> 27;
instruction.R.rd = *aux;
memcpy(aux, &IR, 4);
*aux = *aux << 21;
*aux = *aux >> 27;
instruction.R.aux = *aux;
if(get_flag(FLAG_VERBOSE)) printf("Instrução tipo R decodificada e adicionada a fila\n");
break;
case 2:
case 3:
instruction.J.op = opcode;
memcpy(aux, &IR, 4);
*aux = *aux << 6;
*aux = *aux >> 6;
instruction.J.target = *aux;
jump = -1; //Sinaliza salto incondicional
break;
case 1:
case 4 ... 15:
case 32 ... 38:
case 40 ... 43:
case 46:
case 48:
case 56:
//Instrução I
instruction.I.op = opcode;
memcpy(aux, &IR, 4);
*aux = *aux << 6;
*aux = *aux >> 27;
instruction.I.rs = *aux;
memcpy(aux, &IR, 4);
*aux = *aux << 11;
*aux = *aux >> 27;
instruction.I.rt = *aux;
memcpy(aux, &IR, 4);
*aux = *aux << 16;
*aux = *aux >> 16;
instruction.I.imm = *aux;
if(get_flag(FLAG_VERBOSE)) printf("Instrução tipo I decodificada e adicionada a fila\n");
checkConditionalJump(instruction);
break;
case 60: //NOP;
instruction.R.op = 60;
instruction.R.rs = 0;
instruction.R.rt = 0;
instruction.R.rd = 0;
instruction.R.aux = 0;
if(get_flag(FLAG_VERBOSE)) printf("Instrução NOP adicionada a fila\n");
break;
default:
printf("\n DEBUG \n");
printaBinario(&IR, 0 , NULL);
return; //Fix
break;
}
insereFila(instruction);
free(aux);
IR = FLAG_NULL;
}
/* Função auxiliar, verifica se existe uma ER livre para a operação op */
/* Params = int er_type : O tipo da Operação a ser verificada */
/* return = estacao_reserva* : O ponteiro para a ER ou NULL se não existe ER livre */
estacao_reserva* checa_er(int er_type){
switch(er_type){
case ADD_T:
case BRANCH:
case ADDI_T:
if (er_add1->busy == 0) return er_add1;
if (er_add2->busy == 0) return er_add2;
if (er_add3->busy == 0) return er_add3;
break;
case MUL_T:
case MULTI_T:
if (er_mult1->busy == 0) return er_mult1;
if (er_mult2->busy == 0) return er_mult2;
break;
case LOAD:
if (er_load1->busy == 0) return er_load1;
if (er_load2->busy == 0) return er_load2;
if (er_load3->busy == 0) return er_load3;
if (er_load4->busy == 0) return er_load4;
if (er_load5->busy == 0) return er_load5;
break;
case STORE:
if (er_store1->busy == 0) return er_store1;
if (er_store2->busy == 0) return er_store2;
if (er_store3->busy == 0) return er_store3;
if (er_store4->busy == 0) return er_store4;
if (er_store5->busy == 0) return er_store5;
break;
default:
break;
}
return NULL;
}
/* Etapa de emissão do pipeline */
/* Args = inst instruction : Instrução retirada da fila */
/* return : 1 Se a emissão ocorreu normalmente */
/* 0 Se a emissão falhou (cria stall) */
/* e a instrução deve voltar para a fila */
int emissao(inst instruction){
/* Se existe uma ER do tipo de OP livre : */
/* Se operandos estiverem nos registradores : */
/* Emite op -> ER com operandos em Vj e Vk */
/* Se não: */
/* Emite op -> ER com valores de Qi em Qj e Qk */
/* Se não: */
/* Cria um stall (pula etapa de emissão) */
operation op = getOp(instruction);
estacao_reserva* er = NULL;
er = checa_er(op.er_type);
//if(get_flag(FLAG_DEBUG)) printf("identificador = %d\n", identificaER(er));
if(er == NULL) return 0;
/* Carrega a operação na ER */
er->op = op.op;
er->busy = op.cycles;
switch(op.er_type){
case ADD_T:
case MUL_T:
if(Qi[instruction.R.rs] != 0){
er->qj = Qi[instruction.R.rs];
} else{
er->vj = getReg(instruction.R.rs);
er->qj = 0;
}
if(Qi[instruction.R.rt] != 0){
er->qk = Qi[instruction.R.rt];
} else{
er->vk = getReg(instruction.R.rt);
er->qk = 0;
}
er->A = instruction.R.aux;
if(instruction.R.rd != 0){
Qi[instruction.R.rd] = identificaER(er);
if(get_flag(FLAG_DEBUG)) printf("\nReservando Qi[%d] para saida de %s\n", instruction.R.rd, ER_nomes[identificaER(er) - 1]);
}
casosEspeciais(op.op, er);
break;
case BRANCH:
if(get_flag(FLAG_DEBUG)) printf("Operação de Branch identificada\n");
if(Qi[instruction.I.rs] != 0){
er->qj = Qi[instruction.I.rs];
} else{
er->vj = getReg(instruction.I.rs);
er->qj = 0;
}
if(Qi[instruction.I.rt] != 0){
er->qk = Qi[instruction.I.rt];
} else{
er->vk = getReg(instruction.I.rt);
er->qk = 0;
}
er->A = instruction.I.imm;
break;
case LOAD:
case ADDI_T:
if(Qi[instruction.I.rs] != 0){
er->qj = Qi[instruction.I.rs];
} else{
er->vj = getReg(instruction.I.rs);
er->qj = 0;
}
er->A = instruction.I.imm;
Qi[instruction.I.rt] = identificaER(er);
if(get_flag(FLAG_DEBUG)) printf("\nReservando Qi[%d] para a saida de %s\n", instruction.I.rt , ER_nomes[identificaER(er) - 1]);
break;
case STORE:
if(Qi[instruction.I.rs] != 0){
er->qj = Qi[instruction.I.rs];
} else{
er->vj = getReg(instruction.I.rs);
er->qj = 0;
}
er->A = instruction.I.imm;
if(Qi[instruction.R.rt] != 0){
er->qk = Qi[instruction.R.rt];
} else{
er->vk = getReg(instruction.R.rt);
er->qk = 0;
}
break;
}
if(get_flag(FLAG_DEBUG)) printaER(er, 0, NULL);
return 1;
}
/* Ativa uma estação de reserva Add / Mul para processar sua operação */
/* Params = estacao_reserva* er : Estação de reserva a ser ativada */
int run_add_mul(estacao_reserva* er){
unsigned int identificador = identificaER(er) - 1;
/* Checa se a operação deve ser realizada */
if(er->busy == 1){
/* Checagem de syscall */
if(er->op == SYSCALL){
if(checaSyscall()){
if(get_flag(FLAG_VERBOSE)) printf("Operação de syscall na %s não pode ser executada:\n algum registrador ($v0 ou $a0) esta aguardando um resultado.\n", ER_nomes[identificador]);
return 1;
}
}
if(er->qj == 0 && er->qk == 0){
/* A ULA executára a operação */
if(get_flag(FLAG_DEBUG)) printf("\n| ER %s executando > ", ER_nomes[identificador]);
ulaRet retorno;
retorno = (*p[er->op]) (3, er->vj, er->vk, er->A);
switch (retorno.flag) {
case FLAG_SUCCESS:
buffer_resultados[identificador].dado = retorno.resultado;
buffer_resultados[identificador].validade = 1;
break;
case FLAG_FAIL:
if(get_flag(FLAG_DEBUG)) printf("Operação retornou FAIL\n");
identificaREG(identificador+1); //Limpa Qi na posição de RD da instrução que falhou.
break;
case FLAG_MTHI:
buffer_resultados[identificador].dado = retorno.resultado;
buffer_resultados[identificador].validade = 1;
break;
case FLAG_MTLO:
buffer_resultados[identificador].dado = retorno.resultado;
buffer_resultados[identificador].validade = 1;
break;
case FLAG_MUL_OP:
barr_Lo->er_id = identificador;
barr_Hi->er_id = identificador;
break;
case FLAG_NO_RETURN:
default:
break;
}
last_op[identificador] = er->op;
er->busy = -1;
} else {
/* Operando não estão prontos */
if(get_flag(FLAG_DEBUG)) printf("Operação na ER %s não pode ser executada: Operandos não estão prontos.\n", ER_nomes[identificador]);
}
} else{
if(er->busy > 1){
er->busy = er->busy - 1; //Representa um ciclo gasto na operação
if(get_flag(FLAG_DEBUG)){
printaER(er,0,NULL);
printf("\n");
}
} else {
return 0;
}
}
return 1;
}
/* Ativa uma estação de reserva Load/Store0 para processar sua operação */
/* Params = estacao_reserva* er : Estação de reserva a ser ativada */
int run_load_store(estacao_reserva* er){
unsigned int identificador = identificaER(er) - 1;
/* Checa se a operação deve ser realizada */
if(er->busy == 1){
/* Parte 1 calculo do endereço real */
er->A = er->vj + er->A;
/* Parte 2 Operação */
if(get_flag(FLAG_DEBUG)) printf("\n| ER %s executando > ", ER_nomes[identificador]);
ulaRet retorno;
retorno = (*p[er->op]) (2, er->A, er->vk);
switch (retorno.flag) {
case FLAG_SUCCESS:
if(get_flag(FLAG_DEBUG)) printf("\n\tSUCESS: buffer_resultados[%d] = %d\n", identificador, retorno.resultado);
buffer_resultados[identificador].dado = retorno.resultado;
buffer_resultados[identificador].validade = 1;
break;
case FLAG_FAIL:
if(get_flag(FLAG_DEBUG)) printf("Operação retornou FAIL\n");
break;
case FLAG_NO_RETURN:
default:
break;
}
last_op[identificador] = er->op;
er->busy = -1;
} else{
if(er->busy > 1){
er->busy = er->busy - 1; //Representa um ciclo gasto na operação
if(get_flag(FLAG_DEBUG)){
printaER(er,0,NULL);
printf("\n");
}
} else {
return 0;
}
}
return 1;
}
/* */
int execucao(){
/* Chamar a execução de cada estacao de reserva */
int process = 0;
process += run_add_mul(er_add1);
process += run_add_mul(er_add2);
process += run_add_mul(er_add3);
process += run_add_mul(er_mult1);
process += run_add_mul(er_mult2);
process += run_load_store(er_load1);
process += run_load_store(er_load2);
process += run_load_store(er_load3);
process += run_load_store(er_load4);
process += run_load_store(er_load5);
process += run_load_store(er_store1);
process += run_load_store(er_store2);
process += run_load_store(er_store3);
process += run_load_store(er_store4);
process += run_load_store(er_store5);
if(get_flag(FLAG_DEBUG)){
if(process == 0){
printf("Pulando etapa de execução (Estações de reserva vazias)\n");
}
}
return process;
}
/* Etapa de escrita do pipeline */
/* Utiliza o CDB para transferir os dados do buffer_resultados */
/* para seus respectivos destinos e ERs que precisam do dado */
void escrita(){
unsigned int dado;
int i;
int process = 0;
if(get_flag(FLAG_DEBUG)) printaQi(Qi);
/* Tratamento para as operações de divisão e multiplicação */
if(barr_Hi->er_id != -1){
if(get_flag(FLAG_VERBOSE)) printf("Stremando o dado %d vindo de %s (HI)\n",
barr_Hi->dado, ER_nomes[barr_Hi->er_id - 1]);
stream(barr_Hi->dado, barr_Hi->er_id+1);
barr_Hi->er_id = -1;
process++;
}
if(barr_Lo->er_id != -1){
if(get_flag(FLAG_VERBOSE)) printf("Stremando o dado %d vindo de %s (LO)\n",
barr_Lo->dado, ER_nomes[barr_Lo->er_id - 1]);
stream(barr_Lo->dado, barr_Lo->er_id+1);
barr_Lo->er_id = -1;
process++;
}
for(i = 0; i < NUM_ER; i++){
if (buffer_resultados[i].validade == 1){
dado = buffer_resultados[i].dado;
if(get_flag(FLAG_VERBOSE)) printf("Stremando o dado %d vindo de %s (%s) :\n",
dado, ER_nomes[i], OP_nomes[last_op[i] - 1 ]);
stream(dado, i+1);
process++;
/* No fim do processo limpar o buffer_resultados */
buffer_resultados[i].validade = 0;
}
}
/* Caso uma escrita ocorreu printa o banco de registradores se a flag estiver ativa */
if(process){
if(get_flag(FLAG_REGISTRADORES)) printaRegs(0, NULL);
} else{
if(get_flag(FLAG_VERBOSE)) printf("Pulando etapa de escrita (Nada a ser escrito)\n\n");
}
}
/* Função principal, simula o pipeline*/
unsigned int pipeline(int ciclo){ //pipeline reverso
inst* ptr_instruction;
ptr_instruction = NULL;
int emissao_v = 0;
process = 0;
if(get_flag(FLAG_VERBOSE)) printf("\n======== Ciclo %d ========\n", ciclo);
if(get_flag(FLAG_VERBOSE)) printf("| Escrita >>\n");
escrita();
if(get_flag(FLAG_VERBOSE)) printf("| Execução >>\n");
process = execucao();
if(get_flag(FLAG_VERBOSE)) printf("\n| Emissão >>\n");
ptr_instruction = pegaDaFila(0);
if(ptr_instruction != NULL){
if(get_flag(FLAG_DEBUG)) printaFila();
process = 1;
emissao_v = emissao(*ptr_instruction);
/* Se a emissão ocorreu corretamente retira a instrução da fila */
if (emissao_v){
pegaDaFila(1);
emissao_v = 0;
} else{
if(get_flag(FLAG_DEBUG)) printf("Emissão falhou! Stall criado no pipeline\n");
}
} else {
if(get_flag(FLAG_VERBOSE)) printf("Pulando etapa de emissão (Fila vazia) \n");
}
if(get_flag(FLAG_VERBOSE)) printf("\n| Decodificação >>\n");
if (IR != FLAG_NULL){
process = 1;
decodifica();
} else {
if(get_flag(FLAG_VERBOSE)) printf("Pulando etapa de decodicação (instruction register vazio)\n");
}
if (jump == -1) inconditionalJump(); // Salto inconcidional
if(get_flag(FLAG_VERBOSE)) printf("\n| Busca >>\n");
if (jump > 0){
if(get_flag(FLAG_VERBOSE)) printf("Pulando etapa de busca (Operação de Salto identificada)\n");
}else{
if(get_flag(FLAG_DEBUG)) printf("(PC = %d)\n", getPC());
if (&mem[getPC()] < mem_text_end){
process = 1;
busca();
} else {
if(get_flag(FLAG_DEBUG)) printf("Pulando etapa de busca (PC = END_OF_TEXT)\n");
IR = FLAG_NULL;
}
}
if(get_flag(FLAG_VERBOSE)) printf("\n======== Fim do ciclo %d ========\n", ciclo);
flushes();
return process;
}
/* Função auxiliar: Inicializa o pipeline */
void processadorInit(){
int i;
er_load1 = malloc(sizeof(estacao_reserva));
er_load2 = malloc(sizeof(estacao_reserva));
er_load3 = malloc(sizeof(estacao_reserva));
er_load4 = malloc(sizeof(estacao_reserva));
er_load5 = malloc(sizeof(estacao_reserva));
er_store1 = malloc(sizeof(estacao_reserva));
er_store2 = malloc(sizeof(estacao_reserva));
er_store3 = malloc(sizeof(estacao_reserva));
er_store4 = malloc(sizeof(estacao_reserva));
er_store5 = malloc(sizeof(estacao_reserva));
er_add1 = malloc(sizeof(estacao_reserva));
er_add2 = malloc(sizeof(estacao_reserva));
er_add3 = malloc(sizeof(estacao_reserva));
er_mult1 = malloc(sizeof(estacao_reserva));
er_mult2 = malloc(sizeof(estacao_reserva));
er_load1->busy = 0;
er_load2->busy = 0;
er_load3->busy = 0;
er_load4->busy = 0;
er_load5->busy = 0;
er_store1->busy = 0;
er_store2->busy = 0;
er_store3->busy = 0;
er_store4->busy = 0;
er_store5->busy = 0;
er_add1->busy = 0;
er_add2->busy = 0;
er_add3->busy = 0;
er_mult1->busy = 0;
er_mult2->busy = 0;
fila = malloc(sizeof(fila_inst));
fila->next = NULL;
Qi = calloc(NUM_REGS, sizeof(unsigned int));
barr_Hi = malloc(sizeof(barr_aux));
barr_Lo = malloc(sizeof(barr_aux));
barr_Hi->er_id = FLAG_BUFFER_VAZIO;
barr_Lo->er_id = FLAG_BUFFER_VAZIO;
buffer_resultados = malloc(sizeof(ula_output) * NUM_ER);
last_op = malloc(sizeof(int) * NUM_ER);
for(i = 0; i < NUM_ER; i++){
buffer_resultados[i].validade = 0;
last_op[i] = -1;
}
jump = 0;
}