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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
|
using System;
using static Ryujinx.Graphics.Gal.Shader.ShaderDecodeHelper;
namespace Ryujinx.Graphics.Gal.Shader
{
static partial class ShaderDecode
{
public static void Bfe_C(ShaderIrBlock Block, long OpCode, int Position)
{
EmitBfe(Block, OpCode, ShaderOper.CR);
}
public static void Bfe_I(ShaderIrBlock Block, long OpCode, int Position)
{
EmitBfe(Block, OpCode, ShaderOper.Imm);
}
public static void Bfe_R(ShaderIrBlock Block, long OpCode, int Position)
{
EmitBfe(Block, OpCode, ShaderOper.RR);
}
public static void Fadd_C(ShaderIrBlock Block, long OpCode, int Position)
{
EmitFadd(Block, OpCode, ShaderOper.CR);
}
public static void Fadd_I(ShaderIrBlock Block, long OpCode, int Position)
{
EmitFadd(Block, OpCode, ShaderOper.Immf);
}
public static void Fadd_I32(ShaderIrBlock Block, long OpCode, int Position)
{
ShaderIrNode OperA = OpCode.Gpr8();
ShaderIrNode OperB = OpCode.Immf32_20();
bool NegB = OpCode.Read(53);
bool AbsA = OpCode.Read(54);
bool NegA = OpCode.Read(56);
bool AbsB = OpCode.Read(57);
OperA = GetAluFabsFneg(OperA, AbsA, NegA);
OperB = GetAluFabsFneg(OperB, AbsB, NegB);
ShaderIrOp Op = new ShaderIrOp(ShaderIrInst.Fadd, OperA, OperB);
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op)));
}
public static void Fadd_R(ShaderIrBlock Block, long OpCode, int Position)
{
EmitFadd(Block, OpCode, ShaderOper.RR);
}
public static void Ffma_CR(ShaderIrBlock Block, long OpCode, int Position)
{
EmitFfma(Block, OpCode, ShaderOper.CR);
}
public static void Ffma_I(ShaderIrBlock Block, long OpCode, int Position)
{
EmitFfma(Block, OpCode, ShaderOper.Immf);
}
public static void Ffma_RC(ShaderIrBlock Block, long OpCode, int Position)
{
EmitFfma(Block, OpCode, ShaderOper.RC);
}
public static void Ffma_RR(ShaderIrBlock Block, long OpCode, int Position)
{
EmitFfma(Block, OpCode, ShaderOper.RR);
}
public static void Fmnmx_C(ShaderIrBlock Block, long OpCode, int Position)
{
EmitFmnmx(Block, OpCode, ShaderOper.CR);
}
public static void Fmnmx_I(ShaderIrBlock Block, long OpCode, int Position)
{
EmitFmnmx(Block, OpCode, ShaderOper.Immf);
}
public static void Fmnmx_R(ShaderIrBlock Block, long OpCode, int Position)
{
EmitFmnmx(Block, OpCode, ShaderOper.RR);
}
public static void Fmul_I32(ShaderIrBlock Block, long OpCode, int Position)
{
ShaderIrNode OperA = OpCode.Gpr8();
ShaderIrNode OperB = OpCode.Immf32_20();
ShaderIrOp Op = new ShaderIrOp(ShaderIrInst.Fmul, OperA, OperB);
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op)));
}
public static void Fmul_C(ShaderIrBlock Block, long OpCode, int Position)
{
EmitFmul(Block, OpCode, ShaderOper.CR);
}
public static void Fmul_I(ShaderIrBlock Block, long OpCode, int Position)
{
EmitFmul(Block, OpCode, ShaderOper.Immf);
}
public static void Fmul_R(ShaderIrBlock Block, long OpCode, int Position)
{
EmitFmul(Block, OpCode, ShaderOper.RR);
}
public static void Fset_C(ShaderIrBlock Block, long OpCode, int Position)
{
EmitFset(Block, OpCode, ShaderOper.CR);
}
public static void Fset_I(ShaderIrBlock Block, long OpCode, int Position)
{
EmitFset(Block, OpCode, ShaderOper.Immf);
}
public static void Fset_R(ShaderIrBlock Block, long OpCode, int Position)
{
EmitFset(Block, OpCode, ShaderOper.RR);
}
public static void Fsetp_C(ShaderIrBlock Block, long OpCode, int Position)
{
EmitFsetp(Block, OpCode, ShaderOper.CR);
}
public static void Fsetp_I(ShaderIrBlock Block, long OpCode, int Position)
{
EmitFsetp(Block, OpCode, ShaderOper.Immf);
}
public static void Fsetp_R(ShaderIrBlock Block, long OpCode, int Position)
{
EmitFsetp(Block, OpCode, ShaderOper.RR);
}
public static void Iadd_C(ShaderIrBlock Block, long OpCode, int Position)
{
EmitIadd(Block, OpCode, ShaderOper.CR);
}
public static void Iadd_I(ShaderIrBlock Block, long OpCode, int Position)
{
EmitIadd(Block, OpCode, ShaderOper.Imm);
}
public static void Iadd_I32(ShaderIrBlock Block, long OpCode, int Position)
{
ShaderIrNode OperA = OpCode.Gpr8();
ShaderIrNode OperB = OpCode.Imm32_20();
bool NegA = OpCode.Read(56);
OperA = GetAluIneg(OperA, NegA);
ShaderIrOp Op = new ShaderIrOp(ShaderIrInst.Add, OperA, OperB);
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op)));
}
public static void Iadd_R(ShaderIrBlock Block, long OpCode, int Position)
{
EmitIadd(Block, OpCode, ShaderOper.RR);
}
public static void Iadd3_C(ShaderIrBlock Block, long OpCode, int Position)
{
EmitIadd3(Block, OpCode, ShaderOper.CR);
}
public static void Iadd3_I(ShaderIrBlock Block, long OpCode, int Position)
{
EmitIadd3(Block, OpCode, ShaderOper.Imm);
}
public static void Iadd3_R(ShaderIrBlock Block, long OpCode, int Position)
{
EmitIadd3(Block, OpCode, ShaderOper.RR);
}
public static void Imnmx_C(ShaderIrBlock Block, long OpCode, int Position)
{
EmitImnmx(Block, OpCode, ShaderOper.CR);
}
public static void Imnmx_I(ShaderIrBlock Block, long OpCode, int Position)
{
EmitImnmx(Block, OpCode, ShaderOper.Imm);
}
public static void Imnmx_R(ShaderIrBlock Block, long OpCode, int Position)
{
EmitImnmx(Block, OpCode, ShaderOper.RR);
}
public static void Ipa(ShaderIrBlock Block, long OpCode, int Position)
{
ShaderIrNode OperA = OpCode.Abuf28();
ShaderIrNode OperB = OpCode.Gpr20();
ShaderIpaMode Mode = (ShaderIpaMode)(OpCode.Read(54, 3));
ShaderIrMetaIpa Meta = new ShaderIrMetaIpa(Mode);
ShaderIrOp Op = new ShaderIrOp(ShaderIrInst.Ipa, OperA, OperB, null, Meta);
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op)));
}
public static void Iscadd_C(ShaderIrBlock Block, long OpCode, int Position)
{
EmitIscadd(Block, OpCode, ShaderOper.CR);
}
public static void Iscadd_I(ShaderIrBlock Block, long OpCode, int Position)
{
EmitIscadd(Block, OpCode, ShaderOper.Imm);
}
public static void Iscadd_R(ShaderIrBlock Block, long OpCode, int Position)
{
EmitIscadd(Block, OpCode, ShaderOper.RR);
}
public static void Iset_C(ShaderIrBlock Block, long OpCode, int Position)
{
EmitIset(Block, OpCode, ShaderOper.CR);
}
public static void Iset_I(ShaderIrBlock Block, long OpCode, int Position)
{
EmitIset(Block, OpCode, ShaderOper.Imm);
}
public static void Iset_R(ShaderIrBlock Block, long OpCode, int Position)
{
EmitIset(Block, OpCode, ShaderOper.RR);
}
public static void Isetp_C(ShaderIrBlock Block, long OpCode, int Position)
{
EmitIsetp(Block, OpCode, ShaderOper.CR);
}
public static void Isetp_I(ShaderIrBlock Block, long OpCode, int Position)
{
EmitIsetp(Block, OpCode, ShaderOper.Imm);
}
public static void Isetp_R(ShaderIrBlock Block, long OpCode, int Position)
{
EmitIsetp(Block, OpCode, ShaderOper.RR);
}
public static void Lop_I32(ShaderIrBlock Block, long OpCode, int Position)
{
int SubOp = OpCode.Read(53, 3);
bool InvA = OpCode.Read(55);
bool InvB = OpCode.Read(56);
ShaderIrInst Inst = 0;
switch (SubOp)
{
case 0: Inst = ShaderIrInst.And; break;
case 1: Inst = ShaderIrInst.Or; break;
case 2: Inst = ShaderIrInst.Xor; break;
}
ShaderIrNode OperB = GetAluNot(OpCode.Imm32_20(), InvB);
//SubOp == 3 is pass, used by the not instruction
//which just moves the inverted register value.
if (SubOp < 3)
{
ShaderIrNode OperA = GetAluNot(OpCode.Gpr8(), InvA);
ShaderIrOp Op = new ShaderIrOp(Inst, OperA, OperB);
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op)));
}
else
{
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), OperB)));
}
}
public static void Lop_C(ShaderIrBlock Block, long OpCode, int Position)
{
EmitLop(Block, OpCode, ShaderOper.CR);
}
public static void Lop_I(ShaderIrBlock Block, long OpCode, int Position)
{
EmitLop(Block, OpCode, ShaderOper.Imm);
}
public static void Lop_R(ShaderIrBlock Block, long OpCode, int Position)
{
EmitLop(Block, OpCode, ShaderOper.RR);
}
public static void Mufu(ShaderIrBlock Block, long OpCode, int Position)
{
int SubOp = OpCode.Read(20, 0xf);
bool AbsA = OpCode.Read(46);
bool NegA = OpCode.Read(48);
ShaderIrInst Inst = 0;
switch (SubOp)
{
case 0: Inst = ShaderIrInst.Fcos; break;
case 1: Inst = ShaderIrInst.Fsin; break;
case 2: Inst = ShaderIrInst.Fex2; break;
case 3: Inst = ShaderIrInst.Flg2; break;
case 4: Inst = ShaderIrInst.Frcp; break;
case 5: Inst = ShaderIrInst.Frsq; break;
case 8: Inst = ShaderIrInst.Fsqrt; break;
default: throw new NotImplementedException(SubOp.ToString());
}
ShaderIrNode OperA = OpCode.Gpr8();
ShaderIrOp Op = new ShaderIrOp(Inst, GetAluFabsFneg(OperA, AbsA, NegA));
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op)));
}
public static void Psetp(ShaderIrBlock Block, long OpCode, int Position)
{
bool NegA = OpCode.Read(15);
bool NegB = OpCode.Read(32);
bool NegP = OpCode.Read(42);
ShaderIrInst LopInst = OpCode.BLop24();
ShaderIrNode OperA = OpCode.Pred12();
ShaderIrNode OperB = OpCode.Pred29();
if (NegA)
{
OperA = new ShaderIrOp(ShaderIrInst.Bnot, OperA);
}
if (NegB)
{
OperB = new ShaderIrOp(ShaderIrInst.Bnot, OperB);
}
ShaderIrOp Op = new ShaderIrOp(LopInst, OperA, OperB);
ShaderIrOperPred P0Node = OpCode.Pred3();
ShaderIrOperPred P1Node = OpCode.Pred0();
ShaderIrOperPred P2Node = OpCode.Pred39();
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(P0Node, Op)));
LopInst = OpCode.BLop45();
if (LopInst == ShaderIrInst.Band && P1Node.IsConst && P2Node.IsConst)
{
return;
}
ShaderIrNode P2NNode = P2Node;
if (NegP)
{
P2NNode = new ShaderIrOp(ShaderIrInst.Bnot, P2NNode);
}
Op = new ShaderIrOp(ShaderIrInst.Bnot, P0Node);
Op = new ShaderIrOp(LopInst, Op, P2NNode);
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(P1Node, Op)));
Op = new ShaderIrOp(LopInst, P0Node, P2NNode);
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(P0Node, Op)));
}
public static void Rro_C(ShaderIrBlock Block, long OpCode, int Position)
{
EmitRro(Block, OpCode, ShaderOper.CR);
}
public static void Rro_I(ShaderIrBlock Block, long OpCode, int Position)
{
EmitRro(Block, OpCode, ShaderOper.Immf);
}
public static void Rro_R(ShaderIrBlock Block, long OpCode, int Position)
{
EmitRro(Block, OpCode, ShaderOper.RR);
}
public static void Shl_C(ShaderIrBlock Block, long OpCode, int Position)
{
EmitAluBinary(Block, OpCode, ShaderOper.CR, ShaderIrInst.Lsl);
}
public static void Shl_I(ShaderIrBlock Block, long OpCode, int Position)
{
EmitAluBinary(Block, OpCode, ShaderOper.Imm, ShaderIrInst.Lsl);
}
public static void Shl_R(ShaderIrBlock Block, long OpCode, int Position)
{
EmitAluBinary(Block, OpCode, ShaderOper.RR, ShaderIrInst.Lsl);
}
public static void Shr_C(ShaderIrBlock Block, long OpCode, int Position)
{
EmitAluBinary(Block, OpCode, ShaderOper.CR, GetShrInst(OpCode));
}
public static void Shr_I(ShaderIrBlock Block, long OpCode, int Position)
{
EmitAluBinary(Block, OpCode, ShaderOper.Imm, GetShrInst(OpCode));
}
public static void Shr_R(ShaderIrBlock Block, long OpCode, int Position)
{
EmitAluBinary(Block, OpCode, ShaderOper.RR, GetShrInst(OpCode));
}
private static ShaderIrInst GetShrInst(long OpCode)
{
bool Signed = OpCode.Read(48);
return Signed ? ShaderIrInst.Asr : ShaderIrInst.Lsr;
}
public static void Vmad(ShaderIrBlock Block, long OpCode, int Position)
{
ShaderIrNode OperA = OpCode.Gpr8();
ShaderIrNode OperB;
if (OpCode.Read(50))
{
OperB = OpCode.Gpr20();
}
else
{
OperB = OpCode.Imm19_20();
}
ShaderIrOperGpr OperC = OpCode.Gpr39();
ShaderIrNode Tmp = new ShaderIrOp(ShaderIrInst.Mul, OperA, OperB);
ShaderIrNode Final = new ShaderIrOp(ShaderIrInst.Add, Tmp, OperC);
int Shr = OpCode.Read(51, 3);
if (Shr != 0)
{
int Shift = (Shr == 2) ? 15 : 7;
Final = new ShaderIrOp(ShaderIrInst.Lsr, Final, new ShaderIrOperImm(Shift));
}
Block.AddNode(new ShaderIrCmnt("Stubbed. Instruction is reduced to a * b + c"));
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Final)));
}
public static void Xmad_CR(ShaderIrBlock Block, long OpCode, int Position)
{
EmitXmad(Block, OpCode, ShaderOper.CR);
}
public static void Xmad_I(ShaderIrBlock Block, long OpCode, int Position)
{
EmitXmad(Block, OpCode, ShaderOper.Imm);
}
public static void Xmad_RC(ShaderIrBlock Block, long OpCode, int Position)
{
EmitXmad(Block, OpCode, ShaderOper.RC);
}
public static void Xmad_RR(ShaderIrBlock Block, long OpCode, int Position)
{
EmitXmad(Block, OpCode, ShaderOper.RR);
}
private static void EmitAluBinary(
ShaderIrBlock Block,
long OpCode,
ShaderOper Oper,
ShaderIrInst Inst)
{
ShaderIrNode OperA = OpCode.Gpr8(), OperB;
switch (Oper)
{
case ShaderOper.CR: OperB = OpCode.Cbuf34(); break;
case ShaderOper.Imm: OperB = OpCode.Imm19_20(); break;
case ShaderOper.RR: OperB = OpCode.Gpr20(); break;
default: throw new ArgumentException(nameof(Oper));
}
ShaderIrNode Op = new ShaderIrOp(Inst, OperA, OperB);
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op)));
}
private static void EmitBfe(ShaderIrBlock Block, long OpCode, ShaderOper Oper)
{
//TODO: Handle the case where position + length
//is greater than the word size, in this case the sign bit
//needs to be replicated to fill the remaining space.
bool NegB = OpCode.Read(48);
bool NegA = OpCode.Read(49);
ShaderIrNode OperA = OpCode.Gpr8(), OperB;
switch (Oper)
{
case ShaderOper.CR: OperB = OpCode.Cbuf34(); break;
case ShaderOper.Imm: OperB = OpCode.Imm19_20(); break;
case ShaderOper.RR: OperB = OpCode.Gpr20(); break;
default: throw new ArgumentException(nameof(Oper));
}
ShaderIrNode Op;
bool Signed = OpCode.Read(48); //?
if (OperB is ShaderIrOperImm PosLen)
{
int Position = (PosLen.Value >> 0) & 0xff;
int Length = (PosLen.Value >> 8) & 0xff;
int LSh = 32 - (Position + Length);
ShaderIrInst RightShift = Signed
? ShaderIrInst.Asr
: ShaderIrInst.Lsr;
Op = new ShaderIrOp(ShaderIrInst.Lsl, OperA, new ShaderIrOperImm(LSh));
Op = new ShaderIrOp(RightShift, Op, new ShaderIrOperImm(LSh + Position));
}
else
{
ShaderIrOperImm Shift = new ShaderIrOperImm(8);
ShaderIrOperImm Mask = new ShaderIrOperImm(0xff);
ShaderIrNode OpPos, OpLen;
OpPos = new ShaderIrOp(ShaderIrInst.And, OperB, Mask);
OpLen = new ShaderIrOp(ShaderIrInst.Lsr, OperB, Shift);
OpLen = new ShaderIrOp(ShaderIrInst.And, OpLen, Mask);
Op = new ShaderIrOp(ShaderIrInst.Lsr, OperA, OpPos);
Op = ExtendTo32(Op, Signed, OpLen);
}
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op)));
}
private static void EmitFadd(ShaderIrBlock Block, long OpCode, ShaderOper Oper)
{
bool NegB = OpCode.Read(45);
bool AbsA = OpCode.Read(46);
bool NegA = OpCode.Read(48);
bool AbsB = OpCode.Read(49);
bool Sat = OpCode.Read(50);
ShaderIrNode OperA = OpCode.Gpr8(), OperB;
OperA = GetAluFabsFneg(OperA, AbsA, NegA);
switch (Oper)
{
case ShaderOper.CR: OperB = OpCode.Cbuf34(); break;
case ShaderOper.Immf: OperB = OpCode.Immf19_20(); break;
case ShaderOper.RR: OperB = OpCode.Gpr20(); break;
default: throw new ArgumentException(nameof(Oper));
}
OperB = GetAluFabsFneg(OperB, AbsB, NegB);
ShaderIrNode Op = new ShaderIrOp(ShaderIrInst.Fadd, OperA, OperB);
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), GetAluFsat(Op, Sat))));
}
private static void EmitFmul(ShaderIrBlock Block, long OpCode, ShaderOper Oper)
{
bool NegB = OpCode.Read(48);
bool Sat = OpCode.Read(50);
ShaderIrNode OperA = OpCode.Gpr8(), OperB;
switch (Oper)
{
case ShaderOper.CR: OperB = OpCode.Cbuf34(); break;
case ShaderOper.Immf: OperB = OpCode.Immf19_20(); break;
case ShaderOper.RR: OperB = OpCode.Gpr20(); break;
default: throw new ArgumentException(nameof(Oper));
}
OperB = GetAluFneg(OperB, NegB);
ShaderIrNode Op = new ShaderIrOp(ShaderIrInst.Fmul, OperA, OperB);
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), GetAluFsat(Op, Sat))));
}
private static void EmitFfma(ShaderIrBlock Block, long OpCode, ShaderOper Oper)
{
bool NegB = OpCode.Read(48);
bool NegC = OpCode.Read(49);
bool Sat = OpCode.Read(50);
ShaderIrNode OperA = OpCode.Gpr8(), OperB, OperC;
switch (Oper)
{
case ShaderOper.CR: OperB = OpCode.Cbuf34(); break;
case ShaderOper.Immf: OperB = OpCode.Immf19_20(); break;
case ShaderOper.RC: OperB = OpCode.Gpr39(); break;
case ShaderOper.RR: OperB = OpCode.Gpr20(); break;
default: throw new ArgumentException(nameof(Oper));
}
OperB = GetAluFneg(OperB, NegB);
if (Oper == ShaderOper.RC)
{
OperC = GetAluFneg(OpCode.Cbuf34(), NegC);
}
else
{
OperC = GetAluFneg(OpCode.Gpr39(), NegC);
}
ShaderIrOp Op = new ShaderIrOp(ShaderIrInst.Ffma, OperA, OperB, OperC);
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), GetAluFsat(Op, Sat))));
}
private static void EmitIadd(ShaderIrBlock Block, long OpCode, ShaderOper Oper)
{
ShaderIrNode OperA = OpCode.Gpr8();
ShaderIrNode OperB;
switch (Oper)
{
case ShaderOper.CR: OperB = OpCode.Cbuf34(); break;
case ShaderOper.Imm: OperB = OpCode.Imm19_20(); break;
case ShaderOper.RR: OperB = OpCode.Gpr20(); break;
default: throw new ArgumentException(nameof(Oper));
}
bool NegA = OpCode.Read(49);
bool NegB = OpCode.Read(48);
OperA = GetAluIneg(OperA, NegA);
OperB = GetAluIneg(OperB, NegB);
ShaderIrOp Op = new ShaderIrOp(ShaderIrInst.Add, OperA, OperB);
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op)));
}
private static void EmitIadd3(ShaderIrBlock Block, long OpCode, ShaderOper Oper)
{
int Mode = OpCode.Read(37, 3);
bool Neg1 = OpCode.Read(51);
bool Neg2 = OpCode.Read(50);
bool Neg3 = OpCode.Read(49);
int Height1 = OpCode.Read(35, 3);
int Height2 = OpCode.Read(33, 3);
int Height3 = OpCode.Read(31, 3);
ShaderIrNode OperB;
switch (Oper)
{
case ShaderOper.CR: OperB = OpCode.Cbuf34(); break;
case ShaderOper.Imm: OperB = OpCode.Imm19_20(); break;
case ShaderOper.RR: OperB = OpCode.Gpr20(); break;
default: throw new ArgumentException(nameof(Oper));
}
ShaderIrNode ApplyHeight(ShaderIrNode Src, int Height)
{
if (Oper != ShaderOper.RR)
{
return Src;
}
switch (Height)
{
case 0: return Src;
case 1: return new ShaderIrOp(ShaderIrInst.And, Src, new ShaderIrOperImm(0xffff));
case 2: return new ShaderIrOp(ShaderIrInst.Lsr, Src, new ShaderIrOperImm(16));
default: throw new InvalidOperationException();
}
}
ShaderIrNode Src1 = GetAluIneg(ApplyHeight(OpCode.Gpr8(), Height1), Neg1);
ShaderIrNode Src2 = GetAluIneg(ApplyHeight(OperB, Height2), Neg2);
ShaderIrNode Src3 = GetAluIneg(ApplyHeight(OpCode.Gpr39(), Height3), Neg3);
ShaderIrOp Sum = new ShaderIrOp(ShaderIrInst.Add, Src1, Src2);
if (Oper == ShaderOper.RR)
{
switch (Mode)
{
case 1: Sum = new ShaderIrOp(ShaderIrInst.Lsr, Sum, new ShaderIrOperImm(16)); break;
case 2: Sum = new ShaderIrOp(ShaderIrInst.Lsl, Sum, new ShaderIrOperImm(16)); break;
}
}
//Note: Here there should be a "+ 1" when carry flag is set
//but since carry is mostly ignored by other instructions, it's excluded for now
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), new ShaderIrOp(ShaderIrInst.Add, Sum, Src3))));
}
private static void EmitIscadd(ShaderIrBlock Block, long OpCode, ShaderOper Oper)
{
bool NegB = OpCode.Read(48);
bool NegA = OpCode.Read(49);
ShaderIrNode OperA = OpCode.Gpr8(), OperB;
ShaderIrOperImm Scale = OpCode.Imm5_39();
switch (Oper)
{
case ShaderOper.CR: OperB = OpCode.Cbuf34(); break;
case ShaderOper.Imm: OperB = OpCode.Imm19_20(); break;
case ShaderOper.RR: OperB = OpCode.Gpr20(); break;
default: throw new ArgumentException(nameof(Oper));
}
OperA = GetAluIneg(OperA, NegA);
OperB = GetAluIneg(OperB, NegB);
ShaderIrOp ScaleOp = new ShaderIrOp(ShaderIrInst.Lsl, OperA, Scale);
ShaderIrOp AddOp = new ShaderIrOp(ShaderIrInst.Add, OperB, ScaleOp);
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), AddOp)));
}
private static void EmitFmnmx(ShaderIrBlock Block, long OpCode, ShaderOper Oper)
{
EmitMnmx(Block, OpCode, true, Oper);
}
private static void EmitImnmx(ShaderIrBlock Block, long OpCode, ShaderOper Oper)
{
EmitMnmx(Block, OpCode, false, Oper);
}
private static void EmitMnmx(ShaderIrBlock Block, long OpCode, bool IsFloat, ShaderOper Oper)
{
bool NegB = OpCode.Read(45);
bool AbsA = OpCode.Read(46);
bool NegA = OpCode.Read(48);
bool AbsB = OpCode.Read(49);
ShaderIrNode OperA = OpCode.Gpr8(), OperB;
if (IsFloat)
{
OperA = GetAluFabsFneg(OperA, AbsA, NegA);
}
else
{
OperA = GetAluIabsIneg(OperA, AbsA, NegA);
}
switch (Oper)
{
case ShaderOper.CR: OperB = OpCode.Cbuf34(); break;
case ShaderOper.Imm: OperB = OpCode.Imm19_20(); break;
case ShaderOper.Immf: OperB = OpCode.Immf19_20(); break;
case ShaderOper.RR: OperB = OpCode.Gpr20(); break;
default: throw new ArgumentException(nameof(Oper));
}
if (IsFloat)
{
OperB = GetAluFabsFneg(OperB, AbsB, NegB);
}
else
{
OperB = GetAluIabsIneg(OperB, AbsB, NegB);
}
ShaderIrOperPred Pred = OpCode.Pred39();
ShaderIrOp Op;
ShaderIrInst MaxInst = IsFloat ? ShaderIrInst.Fmax : ShaderIrInst.Max;
ShaderIrInst MinInst = IsFloat ? ShaderIrInst.Fmin : ShaderIrInst.Min;
if (Pred.IsConst)
{
bool IsMax = OpCode.Read(42);
Op = new ShaderIrOp(IsMax
? MaxInst
: MinInst, OperA, OperB);
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op)));
}
else
{
ShaderIrNode PredN = OpCode.Pred39N();
ShaderIrOp OpMax = new ShaderIrOp(MaxInst, OperA, OperB);
ShaderIrOp OpMin = new ShaderIrOp(MinInst, OperA, OperB);
ShaderIrAsg AsgMax = new ShaderIrAsg(OpCode.Gpr0(), OpMax);
ShaderIrAsg AsgMin = new ShaderIrAsg(OpCode.Gpr0(), OpMin);
Block.AddNode(OpCode.PredNode(new ShaderIrCond(PredN, AsgMax, Not: true)));
Block.AddNode(OpCode.PredNode(new ShaderIrCond(PredN, AsgMin, Not: false)));
}
}
private static void EmitRro(ShaderIrBlock Block, long OpCode, ShaderOper Oper)
{
//Note: this is a range reduction instruction and is supposed to
//be used with Mufu, here it just moves the value and ignores the operation.
bool NegA = OpCode.Read(45);
bool AbsA = OpCode.Read(49);
ShaderIrNode OperA;
switch (Oper)
{
case ShaderOper.CR: OperA = OpCode.Cbuf34(); break;
case ShaderOper.Immf: OperA = OpCode.Immf19_20(); break;
case ShaderOper.RR: OperA = OpCode.Gpr20(); break;
default: throw new ArgumentException(nameof(Oper));
}
OperA = GetAluFabsFneg(OperA, AbsA, NegA);
Block.AddNode(new ShaderIrCmnt("Stubbed."));
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), OperA)));
}
private static void EmitFset(ShaderIrBlock Block, long OpCode, ShaderOper Oper)
{
EmitSet(Block, OpCode, true, Oper);
}
private static void EmitIset(ShaderIrBlock Block, long OpCode, ShaderOper Oper)
{
EmitSet(Block, OpCode, false, Oper);
}
private static void EmitSet(ShaderIrBlock Block, long OpCode, bool IsFloat, ShaderOper Oper)
{
bool NegA = OpCode.Read(43);
bool AbsB = OpCode.Read(44);
bool NegB = OpCode.Read(53);
bool AbsA = OpCode.Read(54);
bool BoolFloat = OpCode.Read(IsFloat ? 52 : 44);
ShaderIrNode OperA = OpCode.Gpr8(), OperB;
switch (Oper)
{
case ShaderOper.CR: OperB = OpCode.Cbuf34(); break;
case ShaderOper.Imm: OperB = OpCode.Imm19_20(); break;
case ShaderOper.Immf: OperB = OpCode.Immf19_20(); break;
case ShaderOper.RR: OperB = OpCode.Gpr20(); break;
default: throw new ArgumentException(nameof(Oper));
}
ShaderIrInst CmpInst;
if (IsFloat)
{
OperA = GetAluFabsFneg(OperA, AbsA, NegA);
OperB = GetAluFabsFneg(OperB, AbsB, NegB);
CmpInst = OpCode.CmpF();
}
else
{
CmpInst = OpCode.Cmp();
}
ShaderIrOp Op = new ShaderIrOp(CmpInst, OperA, OperB);
ShaderIrInst LopInst = OpCode.BLop45();
ShaderIrOperPred PNode = OpCode.Pred39();
ShaderIrNode Imm0, Imm1;
if (BoolFloat)
{
Imm0 = new ShaderIrOperImmf(0);
Imm1 = new ShaderIrOperImmf(1);
}
else
{
Imm0 = new ShaderIrOperImm(0);
Imm1 = new ShaderIrOperImm(-1);
}
ShaderIrNode Asg0 = new ShaderIrAsg(OpCode.Gpr0(), Imm0);
ShaderIrNode Asg1 = new ShaderIrAsg(OpCode.Gpr0(), Imm1);
if (LopInst != ShaderIrInst.Band || !PNode.IsConst)
{
ShaderIrOp Op2 = new ShaderIrOp(LopInst, Op, PNode);
Asg0 = new ShaderIrCond(Op2, Asg0, Not: true);
Asg1 = new ShaderIrCond(Op2, Asg1, Not: false);
}
else
{
Asg0 = new ShaderIrCond(Op, Asg0, Not: true);
Asg1 = new ShaderIrCond(Op, Asg1, Not: false);
}
Block.AddNode(OpCode.PredNode(Asg0));
Block.AddNode(OpCode.PredNode(Asg1));
}
private static void EmitFsetp(ShaderIrBlock Block, long OpCode, ShaderOper Oper)
{
EmitSetp(Block, OpCode, true, Oper);
}
private static void EmitIsetp(ShaderIrBlock Block, long OpCode, ShaderOper Oper)
{
EmitSetp(Block, OpCode, false, Oper);
}
private static void EmitSetp(ShaderIrBlock Block, long OpCode, bool IsFloat, ShaderOper Oper)
{
bool AbsA = OpCode.Read(7);
bool NegP = OpCode.Read(42);
bool NegA = OpCode.Read(43);
bool AbsB = OpCode.Read(44);
ShaderIrNode OperA = OpCode.Gpr8(), OperB;
switch (Oper)
{
case ShaderOper.CR: OperB = OpCode.Cbuf34(); break;
case ShaderOper.Imm: OperB = OpCode.Imm19_20(); break;
case ShaderOper.Immf: OperB = OpCode.Immf19_20(); break;
case ShaderOper.RR: OperB = OpCode.Gpr20(); break;
default: throw new ArgumentException(nameof(Oper));
}
ShaderIrInst CmpInst;
if (IsFloat)
{
OperA = GetAluFabsFneg(OperA, AbsA, NegA);
OperB = GetAluFabs (OperB, AbsB);
CmpInst = OpCode.CmpF();
}
else
{
CmpInst = OpCode.Cmp();
}
ShaderIrOp Op = new ShaderIrOp(CmpInst, OperA, OperB);
ShaderIrOperPred P0Node = OpCode.Pred3();
ShaderIrOperPred P1Node = OpCode.Pred0();
ShaderIrOperPred P2Node = OpCode.Pred39();
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(P0Node, Op)));
ShaderIrInst LopInst = OpCode.BLop45();
if (LopInst == ShaderIrInst.Band && P1Node.IsConst && P2Node.IsConst)
{
return;
}
ShaderIrNode P2NNode = P2Node;
if (NegP)
{
P2NNode = new ShaderIrOp(ShaderIrInst.Bnot, P2NNode);
}
Op = new ShaderIrOp(ShaderIrInst.Bnot, P0Node);
Op = new ShaderIrOp(LopInst, Op, P2NNode);
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(P1Node, Op)));
Op = new ShaderIrOp(LopInst, P0Node, P2NNode);
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(P0Node, Op)));
}
private static void EmitLop(ShaderIrBlock Block, long OpCode, ShaderOper Oper)
{
int SubOp = OpCode.Read(41, 3);
bool InvA = OpCode.Read(39);
bool InvB = OpCode.Read(40);
ShaderIrInst Inst = 0;
switch (SubOp)
{
case 0: Inst = ShaderIrInst.And; break;
case 1: Inst = ShaderIrInst.Or; break;
case 2: Inst = ShaderIrInst.Xor; break;
}
ShaderIrNode OperA = GetAluNot(OpCode.Gpr8(), InvA);
ShaderIrNode OperB;
switch (Oper)
{
case ShaderOper.CR: OperB = OpCode.Cbuf34(); break;
case ShaderOper.Imm: OperB = OpCode.Imm19_20(); break;
case ShaderOper.RR: OperB = OpCode.Gpr20(); break;
default: throw new ArgumentException(nameof(Oper));
}
OperB = GetAluNot(OperB, InvB);
ShaderIrNode Op;
if (SubOp < 3)
{
Op = new ShaderIrOp(Inst, OperA, OperB);
}
else
{
Op = OperB;
}
ShaderIrNode Compare = new ShaderIrOp(ShaderIrInst.Cne, Op, new ShaderIrOperImm(0));
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Pred48(), Compare)));
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), Op)));
}
private static void EmitXmad(ShaderIrBlock Block, long OpCode, ShaderOper Oper)
{
//TODO: Confirm SignAB/C, it is just a guess.
//TODO: Implement Mode 3 (CSFU), what it does?
bool SignAB = OpCode.Read(48);
bool SignC = OpCode.Read(49);
bool HighB = OpCode.Read(52);
bool HighA = OpCode.Read(53);
int Mode = OpCode.Read(50, 7);
ShaderIrNode OperA = OpCode.Gpr8(), OperB, OperC;
ShaderIrOperImm Imm16 = new ShaderIrOperImm(16);
ShaderIrOperImm ImmMsk = new ShaderIrOperImm(0xffff);
ShaderIrInst ShiftAB = SignAB ? ShaderIrInst.Asr : ShaderIrInst.Lsr;
ShaderIrInst ShiftC = SignC ? ShaderIrInst.Asr : ShaderIrInst.Lsr;
if (HighA)
{
OperA = new ShaderIrOp(ShiftAB, OperA, Imm16);
}
switch (Oper)
{
case ShaderOper.CR: OperB = OpCode.Cbuf34(); break;
case ShaderOper.Imm: OperB = OpCode.Imm19_20(); break;
case ShaderOper.RC: OperB = OpCode.Gpr39(); break;
case ShaderOper.RR: OperB = OpCode.Gpr20(); break;
default: throw new ArgumentException(nameof(Oper));
}
bool ProductShiftLeft = false, Merge = false;
if (Oper == ShaderOper.RC)
{
OperC = OpCode.Cbuf34();
}
else
{
OperC = OpCode.Gpr39();
ProductShiftLeft = OpCode.Read(36);
Merge = OpCode.Read(37);
}
switch (Mode)
{
//CLO.
case 1: OperC = ExtendTo32(OperC, SignC, 16); break;
//CHI.
case 2: OperC = new ShaderIrOp(ShiftC, OperC, Imm16); break;
}
ShaderIrNode OperBH = OperB;
if (HighB)
{
OperBH = new ShaderIrOp(ShiftAB, OperBH, Imm16);
}
ShaderIrOp MulOp = new ShaderIrOp(ShaderIrInst.Mul, OperA, OperBH);
if (ProductShiftLeft)
{
MulOp = new ShaderIrOp(ShaderIrInst.Lsl, MulOp, Imm16);
}
ShaderIrOp AddOp = new ShaderIrOp(ShaderIrInst.Add, MulOp, OperC);
if (Merge)
{
AddOp = new ShaderIrOp(ShaderIrInst.And, AddOp, ImmMsk);
OperB = new ShaderIrOp(ShaderIrInst.Lsl, OperB, Imm16);
AddOp = new ShaderIrOp(ShaderIrInst.Or, AddOp, OperB);
}
if (Mode == 4)
{
OperB = new ShaderIrOp(ShaderIrInst.Lsl, OperB, Imm16);
AddOp = new ShaderIrOp(ShaderIrInst.Or, AddOp, OperB);
}
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OpCode.Gpr0(), AddOp)));
}
}
}
|