-
Notifications
You must be signed in to change notification settings - Fork 1
/
instructions.h
108 lines (99 loc) · 2.38 KB
/
instructions.h
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
#ifndef _INSTRUCTIONS_H
#define _INSTRUCTIONS_H
#include "mtypes.h"
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
// I Type
void lw(instruction_t i, proc_t *p);
void sw(instruction_t i, proc_t *p);
void addi(instruction_t i, proc_t *p);
void ori(instruction_t i, proc_t *p);
void andi(instruction_t i, proc_t *p);
void slti(instruction_t i, proc_t *p);
void beq(instruction_t i, proc_t *p);
void bne(instruction_t i, proc_t *p);
void lui(instruction_t i, proc_t *p);
// R Type
void add(instruction_t i, proc_t *p);
void sub(instruction_t i, proc_t *p);
void or(instruction_t i, proc_t *p);
void and(instruction_t i, proc_t *p);
void slt(instruction_t i, proc_t *p);
void sll(instruction_t i, proc_t *p);
void srl(instruction_t i, proc_t *p);
void jr(instruction_t i, proc_t *p);
// J Type
void j(instruction_t i, proc_t *p);
void jal(instruction_t i, proc_t *p);
static inline void (*get_j_instruction(uint32_t opcode)) (instruction_t, proc_t *) {
switch (opcode) {
case 0b000011:
return &jal;
case 0b000010:
return &j;
default:
fprintf(stderr, "Got invalid opcode value in j instruction: %d\n", opcode);
exit(-1);
}
}
static inline void (*get_r_instruction(uint32_t opcode)) (instruction_t, proc_t *) {
switch (opcode) {
case 0b100000:
return &add;
case 0b100010:
return ⊂
case 0b100101:
return ∨
case 0b100100:
return ∧
case 0b101010:
return &slt;
case 0b000000:
return &sll;
case 0b000010:
return &srl;
case 0b001000:
return &jr;
default:
fprintf(stderr, "Got invalid opcode value in r instruction: %d\n", opcode);
exit(-1);
}
}
static inline void (*get_i_instruction(uint32_t function)) (instruction_t, proc_t *) {
switch (function) {
case 0b100011:
return &lw;
case 0b101011:
return &sw;
case 0b001000:
return &addi;
case 0b001101:
return &ori;
case 0b001100:
return &andi;
case 0b001010:
return &slti;
case 0b000100:
return &beq;
case 0b000101:
return =⃥
case 0b001111:
return &lui;
default:
fprintf(stderr, "Got invalid function value in i instruction: %d\n", function);
exit(-1);
}
}
static inline void (*get_instruction(instruction_t i)) (instruction_t, proc_t *) {
switch (i.opcode) {
case 0:
return get_r_instruction(i.function);
case 2:
case 3:
return get_j_instruction(i.opcode);
default:
return get_i_instruction(i.opcode);
}
}
#endif