-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathExecuteMEM.cpp
1637 lines (1450 loc) · 57 KB
/
ExecuteMEM.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 "basetypes.h"
#include "byteswap.h"
#include "ExecuteMEM.h"
#include "InstructionCache.h"
#include "mpe.h"
#include "NuonMemoryMap.h"
#include "NuonEnvironment.h"
#define MIP(mip_me) (((uint32)(mip_me)) >> BilinearInfo_XYMipmap(control))
static constexpr uint32 tilemask[16] = { 0xFFFF,0x7FFF,0x3FFF,0x1FFF,0xFFF,0x7FF,0x3FF,0x1FF,0xFF,0x7F,0x3F,0x1F,0xF,0x7,0x3,0x1 }; // already shifted by 16
static constexpr int8 pixel_type_width[16] = {
-2,//Type 0: MPEG Pixel (macroblock size of 16 bytes)
-1,//Type 1: 4 bit (must be accessed in groups of four)
1, //Type 2: 16 bit
0, //Type 3: 8 bit (must be accessed in groups of two)
2, //Type 4: 32 bit
2, //Type 5: 16 bit + 16 bit Z
3, //Type 6: 32 bit + 32 bit Z
0, //Type 7: Reserved
0, //Type 8: Byte
1, //Type 9: Word
2, //Type 10: Scalar
3, //Type 12: Short Vector
4, //Type 13: Vector
0, //Type 14: Reserved
0 //Type 15: Reserved
};
extern NuonEnvironment nuonEnv;
static uint16 mirrorLookup[65536]; // = bit-reversal
static uint8 satColY[1024];
static uint8 satColCrCb[2048]; // last 1024 are for Chnorm
static uint8 satColY16[1024];
static uint8 satColCrCb16[2048]; // last 1024 are for Chnorm
void GenerateMirrorLookupTable()
{
uint8 mirrorLookup8[256];
for (uint32 i = 0; i <= 0xFF; i++)
{
uint8 mirror = 0;
mirror |= ((i & 0x01) << 7) & 0x80;
mirror |= ((i & 0x02) << 5) & 0x40;
mirror |= ((i & 0x04) << 3) & 0x20;
mirror |= ((i & 0x08) << 1) & 0x10;
mirror |= ((i & 0x10) >> 1) & 0x08;
mirror |= ((i & 0x20) >> 3) & 0x04;
mirror |= ((i & 0x40) >> 5) & 0x02;
mirror |= ((i & 0x80) >> 7) & 0x01;
mirrorLookup8[i] = mirror;
}
for (uint32 i = 0; i <= 0xFFFF; i++)
{
union {
struct { uint8 u8[2]; };
uint16 u16;
} xtmp;
#ifdef LITTLE_ENDIAN
xtmp.u8[1] = mirrorLookup8[i&0xFF];
xtmp.u8[0] = mirrorLookup8[(i>>8)&0xFF];
#else
xtmp.u8[0] = mirrorLookup8[i&0xFF];
xtmp.u8[1] = mirrorLookup8[(i>>8)&0xFF];
#endif
mirrorLookup[i] = xtmp.u16;
}
}
inline void SaturateColorComponentsOrg(uint32 &Y, uint32 &Cr, uint32 &Cb, const bool bChnorm)
{
const uint32 YLookup[4] = {(Y >> (16 + 13 - 7)) & 0xFFUL,0xFFUL,0xFFUL,0xFFUL}; //!! was X,FF,0,0 before
Y = YLookup[Y >> (16 + 14)];
switch(Cr >> (16+14))
{
case 0:
//If chnorm bit is set, clamp to 0x7F
if(bChnorm && ((Cr>>16) > 0x1FFFu))
Cr = 0x7F;
else
Cr = (Cr >> (16 + 13 - 7)) & 0xFFUL;
break;
case 1:
//clamp to 0x7F or 0xFF
Cr = bChnorm ? 0x7F : 0xFF;
break;
case 2:
//clamp to 0x80 or 0x00
Cr = bChnorm ? 0x80 : 0x00;
break;
case 3:
//Clamp to 0x80 or 0x00
if(bChnorm && ((Cr>>16) < 0xE000u))
Cr = 0x80;
else
{
if(bChnorm)
Cr = (Cr >> (16 + 13 - 7)) & 0xFFUL;
else
Cr = 0x00;
}
break;
}
switch(Cb >> (16+14))
{
case 0:
//If chnorm bit is set, clamp to 0x7F
if(bChnorm && ((Cb>>16) > 0x1FFFu))
Cb = 0x7F;
else
Cb = (Cb >> (16 + 13 - 7)) & 0xFFUL;
break;
case 1:
//clamp to 0x7F or 0xFF
Cb = bChnorm ? 0x7F : 0xFF;
break;
case 2:
//clamp to 0x80 or 0x00
Cb = bChnorm ? 0x80 : 0x00;
break;
case 3:
//Clamp to 0x80 or 0x00
if(bChnorm && ((Cb>>16) < 0xE000u))
Cb = 0x80;
else
{
if(bChnorm)
Cb = (Cb >> (16 + 13 - 7)) & 0xFFUL;
else
Cb = 0x00;
}
break;
}
if(bChnorm)
{
Cr = (Cr + 0x80) & 0xFFUL;
Cb = (Cb + 0x80) & 0xFFUL;
}
}
__forceinline uint32 SaturateColorComponents32bit(uint32 Y, uint32 Cr, uint32 Cb, const uint32 ChnormOffset)
{
/*uint32 Y2 = Y;
uint32 Cr2 = Cr;
uint32 Cb2 = Cb;
SaturateColorComponentsOrg(Y2, Cr2, Cb2, bChnorm);*/
Y = satColY[Y >> 22];
Cr = satColCrCb[(Cr >> 22) + ChnormOffset];
Cb = satColCrCb[(Cb >> 22) + ChnormOffset];
/*assert(Y2 == Y);
assert(Cr2 == Cr);
assert(Cb2 == Cb);*/
#ifdef LITTLE_ENDIAN
return Y | (Cr << 8) | (Cb << 16);
#else
return (Y << 24) | (Cr << 16) | (Cb << 8);
#endif
}
__forceinline uint16 SaturateColorComponents16bitSwapped(uint32 Y, uint32 Cr, uint32 Cb, const uint32 ChnormOffset)
{
Y = satColY16[Y >> 22];
Cr = satColCrCb16[(Cr >> 22) + ChnormOffset];
Cb = satColCrCb16[(Cb >> 22) + ChnormOffset];
#ifdef LITTLE_ENDIAN
return (uint16)Y | SwapBytes((uint16)((Cr << 5) | Cb));
#else
return (uint16)((Y << 8) | (Cr << 5) | Cb);
#endif
}
void GenerateSaturateColorTables()
{
for (uint32 i = 0; i < 1024; ++i)
{
uint32 Y = i << 22;
uint32 Cr = i << 22;
uint32 Cb = i << 22;
SaturateColorComponentsOrg(Y, Cr, Cb, true);
satColCrCb[i+1024] = Cr; // Chnorm variant
satColCrCb16[i+1024] = (Cr & 0xF8) >> 3; // Chnorm variant
Y = i << 22;
Cr = i << 22;
Cb = i << 22;
SaturateColorComponentsOrg(Y, Cr, Cb, false);
satColY[i] = Y;
satColCrCb[i] = Cr;
satColY16[i] = Y & 0xFC;
satColCrCb16[i] = (Cr & 0xF8) >> 3;
}
}
inline uint32 CalculateBilinearAddress(MPE &mpe, const uint32 control, uint32 x, uint32 y)
{
x>>=16;
y>>=16;
if ((x|y) == 0) // quick computation possible?
return 0;
if (BilinearInfo_XRev(control))
x = mirrorLookup[x];
if (BilinearInfo_YRev(control))
y = mirrorLookup[y];
mpe.ba_mipped_xoffset = MIP(x & tilemask[BilinearInfo_XTile(control)]);
return MIP(y & tilemask[BilinearInfo_YTile(control)]) * MIP(BilinearInfo_XYWidth(control)) + mpe.ba_mipped_xoffset;
}
// leave __fastcall here!
uint32 __fastcall GetBilinearAddress(const uint32 xy, const uint32 control)
{
if (xy == 0) // quick computation possible?
return 0;
uint32 x = xy>>16;
uint32 y = xy&0xFFFFu;
const int8 pixwidth = BilinearInfo_PixelWidth(pixel_type_width,control);
uint32 mipped_xoffset;
if (x == 0) // quick computation possible?
mipped_xoffset = 0;
else
{
if (BilinearInfo_XRev(control))
x = mirrorLookup[x];
mipped_xoffset = MIP(x & tilemask[BilinearInfo_XTile(control)]);
}
uint32 offset;
if (y == 0) // quick computation possible?
offset = mipped_xoffset;
else
{
if (BilinearInfo_YRev(control))
y = mirrorLookup[y];
offset = MIP(y & tilemask[BilinearInfo_YTile(control)]) * MIP(BilinearInfo_XYWidth(control)) + mipped_xoffset;
}
return (pixwidth >= 0) ? (offset << pixwidth) : //Everything but 4-bit pixels and MPEG
((offset >> 1) | (mipped_xoffset<<31)); //4-bit pixels, store single/lowest bit for later-on addressing in MSB (see _LoadPixelAbsolute)
}
void Execute_Mirror(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
uint32 x = pRegs[nuance.fields[FIELD_MEM_FROM]];
x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
mpe.regs[nuance.fields[FIELD_MEM_TO]] = ((x >> 16) | (x << 16));
}
void Execute_MV_SImmediate(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
mpe.regs[nuance.fields[FIELD_MEM_TO]] = nuance.fields[FIELD_MEM_FROM];
}
void Execute_MV_SScalar(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
mpe.regs[nuance.fields[FIELD_MEM_TO]] = pRegs[nuance.fields[FIELD_MEM_FROM]];
}
void Execute_MV_V(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
const uint32 src = nuance.fields[FIELD_MEM_FROM];
const uint32 dest = nuance.fields[FIELD_MEM_TO];
mpe.regs[dest ] = pRegs[src];
mpe.regs[dest + 1] = pRegs[src + 1];
mpe.regs[dest + 2] = pRegs[src + 2];
mpe.regs[dest + 3] = pRegs[src + 3];
}
void Execute_PopVector(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
const uint32* const srcPtr = (uint32 *)&(mpe.dtrom[mpe.sp & MPE_VALID_MEMORY_MASK]);
uint32 dest_vector[4] = {srcPtr[0],srcPtr[1],srcPtr[2],srcPtr[3]};
uint32* const destPtr = (uint32 *)&(mpe.regs[nuance.fields[FIELD_MEM_TO]]);
SwapVectorBytes(dest_vector);
destPtr[0] = dest_vector[0];
destPtr[1] = dest_vector[1];
destPtr[2] = dest_vector[2];
destPtr[3] = dest_vector[3];
mpe.sp += 16;
}
void Execute_PopVectorRz(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
const uint32* const srcPtr = (uint32 *)&(mpe.dtrom[mpe.sp & MPE_VALID_MEMORY_MASK]);
uint32 dest_vector[4] = {srcPtr[0],srcPtr[1],srcPtr[2],srcPtr[3]};
uint32* const destPtr = (uint32 *)&mpe.regs[nuance.fields[FIELD_MEM_TO]];
SwapVectorBytes(dest_vector);
destPtr[0] = dest_vector[0];
destPtr[1] = dest_vector[1];
destPtr[2] = dest_vector[2];
mpe.rz = dest_vector[3];
mpe.sp += 16;
}
void Execute_PopScalarRzi1(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
const uint32* const srcPtr = (uint32 *)&(mpe.dtrom[mpe.sp & MPE_VALID_MEMORY_MASK]);
uint32 dest_vector[4] = {srcPtr[0],srcPtr[1],srcPtr[2],srcPtr[3]};
SwapVectorBytes(dest_vector);
mpe.regs[nuance.fields[FIELD_MEM_TO]] = dest_vector[0];
mpe.cc = dest_vector[1];
mpe.rzi1 = dest_vector[2];
mpe.rz = dest_vector[3];
mpe.sp += 16;
}
void Execute_PopScalarRzi2(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
const uint32* const srcPtr = (uint32 *)&(mpe.dtrom[mpe.sp & MPE_VALID_MEMORY_MASK]);
uint32 dest_vector[4] = {srcPtr[0],srcPtr[1],srcPtr[2],srcPtr[3]};
SwapVectorBytes(dest_vector);
mpe.regs[nuance.fields[FIELD_MEM_TO]] = dest_vector[0];
mpe.cc = dest_vector[1];
mpe.rzi2 = dest_vector[2];
mpe.rz = dest_vector[3];
mpe.sp += 16;
}
void Execute_PushVector(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
mpe.sp -= 16;
const uint32* const pVal = &(pRegs[nuance.fields[FIELD_MEM_FROM]]);
uint32 src_vector[4] = {pVal[0],pVal[1],pVal[2],pVal[3]};
uint32* const destPtr = (uint32 *)&(mpe.dtrom[mpe.sp & MPE_VALID_MEMORY_MASK]);
SwapVectorBytes(src_vector);
destPtr[0] = src_vector[0];
destPtr[1] = src_vector[1];
destPtr[2] = src_vector[2];
destPtr[3] = src_vector[3];
}
void Execute_PushVectorRz(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
mpe.sp -= 16;
const uint32* const pVal = &(pRegs[nuance.fields[FIELD_MEM_FROM]]);
uint32 src_vector[4] = {pVal[0],pVal[1],pVal[2],pRegs[RZ_REG+0]};
uint32* const destPtr = (uint32 *)&(mpe.dtrom[mpe.sp & MPE_VALID_MEMORY_MASK]);
SwapVectorBytes(src_vector);
destPtr[0] = src_vector[0];
destPtr[1] = src_vector[1];
destPtr[2] = src_vector[2];
destPtr[3] = src_vector[3];
}
void Execute_PushScalarRzi1(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
mpe.sp -= 16;
uint32 src_vector[4] = {pRegs[nuance.fields[FIELD_MEM_FROM]],mpe.tempCC,pRegs[RZ_REG+1],pRegs[RZ_REG+0]};
uint32* const destPtr = (uint32 *)&(mpe.dtrom[mpe.sp & MPE_VALID_MEMORY_MASK]);
SwapVectorBytes(src_vector);
destPtr[0] = src_vector[0];
destPtr[1] = src_vector[1];
destPtr[2] = src_vector[2];
destPtr[3] = src_vector[3];
}
void Execute_PushScalarRzi2(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
mpe.sp -= 16;
uint32 src_vector[4] = {pRegs[nuance.fields[FIELD_MEM_FROM]],mpe.tempCC,pRegs[RZ_REG+2],pRegs[RZ_REG+0]};
uint32* const destPtr = (uint32 *)&(mpe.dtrom[mpe.sp & MPE_VALID_MEMORY_MASK]);
SwapVectorBytes(src_vector);
destPtr[0] = src_vector[0];
destPtr[1] = src_vector[1];
destPtr[2] = src_vector[2];
destPtr[3] = src_vector[3];
}
void Execute_LoadScalarControlRegisterAbsolute(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
mpe.regs[nuance.fields[FIELD_MEM_TO]] = mpe.ReadControlRegister(nuance.fields[FIELD_MEM_FROM] - MPE_CTRL_BASE, pRegs);
}
void Execute_LoadByteAbsolute(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
uint32 data = *((uint8 *)nuance.fields[FIELD_MEM_POINTER]);
data <<= 24;
mpe.regs[nuance.fields[FIELD_MEM_TO]] = data;
}
void Execute_LoadWordAbsolute(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
uint32 data = ((uint32)(*((uint8 *)nuance.fields[FIELD_MEM_POINTER]))) << 24;
data |= ((uint32)( *((uint8 *)(nuance.fields[FIELD_MEM_POINTER]+1)) )) << 16;
mpe.regs[nuance.fields[FIELD_MEM_TO]] = data;
}
void Execute_LoadScalarAbsolute(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
mpe.regs[nuance.fields[FIELD_MEM_TO]] = SwapBytes(*((uint32 *)nuance.fields[FIELD_MEM_POINTER]));
}
void Execute_LoadScalarLinear(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
const uint32 dest = nuance.fields[FIELD_MEM_TO];
const uint32 address = pRegs[nuance.fields[FIELD_MEM_FROM]];
if((address < MPE_CTRL_BASE) || (address >= MPE_RESV_BASE))
{
mpe.regs[dest] = SwapBytes(*((uint32 *)nuonEnv.GetPointerToMemory(mpe.mpeIndex, address & 0xFFFFFFFC)));
}
else
{
mpe.regs[dest] = mpe.ReadControlRegister(address - MPE_CTRL_BASE, pRegs);
}
}
void Execute_LoadVectorAbsolute(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
const uint32 * const srcPtr = (uint32 *)nuance.fields[FIELD_MEM_POINTER];
uint32* const destPtr = &mpe.regs[nuance.fields[FIELD_MEM_TO]];
destPtr[0] = srcPtr[0];
destPtr[1] = srcPtr[1];
destPtr[2] = srcPtr[2];
destPtr[3] = srcPtr[3];
SwapVectorBytes(destPtr);
}
void Execute_LoadVectorControlRegisterAbsolute(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
const uint32 dest = nuance.fields[FIELD_MEM_TO];
const uint32 address = nuance.fields[FIELD_MEM_FROM];
mpe.regs[dest ] = mpe.ReadControlRegister(address - MPE_CTRL_BASE, pRegs);
mpe.regs[dest + 1] = mpe.ReadControlRegister(address + 4 - MPE_CTRL_BASE, pRegs);
mpe.regs[dest + 2] = mpe.ReadControlRegister(address + 8 - MPE_CTRL_BASE, pRegs);
mpe.regs[dest + 3] = mpe.ReadControlRegister(address + 12 - MPE_CTRL_BASE, pRegs);
}
// leave __fastcall here!
void __fastcall _LoadPixelAbsolute(MPE* const __restrict mpe, const void* const __restrict memPtr)
{
const uint32 control = mpe->ba_control;
uint32* const __restrict regs = mpe->regs+mpe->ba_reg_offset;
const uint32 pixType = BilinearInfo_XYType(control);
const bool bChnorm = BilinearInfo_XYChnorm(control);
switch(pixType)
{
case 0x0:
//MPEG
return;
case 0x1:
{
//4 bit
//The initial xoffset is guaranteed to start at the first pixel of a group of four.
//This means that for even values of X, the pixel bits to be extracted are always [7:4]
//and for odd values of X, the pixel bits to be extracted are [3:0]
const uint32 pixelData8 = *((uint8 *)memPtr);
const uint32 pixelData32 = (mpe->ba_mipped_xoffset&0x80000000u) ? (pixelData8 & 0x0FUL) : (pixelData8 >> 4);
regs[0] = (mpe->clutbase & 0xFFFFFFC0UL) | (pixelData32 << 2);
regs[1] = 0;
regs[2] = 0;
return;
}
case 0x2:
case 0x5:
{
//16+16Z
const uint16 pixelData16 = SwapBytes(*((uint16*)memPtr));
regs[0] = ((uint32)pixelData16 << 14) & (0xFCUL << 22);
regs[1] = ((uint32)pixelData16 << 20) & (0xF8UL << 22);
regs[2] = ((uint32)pixelData16 << 25) & (0xF8UL << 22);
if(bChnorm)
{
regs[1] = (regs[1] - 0x20000000UL) & 0xFE000000UL;
regs[2] = (regs[2] - 0x20000000UL) & 0xFE000000UL;
}
return;
}
case 0x3:
{
//8 bit
const uint32 pixelData32 = *((uint8 *)memPtr);
regs[0] = (mpe->clutbase & 0xFFFFFC00UL) | (pixelData32 << 2);
regs[1] = 0;
regs[2] = 0;
return;
}
case 0x4:
case 0x6:
{
//32 bit or 32+32Z (both behave the same for LD_P)
const uint32 pixelData32 = *((uint32*)memPtr);
#if 0 // SSSE3 check needed!
__m128i tmp0 = _mm_shuffle_epi8(_mm_cvtsi32_si128(pixelData32), _mm_set_epi8(-1,-1,-1,-1, 2,-1,-1,-1, 1,-1,-1,-1, 0,-1,-1,-1));
tmp0 = _mm_srli_epi32(tmp0, 2);
if (bChnorm)
{
tmp0 = _mm_sub_epi32(tmp0, _mm_set_epi32(0, 0x20000000UL, 0x20000000UL, 0));
tmp0 = _mm_and_si128(tmp0, _mm_set_epi32(0xFFFFFFFFUL, 0xFFC00000UL, 0xFFC00000UL, 0xFFFFFFFFUL));
}
const unsigned int regs3 = regs[3]; //!! meh
_mm_store_si128((__m128i*)regs, tmp0); //!! is this always aligned?
regs[3] = regs3;
#else
#ifdef LITTLE_ENDIAN
regs[0] = (pixelData32 << 22) & (0xFFUL << 22);
regs[1] = (pixelData32 << 14) & (0xFFUL << 22);
regs[2] = (pixelData32 << 6) & (0xFFUL << 22);
#else
regs[0] = (pixelData32 >> 2) & (0xFFUL << 22);
regs[1] = (pixelData32 << 6) & (0xFFUL << 22);
regs[2] = (pixelData32 << 14) & (0xFFUL << 22);
#endif
if(bChnorm)
{
regs[1] = (regs[1] - 0x20000000UL) & 0xFFC00000UL;
regs[2] = (regs[2] - 0x20000000UL) & 0xFFC00000UL;
}
#endif
return;
}
}
}
void Execute_LoadPixelAbsolute(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
void * const memPtr = (void *)((uint32 *)nuance.fields[FIELD_MEM_POINTER]);
const uint32 dest = nuance.fields[FIELD_MEM_TO];
uint32 pixType;
bool bChnorm;
if(nuance.fields[FIELD_MEM_INFO] & MEM_INFO_BILINEAR_UV)
{
pixType = BilinearInfo_XYType(pRegs[UVC_REG]);
bChnorm = BilinearInfo_XYChnorm(pRegs[UVC_REG]);
}
else if(nuance.fields[FIELD_MEM_INFO] & MEM_INFO_BILINEAR_XY)
{
pixType = BilinearInfo_XYType(pRegs[XYC_REG]);
bChnorm = BilinearInfo_XYChnorm(pRegs[XYC_REG]);
}
else
{
pixType = BilinearInfo_XYType(mpe.linpixctl);
bChnorm = BilinearInfo_XYChnorm(mpe.linpixctl);
}
switch(pixType)
{
case 0x0:
//MPEG
return;
case 0x1:
{
//4 bit
//The initial xoffset is guaranteed to start at the first pixel of a group of four.
//This means that for even values of X, the pixel bits to be extracted are always [7:4]
//and for odd values of X, the pixel bits to be extracted are [3:0]
const uint32 pixelData8 = *((uint8 *)memPtr);
const uint32 pixelData32 = (mpe.ba_mipped_xoffset&1u) ? (pixelData8 & 0x0FUL) : (pixelData8 >> 4);
mpe.regs[dest ] = (mpe.clutbase & 0xFFFFFFC0UL) | (pixelData32 << 2);
mpe.regs[dest+1] = 0;
mpe.regs[dest+2] = 0;
return;
}
case 0x2:
case 0x5:
{
//16+16Z
const uint16 pixelData16 = SwapBytes(*((uint16*)memPtr));
mpe.regs[dest ] = ((uint32)pixelData16 << 14) & (0xFCUL << 22);
mpe.regs[dest+1] = ((uint32)pixelData16 << 20) & (0xF8UL << 22);
mpe.regs[dest+2] = ((uint32)pixelData16 << 25) & (0xF8UL << 22);
if(bChnorm)
{
mpe.regs[dest+1] = (mpe.regs[dest+1] - 0x20000000UL) & 0xFE000000UL;
mpe.regs[dest+2] = (mpe.regs[dest+2] - 0x20000000UL) & 0xFE000000UL;
}
return;
}
case 0x3:
{
//8 bit
const uint32 pixelData32 = *((uint8 *)memPtr);
mpe.regs[dest ] = (mpe.clutbase & 0xFFFFFC00UL) | (pixelData32 << 2);
mpe.regs[dest+1] = 0;
mpe.regs[dest+2] = 0;
return;
}
case 0x4:
case 0x6:
{
//32 bit or 32+32Z (both behave the same for LD_P)
const uint32 pixelData32 = *((uint32*)memPtr);
#ifdef LITTLE_ENDIAN
mpe.regs[dest ] = (pixelData32 << 22) & (0xFFUL << 22);
mpe.regs[dest+1] = (pixelData32 << 14) & (0xFFUL << 22);
mpe.regs[dest+2] = (pixelData32 << 6) & (0xFFUL << 22);
#else
mpe.regs[dest ] = (pixelData32 >> 2) & (0xFFUL << 22);
mpe.regs[dest+1] = (pixelData32 << 6) & (0xFFUL << 22);
mpe.regs[dest+2] = (pixelData32 << 14) & (0xFFUL << 22);
#endif
if(bChnorm)
{
mpe.regs[dest+1] = (mpe.regs[dest+1] - 0x20000000UL) & 0xFFC00000UL;
mpe.regs[dest+2] = (mpe.regs[dest+2] - 0x20000000UL) & 0xFFC00000UL;
}
return;
}
}
}
// leave __fastcall here!
void __fastcall _LoadPixelZAbsolute(MPE* const __restrict mpe, const void* const __restrict memPtr)
{
const uint32 control = mpe->ba_control;
uint32* const __restrict regs = mpe->regs+mpe->ba_reg_offset;
const uint32 pixType = BilinearInfo_XYType(control);
const bool bChnorm = BilinearInfo_XYChnorm(control);
switch(pixType)
{
case 0x0:
//MPEG
return;
case 0x1:
//4 bit
return;
case 0x2:
{
//16
const uint16 pixelData16 = SwapBytes(*((uint16*)memPtr));
regs[0] = ((uint32)pixelData16 << 14) & (0xFCUL << 22);
regs[1] = ((uint32)pixelData16 << 20) & (0xF8UL << 22);
regs[2] = ((uint32)pixelData16 << 25) & (0xF8UL << 22);
if(bChnorm)
{
regs[1] = (regs[1] - 0x20000000UL) & 0xFE000000UL;
regs[2] = (regs[2] - 0x20000000UL) & 0xFE000000UL;
}
return;
}
case 0x5:
{
//16+16Z
const uint32 pixelData32 = SwapBytes(*((uint32*)memPtr));
regs[0] = (pixelData32 >> 2) & (0xFCUL << 22);
regs[1] = (pixelData32 << 4) & (0xF8UL << 22);
regs[2] = (pixelData32 << 9) & (0xF8UL << 22);
regs[3] = (pixelData32 << 16);
if(bChnorm)
{
regs[1] = (regs[1] - 0x20000000UL) & 0xFE000000UL;
regs[2] = (regs[2] - 0x20000000UL) & 0xFE000000UL;
}
return;
}
case 0x3:
//8 bit
return;
case 0x4:
case 0x6:
{
//32 bit
const uint32 pixelData32 = ((uint32*)memPtr)[0];
#if 0 // SSSE3 check needed!
__m128i tmp0 = _mm_shuffle_epi8(_mm_cvtsi32_si128(pixelData32), _mm_set_epi8(-1,-1,-1,-1, 2,-1,-1,-1, 1,-1,-1,-1, 0,-1,-1,-1));
tmp0 = _mm_srli_epi32(tmp0, 2);
if (bChnorm)
{
tmp0 = _mm_sub_epi32(tmp0, _mm_set_epi32(0, 0x20000000UL, 0x20000000UL, 0));
tmp0 = _mm_and_si128(tmp0, _mm_set_epi32(0xFFFFFFFFUL, 0xFFC00000UL, 0xFFC00000UL, 0xFFFFFFFFUL));
}
_mm_store_si128((__m128i*)regs, tmp0); //!! is this always aligned?
#else
#ifdef LITTLE_ENDIAN
regs[0] = (pixelData32 << 22) & (0xFFUL << 22);
regs[1] = (pixelData32 << 14) & (0xFFUL << 22);
regs[2] = (pixelData32 << 6) & (0xFFUL << 22);
#else
regs[0] = (pixelData32 >> 2) & (0xFFUL << 22);
regs[1] = (pixelData32 << 6) & (0xFFUL << 22);
regs[2] = (pixelData32 << 14) & (0xFFUL << 22);
#endif
if(bChnorm)
{
regs[1] = (regs[1] - 0x20000000UL) & 0xFFC00000UL;
regs[2] = (regs[2] - 0x20000000UL) & 0xFFC00000UL;
}
#endif
if(pixType == 0x4)
#ifdef LITTLE_ENDIAN
regs[3] = (pixelData32 & 0xFF000000u);
#else
regs[3] = (pixelData32 << 24);
#endif
else //+32Z
regs[3] = SwapBytes(((uint32*)memPtr)[1]);
return;
}
}
}
void Execute_LoadPixelZAbsolute(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
void * const memPtr = (void *)((uint32 *)nuance.fields[FIELD_MEM_POINTER]);
const uint32 dest = nuance.fields[FIELD_MEM_TO];
uint32 pixType;
bool bChnorm;
if(nuance.fields[FIELD_MEM_INFO] & MEM_INFO_BILINEAR_UV)
{
pixType = BilinearInfo_XYType(pRegs[UVC_REG]);
bChnorm = BilinearInfo_XYChnorm(pRegs[UVC_REG]);
}
else if(nuance.fields[FIELD_MEM_INFO] & MEM_INFO_BILINEAR_XY)
{
pixType = BilinearInfo_XYType(pRegs[XYC_REG]);
bChnorm = BilinearInfo_XYChnorm(pRegs[XYC_REG]);
}
else
{
pixType = BilinearInfo_XYType(mpe.linpixctl);
bChnorm = BilinearInfo_XYChnorm(mpe.linpixctl);
}
switch(pixType)
{
case 0x0:
//MPEG
return;
case 0x1:
//4 bit
return;
case 0x2:
{
//16
const uint16 pixelData16 = SwapBytes(*((uint16*)memPtr));
mpe.regs[dest ] = ((uint32)pixelData16 << 14) & (0xFCUL << 22);
mpe.regs[dest+1] = ((uint32)pixelData16 << 20) & (0xF8UL << 22);
mpe.regs[dest+2] = ((uint32)pixelData16 << 25) & (0xF8UL << 22);
if(bChnorm)
{
mpe.regs[dest+1] = (mpe.regs[dest+1] - 0x20000000UL) & 0xFE000000UL;
mpe.regs[dest+2] = (mpe.regs[dest+2] - 0x20000000UL) & 0xFE000000UL;
}
return;
}
case 0x5:
{
//16+16Z
const uint32 pixelData32 = SwapBytes(*((uint32*)memPtr));
mpe.regs[dest ] = (pixelData32 >> 2) & (0xFCUL << 22);
mpe.regs[dest+1] = (pixelData32 << 4) & (0xF8UL << 22);
mpe.regs[dest+2] = (pixelData32 << 9) & (0xF8UL << 22);
mpe.regs[dest+3] = (pixelData32 << 16);
if(bChnorm)
{
mpe.regs[dest+1] = (mpe.regs[dest+1] - 0x20000000UL) & 0xFE000000UL;
mpe.regs[dest+2] = (mpe.regs[dest+2] - 0x20000000UL) & 0xFE000000UL;
}
return;
}
case 0x3:
//8 bit
return;
case 0x4:
case 0x6:
{
//32 bit
const uint32 pixelData32 = ((uint32*)memPtr)[0];
#ifdef LITTLE_ENDIAN
mpe.regs[dest ] = (pixelData32 << 22) & (0xFFUL << 22);
mpe.regs[dest+1] = (pixelData32 << 14) & (0xFFUL << 22);
mpe.regs[dest+2] = (pixelData32 << 6) & (0xFFUL << 22);
#else
mpe.regs[dest ] = (pixelData32 >> 2) & (0xFFUL << 22);
mpe.regs[dest+1] = (pixelData32 << 6) & (0xFFUL << 22);
mpe.regs[dest+2] = (pixelData32 << 14) & (0xFFUL << 22);
#endif
if(pixType == 0x4)
#ifdef LITTLE_ENDIAN
mpe.regs[dest+3] = (pixelData32 & 0xFF000000u);
#else
mpe.regs[dest+3] = (pixelData32 << 24);
#endif
else //+32Z
mpe.regs[dest+3] = SwapBytes(((uint32*)memPtr)[1]);
if(bChnorm)
{
mpe.regs[dest+1] = (mpe.regs[dest+1] - 0x20000000UL) & 0xFFC00000UL;
mpe.regs[dest+2] = (mpe.regs[dest+2] - 0x20000000UL) & 0xFFC00000UL;
}
return;
}
}
}
void Execute_LoadByteLinear(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
const uint32 address = pRegs[nuance.fields[FIELD_MEM_FROM]];
const uint32 data = *((uint8 *)(nuonEnv.GetPointerToMemory(mpe.mpeIndex,address)));
mpe.regs[nuance.fields[FIELD_MEM_TO]] = data << 24;
}
void Execute_LoadByteBilinearUV(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
uint32 address = CalculateBilinearAddress(mpe,pRegs[UVC_REG],pRegs[INDEX_REG+REG_U],pRegs[INDEX_REG+REG_V]);
address = (mpe.uvbase & 0xFFFFFFFC) + (address << BilinearInfo_PixelWidth(pixel_type_width,pRegs[UVC_REG]));
Nuance newNuance;
newNuance.fields[FIELD_MEM_HANDLER] = (size_t)Execute_LoadByteAbsolute;
newNuance.fields[FIELD_MEM_POINTER] = (size_t)nuonEnv.GetPointerToMemory(mpe.mpeIndex,address);
newNuance.fields[FIELD_MEM_FROM] = address;
newNuance.fields[FIELD_MEM_TO] = nuance.fields[FIELD_MEM_TO];
Execute_LoadByteAbsolute(mpe,pRegs,newNuance);
}
void Execute_LoadByteBilinearXY(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
uint32 address = CalculateBilinearAddress(mpe,pRegs[XYC_REG],pRegs[INDEX_REG+REG_X],pRegs[INDEX_REG+REG_Y]);
address = (mpe.xybase & 0xFFFFFFFC) + (address << BilinearInfo_PixelWidth(pixel_type_width,pRegs[XYC_REG]));
Nuance newNuance;
newNuance.fields[FIELD_MEM_HANDLER] = (size_t)Execute_LoadByteAbsolute;
newNuance.fields[FIELD_MEM_POINTER] = (size_t)nuonEnv.GetPointerToMemory(mpe.mpeIndex,address);
newNuance.fields[FIELD_MEM_FROM] = address;
newNuance.fields[FIELD_MEM_TO] = nuance.fields[FIELD_MEM_TO];
Execute_LoadByteAbsolute(mpe,pRegs,newNuance);
}
void Execute_LoadWordLinear(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
const uint32 dest = nuance.fields[FIELD_MEM_TO];
const uint32 address = pRegs[nuance.fields[FIELD_MEM_FROM]];
const uint8* const memPtr = (uint8 *)(nuonEnv.GetPointerToMemory(mpe.mpeIndex,address & 0xFFFFFFFE));
uint32 data = ((uint32)memPtr[0]) << 24;
data |= ((uint32)memPtr[1]) << 16;
mpe.regs[dest] = data;
}
void Execute_LoadWordBilinearUV(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
uint32 address = CalculateBilinearAddress(mpe,pRegs[UVC_REG],pRegs[INDEX_REG+REG_U],pRegs[INDEX_REG+REG_V]);
address = (mpe.uvbase & 0xFFFFFFFC) + (address << BilinearInfo_PixelWidth(pixel_type_width,pRegs[UVC_REG]));
Nuance newNuance;
newNuance.fields[FIELD_MEM_HANDLER] = (size_t)Execute_LoadWordAbsolute;
newNuance.fields[FIELD_MEM_POINTER] = (size_t)nuonEnv.GetPointerToMemory(mpe.mpeIndex,address);
newNuance.fields[FIELD_MEM_FROM] = address;
newNuance.fields[FIELD_MEM_TO] = nuance.fields[FIELD_MEM_TO];
Execute_LoadWordAbsolute(mpe,pRegs,newNuance);
}
void Execute_LoadWordBilinearXY(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
uint32 address = CalculateBilinearAddress(mpe,pRegs[XYC_REG],pRegs[INDEX_REG+REG_X],pRegs[INDEX_REG+REG_Y]);
address = (mpe.xybase & 0xFFFFFFFC) + (address << BilinearInfo_PixelWidth(pixel_type_width,pRegs[XYC_REG]));
Nuance newNuance;
newNuance.fields[FIELD_MEM_HANDLER] = (size_t)Execute_LoadWordAbsolute;
newNuance.fields[FIELD_MEM_POINTER] = (size_t)nuonEnv.GetPointerToMemory(mpe.mpeIndex,address);
newNuance.fields[FIELD_MEM_FROM] = address;
newNuance.fields[FIELD_MEM_TO] = nuance.fields[FIELD_MEM_TO];
Execute_LoadWordAbsolute(mpe,pRegs,newNuance);
}
void Execute_LoadScalarBilinearUV(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
uint32 address = CalculateBilinearAddress(mpe,pRegs[UVC_REG],pRegs[INDEX_REG+REG_U],pRegs[INDEX_REG+REG_V]);
address = (mpe.uvbase & 0xFFFFFFFC) + (address << BilinearInfo_PixelWidth(pixel_type_width,pRegs[UVC_REG]));
Nuance newNuance;
newNuance.fields[FIELD_MEM_HANDLER] = (size_t)Execute_LoadScalarAbsolute;
newNuance.fields[FIELD_MEM_POINTER] = (size_t)nuonEnv.GetPointerToMemory(mpe.mpeIndex,address);
newNuance.fields[FIELD_MEM_FROM] = address;
newNuance.fields[FIELD_MEM_TO] = nuance.fields[FIELD_MEM_TO];
Execute_LoadScalarAbsolute(mpe,pRegs,newNuance);
}
void Execute_LoadScalarBilinearXY(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
uint32 address = CalculateBilinearAddress(mpe,pRegs[XYC_REG],pRegs[INDEX_REG+REG_X],pRegs[INDEX_REG+REG_Y]);
address = (mpe.xybase & 0xFFFFFFFC) + (address << BilinearInfo_PixelWidth(pixel_type_width,pRegs[XYC_REG]));
Nuance newNuance;
newNuance.fields[FIELD_MEM_HANDLER] = (size_t)Execute_LoadScalarAbsolute;
newNuance.fields[FIELD_MEM_POINTER] = (size_t)nuonEnv.GetPointerToMemory(mpe.mpeIndex,address);
newNuance.fields[FIELD_MEM_FROM] = address;
newNuance.fields[FIELD_MEM_TO] = nuance.fields[FIELD_MEM_TO];
Execute_LoadScalarAbsolute(mpe,pRegs,newNuance);
}
void Execute_LoadShortVectorAbsolute(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
const uint32 dest = nuance.fields[FIELD_MEM_TO];
const uint8 * const ptr = (uint8 *)nuance.fields[FIELD_MEM_POINTER];
uint32 data[4] = {*((uint32 *)(ptr + 0)),*((uint32 *)(ptr + 2)),*((uint32 *)(ptr + 4)),*((uint32 *)(ptr + 6))};
SwapVectorBytes(data);
data[0] &= 0xFFFF0000;
data[1] &= 0xFFFF0000;
data[2] &= 0xFFFF0000;
data[3] &= 0xFFFF0000;
mpe.regs[dest ] = data[0];
mpe.regs[dest + 1] = data[1];
mpe.regs[dest + 2] = data[2];
mpe.regs[dest + 3] = data[3];
}
void Execute_LoadShortVectorLinear(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
const uint32 dest = nuance.fields[FIELD_MEM_TO];
const uint8 * const ptr = (uint8 *)(nuonEnv.GetPointerToMemory(mpe.mpeIndex,pRegs[nuance.fields[FIELD_MEM_FROM]] & 0xFFFFFFF8));
uint32 data[4] = {*((uint32 *)(ptr + 0)),*((uint32 *)(ptr + 2)),*((uint32 *)(ptr + 4)),*((uint32 *)(ptr + 6))};
SwapVectorBytes(data);
data[0] &= 0xFFFF0000;
data[1] &= 0xFFFF0000;
data[2] &= 0xFFFF0000;
data[3] &= 0xFFFF0000;
mpe.regs[dest ] = data[0];
mpe.regs[dest + 1] = data[1];
mpe.regs[dest + 2] = data[2];
mpe.regs[dest + 3] = data[3];
}
void Execute_LoadShortVectorBilinearUV(MPE &mpe, const uint32 pRegs[48], const Nuance &nuance)
{
uint32 address = CalculateBilinearAddress(mpe,pRegs[UVC_REG],pRegs[INDEX_REG+REG_U],pRegs[INDEX_REG+REG_V]);