-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfx.cpp
2992 lines (2636 loc) · 78 KB
/
gfx.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
#define PSP_GU_RENDER_MIN_UPDATED_LINES 4
/*
* Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
*
* (c) Copyright 1996 - 2001 Gary Henderson ([email protected]) and
* Jerremy Koot ([email protected])
*
* Super FX C emulator code
* (c) Copyright 1997 - 1999 Ivar ([email protected]) and
* Gary Henderson.
* Super FX assembler emulator code (c) Copyright 1998 zsKnight and _Demo_.
*
* DSP1 emulator code (c) Copyright 1998 Ivar, _Demo_ and Gary Henderson.
* C4 asm and some C emulation code (c) Copyright 2000 zsKnight and _Demo_.
* C4 C code (c) Copyright 2001 Gary Henderson ([email protected]).
*
* DOS port code contains the works of other authors. See headers in
* individual files.
*
* Snes9x homepage: http://www.snes9x.com
*
* Permission to use, copy, modify and distribute Snes9x 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.
*
* Snes9x is freeware for PERSONAL USE only. Commercial users should
* seek permission of the copyright holders first. Commercial use includes
* charging money for Snes9x or software derived from Snes9x.
*
* The copyright holders request that bug fixes and improvements to the code
* should be forwarded to them so everyone can benefit from the modifications
* in future versions.
*
* Super NES and Super Nintendo Entertainment System are trademarks of
* Nintendo Co., Limited and its subsidiary companies.
*/
#define _TRANSP_SUPPORT_
#define OLD_COLOUR_BLENDING
//#include <tapwave.h>
#include "snes9x.h"
#include "memmap.h"
#include "ppu.h"
#include "cpuexec.h"
#include "display.h"
#include "gfx.h"
#include "apu.h"
#include "cheats.h"
#include "tile.h"
extern unsigned char gammatab[10][32];
extern uint32 os9x_gammavalue;
extern int os9x_BG0,os9x_BG1,os9x_BG2,os9x_BG3,os9x_OBJ,os9x_easy;
extern int os9x_renderingpass,os9x_render;
/*extern "C" void asmClearScreenFast16(uint8 *scr,uint32 starty,uint32 endy,uint32 back);
extern "C" void asmClearScreenFast8(uint8 *scr,uint32 starty,uint32 endy,uint32 back);
extern "C" void asmClearBufferFast32_16(uint8 *buf,uint32 val,uint32 size);*/
void asmClearBufferFast32_16(uint8 *buf,uint32 val,uint32 size)
{
for (;size;size--)
*buf++=val;
}
void asmClearScreenFast16(uint8 *scr,uint32 starty,uint32 endy,uint32 back)
{
int sizey=endy-starty+1;
uint32 *p=(uint32*)(scr+starty*256*2);
for (int i=(sizey*256*2>>2);i;i--)
*p++=back;
}
void asmClearScreenFast8(uint8 *scr,uint32 starty,uint32 endy,uint32 back)
{
int sizey=endy-starty+1;
uint32 *p=(uint32*)(scr+starty*256);
for (int i=(sizey*256>>2);i;i--)
*p++=back;
}
extern int os9x_ShowSub;
extern int os9x_fastmode;
extern int os9x_fastsprite;
void (*ptrDrawTileNewSprite)(uint32 Tile, uint32 Offset, uint32 StartLine, uint32 LineCount, uint32 index_spr);
void (*ptrDrawClippedTileNewSprite)(uint32 Tile, uint32 Offset,uint32 StartPixel, uint32 Width,uint32 StartLine, uint32 LineCount, uint32 index_spr);
void (*ptrDrawTileNew)(uint32 Tile, uint32 Offset, uint32 StartLine, uint32 LineCount);
void (*ptrDrawClippedTileNew)(uint32 Tile, uint32 Offset,uint32 StartPixel, uint32 Width,uint32 StartLine, uint32 LineCount);
void (*ptrDrawHiResTileNew) (uint32 Tile, uint32 Offset, uint32 StartLine, uint32 LineCount);
void (*ptrDrawHiResClippedTileNew) (uint32 Tile, uint32 Offset,uint32 StartPixel, uint32 Width,uint32 StartLine, uint32 LineCount);
//#define asmPPU
#define M7 19
#define M8 19
void ComputeClipWindows ();
/*static void S9xDisplayFrameRate ();
static void S9xDisplayString (const char *string);*/
extern uint8 BitShifts[8][4];
extern uint8 TileShifts[8][4];
extern uint8 PaletteShifts[8][4];
extern uint8 PaletteMasks[8][4];
extern uint8 Depths[8][4];
extern uint8 BGSizes [2];
extern struct SBG BG;
extern uint8 Mode7Depths [2];
#define BLACK BUILD_PIXEL(0,0,0)
#define FIXCLIP(n)\
if (GFX.r212c & (1 << (n))) \
GFX.pCurrentClip = &IPPU.Clip [0]; \
else \
GFX.pCurrentClip = &IPPU.Clip [1];
#define DISPLAY(n)\
( !(PPU.BG_Forced & n) && \
( (GFX.r212c & n) || ( ( (GFX.r212d & n) && subadd) ) ) \
)
#define ON_MAIN(N) \
(GFX.r212c & (1 << (N)) && \
!(PPU.BG_Forced & (1 << (N))))
#define SUB_OR_ADD(N) \
(GFX.r2131 & (1 << (N)))
#define ON_SUB(N) \
((GFX.r2130 & 0x30) != 0x30 && \
(GFX.r2130 & 2) && \
(GFX.r212d & (1 << N)) && \
!(PPU.BG_Forced & (1 << (N))))
#define ADD_OR_SUB_ON_ANYTHING \
(GFX.r2131 & 0x3f)
#define ANYTHING_ON_SUB \
((GFX.r2130 & 0x30) != 0x30 && \
(GFX.r2130 & 2) && \
(GFX.r212d & 0x1f))
void os9x_SetTileRender(int32 transp)
{
if (!transp)
{
// if (os9x_softrendering)
{
if (os9x_fastsprite)
{
ptrDrawTileNewSprite=softDrawTile16NewFastSprite;
ptrDrawClippedTileNewSprite=softDrawClippedTile16NewFastSprite;
}
else
{
ptrDrawTileNewSprite=softDrawTile16NewSprite;
ptrDrawClippedTileNewSprite=softDrawClippedTile16NewSprite;
}
ptrDrawTileNew=softDrawTile16New;
ptrDrawClippedTileNew=softDrawClippedTile16New;
ptrDrawHiResTileNew=softDrawHiResTile16New;
ptrDrawHiResClippedTileNew=softDrawHiResClippedTile16New;
}
/* else
{
if (os9x_fastsprite)
{
ptrDrawTileNewSprite=DrawTile16NewFastSprite;
ptrDrawClippedTileNewSprite=DrawClippedTile16NewFastSprite;
}
else
{
ptrDrawTileNewSprite=DrawTile16NewSprite;
ptrDrawClippedTileNewSprite=DrawClippedTile16NewSprite;
}
ptrDrawTileNew=DrawTile16New;
ptrDrawClippedTileNew=DrawClippedTile16New;
ptrDrawHiResTileNew=DrawHiResTile16New;
ptrDrawHiResClippedTileNew=DrawHiResClippedTile16New;
}*/
}
else
{
// if (os9x_softrendering)
{
if (GFX.r2131 & 0x80)
{
if (!(GFX.r2131 & 0x40))
{
ptrDrawTileNewSprite=softDrawTile16SUBNewSprite;
ptrDrawClippedTileNewSprite=softDrawClippedTile16SUBNewSprite;
ptrDrawTileNew=softDrawTile16SUBNew;
ptrDrawClippedTileNew=softDrawClippedTile16SUBNew;
ptrDrawHiResTileNew=softDrawHiResTile16SUBNew;
ptrDrawHiResClippedTileNew=softDrawHiResClippedTile16SUBNew;
}
else
{
ptrDrawTileNewSprite=softDrawTile16SUB1_2NewSprite;
ptrDrawClippedTileNewSprite=softDrawClippedTile16SUB1_2NewSprite;
ptrDrawTileNew=softDrawTile16SUB1_2New;
ptrDrawClippedTileNew=softDrawClippedTile16SUB1_2New;
ptrDrawHiResTileNew=softDrawHiResTile16SUB1_2New;
ptrDrawHiResClippedTileNew=softDrawHiResClippedTile16SUB1_2New;
}
}
else
{
if (!(GFX.r2131 & 0x40))
{
ptrDrawTileNewSprite=softDrawTile16ADDNewSprite;
ptrDrawClippedTileNewSprite=softDrawClippedTile16ADDNewSprite;
ptrDrawTileNew=softDrawTile16ADDNew;
ptrDrawClippedTileNew=softDrawClippedTile16ADDNew;
ptrDrawHiResTileNew=softDrawHiResTile16ADDNew;
ptrDrawHiResClippedTileNew=softDrawHiResClippedTile16ADDNew;
}
else
{
ptrDrawTileNewSprite=softDrawTile16ADD1_2NewSprite;
ptrDrawClippedTileNewSprite=softDrawClippedTile16ADD1_2NewSprite;
ptrDrawTileNew=softDrawTile16ADD1_2New;
ptrDrawClippedTileNew=softDrawClippedTile16ADD1_2New;
ptrDrawHiResTileNew=softDrawHiResTile16ADD1_2New;
ptrDrawHiResClippedTileNew=softDrawHiResClippedTile16ADD1_2New;
}
}
}
/* else
{
}*/
}
}
bool8 S9xGraphicsInit ()
{
register uint32 PixelOdd = 1;
register uint32 PixelEven = 2;
#ifdef GFX_MULTI_FORMAT
if (GFX.BuildPixel == NULL)
S9xSetRenderPixelFormat (RGB565);
#endif
for (uint8 bitshift = 0; bitshift < 4; bitshift++)
{
for (register char i = 0; i < 16; i++)
{
register uint32 h = 0;
register uint32 l = 0;
#if defined(LSB_FIRST)
if (i & 8)
h |= PixelOdd;
if (i & 4)
h |= PixelOdd << 8;
if (i & 2)
h |= PixelOdd << 16;
if (i & 1)
h |= PixelOdd << 24;
if (i & 8)
l |= PixelOdd;
if (i & 4)
l |= PixelOdd << 8;
if (i & 2)
l |= PixelOdd << 16;
if (i & 1)
l |= PixelOdd << 24;
#else
if (i & 8)
h |= (PixelOdd << 24);
if (i & 4)
h |= (PixelOdd << 16);
if (i & 2)
h |= (PixelOdd << 8);
if (i & 1)
h |= PixelOdd;
if (i & 8)
l |= (PixelOdd << 24);
if (i & 4)
l |= (PixelOdd << 16);
if (i & 2)
l |= (PixelOdd << 8);
if (i & 1)
l |= PixelOdd;
#endif
odd_high[bitshift][i] = h;
odd_low[bitshift][i] = l;
h = l = 0;
#if defined(LSB_FIRST)
if (i & 8)
h |= PixelEven;
if (i & 4)
h |= PixelEven << 8;
if (i & 2)
h |= PixelEven << 16;
if (i & 1)
h |= PixelEven << 24;
if (i & 8)
l |= PixelEven;
if (i & 4)
l |= PixelEven << 8;
if (i & 2)
l |= PixelEven << 16;
if (i & 1)
l |= PixelEven << 24;
#else
if (i & 8)
h |= (PixelEven << 24);
if (i & 4)
h |= (PixelEven << 16);
if (i & 2)
h |= (PixelEven << 8);
if (i & 1)
h |= PixelEven;
if (i & 8)
l |= (PixelEven << 24);
if (i & 4)
l |= (PixelEven << 16);
if (i & 2)
l |= (PixelEven << 8);
if (i & 1)
l |= PixelEven;
#endif
even_high[bitshift][i] = h;
even_low[bitshift][i] = l;
}
PixelEven <<= 2;
PixelOdd <<= 2;
}
GFX.RealPitch = GFX.Pitch2 = GFX.Pitch;
GFX.ZPitch = GFX.Pitch;
if (Settings.SixteenBit)
GFX.ZPitch >>= 1;
GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;
GFX.DepthDelta = GFX.SubZBuffer - GFX.ZBuffer;
//GFX.InfoStringTimeout = 0;
//GFX.InfoString = NULL;
PPU.BG_Forced = 0;
IPPU.OBJChanged = TRUE;
if (Settings.Transparency)
Settings.SixteenBit = TRUE;
IPPU.DirectColourMapsNeedRebuild = TRUE;
GFX.PixSize = 1;
if (Settings.SixteenBit)
{
GFX.PPL = GFX.Pitch >> 1;
GFX.PPLx2 = GFX.Pitch;
}
else
{
GFX.PPL = GFX.Pitch;
GFX.PPLx2 = GFX.Pitch * 2;
}
S9xFixColourBrightness ();
#ifdef _TRANSP_SUPPORT_
if (!(GFX.X2 = (uint16 *) malloc (sizeof (uint16) * 0x10000)))
return (FALSE);
if ((!(GFX.ZERO_OR_X2 = (uint16 *) malloc (sizeof (uint16) * 0x10000)))
||!(GFX.ZERO = (uint16 *) malloc (sizeof (uint16) * 0x10000)))
{
if (GFX.ZERO_OR_X2)
{
free ((char *) GFX.ZERO_OR_X2);
GFX.ZERO_OR_X2 = NULL;
}
if (GFX.X2)
{
free ((char *) GFX.X2);
GFX.X2 = NULL;
}
return (FALSE);
}
uint32 r, g, b;
// Build a lookup table that multiplies a packed RGB value by 2 with
// saturation.
for (r = 0; r <= MAX_RED; r++)
{
uint32 r2 = r << 1;
if (r2 > MAX_RED)
r2 = MAX_RED;
for (g = 0; g <= MAX_GREEN; g++)
{
uint32 g2 = g << 1;
if (g2 > MAX_GREEN)
g2 = MAX_GREEN;
for (b = 0; b <= MAX_BLUE; b++)
{
uint32 b2 = b << 1;
if (b2 > MAX_BLUE)
b2 = MAX_BLUE;
GFX.X2 [BUILD_PIXEL2 (r, g, b)] = BUILD_PIXEL2 (r2, g2, b2);
GFX.X2 [BUILD_PIXEL2 (r, g, b) & ~ALPHA_BITS_MASK] = BUILD_PIXEL2 (r2, g2, b2);
}
}
}
ZeroMemory (GFX.ZERO, 0x10000 * sizeof (uint16));
ZeroMemory (GFX.ZERO_OR_X2, 0x10000 * sizeof (uint16));
// Build a lookup table that if the top bit of the color value is zero
// then the value is zero, otherwise multiply the value by 2. Used by
// the color subtraction code.
#if defined(OLD_COLOUR_BLENDING)
for (r = 0; r <= MAX_RED; r++)
{
uint32 r2 = r;
if ((r2 & 0x10) == 0)
r2 = 0;
else
r2 = (r2 << 1) & MAX_RED;
for (g = 0; g <= MAX_GREEN; g++)
{
uint32 g2 = g;
if ((g2 & GREEN_HI_BIT) == 0)
g2 = 0;
else
g2 = (g2 << 1) & MAX_GREEN;
for (b = 0; b <= MAX_BLUE; b++)
{
uint32 b2 = b;
if ((b2 & 0x10) == 0)
b2 = 0;
else
b2 = (b2 << 1) & MAX_BLUE;
GFX.ZERO_OR_X2 [BUILD_PIXEL2 (r, g, b)] = BUILD_PIXEL2 (r2, g2, b2);
GFX.ZERO_OR_X2 [BUILD_PIXEL2 (r, g, b) & ~ALPHA_BITS_MASK] = BUILD_PIXEL2 (r2, g2, b2);
}
}
}
#else
for (r = 0; r <= MAX_RED; r++)
{
uint32 r2 = r;
if ((r2 & 0x10) == 0)
r2 = 0;
else
r2 = (r2 << 1) & MAX_RED;
if (r2 == 0)
r2 = 1;
for (g = 0; g <= MAX_GREEN; g++)
{
uint32 g2 = g;
if ((g2 & GREEN_HI_BIT) == 0)
g2 = 0;
else
g2 = (g2 << 1) & MAX_GREEN;
if (g2 == 0)
g2 = 1;
for (b = 0; b <= MAX_BLUE; b++)
{
uint32 b2 = b;
if ((b2 & 0x10) == 0)
b2 = 0;
else
b2 = (b2 << 1) & MAX_BLUE;
if (b2 == 0)
b2 = 1;
GFX.ZERO_OR_X2 [BUILD_PIXEL2 (r, g, b)] = BUILD_PIXEL2 (r2, g2, b2);
GFX.ZERO_OR_X2 [BUILD_PIXEL2 (r, g, b) & ~ALPHA_BITS_MASK] = BUILD_PIXEL2 (r2, g2, b2);
}
}
}
#endif
// Build a lookup table that if the top bit of the color value is zero
// then the value is zero, otherwise its just the value.
for (r = 0; r <= MAX_RED; r++)
{
uint32 r2 = r;
if ((r2 & 0x10) == 0)
r2 = 0;
else
r2 &= ~0x10;
for (g = 0; g <= MAX_GREEN; g++)
{
uint32 g2 = g;
if ((g2 & GREEN_HI_BIT) == 0)
g2 = 0;
else
g2 &= ~GREEN_HI_BIT;
for (b = 0; b <= MAX_BLUE; b++)
{
uint32 b2 = b;
if ((b2 & 0x10) == 0)
b2 = 0;
else
b2 &= ~0x10;
GFX.ZERO [BUILD_PIXEL2 (r, g, b)] = BUILD_PIXEL2 (r2, g2, b2);
GFX.ZERO [BUILD_PIXEL2 (r, g, b) & ~ALPHA_BITS_MASK] = BUILD_PIXEL2 (r2, g2, b2);
}
}
}
// GFX.ZERO = NULL;
#else
{
GFX.X2 = NULL;
GFX.ZERO_OR_X2 = NULL;
GFX.ZERO = NULL;
}
#endif
return (TRUE);
}
void S9xGraphicsDeinit (void)
{
// Free any memory allocated in S9xGraphicsInit
if (GFX.X2)
{
free (GFX.X2);
GFX.X2 = NULL;
}
if (GFX.ZERO_OR_X2)
{
free (GFX.ZERO_OR_X2);
GFX.ZERO_OR_X2 = NULL;
}
if (GFX.ZERO)
{
free (GFX.ZERO);
GFX.ZERO = NULL;
}
}
void S9xBuildDirectColourMaps ()
{
uint8 *cgamma=(uint8*)gammatab[os9x_gammavalue];
for (uint32 p = 0; p < 8; p++)
{
for (uint32 c = 0; c < 256; c++)
{
// XXX: Brightness
DirectColourMaps [p][c] = BUILD_PIXEL (cgamma[((c & 7) << 2) | ((p & 1) << 1)],
cgamma[((c & 0x38) >> 1) | (p & 2)],
cgamma[((c & 0xc0) >> 3) | (p & 4)]);
}
}
IPPU.DirectColourMapsNeedRebuild = FALSE;
}
void S9xStartScreenRefresh ()
{
if (GFX.InfoStringTimeout > 0 && --GFX.InfoStringTimeout == 0)
GFX.InfoString = NULL;
if (IPPU.RenderThisFrame)
{
if (!S9xInitUpdate ())
{
IPPU.RenderThisFrame = FALSE;
return;
}
IPPU.RenderedFramesCount++;
IPPU.PreviousLine = IPPU.CurrentLine = 0;
IPPU.MaxBrightness = PPU.Brightness;
IPPU.LatchedBlanking = PPU.ForcedBlanking;
IPPU.LatchedInterlace = (FillRAM[0x2133] & 1);
// IPPU.Interlace = (FillRAM[0x2133] & 1);
if (Settings.SupportHiRes && (PPU.BGMode == 5 || PPU.BGMode == 6 ||
IPPU.LatchedInterlace/*IPPU.Interlace*/))
{
if (PPU.BGMode == 5 || PPU.BGMode == 6)
{
IPPU.RenderedScreenWidth = 512;
IPPU.DoubleWidthPixels = TRUE;
}
if (/*IPPU.Interlace*/IPPU.LatchedInterlace)
{
IPPU.RenderedScreenHeight = PPU.ScreenHeight << 1;
GFX.Pitch2 = GFX.RealPitch;
GFX.Pitch = GFX.RealPitch * 2;
if (Settings.SixteenBit)
GFX.PPL = GFX.PPLx2 = GFX.RealPitch;
else
GFX.PPL = GFX.PPLx2 = GFX.RealPitch << 1;
}
else
{
IPPU.RenderedScreenHeight = PPU.ScreenHeight;
GFX.Pitch2 = GFX.Pitch = GFX.RealPitch;
if (Settings.SixteenBit)
GFX.PPL = GFX.Pitch >> 1;
else
GFX.PPL = GFX.Pitch;
GFX.PPLx2 = GFX.PPL << 1;
}
#if defined(USE_GLIDE) || defined(USE_OPENGL)
GFX.ZPitch = GFX.RealPitch;
if (Settings.SixteenBit)
GFX.ZPitch >>= 1;
#endif
}
else
{
IPPU.RenderedScreenWidth = 256;
IPPU.RenderedScreenHeight = PPU.ScreenHeight;
IPPU.DoubleWidthPixels = FALSE;
{
GFX.Pitch2 = GFX.Pitch = GFX.RealPitch;
GFX.PPL = GFX.PPLx2 >> 1;
GFX.ZPitch = GFX.RealPitch;
if (Settings.SixteenBit)
GFX.ZPitch >>= 1;
}
}
PPU.RecomputeClipWindows = TRUE;
GFX.DepthDelta = GFX.SubZBuffer - GFX.ZBuffer;
GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;
}
if (++IPPU.FrameCount % Memory.ROMFramesPerSecond == 0)
{
IPPU.DisplayedRenderedFrameCount = IPPU.RenderedFramesCount;
IPPU.RenderedFramesCount = 0;
IPPU.FrameCount = 0;
}
}
extern void DrawBGMode7BackgroundNew (uint8 *Screen, int bg, int pitch);
extern void DrawBGMode7BackgroundPrioNew (uint8 *Screen, int bg,int prio,int pitch);
extern void DrawBGMode7Background16New (uint16 *Screen, int bg,int pitch);
extern void DrawBGMode7Background16PrioNew (uint16 *Screen, int bg, int prio,int pitch);
/*void RenderLine (uint8 C)
{
if (IPPU.RenderThisFrame)
{
LineData[C].BG[0].VOffset = PPU.BG[0].VOffset + 1;
LineData[C].BG[0].HOffset = PPU.BG[0].HOffset;
LineData[C].BG[1].VOffset = PPU.BG[1].VOffset + 1;
LineData[C].BG[1].HOffset = PPU.BG[1].HOffset;
if (PPU.BGMode == 7)
{
struct SLineMatrixData *p = &LineMatrixData [C];
p->MatrixA = PPU.MatrixA;
p->MatrixB = PPU.MatrixB;
p->MatrixC = PPU.MatrixC;
p->MatrixD = PPU.MatrixD;
p->CentreX = PPU.CentreX;
p->CentreY = PPU.CentreY;
}
else
{
if (Settings.StarfoxHack && PPU.BG[2].VOffset == 0 &&
PPU.BG[2].HOffset == 0xe000)
{
LineData[C].BG[2].VOffset = 0xe1;
LineData[C].BG[2].HOffset = 0;
}
else
{
LineData[C].BG[2].VOffset = PPU.BG[2].VOffset + 1;
LineData[C].BG[2].HOffset = PPU.BG[2].HOffset;
LineData[C].BG[3].VOffset = PPU.BG[3].VOffset + 1;
LineData[C].BG[3].HOffset = PPU.BG[3].HOffset;
}
}
IPPU.CurrentLine = C + 1;
}
}*/
void S9xEndScreenRefresh ()
{
IPPU.HDMAStarted = FALSE;
if (IPPU.RenderThisFrame)
{
// INFO_FLUSH_REDRAW("endrefresh");
FLUSH_REDRAW ();
if (IPPU.ColorsChanged)
{
uint32 saved = PPU.CGDATA[0];
if (!Settings.SixteenBit)
{
// Hack for Super Mario World - to get its sky blue
// (It uses Fixed colour addition on the backdrop colour)
#ifdef SMW_HACK
if (!(FillRAM [0x2131] & 0x80) &&
(FillRAM[0x2131] & 0x20) &&
(PPU.FixedColourRed || PPU.FixedColourGreen ||
PPU.FixedColourBlue))
{
PPU.CGDATA[0] = PPU.FixedColourRed||(PPU.FixedColourGreen<<5)||(PPU.FixedColourBlue<<10);
}
#endif
}
IPPU.ColorsChanged = FALSE;
S9xSetPalette ();
PPU.CGDATA[0] = saved;
}
GFX.Pitch = GFX.Pitch2 = GFX.RealPitch;
GFX.PPL = GFX.PPLx2 >> 1;
/* if (Settings.DisplayFrameRate)
S9xDisplayFrameRate ();
if (GFX.InfoString)
S9xDisplayString (GFX.InfoString);*/
S9xDeinitUpdate (IPPU.RenderedScreenWidth, IPPU.RenderedScreenHeight,
Settings.SixteenBit);
}
// S9xApplyCheats ();
#ifdef DEBUGGER
if (CPU.Flags & FRAME_ADVANCE_FLAG)
{
if (ICPU.FrameAdvanceCount)
{
ICPU.FrameAdvanceCount--;
IPPU.RenderThisFrame = TRUE;
IPPU.FrameSkip = 0;
}
else
{
CPU.Flags &= ~FRAME_ADVANCE_FLAG;
CPU.Flags |= DEBUG_MODE_FLAG;
}
}
#endif
if (CPU.SRAMModified) {
if (!CPU.AutoSaveTimer) {
if (!(CPU.AutoSaveTimer = Settings.AutoSaveDelay * Memory.ROMFramesPerSecond)) CPU.SRAMModified = FALSE;
} else {
if (!--CPU.AutoSaveTimer) {
S9xAutoSaveSRAM ();
CPU.SRAMModified = FALSE;
}
}
}
}
void S9xSetInfoString (const char *string)
{
GFX.InfoString = string;
GFX.InfoStringTimeout = 120;
}
void S9xSetupOBJ ()
{
int SmallSize;
int LargeSize;
switch (PPU.OBJSizeSelect)
{
case 0:
SmallSize = 8;
LargeSize = 16;
break;
case 1:
SmallSize = 8;
LargeSize = 32;
break;
case 2:
SmallSize = 8;
LargeSize = 64;
break;
case 3:
SmallSize = 16;
LargeSize = 32;
break;
case 4:
SmallSize = 16;
LargeSize = 64;
break;
case 5:
default:
SmallSize = 32;
LargeSize = 64;
break;
}
int C = 0;
if (os9x_fastsprite)
{
int FirstSprite = PPU.FirstSprite & 0x7f;
/*yo*/
int LSPR = (FirstSprite-1)&0x7F;
int S = LSPR;
/**/
do
{
int Size;
if (PPU.OBJ [S].Size)
Size = LargeSize;
else
Size = SmallSize;
long VPos = PPU.OBJ [S].VPos;
if (VPos >= PPU.ScreenHeight)
VPos -= 256;
if (PPU.OBJ [S].HPos < 256 && PPU.OBJ [S].HPos > -Size &&
VPos < PPU.ScreenHeight && VPos > -Size)
{
GFX.OBJList [C++] = S;
GFX.Sizes[S] = Size;
GFX.VPositions[S] = VPos;
}
S = (S - 1) & 0x7f;
} while (S != LSPR);
}
else
{
int FirstSprite = PPU.FirstSprite & 0x7f;
int S = FirstSprite;
do
{
int Size;
if (PPU.OBJ [S].Size)
Size = LargeSize;
else
Size = SmallSize;
long VPos = PPU.OBJ [S].VPos;
if (VPos >= PPU.ScreenHeight)
VPos -= 256;
if (PPU.OBJ [S].HPos < 256 && PPU.OBJ [S].HPos > -Size &&
VPos < PPU.ScreenHeight && VPos > -Size)
{
GFX.OBJList [C++] = S;
GFX.Sizes[S] = Size;
GFX.VPositions[S] = VPos;
}
S = (S + 1) & 0x7f;
} while (S != FirstSprite);
}
// Terminate the list
GFX.OBJList [C] = -1;
IPPU.OBJChanged = FALSE;
}
void DrawOBJSNew (int32 prio,int32 OnMain=FALSE)
{
uint32 OGP32;
uint32 BaseTile, Tile;
CHECK_SOUND();
BG.BitShift = 4;
BG.TileShift = 5;
BG.TileAddress = PPU.OBJNameBase;
BG.StartPalette = 128;
BG.PaletteShift = 4;
BG.PaletteMask = 7;
// if (os9x_softrendering)
{
BG.Buffer = IPPU.TileCache [TILE_4BIT];
BG.BufferRam = IPPU.TileCache8 [TILE_4BIT];
}
/* else
{
BG.Buffer = os9x_tileCachesPtr[TILE_4BIT];//IPPU.TileCache [TILE_4BIT];
BG.BufferRam = IPPU.TileCache [TILE_4BIT];
os9x_curTileCache = os9x_tileCaches[TILE_4BIT];
}*/
BG.Buffered = IPPU.TileCached [TILE_4BIT];
BG.NameSelect = PPU.OBJNameSelect;
BG.DirectColourMode = FALSE;
/* if (GFX.OBJList[0]<0) return;
int I = 0;
while (GFX.OBJList[++I]>=0);
for (int S = GFX.OBJList [--I]; I >= 0; S = GFX.OBJList [--I])*/
int I = 0;
for (int S = GFX.OBJList [I++]; S >= 0; S = GFX.OBJList [I++])
{
int VPos = GFX.VPositions [S];
int Size = GFX.Sizes[S];
int TileInc = 1;
int Offset;
if ((PPU.OBJ[S].Priority!=prio)&&(prio!=-1))
continue;
if (VPos + Size <= (int) GFX.StartY || VPos > (int) GFX.EndY)
continue;
if (OnMain && SUB_OR_ADD(4))
{
os9x_SetTileRender( PPU.OBJ [S].Palette >= 4);
}
BaseTile = PPU.OBJ[S].Name | (PPU.OBJ[S].Palette << 10);
if (PPU.OBJ[S].HFlip)
{
BaseTile += ((Size >> 3) - 1) | H_FLIP;
TileInc = -1;
}
if (PPU.OBJ[S].VFlip)
BaseTile |= V_FLIP;
int clipcount = GFX.pCurrentClip->Count [4];
if (!clipcount)
clipcount = 1;
for (int clip = 0; clip < clipcount; clip++)
{
int Left;
int Right;
if (!GFX.pCurrentClip->Count [4])
{
Left = 0;
Right = 256;
}
else
{
Left = GFX.pCurrentClip->Left [clip][4];
Right = GFX.pCurrentClip->Right [clip][4];
}
if (Right <= Left || PPU.OBJ[S].HPos + Size <= Left ||
PPU.OBJ[S].HPos >= Right)
continue;
for (int Y = 0; Y < Size; Y += 8)
{
if (VPos + Y + 7 >= (int) GFX.StartY && VPos + Y <= (int) GFX.EndY)
{
int StartLine;
int TileLine;
int LineCount;
int Last;
if ((StartLine = VPos + Y) < (int) GFX.StartY)
{
StartLine = GFX.StartY - StartLine;
LineCount = 8 - StartLine;
}
else
{
StartLine = 0;
LineCount = 8;
}
if ((Last = VPos + Y + 7 - GFX.EndY) > 0)
if ((LineCount -= Last) <= 0)
break;
TileLine = StartLine;// << 3;
OGP32 = (VPos + Y + StartLine)*256;
if (!PPU.OBJ[S].VFlip)
Tile = BaseTile + (Y << 1);
else
Tile = BaseTile + ((Size - Y - 8) << 1);
int Middle = Size >> 3;
if (PPU.OBJ[S].HPos < Left)
{
Tile += ((Left - PPU.OBJ[S].HPos) >> 3) * TileInc;