-
Notifications
You must be signed in to change notification settings - Fork 19
/
debug_rsp.cpp
1433 lines (1267 loc) · 43.9 KB
/
debug_rsp.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "debug_rsp.hpp"
#include "rsp_disasm.hpp"
#include <utility>
using namespace std;
#define TRACE
extern "C"
{
#ifdef INTENSE_DEBUG
static uint64_t hash_imem(const uint8_t *data, size_t size)
{
uint64_t h = 0xcbf29ce484222325ull;
size_t i;
for (i = 0; i < size; i++)
h = (h * 0x100000001b3ull) ^ data[i];
if (h == BREAKVAL)
breakme();
return h;
}
void RSP_DEBUG(RSP::CPUState *rsp, const char *tag, unsigned pc, unsigned value)
{
uint64_t hash = hash_imem((const uint8_t *)rsp->cp2.regs, sizeof(rsp->cp2.regs));
fprintf(DUMP_FILE, "%s (PC: %u): %u, %llu\n", tag, pc, value, hash);
if (value)
fprintf(DUMP_FILE, " DMEM HASH: 0x%016llx\n", hash_imem((const uint8_t *)rsp->dmem, 0x1000));
}
#endif
}
namespace RSP
{
CPU::CPU()
#ifndef DEBUG_JIT
: jit_engine(symbol_table)
#endif
{
init_symbol_table();
}
CPU::~CPU()
{
}
void CPU::init_symbol_table()
{
#define S(sym) symbol_table["RSP_" #sym] = reinterpret_cast<uint64_t>(RSP_##sym)
S(EXIT);
S(CALL);
S(RETURN);
S(REPORT_PC);
#ifdef INTENSE_DEBUG
S(DEBUG);
#endif
S(MFC0);
S(MTC0);
S(MTC2);
S(MFC2);
S(CFC2);
S(CTC2);
S(LBV);
S(LSV);
S(LLV);
S(LDV);
S(LQV);
S(LRV);
S(LPV);
S(LUV);
S(LHV);
S(LTV);
S(SBV);
S(SSV);
S(SLV);
S(SDV);
S(SQV);
S(SRV);
S(SPV);
S(SUV);
S(SHV);
S(SFV);
S(STV);
S(VMULF);
S(VMULU);
S(VMUDL);
S(VMUDM);
S(VMUDN);
S(VMUDH);
S(VMACF);
S(VMACU);
//S(VMACQ);
S(VMADL);
S(VMADM);
S(VMADN);
S(VMADH);
S(VADD);
S(VSUB);
S(VABS);
S(VADDC);
S(VSUBC);
S(VSAR);
S(VLT);
S(VEQ);
S(VNE);
S(VGE);
S(VCL);
S(VCH);
S(VCR);
S(VMRG);
S(VAND);
S(VNAND);
S(VOR);
S(VNOR);
S(VXOR);
S(VNXOR);
S(VRCP);
S(VRCPL);
S(VRCPH);
S(VMOV);
S(VRSQ);
S(VRSQL);
S(VRSQH);
S(VNOP);
#undef S
}
void CPU::invalidate_imem()
{
for (unsigned i = 0; i < CODE_BLOCKS; i++)
if (memcmp(cached_imem + i * CODE_BLOCK_WORDS, state.imem + i * CODE_BLOCK_WORDS, CODE_BLOCK_SIZE))
state.dirty_blocks |= (0x3 << i) >> 1;
}
void CPU::invalidate_code()
{
if (!state.dirty_blocks)
return;
for (unsigned i = 0; i < CODE_BLOCKS; i++)
{
if (state.dirty_blocks & (1 << i))
{
memset(blocks + i * CODE_BLOCK_WORDS, 0, CODE_BLOCK_WORDS * sizeof(blocks[0]));
memcpy(cached_imem + i * CODE_BLOCK_WORDS, state.imem + i * CODE_BLOCK_WORDS, CODE_BLOCK_SIZE);
}
}
state.dirty_blocks = 0;
}
// Need super-fast hash here.
uint64_t CPU::hash_imem(unsigned pc, unsigned count) const
{
size_t size = count;
// FNV-1.
const auto *data = state.imem + pc;
uint64_t h = 0xcbf29ce484222325ull;
h = (h * 0x100000001b3ull) ^ pc;
h = (h * 0x100000001b3ull) ^ count;
for (size_t i = 0; i < size; i++)
h = (h * 0x100000001b3ull) ^ data[i];
return h;
}
unsigned CPU::analyze_static_end(unsigned pc, unsigned end)
{
// Scans through IMEM and finds the logical "end" of the instruction stream.
unsigned max_static_pc = pc;
unsigned count = end - pc;
for (unsigned i = 0; i < count; i++)
{
uint32_t instr = state.imem[pc + i];
uint32_t type = instr >> 26;
uint32_t target;
bool forward_goto;
if (pc + i + 1 >= max_static_pc)
{
forward_goto = false;
max_static_pc = pc + i + 1;
}
else
forward_goto = true;
// VU
if ((instr >> 25) == 0x25)
continue;
switch (type)
{
case 000:
switch (instr & 63)
{
case 010:
// JR always terminates either by returning or exiting.
// We execute the next instruction via delay slot and exit.
// Unless we can branch past the JR
// (max_static_pc will be higher than expected),
// this will be the static end.
if (!forward_goto)
{
max_static_pc = max(pc + i + 2, max_static_pc);
goto end;
}
break;
case 015:
// BREAK always terminates.
if (!forward_goto)
goto end;
break;
default:
break;
}
break;
case 001: // REGIMM
switch ((instr >> 16) & 31)
{
case 000: // BLTZ
case 001: // BGEZ
case 021: // BGEZAL
case 020: // BLTZAL
target = (pc + i + 1 + instr) & 0x3ff;
if (target >= pc && target < end) // goto
max_static_pc = max(max_static_pc, target + 1);
break;
default:
break;
}
break;
case 002:
// J is resolved by goto.
target = instr & 0x3ff;
if (target >= pc && target < end) // goto
{
// J is a static jump, so if we aren't branching
// past this instruction and we're branching backwards,
// we can end the block here.
if (!forward_goto && target < end)
{
max_static_pc = max(pc + i + 2, max_static_pc);
goto end;
}
else
max_static_pc = max(max_static_pc, target + 1);
}
else if (!forward_goto)
{
// If we have static branch outside our block,
// we terminate the block.
max_static_pc = max(pc + i + 2, max_static_pc);
goto end;
}
break;
case 004: // BEQ
case 005: // BNE
case 006: // BLEZ
case 007: // BGTZ
target = (pc + i + 1 + instr) & 0x3ff;
if (target >= pc && target < end) // goto
max_static_pc = max(max_static_pc, target + 1);
break;
default:
break;
}
}
end:
unsigned ret = min(max_static_pc, end);
return ret;
}
Func CPU::jit_region(uint64_t hash, unsigned pc, unsigned count)
{
full_code.clear();
body.clear();
full_code.reserve(16 * 1024);
body.reserve(16 * 1024);
// Local branch delays resolve to within the block, so we can use goto.
bool pending_local_branch_delay = false;
bool pending_branch_delay = false;
bool pending_call = false;
bool pending_indirect_call = false;
bool pending_return = false;
bool pipe_pending_local_branch_delay = false;
bool pipe_pending_branch_delay = false;
bool pipe_pending_call = false;
bool pipe_pending_indirect_call = false;
bool pipe_pending_return = false;
uint32_t branch_delay = 0;
uint32_t pipe_branch_delay = 0;
char buf[256];
#define APPEND(...) \
do \
{ \
sprintf(buf, __VA_ARGS__); \
body += buf; \
} while (0)
#define APPEND_RD_NOT_R0(...) \
if (rd != 0) \
do \
{ \
APPEND(__VA_ARGS__); \
} while (0)
#define APPEND_RT_NOT_R0(...) \
if (rt != 0) \
do \
{ \
APPEND(__VA_ARGS__); \
} while (0)
#define DISASM(...) \
do \
{ \
APPEND("// "); \
APPEND(__VA_ARGS__); \
} while (0)
#define PIPELINE_BRANCH() \
do \
{ \
pending_local_branch_delay = pipe_pending_local_branch_delay; \
pending_branch_delay = pipe_pending_branch_delay; \
pending_call = pipe_pending_call; \
pending_indirect_call = pipe_pending_indirect_call; \
pending_return = pipe_pending_return; \
branch_delay = pipe_branch_delay; \
pipe_pending_local_branch_delay = false; \
pipe_pending_branch_delay = false; \
pipe_pending_call = false; \
pipe_pending_indirect_call = false; \
pipe_pending_return = false; \
pipe_branch_delay = 0; \
APPEND("ADVANCE_DELAY_SLOT();\n"); \
} while (0)
#define PROMOTE_LOCAL_DELAY_SLOT() \
do \
{ \
APPEND("if (pipe_branch) {\n"); \
APPEND(" STATE->has_delay_slot = 1;\n"); \
APPEND(" STATE->branch_target = %u;\n", pipe_branch_delay * 4); \
APPEND("}\n"); \
} while (0)
#define PROMOTE_DELAY_SLOT() \
do \
{ \
if (pipe_pending_local_branch_delay) \
PROMOTE_LOCAL_DELAY_SLOT(); \
else if (pipe_pending_branch_delay) \
{ \
APPEND(" PROMOTE_DELAY_SLOT();\n"); \
} \
} while (0)
// Statically checks if we need to handle branch delay slots.
// Only relevant if the last instruction did anything branch related.
// Double branch delays are technically undefined, but I assume it works like this.
#define CHECK_BRANCH_DELAY() \
do \
{ \
if (pending_call && !pipe_pending_local_branch_delay && !pipe_pending_branch_delay) \
{ \
APPEND("if (LIKELY(branch)) {\n"); \
APPEND(" RSP_CALL(opaque, 0x%03x, 0x%03x);\n", branch_delay * 4, ((pc + i + 1) << 2) & (IMEM_SIZE - 1)); \
APPEND("}\n"); \
} \
else if (pending_indirect_call && !pipe_pending_local_branch_delay && !pipe_pending_branch_delay) \
{ \
APPEND("if (LIKELY(branch)) {\n"); \
APPEND(" RSP_CALL(opaque, (branch_delay << 2) & %u, 0x%03x);\n", IMEM_SIZE - 1, \
((pc + i + 1) << 2) & (IMEM_SIZE - 1)); \
APPEND("}\n"); \
} \
else if (pending_return && !pipe_pending_local_branch_delay && !pipe_pending_branch_delay) \
{ \
APPEND("if (LIKELY(branch)) {\n"); \
APPEND(" if (RSP_RETURN(opaque, (branch_delay << 2) & %u)) return;\n", IMEM_SIZE - 1); \
APPEND(" STATE->pc = (branch_delay << 2) & %u;\n", IMEM_SIZE - 1); \
APPEND(" EXIT(MODE_CONTINUE);\n"); \
APPEND("}\n"); \
} \
else if (pending_local_branch_delay) \
{ \
if (pipe_pending_local_branch_delay || pipe_pending_branch_delay) \
{ \
APPEND("if (branch && pipe_branch) {\n"); \
APPEND(" STATE->pc = %u;\n", branch_delay * 4); \
APPEND(" PROMOTE_DELAY_SLOT();\n"); \
APPEND(" EXIT(MODE_CONTINUE);\n"); \
APPEND("} else if (branch) {\n"); \
APPEND(" goto pc_%03x;\n", branch_delay * 4); \
APPEND("}\n"); \
} \
else \
{ \
APPEND("if (branch) goto pc_%03x;\n", branch_delay * 4); \
} \
} \
else if (pending_branch_delay) \
{ \
APPEND("if (branch) {\n"); \
APPEND(" STATE->pc = (branch_delay << 2) & %u;\n", IMEM_SIZE - 1); \
PROMOTE_DELAY_SLOT(); \
APPEND(" EXIT(MODE_CONTINUE);\n"); \
APPEND("}\n"); \
} \
pending_call = false; \
pending_indirect_call = false; \
pending_return = false; \
pending_branch_delay = false; \
pending_local_branch_delay = false; \
} while (0)
#define CHECK_INHERIT_BRANCH_DELAY() \
do \
{ \
APPEND("if (UNLIKELY(STATE->has_delay_slot)) {\n"); \
APPEND(" STATE->pc = STATE->branch_target;\n"); \
APPEND(" STATE->has_delay_slot = 0;\n"); \
PROMOTE_DELAY_SLOT(); \
APPEND(" EXIT(MODE_CONTINUE);\n"); \
APPEND("}\n"); \
} while (0)
#define EXIT_WITH_DELAY(mode) \
do \
{ \
if (pending_local_branch_delay) \
{ \
APPEND("STATE->pc = branch ? %u : %u;\n", branch_delay * 4, ((pc + i + 1) << 2) & (IMEM_SIZE - 1)); \
APPEND("EXIT(%s);\n", #mode); \
} \
else if (pending_branch_delay) \
{ \
APPEND("STATE->pc = branch ? ((branch_delay << 2) & %u) : %u;\n", IMEM_SIZE - 1, \
((pc + i + 1) << 2) & (IMEM_SIZE - 1)); \
APPEND("EXIT(%s);\n", #mode); \
} \
else \
{ \
APPEND("if (UNLIKELY(STATE->has_delay_slot)) {\n"); \
APPEND(" STATE->pc = STATE->branch_target;\n"); \
APPEND(" STATE->has_delay_slot = 0;\n"); \
APPEND(" EXIT(%s);\n", #mode); \
APPEND("} else {\n"); \
APPEND(" STATE->pc = %u;\n", ((pc + i + 1) << 2) & (IMEM_SIZE - 1)); \
APPEND(" EXIT(%s);\n", #mode); \
APPEND("}\n"); \
} \
} while (0)
auto set_pc = [&](uint32_t next_pc) {
next_pc &= (IMEM_SIZE >> 2) - 1;
if (next_pc >= pc && next_pc < (pc + count))
{
pipe_pending_local_branch_delay = true;
pipe_branch_delay = next_pc;
}
else
{
pipe_pending_branch_delay = true;
pipe_branch_delay = next_pc;
APPEND("pipe_branch_delay = %u;\n", next_pc);
}
};
auto set_pc_indirect = [&](uint32_t reg) {
pipe_pending_branch_delay = true;
APPEND("BRANCH_INDIRECT((r%u & 0xfff) >> 2);\n", reg);
};
APPEND("unsigned branch = 0;\n");
APPEND("unsigned branch_delay = 0;\n");
APPEND("unsigned pipe_branch = 0;\n");
APPEND("unsigned pipe_branch_delay = 0;\n");
APPEND("unsigned cp0_result;\n");
APPEND("unsigned addr;\n");
APPEND("unsigned *dmem = STATE->dmem;\n");
for (unsigned i = 0; i < count; i++)
{
uint32_t instr = state.imem[pc + i];
APPEND("pc_%03x:\n", (pc + i) * 4);
#ifdef TRACE
APPEND("RSP_REPORT_PC(STATE, %u, %u);\n", (pc + i) * 4, instr);
#endif
PIPELINE_BRANCH();
uint32_t type = instr >> 26;
uint32_t rd, rs, rt, shift, imm;
int16_t simm;
if ((instr >> 25) == 0x25)
{
// VU instruction.
uint32_t op = instr & 63;
uint32_t vd = (instr >> 6) & 31;
uint32_t vs = (instr >> 11) & 31;
uint32_t vt = (instr >> 16) & 31;
uint32_t e = (instr >> 21) & 15;
static const char *ops[64] = {
"VMULF", "VMULU", nullptr, nullptr, "VMUDL", "VMUDM", "VMUDN", "VMUDH", "VMACF", "VMACU", nullptr,
nullptr, "VMADL", "VMADM", "VMADN", "VMADH", "VADD", "VSUB", nullptr, "VABS", "VADDC", "VSUBC",
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, "VSAR", nullptr, nullptr, "VLT",
"VEQ", "VNE", "VGE", "VCL", "VCH", "VCR", "VMRG", "VAND", "VNAND", "VOR", "VNOR",
"VXOR", "VNXOR", nullptr, nullptr, "VRCP", "VRCPL", "VRCPH", "VMOV", "VRSQ", "VRSQL", "VRSQH",
"VNOP", nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
};
auto vop = ops[op];
if (vop)
{
APPEND("RSP_%s(STATE, %u, %u, %u, %u);\n", vop, vd, vs, vt, e);
DISASM("%s v%u, v%u, v%u[%u]\n", vop, vd, vs, vt, e);
}
else
{
APPEND("RSP_RESERVED(STATE, %u, %u, %u, %u);\n", vd, vs, vt, e);
DISASM("RSP_RESERVED v%u, v%u, v%u[%u]\n", vd, vs, vt, e);
//fprintf(DUMP_FILE, "Unimplemented COP2 op %u.\n", op);
}
#ifdef INTENSE_DEBUG
APPEND("RSP_DEBUG(STATE, \"CP2\", %u, 0);\n", op);
#endif
}
else
{
// Everything else.
switch (type)
{
case 000:
{
rd = (instr & 0xffff) >> 11;
rt = (instr >> 16) & 31;
shift = (instr >> 6) & 31;
rs = instr >> 21;
switch (instr & 63)
{
case 000: // SLL
APPEND_RD_NOT_R0("r%u = r%u << %u;\n", rd, rt, shift);
if (instr)
DISASM("sll %s, %s, %u\n", register_name(rd), register_name(rt), shift);
else
DISASM("nop\n");
break;
case 002: // SRL
APPEND_RD_NOT_R0("r%u = r%u >> %u;\n", rd, rt, shift);
DISASM("srl %s, %s, %u\n", register_name(rd), register_name(rt), shift);
break;
case 003: // SRA
APPEND_RD_NOT_R0("r%u = (int)r%u >> (int)%u;\n", rd, rt, shift);
DISASM("sra %s, %s, %u\n", register_name(rd), register_name(rt), shift);
break;
case 004: // SLLV
APPEND_RD_NOT_R0("r%u = r%u << MASK_SA(r%u);\n", rd, rt, rs);
DISASM("sllv %s, %s, $%u\n", register_name(rd), register_name(rt), rs);
break;
case 006: // SRLV
APPEND_RD_NOT_R0("r%u = r%u >> MASK_SA(r%u);\n", rd, rt, rs);
DISASM("srlv %s, %s, $%u\n", register_name(rd), register_name(rt), rs);
break;
case 007: // SRAV
APPEND_RD_NOT_R0("r%u = (int)r%u >> (int)MASK_SA(r%u);\n", rd, rt, rs);
DISASM("srav %s, %s, $%u\n", register_name(rd), register_name(rt), rs);
break;
case 011: // JALR
if (rd != 0)
{
APPEND("r%u = %u;\n", rd, ((pc + i + 2) << 2) & 0xffc);
}
set_pc_indirect(rs);
pipe_pending_indirect_call = true;
DISASM("jalr %s\n", register_name(rs));
#ifdef INTENSE_DEBUG
APPEND("RSP_DEBUG(STATE, \"JALR\", pipe_branch_delay * 4, 0);\n");
#endif
break;
case 010: // JR
set_pc_indirect(rs);
pipe_pending_return = true;
DISASM("jr %s\n", register_name(rs));
#ifdef INTENSE_DEBUG
APPEND("RSP_DEBUG(STATE, \"JR\", pipe_branch_delay * 4, 0);\n");
#endif
break;
case 015: // BREAK
EXIT_WITH_DELAY(MODE_BREAK);
break;
case 040: // ADD
case 041: // ADDU
APPEND_RD_NOT_R0("r%u = r%u + r%u;\n", rd, rs, rt);
DISASM("add %s, %s, %s\n", register_name(rd), register_name(rs), register_name(rt));
break;
case 042: // SUB
case 043: // SUBU
APPEND_RD_NOT_R0("r%u = r%u - r%u;\n", rd, rs, rt);
DISASM("sub %s, %s, %s\n", register_name(rd), register_name(rs), register_name(rt));
break;
case 044: // AND
APPEND_RD_NOT_R0("r%u = r%u & r%u;\n", rd, rs, rt);
DISASM("and %s, %s, %s\n", register_name(rd), register_name(rs), register_name(rt));
break;
case 045: // OR
APPEND_RD_NOT_R0("r%u = r%u | r%u;\n", rd, rs, rt);
DISASM("or %s, %s, %s\n", register_name(rd), register_name(rs), register_name(rt));
break;
case 046: // XOR
APPEND_RD_NOT_R0("r%u = r%u ^ r%u;\n", rd, rs, rt);
DISASM("xor %s, %s, %s\n", register_name(rd), register_name(rs), register_name(rt));
break;
case 047: // NOR
APPEND_RD_NOT_R0("r%u = ~(r%u | r%u);\n", rd, rs, rt);
DISASM("nor %s, %s, %s\n", register_name(rd), register_name(rs), register_name(rt));
break;
case 052: // SLT
APPEND_RD_NOT_R0("r%u = (int)r%u < (int)r%u;\n", rd, rs, rt);
DISASM("slt %s, %s, %s\n", register_name(rd), register_name(rs), register_name(rt));
break;
case 053: // SLTU
APPEND_RD_NOT_R0("r%u = r%u < r%u;\n", rd, rs, rt);
DISASM("sltu %s, %s, %s\n", register_name(rd), register_name(rs), register_name(rt));
break;
default:
break;
}
break;
}
case 001: // REGIMM
rs = (instr >> 21) & 31;
rt = (instr >> 16) & 31;
switch (rt)
{
case 020: // BLTZAL
APPEND("r31 = %u;\n", ((pc + i + 2) << 2) & 0xffc);
rs = (instr >> 21) & 31;
set_pc(pc + i + 1 + instr);
APPEND("BRANCH_IF((int)r%u < 0);\n", rs);
DISASM("bltzal %s, 0x%x\n", register_name(rs), ((pc + i + 1 + instr) << 2) & 0xffc);
break;
case 000: // BLTZ
rs = (instr >> 21) & 31;
set_pc(pc + i + 1 + instr);
APPEND("BRANCH_IF((int)r%u < 0);\n", rs);
DISASM("bltz %s, 0x%x\n", register_name(rs), ((pc + i + 1 + instr) << 2) & 0xffc);
break;
case 021: // BGEZAL
APPEND("r31 = %u;\n", ((pc + i + 2) << 2) & 0xffc);
rs = (instr >> 21) & 31;
set_pc(pc + i + 1 + instr);
APPEND("BRANCH_IF((int)r%u >= 0);\n", rs);
DISASM("bgezal %s, 0x%x\n", register_name(rs), ((pc + i + 1 + instr) << 2) & 0xffc);
break;
case 001: // BGEZ
rs = (instr >> 21) & 31;
set_pc(pc + i + 1 + instr);
APPEND("BRANCH_IF((int)r%u >= 0);\n", rs);
DISASM("bgez %s, 0x%x\n", register_name(rs), ((pc + i + 1 + instr) << 2) & 0xffc);
break;
default:
break;
}
break;
case 003: // JAL
APPEND("r31 = %u;\n", ((pc + i + 2) << 2) & 0xffc);
imm = instr & 0x3ff;
set_pc(imm);
pipe_pending_call = true;
APPEND("BRANCH();\n");
DISASM("jal 0x%x\n", (instr & 0x3ff) << 2);
#ifdef INTENSE_DEBUG
APPEND("RSP_DEBUG(STATE, \"JAL\", %u, 0);\n", pipe_branch_delay * 4);
#endif
break;
case 002: // J
imm = instr & 0x3ff;
set_pc(imm);
APPEND("BRANCH();\n");
DISASM("j 0x%x\n", (instr & 0x3ff) << 2);
break;
case 004: // BEQ
rs = (instr >> 21) & 31;
rt = (instr >> 16) & 31;
set_pc(pc + i + 1 + instr);
APPEND("BRANCH_IF(r%u == r%u);\n", rs, rt);
DISASM("beq %s, %s, 0x%x\n", register_name(rs), register_name(rt), ((pc + i + 1 + instr) & 0x3ff) << 2);
break;
case 005: // BNE
rs = (instr >> 21) & 31;
rt = (instr >> 16) & 31;
set_pc(pc + i + 1 + instr);
APPEND("BRANCH_IF(r%u != r%u);\n", rs, rt);
DISASM("bne %s, %s, 0x%x\n", register_name(rs), register_name(rt), ((pc + i + 1 + instr) & 0x3ff) << 2);
break;
case 006: // BLEZ
rs = (instr >> 21) & 31;
set_pc(pc + i + 1 + instr);
APPEND("BRANCH_IF((int)r%u <= 0);\n", rs);
DISASM("blez %s, 0x%x\n", register_name(rs), ((pc + i + 1 + instr) & 0x3ff) << 2);
break;
case 007: // BGTZ
rs = (instr >> 21) & 31;
set_pc(pc + i + 1 + instr);
APPEND("BRANCH_IF((int)r%u > 0);\n", rs);
DISASM("bgtz %s, 0x%x\n", register_name(rs), ((pc + i + 1 + instr) & 0x3ff) << 2);
break;
case 010:
case 011: // ADDI
simm = instr;
rs = (instr >> 21) & 31;
rt = (instr >> 16) & 31;
APPEND_RT_NOT_R0("r%u = (int)r%u + %d;\n", rt, rs, simm);
if (rs != 0)
DISASM("addi %s, %s, %d\n", register_name(rt), register_name(rs), simm);
else
DISASM("li %s, %d\n", register_name(rt), simm);
break;
case 012: // SLTI
simm = instr;
rs = (instr >> 21) & 31;
rt = (instr >> 16) & 31;
APPEND_RT_NOT_R0("r%u = (int)r%u < %d;\n", rt, rs, simm);
DISASM("slti %s, %s, %d\n", register_name(rt), register_name(rs), simm);
break;
case 013: // SLTIU
imm = instr & 0xffff;
rs = (instr >> 21) & 31;
rt = (instr >> 16) & 31;
APPEND_RT_NOT_R0("r%u = r%u < %u;\n", rt, rs, imm);
DISASM("sltiu %s, %s, %u\n", register_name(rt), register_name(rs), imm);
break;
case 014: // ANDI
imm = instr & 0xffff;
rs = (instr >> 21) & 31;
rt = (instr >> 16) & 31;
APPEND_RT_NOT_R0("r%u = r%u & %u;\n", rt, rs, imm);
DISASM("andi %s, %s, 0x%x\n", register_name(rt), register_name(rs), imm);
break;
case 015: // ORI
imm = instr & 0xffff;
rs = (instr >> 21) & 31;
rt = (instr >> 16) & 31;
APPEND_RT_NOT_R0("r%u = r%u | %u;\n", rt, rs, imm);
DISASM("ori %s, %s, 0x%x\n", register_name(rt), register_name(rs), imm);
break;
case 016: // XORI
imm = instr & 0xffff;
rs = (instr >> 21) & 31;
rt = (instr >> 16) & 31;
APPEND_RT_NOT_R0("r%u = r%u ^ %u;\n", rt, rs, imm);
DISASM("xori %s, %s, 0x%x\n", register_name(rt), register_name(rs), imm);
break;
case 017: // LUI
imm = instr & 0xffff;
rt = (instr >> 16) & 31;
APPEND_RT_NOT_R0("r%u = %uu << 16u;\n", rt, imm);
DISASM("lui %s, 0x%x\n", register_name(rt), imm);
break;
case 020: // COP0
rd = (instr >> 11) & 31;
rs = (instr >> 21) & 31;
rt = (instr >> 16) & 31;
switch (rs)
{
case 000: // MFC0
APPEND("cp0_result = RSP_MFC0(STATE, %u, %u);\n", rt, rd);
DISASM("mfc0 %u, %u\n", rt, rd);
APPEND("if (UNLIKELY(cp0_result != MODE_CONTINUE)) {\n");
EXIT_WITH_DELAY(cp0_result);
APPEND("}\n");
break;
case 004: // MTC0
APPEND("cp0_result = RSP_MTC0(STATE, %u, %u);\n", rd, rt);
DISASM("mtc0 %u, %u\n", rd, rt);
APPEND("if (UNLIKELY(cp0_result != MODE_CONTINUE)) {\n");
EXIT_WITH_DELAY(cp0_result);
APPEND("}\n");
break;
default:
break;
}
break;
case 022: // COP2
rd = (instr >> 11) & 31;
rs = (instr >> 21) & 31;
rt = (instr >> 16) & 31;
imm = (instr >> 7) & 15;
switch (rs)
{
case 000: // MFC2
APPEND("RSP_MFC2(STATE, %u, %u, %u);\n", rt, rd, imm);
DISASM("mfc2 %u, %u, %u\n", rt, rd, imm);
break;
case 002: // CFC2
APPEND("RSP_CFC2(STATE, %u, %u);\n", rt, rd);
DISASM("cfc2 %u, %u\n", rt, rd);
break;
case 004: // MTC2
APPEND("RSP_MTC2(STATE, %u, %u, %u);\n", rt, rd, imm);
DISASM("mtc2 %u, %u, %u\n", rt, rd, imm);
#ifdef INTENSE_DEBUG
APPEND("RSP_DEBUG(STATE, \"MTC2\", %u, 0);\n", 0);
#endif
break;
case 006: // CTC2
APPEND("RSP_CTC2(STATE, %u, %u);\n", rt, rd);
DISASM("ctc2 %u, %u\n", rt, rd);
break;
default:
break;
}
break;
case 040: // LB
simm = instr;
rt = (instr >> 16) & 31;
rs = (instr >> 21) & 31;
if (rt != 0)
{
APPEND("r%u = (signed char)READ_MEM_U8(dmem, (r%u + (%d)) & 0xfff);\n", rt, rs, simm);
}
DISASM("lb %s, %d(%s)\n", register_name(rt), simm, register_name(rs));
break;
case 041: // LH
simm = instr;
rt = (instr >> 16) & 31;
rs = (instr >> 21) & 31;
if (rt != 0)
{
APPEND("addr = (r%u + (%d)) & 0xfff;\n", rs, simm);
APPEND("if (UNLIKELY(addr & 1))\n");
APPEND(" r%u = (signed short)READ_MEM_U16_UNALIGNED(dmem, addr);\n", rt);
APPEND("else\n");
APPEND(" r%u = (signed short)READ_MEM_U16(dmem, addr);\n", rt);
}
DISASM("lh %s, %d(%s)\n", register_name(rt), simm, register_name(rs));
break;
case 043: // LW
simm = instr;
rt = (instr >> 16) & 31;
rs = (instr >> 21) & 31;
if (rt != 0)
{
APPEND("addr = (r%u + (%d)) & 0xfff;\n", rs, simm);
APPEND("if (UNLIKELY(addr & 3))\n");
APPEND(" r%u = READ_MEM_U32_UNALIGNED(dmem, addr);\n", rt);
APPEND("else\n");
APPEND(" r%u = READ_MEM_U32(dmem, addr);\n", rt);
}
DISASM("lw %s, %d(%s)\n", register_name(rt), simm, register_name(rs));
break;
case 044: // LBU
simm = instr;
rt = (instr >> 16) & 31;
rs = (instr >> 21) & 31;
if (rt != 0)
{
APPEND("r%u = READ_MEM_U8(dmem, (r%u + (%d)) & 0xfff);\n", rt, rs, simm);
}
DISASM("lbu %s, %d(%s)\n", register_name(rt), simm, register_name(rs));
break;
case 045: // LHU
simm = instr;
rt = (instr >> 16) & 31;
rs = (instr >> 21) & 31;
if (rt != 0)
{
APPEND("addr = (r%u + (%d)) & 0xfff;\n", rs, simm);
APPEND("if (UNLIKELY(addr & 1))\n");
APPEND(" r%u = READ_MEM_U16_UNALIGNED(dmem, addr);\n", rt);
APPEND("else\n");
APPEND(" r%u = READ_MEM_U16(dmem, addr);\n", rt);
}
DISASM("lhu %s, %d(%s)\n", register_name(rt), simm, register_name(rs));
break;
case 050: // SB
simm = instr;
rt = (instr >> 16) & 31;
rs = (instr >> 21) & 31;
APPEND("WRITE_MEM_U8(dmem, ((r%u + (%d)) & 0xfff), r%u);\n", rs, simm, rt);
DISASM("sb %s, %d(%s)\n", register_name(rt), simm, register_name(rs));
break;
case 051: // SH
rt = (instr >> 16) & 31;
rs = (instr >> 21) & 31;
simm = instr;
APPEND("addr = (r%u + (%d)) & 0xfff;\n", rs, simm);
APPEND("if (UNLIKELY(addr & 1))\n");
APPEND(" WRITE_MEM_U16_UNALIGNED(dmem, addr, r%u);\n", rt);
APPEND("else\n");
APPEND(" WRITE_MEM_U16(dmem, addr, r%u);\n", rt);
DISASM("sh %s, %d(%s)\n", register_name(rt), simm, register_name(rs));
break;
case 053: // SW
rt = (instr >> 16) & 31;
rs = (instr >> 21) & 31;
simm = instr;
APPEND("addr = (r%u + (%d)) & 0xfff;\n", rs, simm);
APPEND("if (UNLIKELY(addr & 3))\n");
APPEND(" WRITE_MEM_U32_UNALIGNED(dmem, addr, r%u);\n", rt);
APPEND("else\n");
APPEND(" WRITE_MEM_U32(dmem, addr, r%u);\n", rt);
DISASM("sw %s, %d(%s)\n", register_name(rt), simm, register_name(rs));
break;
case 062: // LWC2
{
rt = (instr >> 16) & 31;
simm = instr;
// Sign extend.
simm <<= 9;
simm >>= 9;
rs = (instr >> 21) & 31;
rd = (instr >> 11) & 31;
imm = (instr >> 7) & 15;
static const char *lwc2_ops[32] = {
"LBV", "LSV", "LLV", "LDV", "LQV", "LRV", "LPV", "LUV", "LHV", nullptr, nullptr,
"LTV", nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
};
auto *op = lwc2_ops[rd];
if (op)
{
APPEND("RSP_%s(STATE, %u, %u, %d, %u);\n", op, rt, imm, simm, rs);
DISASM("%s %u, %u, %d, %u\n", op, rt, imm, simm, rs);