This repository has been archived by the owner on May 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmipsopfuncs.c
417 lines (338 loc) · 11.1 KB
/
mipsopfuncs.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
#include <stdio.h>
#include <stdlib.h>
#include <mipscpu.h>
#include <mipsopfuncs.h>
uint32_t signext32(uint32_t val, uint8_t bits)
{
if ((val >> (bits - 1)) & 1)
val |= 0xffffffff << (32 - bits);
return val;
}
void rfe(struct mipscpu* cpu, struct instruction* ins)
{
uint8_t int_mask = cpu->cp0.status >> 8;
uint8_t prev_status = (cpu->cp0.status >> 2) & 0b1111;
cpu->cp0.status = (int_mask << 8) | prev_status;
cpu->pc = cpu->cp0.epc;
}
void mtc0(struct mipscpu* cpu, struct instruction* ins)
{
switch (ins->rformrt)
{
case 12:
cpu->cp0.status = cpu->r[ins->rformrd];
break;
case 13:
cpu->cp0.cause = cpu->r[ins->rformrd];
break;
case 14:
cpu->cp0.epc = cpu->r[ins->rformrd];
break;
}
cpu->pc += INS_SIZE;
}
void mfc0(struct mipscpu* cpu, struct instruction* ins)
{
switch (ins->rformrt)
{
case 12:
cpu->r[ins->rformrd] = cpu->cp0.status;
break;
case 13:
cpu->r[ins->rformrd] = cpu->cp0.cause;
break;
case 14:
cpu->r[ins->rformrd] = cpu->cp0.epc;
break;
}
cpu->pc += INS_SIZE;
}
void sll0(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[ins->rformrd] = cpu->r[ins->rformrt] << ins->rformshift;
cpu->pc += INS_SIZE;
}
void srl2(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[ins->rformrd] = cpu->r[ins->rformrt] >> ins->rformshift;
cpu->pc += INS_SIZE;
}
void sra3(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[ins->rformrd] = cpu->r[ins->rformrt] >> ins->rformshift;
if ((cpu->r[ins->rformrt] >> 31) & 1)
cpu->r[ins->rformrd] |= 0xffffffff << (32 - ins->rformshift);
cpu->pc += INS_SIZE;
}
void sllv4(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[ins->rformrd] = cpu->r[ins->rformrt] << cpu->r[ins->rformrs];
cpu->pc += INS_SIZE;
}
void srlv6(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[ins->rformrd] = cpu->r[ins->rformrt] >> cpu->r[ins->rformrs];
cpu->pc += INS_SIZE;
}
void srav7(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[ins->rformrd] = cpu->r[ins->rformrt] >> cpu->r[ins->rformrs];
if ((cpu->r[ins->rformrt] >> 31) & 1)
cpu->r[ins->rformrd] |= 0xffffffff << (32 - cpu->r[ins->rformrs]);
cpu->pc += INS_SIZE;
}
void jr8(struct mipscpu* cpu, struct instruction* ins)
{
cpu->pc = cpu->r[ins->rformrs];
}
void jalr9(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[ins->rformrd] = cpu->pc + INS_SIZE;
cpu->pc = cpu->r[ins->rformrs];
}
void syscall12(struct mipscpu* cpu, struct instruction* ins)
{
gen_exception(cpu, SYSCALL, 0);
}
void mfhi16(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[ins->rformrd] = cpu->hi;
cpu->pc += INS_SIZE;
}
void mthi17(struct mipscpu* cpu, struct instruction* ins)
{
cpu->hi = cpu->r[ins->rformrs];
cpu->pc += INS_SIZE;
}
void mflo18(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[ins->rformrd] = cpu->lo;
cpu->pc += INS_SIZE;
}
void mtlo19(struct mipscpu* cpu, struct instruction* ins)
{
cpu->lo = cpu->r[ins->rformrs];
cpu->pc += INS_SIZE;
}
void mult24(struct mipscpu* cpu, struct instruction* ins)
{
int64_t result = (int64_t)cpu->r[ins->rformrs] * (int64_t)cpu->r[ins->rformrt];
cpu->hi = (result >> 32) & 0xffffffff;
cpu->lo = result & 0xffffffff;
cpu->pc += INS_SIZE;
}
void multu25(struct mipscpu* cpu, struct instruction* ins)
{
uint64_t result = (uint64_t)cpu->r[ins->rformrs] * (uint64_t)cpu->r[ins->rformrt];
cpu->hi = (result >> 32) & 0xffffffff;
cpu->lo = result & 0xffffffff;
cpu->pc += INS_SIZE;
}
void div26(struct mipscpu* cpu, struct instruction* ins)
{
cpu->hi = ((int32_t)cpu->r[ins->rformrs] / (int32_t)cpu->r[ins->rformrt]) & 0xffffffff;
cpu->lo = (int32_t)cpu->r[ins->rformrs] % (int32_t)cpu->r[ins->rformrt];
if (cpu->r[ins->rformrt] == 0)
gen_exception(cpu, DBUS, 0);
else
cpu->pc += INS_SIZE;
}
void divu27(struct mipscpu* cpu, struct instruction* ins)
{
cpu->hi = (cpu->r[ins->rformrs] / cpu->r[ins->rformrt]) & 0xffffffff;
cpu->lo = cpu->r[ins->rformrs] % cpu->r[ins->rformrt];
if (cpu->r[ins->rformrt] == 0)
gen_exception(cpu, DBUS, 0);
else
cpu->pc += INS_SIZE;
}
void add32(struct mipscpu* cpu, struct instruction* ins)
{
int64_t res64 = (int64_t)cpu->r[ins->rformrs] + (int64_t)cpu->r[ins->rformrt];
int32_t res32 = (int32_t)cpu->r[ins->rformrs] + (int32_t)cpu->r[ins->rformrt];
cpu->r[ins->rformrd] = res32;
if (res64 != res32)
gen_exception(cpu, OVF, 0);
else
cpu->pc += INS_SIZE;
}
void addu33(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[ins->rformrd] = cpu->r[ins->rformrs] + cpu->r[ins->rformrt];
cpu->pc += INS_SIZE;
}
void sub34(struct mipscpu* cpu, struct instruction* ins)
{
int64_t res64 = (int64_t)cpu->r[ins->rformrs] - (int64_t)cpu->r[ins->rformrt];
int32_t res32 = (int32_t)cpu->r[ins->rformrs] - (int32_t)cpu->r[ins->rformrt];
cpu->r[ins->rformrd] = res32;
if (res64 != res32)
gen_exception(cpu, OVF, 0);
else
cpu->pc += INS_SIZE;
}
void subu35(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[ins->rformrd] = cpu->r[ins->rformrs] - cpu->r[ins->rformrt];
cpu->pc += INS_SIZE;
}
void and36(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[ins->rformrd] = cpu->r[ins->rformrs] & cpu->r[ins->rformrt];
cpu->pc += INS_SIZE;
}
void or37(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[ins->rformrd] = cpu->r[ins->rformrs] | cpu->r[ins->rformrt];
cpu->pc += INS_SIZE;
}
void xor38(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[ins->rformrd] = cpu->r[ins->rformrs] ^ cpu->r[ins->rformrt];
cpu->pc += INS_SIZE;
}
void nor39(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[ins->rformrd] = !(cpu->r[ins->rformrs] | cpu->r[ins->rformrt]);
cpu->pc += INS_SIZE;
}
void slt42(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[ins->rformrd] = (int32_t)cpu->r[ins->rformrs] < (int32_t)cpu->r[ins->rformrt];
cpu->pc += INS_SIZE;
}
void sltu43(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[ins->rformrd] = cpu->r[ins->rformrs] < cpu->r[ins->rformrt];
cpu->pc += INS_SIZE;
}
void j2(struct mipscpu* cpu, struct instruction* ins)
{
cpu->pc = ((cpu->pc + INS_SIZE) & 4026531840) | (ins->jformaddress << 2);
}
void jal3(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[31] = cpu->pc + INS_SIZE;
cpu->pc = ((cpu->pc + INS_SIZE) & 4026531840) | (ins->jformaddress << 2);
}
void beq4(struct mipscpu* cpu, struct instruction* ins)
{
if (cpu->r[ins->iformrs] == cpu->r[ins->iformrt])
cpu->pc += signext32(ins->iformimm << 2, 18);
cpu->pc += INS_SIZE;
}
void bne5(struct mipscpu* cpu, struct instruction* ins)
{
if (cpu->r[ins->iformrs] != cpu->r[ins->iformrt])
cpu->pc += signext32(ins->iformimm << 2, 18);
cpu->pc += INS_SIZE;
}
void blez6(struct mipscpu* cpu, struct instruction* ins)
{
if ((int32_t)cpu->r[ins->iformrs] <= 0)
cpu->pc += signext32(ins->iformimm << 2, 18);
cpu->pc += INS_SIZE;
}
void bgtz7(struct mipscpu* cpu, struct instruction* ins)
{
if ((int32_t)cpu->r[ins->iformrs] > 0)
cpu->pc += signext32(ins->iformimm << 2, 18);
cpu->pc += INS_SIZE;
}
void addi8(struct mipscpu* cpu, struct instruction* ins)
{
int64_t res64 = (int64_t)cpu->r[ins->iformrs] + (int64_t)signext32(ins->iformimm, 16);
int32_t res32 = (int32_t)cpu->r[ins->iformrs] + (int32_t)signext32(ins->iformimm, 16);
cpu->r[ins->iformrt] = res32;
if (res64 != res32)
gen_exception(cpu, OVF, 0);
else
cpu->pc += INS_SIZE;
}
void addiu9(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[ins->iformrt] = cpu->r[ins->iformrs] + signext32(ins->iformimm, 16);
cpu->pc += INS_SIZE;
}
void slti10(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[ins->iformrt] = (int32_t)cpu->r[ins->iformrs] < (int32_t)signext32(ins->iformimm, 16);
cpu->pc += INS_SIZE;
}
void sltiu11(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[ins->iformrt] = cpu->r[ins->iformrs] < signext32(ins->iformimm, 16);
cpu->pc += INS_SIZE;
}
void andi12(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[ins->iformrt] = cpu->r[ins->iformrs] & (uint32_t)ins->iformimm;
cpu->pc += INS_SIZE;
}
void ori13(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[ins->iformrt] = cpu->r[ins->iformrs] | (uint32_t)ins->iformimm;
cpu->pc += INS_SIZE;
}
void xori14(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[ins->iformrt] = cpu->r[ins->iformrs] ^ (uint32_t)ins->iformimm;
cpu->pc += INS_SIZE;
}
void lui15(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[ins->iformrt] = (((uint32_t)ins->iformimm) << 16);
cpu->pc += INS_SIZE;
}
void lb32(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[ins->iformrt] = signext32(cpu->memory[cpu->r[ins->iformrs] + signext32(ins->iformimm, 16)], 8);
cpu->pc += INS_SIZE;
}
void lh33(struct mipscpu* cpu, struct instruction* ins)
{
uint8_t byte1 = cpu->memory[cpu->r[ins->iformrs] + signext32(ins->iformimm, 16)];
uint8_t byte2 = cpu->memory[cpu->r[ins->iformrs] + signext32(ins->iformimm, 16) + 1];
cpu->r[ins->iformrt] = signext32((byte2 << 8) | byte1, 16);
cpu->pc += INS_SIZE;
}
void lw35(struct mipscpu* cpu, struct instruction* ins)
{
uint8_t byte1 = cpu->memory[cpu->r[ins->iformrs] + signext32(ins->iformimm, 16)];
uint8_t byte2 = cpu->memory[cpu->r[ins->iformrs] + signext32(ins->iformimm, 16) + 1];
uint8_t byte3 = cpu->memory[cpu->r[ins->iformrs] + signext32(ins->iformimm, 16) + 2];
uint8_t byte4 = cpu->memory[cpu->r[ins->iformrs] + signext32(ins->iformimm, 16) + 3];
cpu->r[ins->iformrt] = (byte4 << 24) | (byte3 << 16) | (byte2 << 8) | byte1;
cpu->pc += INS_SIZE;
}
void lbu36(struct mipscpu* cpu, struct instruction* ins)
{
cpu->r[ins->iformrt] = (uint32_t)cpu->memory[cpu->r[ins->iformrs] + signext32(ins->iformimm, 16)];;
cpu->pc += INS_SIZE;
}
void lhu37(struct mipscpu* cpu, struct instruction* ins)
{
uint8_t byte1 = cpu->memory[cpu->r[ins->iformrs] + signext32(ins->iformimm, 16)];
uint8_t byte2 = cpu->memory[cpu->r[ins->iformrs] + signext32(ins->iformimm, 16) + 1];
cpu->r[ins->iformrt] = (uint32_t)((byte2 << 8) | byte1);
cpu->pc += INS_SIZE;
}
void sb40(struct mipscpu* cpu, struct instruction* ins)
{
cpu->memory[cpu->r[ins->iformrs] + signext32(ins->iformimm, 16)] = cpu->r[ins->iformrt] & 0xff;
cpu->pc += INS_SIZE;
}
void sh41(struct mipscpu* cpu, struct instruction* ins)
{
cpu->memory[cpu->r[ins->iformrs] + signext32(ins->iformimm, 16)] = cpu->r[ins->iformrt] & 0xff;
cpu->memory[cpu->r[ins->iformrs] + signext32(ins->iformimm, 16) + 1] = (cpu->r[ins->iformrt] >> 8) & 0xff;
cpu->pc += INS_SIZE;
}
void sw43(struct mipscpu* cpu, struct instruction* ins)
{
cpu->memory[cpu->r[ins->iformrs] + signext32(ins->iformimm, 16)] = cpu->r[ins->iformrt] & 0xff;
cpu->memory[cpu->r[ins->iformrs] + signext32(ins->iformimm, 16) + 1] = (cpu->r[ins->iformrt] >> 8) & 0xff;
cpu->memory[cpu->r[ins->iformrs] + signext32(ins->iformimm, 16) + 2] = (cpu->r[ins->iformrt] >> 16) & 0xff;
cpu->memory[cpu->r[ins->iformrs] + signext32(ins->iformimm, 16) + 3] = (cpu->r[ins->iformrt] >> 24) & 0xff;
cpu->pc += INS_SIZE;
}