summaryrefslogtreecommitdiff
path: root/src/usf/rsp_recompiler_cpu.c
blob: f26f07530bc0e4d1622be29efea34ab698dc429f (plain)
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
/*
 * RSP Compiler plug in for Project 64 (A Nintendo 64 emulator).
 *
 * (c) Copyright 2001 jabo (jabo@emulation64.com) and
 * zilmar (zilmar@emulation64.com)
 *
 * pj64 homepage: www.pj64.net
 *
 * Permission to use, copy, modify and distribute Project64 in both binary and
 * source form, for non-commercial purposes, is hereby granted without fee,
 * providing that this license information and copyright notice appear with
 * all copies and any derived work.
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event shall the authors be held liable for any damages
 * arising from the use of this software.
 *
 * Project64 is freeware for PERSONAL USE only. Commercial users should
 * seek permission of the copyright holders first. Commercial use includes
 * charging money for Project64 or software derived from Project64.
 *
 * The copyright holders request that bug fixes and improvements to the code
 * should be forwarded to them so if they want them.
 *
 */

#include <stdint.h>
#include <stdlib.h>
#include <float.h>
#include "rsp.h"
#include "rsp_recompiler_cpu.h"
#include "rsp_recompiler_ops.h"
#include "rsp_x86.h"
#include "recompiler_cpu.h"
#include "memory.h"
#include "main.h"
#include "usf.h"
#include "types.h"

/* #define REORDER_BLOCK_VERBOSE */
#define LINK_BRANCHES_VERBOSE /* no choice really */
#define X86_RECOMP_VERBOSE
#define BUILD_BRANCHLABELS_VERBOSE

uint32_t RSPCompilePC, RSPBlockID = 0;
uint32_t dwBuffer = MainBuffer;

RSP_BLOCK RSPCurrentBlock;
RSP_CODE RspCode;

uint8_t * pLastSecondary = NULL, * pLastPrimary = NULL;

void BuildRecompilerCPU ( void ) {
	RSP_Opcode[ 0] = RSPCompile_SPECIAL;
	RSP_Opcode[ 1] = RSPCompile_REGIMM;
	RSP_Opcode[ 2] = RSPCompile_J;
	RSP_Opcode[ 3] = RSPCompile_JAL;
	RSP_Opcode[ 4] = RSPCompile_BEQ;
	RSP_Opcode[ 5] = RSPCompile_BNE;
	RSP_Opcode[ 6] = RSPCompile_BLEZ;
	RSP_Opcode[ 7] = RSPCompile_BGTZ;
	RSP_Opcode[ 8] = RSPCompile_ADDI;
	RSP_Opcode[ 9] = RSPCompile_ADDIU;
	RSP_Opcode[10] = RSPCompile_SLTI;
	RSP_Opcode[11] = RSPCompile_SLTIU;
	RSP_Opcode[12] = RSPCompile_ANDI;
	RSP_Opcode[13] = RSPCompile_ORI;
	RSP_Opcode[14] = RSPCompile_XORI;
	RSP_Opcode[15] = RSPCompile_LUI;
	RSP_Opcode[16] = RSPCompile_COP0;
	RSP_Opcode[17] = RSPCompile_UnknownOpcode;
	RSP_Opcode[18] = RSPCompile_COP2;
	RSP_Opcode[19] = RSPCompile_UnknownOpcode;
	RSP_Opcode[20] = RSPCompile_UnknownOpcode;
	RSP_Opcode[21] = RSPCompile_UnknownOpcode;
	RSP_Opcode[22] = RSPCompile_UnknownOpcode;
	RSP_Opcode[23] = RSPCompile_UnknownOpcode;
	RSP_Opcode[24] = RSPCompile_UnknownOpcode;
	RSP_Opcode[25] = RSPCompile_UnknownOpcode;
	RSP_Opcode[26] = RSPCompile_UnknownOpcode;
	RSP_Opcode[27] = RSPCompile_UnknownOpcode;
	RSP_Opcode[28] = RSPCompile_UnknownOpcode;
	RSP_Opcode[29] = RSPCompile_UnknownOpcode;
	RSP_Opcode[30] = RSPCompile_UnknownOpcode;
	RSP_Opcode[31] = RSPCompile_UnknownOpcode;
	RSP_Opcode[32] = RSPCompile_LB;
	RSP_Opcode[33] = RSPCompile_LH;
	RSP_Opcode[34] = RSPCompile_UnknownOpcode;
	RSP_Opcode[35] = RSPCompile_LW;
	RSP_Opcode[36] = RSPCompile_LBU;
	RSP_Opcode[37] = RSPCompile_LHU;
	RSP_Opcode[38] = RSPCompile_UnknownOpcode;
	RSP_Opcode[39] = RSPCompile_UnknownOpcode;
	RSP_Opcode[40] = RSPCompile_SB;
	RSP_Opcode[41] = RSPCompile_SH;
	RSP_Opcode[42] = RSPCompile_UnknownOpcode;
	RSP_Opcode[43] = RSPCompile_SW;
	RSP_Opcode[44] = RSPCompile_UnknownOpcode;
	RSP_Opcode[45] = RSPCompile_UnknownOpcode;
	RSP_Opcode[46] = RSPCompile_UnknownOpcode;
	RSP_Opcode[47] = RSPCompile_UnknownOpcode;
	RSP_Opcode[48] = RSPCompile_UnknownOpcode;
	RSP_Opcode[49] = RSPCompile_UnknownOpcode;
	RSP_Opcode[50] = RSPCompile_LC2;
	RSP_Opcode[51] = RSPCompile_UnknownOpcode;
	RSP_Opcode[52] = RSPCompile_UnknownOpcode;
	RSP_Opcode[53] = RSPCompile_UnknownOpcode;
	RSP_Opcode[54] = RSPCompile_UnknownOpcode;
	RSP_Opcode[55] = RSPCompile_UnknownOpcode;
	RSP_Opcode[56] = RSPCompile_UnknownOpcode;
	RSP_Opcode[57] = RSPCompile_UnknownOpcode;
	RSP_Opcode[58] = RSPCompile_SC2;
	RSP_Opcode[59] = RSPCompile_UnknownOpcode;
	RSP_Opcode[60] = RSPCompile_UnknownOpcode;
	RSP_Opcode[61] = RSPCompile_UnknownOpcode;
	RSP_Opcode[62] = RSPCompile_UnknownOpcode;
	RSP_Opcode[63] = RSPCompile_UnknownOpcode;

	RSP_Special[ 0] = RSPCompile_Special_SLL;
	RSP_Special[ 1] = RSPCompile_UnknownOpcode;
	RSP_Special[ 2] = RSPCompile_Special_SRL;
	RSP_Special[ 3] = RSPCompile_Special_SRA;
	RSP_Special[ 4] = RSPCompile_Special_SLLV;
	RSP_Special[ 5] = RSPCompile_UnknownOpcode;
	RSP_Special[ 6] = RSPCompile_Special_SRLV;
	RSP_Special[ 7] = RSPCompile_Special_SRAV;
	RSP_Special[ 8] = RSPCompile_Special_JR;
	RSP_Special[ 9] = RSPCompile_Special_JALR;
	RSP_Special[10] = RSPCompile_UnknownOpcode;
	RSP_Special[11] = RSPCompile_UnknownOpcode;
	RSP_Special[12] = RSPCompile_UnknownOpcode;
	RSP_Special[13] = RSPCompile_Special_BREAK;
	RSP_Special[14] = RSPCompile_UnknownOpcode;
	RSP_Special[15] = RSPCompile_UnknownOpcode;
	RSP_Special[16] = RSPCompile_UnknownOpcode;
	RSP_Special[17] = RSPCompile_UnknownOpcode;
	RSP_Special[18] = RSPCompile_UnknownOpcode;
	RSP_Special[19] = RSPCompile_UnknownOpcode;
	RSP_Special[20] = RSPCompile_UnknownOpcode;
	RSP_Special[21] = RSPCompile_UnknownOpcode;
	RSP_Special[22] = RSPCompile_UnknownOpcode;
	RSP_Special[23] = RSPCompile_UnknownOpcode;
	RSP_Special[24] = RSPCompile_UnknownOpcode;
	RSP_Special[25] = RSPCompile_UnknownOpcode;
	RSP_Special[26] = RSPCompile_UnknownOpcode;
	RSP_Special[27] = RSPCompile_UnknownOpcode;
	RSP_Special[28] = RSPCompile_UnknownOpcode;
	RSP_Special[29] = RSPCompile_UnknownOpcode;
	RSP_Special[30] = RSPCompile_UnknownOpcode;
	RSP_Special[31] = RSPCompile_UnknownOpcode;
	RSP_Special[32] = RSPCompile_Special_ADD;
	RSP_Special[33] = RSPCompile_Special_ADDU;
	RSP_Special[34] = RSPCompile_Special_SUB;
	RSP_Special[35] = RSPCompile_Special_SUBU;
	RSP_Special[36] = RSPCompile_Special_AND;
	RSP_Special[37] = RSPCompile_Special_OR;
	RSP_Special[38] = RSPCompile_Special_XOR;
	RSP_Special[39] = RSPCompile_Special_NOR;
	RSP_Special[40] = RSPCompile_UnknownOpcode;
	RSP_Special[41] = RSPCompile_UnknownOpcode;
	RSP_Special[42] = RSPCompile_Special_SLT;
	RSP_Special[43] = RSPCompile_Special_SLTU;
	RSP_Special[44] = RSPCompile_UnknownOpcode;
	RSP_Special[45] = RSPCompile_UnknownOpcode;
	RSP_Special[46] = RSPCompile_UnknownOpcode;
	RSP_Special[47] = RSPCompile_UnknownOpcode;
	RSP_Special[48] = RSPCompile_UnknownOpcode;
	RSP_Special[49] = RSPCompile_UnknownOpcode;
	RSP_Special[50] = RSPCompile_UnknownOpcode;
	RSP_Special[51] = RSPCompile_UnknownOpcode;
	RSP_Special[52] = RSPCompile_UnknownOpcode;
	RSP_Special[53] = RSPCompile_UnknownOpcode;
	RSP_Special[54] = RSPCompile_UnknownOpcode;
	RSP_Special[55] = RSPCompile_UnknownOpcode;
	RSP_Special[56] = RSPCompile_UnknownOpcode;
	RSP_Special[57] = RSPCompile_UnknownOpcode;
	RSP_Special[58] = RSPCompile_UnknownOpcode;
	RSP_Special[59] = RSPCompile_UnknownOpcode;
	RSP_Special[60] = RSPCompile_UnknownOpcode;
	RSP_Special[61] = RSPCompile_UnknownOpcode;
	RSP_Special[62] = RSPCompile_UnknownOpcode;
	RSP_Special[63] = RSPCompile_UnknownOpcode;

	RSP_RegImm[ 0] = RSPCompile_RegImm_BLTZ;
	RSP_RegImm[ 1] = RSPCompile_RegImm_BGEZ;
	RSP_RegImm[ 2] = RSPCompile_UnknownOpcode;
	RSP_RegImm[ 3] = RSPCompile_UnknownOpcode;
	RSP_RegImm[ 4] = RSPCompile_UnknownOpcode;
	RSP_RegImm[ 5] = RSPCompile_UnknownOpcode;
	RSP_RegImm[ 6] = RSPCompile_UnknownOpcode;
	RSP_RegImm[ 7] = RSPCompile_UnknownOpcode;
	RSP_RegImm[ 8] = RSPCompile_UnknownOpcode;
	RSP_RegImm[ 9] = RSPCompile_UnknownOpcode;
	RSP_RegImm[10] = RSPCompile_UnknownOpcode;
	RSP_RegImm[11] = RSPCompile_UnknownOpcode;
	RSP_RegImm[12] = RSPCompile_UnknownOpcode;
	RSP_RegImm[13] = RSPCompile_UnknownOpcode;
	RSP_RegImm[14] = RSPCompile_UnknownOpcode;
	RSP_RegImm[15] = RSPCompile_UnknownOpcode;
	RSP_RegImm[16] = RSPCompile_RegImm_BLTZAL;
	RSP_RegImm[17] = RSPCompile_RegImm_BGEZAL;
	RSP_RegImm[18] = RSPCompile_UnknownOpcode;
	RSP_RegImm[19] = RSPCompile_UnknownOpcode;
	RSP_RegImm[20] = RSPCompile_UnknownOpcode;
	RSP_RegImm[21] = RSPCompile_UnknownOpcode;
	RSP_RegImm[22] = RSPCompile_UnknownOpcode;
	RSP_RegImm[23] = RSPCompile_UnknownOpcode;
	RSP_RegImm[24] = RSPCompile_UnknownOpcode;
	RSP_RegImm[25] = RSPCompile_UnknownOpcode;
	RSP_RegImm[26] = RSPCompile_UnknownOpcode;
	RSP_RegImm[27] = RSPCompile_UnknownOpcode;
	RSP_RegImm[28] = RSPCompile_UnknownOpcode;
	RSP_RegImm[29] = RSPCompile_UnknownOpcode;
	RSP_RegImm[30] = RSPCompile_UnknownOpcode;
	RSP_RegImm[31] = RSPCompile_UnknownOpcode;

	RSP_Cop0[ 0] = RSPCompile_Cop0_MF;
	RSP_Cop0[ 1] = RSPCompile_UnknownOpcode;
	RSP_Cop0[ 2] = RSPCompile_UnknownOpcode;
	RSP_Cop0[ 3] = RSPCompile_UnknownOpcode;
	RSP_Cop0[ 4] = RSPCompile_Cop0_MT;
	RSP_Cop0[ 5] = RSPCompile_UnknownOpcode;
	RSP_Cop0[ 6] = RSPCompile_UnknownOpcode;
	RSP_Cop0[ 7] = RSPCompile_UnknownOpcode;
	RSP_Cop0[ 8] = RSPCompile_UnknownOpcode;
	RSP_Cop0[ 9] = RSPCompile_UnknownOpcode;
	RSP_Cop0[10] = RSPCompile_UnknownOpcode;
	RSP_Cop0[11] = RSPCompile_UnknownOpcode;
	RSP_Cop0[12] = RSPCompile_UnknownOpcode;
	RSP_Cop0[13] = RSPCompile_UnknownOpcode;
	RSP_Cop0[14] = RSPCompile_UnknownOpcode;
	RSP_Cop0[15] = RSPCompile_UnknownOpcode;
	RSP_Cop0[16] = RSPCompile_UnknownOpcode;
	RSP_Cop0[17] = RSPCompile_UnknownOpcode;
	RSP_Cop0[18] = RSPCompile_UnknownOpcode;
	RSP_Cop0[19] = RSPCompile_UnknownOpcode;
	RSP_Cop0[20] = RSPCompile_UnknownOpcode;
	RSP_Cop0[21] = RSPCompile_UnknownOpcode;
	RSP_Cop0[22] = RSPCompile_UnknownOpcode;
	RSP_Cop0[23] = RSPCompile_UnknownOpcode;
	RSP_Cop0[24] = RSPCompile_UnknownOpcode;
	RSP_Cop0[25] = RSPCompile_UnknownOpcode;
	RSP_Cop0[26] = RSPCompile_UnknownOpcode;
	RSP_Cop0[27] = RSPCompile_UnknownOpcode;
	RSP_Cop0[28] = RSPCompile_UnknownOpcode;
	RSP_Cop0[29] = RSPCompile_UnknownOpcode;
	RSP_Cop0[30] = RSPCompile_UnknownOpcode;
	RSP_Cop0[31] = RSPCompile_UnknownOpcode;

	RSP_Cop2[ 0] = RSPCompile_Cop2_MF;
	RSP_Cop2[ 1] = RSPCompile_UnknownOpcode;
	RSP_Cop2[ 2] = RSPCompile_Cop2_CF;
	RSP_Cop2[ 3] = RSPCompile_UnknownOpcode;
	RSP_Cop2[ 4] = RSPCompile_Cop2_MT;
	RSP_Cop2[ 5] = RSPCompile_UnknownOpcode;
	RSP_Cop2[ 6] = RSPCompile_Cop2_CT;
	RSP_Cop2[ 7] = RSPCompile_UnknownOpcode;
	RSP_Cop2[ 8] = RSPCompile_UnknownOpcode;
	RSP_Cop2[ 9] = RSPCompile_UnknownOpcode;
	RSP_Cop2[10] = RSPCompile_UnknownOpcode;
	RSP_Cop2[11] = RSPCompile_UnknownOpcode;
	RSP_Cop2[12] = RSPCompile_UnknownOpcode;
	RSP_Cop2[13] = RSPCompile_UnknownOpcode;
	RSP_Cop2[14] = RSPCompile_UnknownOpcode;
	RSP_Cop2[15] = RSPCompile_UnknownOpcode;
	RSP_Cop2[16] = RSPCompile_COP2_VECTOR;
	RSP_Cop2[17] = RSPCompile_COP2_VECTOR;
	RSP_Cop2[18] = RSPCompile_COP2_VECTOR;
	RSP_Cop2[19] = RSPCompile_COP2_VECTOR;
	RSP_Cop2[20] = RSPCompile_COP2_VECTOR;
	RSP_Cop2[21] = RSPCompile_COP2_VECTOR;
	RSP_Cop2[22] = RSPCompile_COP2_VECTOR;
	RSP_Cop2[23] = RSPCompile_COP2_VECTOR;
	RSP_Cop2[24] = RSPCompile_COP2_VECTOR;
	RSP_Cop2[25] = RSPCompile_COP2_VECTOR;
	RSP_Cop2[26] = RSPCompile_COP2_VECTOR;
	RSP_Cop2[27] = RSPCompile_COP2_VECTOR;
	RSP_Cop2[28] = RSPCompile_COP2_VECTOR;
	RSP_Cop2[29] = RSPCompile_COP2_VECTOR;
	RSP_Cop2[30] = RSPCompile_COP2_VECTOR;
	RSP_Cop2[31] = RSPCompile_COP2_VECTOR;

	RSP_Vector[ 0] = RSPCompile_Vector_VMULF;
	RSP_Vector[ 1] = RSPCompile_Vector_VMULU;
	RSP_Vector[ 2] = RSPCompile_UnknownOpcode;
	RSP_Vector[ 3] = RSPCompile_UnknownOpcode;
	RSP_Vector[ 4] = RSPCompile_Vector_VMUDL;
	RSP_Vector[ 5] = RSPCompile_Vector_VMUDM;
	RSP_Vector[ 6] = RSPCompile_Vector_VMUDN;
	RSP_Vector[ 7] = RSPCompile_Vector_VMUDH;
	RSP_Vector[ 8] = RSPCompile_Vector_VMACF;
	RSP_Vector[ 9] = RSPCompile_Vector_VMACU;
	RSP_Vector[10] = RSPCompile_UnknownOpcode;
	RSP_Vector[11] = RSPCompile_Vector_VMACQ;
	RSP_Vector[12] = RSPCompile_Vector_VMADL;
	RSP_Vector[13] = RSPCompile_Vector_VMADM;
	RSP_Vector[14] = RSPCompile_Vector_VMADN;
	RSP_Vector[15] = RSPCompile_Vector_VMADH;
	RSP_Vector[16] = RSPCompile_Vector_VADD;
	RSP_Vector[17] = RSPCompile_Vector_VSUB;
	RSP_Vector[18] = RSPCompile_UnknownOpcode;
	RSP_Vector[19] = RSPCompile_Vector_VABS;
	RSP_Vector[20] = RSPCompile_Vector_VADDC;
	RSP_Vector[21] = RSPCompile_Vector_VSUBC;
	RSP_Vector[22] = RSPCompile_UnknownOpcode;
	RSP_Vector[23] = RSPCompile_UnknownOpcode;
	RSP_Vector[24] = RSPCompile_UnknownOpcode;
	RSP_Vector[25] = RSPCompile_UnknownOpcode;
	RSP_Vector[26] = RSPCompile_UnknownOpcode;
	RSP_Vector[27] = RSPCompile_UnknownOpcode;
	RSP_Vector[28] = RSPCompile_UnknownOpcode;
	RSP_Vector[29] = RSPCompile_Vector_VSAW;
	RSP_Vector[30] = RSPCompile_UnknownOpcode;
	RSP_Vector[31] = RSPCompile_UnknownOpcode;
	RSP_Vector[32] = RSPCompile_Vector_VLT;
	RSP_Vector[33] = RSPCompile_Vector_VEQ;
	RSP_Vector[34] = RSPCompile_Vector_VNE;
	RSP_Vector[35] = RSPCompile_Vector_VGE;
	RSP_Vector[36] = RSPCompile_Vector_VCL;
	RSP_Vector[37] = RSPCompile_Vector_VCH;
	RSP_Vector[38] = RSPCompile_Vector_VCR;
	RSP_Vector[39] = RSPCompile_Vector_VMRG;
	RSP_Vector[40] = RSPCompile_Vector_VAND;
	RSP_Vector[41] = RSPCompile_Vector_VNAND;
	RSP_Vector[42] = RSPCompile_Vector_VOR;
	RSP_Vector[43] = RSPCompile_Vector_VNOR;
	RSP_Vector[44] = RSPCompile_Vector_VXOR;
	RSP_Vector[45] = RSPCompile_Vector_VNXOR;
	RSP_Vector[46] = RSPCompile_UnknownOpcode;
	RSP_Vector[47] = RSPCompile_UnknownOpcode;
	RSP_Vector[48] = RSPCompile_Vector_VRCP;
	RSP_Vector[49] = RSPCompile_Vector_VRCPL;
	RSP_Vector[50] = RSPCompile_Vector_VRCPH;
	RSP_Vector[51] = RSPCompile_Vector_VMOV;
	RSP_Vector[52] = RSPCompile_Vector_VRSQ;
	RSP_Vector[53] = RSPCompile_Vector_VRSQL;
	RSP_Vector[54] = RSPCompile_Vector_VRSQH;
	RSP_Vector[55] = RSPCompile_Vector_VNOOP;
	RSP_Vector[56] = RSPCompile_UnknownOpcode;
	RSP_Vector[57] = RSPCompile_UnknownOpcode;
	RSP_Vector[58] = RSPCompile_UnknownOpcode;
	RSP_Vector[59] = RSPCompile_UnknownOpcode;
	RSP_Vector[60] = RSPCompile_UnknownOpcode;
	RSP_Vector[61] = RSPCompile_UnknownOpcode;
	RSP_Vector[62] = RSPCompile_UnknownOpcode;
	RSP_Vector[63] = RSPCompile_UnknownOpcode;

	RSP_Lc2[ 0] = RSPCompile_Opcode_LBV;
	RSP_Lc2[ 1] = RSPCompile_Opcode_LSV;
	RSP_Lc2[ 2] = RSPCompile_Opcode_LLV;
	RSP_Lc2[ 3] = RSPCompile_Opcode_LDV;
	RSP_Lc2[ 4] = RSPCompile_Opcode_LQV;
	RSP_Lc2[ 5] = RSPCompile_Opcode_LRV;
	RSP_Lc2[ 6] = RSPCompile_Opcode_LPV;
	RSP_Lc2[ 7] = RSPCompile_Opcode_LUV;
	RSP_Lc2[ 8] = RSPCompile_UnknownOpcode;
	RSP_Lc2[ 9] = RSPCompile_UnknownOpcode;
	RSP_Lc2[10] = RSPCompile_UnknownOpcode;
	RSP_Lc2[11] = RSPCompile_Opcode_LTV;
	RSP_Lc2[12] = RSPCompile_UnknownOpcode;
	RSP_Lc2[13] = RSPCompile_UnknownOpcode;
	RSP_Lc2[14] = RSPCompile_UnknownOpcode;
	RSP_Lc2[15] = RSPCompile_UnknownOpcode;
	RSP_Lc2[16] = RSPCompile_UnknownOpcode;
	RSP_Lc2[17] = RSPCompile_UnknownOpcode;
	RSP_Lc2[18] = RSPCompile_UnknownOpcode;
	RSP_Lc2[19] = RSPCompile_UnknownOpcode;
	RSP_Lc2[20] = RSPCompile_UnknownOpcode;
	RSP_Lc2[21] = RSPCompile_UnknownOpcode;
	RSP_Lc2[22] = RSPCompile_UnknownOpcode;
	RSP_Lc2[23] = RSPCompile_UnknownOpcode;
	RSP_Lc2[24] = RSPCompile_UnknownOpcode;
	RSP_Lc2[25] = RSPCompile_UnknownOpcode;
	RSP_Lc2[26] = RSPCompile_UnknownOpcode;
	RSP_Lc2[27] = RSPCompile_UnknownOpcode;
	RSP_Lc2[28] = RSPCompile_UnknownOpcode;
	RSP_Lc2[29] = RSPCompile_UnknownOpcode;
	RSP_Lc2[30] = RSPCompile_UnknownOpcode;
	RSP_Lc2[31] = RSPCompile_UnknownOpcode;

	RSP_Sc2[ 0] = RSPCompile_Opcode_SBV;
	RSP_Sc2[ 1] = RSPCompile_Opcode_SSV;
	RSP_Sc2[ 2] = RSPCompile_Opcode_SLV;
	RSP_Sc2[ 3] = RSPCompile_Opcode_SDV;
	RSP_Sc2[ 4] = RSPCompile_Opcode_SQV;
	RSP_Sc2[ 5] = RSPCompile_Opcode_SRV;
	RSP_Sc2[ 6] = RSPCompile_Opcode_SPV;
	RSP_Sc2[ 7] = RSPCompile_Opcode_SUV;
	RSP_Sc2[ 8] = RSPCompile_UnknownOpcode;
	RSP_Sc2[ 9] = RSPCompile_UnknownOpcode;
	RSP_Sc2[10] = RSPCompile_UnknownOpcode;
	RSP_Sc2[11] = RSPCompile_Opcode_STV;
	RSP_Sc2[12] = RSPCompile_UnknownOpcode;
	RSP_Sc2[13] = RSPCompile_UnknownOpcode;
	RSP_Sc2[14] = RSPCompile_UnknownOpcode;
	RSP_Sc2[15] = RSPCompile_UnknownOpcode;
	RSP_Sc2[16] = RSPCompile_UnknownOpcode;
	RSP_Sc2[17] = RSPCompile_UnknownOpcode;
	RSP_Sc2[18] = RSPCompile_UnknownOpcode;
	RSP_Sc2[19] = RSPCompile_UnknownOpcode;
	RSP_Sc2[20] = RSPCompile_UnknownOpcode;
	RSP_Sc2[21] = RSPCompile_UnknownOpcode;
	RSP_Sc2[22] = RSPCompile_UnknownOpcode;
	RSP_Sc2[23] = RSPCompile_UnknownOpcode;
	RSP_Sc2[24] = RSPCompile_UnknownOpcode;
	RSP_Sc2[25] = RSPCompile_UnknownOpcode;
	RSP_Sc2[26] = RSPCompile_UnknownOpcode;
	RSP_Sc2[27] = RSPCompile_UnknownOpcode;
	RSP_Sc2[28] = RSPCompile_UnknownOpcode;
	RSP_Sc2[29] = RSPCompile_UnknownOpcode;
	RSP_Sc2[30] = RSPCompile_UnknownOpcode;
	RSP_Sc2[31] = RSPCompile_UnknownOpcode;

	RSPBlockID = 0;
}

/******************************************************
** ReOrderSubBlock
**
** Desc:
** this can be done, but will be interesting to put
** between branches labels, and actual branches, whichever
** occurs first in code
**
********************************************************/

void ReOrderInstructions(uint32_t StartPC, uint32_t EndPC) {
	uint32_t InstructionCount = EndPC - StartPC;
	uint32_t Count, ReorderedOps, CurrentPC;
	RSPOPCODE PreviousOp, CurrentOp, RspOp;

	PreviousOp.Hex = *(uint32_t*)(IMEM + StartPC);

	if (1 == IsOpcodeBranch(StartPC, PreviousOp)) {
		/* the sub block ends here anyway */
		return;
	}

	if (IsOpcodeNop(StartPC) && IsOpcodeNop(StartPC + 4) && IsOpcodeNop(StartPC + 8)) {
		/* Dont even bother */
		return;
	}

	CPU_Message("***** Doing reorder (%X to %X) *****", StartPC, EndPC);

	if (InstructionCount < 0x0010) { return; }
	if (InstructionCount > 0x0A00) { return; }

	CPU_Message(" Before:");
	for (Count = StartPC; Count < EndPC; Count += 4) {
		RSP_LW_IMEM(Count, &RspOp.Hex);
		//CPU_Message("  %X %s",Count,RSPOpcodeName(RspOp.Hex,Count));
	}

	for (Count = 0; Count < InstructionCount; Count += 4) {
		CurrentPC = StartPC;
		PreviousOp.Hex = *(uint32_t*)(IMEM + CurrentPC);
		ReorderedOps = 0;

		for (;;) {
			CurrentPC += 4;
			if (CurrentPC >= EndPC) { break; }
			CurrentOp.Hex = *(uint32_t*)(IMEM + CurrentPC);

			if (1 == CompareInstructions(CurrentPC, &PreviousOp, &CurrentOp)) {
				/* Move current opcode up */
				*(uint32_t*)(IMEM + CurrentPC - 4) = CurrentOp.Hex;
			 	*(uint32_t*)(IMEM + CurrentPC) = PreviousOp.Hex;

				ReorderedOps++;
				#ifdef REORDER_BLOCK_VERBOSE
				CPU_Message("Swapped %X and %X", CurrentPC - 4, CurrentPC);
				#endif
			}
			PreviousOp.Hex = *(uint32_t*)(IMEM + CurrentPC);

			if (IsOpcodeNop(CurrentPC) && IsOpcodeNop(CurrentPC + 4) && IsOpcodeNop(CurrentPC + 8)) {
				CurrentPC = EndPC;
			}
		}

		if (ReorderedOps == 0) {
			Count = InstructionCount;
		}
	}

	CPU_Message(" After:");
	for (Count = StartPC; Count < EndPC; Count += 4) {
		RSP_LW_IMEM(Count, &RspOp.Hex);
		//CPU_Message("  %X %s",Count,RSPOpcodeName(RspOp.Hex,Count));
	}
	CPU_Message("");
}

void ReOrderSubBlock(RSP_BLOCK * Block) {
	uint32_t end = 0x0ffc;
	uint32_t count;

	//return; //fixthis
	if (!Compiler.bReOrdering) {
		return;
	}
	if (Block->CurrPC > 0xFF0) {
		return;
	}

	/* find the label or jump closest to us */
	if (RspCode.LabelCount) {
		for (count = 0; count < RspCode.LabelCount; count++) {
			if (RspCode.BranchLabels[count] < end &&
				  RspCode.BranchLabels[count] > Block->CurrPC) {
				end = RspCode.BranchLabels[count];
			}
		}
	}
	if (RspCode.BranchCount) {
		for (count = 0; count < RspCode.BranchCount; count++) {
			if (RspCode.BranchLocations[count] < end &&
				  RspCode.BranchLocations[count] > Block->CurrPC) {
				end = RspCode.BranchLocations[count];
			}
		}
	}

	/* it wont actually re-order the op at the end */
	ReOrderInstructions(Block->CurrPC, end);
}

/******************************************************
** DetectGPRConstants
**
** Desc:
**  this needs to be called on a sub-block basis, like
**  after every time we hit a branch and delay slot
**
********************************************************/

void DetectGPRConstants(RSP_CODE * code) {
	uint32_t Count, Constant = 0;

	memset(&code->bIsRegConst, 0, sizeof(int32_t) * 0x20);
	memset(&code->MipsRegConst, 0, sizeof(uint32_t) * 0x20);

	if (!Compiler.bGPRConstants) {
		return;
	}
	CPU_Message("***** Detecting constants *****");

	/*** Setup R0 ***/
	code->bIsRegConst[0] = 1;
	code->MipsRegConst[0] = 0;

	/* Do your global search for them */
	for (Count = 1; Count < 32; Count++) {
		if (IsRegisterConstant(Count, &Constant) == 1) {
			//CPU_Message("Global: %s is a constant of: %08X", GPR_Name(Count), Constant);
			code->bIsRegConst[Count] = 1;
			code->MipsRegConst[Count] = Constant;
		}
	}
	CPU_Message("");
}

/******************************************************
** CompilerToggleBuffer and ClearX86Code
**
** Desc:
**  1> toggles the compiler buffer, useful for poorly
**  taken branches like alignment
**
**  2> clears all the x86 code, jump tables etc
**
********************************************************/

void CompilerToggleBuffer(void) {
	if (dwBuffer == MainBuffer) {
		dwBuffer = SecondaryBuffer;
		pLastPrimary = RSPRecompPos;

		if (pLastSecondary == NULL) {
			pLastSecondary = RSPRecompCodeSecondary;
		}

		RSPRecompPos = pLastSecondary;
		CPU_Message("   (Secondary Buffer Active 0x%08X)", pLastSecondary);
	} else {
		dwBuffer = MainBuffer;
		pLastSecondary = RSPRecompPos;

		if (pLastPrimary == NULL) {
			pLastPrimary = RSPRecompCode;
		}

		RSPRecompPos = pLastPrimary;
		CPU_Message("   (Primary Buffer Active 0x%08X)", pLastPrimary);
	}
}

void ClearAllx86Code (void) {
	extern uint32_t NoOfMaps, MapsCRC[32];
	extern uint8_t *RSPJumpTables;

	memset(&MapsCRC, 0, sizeof(uint32_t) * 0x20);
	NoOfMaps = 0;
	memset(RSPJumpTables,0,0x2000*32);

	RSPRecompPos = RSPRecompCode;

	pLastPrimary = NULL;
	pLastSecondary = NULL;
}

/******************************************************
** Link Branches
**
** Desc:
**  resolves all the collected branches, x86 style
**
********************************************************/

void LinkBranches(RSP_BLOCK * Block) {
	uint32_t Count, Target;
	uint32_t * JumpWord;
	uint8_t * X86Code;
	RSP_BLOCK Save;

	if (!RSPCurrentBlock.ResolveCount) {
		return;
	}
	CPU_Message("***** Linking branches (%i) *****", RSPCurrentBlock.ResolveCount);

	for (Count = 0; Count < RSPCurrentBlock.ResolveCount; Count++) {
		Target = RSPCurrentBlock.BranchesToResolve[Count].TargetPC;
		X86Code = *(RSPJumpTable + (Target >> 2));

		if (!X86Code) {
			*PrgCount = Target;
			CPU_Message("");
			CPU_Message("===== (Generate Code: %04X) =====", Target);
			Save = *Block;

			/* compile this block and link */
			CompilerRSPBlock();
			LinkBranches(Block);

			*Block = Save;
			CPU_Message("===== (End Generate Code: %04X) =====", Target);
			CPU_Message("");
			X86Code = *(RSPJumpTable + (Target >> 2));
		}

		JumpWord = RSPCurrentBlock.BranchesToResolve[Count].X86JumpLoc;
		RSPx86_SetBranch32b(JumpWord, (uint32_t*)X86Code);

		CPU_Message("Linked RSP branch from x86: %08X, to RSP: %X / x86: %08X",
			JumpWord, Target, X86Code);
	}
	CPU_Message("***** Done Linking Branches *****");
	CPU_Message("");
}

/******************************************************
** BuildBranchLabels
**
** Desc:
**   Branch labels are used to start and stop re-ordering
**   sections as well as set the jump table to points
**   within a block that are safe
**
********************************************************/

void BuildBranchLabels(void) {
	RSPOPCODE RspOp;
	uint32_t i, Dest;

	#ifdef BUILD_BRANCHLABELS_VERBOSE
	CPU_Message("***** Building branch labels *****");
	#endif

	for (i = 0; i < 0x1000; i += 4) {
		RspOp.Hex = *(uint32_t*)(IMEM + i);

		if (1 == IsOpcodeBranch(i, RspOp)) {
			if (RspCode.LabelCount >= 175) {
				CompilerWarning("Out of space for Branch Labels",1);
				return;
			}

			RspCode.BranchLocations[RspCode.BranchCount++] = i;
			if (RspOp.op == RSP_SPECIAL) {
				/* register jump not predictable */
			} else if (RspOp.op == RSP_J || RspOp.op == RSP_JAL) {
				/* for JAL its a sub-block for returns */
				Dest = (RspOp.target << 2) & 0xFFC;
				RspCode.BranchLabels[RspCode.LabelCount] = Dest;
				RspCode.LabelCount += 1;
				#ifdef BUILD_BRANCHLABELS_VERBOSE
				CPU_Message("[%02i] Added branch at %X to %X", RspCode.LabelCount, i, Dest);
				#endif
			} else {
				Dest = (i + ((int16_t)RspOp.offset << 2) + 4) & 0xFFC;
				RspCode.BranchLabels[RspCode.LabelCount] = Dest;
				RspCode.LabelCount += 1;
				#ifdef BUILD_BRANCHLABELS_VERBOSE
				CPU_Message("[%02i] Added branch at %X to %X", RspCode.LabelCount, i, Dest);
				#endif
			}
		}
	}

	#ifdef BUILD_BRANCHLABELS_VERBOSE
	CPU_Message("***** End branch labels *****");
	CPU_Message("");
	#endif
}

int32_t IsJumpLabel(uint32_t PC) {
	uint32_t Count;

	if (!RspCode.LabelCount) {
		return 0;
	}

	for (Count = 0; Count < RspCode.LabelCount; Count++) {
		if (PC == RspCode.BranchLabels[Count]) {
			return 1;
		}
	}
	return 0;
}



void CompilerLinkBlocks(void) {
	uint8_t * KnownCode = *(RSPJumpTable + (RSPCompilePC >> 2));

	CPU_Message("***** Linking block to X86: %08X *****", KnownCode);
	RSPNextInstruction = FINISH_BLOCK;

	/* block linking scenario */
	RSPJmpLabel32(0);
	RSPx86_SetBranch32b(RSPRecompPos - 4, KnownCode);
}


void CompilerRSPBlock ( void ) {
	uint32_t Count, Padding, X86BaseAddress = (uint32_t)RSPRecompPos;

	RSPNextInstruction = NORMAL;
	RSPCompilePC = *PrgCount;

	memset(&RSPCurrentBlock, 0, sizeof(RSPCurrentBlock));
	RSPCurrentBlock.StartPC = RSPCompilePC;
	RSPCurrentBlock.CurrPC = RSPCompilePC;


	/* Align the block to a boundary */
	if (X86BaseAddress & 7) {
		Padding = (8 - (X86BaseAddress & 7)) & 7;
		for (Count = 0; Count < Padding; Count++) {
			*(RSPRecompPos++) = 0x90;
		}
	}

	if (Compiler.bReOrdering == 1) {
		memcpy(&RSPCurrentBlock.IMEM[0], IMEM, 0x1000);
		ReOrderSubBlock(&RSPCurrentBlock);
	}

	/* this is for the block about to be compiled */
	*(RSPJumpTable + (RSPCompilePC >> 2)) = RSPRecompPos;


#ifdef USEX64
	RSPMoveConstQwordToX86reg(TLB_Map, x86_R15);
#endif

	do {
		/*
		** Re-Ordering is setup to allow us to have loop labels
		** so here we see if this is one and put it in the jump table
		**/

		if (RSPNextInstruction == NORMAL && IsJumpLabel(RSPCompilePC)) {
			/* jumps come around twice */
			if (NULL == *(RSPJumpTable + (RSPCompilePC >> 2))) {
				CPU_Message("***** Adding Jump Table Entry for PC: %04X at X86: %08X *****", RSPCompilePC, RSPRecompPos);
				CPU_Message("");
				*(RSPJumpTable + (RSPCompilePC >> 2)) = RSPRecompPos;

				/* reorder from here to next label or branch */
				RSPCurrentBlock.CurrPC = RSPCompilePC;
				ReOrderSubBlock(&RSPCurrentBlock);
			} else if (RSPNextInstruction != DELAY_SLOT_DONE) {
				/*
				 * we could link the blocks here, but performance
				 * wise it might be better to just let it run
				 */
			}
		}

		if (Compiler.bSections == 1) {
		//	if (1 == RSP_DoSections()) {
		//		continue;
		//	}
		}

		#ifdef X86_RECOMP_VERBOSE
		if (0 == IsOpcodeNop(RSPCompilePC)) {
			CPU_Message("X86 Address: %08X", RSPRecompPos);
		}
		#endif

		RSP_LW_IMEM(RSPCompilePC, &RSPOpC.Hex);

		if (RSPOpC.Hex == 0xFFFFFFFF) {
			/* i think this pops up an unknown op dialog */
			/* NextInstruction = FINISH_BLOCK; */
		} else {
			((void (*)()) RSP_Opcode[ RSPOpC.op ])();
		}

		//printf("%08x\n", RSPNextInstruction);

		switch (RSPNextInstruction) {
		case NORMAL:
			RSPCompilePC += 4;
			break;
		case DO_DELAY_SLOT:
			RSPNextInstruction = DELAY_SLOT;
			RSPCompilePC += 4;
			break;
		case DELAY_SLOT:
			RSPNextInstruction = DELAY_SLOT_DONE;
			RSPCompilePC -= 4;
			break;
		case FINISH_SUB_BLOCK:
			RSPNextInstruction = NORMAL;
			RSPCompilePC += 8;
			if (RSPCompilePC >= 0x1000) {
				RSPNextInstruction = FINISH_BLOCK;
			} else if (NULL == *(RSPJumpTable + (RSPCompilePC >> 2))) {
				/* this is for the new block being compiled now */
				CPU_Message("**** Continuing static SubBlock (jump table entry added for PC: %04X at X86: %08X) *****", RSPCompilePC, RSPRecompPos);
				*(RSPJumpTable + (RSPCompilePC >> 2)) = RSPRecompPos;

				RSPCurrentBlock.CurrPC = RSPCompilePC;
				/* reorder from after delay to next label or branch */
				ReOrderSubBlock(&RSPCurrentBlock);
			} else {
				CompilerLinkBlocks();
			}
			break;

		case FINISH_BLOCK: break;
		default:
			DisplayError("Rsp Main loop\n\nWTF NextInstruction = %d",RSPNextInstruction);
			RSPCompilePC += 4;
			break;
		}
	} while ( RSPNextInstruction != FINISH_BLOCK && RSPCompilePC < 0x1000);
	CPU_Message("==== end of recompiled code ====");

	if (Compiler.bReOrdering == 1) {
		memcpy(IMEM, &RSPCurrentBlock.IMEM[0], 0x1000);
	}
}

#include "types.h"


void RunRecompilerCPU ( uint32_t Cycles ) {
	uint8_t * Block = 0;

	RSP_Running = 1;
	RSPSetJumpTable();



	while (RSP_Running) {

		Block = *(RSPJumpTable + (*PrgCount >> 2));

		if (Block == NULL) {
			//__try {
				memset(&RspCode, 0, sizeof(RspCode));
				BuildBranchLabels();
				DetectGPRConstants(&RspCode);
				CompilerRSPBlock();
			/*} __except(EXCEPTION_EXECUTE_HANDLER) {
				DisplayError("Error RSPCompilePC = %08X", RSPCompilePC);
				ClearAllx86Code();
				continue;
			}*/

			Block = *(RSPJumpTable + (*PrgCount >> 2));

			/*
			** we are done compiling, but we may have references
			** to fill in still either from this block, or jumps
			** that go out of it, let's rock
			**/

			LinkBranches(&RSPCurrentBlock);
		}
		{

			void (*block)();
			block=Block;
#ifdef USEX64			
			__asm__ __volatile__("pushq %rax");
			__asm__ __volatile__("pushq %rbx");
			__asm__ __volatile__("pushq %rcx");
			__asm__ __volatile__("pushq %rdx");
			__asm__ __volatile__("pushq %rbp");
			__asm__ __volatile__("pushq %r8");
			__asm__ __volatile__("pushq %r9");
			__asm__ __volatile__("pushq %r10");
			__asm__ __volatile__("pushq %r11");
			__asm__ __volatile__("pushq %r12");
			__asm__ __volatile__("pushq %r13");
			__asm__ __volatile__("pushq %r14");
			__asm__ __volatile__("pushq %r15");

			block();

			__asm__ __volatile__("popq %r15");
			__asm__ __volatile__("popq %r14");
			__asm__ __volatile__("popq %r13");
			__asm__ __volatile__("popq %r12");
			__asm__ __volatile__("popq %r11");
			__asm__ __volatile__("popq %r10");
			__asm__ __volatile__("popq %r9");
			__asm__ __volatile__("popq %r8");
			__asm__ __volatile__("popq %rbp");
			__asm__ __volatile__("popq %rdx");
			__asm__ __volatile__("popq %rcx");
			__asm__ __volatile__("popq %rbx");
			__asm__ __volatile__("popq %rax");

#else
			__asm__ __volatile__("pusha");
			block();
			__asm__ __volatile__("popa");
#endif
		}

	}

//	if (IsMmxEnabled == 1) {
//		_Emms();
//	}
	return;
}