-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstruction.c
148 lines (145 loc) · 4.85 KB
/
instruction.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
#include "instruction.h"
#include <stdio.h>
#define R 0
#define J 2
#define JAL 3
#define MASK 31
//{"and","or","add","addu","sll","srl","sra","sub","sltu","jr","syscall"};
const int R_FUNC_CODES[] = {0x24,0x25,0x20,0x21,0x00,0x02,0x03,0x22,0x2B,0x08,0xC};
//{"addi", "addiu", "sltiu", "beq", "bne", "lw", "sw", "ori", "lui"};
const int I_OPCODES[] = {0x08,0x09,0x0B,4,5,0x23,0x2B,0x0D,0x0F};
void decode(Instruction *instr, int *pc){
instr->opcode = *pc >> 26 & 0x3F;
switch (instr->opcode){
case R:
instr->rs = *pc >> 21 & MASK;
instr->rt = *pc >> 16 & MASK;
instr->rd = *pc >> 11 & MASK;
instr->shamt = *pc >> 6 & MASK;
instr->funct = *pc & 63;
break;
case J: case JAL:
instr->imm = *pc & 0x03FFFFFF;
break;
default:
instr->rs = *pc >> 21 & MASK;
instr->rt = *pc >> 16 & MASK;
instr->imm = *pc & 0x0000FFFF;
break;
}
}
// returns 1 if executed, 0 if there is a halt instruction
int execute(Instruction *instr, int **pc, long *reg, int *mem, int *cycles, int *memRefs){
printf("[%p] %08X\n", *pc, **pc);
int func, br, ret = 1;
int *temp;
(*pc)++;
switch (instr->opcode){
case R:
*cycles += 4;
for(func = 0, br = 1; br;)
if(R_FUNC_CODES[func] == instr->funct)
br = 0;
else func++;
switch (func){
case 0: //and
reg[instr->rd] = reg[instr->rs] & reg[instr->rt];
break;
case 1: //or
reg[instr->rd] = reg[instr->rs] | reg[instr->rt];
break;
case 2: //add
reg[instr->rd] = reg[instr->rs] + reg[instr->rt];
break;
case 3: //addu
reg[instr->rd] = reg[instr->rs] + reg[instr->rt];
break;
case 4: //sll
reg[instr->rd] = reg[instr->rt] << instr->shamt;
break;
case 5: //srl
reg[instr->rd] = (unsigned int)reg[instr->rt] >> instr->shamt;
break;
case 6: //sra
reg[instr->rd] = reg[instr->rt] >> instr->shamt;
break;
case 7: //sub
reg[instr->rd] = reg[instr->rs] - reg[instr->rt];
break;
case 8: //sltu
reg[instr->rd] = ((unsigned int)reg[instr->rs] < (unsigned int)reg[instr->rt]) ? 1 : 0;
break;
case 9: //jr
temp = *pc;
*pc = (int *)reg[instr->rs];
reg[31] = (long)temp;
break;
case 10: //syscall (assume halt)
ret = 0;
*cycles -= 3;
break;
}
break;
case J:
*pc = mem + instr->imm;
*cycles += 3;
break;
case JAL:
reg[31] = (long)*pc;
*pc = mem + instr->imm;
*cycles += 3;
break;
default:
for(func = 0, br = 1; br;)
if(I_OPCODES[func] == instr->opcode)
br = 0;
else func++;
switch (func){
case 0: //addi
if(instr->imm >> 15) instr->imm |= 0xFFFF0000;
reg[instr->rt] = reg[instr->rs] + instr->imm;
*cycles += 4;
break;
case 1: //addiu
reg[instr->rt] = (unsigned int)reg[instr->rs] + (unsigned int)instr->imm;
*cycles += 4;
break;
case 2: //sltiu
reg[instr->rt] = reg[instr->rs] < instr->imm ? 1 : 0;
*cycles += 4;
break;
case 3: //beq
if(instr->imm >> 15) instr->imm |= 0xFFFFFFFFFFFF0000;
if((int)reg[instr->rs] == (int)reg[instr->rt]) *pc += instr->imm - 1;
*cycles += 3;
break;
case 4: //bne
if(instr->imm >> 15) instr->imm |= 0xFFFFFFFFFFFF0000;
if((int)reg[instr->rs] != (int)reg[instr->rt]) *pc += instr->imm - 1;
*cycles += 3;
break;
case 5: //lw
if(instr->imm >> 15) instr->imm |= 0xFFFF0000;
reg[instr->rt] = mem[reg[instr->rs] + instr->imm];
(*memRefs)++;
*cycles += 5;
break;
case 6: //sw
if(instr->imm >> 15) instr->imm |= 0xFFFF0000;
mem[reg[instr->rs] + instr->imm] = reg[instr->rt];
(*memRefs)++;
*cycles += 4;
break;
case 7: //ori
reg[instr->rt] = reg[instr->rs] | instr->imm;
*cycles += 4;
break;
case 8: //lui
reg[instr->rt] = instr->imm << 16;
*cycles += 4;
break;
}
break;
}
return ret;
}