-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecode.c
64 lines (53 loc) · 1.44 KB
/
decode.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
#include <stdio.h>
#include <stdlib.h>
#include "decode.h"
#include "fetch.h"
#define R 0
#define J 2
#define JAL 3
#define MASK 31
Decode *DecodeInit(long *reg, FetchDecode *inBucket,
DecodeExecute *outBucket) {
Decode *res = malloc(sizeof(Decode));
res->count = 0;
res->inBucket = inBucket;
res->outBucket = outBucket;
res->reg = reg;
return res;
}
/**
* Decode phase splits instruction into it's corresponding sections and looks
* up register values in the register file.
*/
void DecodePhase(Decode *decode) {
FetchDecode *in = decode->inBucket;
DecodeExecute *out = decode->outBucket;
int instruction = decode->inBucket->instruction;
if (!in->ready)
return;
in->ready = 0;
if (instruction == 0) {
return;
}
out->opcode = instruction >> 26 & 0x3F;
switch (out->opcode){
case R:
out->rs = decode->reg[instruction >> 21 & MASK];
out->rt = decode->reg[instruction >> 16 & MASK];
out->rd = instruction >> 11 & MASK;
out->shamt = instruction >> 6 & MASK;
out->funct = instruction & 63;
break;
case J: case JAL:
out->imm = instruction & 0x03FFFFFF;
break;
default:
out->rs = decode->reg[instruction >> 21 & MASK];
out->rt = instruction >> 16 & MASK;
out->rtval = decode->reg[out->rt];
out->imm = instruction & 0x0000FFFF;
break;
}
decode->count++;
out->ready = 1;
}