-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor_r_type.cpp
500 lines (408 loc) · 12.7 KB
/
processor_r_type.cpp
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#include "processor.h"
#include "exceptions.h"
#include "processor_utils.h"
void processor::init_r_type()
{
r_type_methods[0] = &processor::r_type_00;
r_type_methods[1] = &processor::r_type_01;
r_type_methods[2] = &processor::r_type_02;
r_type_methods[3] = &processor::r_type_03;
r_type_methods[4] = &processor::r_type_04;
r_type_methods[5] = &processor::r_type_05;
r_type_methods[6] = &processor::r_type_06;
r_type_methods[7] = &processor::r_type_07;
r_type_methods[8] = &processor::r_type_08;
r_type_methods[9] = &processor::r_type_09;
r_type_methods[10] = &processor::r_type_0a;
r_type_methods[11] = &processor::r_type_0b;
r_type_methods[12] = &processor::r_type_0c;
r_type_methods[13] = &processor::r_type_0d;
r_type_methods[14] = &processor::r_type_0e;
r_type_methods[15] = &processor::r_type_0f;
r_type_methods[16] = &processor::r_type_10;
r_type_methods[17] = &processor::r_type_11;
r_type_methods[18] = &processor::r_type_12;
r_type_methods[19] = &processor::r_type_13;
r_type_methods[20] = &processor::r_type_14;
r_type_methods[21] = &processor::r_type_15;
r_type_methods[22] = &processor::r_type_16;
r_type_methods[23] = &processor::r_type_17;
r_type_methods[24] = &processor::r_type_18;
r_type_methods[25] = &processor::r_type_19;
r_type_methods[26] = &processor::r_type_1a;
r_type_methods[27] = &processor::r_type_1b;
r_type_methods[28] = &processor::r_type_1c;
r_type_methods[29] = &processor::r_type_1d;
r_type_methods[30] = &processor::r_type_1e;
r_type_methods[31] = &processor::r_type_1f;
r_type_methods[32] = &processor::r_type_20;
r_type_methods[33] = &processor::r_type_21;
r_type_methods[34] = &processor::r_type_22;
r_type_methods[35] = &processor::r_type_23;
r_type_methods[36] = &processor::r_type_24;
r_type_methods[37] = &processor::r_type_25;
r_type_methods[38] = &processor::r_type_26;
r_type_methods[39] = &processor::r_type_27;
r_type_methods[40] = &processor::r_type_28;
r_type_methods[41] = &processor::r_type_29;
r_type_methods[42] = &processor::r_type_2a;
r_type_methods[43] = &processor::r_type_2b;
r_type_methods[44] = &processor::r_type_2c;
r_type_methods[45] = &processor::r_type_2d;
r_type_methods[46] = &processor::r_type_2e;
r_type_methods[47] = &processor::r_type_2f;
r_type_methods[48] = &processor::r_type_30;
r_type_methods[49] = &processor::r_type_31;
r_type_methods[50] = &processor::r_type_32;
r_type_methods[51] = &processor::r_type_33;
r_type_methods[52] = &processor::r_type_34;
r_type_methods[53] = &processor::r_type_35;
r_type_methods[54] = &processor::r_type_36;
r_type_methods[55] = &processor::r_type_37;
r_type_methods[56] = &processor::r_type_38;
r_type_methods[57] = &processor::r_type_39;
r_type_methods[58] = &processor::r_type_3a;
r_type_methods[59] = &processor::r_type_3b;
r_type_methods[60] = &processor::r_type_3c;
r_type_methods[61] = &processor::r_type_3d;
r_type_methods[62] = &processor::r_type_3e;
r_type_methods[63] = &processor::r_type_3f;
}
void processor::r_type_00(uint32_t instruction) // NOP / SLL
{
uint8_t sa = get_SA(instruction);
uint8_t rd = get_RD(instruction);
uint8_t rt = get_RT(instruction);
if (sa) // if sa==0 then NOP
set_register_32b_se(rd, get_register_32b_unsigned(rt) << sa);
}
void processor::r_type_01(uint32_t instruction)
{
// pdc -> dc_log("r_type_01 not implemented");
}
void processor::r_type_02(uint32_t instruction) // SRL / ROTR
{
uint8_t sa = get_SA(instruction);
uint8_t rd = get_RD(instruction);
uint8_t rt = get_RT(instruction);
if (IS_BIT_OFF0_SET(21, instruction))
set_register_32b(rd, rotate_right(get_register_32b_unsigned(rt), sa, 32));
else // SRL
set_register_32b_se(rd, get_register_32b_unsigned(rt) >> sa);
}
void processor::r_type_03(uint32_t instruction) // SRA
{
uint8_t sa = get_SA(instruction);
uint8_t rd = get_RD(instruction);
uint8_t rt = get_RT(instruction);
set_register_32b_se(rd, sign_extend(get_register_32b_unsigned(rt) >> sa, 32 - sa));
}
void processor::r_type_04(uint32_t instruction) // SLLV
{
uint8_t sa = get_SA(instruction);
uint8_t rd = get_RD(instruction);
uint8_t rt = get_RT(instruction);
set_register_32b_se(rd, get_register_32b_unsigned(rt) << sa);
}
void processor::r_type_05(uint32_t instruction)
{
// pdc -> dc_log("r_type_05 not implemented");
}
void processor::r_type_06(uint32_t instruction) // SRLV / ROTRV
{
uint8_t rd = get_RD(instruction);
uint8_t rt = get_RT(instruction);
uint8_t rs = get_RS(instruction);
if (IS_BIT_OFF0_SET(21, instruction))
set_register_32b(rd, rotate_right(get_register_32b_unsigned(rt), get_register_32b_unsigned(rs) & MASK_5B, 32));
else
set_register_32b_se(rd, get_register_32b_unsigned(rt) >> (get_register_32b_unsigned(rs) & MASK_5B));
}
void processor::r_type_07(uint32_t instruction)
{
// pdc -> dc_log("r_type_07 not implemented");
}
void processor::r_type_08(uint32_t instruction) // JR
{
uint8_t hint = (instruction >> 6) & MASK_5B;
uint8_t rs = get_RS(instruction);
if (unlikely(hint))
DEBUG(pdc -> dc_log("r_type_08: hint is unexpected value (%d, expected 0)", hint));
set_delay_slot(PC);
PC = get_register_64b_unsigned(rs);
if (unlikely(PC & 0x03))
throw processor_exception(PC, status_register, 0, PE_ADDRL, PC);
}
void processor::r_type_09(uint32_t instruction) // JALR
{
uint8_t rd = get_RD(instruction);
uint8_t rs = get_RS(instruction);
set_delay_slot(PC);
set_register_64b(rd, PC + 4);
PC = get_register_64b_unsigned(rs);
if (unlikely(PC & 0x03))
throw processor_exception(PC, status_register, 0, PE_ADDRL, PC);
}
void processor::r_type_0a(uint32_t instruction) // MOVZ
{
uint8_t rd = get_RD(instruction);
uint8_t rt = get_RT(instruction);
uint8_t rs = get_RS(instruction);
if (get_register_32b_unsigned(rt) == 0)
set_register_32b(rd, get_register_32b_unsigned(rs));
}
void processor::r_type_0b(uint32_t instruction) // MOVN
{
uint8_t rd = get_RD(instruction);
uint8_t rt = get_RT(instruction);
uint8_t rs = get_RS(instruction);
if (get_register_32b_unsigned(rt))
set_register_32b(rd, get_register_32b_unsigned(rs));
}
void processor::r_type_0c(uint32_t instruction) // SYSCALL
{
// DEBUG(pdc -> dc_log("SYSCALL"));
throw processor_exception(PC, status_register, 0, PE_SYSCALL, 0);
}
void processor::r_type_0d(uint32_t instruction) // BREAK for debugging FIXME
{
// DEBUG(pdc -> dc_log("BREAK"));
throw processor_exception(PC, status_register, 0, PE_BKPT, 0);
}
void processor::r_type_0e(uint32_t instruction)
{
// pdc -> dc_log("r_type_0e not implemented");
}
void processor::r_type_0f(uint32_t instruction)
{
// pdc -> dc_log("r_type_0f not implemented");
}
void processor::r_type_10(uint32_t instruction) // MFHI
{
uint8_t rd = get_RD(instruction);
set_register_32b(rd, HI);
}
void processor::r_type_11(uint32_t instruction) // MTHI
{
uint8_t rs = get_RS(instruction);
HI = get_register_64b_unsigned(rs);
}
void processor::r_type_12(uint32_t instruction) // MFLO
{
uint8_t rd = get_RD(instruction);
set_register_32b(rd, LO);
}
void processor::r_type_13(uint32_t instruction) // MTLO
{
uint8_t rs = get_RS(instruction);
LO = get_register_64b_unsigned(rs);
}
void processor::r_type_14(uint32_t instruction)
{
// pdc -> dc_log("r_type_14 not implemented");
}
void processor::r_type_15(uint32_t instruction)
{
// pdc -> dc_log("r_type_15 not implemented");
}
void processor::r_type_16(uint32_t instruction)
{
// pdc -> dc_log("r_type_16 not implemented");
}
void processor::r_type_17(uint32_t instruction)
{
// pdc -> dc_log("r_type_17 not implemented");
}
void processor::r_type_18(uint32_t instruction)
{
// pdc -> dc_log("r_type_18 not implemented");
}
void processor::r_type_19(uint32_t instruction)
{
// pdc -> dc_log("r_type_19 not implemented");
}
void processor::r_type_1a(uint32_t instruction)
{
// pdc -> dc_log("r_type_1a not implemented");
}
void processor::r_type_1b(uint32_t instruction)
{
// pdc -> dc_log("r_type_1b not implemented");
}
void processor::r_type_1c(uint32_t instruction)
{
// pdc -> dc_log("r_type_1c not implemented");
}
void processor::r_type_1d(uint32_t instruction)
{
// pdc -> dc_log("r_type_1d not implemented");
}
void processor::r_type_1e(uint32_t instruction)
{
// pdc -> dc_log("r_type_1e not implemented");
}
void processor::r_type_1f(uint32_t instruction)
{
// pdc -> dc_log("r_type_1f not implemented");
}
void processor::r_type_20(uint32_t instruction)
{
// pdc -> dc_log("r_type_20 not implemented");
}
void processor::r_type_21(uint32_t instruction) // ADDU
{
uint8_t rd = get_RD(instruction);
uint8_t rt = get_RT(instruction);
uint8_t rs = get_RS(instruction);
set_register_32b_se(rd, get_register_32b_unsigned(rs) + get_register_32b_unsigned(rt));
}
void processor::r_type_22(uint32_t instruction) // MFHI
{
uint8_t rd = get_RD(instruction);
set_register_32b_se(rd, get_HI());
}
void processor::r_type_23(uint32_t instruction) // SUBU
{
uint8_t rd = get_RD(instruction);
uint8_t rt = get_RT(instruction);
uint8_t rs = get_RS(instruction);
set_register_32b(rd, uint32_t(get_register_32b_unsigned(rs) - get_register_32b_unsigned(rt)));
}
void processor::r_type_24(uint32_t instruction) // AND
{
uint8_t rd = get_RD(instruction);
uint8_t rt = get_RT(instruction);
uint8_t rs = get_RS(instruction);
set_register_64b(rd, get_register_64b_unsigned(rs) & get_register_64b_unsigned(rt));
}
void processor::r_type_25(uint32_t instruction) // OR
{
uint8_t rd = get_RD(instruction);
uint8_t rt = get_RT(instruction);
uint8_t rs = get_RS(instruction);
set_register_64b(rd, get_register_64b_unsigned(rs) | get_register_64b_unsigned(rt));
}
void processor::r_type_26(uint32_t instruction) // XOR
{
uint8_t rd = get_RD(instruction);
uint8_t rt = get_RT(instruction);
uint8_t rs = get_RS(instruction);
set_register_64b(rd, get_register_64b_unsigned(rs) ^ get_register_64b_unsigned(rt));
}
void processor::r_type_27(uint32_t instruction) // DIVU
{
uint8_t rt = get_RT(instruction);
uint8_t rs = get_RS(instruction);
if (rt) {
set_LO(rs / rt);
set_HI(rs % rt);
}
}
void processor::r_type_28(uint32_t instruction)
{
// pdc -> dc_log("r_type_28 not implemented");
}
void processor::r_type_29(uint32_t instruction)
{
// pdc -> dc_log("r_type_29 not implemented");
}
void processor::r_type_2a(uint32_t instruction) // SLT
{
uint8_t rd = get_RD(instruction);
uint8_t rt = get_RT(instruction);
uint8_t rs = get_RS(instruction);
if (get_register_64b_signed(rs) < get_register_64b_signed(rt))
set_register_64b(rd, 1);
else
set_register_64b(rd, 0);
}
void processor::r_type_2b(uint32_t instruction) // SLTU
{
uint8_t rd = get_RD(instruction);
uint8_t rt = get_RT(instruction);
uint8_t rs = get_RS(instruction);
if (get_register_64b_unsigned(rs) < get_register_64b_unsigned(rt))
set_register_64b(rd, 1);
else
set_register_64b(rd, 0);
}
void processor::r_type_2c(uint32_t instruction)
{
// pdc -> dc_log("r_type_2c not implemented");
}
void processor::r_type_2d(uint32_t instruction)
{
// pdc -> dc_log("r_type_2d not implemented");
}
void processor::r_type_2e(uint32_t instruction)
{
// pdc -> dc_log("r_type_2e not implemented");
}
void processor::r_type_2f(uint32_t instruction)
{
// pdc -> dc_log("r_type_2f not implemented");
}
void processor::r_type_30(uint32_t instruction)
{
// pdc -> dc_log("r_type_30 not implemented");
}
void processor::r_type_31(uint32_t instruction)
{
// pdc -> dc_log("r_type_31 not implemented");
}
void processor::r_type_32(uint32_t instruction)
{
// pdc -> dc_log("r_type_32 not implemented");
}
void processor::r_type_33(uint32_t instruction)
{
// pdc -> dc_log("r_type_33 not implemented");
}
void processor::r_type_34(uint32_t instruction)
{
// pdc -> dc_log("r_type_34 not implemented");
}
void processor::r_type_35(uint32_t instruction)
{
// pdc -> dc_log("r_type_35 not implemented");
}
void processor::r_type_36(uint32_t instruction)
{
// pdc -> dc_log("r_type_36 not implemented");
}
void processor::r_type_37(uint32_t instruction)
{
// pdc -> dc_log("r_type_37 not implemented");
}
void processor::r_type_38(uint32_t instruction)
{
// pdc -> dc_log("r_type_38 not implemented");
}
void processor::r_type_39(uint32_t instruction)
{
// pdc -> dc_log("r_type_39 not implemented");
}
void processor::r_type_3a(uint32_t instruction)
{
// pdc -> dc_log("r_type_3a not implemented");
}
void processor::r_type_3b(uint32_t instruction)
{
// pdc -> dc_log("r_type_3b not implemented");
}
void processor::r_type_3c(uint32_t instruction)
{
// pdc -> dc_log("r_type_3c not implemented");
}
void processor::r_type_3d(uint32_t instruction)
{
// pdc -> dc_log("r_type_3d not implemented");
}
void processor::r_type_3e(uint32_t instruction)
{
// pdc -> dc_log("r_type_3e not implemented");
}
void processor::r_type_3f(uint32_t instruction)
{
// pdc -> dc_log("r_type_3f not implemented");
}