2 * Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
4 * (c) Copyright 1996 - 2001 Gary Henderson (gary.henderson@ntlworld.com) and
5 * Jerremy Koot (jkoot@snes9x.com)
7 * Super FX C emulator code
8 * (c) Copyright 1997 - 1999 Ivar (ivar@snes9x.com) and
10 * Super FX assembler emulator code (c) Copyright 1998 zsKnight and _Demo_.
12 * DSP1 emulator code (c) Copyright 1998 Ivar, _Demo_ and Gary Henderson.
13 * C4 asm and some C emulation code (c) Copyright 2000 zsKnight and _Demo_.
14 * C4 C code (c) Copyright 2001 Gary Henderson (gary.henderson@ntlworld.com).
16 * DOS port code contains the works of other authors. See headers in
19 * Snes9x homepage: http://www.snes9x.com
21 * Permission to use, copy, modify and distribute Snes9x in both binary and
22 * source form, for non-commercial purposes, is hereby granted without fee,
23 * providing that this license information and copyright notice appear with
24 * all copies and any derived work.
26 * This software is provided 'as-is', without any express or implied
27 * warranty. In no event shall the authors be held liable for any damages
28 * arising from the use of this software.
30 * Snes9x is freeware for PERSONAL USE only. Commercial users should
31 * seek permission of the copyright holders first. Commercial use includes
32 * charging money for Snes9x or software derived from Snes9x.
34 * The copyright holders request that bug fixes and improvements to the code
35 * should be forwarded to them so everyone can benefit from the modifications
38 * Super NES and Super Nintendo Entertainment System are trademarks of
39 * Nintendo Co., Limited and its subsidiary companies.
51 //#include "asmmemfuncs.h"
57 extern void memcpy16(unsigned short *dest, unsigned short *src, int count);
58 extern void memcpy16bswap(unsigned short *dest, void *src, int count);
59 extern void memcpy32(uint32_t *dest, uint32_t *src, int count);
60 extern void memset32(uint32_t *dest, int c, int count);
68 void ComputeClipWindows ();
69 static void S9xDisplayFrameRate ();
70 static void S9xDisplayString (const char *string);
72 extern uint8 BitShifts[8][4];
73 extern uint8 TileShifts[8][4];
74 extern uint8 PaletteShifts[8][4];
75 extern uint8 PaletteMasks[8][4];
76 extern uint8 Depths[8][4];
77 extern uint8 BGSizes [2];
79 extern NormalTileRenderer DrawTilePtr;
80 extern ClippedTileRenderer DrawClippedTilePtr;
81 extern NormalTileRenderer DrawHiResTilePtr;
82 extern ClippedTileRenderer DrawHiResClippedTilePtr;
83 extern LargePixelRenderer DrawLargePixelPtr;
87 extern struct SLineData LineData[240];
88 extern struct SLineMatrixData LineMatrixData [240];
90 extern uint8 Mode7Depths [2];
93 (GFX.r212c & (1 << (N)) && \
94 !(PPU.BG_Forced & (1 << (N))))
96 #define SUB_OR_ADD(N) \
97 (GFX.r2131 & (1 << (N)))
100 ((GFX.r2130 & 0x30) != 0x30 && \
102 (GFX.r212d & (1 << N)) && \
103 !(PPU.BG_Forced & (1 << (N))))
105 #define ANYTHING_ON_SUB \
106 ((GFX.r2130 & 0x30) != 0x30 && \
110 #define ADD_OR_SUB_ON_ANYTHING \
113 #define BLACK BUILD_PIXEL(0,0,0)
115 void DrawTile (uint32 Tile, uint32 Offset, uint32 StartLine,
116 uint32 LineCount, struct SGFX * gfx);
117 void DrawClippedTile (uint32 Tile, uint32 Offset,
118 uint32 StartPixel, uint32 Width,
119 uint32 StartLine, uint32 LineCount, struct SGFX * gfx);
120 void DrawTilex2 (uint32 Tile, uint32 Offset, uint32 StartLine,
121 uint32 LineCount, struct SGFX * gfx);
122 void DrawClippedTilex2 (uint32 Tile, uint32 Offset,
123 uint32 StartPixel, uint32 Width,
124 uint32 StartLine, uint32 LineCount, struct SGFX * gfx);
125 void DrawTilex2x2 (uint32 Tile, uint32 Offset, uint32 StartLine,
126 uint32 LineCount, struct SGFX * gfx);
127 void DrawClippedTilex2x2 (uint32 Tile, uint32 Offset,
128 uint32 StartPixel, uint32 Width,
129 uint32 StartLine, uint32 LineCount, struct SGFX * gfx);
130 void DrawLargePixel (uint32 Tile, uint32 Offset,
131 uint32 StartPixel, uint32 Pixels,
132 uint32 StartLine, uint32 LineCount, struct SGFX * gfx);
134 void DrawTile16 (uint32 Tile, uint32 Offset, uint32 StartLine,
135 uint32 LineCount, struct SGFX * gfx);
136 void DrawClippedTile16 (uint32 Tile, uint32 Offset,
137 uint32 StartPixel, uint32 Width,
138 uint32 StartLine, uint32 LineCount, struct SGFX * gfx);
139 void DrawTile16x2 (uint32 Tile, uint32 Offset, uint32 StartLine,
140 uint32 LineCount, struct SGFX * gfx);
141 void DrawClippedTile16x2 (uint32 Tile, uint32 Offset,
142 uint32 StartPixel, uint32 Width,
143 uint32 StartLine, uint32 LineCount, struct SGFX * gfx);
144 void DrawTile16x2x2 (uint32 Tile, uint32 Offset, uint32 StartLine,
145 uint32 LineCount, struct SGFX * gfx);
146 void DrawClippedTile16x2x2 (uint32 Tile, uint32 Offset,
147 uint32 StartPixel, uint32 Width,
148 uint32 StartLine, uint32 LineCount, struct SGFX * gfx);
149 void DrawLargePixel16 (uint32 Tile, uint32 Offset,
150 uint32 StartPixel, uint32 Pixels,
151 uint32 StartLine, uint32 LineCount, struct SGFX * gfx);
153 void DrawTile16Add (uint32 Tile, uint32 Offset, uint32 StartLine,
154 uint32 LineCount, struct SGFX * gfx);
156 void DrawClippedTile16Add (uint32 Tile, uint32 Offset,
157 uint32 StartPixel, uint32 Width,
158 uint32 StartLine, uint32 LineCount, struct SGFX * gfx);
160 void DrawTile16Add1_2 (uint32 Tile, uint32 Offset, uint32 StartLine,
161 uint32 LineCount, struct SGFX * gfx);
163 void DrawClippedTile16Add1_2 (uint32 Tile, uint32 Offset,
164 uint32 StartPixel, uint32 Width,
165 uint32 StartLine, uint32 LineCount, struct SGFX * gfx);
167 void DrawTile16FixedAdd1_2 (uint32 Tile, uint32 Offset, uint32 StartLine,
168 uint32 LineCount, struct SGFX * gfx);
170 void DrawClippedTile16FixedAdd1_2 (uint32 Tile, uint32 Offset,
171 uint32 StartPixel, uint32 Width,
172 uint32 StartLine, uint32 LineCount, struct SGFX * gfx);
174 void DrawTile16Sub (uint32 Tile, uint32 Offset, uint32 StartLine,
175 uint32 LineCount, struct SGFX * gfx);
177 void DrawClippedTile16Sub (uint32 Tile, uint32 Offset,
178 uint32 StartPixel, uint32 Width,
179 uint32 StartLine, uint32 LineCount, struct SGFX * gfx);
181 void DrawTile16Sub1_2 (uint32 Tile, uint32 Offset, uint32 StartLine,
182 uint32 LineCount, struct SGFX * gfx);
184 void DrawClippedTile16Sub1_2 (uint32 Tile, uint32 Offset,
185 uint32 StartPixel, uint32 Width,
186 uint32 StartLine, uint32 LineCount, struct SGFX * gfx);
188 void DrawTile16FixedSub1_2 (uint32 Tile, uint32 Offset, uint32 StartLine,
189 uint32 LineCount, struct SGFX * gfx);
191 void DrawClippedTile16FixedSub1_2 (uint32 Tile, uint32 Offset,
192 uint32 StartPixel, uint32 Width,
193 uint32 StartLine, uint32 LineCount, struct SGFX * gfx);
195 void DrawLargePixel16Add (uint32 Tile, uint32 Offset,
196 uint32 StartPixel, uint32 Pixels,
197 uint32 StartLine, uint32 LineCount, struct SGFX * gfx);
199 void DrawLargePixel16Add1_2 (uint32 Tile, uint32 Offset,
200 uint32 StartPixel, uint32 Pixels,
201 uint32 StartLine, uint32 LineCount, struct SGFX * gfx);
203 void DrawLargePixel16Sub (uint32 Tile, uint32 Offset,
204 uint32 StartPixel, uint32 Pixels,
205 uint32 StartLine, uint32 LineCount, struct SGFX * gfx);
207 void DrawLargePixel16Sub1_2 (uint32 Tile, uint32 Offset,
208 uint32 StartPixel, uint32 Pixels,
209 uint32 StartLine, uint32 LineCount, struct SGFX * gfx);
211 void DrawHiResClippedTile16 (uint32 Tile, uint32 Offset,
212 uint32 StartPixel, uint32 Width,
213 uint32 StartLine, uint32 LineCount, struct SGFX * gfx);
215 void DrawHiResTile16 (uint32 Tile, uint32 Offset,
216 uint32 StartLine, uint32 LineCount, struct SGFX * gfx);
218 bool8_32 S9xGraphicsInit ()
220 register uint32 PixelOdd = 1;
221 register uint32 PixelEven = 2;
223 #ifdef GFX_MULTI_FORMAT
224 if (GFX.BuildPixel == NULL)
225 S9xSetRenderPixelFormat (RGB565);
228 for (uint8 bitshift = 0; bitshift < 4; bitshift++)
230 for (register int i = 0; i < 16; i++)
232 register uint32 h = 0;
233 register uint32 l = 0;
235 #if defined(LSB_FIRST)
254 h |= (PixelOdd << 24);
256 h |= (PixelOdd << 16);
258 h |= (PixelOdd << 8);
262 l |= (PixelOdd << 24);
264 l |= (PixelOdd << 16);
266 l |= (PixelOdd << 8);
271 odd_high[bitshift][i] = h;
272 odd_low[bitshift][i] = l;
275 #if defined(LSB_FIRST)
281 h |= PixelEven << 16;
283 h |= PixelEven << 24;
289 l |= PixelEven << 16;
291 l |= PixelEven << 24;
294 h |= (PixelEven << 24);
296 h |= (PixelEven << 16);
298 h |= (PixelEven << 8);
302 l |= (PixelEven << 24);
304 l |= (PixelEven << 16);
306 l |= (PixelEven << 8);
311 even_high[bitshift][i] = h;
312 even_low[bitshift][i] = l;
318 GFX.RealPitch = GFX.Pitch2 = GFX.Pitch;
319 GFX.ZPitch = GFX.Pitch;
320 if (Settings.SixteenBit)
322 GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;
323 GFX.DepthDelta = GFX.SubZBuffer - GFX.ZBuffer;
324 //GFX.InfoStringTimeout = 0;
325 //GFX.InfoString = NULL;
328 IPPU.OBJChanged = TRUE;
329 if (Settings.Transparency)
330 Settings.SixteenBit = TRUE;
332 IPPU.DirectColourMapsNeedRebuild = TRUE;
334 if (Settings.SixteenBit)
336 DrawTilePtr = DrawTile16;
337 DrawClippedTilePtr = DrawClippedTile16;
338 DrawLargePixelPtr = DrawLargePixel16;
339 DrawHiResTilePtr= DrawHiResTile16;
340 DrawHiResClippedTilePtr = DrawHiResClippedTile16;
341 GFX.PPL = GFX.Pitch >> 1;
342 GFX.PPLx2 = GFX.Pitch;
346 DrawTilePtr = DrawTile;
347 DrawClippedTilePtr = DrawClippedTile;
348 DrawLargePixelPtr = DrawLargePixel;
349 DrawHiResTilePtr = DrawTile;
350 DrawHiResClippedTilePtr = DrawClippedTile;
352 GFX.PPLx2 = GFX.Pitch * 2;
354 S9xFixColourBrightness ();
356 if (Settings.SixteenBit)
358 if (!(GFX.X2 = (uint16 *) malloc (sizeof (uint16) * 0x10000)))
361 if (!(GFX.ZERO_OR_X2 = (uint16 *) malloc (sizeof (uint16) * 0x10000)) ||
362 !(GFX.ZERO = (uint16 *) malloc (sizeof (uint16) * 0x10000)))
366 free ((char *) GFX.ZERO_OR_X2);
367 GFX.ZERO_OR_X2 = NULL;
371 free ((char *) GFX.X2);
378 // Build a lookup table that multiplies a packed RGB value by 2 with
380 for (r = 0; r <= MAX_RED; r++)
385 for (g = 0; g <= MAX_GREEN; g++)
390 for (b = 0; b <= MAX_BLUE; b++)
395 GFX.X2 [BUILD_PIXEL2 (r, g, b)] = BUILD_PIXEL2 (r2, g2, b2);
396 GFX.X2 [BUILD_PIXEL2 (r, g, b) & ~ALPHA_BITS_MASK] = BUILD_PIXEL2 (r2, g2, b2);
400 ZeroMemory (GFX.ZERO, 0x10000 * sizeof (uint16));
401 ZeroMemory (GFX.ZERO_OR_X2, 0x10000 * sizeof (uint16));
402 // Build a lookup table that if the top bit of the color value is zero
403 // then the value is zero, otherwise multiply the value by 2. Used by
404 // the color subtraction code.
406 #if defined(OLD_COLOUR_BLENDING)
407 for (r = 0; r <= MAX_RED; r++)
410 if ((r2 & 0x10) == 0)
413 r2 = (r2 << 1) & MAX_RED;
415 for (g = 0; g <= MAX_GREEN; g++)
418 if ((g2 & GREEN_HI_BIT) == 0)
421 g2 = (g2 << 1) & MAX_GREEN;
423 for (b = 0; b <= MAX_BLUE; b++)
426 if ((b2 & 0x10) == 0)
429 b2 = (b2 << 1) & MAX_BLUE;
431 GFX.ZERO_OR_X2 [BUILD_PIXEL2 (r, g, b)] = BUILD_PIXEL2 (r2, g2, b2);
432 GFX.ZERO_OR_X2 [BUILD_PIXEL2 (r, g, b) & ~ALPHA_BITS_MASK] = BUILD_PIXEL2 (r2, g2, b2);
437 for (r = 0; r <= MAX_RED; r++)
440 if ((r2 & 0x10) == 0)
443 r2 = (r2 << 1) & MAX_RED;
447 for (g = 0; g <= MAX_GREEN; g++)
450 if ((g2 & GREEN_HI_BIT) == 0)
453 g2 = (g2 << 1) & MAX_GREEN;
457 for (b = 0; b <= MAX_BLUE; b++)
460 if ((b2 & 0x10) == 0)
463 b2 = (b2 << 1) & MAX_BLUE;
467 GFX.ZERO_OR_X2 [BUILD_PIXEL2 (r, g, b)] = BUILD_PIXEL2 (r2, g2, b2);
468 GFX.ZERO_OR_X2 [BUILD_PIXEL2 (r, g, b) & ~ALPHA_BITS_MASK] = BUILD_PIXEL2 (r2, g2, b2);
474 // Build a lookup table that if the top bit of the color value is zero
475 // then the value is zero, otherwise its just the value.
476 for (r = 0; r <= MAX_RED; r++)
479 if ((r2 & 0x10) == 0)
484 for (g = 0; g <= MAX_GREEN; g++)
487 if ((g2 & GREEN_HI_BIT) == 0)
491 for (b = 0; b <= MAX_BLUE; b++)
494 if ((b2 & 0x10) == 0)
499 GFX.ZERO [BUILD_PIXEL2 (r, g, b)] = BUILD_PIXEL2 (r2, g2, b2);
500 GFX.ZERO [BUILD_PIXEL2 (r, g, b) & ~ALPHA_BITS_MASK] = BUILD_PIXEL2 (r2, g2, b2);
508 GFX.ZERO_OR_X2 = NULL;
515 void S9xGraphicsDeinit (void)
517 // Free any memory allocated in S9xGraphicsInit
520 free ((char *) GFX.X2);
525 free ((char *) GFX.ZERO_OR_X2);
526 GFX.ZERO_OR_X2 = NULL;
530 free ((char *) GFX.ZERO);
535 void S9xBuildDirectColourMaps ()
537 for (uint32 p = 0; p < 8; p++)
539 for (uint32 c = 0; c < 256; c++)
542 DirectColourMaps [p][c] = BUILD_PIXEL (((c & 7) << 2) | ((p & 1) << 1),
543 ((c & 0x38) >> 1) | (p & 2),
544 ((c & 0xc0) >> 3) | (p & 4));
547 IPPU.DirectColourMapsNeedRebuild = FALSE;
550 void S9xStartScreenRefresh ()
552 if (GFX.InfoStringTimeout > 0 && --GFX.InfoStringTimeout == 0)
553 GFX.InfoString = NULL;
555 if (IPPU.RenderThisFrame)
558 if (!S9xInitUpdate ())
560 IPPU.RenderThisFrame = FALSE;
564 IPPU.RenderedFramesCount++;
565 IPPU.PreviousLine = IPPU.CurrentLine = 0;
566 IPPU.MaxBrightness = PPU.Brightness;
567 IPPU.LatchedBlanking = PPU.ForcedBlanking;
568 IPPU.LatchedInterlace = (Memory.FillRAM[0x2133] & 1);
569 IPPU.RenderedScreenWidth = 256;
570 IPPU.RenderedScreenHeight = PPU.ScreenHeight;
571 IPPU.DoubleWidthPixels = FALSE;
572 GFX.Pitch2 = GFX.Pitch = GFX.RealPitch;
573 GFX.PPL = GFX.PPLx2 >> 1;
574 GFX.ZPitch = GFX.RealPitch;
575 if (Settings.SixteenBit)
577 PPU.RecomputeClipWindows = TRUE;
578 GFX.DepthDelta = GFX.SubZBuffer - GFX.ZBuffer;
579 GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;
581 if (++IPPU.FrameCount % Memory.ROMFramesPerSecond == 0)
583 IPPU.DisplayedRenderedFrameCount = IPPU.RenderedFramesCount;
584 IPPU.RenderedFramesCount = 0;
589 void RenderLine (uint8 C)
591 if (IPPU.RenderThisFrame)
594 LineData[C].BG[0].VOffset = PPU.BG[0].VOffset + 1;
595 LineData[C].BG[0].HOffset = PPU.BG[0].HOffset;
596 LineData[C].BG[1].VOffset = PPU.BG[1].VOffset + 1;
597 LineData[C].BG[1].HOffset = PPU.BG[1].HOffset;
601 struct SLineMatrixData *p = &LineMatrixData [C];
602 p->MatrixA = PPU.MatrixA;
603 p->MatrixB = PPU.MatrixB;
604 p->MatrixC = PPU.MatrixC;
605 p->MatrixD = PPU.MatrixD;
606 p->CentreX = PPU.CentreX;
607 p->CentreY = PPU.CentreY;
612 if (Settings.StarfoxHack && PPU.BG[2].VOffset == 0 &&
613 PPU.BG[2].HOffset == 0xe000)
615 LineData[C].BG[2].VOffset = 0xe1;
616 LineData[C].BG[2].HOffset = 0;
622 LineData[C].BG[2].VOffset = PPU.BG[2].VOffset + 1;
623 LineData[C].BG[2].HOffset = PPU.BG[2].HOffset;
624 LineData[C].BG[3].VOffset = PPU.BG[3].VOffset + 1;
625 LineData[C].BG[3].HOffset = PPU.BG[3].HOffset;
629 IPPU.CurrentLine = C + 1;
634 void S9xEndScreenRefresh()
636 IPPU.HDMAStarted = FALSE;
639 if (IPPU.RenderThisFrame)
642 if (IPPU.ColorsChanged)
644 uint32 saved = PPU.CGDATA[0];
645 if (!Settings.SixteenBit)
647 // Hack for Super Mario World - to get its sky blue
648 // (It uses Fixed colour addition on the backdrop colour)
649 if (!(Memory.FillRAM [0x2131] & 0x80) &&
650 (Memory.FillRAM[0x2131] & 0x20) &&
651 (PPU.FixedColourRed || PPU.FixedColourGreen ||
652 PPU.FixedColourBlue))
654 PPU.CGDATA[0] = PPU.FixedColourRed |
655 (PPU.FixedColourGreen << 5) |
656 (PPU.FixedColourBlue << 10);
659 IPPU.ColorsChanged = FALSE;
663 PPU.CGDATA[0] = saved;
665 GFX.Pitch = GFX.Pitch2 = GFX.RealPitch;
666 GFX.PPL = GFX.PPLx2 >> 1;
668 if (Settings.DisplayFrameRate)
669 S9xDisplayFrameRate ();
671 S9xDisplayString (GFX.InfoString);
673 S9xDeinitUpdate (IPPU.RenderedScreenWidth, IPPU.RenderedScreenHeight,
674 Settings.SixteenBit);
682 if (CPU.Flags & FRAME_ADVANCE_FLAG)
684 if (ICPU.FrameAdvanceCount)
686 ICPU.FrameAdvanceCount--;
687 IPPU.RenderThisFrame = TRUE;
692 CPU.Flags &= ~FRAME_ADVANCE_FLAG;
693 CPU.Flags |= DEBUG_MODE_FLAG;
699 if (CPU.SRAMModified)
701 if (!CPU.AutoSaveTimer)
703 if (!(CPU.AutoSaveTimer = Settings.AutoSaveDelay * Memory.ROMFramesPerSecond))
704 CPU.SRAMModified = FALSE;
708 if (!--CPU.AutoSaveTimer)
711 CPU.SRAMModified = FALSE;
718 void S9xSetInfoString (const char *string)
720 GFX.InfoString = string;
721 GFX.InfoStringTimeout = 120;
724 INLINE void SelectTileRenderer (bool8_32 normal)
728 DrawTilePtr = DrawTile16;
729 DrawClippedTilePtr = DrawClippedTile16;
730 DrawLargePixelPtr = DrawLargePixel16;
734 if (GFX.r2131 & 0x80)
736 if (GFX.r2131 & 0x40)
740 DrawTilePtr = DrawTile16Sub1_2;
741 DrawClippedTilePtr = DrawClippedTile16Sub1_2;
745 // Fixed colour substraction
746 DrawTilePtr = DrawTile16FixedSub1_2;
747 DrawClippedTilePtr = DrawClippedTile16FixedSub1_2;
749 DrawLargePixelPtr = DrawLargePixel16Sub1_2;
753 DrawTilePtr = DrawTile16Sub;
754 DrawClippedTilePtr = DrawClippedTile16Sub;
755 DrawLargePixelPtr = DrawLargePixel16Sub;
760 if (GFX.r2131 & 0x40)
764 DrawTilePtr = DrawTile16Add1_2;
765 DrawClippedTilePtr = DrawClippedTile16Add1_2;
769 // Fixed colour addition
770 DrawTilePtr = DrawTile16FixedAdd1_2;
771 DrawClippedTilePtr = DrawClippedTile16FixedAdd1_2;
773 DrawLargePixelPtr = DrawLargePixel16Add1_2;
777 DrawTilePtr = DrawTile16Add;
778 DrawClippedTilePtr = DrawClippedTile16Add;
779 DrawLargePixelPtr = DrawLargePixel16Add;
790 switch (PPU.OBJSizeSelect)
821 int FirstSprite = PPU.FirstSprite & 0x7f;
826 if (PPU.OBJ [S].Size)
831 long VPos = PPU.OBJ [S].VPos;
833 if (VPos >= PPU.ScreenHeight)
835 if (PPU.OBJ [S].HPos < 256 && PPU.OBJ [S].HPos > -Size &&
836 VPos < PPU.ScreenHeight && VPos > -Size)
838 GFX.OBJList [C++] = S;
840 GFX.VPositions[S] = VPos;
843 } while (S != FirstSprite);
845 // Terminate the list
846 GFX.OBJList [C] = -1;
847 IPPU.OBJChanged = FALSE;
850 void DrawOBJS (bool8_32 OnMain = FALSE, uint8 D = 0)
853 uint32 BaseTile, Tile;
859 BG.TileAddress = PPU.OBJNameBase;
860 BG.StartPalette = 128;
863 BG.Buffer = IPPU.TileCache [TILE_4BIT];
864 BG.Buffered = IPPU.TileCached [TILE_4BIT];
865 BG.NameSelect = PPU.OBJNameSelect;
866 BG.DirectColourMode = FALSE;
873 for (int S = GFX.OBJList [I++]; S >= 0; S = GFX.OBJList [I++])
875 int VPos = GFX.VPositions [S];
876 int Size = GFX.Sizes[S];
880 if (VPos + Size <= (int) GFX.StartY || VPos > (int) GFX.EndY)
883 if (OnMain && SUB_OR_ADD(4))
885 SelectTileRenderer (!GFX.Pseudo && PPU.OBJ [S].Palette < 4);
888 BaseTile = PPU.OBJ[S].Name | (PPU.OBJ[S].Palette << 10);
890 if (PPU.OBJ[S].HFlip)
892 BaseTile += ((Size >> 3) - 1) | H_FLIP;
895 if (PPU.OBJ[S].VFlip)
898 int clipcount = GFX.pCurrentClip->Count [4];
902 GFX.Z2 = (PPU.OBJ[S].Priority + 1) * 4 + D;
904 for (int clip = 0; clip < clipcount; clip++)
908 if (!GFX.pCurrentClip->Count [4])
915 Left = GFX.pCurrentClip->Left [clip][4];
916 Right = GFX.pCurrentClip->Right [clip][4];
919 if (Right <= Left || PPU.OBJ[S].HPos + Size <= Left ||
920 PPU.OBJ[S].HPos >= Right)
923 for (int Y = 0; Y < Size; Y += 8)
925 if (VPos + Y + 7 >= (int) GFX.StartY && VPos + Y <= (int) GFX.EndY)
932 if ((StartLine = VPos + Y) < (int) GFX.StartY)
934 StartLine = GFX.StartY - StartLine;
935 LineCount = 8 - StartLine;
942 if ((Last = VPos + Y + 7 - GFX.EndY) > 0)
943 if ((LineCount -= Last) <= 0)
946 TileLine = StartLine << 3;
947 O = (VPos + Y + StartLine) * GFX.PPL;
948 if (!PPU.OBJ[S].VFlip)
949 Tile = BaseTile + (Y << 1);
951 Tile = BaseTile + ((Size - Y - 8) << 1);
953 int Middle = Size >> 3;
954 if (PPU.OBJ[S].HPos < Left)
956 Tile += ((Left - PPU.OBJ[S].HPos) >> 3) * TileInc;
957 Middle -= (Left - PPU.OBJ[S].HPos) >> 3;
958 O += Left * GFX.PixSize;
959 if ((Offset = (Left - PPU.OBJ[S].HPos) & 7))
961 O -= Offset * GFX.PixSize;
963 int Width = Right - Left;
966 (*DrawClippedTilePtr) (Tile, O, Offset, W,
967 TileLine, LineCount, &GFX);
973 O += 8 * GFX.PixSize;
977 O += PPU.OBJ[S].HPos * GFX.PixSize;
979 if (PPU.OBJ[S].HPos + Size >= Right)
981 Middle -= ((PPU.OBJ[S].HPos + Size + 7) -
983 Offset = (Right - (PPU.OBJ[S].HPos + Size)) & 7;
988 for (int X = 0; X < Middle; X++, O += 8 * GFX.PixSize,
991 (*DrawTilePtr) (Tile, O, TileLine, LineCount, &GFX);
995 (*DrawClippedTilePtr) (Tile, O, 0, Offset,
996 TileLine, LineCount, &GFX);
1004 void DrawBackgroundMosaic (uint32 BGMode, uint32 bg, uint8 Z1, uint8 Z2)
1013 uint8 depths [2] = {Z1, Z2};
1016 BG.StartPalette = bg << 5;
1018 BG.StartPalette = 0;
1020 SC0 = (uint16 *) &Memory.VRAM[PPU.BG[bg].SCBase << 1];
1022 if (PPU.BG[bg].SCSize & 1)
1027 if (PPU.BG[bg].SCSize & 2)
1032 if (PPU.BG[bg].SCSize & 1)
1041 if (BG.TileSize == 16)
1052 for (uint32 Y = GFX.StartY; Y <= GFX.EndY; Y += Lines)
1054 uint32 VOffset = LineData [Y].BG[bg].VOffset;
1055 uint32 HOffset = LineData [Y].BG[bg].HOffset;
1056 uint32 MosaicOffset = Y % PPU.Mosaic;
1058 for (Lines = 1; Lines < PPU.Mosaic - MosaicOffset; Lines++)
1059 if ((VOffset != LineData [Y + Lines].BG[bg].VOffset) ||
1060 (HOffset != LineData [Y + Lines].BG[bg].HOffset))
1063 uint32 MosaicLine = VOffset + Y - MosaicOffset;
1065 if (Y + Lines > GFX.EndY)
1066 Lines = GFX.EndY + 1 - Y;
1067 uint32 VirtAlign = (MosaicLine & 7) << 3;
1072 uint32 ScreenLine = MosaicLine >> OffsetShift;
1073 uint32 Rem16 = MosaicLine & 15;
1075 if (ScreenLine & 0x20)
1080 b1 += (ScreenLine & 0x1f) << 5;
1081 b2 += (ScreenLine & 0x1f) << 5;
1086 uint32 ClipCount = GFX.pCurrentClip->Count [bg];
1087 uint32 HPos = HOffset;
1088 uint32 PixWidth = PPU.Mosaic;
1093 for (uint32 clip = 0; clip < ClipCount; clip++)
1095 if (GFX.pCurrentClip->Count [bg])
1097 Left = GFX.pCurrentClip->Left [clip][bg];
1098 Right = GFX.pCurrentClip->Right [clip][bg];
1099 uint32 r = Left % PPU.Mosaic;
1100 HPos = HOffset + Left;
1101 PixWidth = PPU.Mosaic - r;
1103 uint32 s = Y * GFX.PPL + Left * GFX.PixSize;
1104 for (uint32 x = Left; x < Right; x += PixWidth,
1105 s += PixWidth * GFX.PixSize,
1106 HPos += PixWidth, PixWidth = PPU.Mosaic)
1108 uint32 Quot = (HPos & OffsetMask) >> 3;
1110 if (x + PixWidth >= Right)
1111 PixWidth = Right - x;
1113 if (BG.TileSize == 8)
1116 t = b2 + (Quot & 0x1f);
1123 t = b2 + ((Quot >> 1) & 0x1f);
1125 t = b1 + (Quot >> 1);
1128 Tile = READ_2BYTES (t);
1129 GFX.Z1 = GFX.Z2 = depths [(Tile & 0x2000) >> 13];
1132 if (BG.TileSize != 8)
1136 // Horizontal flip, but what about vertical flip ?
1139 // Both horzontal & vertical flip
1142 (*DrawLargePixelPtr) (Tile + 17 - (Quot & 1), s,
1144 VirtAlign, Lines, &GFX);
1148 (*DrawLargePixelPtr) (Tile + 1 - (Quot & 1), s,
1150 VirtAlign, Lines, &GFX);
1155 // Horizontal flip only
1158 (*DrawLargePixelPtr) (Tile + 17 - (Quot & 1), s,
1160 VirtAlign, Lines, &GFX);
1164 (*DrawLargePixelPtr) (Tile + 1 - (Quot & 1), s,
1166 VirtAlign, Lines, &GFX);
1172 // No horizontal flip, but is there a vertical flip ?
1175 // Vertical flip only
1178 (*DrawLargePixelPtr) (Tile + 16 + (Quot & 1), s,
1180 VirtAlign, Lines, &GFX);
1184 (*DrawLargePixelPtr) (Tile + (Quot & 1), s,
1186 VirtAlign, Lines, &GFX);
1194 (*DrawLargePixelPtr) (Tile + 16 + (Quot & 1), s,
1196 VirtAlign, Lines, &GFX);
1200 (*DrawLargePixelPtr) (Tile + (Quot & 1), s,
1202 VirtAlign, Lines, &GFX);
1208 (*DrawLargePixelPtr) (Tile, s, HPos & 7, PixWidth,
1209 VirtAlign, Lines, &GFX);
1215 void DrawBackgroundOffset (uint32 BGMode, uint32 bg, uint8 Z1, uint8 Z2)
1229 int VOffsetOffset = BGMode == 4 ? 0 : 32;
1230 uint8 depths [2] = {Z1, Z2};
1232 BG.StartPalette = 0;
1234 BPS0 = (uint16 *) &Memory.VRAM[PPU.BG[2].SCBase << 1];
1236 if (PPU.BG[2].SCSize & 1)
1241 if (PPU.BG[2].SCSize & 2)
1246 if (PPU.BG[2].SCSize & 1)
1251 SC0 = (uint16 *) &Memory.VRAM[PPU.BG[bg].SCBase << 1];
1253 if (PPU.BG[bg].SCSize & 1)
1258 if (PPU.BG[bg].SCSize & 2)
1262 if (PPU.BG[bg].SCSize & 1)
1267 static const int Lines = 1;
1270 int OffsetEnableMask = 1 << (bg + 13);
1272 if (BG.TileSize == 16)
1283 for (uint32 Y = GFX.StartY; Y <= GFX.EndY; Y++)
1285 uint32 VOff = LineData [Y].BG[2].VOffset;
1286 uint32 HOff = LineData [Y].BG[2].HOffset;
1288 int ScreenLine = VOff >> 3;
1295 if (ScreenLine & 0x20)
1296 s1 = BPS2, s2 = BPS3;
1298 s1 = BPS0, s2 = BPS1;
1300 s1 += (ScreenLine & 0x1f) << 5;
1301 s2 += (ScreenLine & 0x1f) << 5;
1303 int clipcount = GFX.pCurrentClip->Count [bg];
1307 for (int clip = 0; clip < clipcount; clip++)
1312 if (!GFX.pCurrentClip->Count [bg])
1319 Left = GFX.pCurrentClip->Left [clip][bg];
1320 Right = GFX.pCurrentClip->Right [clip][bg];
1328 uint32 LineHOffset=LineData [Y].BG[bg].HOffset;
1339 uint32 TotalCount = 0;
1340 uint32 MaxCount = 8;
1342 uint32 s = Left * GFX.PixSize + Y * GFX.PPL;
1343 bool8_32 left_hand_edge = (Left == 0);
1344 Width = Right - Left;
1347 MaxCount = 8 - (Left & 7);
1349 while (Left < Right)
1353 // The SNES offset-per-tile background mode has a
1354 // hardware limitation that the offsets cannot be set
1355 // for the tile at the left-hand edge of the screen.
1356 VOffset = LineData [Y].BG[bg].VOffset;
1357 HOffset = LineHOffset;
1358 left_hand_edge = FALSE;
1362 // All subsequent offset tile data is shifted left by one,
1363 // hence the - 1 below.
1364 Quot2 = ((HOff + Left - 1) & OffsetMask) >> 3;
1367 s0 = s2 + (Quot2 & 0x1f);
1371 HCellOffset = READ_2BYTES (s0);
1375 VOffset = LineData [Y].BG[bg].VOffset;
1376 HOffset=LineHOffset;
1377 if ((HCellOffset & OffsetEnableMask))
1379 if (HCellOffset & 0x8000)
1380 VOffset = HCellOffset + 1;
1382 HOffset = HCellOffset;
1387 VCellOffset = READ_2BYTES (s0 + VOffsetOffset);
1388 if ((VCellOffset & OffsetEnableMask))
1389 VOffset = VCellOffset + 1;
1391 VOffset = LineData [Y].BG[bg].VOffset;
1393 if ((HCellOffset & OffsetEnableMask))
1394 HOffset = (HCellOffset & ~7)|(LineHOffset&7);
1396 HOffset=LineHOffset;
1399 VirtAlign = ((Y + VOffset) & 7) << 3;
1400 ScreenLine = (VOffset + Y) >> OffsetShift;
1402 if (((VOffset + Y) & 15) > 7)
1413 if (ScreenLine & 0x20)
1418 b1 += (ScreenLine & 0x1f) << 5;
1419 b2 += (ScreenLine & 0x1f) << 5;
1421 HPos = (HOffset + Left) & OffsetMask;
1425 if (BG.TileSize == 8)
1428 t = b2 + (Quot & 0x1f);
1435 t = b2 + ((Quot >> 1) & 0x1f);
1437 t = b1 + (Quot >> 1);
1440 if (MaxCount + TotalCount > Width)
1441 MaxCount = Width - TotalCount;
1446 if (Count > MaxCount)
1449 s -= Offset * GFX.PixSize;
1450 Tile = READ_2BYTES(t);
1451 GFX.Z1 = GFX.Z2 = depths [(Tile & 0x2000) >> 13];
1453 if (BG.TileSize == 8)
1454 (*DrawClippedTilePtr) (Tile, s, Offset, Count, VirtAlign, Lines, &GFX);
1457 if (!(Tile & (V_FLIP | H_FLIP)))
1459 // Normal, unflipped
1460 (*DrawClippedTilePtr) (Tile + t1 + (Quot & 1),
1461 s, Offset, Count, VirtAlign, Lines, &GFX);
1469 (*DrawClippedTilePtr) (Tile + t2 + 1 - (Quot & 1),
1470 s, Offset, Count, VirtAlign, Lines, &GFX);
1475 (*DrawClippedTilePtr) (Tile + t1 + 1 - (Quot & 1),
1476 s, Offset, Count, VirtAlign, Lines, &GFX);
1482 (*DrawClippedTilePtr) (Tile + t2 + (Quot & 1),
1483 s, Offset, Count, VirtAlign, Lines, &GFX);
1488 TotalCount += Count;
1489 s += (Offset + Count) * GFX.PixSize;
1496 void DrawBackgroundMode5 (uint32 /* BGMODE */, uint32 bg, uint8 Z1, uint8 Z2)
1500 GFX.Pitch = GFX.RealPitch;
1501 GFX.PPL = GFX.PPLx2 >> 1;
1503 uint8 depths [2] = {Z1, Z2};
1512 BG.StartPalette = 0;
1514 SC0 = (uint16 *) &Memory.VRAM[PPU.BG[bg].SCBase << 1];
1516 if ((PPU.BG[bg].SCSize & 1))
1521 if ((PPU.BG[bg].SCSize & 2))
1526 if ((PPU.BG[bg].SCSize & 1))
1535 if (BG.TileSize == 16)
1537 VOffsetMask = 0x3ff;
1542 VOffsetMask = 0x1ff;
1545 int endy = GFX.EndY;
1547 for (int Y = GFX.StartY; Y <= endy; Y += Lines)
1550 uint32 VOffset = LineData [y].BG[bg].VOffset;
1551 uint32 HOffset = LineData [y].BG[bg].HOffset;
1552 int VirtAlign = (Y + VOffset) & 7;
1554 for (Lines = 1; Lines < 8 - VirtAlign; Lines++)
1555 if ((VOffset != LineData [y + Lines].BG[bg].VOffset) ||
1556 (HOffset != LineData [y + Lines].BG[bg].HOffset))
1560 if (Y + Lines > endy)
1561 Lines = endy + 1 - Y;
1563 int ScreenLine = (VOffset + Y) >> VOffsetShift;
1566 if (((VOffset + Y) & 15) > 7)
1579 if (ScreenLine & 0x20)
1584 b1 += (ScreenLine & 0x1f) << 5;
1585 b2 += (ScreenLine & 0x1f) << 5;
1587 int clipcount = GFX.pCurrentClip->Count [bg];
1590 for (int clip = 0; clip < clipcount; clip++)
1595 if (!GFX.pCurrentClip->Count [bg])
1602 Left = GFX.pCurrentClip->Left [clip][bg] * 2;
1603 Right = GFX.pCurrentClip->Right [clip][bg] * 2;
1609 uint32 s = (Left>>1) * GFX.PixSize + Y * GFX.PPL;
1610 uint32 HPos = (HOffset + Left * GFX.PixSize) & 0x3ff;
1612 uint32 Quot = HPos >> 3;
1617 t = b2 + ((Quot >> 1) & 0x1f);
1619 t = b1 + (Quot >> 1);
1621 Width = Right - Left;
1622 // Left hand edge clipped tile
1625 int Offset = (HPos & 7);
1630 Tile = READ_2BYTES (t);
1631 GFX.Z1 = GFX.Z2 = depths [(Tile & 0x2000) >> 13];
1633 if (BG.TileSize == 8)
1635 if (!(Tile & H_FLIP))
1637 // Normal, unflipped
1638 (*DrawHiResClippedTilePtr) (Tile + (Quot & 1),
1639 s, Offset, Count, VirtAlign, Lines, &GFX);
1644 (*DrawHiResClippedTilePtr) (Tile + 1 - (Quot & 1),
1645 s, Offset, Count, VirtAlign, Lines, &GFX);
1650 if (!(Tile & (V_FLIP | H_FLIP)))
1652 // Normal, unflipped
1653 (*DrawHiResClippedTilePtr) (Tile + t1 + (Quot & 1),
1654 s, Offset, Count, VirtAlign, Lines, &GFX);
1662 (*DrawHiResClippedTilePtr) (Tile + t2 + 1 - (Quot & 1),
1663 s, Offset, Count, VirtAlign, Lines, &GFX);
1668 (*DrawHiResClippedTilePtr) (Tile + t1 + 1 - (Quot & 1),
1669 s, Offset, Count, VirtAlign, Lines, &GFX);
1675 (*DrawHiResClippedTilePtr) (Tile + t2 + (Quot & 1),
1676 s, Offset, Count, VirtAlign, Lines, &GFX);
1683 else if (Quot == 127)
1689 // Middle, unclipped tiles
1690 Count = Width - Count;
1691 int Middle = Count >> 3;
1693 for (int C = Middle; C > 0; s += 4, Quot++, C--)
1695 Tile = READ_2BYTES(t);
1696 GFX.Z1 = GFX.Z2 = depths [(Tile & 0x2000) >> 13];
1697 if (BG.TileSize == 8)
1699 if (!(Tile & H_FLIP))
1701 // Normal, unflipped
1702 (*DrawHiResTilePtr) (Tile + (Quot & 1),
1703 s, VirtAlign, Lines, &GFX);
1708 (*DrawHiResTilePtr) (Tile + 1 - (Quot & 1),
1709 s, VirtAlign, Lines, &GFX);
1714 if (!(Tile & (V_FLIP | H_FLIP)))
1716 // Normal, unflipped
1717 (*DrawHiResTilePtr) (Tile + t1 + (Quot & 1),
1718 s, VirtAlign, Lines, &GFX);
1726 (*DrawHiResTilePtr) (Tile + t2 + 1 - (Quot & 1),
1727 s, VirtAlign, Lines, &GFX);
1732 (*DrawHiResTilePtr) (Tile + t1 + 1 - (Quot & 1),
1733 s, VirtAlign, Lines, &GFX);
1739 (*DrawHiResTilePtr) (Tile + t2 + (Quot & 1),
1740 s, VirtAlign, Lines, &GFX);
1752 // Right-hand edge clipped tiles
1755 Tile = READ_2BYTES(t);
1756 GFX.Z1 = GFX.Z2 = depths [(Tile & 0x2000) >> 13];
1757 if (BG.TileSize == 8)
1759 if (!(Tile & H_FLIP))
1761 // Normal, unflipped
1762 (*DrawHiResClippedTilePtr) (Tile + (Quot & 1),
1763 s, 0, Count, VirtAlign, Lines, &GFX);
1768 (*DrawHiResClippedTilePtr) (Tile + 1 - (Quot & 1),
1769 s, 0, Count, VirtAlign, Lines, &GFX);
1774 if (!(Tile & (V_FLIP | H_FLIP)))
1776 // Normal, unflipped
1777 (*DrawHiResClippedTilePtr) (Tile + t1 + (Quot & 1),
1778 s, 0, Count, VirtAlign, Lines, &GFX);
1786 (*DrawHiResClippedTilePtr) (Tile + t2 + 1 - (Quot & 1),
1787 s, 0, Count, VirtAlign, Lines, &GFX);
1792 (*DrawHiResClippedTilePtr) (Tile + t1 + 1 - (Quot & 1),
1793 s, 0, Count, VirtAlign, Lines, &GFX);
1799 (*DrawHiResClippedTilePtr) (Tile + t2 + (Quot & 1),
1800 s, 0, Count, VirtAlign, Lines, &GFX);
1808 void DrawBackground (uint32 BGMode, uint32 bg, uint8 Z1, uint8 Z2)
1812 BG.TileSize = BGSizes [PPU.BG[bg].BGSize];
1813 BG.BitShift = BitShifts[BGMode][bg];
1814 BG.TileShift = TileShifts[BGMode][bg];
1815 BG.TileAddress = PPU.BG[bg].NameBase << 1;
1817 BG.Buffer = IPPU.TileCache [Depths [BGMode][bg]];
1818 BG.Buffered = IPPU.TileCached [Depths [BGMode][bg]];
1819 BG.PaletteShift = PaletteShifts[BGMode][bg];
1820 BG.PaletteMask = PaletteMasks[BGMode][bg];
1821 BG.DirectColourMode = (BGMode == 3 || BGMode == 4) && bg == 0 &&
1824 if (PPU.BGMosaic [bg] && PPU.Mosaic > 1)
1826 DrawBackgroundMosaic (BGMode, bg, Z1, Z2);
1833 if (Settings.WrestlemaniaArcade)
1835 case 4: // Used by Puzzle Bobble
1836 DrawBackgroundOffset (BGMode, bg, Z1, Z2);
1840 case 6: // XXX: is also offset per tile.
1841 DrawBackgroundMode5 (BGMode, bg, Z1, Z2);
1857 uint8 depths [2] = {Z1, Z2};
1860 BG.StartPalette = bg << 5;
1862 BG.StartPalette = 0;
1864 SC0 = (uint16 *) &Memory.VRAM[PPU.BG[bg].SCBase << 1];
1866 if (PPU.BG[bg].SCSize & 1)
1871 if (PPU.BG[bg].SCSize & 2)
1876 if (PPU.BG[bg].SCSize & 1)
1885 if (BG.TileSize == 16)
1896 for (uint32 Y = GFX.StartY; Y <= GFX.EndY; Y += Lines)
1898 uint32 VOffset = LineData [Y].BG[bg].VOffset;
1899 uint32 HOffset = LineData [Y].BG[bg].HOffset;
1900 int VirtAlign = (Y + VOffset) & 7;
1902 for (Lines = 1; Lines < 8 - VirtAlign; Lines++)
1903 if ((VOffset != LineData [Y + Lines].BG[bg].VOffset) ||
1904 (HOffset != LineData [Y + Lines].BG[bg].HOffset))
1907 if (Y + Lines > GFX.EndY)
1908 Lines = GFX.EndY + 1 - Y;
1912 uint32 ScreenLine = (VOffset + Y) >> OffsetShift;
1915 if (((VOffset + Y) & 15) > 7)
1928 if (ScreenLine & 0x20)
1933 b1 += (ScreenLine & 0x1f) << 5;
1934 b2 += (ScreenLine & 0x1f) << 5;
1936 int clipcount = GFX.pCurrentClip->Count [bg];
1939 for (int clip = 0; clip < clipcount; clip++)
1944 if (!GFX.pCurrentClip->Count [bg])
1951 Left = GFX.pCurrentClip->Left [clip][bg];
1952 Right = GFX.pCurrentClip->Right [clip][bg];
1958 uint32 s = Left * GFX.PixSize + Y * GFX.PPL;
1959 uint32 HPos = (HOffset + Left) & OffsetMask;
1961 uint32 Quot = HPos >> 3;
1965 if (BG.TileSize == 8)
1968 t = b2 + (Quot & 0x1f);
1975 t = b2 + ((Quot >> 1) & 0x1f);
1977 t = b1 + (Quot >> 1);
1980 Width = Right - Left;
1981 // Left hand edge clipped tile
1984 uint32 Offset = (HPos & 7);
1988 s -= Offset * GFX.PixSize;
1989 Tile = READ_2BYTES(t);
1990 GFX.Z1 = GFX.Z2 = depths [(Tile & 0x2000) >> 13];
1992 if (BG.TileSize == 8)
1994 (*DrawClippedTilePtr) (Tile, s, Offset, Count, VirtAlign,
1999 if (!(Tile & (V_FLIP | H_FLIP)))
2001 // Normal, unflipped
2002 (*DrawClippedTilePtr) (Tile + t1 + (Quot & 1),
2003 s, Offset, Count, VirtAlign, Lines, &GFX);
2011 (*DrawClippedTilePtr) (Tile + t2 + 1 - (Quot & 1),
2012 s, Offset, Count, VirtAlign, Lines, &GFX);
2017 (*DrawClippedTilePtr) (Tile + t1 + 1 - (Quot & 1),
2018 s, Offset, Count, VirtAlign, Lines, &GFX);
2024 (*DrawClippedTilePtr) (Tile + t2 + (Quot & 1), s,
2025 Offset, Count, VirtAlign, Lines, &GFX);
2029 if (BG.TileSize == 8)
2034 else if (Quot == 63)
2042 else if (Quot == 127)
2046 s += 8 * GFX.PixSize;
2049 // Middle, unclipped tiles
2050 Count = Width - Count;
2051 int Middle = Count >> 3;
2053 for (int C = Middle; C > 0; s += 8 * GFX.PixSize, Quot++, C--)
2055 Tile = READ_2BYTES(t);
2056 GFX.Z1 = GFX.Z2 = depths [(Tile & 0x2000) >> 13];
2058 if (BG.TileSize != 8)
2062 // Horizontal flip, but what about vertical flip ?
2065 // Both horzontal & vertical flip
2066 (*DrawTilePtr) (Tile + t2 + 1 - (Quot & 1), s,
2067 VirtAlign, Lines, &GFX);
2071 // Horizontal flip only
2072 (*DrawTilePtr) (Tile + t1 + 1 - (Quot & 1), s,
2073 VirtAlign, Lines, &GFX);
2078 // No horizontal flip, but is there a vertical flip ?
2081 // Vertical flip only
2082 (*DrawTilePtr) (Tile + t2 + (Quot & 1), s,
2083 VirtAlign, Lines, &GFX);
2088 (*DrawTilePtr) (Tile + t1 + (Quot & 1), s,
2089 VirtAlign, Lines, &GFX);
2095 (*DrawTilePtr) (Tile, s, VirtAlign, Lines, &GFX);
2098 if (BG.TileSize == 8)
2117 // Right-hand edge clipped tiles
2120 Tile = READ_2BYTES(t);
2121 GFX.Z1 = GFX.Z2 = depths [(Tile & 0x2000) >> 13];
2123 if (BG.TileSize == 8)
2124 (*DrawClippedTilePtr) (Tile, s, 0, Count, VirtAlign,
2128 if (!(Tile & (V_FLIP | H_FLIP)))
2130 // Normal, unflipped
2131 (*DrawClippedTilePtr) (Tile + t1 + (Quot & 1), s, 0,
2132 Count, VirtAlign, Lines, &GFX);
2140 (*DrawClippedTilePtr) (Tile + t2 + 1 - (Quot & 1),
2141 s, 0, Count, VirtAlign,
2147 (*DrawClippedTilePtr) (Tile + t1 + 1 - (Quot & 1),
2148 s, 0, Count, VirtAlign,
2155 (*DrawClippedTilePtr) (Tile + t2 + (Quot & 1),
2156 s, 0, Count, VirtAlign,
2165 #define RENDER_BACKGROUND_MODE7(TYPE,FUNC) \
2168 uint8 *VRAM1 = Memory.VRAM + 1; \
2169 if (GFX.r2130 & 1) \
2171 if (IPPU.DirectColourMapsNeedRebuild) \
2172 S9xBuildDirectColourMaps (); \
2173 GFX.ScreenColors = DirectColourMaps [0]; \
2176 GFX.ScreenColors = IPPU.ScreenColors; \
2182 uint32 Right = 256; \
2183 uint32 ClipCount = GFX.pCurrentClip->Count [bg]; \
2188 Screen += GFX.StartY * GFX.Pitch; \
2189 uint8 *Depth = GFX.DB + GFX.StartY * GFX.PPL; \
2190 struct SLineMatrixData *l = &LineMatrixData [GFX.StartY]; \
2192 for (uint32 Line = GFX.StartY; Line <= GFX.EndY; Line++, Screen += GFX.Pitch, Depth += GFX.PPL, l++) \
2196 int32 HOffset = ((int32) LineData [Line].BG[0].HOffset << M7) >> M7; \
2197 int32 VOffset = ((int32) LineData [Line].BG[0].VOffset << M7) >> M7; \
2199 int32 CentreX = ((int32) l->CentreX << M7) >> M7; \
2200 int32 CentreY = ((int32) l->CentreY << M7) >> M7; \
2202 if (PPU.Mode7VFlip) \
2203 yy = 261 - (int) Line; \
2207 if (PPU.Mode7Repeat == 0) \
2208 yy += (VOffset - CentreY) % 1023; \
2210 yy += VOffset - CentreY; \
2211 int BB = l->MatrixB * yy + (CentreX << 8); \
2212 int DD = l->MatrixD * yy + (CentreY << 8); \
2214 for (uint32 clip = 0; clip < ClipCount; clip++) \
2216 if (GFX.pCurrentClip->Count [bg]) \
2218 Left = GFX.pCurrentClip->Left [clip][bg]; \
2219 Right = GFX.pCurrentClip->Right [clip][bg]; \
2220 if (Right <= Left) \
2223 TYPE *p = (TYPE *) Screen + Left; \
2224 uint8 *d = Depth + Left; \
2226 if (PPU.Mode7HFlip) \
2228 startx = Right - 1; \
2243 if (PPU.Mode7Repeat == 0) \
2244 xx = startx + (HOffset - CentreX) % 1023; \
2246 xx = startx + HOffset - CentreX; \
2247 int AA = l->MatrixA * xx; \
2248 int CC = l->MatrixC * xx; \
2250 if (!PPU.Mode7Repeat) \
2252 for (int x = startx; x != endx; x += dir, AA += aa, CC += cc, p++, d++) \
2254 int X = ((AA + BB) >> 8) & 0x3ff; \
2255 int Y = ((CC + DD) >> 8) & 0x3ff; \
2256 uint8 *TileData = VRAM1 + (Memory.VRAM[((Y & ~7) << 5) + ((X >> 2) & ~1)] << 7); \
2257 uint32 b = *(TileData + ((Y & 7) << 4) + ((X & 7) << 1)); \
2258 GFX.Z1 = Mode7Depths [(b & GFX.Mode7PriorityMask) >> 7]; \
2259 if (GFX.Z1 > *d && b) \
2268 for (int x = startx; x != endx; x += dir, AA += aa, CC += cc, p++, d++) \
2270 int X = ((AA + BB) >> 8); \
2271 int Y = ((CC + DD) >> 8); \
2273 if (Settings.Dezaemon && PPU.Mode7Repeat == 2) \
2279 if (((X | Y) & ~0x3ff) == 0) \
2281 uint8 *TileData = VRAM1 + (Memory.VRAM[((Y & ~7) << 5) + ((X >> 2) & ~1)] << 7); \
2282 uint32 b = *(TileData + ((Y & 7) << 4) + ((X & 7) << 1)); \
2283 GFX.Z1 = Mode7Depths [(b & GFX.Mode7PriorityMask) >> 7]; \
2284 if (GFX.Z1 > *d && b) \
2292 if (PPU.Mode7Repeat == 3) \
2294 X = (x + HOffset) & 7; \
2295 Y = (yy + CentreY) & 7; \
2296 uint32 b = *(VRAM1 + ((Y & 7) << 4) + ((X & 7) << 1)); \
2297 GFX.Z1 = Mode7Depths [(b & GFX.Mode7PriorityMask) >> 7]; \
2298 if (GFX.Z1 > *d && b) \
2310 void DrawBGMode7Background (uint8 *Screen, int bg)
2312 RENDER_BACKGROUND_MODE7 (uint8, (uint8) (b & GFX.Mode7Mask))
2315 void DrawBGMode7Background16 (uint8 *Screen, int bg)
2317 RENDER_BACKGROUND_MODE7 (uint16, GFX.ScreenColors [b & GFX.Mode7Mask]);
2320 void DrawBGMode7Background16Add (uint8 *Screen, int bg)
2322 RENDER_BACKGROUND_MODE7 (uint16, *(d + GFX.DepthDelta) ?
2323 (*(d + GFX.DepthDelta) != 1 ?
2324 COLOR_ADD (GFX.ScreenColors [b & GFX.Mode7Mask],
2326 COLOR_ADD (GFX.ScreenColors [b & GFX.Mode7Mask],
2328 GFX.ScreenColors [b & GFX.Mode7Mask]);
2331 void DrawBGMode7Background16Add1_2 (uint8 *Screen, int bg)
2333 RENDER_BACKGROUND_MODE7 (uint16, *(d + GFX.DepthDelta) ?
2334 (*(d + GFX.DepthDelta) != 1 ?
2335 COLOR_ADD1_2 (GFX.ScreenColors [b & GFX.Mode7Mask],
2337 COLOR_ADD (GFX.ScreenColors [b & GFX.Mode7Mask],
2339 GFX.ScreenColors [b & GFX.Mode7Mask]);
2342 void DrawBGMode7Background16Sub (uint8 *Screen, int bg)
2344 RENDER_BACKGROUND_MODE7 (uint16, *(d + GFX.DepthDelta) ?
2345 (*(d + GFX.DepthDelta) != 1 ?
2346 COLOR_SUB (GFX.ScreenColors [b & GFX.Mode7Mask],
2348 COLOR_SUB (GFX.ScreenColors [b & GFX.Mode7Mask],
2350 GFX.ScreenColors [b & GFX.Mode7Mask]);
2353 void DrawBGMode7Background16Sub1_2 (uint8 *Screen, int bg)
2355 RENDER_BACKGROUND_MODE7 (uint16, *(d + GFX.DepthDelta) ?
2356 (*(d + GFX.DepthDelta) != 1 ?
2357 COLOR_SUB1_2 (GFX.ScreenColors [b & GFX.Mode7Mask],
2359 COLOR_SUB (GFX.ScreenColors [b & GFX.Mode7Mask],
2361 GFX.ScreenColors [b & GFX.Mode7Mask]);
2364 #define RENDER_BACKGROUND_MODE7_i(TYPE,FUNC,COLORFUNC) \
2367 uint8 *VRAM1 = Memory.VRAM + 1; \
2368 if (GFX.r2130 & 1) \
2370 if (IPPU.DirectColourMapsNeedRebuild) \
2371 S9xBuildDirectColourMaps (); \
2372 GFX.ScreenColors = DirectColourMaps [0]; \
2375 GFX.ScreenColors = IPPU.ScreenColors; \
2381 uint32 Right = 256; \
2382 uint32 ClipCount = GFX.pCurrentClip->Count [bg]; \
2387 Screen += GFX.StartY * GFX.Pitch; \
2388 uint8 *Depth = GFX.DB + GFX.StartY * GFX.PPL; \
2389 struct SLineMatrixData *l = &LineMatrixData [GFX.StartY]; \
2390 bool8_32 allowSimpleCase = FALSE; \
2391 if (!l->MatrixB && !l->MatrixC && (l->MatrixA == 0x0100) && (l->MatrixD == 0x0100) \
2392 && !LineMatrixData[GFX.EndY].MatrixB && !LineMatrixData[GFX.EndY].MatrixC \
2393 && (LineMatrixData[GFX.EndY].MatrixA == 0x0100) && (LineMatrixData[GFX.EndY].MatrixD == 0x0100) \
2395 allowSimpleCase = TRUE; \
2397 for (uint32 Line = GFX.StartY; Line <= GFX.EndY; Line++, Screen += GFX.Pitch, Depth += GFX.PPL, l++) \
2401 int HOffset = ((int) LineData [Line].BG[0].HOffset << M7) >> M7; \
2402 int VOffset = ((int) LineData [Line].BG[0].VOffset << M7) >> M7; \
2404 int CentreX = ((int) l->CentreX << M7) >> M7; \
2405 int CentreY = ((int) l->CentreY << M7) >> M7; \
2407 if (PPU.Mode7VFlip) \
2408 yy = 261 - (int) Line; \
2412 if (PPU.Mode7Repeat == 0) \
2413 yy += (VOffset - CentreY) % 1023; \
2415 yy += VOffset - CentreY; \
2416 bool8_32 simpleCase = FALSE; \
2419 /* Make a special case for the identity matrix, since it's a common case and */ \
2420 /* can be done much more quickly without special effects */ \
2421 if (allowSimpleCase && !l->MatrixB && !l->MatrixC && (l->MatrixA == 0x0100) && (l->MatrixD == 0x0100)) \
2423 BB = CentreX << 8; \
2424 DD = (yy + CentreY) << 8; \
2425 simpleCase = TRUE; \
2429 BB = l->MatrixB * yy + (CentreX << 8); \
2430 DD = l->MatrixD * yy + (CentreY << 8); \
2433 for (uint32 clip = 0; clip < ClipCount; clip++) \
2435 if (GFX.pCurrentClip->Count [bg]) \
2437 Left = GFX.pCurrentClip->Left [clip][bg]; \
2438 Right = GFX.pCurrentClip->Right [clip][bg]; \
2439 if (Right <= Left) \
2442 TYPE *p = (TYPE *) Screen + Left; \
2443 uint8 *d = Depth + Left; \
2445 if (PPU.Mode7HFlip) \
2447 startx = Right - 1; \
2462 if (PPU.Mode7Repeat == 0) \
2463 xx = startx + (HOffset - CentreX) % 1023; \
2465 xx = startx + HOffset - CentreX; \
2473 AA = l->MatrixA * xx; \
2474 CC = l->MatrixC * xx; \
2478 if (!PPU.Mode7Repeat) \
2483 int X = ((AA + BB) >> 8) & 0x3ff; \
2484 int Y = (DD >> 8) & 0x3ff; \
2485 uint8 *TileData = VRAM1 + (Memory.VRAM[((Y & ~7) << 5) + ((X >> 2) & ~1)] << 7); \
2486 uint32 b = *(TileData + ((Y & 7) << 4) + ((X & 7) << 1)); \
2487 GFX.Z1 = Mode7Depths [(b & GFX.Mode7PriorityMask) >> 7]; \
2488 if (GFX.Z1 > *d && b) \
2490 TYPE theColor = COLORFUNC; \
2491 *p = (FUNC) | ALPHA_BITS_MASK; \
2494 AA += aa, p++, d++; \
2496 } while (x != endx); \
2502 int X = (AA + BB) >> 8; \
2505 if(Settings.Dezaemon && PPU.Mode7Repeat == 2) \
2511 if (((X | Y) & ~0x3ff) == 0) \
2513 uint8 *TileData = VRAM1 + (Memory.VRAM[((Y & ~7) << 5) + ((X >> 2) & ~1)] << 7); \
2514 uint32 b = *(TileData + ((Y & 7) << 4) + ((X & 7) << 1)); \
2515 GFX.Z1 = Mode7Depths [(b & GFX.Mode7PriorityMask) >> 7]; \
2516 if (GFX.Z1 > *d && b) \
2518 TYPE theColor = COLORFUNC; \
2519 *p = (FUNC) | ALPHA_BITS_MASK; \
2523 else if (PPU.Mode7Repeat == 3) \
2525 X = (x + HOffset) & 7; \
2526 Y = (yy + CentreY) & 7; \
2527 uint8 *TileData = VRAM1 + (Memory.VRAM[((Y & ~7) << 5) + ((X >> 2) & ~1)] << 7); \
2528 uint32 b = *(TileData + ((Y & 7) << 4) + ((X & 7) << 1)); \
2529 GFX.Z1 = Mode7Depths [(b & GFX.Mode7PriorityMask) >> 7]; \
2530 if (GFX.Z1 > *d && b) \
2532 TYPE theColor = COLORFUNC; \
2533 *p = (FUNC) | ALPHA_BITS_MASK; \
2537 AA += aa; p++; d++; \
2539 } while (x != endx); \
2542 else if (!PPU.Mode7Repeat) \
2544 /* The bilinear interpolator: get the colors at the four points surrounding */ \
2545 /* the location of one point in the _sampled_ image, and weight them according */ \
2546 /* to their (city block) distance. It's very smooth, but blurry with "close up" */ \
2549 /* 460 (slightly less than 2 source pixels per displayed pixel) is an educated */ \
2550 /* guess for where bilinear filtering will become a poor method for averaging. */ \
2551 /* (When reducing the image, the weighting used by a bilinear filter becomes */ \
2552 /* arbitrary, and a simple mean is a better way to represent the source image.) */ \
2553 /* You can think of this as a kind of mipmapping. */ \
2554 if ((aa < 460 && aa > -460) && (cc < 460 && cc > -460)) \
2556 for (int x = startx; x != endx; x += dir, AA += aa, CC += cc, p++, d++) \
2558 uint32 xPos = AA + BB; \
2559 uint32 xPix = xPos >> 8; \
2560 uint32 yPos = CC + DD; \
2561 uint32 yPix = yPos >> 8; \
2562 uint32 X = xPix & 0x3ff; \
2563 uint32 Y = yPix & 0x3ff; \
2564 uint8 *TileData = VRAM1 + (Memory.VRAM[((Y & ~7) << 5) + ((X >> 2) & ~1)] << 7); \
2565 uint32 b = *(TileData + ((Y & 7) << 4) + ((X & 7) << 1)); \
2566 GFX.Z1 = Mode7Depths [(b & GFX.Mode7PriorityMask) >> 7]; \
2567 if (GFX.Z1 > *d && b) \
2569 /* X10 and Y01 are the X and Y coordinates of the next source point over. */ \
2570 uint32 X10 = (xPix + dir) & 0x3ff; \
2571 uint32 Y01 = (yPix + dir) & 0x3ff; \
2572 uint8 *TileData10 = VRAM1 + (Memory.VRAM[((Y & ~7) << 5) + ((X10 >> 2) & ~1)] << 7); \
2573 uint8 *TileData11 = VRAM1 + (Memory.VRAM[((Y01 & ~7) << 5) + ((X10 >> 2) & ~1)] << 7); \
2574 uint8 *TileData01 = VRAM1 + (Memory.VRAM[((Y01 & ~7) << 5) + ((X >> 2) & ~1)] << 7); \
2575 uint32 p1 = COLORFUNC; \
2576 p1 = (p1 & FIRST_THIRD_COLOR_MASK) | ((p1 & SECOND_COLOR_MASK) << 16); \
2577 b = *(TileData10 + ((Y & 7) << 4) + ((X10 & 7) << 1)); \
2578 uint32 p2 = COLORFUNC; \
2579 p2 = (p2 & FIRST_THIRD_COLOR_MASK) | ((p2 & SECOND_COLOR_MASK) << 16); \
2580 b = *(TileData11 + ((Y01 & 7) << 4) + ((X10 & 7) << 1)); \
2581 uint32 p4 = COLORFUNC; \
2582 p4 = (p4 & FIRST_THIRD_COLOR_MASK) | ((p4 & SECOND_COLOR_MASK) << 16); \
2583 b = *(TileData01 + ((Y01 & 7) << 4) + ((X & 7) << 1)); \
2584 uint32 p3 = COLORFUNC; \
2585 p3 = (p3 & FIRST_THIRD_COLOR_MASK) | ((p3 & SECOND_COLOR_MASK) << 16); \
2586 /* Xdel, Ydel: position (in 1/32nds) between the points */ \
2587 uint32 Xdel = (xPos >> 3) & 0x1F; \
2588 uint32 Ydel = (yPos >> 3) & 0x1F; \
2589 uint32 XY = (Xdel*Ydel) >> 5; \
2590 uint32 area1 = 0x20 + XY - Xdel - Ydel; \
2591 uint32 area2 = Xdel - XY; \
2592 uint32 area3 = Ydel - XY; \
2593 uint32 area4 = XY; \
2594 uint32 tempColor = ((area1 * p1) + \
2597 (area4 * p4)) >> 5; \
2598 TYPE theColor = (tempColor & FIRST_THIRD_COLOR_MASK) | ((tempColor >> 16) & SECOND_COLOR_MASK); \
2599 *p = (FUNC) | ALPHA_BITS_MASK; \
2605 /* The oversampling method: get the colors at four corners of a square */ \
2606 /* in the _displayed_ image, and average them. It's sharp and clean, but */ \
2607 /* gives the usual huge pixels when the source image gets "close." */ \
2609 /* Find the dimensions of the square in the source image whose corners will be examined. */ \
2610 uint32 aaDelX = aa >> 1; \
2611 uint32 ccDelX = cc >> 1; \
2612 uint32 bbDelY = l->MatrixB >> 1; \
2613 uint32 ddDelY = l->MatrixD >> 1; \
2614 /* Offset the location within the source image so that the four sampled points */ \
2615 /* center around where the single point would otherwise have been drawn. */ \
2616 BB -= (bbDelY >> 1); \
2617 DD -= (ddDelY >> 1); \
2618 AA -= (aaDelX >> 1); \
2619 CC -= (ccDelX >> 1); \
2620 uint32 BB10 = BB + aaDelX; \
2621 uint32 BB01 = BB + bbDelY; \
2622 uint32 BB11 = BB + aaDelX + bbDelY; \
2623 uint32 DD10 = DD + ccDelX; \
2624 uint32 DD01 = DD + ddDelY; \
2625 uint32 DD11 = DD + ccDelX + ddDelY; \
2626 for (int x = startx; x != endx; x += dir, AA += aa, CC += cc, p++, d++) \
2628 uint32 X = ((AA + BB) >> 8) & 0x3ff; \
2629 uint32 Y = ((CC + DD) >> 8) & 0x3ff; \
2630 uint8 *TileData = VRAM1 + (Memory.VRAM[((Y & ~7) << 5) + ((X >> 2) & ~1)] << 7); \
2631 uint32 b = *(TileData + ((Y & 7) << 4) + ((X & 7) << 1)); \
2632 GFX.Z1 = Mode7Depths [(b & GFX.Mode7PriorityMask) >> 7]; \
2633 if (GFX.Z1 > *d && b) \
2635 /* X, Y, X10, Y10, etc. are the coordinates of the four pixels within the */ \
2636 /* source image that we're going to examine. */ \
2637 uint32 X10 = ((AA + BB10) >> 8) & 0x3ff; \
2638 uint32 Y10 = ((CC + DD10) >> 8) & 0x3ff; \
2639 uint32 X01 = ((AA + BB01) >> 8) & 0x3ff; \
2640 uint32 Y01 = ((CC + DD01) >> 8) & 0x3ff; \
2641 uint32 X11 = ((AA + BB11) >> 8) & 0x3ff; \
2642 uint32 Y11 = ((CC + DD11) >> 8) & 0x3ff; \
2643 uint8 *TileData10 = VRAM1 + (Memory.VRAM[((Y10 & ~7) << 5) + ((X10 >> 2) & ~1)] << 7); \
2644 uint8 *TileData01 = VRAM1 + (Memory.VRAM[((Y01 & ~7) << 5) + ((X01 >> 2) & ~1)] << 7); \
2645 uint8 *TileData11 = VRAM1 + (Memory.VRAM[((Y11 & ~7) << 5) + ((X11 >> 2) & ~1)] << 7); \
2646 TYPE p1 = COLORFUNC; \
2647 b = *(TileData10 + ((Y10 & 7) << 4) + ((X10 & 7) << 1)); \
2648 TYPE p2 = COLORFUNC; \
2649 b = *(TileData01 + ((Y01 & 7) << 4) + ((X01 & 7) << 1)); \
2650 TYPE p3 = COLORFUNC; \
2651 b = *(TileData11 + ((Y11 & 7) << 4) + ((X11 & 7) << 1)); \
2652 TYPE p4 = COLORFUNC; \
2653 TYPE theColor = Q_INTERPOLATE(p1, p2, p3, p4); \
2654 *p = (FUNC) | ALPHA_BITS_MASK; \
2662 for (int x = startx; x != endx; x += dir, AA += aa, CC += cc, p++, d++) \
2664 uint32 xPos = AA + BB; \
2665 uint32 xPix = xPos >> 8; \
2666 uint32 yPos = CC + DD; \
2667 uint32 yPix = yPos >> 8; \
2672 if(Settings.Dezaemon && PPU.Mode7Repeat == 2) \
2678 if (((X | Y) & ~0x3ff) == 0) \
2680 uint8 *TileData = VRAM1 + (Memory.VRAM[((Y & ~7) << 5) + ((X >> 2) & ~1)] << 7); \
2681 uint32 b = *(TileData + ((Y & 7) << 4) + ((X & 7) << 1)); \
2682 GFX.Z1 = Mode7Depths [(b & GFX.Mode7PriorityMask) >> 7]; \
2683 if (GFX.Z1 > *d && b) \
2685 /* X10 and Y01 are the X and Y coordinates of the next source point over. */ \
2686 uint32 X10 = (xPix + dir) & 0x3ff; \
2687 uint32 Y01 = (yPix + dir) & 0x3ff; \
2688 uint8 *TileData10 = VRAM1 + (Memory.VRAM[((Y & ~7) << 5) + ((X10 >> 2) & ~1)] << 7); \
2689 uint8 *TileData11 = VRAM1 + (Memory.VRAM[((Y01 & ~7) << 5) + ((X10 >> 2) & ~1)] << 7); \
2690 uint8 *TileData01 = VRAM1 + (Memory.VRAM[((Y01 & ~7) << 5) + ((X >> 2) & ~1)] << 7); \
2691 uint32 p1 = COLORFUNC; \
2692 p1 = (p1 & FIRST_THIRD_COLOR_MASK) | ((p1 & SECOND_COLOR_MASK) << 16); \
2693 b = *(TileData10 + ((Y & 7) << 4) + ((X10 & 7) << 1)); \
2694 uint32 p2 = COLORFUNC; \
2695 p2 = (p2 & FIRST_THIRD_COLOR_MASK) | ((p2 & SECOND_COLOR_MASK) << 16); \
2696 b = *(TileData11 + ((Y01 & 7) << 4) + ((X10 & 7) << 1)); \
2697 uint32 p4 = COLORFUNC; \
2698 p4 = (p4 & FIRST_THIRD_COLOR_MASK) | ((p4 & SECOND_COLOR_MASK) << 16); \
2699 b = *(TileData01 + ((Y01 & 7) << 4) + ((X & 7) << 1)); \
2700 uint32 p3 = COLORFUNC; \
2701 p3 = (p3 & FIRST_THIRD_COLOR_MASK) | ((p3 & SECOND_COLOR_MASK) << 16); \
2702 /* Xdel, Ydel: position (in 1/32nds) between the points */ \
2703 uint32 Xdel = (xPos >> 3) & 0x1F; \
2704 uint32 Ydel = (yPos >> 3) & 0x1F; \
2705 uint32 XY = (Xdel*Ydel) >> 5; \
2706 uint32 area1 = 0x20 + XY - Xdel - Ydel; \
2707 uint32 area2 = Xdel - XY; \
2708 uint32 area3 = Ydel - XY; \
2709 uint32 area4 = XY; \
2710 uint32 tempColor = ((area1 * p1) + \
2713 (area4 * p4)) >> 5; \
2714 TYPE theColor = (tempColor & FIRST_THIRD_COLOR_MASK) | ((tempColor >> 16) & SECOND_COLOR_MASK); \
2715 *p = (FUNC) | ALPHA_BITS_MASK; \
2721 if (PPU.Mode7Repeat == 3) \
2723 X = (x + HOffset) & 7; \
2724 Y = (yy + CentreY) & 7; \
2725 uint32 b = *(VRAM1 + ((Y & 7) << 4) + ((X & 7) << 1)); \
2726 GFX.Z1 = Mode7Depths [(b & GFX.Mode7PriorityMask) >> 7]; \
2727 if (GFX.Z1 > *d && b) \
2729 TYPE theColor = COLORFUNC; \
2730 *p = (FUNC) | ALPHA_BITS_MASK; \
2740 STATIC uint32 Q_INTERPOLATE(uint32 A, uint32 B, uint32 C, uint32 D)
2742 register uint32 x = ((A >> 2) & HIGH_BITS_SHIFTED_TWO_MASK) +
2743 ((B >> 2) & HIGH_BITS_SHIFTED_TWO_MASK) +
2744 ((C >> 2) & HIGH_BITS_SHIFTED_TWO_MASK) +
2745 ((D >> 2) & HIGH_BITS_SHIFTED_TWO_MASK);
2746 register uint32 y = (A & TWO_LOW_BITS_MASK) +
2747 (B & TWO_LOW_BITS_MASK) +
2748 (C & TWO_LOW_BITS_MASK) +
2749 (D & TWO_LOW_BITS_MASK);
2750 y = (y>>2) & TWO_LOW_BITS_MASK;
2754 void DrawBGMode7Background16_i (uint8 *Screen, int bg)
2756 RENDER_BACKGROUND_MODE7_i (uint16, theColor, (GFX.ScreenColors[b & GFX.Mode7Mask]));
2759 void DrawBGMode7Background16Add_i (uint8 *Screen, int bg)
2761 RENDER_BACKGROUND_MODE7_i (uint16, *(d + GFX.DepthDelta) ?
2762 (*(d + GFX.DepthDelta) != 1 ?
2763 (COLOR_ADD (theColor,
2765 (COLOR_ADD (theColor,
2766 GFX.FixedColour))) :
2767 theColor, (GFX.ScreenColors[b & GFX.Mode7Mask]));
2770 void DrawBGMode7Background16Add1_2_i (uint8 *Screen, int bg)
2772 RENDER_BACKGROUND_MODE7_i (uint16, *(d + GFX.DepthDelta) ?
2773 (*(d + GFX.DepthDelta) != 1 ?
2774 COLOR_ADD1_2 (theColor,
2776 COLOR_ADD (theColor,
2778 theColor, (GFX.ScreenColors[b & GFX.Mode7Mask]));
2781 void DrawBGMode7Background16Sub_i (uint8 *Screen, int bg)
2783 RENDER_BACKGROUND_MODE7_i (uint16, *(d + GFX.DepthDelta) ?
2784 (*(d + GFX.DepthDelta) != 1 ?
2785 COLOR_SUB (theColor,
2787 COLOR_SUB (theColor,
2789 theColor, (GFX.ScreenColors[b & GFX.Mode7Mask]));
2792 void DrawBGMode7Background16Sub1_2_i (uint8 *Screen, int bg)
2794 RENDER_BACKGROUND_MODE7_i (uint16, *(d + GFX.DepthDelta) ?
2795 (*(d + GFX.DepthDelta) != 1 ?
2796 COLOR_SUB1_2 (theColor,
2798 COLOR_SUB (theColor,
2800 theColor, (GFX.ScreenColors[b & GFX.Mode7Mask]));
2803 #define _BUILD_SETUP(F) \
2804 GFX.BuildPixel = BuildPixel##F; \
2805 GFX.BuildPixel2 = BuildPixel2##F; \
2806 GFX.DecomposePixel = DecomposePixel##F; \
2807 RED_LOW_BIT_MASK = RED_LOW_BIT_MASK_##F; \
2808 GREEN_LOW_BIT_MASK = GREEN_LOW_BIT_MASK_##F; \
2809 BLUE_LOW_BIT_MASK = BLUE_LOW_BIT_MASK_##F; \
2810 RED_HI_BIT_MASK = RED_HI_BIT_MASK_##F; \
2811 GREEN_HI_BIT_MASK = GREEN_HI_BIT_MASK_##F; \
2812 BLUE_HI_BIT_MASK = BLUE_HI_BIT_MASK_##F; \
2813 MAX_RED = MAX_RED_##F; \
2814 MAX_GREEN = MAX_GREEN_##F; \
2815 MAX_BLUE = MAX_BLUE_##F; \
2816 GREEN_HI_BIT = ((MAX_GREEN_##F + 1) >> 1); \
2817 SPARE_RGB_BIT_MASK = SPARE_RGB_BIT_MASK_##F; \
2818 RGB_LOW_BITS_MASK = (RED_LOW_BIT_MASK_##F | \
2819 GREEN_LOW_BIT_MASK_##F | \
2820 BLUE_LOW_BIT_MASK_##F); \
2821 RGB_HI_BITS_MASK = (RED_HI_BIT_MASK_##F | \
2822 GREEN_HI_BIT_MASK_##F | \
2823 BLUE_HI_BIT_MASK_##F); \
2824 RGB_HI_BITS_MASKx2 = ((RED_HI_BIT_MASK_##F | \
2825 GREEN_HI_BIT_MASK_##F | \
2826 BLUE_HI_BIT_MASK_##F) << 1); \
2827 RGB_REMOVE_LOW_BITS_MASK = ~RGB_LOW_BITS_MASK; \
2828 FIRST_COLOR_MASK = FIRST_COLOR_MASK_##F; \
2829 SECOND_COLOR_MASK = SECOND_COLOR_MASK_##F; \
2830 THIRD_COLOR_MASK = THIRD_COLOR_MASK_##F; \
2831 ALPHA_BITS_MASK = ALPHA_BITS_MASK_##F; \
2832 FIRST_THIRD_COLOR_MASK = FIRST_COLOR_MASK | THIRD_COLOR_MASK; \
2833 TWO_LOW_BITS_MASK = RGB_LOW_BITS_MASK | (RGB_LOW_BITS_MASK << 1); \
2834 HIGH_BITS_SHIFTED_TWO_MASK = (( (FIRST_COLOR_MASK | SECOND_COLOR_MASK | THIRD_COLOR_MASK) & \
2835 ~TWO_LOW_BITS_MASK ) >> 2);
2837 void RenderScreen (uint8 *Screen, bool8_32 sub, bool8_32 force_no_add, uint8 D)
2849 GFX.pCurrentClip = &IPPU.Clip [0];
2850 BG0 = ON_MAIN (0) && !(Settings.os9x_hack & GFX_IGNORE_BG0);
2851 BG1 = ON_MAIN (1) && !(Settings.os9x_hack & GFX_IGNORE_BG1);
2852 BG2 = ON_MAIN (2) && !(Settings.os9x_hack & GFX_IGNORE_BG2);
2853 BG3 = ON_MAIN (3) && !(Settings.os9x_hack & GFX_IGNORE_BG3);
2854 OB = ON_MAIN (4) && !(Settings.os9x_hack & GFX_IGNORE_OBJ);
2858 GFX.pCurrentClip = &IPPU.Clip [1];
2859 BG0 = ON_SUB (0) && !(Settings.os9x_hack & GFX_IGNORE_BG0);
2860 BG1 = ON_SUB (1) && !(Settings.os9x_hack & GFX_IGNORE_BG1);
2861 BG2 = ON_SUB (2) && !(Settings.os9x_hack & GFX_IGNORE_BG2);
2862 BG3 = ON_SUB (3) && !(Settings.os9x_hack & GFX_IGNORE_BG3);
2863 OB = ON_SUB (4) && !(Settings.os9x_hack & GFX_IGNORE_OBJ);
2866 sub |= force_no_add;
2868 if (PPU.BGMode <= 1)
2872 SelectTileRenderer (sub || !SUB_OR_ADD(4));
2877 SelectTileRenderer (sub || !SUB_OR_ADD(0));
2878 DrawBackground (PPU.BGMode, 0, D + 10, D + 14);
2882 SelectTileRenderer (sub || !SUB_OR_ADD(1));
2883 DrawBackground (PPU.BGMode, 1, D + 9, D + 13);
2887 SelectTileRenderer (sub || !SUB_OR_ADD(2));
2888 DrawBackground (PPU.BGMode, 2, D + 3,
2889 (Memory.FillRAM [0x2105] & 8) == 0 ? D + 6 : D + 17);
2891 if (BG3 && PPU.BGMode == 0)
2893 SelectTileRenderer (sub || !SUB_OR_ADD(3));
2894 DrawBackground (PPU.BGMode, 3, D + 2, D + 5);
2897 else if (PPU.BGMode != 7)
2901 SelectTileRenderer (sub || !SUB_OR_ADD(4));
2906 SelectTileRenderer (sub || !SUB_OR_ADD(0));
2907 DrawBackground (PPU.BGMode, 0, D + 5, D + 13);
2909 if (PPU.BGMode != 6 && BG1)
2911 SelectTileRenderer (sub || !SUB_OR_ADD(1));
2912 DrawBackground (PPU.BGMode, 1, D + 2, D + 9);
2919 SelectTileRenderer (sub || !SUB_OR_ADD(4));
2922 if (BG0 || ((Memory.FillRAM [0x2133] & 0x40) && BG1))
2926 if (Memory.FillRAM [0x2133] & 0x40)
2928 GFX.Mode7Mask = 0x7f;
2929 GFX.Mode7PriorityMask = 0x80;
2930 Mode7Depths [0] = 5 + D;
2931 Mode7Depths [1] = 9 + D;
2936 GFX.Mode7Mask = 0xff;
2937 GFX.Mode7PriorityMask = 0;
2938 Mode7Depths [0] = 5 + D;
2939 Mode7Depths [1] = 5 + D;
2942 if (sub || !SUB_OR_ADD(0))
2944 if (!Settings.Mode7Interpolate)
2945 DrawBGMode7Background16 (Screen, bg);
2947 DrawBGMode7Background16_i (Screen, bg);
2951 if (GFX.r2131 & 0x80)
2953 if (GFX.r2131 & 0x40)
2955 if (!Settings.Mode7Interpolate)
2956 DrawBGMode7Background16Sub1_2 (Screen, bg);
2958 DrawBGMode7Background16Sub1_2_i (Screen, bg);
2962 if (!Settings.Mode7Interpolate)
2963 DrawBGMode7Background16Sub (Screen, bg);
2965 DrawBGMode7Background16Sub_i (Screen, bg);
2970 if (GFX.r2131 & 0x40)
2972 if (!Settings.Mode7Interpolate)
2973 DrawBGMode7Background16Add1_2 (Screen, bg);
2975 DrawBGMode7Background16Add1_2_i (Screen, bg);
2979 if (!Settings.Mode7Interpolate)
2980 DrawBGMode7Background16Add (Screen, bg);
2982 DrawBGMode7Background16Add_i (Screen, bg);
2992 void DisplayChar (uint8 *Screen, uint8 c)
2994 int line = (((c & 0x7f) - 32) >> 4) * font_height;
2995 int offset = (((c & 0x7f) - 32) & 15) * font_width;
2997 if (Settings.SixteenBit)
3001 uint16 *s = (uint16 *) Screen;
3002 for (h = 0; h < font_height; h++, line++,
3003 s += GFX.PPL - font_width)
3005 for (w = 0; w < font_width; w++, s++)
3007 uint8 p = font [line][offset + w];
3022 for (h = 0; h < font_height; h++, line++,
3023 s += GFX.PPL - font_width)
3025 for (w = 0; w < font_width; w++, s++)
3027 uint8 p = font [line][offset + w];
3040 static void S9xDisplayFrameRate ()
3042 uint8 *Screen = GFX.Screen + 2 +
3043 (IPPU.RenderedScreenHeight - font_height - 1) * GFX.Pitch2;
3047 sprintf (string, "%02d/%02d", IPPU.DisplayedRenderedFrameCount,
3048 (int) Memory.ROMFramesPerSecond);
3052 Screen += (font_width - 1) * sizeof(uint16);
3054 for (i = 0; i < len; i++)
3056 DisplayChar (Screen, string [i]);
3057 Screen += Settings.SixteenBit ? (font_width - 1) * sizeof (uint16) :
3062 static void S9xDisplayString (const char *string)
3064 uint8 *Screen = GFX.Screen + 2 +
3065 (IPPU.RenderedScreenHeight - font_height * 5) * GFX.Pitch2;
3066 int len = strlen (string);
3067 int max_chars = IPPU.RenderedScreenWidth / (font_width - 1);
3071 for (i = 0; i < len; i++, char_count++)
3073 if (char_count >= max_chars || string [i] < 32)
3075 Screen -= Settings.SixteenBit ?
3076 (font_width - 1) * sizeof (uint16) * max_chars :
3077 (font_width - 1) * max_chars;
3078 Screen += font_height * GFX.Pitch;
3079 if (Screen >= GFX.Screen + GFX.Pitch * IPPU.RenderedScreenHeight)
3081 char_count -= max_chars;
3083 if (string [i] < 32)
3085 DisplayChar (Screen, string [i]);
3086 Screen += Settings.SixteenBit ? (font_width - 1) * sizeof (uint16) :
3091 void S9xUpdateScreen () // ~30-50ms! (called from FLUSH_REDRAW())
3097 unsigned char *memoryfillram = Memory.FillRAM;
3099 // get local copies of vid registers to be used later
3100 GFX.r2131 = memoryfillram [0x2131]; // ADDITION/SUBTRACTION & SUBTRACTION DESIGNATION FOR EACH SCREEN
3101 GFX.r212c = memoryfillram [0x212c]; // MAIN SCREEN, DESIGNATION - used to enable BGS
3102 GFX.r212d = memoryfillram [0x212d]; // SUB SCREEN DESIGNATION - used to enable sub BGS
3103 GFX.r2130 = memoryfillram [0x2130]; // INITIAL SETTINGS FOR FIXED COLOR ADDITION OR SCREEN ADDITION
3105 // If external sync is off and
3106 // main screens have not been configured the same as the sub screen and
3107 // color addition and subtraction has been diabled then
3109 // anything else it is 0
3110 GFX.Pseudo = (memoryfillram [0x2133] & 8) != 0 && // Use EXTERNAL SYNCHRONIZATION?
3111 (GFX.r212c & 15) != (GFX.r212d & 15) && // Are the main screens different from the sub screens?
3112 (GFX.r2131 & 0x3f) == 0; // Is colour data addition/subtraction disabled on all BGS?
3114 // If sprite data has been changed then go through and
3115 // refresh the sprites.
3116 if (IPPU.OBJChanged)
3121 if (PPU.RecomputeClipWindows)
3123 ComputeClipWindows ();
3124 PPU.RecomputeClipWindows = FALSE;
3127 GFX.StartY = IPPU.PreviousLine;
3128 if ((GFX.EndY = IPPU.CurrentLine - 1) >= PPU.ScreenHeight)
3129 GFX.EndY = PPU.ScreenHeight - 1;
3131 uint32 starty = GFX.StartY;
3132 uint32 endy = GFX.EndY;
3134 #ifndef RC_OPTIMIZED
3135 if (Settings.SupportHiRes &&
3136 (PPU.BGMode == 5 || PPU.BGMode == 6 || IPPU.LatchedInterlace))
3138 if (PPU.BGMode == 5 || PPU.BGMode == 6)
3140 IPPU.RenderedScreenWidth = 512;
3143 if (IPPU.LatchedInterlace)
3145 starty = GFX.StartY * 2;
3146 endy = GFX.EndY * 2 + 1;
3148 if (!IPPU.DoubleWidthPixels)
3150 // The game has switched from lo-res to hi-res mode part way down
3151 // the screen. Scale any existing lo-res pixels on screen
3153 if (Settings.SixteenBit)
3156 #if defined (USE_GLIDE) || defined (USE_OPENGL)
3159 (Settings.GlideEnable && GFX.Pitch == 512) ||
3162 (Settings.OpenGLEnable && GFX.Pitch == 512) ||
3166 // Have to back out of the speed up hack where the low res.
3167 // SNES image was rendered into a 256x239 sized buffer,
3168 // ignoring the true, larger size of the buffer.
3170 for (register int32 y = (int32) GFX.StartY - 1; y >= 0; y--)
3172 register uint16 *p = (uint16 *) (GFX.Screen + y * GFX.Pitch) + 255;
3173 register uint16 *q = (uint16 *) (GFX.Screen + y * GFX.RealPitch) + 510;
3174 for (register int x = 255; x >= 0; x--, p--, q -= 2)
3177 GFX.Pitch = GFX.Pitch2 = GFX.RealPitch;
3178 GFX.PPL = GFX.Pitch >> 1;
3179 GFX.PPLx2 = GFX.Pitch;
3180 GFX.ZPitch = GFX.PPL;
3184 for (register uint32 y = 0; y < GFX.StartY; y++)
3186 register uint16 *p = (uint16 *) (GFX.Screen + y * GFX.Pitch) + 255;
3187 register uint16 *q = (uint16 *) (GFX.Screen + y * GFX.Pitch) + 510;
3188 for (register int x = 255; x >= 0; x--, p--, q -= 2)
3195 for (register uint32 y = 0; y < GFX.StartY; y++)
3197 register uint8 *p = GFX.Screen + y * GFX.Pitch + 255;
3198 register uint8 *q = GFX.Screen + y * GFX.Pitch + 510;
3199 for (register int x = 255; x >= 0; x--, p--, q -= 2)
3204 IPPU.DoubleWidthPixels = TRUE;
3207 #endif //RC_OPTIMIZED (DONT DO ABOVE)
3209 uint32 black = BLACK | (BLACK << 16);
3211 // Are we worrying about transparencies?
3212 if (Settings.Transparency && Settings.SixteenBit)
3216 GFX.r2131 = 0x5f; //0101 1111 - enable addition/subtraction on all BGS and sprites and "1/2 OF COLOR DATA" DESIGNATION
3217 GFX.r212d = (Memory.FillRAM [0x212c] ^ // any BGS which are set as main and as sub then switch off the sub
3218 Memory.FillRAM [0x212d]) & 15;
3219 GFX.r212c &= ~GFX.r212d; // make sure the main BG reg is the reverse of the sub BG reg
3220 GFX.r2130 |= 2; // enable ADDITION/SUBTRACTION FOR SUB SCREEN
3223 // Check to see if any transparency effects are currently in use
3224 if (!PPU.ForcedBlanking && ADD_OR_SUB_ON_ANYTHING &&
3225 (GFX.r2130 & 0x30) != 0x30 &&
3226 !((GFX.r2130 & 0x30) == 0x10 && IPPU.Clip[1].Count[5] == 0))
3228 // transparency effects in use, so lets get busy!
3229 struct ClipData *pClip;
3231 GFX.FixedColour = BUILD_PIXEL (IPPU.XB [PPU.FixedColourRed],
3232 IPPU.XB [PPU.FixedColourGreen],
3233 IPPU.XB [PPU.FixedColourBlue]);
3234 fixedColour = (GFX.FixedColour<<16|GFX.FixedColour);
3235 // Clear the z-buffer, marking areas 'covered' by the fixed
3236 // colour as depth 1.
3237 pClip = &IPPU.Clip [1];
3239 // Clear the z-buffer
3241 if (pClip->Count [5])
3244 // Colour window enabled.
3247 for (uint32 y = starty; y <= endy; y++)
3250 ZeroMemory (GFX.SubZBuffer + y * GFX.ZPitch,
3251 IPPU.RenderedScreenWidth);
3252 ZeroMemory (GFX.ZBuffer + y * GFX.ZPitch,
3253 IPPU.RenderedScreenWidth);
3255 if (IPPU.Clip [0].Count [5])
3257 memset ((GFX.SubScreen + y * GFX.Pitch2), black, IPPU.RenderedScreenWidth);
3259 for (uint32 c = 0; c < pClip->Count [5]; c++)
3261 if (pClip->Right [c][5] > pClip->Left [c][5])
3263 memset (GFX.SubZBuffer + y * GFX.ZPitch + pClip->Left [c][5] * x2,
3264 1, (pClip->Right [c][5] - pClip->Left [c][5]) * x2);
3265 if (IPPU.Clip [0].Count [5])
3267 // Blast, have to clear the sub-screen to the fixed-colour
3268 // because there is a colour window in effect clipping
3269 // the main screen that will allow the sub-screen
3270 // 'underneath' to show through.
3271 memset ((GFX.SubScreen + y * GFX.Pitch2) + pClip->Left [c][5] * x2,
3273 pClip->Right[c][5]*x2 - pClip->Left [c][5] * x2);
3279 #else // NOT RC_OPTIMIZED
3280 // loop around all of the lines being updated
3281 for (uint32 y = starty; y <= endy; y++)
3283 // Clear the subZbuffer
3284 memset32 ((uint32_t*)(GFX.SubZBuffer + y * GFX.ZPitch),0,
3285 IPPU.RenderedScreenWidth>>2);
3286 // Clear the Zbuffer
3287 memset32 ((uint32_t*)(GFX.ZBuffer + y * GFX.ZPitch),0,
3288 IPPU.RenderedScreenWidth>>2);
3290 // if there is clipping then clear subscreen to a black color
3291 if (IPPU.Clip [0].Count [5])
3293 memset32 ((uint32_t*)(GFX.SubScreen + y * GFX.Pitch2), black, IPPU.RenderedScreenWidth>>1);
3296 // loop through all window clippings
3297 for (uint32 c = 0; c < pClip->Count [5]; c++)
3299 if (pClip->Right [c][5] > pClip->Left [c][5])
3301 memset (GFX.SubZBuffer + y * GFX.ZPitch + pClip->Left [c][5] * x2,
3302 1, (pClip->Right [c][5] - pClip->Left [c][5]) * x2);
3303 if (IPPU.Clip [0].Count [5])
3305 // Blast, have to clear the sub-screen to the fixed-colour
3306 // because there is a colour window in effect clipping
3307 // the main screen that will allow the sub-screen
3308 // 'underneath' to show through.
3310 register uint16 *p = (uint16 *) (GFX.SubScreen + y * GFX.Pitch2);
3311 register uint16 *q = p + pClip->Right [c][5] * x2;
3312 p += pClip->Left [c][5] * x2;
3315 *p++ = (uint16) GFX.FixedColour;
3321 //#undef RC_OPTIMIZED
3326 // No windows are clipping the main screen
3327 // this simplifies the screen clearing process
3330 if (GFX.ZPitch == (uint32)IPPU.RenderedScreenWidth)
3333 memset (GFX.ZBuffer + starty * GFX.ZPitch, 0, GFX.ZPitch * (endy - starty - 1));
3334 memset (GFX.SubZBuffer + starty * GFX.ZPitch, 1, GFX.ZPitch * (endy - starty - 1));
3338 for (uint32 y = starty; y <= endy; y++)
3340 ZeroMemory (GFX.ZBuffer + y * GFX.ZPitch,
3341 IPPU.RenderedScreenWidth);
3342 memset (GFX.SubZBuffer + y * GFX.ZPitch, 1,
3343 IPPU.RenderedScreenWidth);
3347 if (IPPU.Clip [0].Count [5])
3349 // Blast, have to clear the sub-screen to the fixed-colour
3350 // because there is a colour window in effect clipping
3351 // the main screen that will allow the sub-screen
3352 // 'underneath' to show through.
3353 if (GFX.Pitch2 == (uint32)IPPU.RenderedScreenWidth)
3355 memset ((GFX.SubScreen + starty * GFX.Pitch2),
3356 GFX.FixedColour | (GFX.FixedColour << 16),
3357 GFX.Pitch2 * (endy - starty - 1));
3361 for (uint32 y = starty; y <= endy; y++)
3363 memset ((GFX.SubScreen + y * GFX.Pitch2),
3364 GFX.FixedColour | (GFX.FixedColour << 16),
3365 IPPU.RenderedScreenWidth);
3370 #else // NOT RC_OPTIMIZED
3371 // loop through all of the lines to be updated
3372 for (uint32 y = starty; y <= endy; y++)
3374 // Clear the Zbuffer
3375 memset32 ((uint32_t*)(GFX.ZBuffer + y * GFX.ZPitch),0,
3376 IPPU.RenderedScreenWidth>>2);
3377 // clear the sub Zbuffer to 1
3378 memset32 ((uint32_t*)(GFX.SubZBuffer + y * GFX.ZPitch), 0x01010101,
3379 IPPU.RenderedScreenWidth>>2);
3380 if (IPPU.Clip [0].Count [5])
3382 // Blast, have to clear the sub-screen to the fixed-colour
3383 // because there is a colour window in effect clipping
3384 // the main screen that will allow the sub-screen
3385 // 'underneath' to show through.
3388 memset32 ((uint32_t*)(GFX.SubScreen + y * GFX.Pitch2), fixedColour,
3389 IPPU.RenderedScreenWidth>>1);
3396 if (ANYTHING_ON_SUB)
3398 GFX.DB = GFX.SubZBuffer;
3399 RenderScreen (GFX.SubScreen, TRUE, TRUE, SUB_SCREEN_DEPTH);
3402 if (IPPU.Clip [0].Count [5])
3404 for (uint32 y = starty; y <= endy; y++)
3406 register uint16 *p = (uint16 *) (GFX.Screen + y * GFX.Pitch2);
3407 register uint8 *d = GFX.SubZBuffer + y * GFX.ZPitch ;
3408 register uint8 *e = d + SNES_WIDTH;
3413 *p = *(p + GFX.Delta);
3422 GFX.DB = GFX.ZBuffer;
3423 RenderScreen (GFX.Screen, FALSE, FALSE, MAIN_SCREEN_DEPTH);
3426 uint32 back = IPPU.ScreenColors [0];
3431 pClip = &IPPU.Clip [0];
3433 for (uint32 y = starty; y <= endy; y++)
3435 if (!(Count = pClip->Count [5]))
3442 for (uint32 b = 0; b < Count; b++)
3444 if (pClip->Count [5])
3446 Left = pClip->Left [b][5] * x2;
3447 Right = pClip->Right [b][5] * x2;
3452 if (GFX.r2131 & 0x80)
3454 if (GFX.r2131 & 0x40)
3456 // Subtract, halving the result.
3457 register uint16 *p = (uint16 *) (GFX.Screen + y * GFX.Pitch2) + Left;
3458 register uint8 *d = GFX.ZBuffer + y * GFX.ZPitch;
3459 register uint8 *s = GFX.SubZBuffer + y * GFX.ZPitch + Left;
3460 register uint8 *e = d + Right;
3461 uint16 back_fixed = COLOR_SUB (back, GFX.FixedColour);
3471 *p = COLOR_SUB1_2 (back, *(p + GFX.Delta));
3486 register uint16 *p = (uint16 *) (GFX.Screen + y * GFX.Pitch2) + Left;
3487 register uint8 *s = GFX.SubZBuffer + y * GFX.ZPitch + Left;
3488 register uint8 *d = GFX.ZBuffer + y * GFX.ZPitch;
3489 register uint8 *e = d + Right;
3490 uint16 back_fixed = COLOR_SUB (back, GFX.FixedColour);
3500 *p = COLOR_SUB (back, *(p + GFX.Delta));
3514 if (GFX.r2131 & 0x40)
3516 register uint16 *p = (uint16 *) (GFX.Screen + y * GFX.Pitch2) + Left;
3517 register uint8 *d = GFX.ZBuffer + y * GFX.ZPitch;
3518 register uint8 *s = GFX.SubZBuffer + y * GFX.ZPitch + Left;
3519 register uint8 *e = d + Right;
3520 uint16 back_fixed = COLOR_ADD (back, GFX.FixedColour);
3529 *p = COLOR_ADD1_2 (back, *(p + GFX.Delta));
3544 register uint16 *p = (uint16 *) (GFX.Screen + y * GFX.Pitch2) + Left;
3545 register uint8 *d = GFX.ZBuffer + y * GFX.ZPitch;
3546 register uint8 *s = GFX.SubZBuffer + y * GFX.ZPitch + Left;
3547 register uint8 *e = d + Right;
3548 uint16 back_fixed = COLOR_ADD (back, GFX.FixedColour);
3557 *p = COLOR_ADD (back, *(p + GFX.Delta));
3571 if (!pClip->Count [5])
3573 // The backdrop has not been cleared yet - so
3574 // copy the sub-screen to the main screen
3575 // or fill it with the back-drop colour if the
3576 // sub-screen is clear.
3577 register uint16 *p = (uint16 *) (GFX.Screen + y * GFX.Pitch2) + Left;
3578 register uint8 *d = GFX.ZBuffer + y * GFX.ZPitch;
3579 register uint8 *s = GFX.SubZBuffer + y * GFX.ZPitch + Left;
3580 register uint8 *e = d + Right;
3589 *p = *(p + GFX.Delta);
3591 *p = GFX.FixedColour;
3608 // Subscreen not being added to back
3609 uint32 back = IPPU.ScreenColors [0] | (IPPU.ScreenColors [0] << 16);
3610 pClip = &IPPU.Clip [0];
3612 if (pClip->Count [5])
3614 for (uint32 y = starty; y <= endy; y++)
3616 for (uint32 b = 0; b < pClip->Count [5]; b++)
3618 uint32 Left = pClip->Left [b][5] * x2;
3619 uint32 Right = pClip->Right [b][5] * x2;
3620 register uint16 *p = (uint16 *) (GFX.Screen + y * GFX.Pitch2) + Left;
3621 register uint8 *d = GFX.ZBuffer + y * GFX.ZPitch;
3622 register uint8 *e = d + Right;
3636 for (uint32 y = starty; y <= endy; y++)
3638 register uint16 *p = (uint16 *) (GFX.Screen + y * GFX.Pitch2);
3639 register uint8 *d = GFX.ZBuffer + y * GFX.ZPitch;
3640 register uint8 *e = d + 256 * x2;
3660 // 16bit and transparency but currently no transparency effects in
3663 // get the back colour of the current screen
3664 uint32 back = IPPU.ScreenColors [0] |
3665 (IPPU.ScreenColors [0] << 16);
3667 // if forceblanking in use then use black instead of the back color
3668 if (PPU.ForcedBlanking)
3671 // not sure what Clip is used for yet
3672 // could be a check to see if there is any clipping present?
3673 if (IPPU.Clip [0].Count[5])
3677 if (GFX.Pitch2 == (uint32)IPPU.RenderedScreenWidth)
3679 memset (GFX.Screen + starty * GFX.Pitch2, black,
3680 GFX.Pitch2 * (endy - starty - 1));
3684 for (uint32 y = starty; y <= endy; y++)
3686 memset (GFX.Screen + y * GFX.Pitch2, black,
3690 for (uint32 y = starty; y <= endy; y++)
3692 for (uint32 c = 0; c < IPPU.Clip [0].Count [5]; c++)
3694 if (IPPU.Clip [0].Right [c][5] > IPPU.Clip [0].Left [c][5])
3697 memset ((GFX.Screen + y * GFX.Pitch2) + IPPU.Clip [0].Left [c][5] * x2,
3699 IPPU.Clip [0].Right [c][5] * x2 - IPPU.Clip [0].Left [c][5] * x2);
3704 // loop through all of the lines that are going to be updated as part of this screen update
3705 for (uint32 y = starty; y <= endy; y++)
3707 memset32 ((uint32_t*)(GFX.Screen + y * GFX.Pitch2), black,
3708 IPPU.RenderedScreenWidth>>1);
3712 for (uint32 c = 0; c < IPPU.Clip [0].Count [5]; c++)
3714 if (IPPU.Clip [0].Right [c][5] > IPPU.Clip [0].Left [c][5])
3716 register uint16 *p = (uint16 *) (GFX.Screen + y * GFX.Pitch2); // get pointer to current line in screen buffer
3717 register uint16 *q = p + IPPU.Clip [0].Right [c][5] * x2; // get pointer to end of line
3718 p += IPPU.Clip [0].Left [c][5] * x2;
3721 *p++ = (uint16) back; // fill all pixels in clipped section with the back colour
3731 if (GFX.Pitch2 == (uint32)IPPU.RenderedScreenWidth)
3733 memset (GFX.Screen + starty * GFX.Pitch2, back,
3734 GFX.Pitch2 * (endy - starty - 1));
3738 for (uint32 y = starty; y <= endy; y++)
3740 memset (GFX.Screen + y * GFX.Pitch2, back,
3745 // there is no clipping to worry about so just fill with the back colour
3746 for (uint32 y = starty; y <= endy; y++)
3748 memset32 ((uint32_t*)(GFX.Screen + y * GFX.Pitch2), back,
3749 IPPU.RenderedScreenWidth>>1);
3754 // If Forced blanking is not in effect
3755 if (!PPU.ForcedBlanking)
3758 if (GFX.ZPitch == (uint32)IPPU.RenderedScreenWidth)
3760 memset (GFX.ZBuffer + starty * GFX.ZPitch, 0,
3761 GFX.ZPitch * (endy - starty - 1));
3765 for (uint32 y = starty; y <= endy; y++)
3767 memset (GFX.ZBuffer + y * GFX.ZPitch, 0,
3772 // Clear the Zbuffer for each of the lines which are going to be updated
3773 for (uint32 y = starty; y <= endy; y++)
3775 memset32 ((uint32_t*)(GFX.ZBuffer + y * GFX.ZPitch), 0,
3779 GFX.DB = GFX.ZBuffer; // save pointer to Zbuffer in GFX object
3780 RenderScreen (GFX.Screen, FALSE, TRUE, SUB_SCREEN_DEPTH);
3784 else // Transparencys are disabled, ahh lovely ... nice and easy.
3787 if (Settings.SixteenBit)
3790 // get back colour to be used in clearing the screen
3791 register uint32 back;
3792 if (!(Memory.FillRAM [0x2131] & 0x80) &&(Memory.FillRAM[0x2131] & 0x20) &&
3793 (PPU.FixedColourRed || PPU.FixedColourGreen || PPU.FixedColourBlue))
3795 back = (IPPU.XB[PPU.FixedColourRed]<<11) |
3796 (IPPU.XB[PPU.FixedColourGreen] << 6) |
3797 (IPPU.XB[PPU.FixedColourBlue] << 1) | 1;
3798 back = (back << 16) | back;
3802 back = IPPU.ScreenColors [0] | (IPPU.ScreenColors [0] << 16);
3805 // if Forcedblanking in use then back colour becomes black
3806 if (PPU.ForcedBlanking)
3810 SelectTileRenderer (TRUE); //selects the tile renderers to be used
3811 // TRUE means to use the default
3812 // FALSE means use best renderer based on current
3813 // graphics register settings
3816 // now clear all graphics lines which are being updated using the back colour
3817 for (register uint32 y = starty; y <= endy; y++)
3819 memset32 ((uint32_t*)(GFX.Screen + y * GFX.Pitch2), back,
3820 IPPU.RenderedScreenWidth>>1);
3824 else // Settings.SixteenBit == false
3826 // because we are in 8 bit we can just use 0 to clear the screen
3827 // this means we can use the Zero Memory function
3829 // Loop through all lines being updated and clear the pixels to 0
3830 for (uint32 y = starty; y <= endy; y++)
3832 ZeroMemory (GFX.Screen + y * GFX.Pitch2,
3833 IPPU.RenderedScreenWidth);
3837 if (!PPU.ForcedBlanking)
3839 // Loop through all lines being updated and clear the
3840 // zbuffer for each of the lines
3841 for (uint32 y = starty; y <= endy; y++)
3843 memset32 ((uint32_t*)(GFX.ZBuffer + y * GFX.ZPitch), 0,
3844 IPPU.RenderedScreenWidth>>2);
3846 GFX.DB = GFX.ZBuffer; // save pointer to Zbuffer in GFX object
3847 GFX.pCurrentClip = &IPPU.Clip [0];
3849 // Define an inline function to handle clipping
3850 #define FIXCLIP(n) \
3851 if (GFX.r212c & (1 << (n))) \
3852 GFX.pCurrentClip = &IPPU.Clip [0]; \
3854 GFX.pCurrentClip = &IPPU.Clip [1]
3856 // Define an inline function to handle which BGs are being displayed
3857 #define DISPLAY(n) \
3858 (!(PPU.BG_Forced & n) && \
3859 (GFX.r212c & n) || \
3860 ((GFX.r212d & n) && subadd))
3862 uint8 subadd = GFX.r2131 & 0x3f;
3864 // go through all BGS are check if they need to be displayed
3865 bool8_32 BG0 = DISPLAY(1) && !(Settings.os9x_hack & GFX_IGNORE_BG0);
3866 bool8_32 BG1 = DISPLAY(2) && !(Settings.os9x_hack & GFX_IGNORE_BG1);
3867 bool8_32 BG2 = DISPLAY(4) && !(Settings.os9x_hack & GFX_IGNORE_BG2);
3868 bool8_32 BG3 = DISPLAY(8) && !(Settings.os9x_hack & GFX_IGNORE_BG3);
3869 bool8_32 OB = DISPLAY(16) && !(Settings.os9x_hack & GFX_IGNORE_OBJ);
3871 if (PPU.BGMode <= 1)
3873 // screen modes 0 and 1
3882 DrawBackground (PPU.BGMode, 0, 10, 14);
3887 DrawBackground (PPU.BGMode, 1, 9, 13);
3892 DrawBackground (PPU.BGMode, 2, 3,
3893 (Memory.FillRAM [0x2105] & 8) == 0 ? 6 : 17);
3895 if (BG3 && PPU.BGMode == 0)
3898 DrawBackground (PPU.BGMode, 3, 2, 5);
3901 else if (PPU.BGMode != 7)
3903 // screen modes 2 and up but not mode 7
3912 DrawBackground (PPU.BGMode, 0, 5, 13);
3914 if (BG1 && PPU.BGMode != 6)
3917 DrawBackground (PPU.BGMode, 1, 2, 9);
3928 if (BG0 || ((Memory.FillRAM [0x2133] & 0x40) && BG1))
3932 if (Memory.FillRAM [0x2133] & 0x40)
3934 GFX.Mode7Mask = 0x7f;
3935 GFX.Mode7PriorityMask = 0x80;
3936 Mode7Depths [0] = 5;
3937 Mode7Depths [1] = 9;
3942 GFX.Mode7Mask = 0xff;
3943 GFX.Mode7PriorityMask = 0;
3944 Mode7Depths [0] = 5;
3945 Mode7Depths [1] = 5;
3950 if (!Settings.SixteenBit)
3951 DrawBGMode7Background (GFX.Screen, bg);
3955 if (!Settings.Mode7Interpolate)
3957 DrawBGMode7Background16 (GFX.Screen, bg);
3961 DrawBGMode7Background16_i (GFX.Screen, bg);
3968 #ifndef RC_OPTIMIZE // no hi res
3969 if (Settings.SupportHiRes && PPU.BGMode != 5 && PPU.BGMode != 6)
3971 if (IPPU.DoubleWidthPixels)
3973 // Mixure of background modes used on screen - scale width
3974 // of all non-mode 5 and 6 pixels.
3976 if (Settings.SixteenBit)
3979 for (register uint32 y = GFX.StartY; y <= GFX.EndY; y++)
3981 register uint16 *p = (uint16 *) (GFX.Screen + y * GFX.Pitch) + 255;
3982 register uint16 *q = (uint16 *) (GFX.Screen + y * GFX.Pitch) + 510;
3983 for (register int x = 255; x >= 0; x--, p--, q -= 2)
3990 for (register uint32 y = GFX.StartY; y <= GFX.EndY; y++)
3992 register uint8 *p = GFX.Screen + y * GFX.Pitch + 255;
3993 register uint8 *q = GFX.Screen + y * GFX.Pitch + 510;
3994 for (register int x = 255; x >= 0; x--, p--, q -= 2)
4001 if (IPPU.LatchedInterlace)
4003 // Interlace is enabled - double the height of all non-mode 5 and 6
4005 for (uint32 y = GFX.StartY; y <= GFX.EndY; y++)
4007 memcpy32 ((uint32_t*)(GFX.Screen + (y * 2 + 1) * GFX.Pitch2),
4008 (uint32_t*)(GFX.Screen + y * 2 * GFX.Pitch2),
4014 IPPU.PreviousLine = IPPU.CurrentLine;
4017 #ifdef GFX_MULTI_FORMAT
4019 #define _BUILD_PIXEL(F) \
4020 uint32 BuildPixel##F(uint32 R, uint32 G, uint32 B) \
4022 return (BUILD_PIXEL_##F(R,G,B)); \
4024 uint32 BuildPixel2##F(uint32 R, uint32 G, uint32 B) \
4026 return (BUILD_PIXEL2_##F(R,G,B)); \
4028 void DecomposePixel##F(uint32 pixel, uint32 &R, uint32 &G, uint32 &B) \
4030 DECOMPOSE_PIXEL_##F(pixel,R,G,B); \
4033 _BUILD_PIXEL(RGB565)
4034 _BUILD_PIXEL(RGB555)
4035 _BUILD_PIXEL(BGR565)
4036 _BUILD_PIXEL(BGR555)
4037 _BUILD_PIXEL(GBR565)
4038 _BUILD_PIXEL(GBR555)
4039 _BUILD_PIXEL(RGB5551)
4041 bool8_32 S9xSetRenderPixelFormat (int format)
4043 extern uint32 current_graphic_format;
4045 current_graphic_format = format;
4050 _BUILD_SETUP(RGB565)
4053 _BUILD_SETUP(RGB555)
4056 _BUILD_SETUP(BGR565)
4059 _BUILD_SETUP(BGR555)
4062 _BUILD_SETUP(GBR565)
4065 _BUILD_SETUP(GBR555)
4068 _BUILD_SETUP(RGB5551)