forked from snes9xgit/snes9x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfx.cpp
2398 lines (2009 loc) · 61 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
/***********************************************************************************
Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
(c) Copyright 1996 - 2002 Gary Henderson ([email protected]),
Jerremy Koot ([email protected])
(c) Copyright 2002 - 2004 Matthew Kendora
(c) Copyright 2002 - 2005 Peter Bortas ([email protected])
(c) Copyright 2004 - 2005 Joel Yliluoma (http://iki.fi/bisqwit/)
(c) Copyright 2001 - 2006 John Weidman ([email protected])
(c) Copyright 2002 - 2006 funkyass ([email protected]),
Kris Bleakley ([email protected])
(c) Copyright 2002 - 2010 Brad Jorsch ([email protected]),
Nach ([email protected]),
(c) Copyright 2002 - 2011 zones ([email protected])
(c) Copyright 2006 - 2007 nitsuja
(c) Copyright 2009 - 2011 BearOso,
OV2
BS-X C emulator code
(c) Copyright 2005 - 2006 Dreamer Nom,
zones
C4 x86 assembler and some C emulation code
(c) Copyright 2000 - 2003 _Demo_ ([email protected]),
Nach,
zsKnight ([email protected])
C4 C++ code
(c) Copyright 2003 - 2006 Brad Jorsch,
Nach
DSP-1 emulator code
(c) Copyright 1998 - 2006 _Demo_,
Andreas Naive ([email protected]),
Gary Henderson,
Ivar ([email protected]),
John Weidman,
Kris Bleakley,
Matthew Kendora,
Nach,
neviksti ([email protected])
DSP-2 emulator code
(c) Copyright 2003 John Weidman,
Kris Bleakley,
Lord Nightmare ([email protected]),
Matthew Kendora,
neviksti
DSP-3 emulator code
(c) Copyright 2003 - 2006 John Weidman,
Kris Bleakley,
Lancer,
z80 gaiden
DSP-4 emulator code
(c) Copyright 2004 - 2006 Dreamer Nom,
John Weidman,
Kris Bleakley,
Nach,
z80 gaiden
OBC1 emulator code
(c) Copyright 2001 - 2004 zsKnight,
pagefault ([email protected]),
Kris Bleakley
Ported from x86 assembler to C by sanmaiwashi
SPC7110 and RTC C++ emulator code used in 1.39-1.51
(c) Copyright 2002 Matthew Kendora with research by
zsKnight,
John Weidman,
Dark Force
SPC7110 and RTC C++ emulator code used in 1.52+
(c) Copyright 2009 byuu,
neviksti
S-DD1 C emulator code
(c) Copyright 2003 Brad Jorsch with research by
Andreas Naive,
John Weidman
S-RTC C emulator code
(c) Copyright 2001 - 2006 byuu,
John Weidman
ST010 C++ emulator code
(c) Copyright 2003 Feather,
John Weidman,
Kris Bleakley,
Matthew Kendora
Super FX x86 assembler emulator code
(c) Copyright 1998 - 2003 _Demo_,
pagefault,
zsKnight
Super FX C emulator code
(c) Copyright 1997 - 1999 Ivar,
Gary Henderson,
John Weidman
Sound emulator code used in 1.5-1.51
(c) Copyright 1998 - 2003 Brad Martin
(c) Copyright 1998 - 2006 Charles Bilyue'
Sound emulator code used in 1.52+
(c) Copyright 2004 - 2007 Shay Green ([email protected])
SH assembler code partly based on x86 assembler code
(c) Copyright 2002 - 2004 Marcus Comstedt ([email protected])
2xSaI filter
(c) Copyright 1999 - 2001 Derek Liauw Kie Fa
HQ2x, HQ3x, HQ4x filters
(c) Copyright 2003 Maxim Stepin ([email protected])
NTSC filter
(c) Copyright 2006 - 2007 Shay Green
GTK+ GUI code
(c) Copyright 2004 - 2011 BearOso
Win32 GUI code
(c) Copyright 2003 - 2006 blip,
funkyass,
Matthew Kendora,
Nach,
nitsuja
(c) Copyright 2009 - 2011 OV2
Mac OS GUI code
(c) Copyright 1998 - 2001 John Stiles
(c) Copyright 2001 - 2011 zones
Specific ports contains the works of other authors. See headers in
individual files.
Snes9x homepage: http://www.snes9x.com/
Permission to use, copy, modify and/or 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 or it's derivatives.
Snes9x is freeware for PERSONAL USE only. Commercial users should
seek permission of the copyright holders first. Commercial use includes,
but is not limited to, charging money for Snes9x or software derived from
Snes9x, including Snes9x or derivatives in commercial game bundles, and/or
using Snes9x as a promotion for your commercial product.
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.
***********************************************************************************/
#include "snes9x.h"
#include "ppu.h"
#include "tile.h"
#include "controls.h"
#include "crosshairs.h"
#include "cheats.h"
#include "movie.h"
#include "screenshot.h"
#include "font.h"
#include "display.h"
extern struct SCheatData Cheat;
extern struct SLineData LineData[240];
extern struct SLineMatrixData LineMatrixData[240];
void S9xComputeClipWindows (void);
static int font_width = 8, font_height = 9;
static void SetupOBJ (void);
static void DrawOBJS (int);
static void DisplayFrameRate (void);
static void DisplayPressedKeys (void);
static void DisplayWatchedAddresses (void);
static void DisplayStringFromBottom (const char *, int, int, bool);
static void DrawBackground (int, uint8, uint8);
static void DrawBackgroundMosaic (int, uint8, uint8);
static void DrawBackgroundOffset (int, uint8, uint8, int);
static void DrawBackgroundOffsetMosaic (int, uint8, uint8, int);
static inline void DrawBackgroundMode7 (int, void (*DrawMath) (uint32, uint32, int), void (*DrawNomath) (uint32, uint32, int), int);
static inline void DrawBackdrop (void);
static inline void RenderScreen (bool8);
static uint16 get_crosshair_color (uint8);
#define TILE_PLUS(t, x) (((t) & 0xfc00) | ((t + x) & 0x3ff))
bool8 S9xGraphicsInit (void)
{
S9xInitTileRenderer();
memset(BlackColourMap, 0, 256 * sizeof(uint16));
#ifdef GFX_MULTI_FORMAT
if (GFX.BuildPixel == NULL)
S9xSetRenderPixelFormat(RGB565);
#endif
GFX.DoInterlace = 0;
GFX.InterlaceFrame = 0;
GFX.RealPPL = GFX.Pitch >> 1;
IPPU.OBJChanged = TRUE;
IPPU.DirectColourMapsNeedRebuild = TRUE;
Settings.BG_Forced = 0;
S9xFixColourBrightness();
GFX.X2 = (uint16 *) malloc(sizeof(uint16) * 0x10000);
GFX.ZERO = (uint16 *) malloc(sizeof(uint16) * 0x10000);
GFX.ScreenSize = GFX.Pitch / 2 * SNES_HEIGHT_EXTENDED * (Settings.SupportHiRes ? 2 : 1);
GFX.SubScreen = (uint16 *) malloc(GFX.ScreenSize * sizeof(uint16));
GFX.ZBuffer = (uint8 *) malloc(GFX.ScreenSize);
GFX.SubZBuffer = (uint8 *) malloc(GFX.ScreenSize);
if (!GFX.X2 || !GFX.ZERO || !GFX.SubScreen || !GFX.ZBuffer || !GFX.SubZBuffer)
{
S9xGraphicsDeinit();
return (FALSE);
}
// Lookup table for color addition
memset(GFX.X2, 0, 0x10000 * sizeof(uint16));
for (uint32 r = 0; r <= MAX_RED; r++)
{
uint32 r2 = r << 1;
if (r2 > MAX_RED)
r2 = MAX_RED;
for (uint32 g = 0; g <= MAX_GREEN; g++)
{
uint32 g2 = g << 1;
if (g2 > MAX_GREEN)
g2 = MAX_GREEN;
for (uint32 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);
}
}
}
// Lookup table for 1/2 color subtraction
memset(GFX.ZERO, 0, 0x10000 * sizeof(uint16));
for (uint32 r = 0; r <= MAX_RED; r++)
{
uint32 r2 = r;
if (r2 & 0x10)
r2 &= ~0x10;
else
r2 = 0;
for (uint32 g = 0; g <= MAX_GREEN; g++)
{
uint32 g2 = g;
if (g2 & GREEN_HI_BIT)
g2 &= ~GREEN_HI_BIT;
else
g2 = 0;
for (uint32 b = 0; b <= MAX_BLUE; b++)
{
uint32 b2 = b;
if (b2 & 0x10)
b2 &= ~0x10;
else
b2 = 0;
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);
}
}
}
return (TRUE);
}
void S9xGraphicsDeinit (void)
{
if (GFX.X2) { free(GFX.X2); GFX.X2 = NULL; }
if (GFX.ZERO) { free(GFX.ZERO); GFX.ZERO = NULL; }
if (GFX.SubScreen) { free(GFX.SubScreen); GFX.SubScreen = NULL; }
if (GFX.ZBuffer) { free(GFX.ZBuffer); GFX.ZBuffer = NULL; }
if (GFX.SubZBuffer) { free(GFX.SubZBuffer); GFX.SubZBuffer = NULL; }
}
void S9xBuildDirectColourMaps (void)
{
IPPU.XB = mul_brightness[PPU.Brightness];
for (uint32 p = 0; p < 8; p++)
for (uint32 c = 0; c < 256; c++)
DirectColourMaps[p][c] = BUILD_PIXEL(IPPU.XB[((c & 7) << 2) | ((p & 1) << 1)], IPPU.XB[((c & 0x38) >> 1) | (p & 2)], IPPU.XB[((c & 0xc0) >> 3) | (p & 4)]);
IPPU.DirectColourMapsNeedRebuild = FALSE;
}
void S9xStartScreenRefresh (void)
{
if (IPPU.RenderThisFrame)
{
GFX.InterlaceFrame = !GFX.InterlaceFrame;
if (!GFX.DoInterlace || !GFX.InterlaceFrame)
{
if (!S9xInitUpdate())
{
IPPU.RenderThisFrame = FALSE;
return;
}
if (GFX.DoInterlace)
GFX.DoInterlace--;
IPPU.MaxBrightness = PPU.Brightness;
IPPU.Interlace = Memory.FillRAM[0x2133] & 1;
IPPU.InterlaceOBJ = Memory.FillRAM[0x2133] & 2;
IPPU.PseudoHires = Memory.FillRAM[0x2133] & 8;
if (Settings.SupportHiRes && (PPU.BGMode == 5 || PPU.BGMode == 6 || IPPU.PseudoHires))
{
GFX.RealPPL = GFX.Pitch >> 1;
IPPU.DoubleWidthPixels = TRUE;
IPPU.RenderedScreenWidth = SNES_WIDTH << 1;
}
else
{
#ifdef USE_OPENGL
if (Settings.OpenGLEnable)
GFX.RealPPL = SNES_WIDTH;
else
#endif
GFX.RealPPL = GFX.Pitch >> 1;
IPPU.DoubleWidthPixels = FALSE;
IPPU.RenderedScreenWidth = SNES_WIDTH;
}
if (Settings.SupportHiRes && IPPU.Interlace)
{
GFX.PPL = GFX.RealPPL << 1;
IPPU.DoubleHeightPixels = TRUE;
IPPU.RenderedScreenHeight = PPU.ScreenHeight << 1;
GFX.DoInterlace++;
}
else
{
GFX.PPL = GFX.RealPPL;
IPPU.DoubleHeightPixels = FALSE;
IPPU.RenderedScreenHeight = PPU.ScreenHeight;
}
IPPU.RenderedFramesCount++;
}
PPU.MosaicStart = 0;
PPU.RecomputeClipWindows = TRUE;
IPPU.PreviousLine = IPPU.CurrentLine = 0;
memset(GFX.ZBuffer, 0, GFX.ScreenSize);
memset(GFX.SubZBuffer, 0, GFX.ScreenSize);
}
if (++IPPU.FrameCount % Memory.ROMFramesPerSecond == 0)
{
IPPU.DisplayedRenderedFrameCount = IPPU.RenderedFramesCount;
IPPU.RenderedFramesCount = 0;
IPPU.FrameCount = 0;
}
if (GFX.InfoStringTimeout > 0 && --GFX.InfoStringTimeout == 0)
GFX.InfoString = NULL;
IPPU.TotalEmulatedFrames++;
}
void S9xEndScreenRefresh (void)
{
if (IPPU.RenderThisFrame)
{
FLUSH_REDRAW();
if (GFX.DoInterlace && GFX.InterlaceFrame == 0)
{
S9xControlEOF();
S9xContinueUpdate(IPPU.RenderedScreenWidth, IPPU.RenderedScreenHeight);
}
else
{
if (IPPU.ColorsChanged)
{
uint32 saved = PPU.CGDATA[0];
IPPU.ColorsChanged = FALSE;
S9xSetPalette();
PPU.CGDATA[0] = saved;
}
S9xControlEOF();
if (Settings.TakeScreenshot)
S9xDoScreenshot(IPPU.RenderedScreenWidth, IPPU.RenderedScreenHeight);
if (Settings.AutoDisplayMessages)
S9xDisplayMessages(GFX.Screen, GFX.RealPPL, IPPU.RenderedScreenWidth, IPPU.RenderedScreenHeight, 1);
S9xDeinitUpdate(IPPU.RenderedScreenWidth, IPPU.RenderedScreenHeight);
}
}
else
S9xControlEOF();
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 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;
p->M7HOFS = PPU.M7HOFS;
p->M7VOFS = PPU.M7VOFS;
}
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;
}
else
{
// if we're not rendering this frame, we still need to update this
// XXX: Check ForceBlank? Or anything else?
if (IPPU.OBJChanged)
SetupOBJ();
PPU.RangeTimeOver |= GFX.OBJLines[C].RTOFlags;
}
}
static inline void RenderScreen (bool8 sub)
{
uint8 BGActive;
int D;
if (!sub)
{
GFX.S = GFX.Screen;
if (GFX.DoInterlace && GFX.InterlaceFrame)
GFX.S += GFX.RealPPL;
GFX.DB = GFX.ZBuffer;
GFX.Clip = IPPU.Clip[0];
BGActive = Memory.FillRAM[0x212c] & ~Settings.BG_Forced;
D = 32;
}
else
{
GFX.S = GFX.SubScreen;
GFX.DB = GFX.SubZBuffer;
GFX.Clip = IPPU.Clip[1];
BGActive = Memory.FillRAM[0x212d] & ~Settings.BG_Forced;
D = (Memory.FillRAM[0x2130] & 2) << 4; // 'do math' depth flag
}
if (BGActive & 0x10)
{
BG.TileAddress = PPU.OBJNameBase;
BG.NameSelect = PPU.OBJNameSelect;
BG.EnableMath = !sub && (Memory.FillRAM[0x2131] & 0x10);
BG.StartPalette = 128;
S9xSelectTileConverter(4, FALSE, sub, FALSE);
S9xSelectTileRenderers(PPU.BGMode, sub, TRUE);
DrawOBJS(D + 4);
}
BG.NameSelect = 0;
S9xSelectTileRenderers(PPU.BGMode, sub, FALSE);
#define DO_BG(n, pal, depth, hires, offset, Zh, Zl, voffoff) \
if (BGActive & (1 << n)) \
{ \
BG.StartPalette = pal; \
BG.EnableMath = !sub && (Memory.FillRAM[0x2131] & (1 << n)); \
BG.TileSizeH = (!hires && PPU.BG[n].BGSize) ? 16 : 8; \
BG.TileSizeV = (PPU.BG[n].BGSize) ? 16 : 8; \
S9xSelectTileConverter(depth, hires, sub, PPU.BGMosaic[n]); \
\
if (offset) \
{ \
BG.OffsetSizeH = (!hires && PPU.BG[2].BGSize) ? 16 : 8; \
BG.OffsetSizeV = (PPU.BG[2].BGSize) ? 16 : 8; \
\
if (PPU.BGMosaic[n] && (hires || PPU.Mosaic > 1)) \
DrawBackgroundOffsetMosaic(n, D + Zh, D + Zl, voffoff); \
else \
DrawBackgroundOffset(n, D + Zh, D + Zl, voffoff); \
} \
else \
{ \
if (PPU.BGMosaic[n] && (hires || PPU.Mosaic > 1)) \
DrawBackgroundMosaic(n, D + Zh, D + Zl); \
else \
DrawBackground(n, D + Zh, D + Zl); \
} \
}
switch (PPU.BGMode)
{
case 0:
DO_BG(0, 0, 2, FALSE, FALSE, 15, 11, 0);
DO_BG(1, 32, 2, FALSE, FALSE, 14, 10, 0);
DO_BG(2, 64, 2, FALSE, FALSE, 7, 3, 0);
DO_BG(3, 96, 2, FALSE, FALSE, 6, 2, 0);
break;
case 1:
DO_BG(0, 0, 4, FALSE, FALSE, 15, 11, 0);
DO_BG(1, 0, 4, FALSE, FALSE, 14, 10, 0);
DO_BG(2, 0, 2, FALSE, FALSE, (PPU.BG3Priority ? 17 : 7), 3, 0);
break;
case 2:
DO_BG(0, 0, 4, FALSE, TRUE, 15, 7, 8);
DO_BG(1, 0, 4, FALSE, TRUE, 11, 3, 8);
break;
case 3:
DO_BG(0, 0, 8, FALSE, FALSE, 15, 7, 0);
DO_BG(1, 0, 4, FALSE, FALSE, 11, 3, 0);
break;
case 4:
DO_BG(0, 0, 8, FALSE, TRUE, 15, 7, 0);
DO_BG(1, 0, 2, FALSE, TRUE, 11, 3, 0);
break;
case 5:
DO_BG(0, 0, 4, TRUE, FALSE, 15, 7, 0);
DO_BG(1, 0, 2, TRUE, FALSE, 11, 3, 0);
break;
case 6:
DO_BG(0, 0, 4, TRUE, TRUE, 15, 7, 8);
break;
case 7:
if (BGActive & 0x01)
{
BG.EnableMath = !sub && (Memory.FillRAM[0x2131] & 1);
DrawBackgroundMode7(0, GFX.DrawMode7BG1Math, GFX.DrawMode7BG1Nomath, D);
}
if ((Memory.FillRAM[0x2133] & 0x40) && (BGActive & 0x02))
{
BG.EnableMath = !sub && (Memory.FillRAM[0x2131] & 2);
DrawBackgroundMode7(1, GFX.DrawMode7BG2Math, GFX.DrawMode7BG2Nomath, D);
}
break;
}
#undef DO_BG
BG.EnableMath = !sub && (Memory.FillRAM[0x2131] & 0x20);
DrawBackdrop();
}
void S9xUpdateScreen (void)
{
if (IPPU.OBJChanged || IPPU.InterlaceOBJ)
SetupOBJ();
// XXX: Check ForceBlank? Or anything else?
PPU.RangeTimeOver |= GFX.OBJLines[GFX.EndY].RTOFlags;
GFX.StartY = IPPU.PreviousLine;
if ((GFX.EndY = IPPU.CurrentLine - 1) >= PPU.ScreenHeight)
GFX.EndY = PPU.ScreenHeight - 1;
if (!PPU.ForcedBlanking)
{
// If force blank, may as well completely skip all this. We only did
// the OBJ because (AFAWK) the RTO flags are updated even during force-blank.
if (PPU.RecomputeClipWindows)
{
S9xComputeClipWindows();
PPU.RecomputeClipWindows = FALSE;
}
if (Settings.SupportHiRes)
{
if (!IPPU.DoubleWidthPixels && (PPU.BGMode == 5 || PPU.BGMode == 6 || IPPU.PseudoHires))
{
#ifdef USE_OPENGL
if (Settings.OpenGLEnable && GFX.RealPPL == 256)
{
// Have to back out of the speed up hack where the low res.
// SNES image was rendered into a 256x239 sized buffer,
// ignoring the true, larger size of the buffer.
GFX.RealPPL = GFX.Pitch >> 1;
for (register int32 y = (int32) GFX.StartY - 1; y >= 0; y--)
{
register uint16 *p = GFX.Screen + y * GFX.PPL + 255;
register uint16 *q = GFX.Screen + y * GFX.RealPPL + 510;
for (register int x = 255; x >= 0; x--, p--, q -= 2)
*q = *(q + 1) = *p;
}
GFX.PPL = GFX.RealPPL; // = GFX.Pitch >> 1 above
}
else
#endif
{
// Have to back out of the regular speed hack
for (register uint32 y = 0; y < GFX.StartY; y++)
{
register uint16 *p = GFX.Screen + y * GFX.PPL + 255;
register uint16 *q = GFX.Screen + y * GFX.PPL + 510;
for (register int x = 255; x >= 0; x--, p--, q -= 2)
*q = *(q + 1) = *p;
}
}
IPPU.DoubleWidthPixels = TRUE;
IPPU.RenderedScreenWidth = 512;
}
if (!IPPU.DoubleHeightPixels && IPPU.Interlace && (PPU.BGMode == 5 || PPU.BGMode == 6))
{
IPPU.DoubleHeightPixels = TRUE;
IPPU.RenderedScreenHeight = PPU.ScreenHeight << 1;
GFX.PPL = GFX.RealPPL << 1;
GFX.DoInterlace = 2;
for (register int32 y = (int32) GFX.StartY - 1; y >= 0; y--)
memmove(GFX.Screen + y * GFX.PPL, GFX.Screen + y * GFX.RealPPL, IPPU.RenderedScreenWidth * sizeof(uint16));
}
}
if ((Memory.FillRAM[0x2130] & 0x30) != 0x30 && (Memory.FillRAM[0x2131] & 0x3f))
GFX.FixedColour = BUILD_PIXEL(IPPU.XB[PPU.FixedColourRed], IPPU.XB[PPU.FixedColourGreen], IPPU.XB[PPU.FixedColourBlue]);
if (PPU.BGMode == 5 || PPU.BGMode == 6 || IPPU.PseudoHires ||
((Memory.FillRAM[0x2130] & 0x30) != 0x30 && (Memory.FillRAM[0x2130] & 2) && (Memory.FillRAM[0x2131] & 0x3f) && (Memory.FillRAM[0x212d] & 0x1f)))
// If hires (Mode 5/6 or pseudo-hires) or math is to be done
// involving the subscreen, then we need to render the subscreen...
RenderScreen(TRUE);
RenderScreen(FALSE);
}
else
{
const uint16 black = BUILD_PIXEL(0, 0, 0);
GFX.S = GFX.Screen + GFX.StartY * GFX.PPL;
if (GFX.DoInterlace && GFX.InterlaceFrame)
GFX.S += GFX.RealPPL;
for (uint32 l = GFX.StartY; l <= GFX.EndY; l++, GFX.S += GFX.PPL)
for (int x = 0; x < IPPU.RenderedScreenWidth; x++)
GFX.S[x] = black;
}
IPPU.PreviousLine = IPPU.CurrentLine;
}
static void SetupOBJ (void)
{
int SmallWidth, SmallHeight, LargeWidth, LargeHeight;
switch (PPU.OBJSizeSelect)
{
case 0:
SmallWidth = SmallHeight = 8;
LargeWidth = LargeHeight = 16;
break;
case 1:
SmallWidth = SmallHeight = 8;
LargeWidth = LargeHeight = 32;
break;
case 2:
SmallWidth = SmallHeight = 8;
LargeWidth = LargeHeight = 64;
break;
case 3:
SmallWidth = SmallHeight = 16;
LargeWidth = LargeHeight = 32;
break;
case 4:
SmallWidth = SmallHeight = 16;
LargeWidth = LargeHeight = 64;
break;
case 5:
default:
SmallWidth = SmallHeight = 32;
LargeWidth = LargeHeight = 64;
break;
case 6:
SmallWidth = 16; SmallHeight = 32;
LargeWidth = 32; LargeHeight = 64;
break;
case 7:
SmallWidth = 16; SmallHeight = 32;
LargeWidth = LargeHeight = 32;
break;
}
int inc = IPPU.InterlaceOBJ ? 2 : 1;
int startline = (IPPU.InterlaceOBJ && GFX.InterlaceFrame) ? 1 : 0;
// OK, we have three cases here. Either there's no priority, priority is
// normal FirstSprite, or priority is FirstSprite+Y. The first two are
// easy, the last is somewhat more ... interesting. So we split them up.
int Height;
uint8 S;
if (!PPU.OAMPriorityRotation || !(PPU.OAMFlip & PPU.OAMAddr & 1)) // normal case
{
uint8 LineOBJ[SNES_HEIGHT_EXTENDED];
memset(LineOBJ, 0, sizeof(LineOBJ));
for (int i = 0; i < SNES_HEIGHT_EXTENDED; i++)
{
GFX.OBJLines[i].RTOFlags = 0;
GFX.OBJLines[i].Tiles = 34;
for (int j = 0; j < 32; j++)
GFX.OBJLines[i].OBJ[j].Sprite = -1;
}
uint8 FirstSprite = PPU.FirstSprite;
S = FirstSprite;
do
{
if (PPU.OBJ[S].Size)
{
GFX.OBJWidths[S] = LargeWidth;
Height = LargeHeight;
}
else
{
GFX.OBJWidths[S] = SmallWidth;
Height = SmallHeight;
}
int HPos = PPU.OBJ[S].HPos;
if (HPos == -256)
HPos = 0;
if (HPos > -GFX.OBJWidths[S] && HPos <= 256)
{
if (HPos < 0)
GFX.OBJVisibleTiles[S] = (GFX.OBJWidths[S] + HPos + 7) >> 3;
else
if (HPos + GFX.OBJWidths[S] > 255)
GFX.OBJVisibleTiles[S] = (256 - HPos + 7) >> 3;
else
GFX.OBJVisibleTiles[S] = GFX.OBJWidths[S] >> 3;
for (uint8 line = startline, Y = (uint8) (PPU.OBJ[S].VPos & 0xff); line < Height; Y++, line += inc)
{
if (Y >= SNES_HEIGHT_EXTENDED)
continue;
if (LineOBJ[Y] >= 32)
{
GFX.OBJLines[Y].RTOFlags |= 0x40;
continue;
}
GFX.OBJLines[Y].Tiles -= GFX.OBJVisibleTiles[S];
if (GFX.OBJLines[Y].Tiles < 0)
GFX.OBJLines[Y].RTOFlags |= 0x80;
GFX.OBJLines[Y].OBJ[LineOBJ[Y]].Sprite = S;
if (PPU.OBJ[S].VFlip)
// Yes, Width not Height. It so happens that the
// sprites with H=2*W flip as two WxW sprites.
GFX.OBJLines[Y].OBJ[LineOBJ[Y]].Line = line ^ (GFX.OBJWidths[S] - 1);
else
GFX.OBJLines[Y].OBJ[LineOBJ[Y]].Line = line;
LineOBJ[Y]++;
}
}
S = (S + 1) & 0x7f;
} while (S != FirstSprite);
for (int Y = 1; Y < SNES_HEIGHT_EXTENDED; Y++)
GFX.OBJLines[Y].RTOFlags |= GFX.OBJLines[Y - 1].RTOFlags;
}
else // evil FirstSprite+Y case
{
// First, find out which sprites are on which lines
uint8 OBJOnLine[SNES_HEIGHT_EXTENDED][128];
memset(OBJOnLine, 0, sizeof(OBJOnLine));
for (S = 0; S < 128; S++)
{
if (PPU.OBJ[S].Size)
{
GFX.OBJWidths[S] = LargeWidth;
Height = LargeHeight;
}
else
{
GFX.OBJWidths[S] = SmallWidth;
Height = SmallHeight;
}
int HPos = PPU.OBJ[S].HPos;
if (HPos == -256)
HPos = 256;
if (HPos > -GFX.OBJWidths[S] && HPos <= 256)
{
if (HPos < 0)
GFX.OBJVisibleTiles[S] = (GFX.OBJWidths[S] + HPos + 7) >> 3;
else
if (HPos + GFX.OBJWidths[S] >= 257)
GFX.OBJVisibleTiles[S] = (257 - HPos + 7) >> 3;
else
GFX.OBJVisibleTiles[S] = GFX.OBJWidths[S] >> 3;
for (uint8 line = startline, Y = (uint8) (PPU.OBJ[S].VPos & 0xff); line < Height; Y++, line += inc)
{
if (Y >= SNES_HEIGHT_EXTENDED)
continue;
if (PPU.OBJ[S].VFlip)
// Yes, Width not Height. It so happens that the
// sprites with H=2*W flip as two WxW sprites.
OBJOnLine[Y][S] = (line ^ (GFX.OBJWidths[S] - 1)) | 0x80;
else
OBJOnLine[Y][S] = line | 0x80;
}
}
}
// Now go through and pull out those OBJ that are actually visible.
int j;
for (int Y = 0; Y < SNES_HEIGHT_EXTENDED; Y++)
{
GFX.OBJLines[Y].RTOFlags = Y ? GFX.OBJLines[Y - 1].RTOFlags : 0;
GFX.OBJLines[Y].Tiles = 34;
uint8 FirstSprite = (PPU.FirstSprite + Y) & 0x7f;
S = FirstSprite;
j = 0;
do
{
if (OBJOnLine[Y][S])
{
if (j >= 32)
{
GFX.OBJLines[Y].RTOFlags |= 0x40;
break;
}
GFX.OBJLines[Y].Tiles -= GFX.OBJVisibleTiles[S];
if (GFX.OBJLines[Y].Tiles < 0)
GFX.OBJLines[Y].RTOFlags |= 0x80;
GFX.OBJLines[Y].OBJ[j].Sprite = S;
GFX.OBJLines[Y].OBJ[j++].Line = OBJOnLine[Y][S] & ~0x80;
}
S = (S + 1) & 0x7f;
} while (S != FirstSprite);
if (j < 32)
GFX.OBJLines[Y].OBJ[j].Sprite = -1;
}
}
IPPU.OBJChanged = FALSE;
}
static void DrawOBJS (int D)
{
void (*DrawTile) (uint32, uint32, uint32, uint32) = NULL;
void (*DrawClippedTile) (uint32, uint32, uint32, uint32, uint32, uint32) = NULL;
int PixWidth = IPPU.DoubleWidthPixels ? 2 : 1;
BG.InterlaceLine = GFX.InterlaceFrame ? 8 : 0;
GFX.Z1 = 2;
for (uint32 Y = GFX.StartY, Offset = Y * GFX.PPL; Y <= GFX.EndY; Y++, Offset += GFX.PPL)
{
int I = 0;
int tiles = GFX.OBJLines[Y].Tiles;
for (int S = GFX.OBJLines[Y].OBJ[I].Sprite; S >= 0 && I < 32; S = GFX.OBJLines[Y].OBJ[++I].Sprite)
{
tiles += GFX.OBJVisibleTiles[S];
if (tiles <= 0)
continue;
int BaseTile = (((GFX.OBJLines[Y].OBJ[I].Line << 1) + (PPU.OBJ[S].Name & 0xf0)) & 0xf0) | (PPU.OBJ[S].Name & 0x100) | (PPU.OBJ[S].Palette << 10);
int TileX = PPU.OBJ[S].Name & 0x0f;
int TileLine = (GFX.OBJLines[Y].OBJ[I].Line & 7) * 8;
int TileInc = 1;
if (PPU.OBJ[S].HFlip)
{
TileX = (TileX + (GFX.OBJWidths[S] >> 3) - 1) & 0x0f;