tac0S
Template Affectional Command Operating System
mipssim.h
Go to the documentation of this file.
1 
12 #ifndef MIPSSIM_H
13 #define MIPSSIM_H
14 
15 #include "copyright.h"
16 
27 #define OP_ADD 1
28 #define OP_ADDI 2
29 #define OP_ADDIU 3
30 #define OP_ADDU 4
31 #define OP_AND 5
32 #define OP_ANDI 6
33 #define OP_BEQ 7
34 #define OP_BGEZ 8
35 #define OP_BGEZAL 9
36 #define OP_BGTZ 10
37 #define OP_BLEZ 11
38 #define OP_BLTZ 12
39 #define OP_BLTZAL 13
40 #define OP_BNE 14
41 
42 #define OP_DIV 16
43 #define OP_DIVU 17
44 #define OP_J 18
45 #define OP_JAL 19
46 #define OP_JALR 20
47 #define OP_JR 21
48 #define OP_LB 22
49 #define OP_LBU 23
50 #define OP_LH 24
51 #define OP_LHU 25
52 #define OP_LUI 26
53 #define OP_LW 27
54 #define OP_LWL 28
55 #define OP_LWR 29
56 
57 #define OP_MFHI 31
58 #define OP_MFLO 32
59 
60 #define OP_MTHI 34
61 #define OP_MTLO 35
62 #define OP_MULT 36
63 #define OP_MULTU 37
64 #define OP_NOR 38
65 #define OP_OR 39
66 #define OP_ORI 40
67 #define OP_RFE 41
68 #define OP_SB 42
69 #define OP_SH 43
70 #define OP_SLL 44
71 #define OP_SLLV 45
72 #define OP_SLT 46
73 #define OP_SLTI 47
74 #define OP_SLTIU 48
75 #define OP_SLTU 49
76 #define OP_SRA 50
77 #define OP_SRAV 51
78 #define OP_SRL 52
79 #define OP_SRLV 53
80 #define OP_SUB 54
81 #define OP_SUBU 55
82 #define OP_SW 56
83 #define OP_SWL 57
84 #define OP_SWR 58
85 #define OP_XOR 59
86 #define OP_XORI 60
87 #define OP_SYSCALL 61
88 #define OP_UNIMP 62
89 #define OP_RES 63
90 #define MaxOpcode 63
91 
92 /*
93  * Miscellaneous definitions:
94  */
95 
96 #define IndexToAddr(x) ((x) << 2)
97 
98 #define SIGN_BIT 0x80000000
99 #define R31 31
100 
101 /*
102  * The table below is used to translate bits 31:26 of the instruction
103  * into a value suitable for the "opCode" field of a MemWord structure,
104  * or into a special value for further decoding.
105  */
106 
107 #define SPECIAL 100
108 #define BCOND 101
109 
110 #define IFMT 1
111 #define JFMT 2
112 #define RFMT 3
113 
114 struct OpInfo {
115  int opCode; /* Translated op code. */
116  int format; /* Format type (IFMT or JFMT or RFMT) */
117 };
118 
119 static OpInfo opTable[] = {
120  {SPECIAL, RFMT}, {BCOND, IFMT}, {OP_J, JFMT}, {OP_JAL, JFMT},
121  {OP_BEQ, IFMT}, {OP_BNE, IFMT}, {OP_BLEZ, IFMT}, {OP_BGTZ, IFMT},
122  {OP_ADDI, IFMT}, {OP_ADDIU, IFMT}, {OP_SLTI, IFMT}, {OP_SLTIU, IFMT},
123  {OP_ANDI, IFMT}, {OP_ORI, IFMT}, {OP_XORI, IFMT}, {OP_LUI, IFMT},
124  {OP_UNIMP, IFMT}, {OP_UNIMP, IFMT}, {OP_UNIMP, IFMT}, {OP_UNIMP, IFMT},
125  {OP_RES, IFMT}, {OP_RES, IFMT}, {OP_RES, IFMT}, {OP_RES, IFMT},
126  {OP_RES, IFMT}, {OP_RES, IFMT}, {OP_RES, IFMT}, {OP_RES, IFMT},
127  {OP_RES, IFMT}, {OP_RES, IFMT}, {OP_RES, IFMT}, {OP_RES, IFMT},
128  {OP_LB, IFMT}, {OP_LH, IFMT}, {OP_LWL, IFMT}, {OP_LW, IFMT},
129  {OP_LBU, IFMT}, {OP_LHU, IFMT}, {OP_LWR, IFMT}, {OP_RES, IFMT},
130  {OP_SB, IFMT}, {OP_SH, IFMT}, {OP_SWL, IFMT}, {OP_SW, IFMT},
131  {OP_RES, IFMT}, {OP_RES, IFMT}, {OP_SWR, IFMT}, {OP_RES, IFMT},
132  {OP_UNIMP, IFMT}, {OP_UNIMP, IFMT}, {OP_UNIMP, IFMT}, {OP_UNIMP, IFMT},
133  {OP_RES, IFMT}, {OP_RES, IFMT}, {OP_RES, IFMT}, {OP_RES, IFMT},
134  {OP_UNIMP, IFMT}, {OP_UNIMP, IFMT}, {OP_UNIMP, IFMT}, {OP_UNIMP, IFMT},
135  {OP_RES, IFMT}, {OP_RES, IFMT}, {OP_RES, IFMT}, {OP_RES, IFMT}
136 };
137 
138 /*
139  * The table below is used to convert the "funct" field of SPECIAL
140  * instructions into the "opCode" field of a MemWord.
141  */
142 
143 static int specialTable[] = {
144  OP_SLL, OP_RES, OP_SRL, OP_SRA, OP_SLLV, OP_RES, OP_SRLV, OP_SRAV,
145  OP_JR, OP_JALR, OP_RES, OP_RES, OP_SYSCALL, OP_UNIMP, OP_RES, OP_RES,
146  OP_MFHI, OP_MTHI, OP_MFLO, OP_MTLO, OP_RES, OP_RES, OP_RES, OP_RES,
147  OP_MULT, OP_MULTU, OP_DIV, OP_DIVU, OP_RES, OP_RES, OP_RES, OP_RES,
148  OP_ADD, OP_ADDU, OP_SUB, OP_SUBU, OP_AND, OP_OR, OP_XOR, OP_NOR,
149  OP_RES, OP_RES, OP_SLT, OP_SLTU, OP_RES, OP_RES, OP_RES, OP_RES,
150  OP_RES, OP_RES, OP_RES, OP_RES, OP_RES, OP_RES, OP_RES, OP_RES,
151  OP_RES, OP_RES, OP_RES, OP_RES, OP_RES, OP_RES, OP_RES, OP_RES
152 };
153 
154 
155 // Stuff to help print out each instruction, for debugging
156 
157 enum RegType { NONE, RS, RT, RD, EXTRA };
158 
159 struct OpString {
160  const char *string; // Printed version of instruction
161  RegType args[3];
162 };
163 
164 static struct OpString opStrings[] = {
165  {"Shouldn't happen", {NONE, NONE, NONE}},
166  {"ADD r%d,r%d,r%d", {RD, RS, RT}},
167  {"ADDI r%d,r%d,%d", {RT, RS, EXTRA}},
168  {"ADDIU r%d,r%d,%d", {RT, RS, EXTRA}},
169  {"ADDU r%d,r%d,r%d", {RD, RS, RT}},
170  {"AND r%d,r%d,r%d", {RD, RS, RT}},
171  {"ANDI r%d,r%d,%d", {RT, RS, EXTRA}},
172  {"BEQ r%d,r%d,%d", {RS, RT, EXTRA}},
173  {"BGEZ r%d,%d", {RS, EXTRA, NONE}},
174  {"BGEZAL r%d,%d", {RS, EXTRA, NONE}},
175  {"BGTZ r%d,%d", {RS, EXTRA, NONE}},
176  {"BLEZ r%d,%d", {RS, EXTRA, NONE}},
177  {"BLTZ r%d,%d", {RS, EXTRA, NONE}},
178  {"BLTZAL r%d,%d", {RS, EXTRA, NONE}},
179  {"BNE r%d,r%d,%d", {RS, RT, EXTRA}},
180  {"Shouldn't happen", {NONE, NONE, NONE}},
181  {"DIV r%d,r%d", {RS, RT, NONE}},
182  {"DIVU r%d,r%d", {RS, RT, NONE}},
183  {"J %d", {EXTRA, NONE, NONE}},
184  {"JAL %d", {EXTRA, NONE, NONE}},
185  {"JALR r%d,r%d", {RD, RS, NONE}},
186  {"JR r%d,r%d", {RD, RS, NONE}},
187  {"LB r%d,%d(r%d)", {RT, EXTRA, RS}},
188  {"LBU r%d,%d(r%d)", {RT, EXTRA, RS}},
189  {"LH r%d,%d(r%d)", {RT, EXTRA, RS}},
190  {"LHU r%d,%d(r%d)", {RT, EXTRA, RS}},
191  {"LUI r%d,%d", {RT, EXTRA, NONE}},
192  {"LW r%d,%d(r%d)", {RT, EXTRA, RS}},
193  {"LWL r%d,%d(r%d)", {RT, EXTRA, RS}},
194  {"LWR r%d,%d(r%d)", {RT, EXTRA, RS}},
195  {"Shouldn't happen", {NONE, NONE, NONE}},
196  {"MFHI r%d", {RD, NONE, NONE}},
197  {"MFLO r%d", {RD, NONE, NONE}},
198  {"Shouldn't happen", {NONE, NONE, NONE}},
199  {"MTHI r%d", {RS, NONE, NONE}},
200  {"MTLO r%d", {RS, NONE, NONE}},
201  {"MULT r%d,r%d", {RS, RT, NONE}},
202  {"MULTU r%d,r%d", {RS, RT, NONE}},
203  {"NOR r%d,r%d,r%d", {RD, RS, RT}},
204  {"OR r%d,r%d,r%d", {RD, RS, RT}},
205  {"ORI r%d,r%d,%d", {RT, RS, EXTRA}},
206  {"RFE", {NONE, NONE, NONE}},
207  {"SB r%d,%d(r%d)", {RT, EXTRA, RS}},
208  {"SH r%d,%d(r%d)", {RT, EXTRA, RS}},
209  {"SLL r%d,r%d,%d", {RD, RT, EXTRA}},
210  {"SLLV r%d,r%d,r%d", {RD, RT, RS}},
211  {"SLT r%d,r%d,r%d", {RD, RS, RT}},
212  {"SLTI r%d,r%d,%d", {RT, RS, EXTRA}},
213  {"SLTIU r%d,r%d,%d", {RT, RS, EXTRA}},
214  {"SLTU r%d,r%d,r%d", {RD, RS, RT}},
215  {"SRA r%d,r%d,%d", {RD, RT, EXTRA}},
216  {"SRAV r%d,r%d,r%d", {RD, RT, RS}},
217  {"SRL r%d,r%d,%d", {RD, RT, EXTRA}},
218  {"SRLV r%d,r%d,r%d", {RD, RT, RS}},
219  {"SUB r%d,r%d,r%d", {RD, RS, RT}},
220  {"SUBU r%d,r%d,r%d", {RD, RS, RT}},
221  {"SW r%d,%d(r%d)", {RT, EXTRA, RS}},
222  {"SWL r%d,%d(r%d)", {RT, EXTRA, RS}},
223  {"SWR r%d,%d(r%d)", {RT, EXTRA, RS}},
224  {"XOR r%d,r%d,r%d", {RD, RS, RT}},
225  {"XORI r%d,r%d,%d", {RT, RS, EXTRA}},
226  {"SYSCALL", {NONE, NONE, NONE}},
227  {"Unimplemented", {NONE, NONE, NONE}},
228  {"Reserved", {NONE, NONE, NONE}}
229  };
230 
231 #endif // MIPSSIM_H
Definition: mipssim.h:114
Definition: mipssim.h:159
#define OP_ADD
Definition: mipssim.h:27