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.
52 //#include "netplay.h"
62 extern struct FxInit_s SuperFX;
63 extern struct FxRegs_s GSU;
65 EXTERN_C void S9xSuperFXWriteReg (uint8, uint32);
66 EXTERN_C uint8 S9xSuperFXReadReg (uint32);
69 void S9xUpdateHTimer ()
71 if (PPU.HTimerEnabled)
74 missing.hirq_pos = PPU.IRQHBeamPos;
76 PPU.HTimerPosition = PPU.IRQHBeamPos * Settings.H_Max / SNES_HCOUNTER_MAX;
77 if (PPU.HTimerPosition == Settings.H_Max ||
78 PPU.HTimerPosition == Settings.HBlankStart)
83 if (!PPU.VTimerEnabled || CPU.V_Counter == PPU.IRQVBeamPos)
85 if (PPU.HTimerPosition < CPU.Cycles)
87 // Missed the IRQ on this line already
88 if (CPU.WhichEvent == HBLANK_END_EVENT ||
89 CPU.WhichEvent == HTIMER_AFTER_EVENT)
91 CPU.WhichEvent = HBLANK_END_EVENT;
92 CPU.NextEvent = Settings.H_Max;
96 CPU.WhichEvent = HBLANK_START_EVENT;
97 CPU.NextEvent = Settings.HBlankStart;
102 if (CPU.WhichEvent == HTIMER_BEFORE_EVENT ||
103 CPU.WhichEvent == HBLANK_START_EVENT)
105 if (PPU.HTimerPosition > Settings.HBlankStart)
107 // HTimer was to trigger before h-blank start,
108 // now triggers after start of h-blank
109 CPU.NextEvent = Settings.HBlankStart;
110 CPU.WhichEvent = HBLANK_START_EVENT;
114 CPU.NextEvent = PPU.HTimerPosition;
115 CPU.WhichEvent = HTIMER_BEFORE_EVENT;
120 CPU.WhichEvent = HTIMER_AFTER_EVENT;
121 CPU.NextEvent = PPU.HTimerPosition;
128 void S9xFixColourBrightness ()
130 IPPU.XB = mul_brightness [PPU.Brightness];
131 for (int i = 0; i < 256; i++)
133 IPPU.Red [i] = IPPU.XB [PPU.CGDATA [i] & 0x1f];
134 IPPU.Green [i] = IPPU.XB [(PPU.CGDATA [i] >> 5) & 0x1f];
135 IPPU.Blue [i] = IPPU.XB [(PPU.CGDATA [i] >> 10) & 0x1f];
136 IPPU.ScreenColors [i] = BUILD_PIXEL (IPPU.Red [i], IPPU.Green [i],
141 /**********************************************************************************************/
143 /* This function sets a PPU Register to a specific byte */
144 /**********************************************************************************************/
145 void S9xSetPPU(uint8 Byte, uint16 Address)
147 if (Address <= 0x2183)
152 // Brightness and screen blank bit
153 if (Byte != Memory.FillRAM[0x2100])
156 if (PPU.Brightness != (Byte & 0xF))
158 IPPU.ColorsChanged = TRUE;
159 IPPU.DirectColourMapsNeedRebuild = TRUE;
160 PPU.Brightness = Byte & 0xF;
161 S9xFixColourBrightness();
162 if (PPU.Brightness > IPPU.MaxBrightness)
163 IPPU.MaxBrightness = PPU.Brightness;
165 if ((Memory.FillRAM[0x2100] & 0x80) != (Byte & 0x80))
167 IPPU.ColorsChanged = TRUE;
168 PPU.ForcedBlanking = (Byte >> 7) & 1;
174 // Sprite (OBJ) tile address
175 if (Byte != Memory.FillRAM[0x2101])
178 PPU.OBJNameBase = (Byte & 3) << 14;
179 PPU.OBJNameSelect = ((Byte >> 3) & 3) << 13;
180 PPU.OBJSizeSelect = (Byte >> 5) & 7;
181 IPPU.OBJChanged = TRUE;
186 // Sprite write address (low)
190 PPU.SavedOAMAddr = PPU.OAMAddr;
191 if (PPU.OAMPriorityRotation)
193 PPU.FirstSprite = PPU.OAMAddr & 0x7f;
195 missing.sprite_priority_rotation = 1;
201 // Sprite register write address (high), sprite priority rotation
203 if ((PPU.OAMPriorityRotation = (Byte & 0x80) == 0 ? 0 : 1))
205 PPU.FirstSprite = PPU.OAMAddr & 0x7f;
207 missing.sprite_priority_rotation = 1;
210 // Only update the sprite write address top bit if the low byte has
211 // been written to first.
214 PPU.OAMAddr &= 0x00FF;
215 PPU.OAMAddr |= (Byte & 1) << 8;
219 PPU.SavedOAMAddr = PPU.OAMAddr;
223 // Sprite register write
229 // Screen mode (0 - 7), background tile sizes and background 3
231 if (Byte != Memory.FillRAM[0x2105])
234 PPU.BG[0].BGSize = (Byte >> 4) & 1;
235 PPU.BG[1].BGSize = (Byte >> 5) & 1;
236 PPU.BG[2].BGSize = (Byte >> 6) & 1;
237 PPU.BG[3].BGSize = (Byte >> 7) & 1;
238 PPU.BGMode = Byte & 7;
239 // BJ: BG3Priority only takes effect if BGMode==1 and the bit is set
240 PPU.BG3Priority = ((Byte & 0x0f) == 0x09);
242 missing.modes[PPU.BGMode] = 1;
248 // Mosaic pixel size and enable
249 if (Byte != Memory.FillRAM[0x2106])
253 if ((Byte & 0xf0) && (Byte & 0x0f))
256 PPU.Mosaic = (Byte >> 4) + 1;
257 PPU.BGMosaic[0] = (Byte & 1) && PPU.Mosaic > 1;
258 PPU.BGMosaic[1] = (Byte & 2) && PPU.Mosaic > 1;
259 PPU.BGMosaic[2] = (Byte & 4) && PPU.Mosaic > 1;
260 PPU.BGMosaic[3] = (Byte & 8) && PPU.Mosaic > 1;
263 case 0x2107 : // [BG0SC]
264 if (Byte != Memory.FillRAM[0x2107])
267 PPU.BG[0].SCSize = Byte & 3;
268 PPU.BG[0].SCBase = (Byte & 0x7c) << 8;
272 case 0x2108 : // [BG1SC]
273 if (Byte != Memory.FillRAM[0x2108])
276 PPU.BG[1].SCSize = Byte & 3;
277 PPU.BG[1].SCBase = (Byte & 0x7c) << 8;
281 case 0x2109 : // [BG2SC]
282 if (Byte != Memory.FillRAM[0x2109])
285 PPU.BG[2].SCSize = Byte & 3;
286 PPU.BG[2].SCBase = (Byte & 0x7c) << 8;
290 case 0x210A : // [BG3SC]
291 if (Byte != Memory.FillRAM[0x210a])
294 PPU.BG[3].SCSize = Byte & 3;
295 PPU.BG[3].SCBase = (Byte & 0x7c) << 8;
299 case 0x210B : // [BG01NBA]
300 if (Byte != Memory.FillRAM[0x210b])
303 PPU.BG[0].NameBase = (Byte & 7) << 12;
304 PPU.BG[1].NameBase = ((Byte >> 4) & 7) << 12;
308 case 0x210C : // [BG23NBA]
309 if (Byte != Memory.FillRAM[0x210c])
312 PPU.BG[2].NameBase = (Byte & 7) << 12;
313 PPU.BG[3].NameBase = ((Byte >> 4) & 7) << 12;
319 ((PPU.BG[0].HOffset >> 8) & 0xff) | ((uint16) Byte << 8);
324 ((PPU.BG[0].VOffset >> 8) & 0xff) | ((uint16) Byte << 8);
328 ((PPU.BG[1].HOffset >> 8) & 0xff) | ((uint16) Byte << 8);
333 ((PPU.BG[1].VOffset >> 8) & 0xff) | ((uint16) Byte << 8);
338 ((PPU.BG[2].HOffset >> 8) & 0xff) | ((uint16) Byte << 8);
343 ((PPU.BG[2].VOffset >> 8) & 0xff) | ((uint16) Byte << 8);
348 ((PPU.BG[3].HOffset >> 8) & 0xff) | ((uint16) Byte << 8);
353 ((PPU.BG[3].VOffset >> 8) & 0xff) | ((uint16) Byte << 8);
357 // VRAM byte/word access flag and increment
358 PPU.VMA.High = (Byte & 0x80) == 0 ? FALSE : TRUE;
362 PPU.VMA.Increment = 1;
365 PPU.VMA.Increment = 32;
368 PPU.VMA.Increment = 128;
371 PPU.VMA.Increment = 128;
376 missing.vram_inc = Byte & 3;
380 static uint16 IncCount[4] = { 0, 32, 64, 128 };
381 static uint16 Shift[4] = { 0, 5, 6, 7 };
383 missing.vram_full_graphic_inc =
386 PPU.VMA.Increment = 1;
387 uint8 i = (Byte & 0x0c) >> 2;
388 PPU.VMA.FullGraphicCount = IncCount[i];
389 PPU.VMA.Mask1 = IncCount[i] * 8 - 1;
390 PPU.VMA.Shift = Shift[i];
393 PPU.VMA.FullGraphicCount = 0;
397 // VRAM read/write address (low)
398 PPU.VMA.Address &= 0xFF00;
399 PPU.VMA.Address |= Byte;
400 IPPU.FirstVRAMRead = TRUE;
404 // VRAM read/write address (high)
405 PPU.VMA.Address &= 0x00FF;
406 PPU.VMA.Address |= Byte << 8;
407 IPPU.FirstVRAMRead = TRUE;
411 // VRAM write data (low)
412 IPPU.FirstVRAMRead = TRUE;
417 // VRAM write data (high)
418 IPPU.FirstVRAMRead = TRUE;
423 // Mode 7 outside rotation area display mode and flipping
424 if (Byte != Memory.FillRAM[0x211a])
427 PPU.Mode7Repeat = Byte >> 6;
428 PPU.Mode7VFlip = (Byte & 2) >> 1;
429 PPU.Mode7HFlip = Byte & 1;
433 // Mode 7 matrix A (low & high)
434 PPU.MatrixA = ((PPU.MatrixA >> 8) & 0xff) | (Byte << 8);
435 PPU.Need16x8Mulitply = TRUE;
438 // Mode 7 matrix B (low & high)
439 PPU.MatrixB = ((PPU.MatrixB >> 8) & 0xff) | (Byte << 8);
440 PPU.Need16x8Mulitply = TRUE;
443 // Mode 7 matrix C (low & high)
444 PPU.MatrixC = ((PPU.MatrixC >> 8) & 0xff) | (Byte << 8);
447 // Mode 7 matrix D (low & high)
448 PPU.MatrixD = ((PPU.MatrixD >> 8) & 0xff) | (Byte << 8);
451 // Mode 7 centre of rotation X (low & high)
452 PPU.CentreX = ((PPU.CentreX >> 8) & 0xff) | (Byte << 8);
455 // Mode 7 centre of rotation Y (low & high)
456 PPU.CentreY = ((PPU.CentreY >> 8) & 0xff) | (Byte << 8);
471 // Window 1 and 2 enable for backgrounds 1 and 2
472 if (Byte != Memory.FillRAM[0x2123])
474 if (Settings.os9x_hack&PPU_IGNORE_WINDOW) return;
477 PPU.ClipWindow1Enable[0] = !!(Byte & 0x02);
478 PPU.ClipWindow1Enable[1] = !!(Byte & 0x20);
479 PPU.ClipWindow2Enable[0] = !!(Byte & 0x08);
480 PPU.ClipWindow2Enable[1] = !!(Byte & 0x80);
481 PPU.ClipWindow1Inside[0] = !(Byte & 0x01);
482 PPU.ClipWindow1Inside[1] = !(Byte & 0x10);
483 PPU.ClipWindow2Inside[0] = !(Byte & 0x04);
484 PPU.ClipWindow2Inside[1] = !(Byte & 0x40);
485 PPU.RecomputeClipWindows = TRUE;
488 missing.window2[1] = 1;
490 missing.window1[1] = 1;
492 missing.window2[0] = 1;
494 missing.window1[0] = 1;
499 // Window 1 and 2 enable for backgrounds 3 and 4
500 if (Byte != Memory.FillRAM[0x2124])
502 if (Settings.os9x_hack&PPU_IGNORE_WINDOW) return;
505 PPU.ClipWindow1Enable[2] = !!(Byte & 0x02);
506 PPU.ClipWindow1Enable[3] = !!(Byte & 0x20);
507 PPU.ClipWindow2Enable[2] = !!(Byte & 0x08);
508 PPU.ClipWindow2Enable[3] = !!(Byte & 0x80);
509 PPU.ClipWindow1Inside[2] = !(Byte & 0x01);
510 PPU.ClipWindow1Inside[3] = !(Byte & 0x10);
511 PPU.ClipWindow2Inside[2] = !(Byte & 0x04);
512 PPU.ClipWindow2Inside[3] = !(Byte & 0x40);
513 PPU.RecomputeClipWindows = TRUE;
516 missing.window2[3] = 1;
518 missing.window1[3] = 1;
520 missing.window2[2] = 1;
522 missing.window1[2] = 1;
527 // Window 1 and 2 enable for objects and colour window
528 if (Byte != Memory.FillRAM[0x2125])
530 if (Settings.os9x_hack&PPU_IGNORE_WINDOW) return;
533 PPU.ClipWindow1Enable[4] = !!(Byte & 0x02);
534 PPU.ClipWindow1Enable[5] = !!(Byte & 0x20);
535 PPU.ClipWindow2Enable[4] = !!(Byte & 0x08);
536 PPU.ClipWindow2Enable[5] = !!(Byte & 0x80);
537 PPU.ClipWindow1Inside[4] = !(Byte & 0x01);
538 PPU.ClipWindow1Inside[5] = !(Byte & 0x10);
539 PPU.ClipWindow2Inside[4] = !(Byte & 0x04);
540 PPU.ClipWindow2Inside[5] = !(Byte & 0x40);
541 PPU.RecomputeClipWindows = TRUE;
544 missing.window2[5] = 1;
546 missing.window1[5] = 1;
548 missing.window2[4] = 1;
550 missing.window1[4] = 1;
555 // Window 1 left position
556 if (Byte != Memory.FillRAM[0x2126])
558 if (Settings.os9x_hack&PPU_IGNORE_WINDOW) return;
561 PPU.Window1Left = Byte;
562 PPU.RecomputeClipWindows = TRUE;
566 // Window 1 right position
567 if (Byte != Memory.FillRAM[0x2127])
569 if (Settings.os9x_hack&PPU_IGNORE_WINDOW) return;
572 PPU.Window1Right = Byte;
573 PPU.RecomputeClipWindows = TRUE;
577 // Window 2 left position
578 if (Byte != Memory.FillRAM[0x2128])
580 if (Settings.os9x_hack&PPU_IGNORE_WINDOW) return;
583 PPU.Window2Left = Byte;
584 PPU.RecomputeClipWindows = TRUE;
588 // Window 2 right position
589 if (Byte != Memory.FillRAM[0x2129])
591 if (Settings.os9x_hack&PPU_IGNORE_WINDOW) return;
594 PPU.Window2Right = Byte;
595 PPU.RecomputeClipWindows = TRUE;
599 // Windows 1 & 2 overlap logic for backgrounds 1 - 4
600 if (Byte != Memory.FillRAM[0x212a])
602 if (Settings.os9x_hack&PPU_IGNORE_WINDOW) return;
605 PPU.ClipWindowOverlapLogic[0] = (Byte & 0x03);
606 PPU.ClipWindowOverlapLogic[1] = (Byte & 0x0c) >> 2;
607 PPU.ClipWindowOverlapLogic[2] = (Byte & 0x30) >> 4;
608 PPU.ClipWindowOverlapLogic[3] = (Byte & 0xc0) >> 6;
609 PPU.RecomputeClipWindows = TRUE;
613 // Windows 1 & 2 overlap logic for objects and colour window
614 if (Byte != Memory.FillRAM[0x212b])
616 if (Settings.os9x_hack&PPU_IGNORE_WINDOW) return;
619 PPU.ClipWindowOverlapLogic[4] = Byte & 0x03;
620 PPU.ClipWindowOverlapLogic[5] = (Byte & 0x0c) >> 2;
621 PPU.RecomputeClipWindows = TRUE;
625 // Main screen designation (backgrounds 1 - 4 and objects)
626 if (Byte != Memory.FillRAM[0x212c])
630 PPU.RecomputeClipWindows = TRUE;
631 Memory.FillRAM[Address] = Byte;
636 // Sub-screen designation (backgrounds 1 - 4 and objects)
637 if (Byte != Memory.FillRAM[0x212d])
643 missing.subscreen = 1;
645 PPU.RecomputeClipWindows = TRUE;
646 Memory.FillRAM[Address] = Byte;
651 // Window mask designation for main screen ?
652 if (Byte != Memory.FillRAM[0x212e])
656 PPU.RecomputeClipWindows = TRUE;
660 // Window mask designation for sub-screen ?
661 if (Byte != Memory.FillRAM[0x212f])
665 PPU.RecomputeClipWindows = TRUE;
669 // Fixed colour addition or screen addition
670 if (Byte != Memory.FillRAM[0x2130])
672 if (Settings.os9x_hack&PPU_IGNORE_ADDSUB) return;
675 PPU.RecomputeClipWindows = TRUE;
677 if ((Byte & 1) && (PPU.BGMode == 3 || PPU.BGMode == 4
684 // Colour addition or subtraction select
685 if (Byte != Memory.FillRAM[0x2131])
687 if (Settings.os9x_hack&PPU_IGNORE_ADDSUB) return;
690 // Backgrounds 1 - 4, objects and backdrop colour add/sub enable
695 if (Memory.FillRAM[0x2130] & 0x02)
696 missing.subscreen_sub = 1;
698 missing.fixed_colour_sub = 1;
703 if (Memory.FillRAM[0x2130] & 0x02)
704 missing.subscreen_add = 1;
706 missing.fixed_colour_add = 1;
709 Memory.FillRAM[0x2131] = Byte;
713 if (Byte != Memory.FillRAM[0x2132])
717 // Colour data for fixed colour addition/subtraction
719 //PPU.FixedColourBlue = Byte & 0x1f;
720 new_fixedcol=(Byte & 0x1f);
721 if (new_fixedcol!=PPU.FixedColourBlue) {if (!(Settings.os9x_hack&PPU_IGNORE_FIXEDCOLCHANGES)) FLUSH_REDRAW();PPU.FixedColourBlue=new_fixedcol;}
724 //PPU.FixedColourGreen = Byte & 0x1f;
725 new_fixedcol=(Byte & 0x1f);
726 if (new_fixedcol!=PPU.FixedColourGreen) {if (!(Settings.os9x_hack&PPU_IGNORE_FIXEDCOLCHANGES)) FLUSH_REDRAW();PPU.FixedColourGreen=new_fixedcol;}
729 //PPU.FixedColourRed = Byte & 0x1f;
730 new_fixedcol=(Byte & 0x1f);
731 if (new_fixedcol!=PPU.FixedColourRed) {if (!(Settings.os9x_hack&PPU_IGNORE_FIXEDCOLCHANGES)) FLUSH_REDRAW();PPU.FixedColourRed=new_fixedcol;}
735 // Colour data for fixed colour addition/subtraction
737 PPU.FixedColourBlue = Byte & 0x1f;
739 PPU.FixedColourGreen = Byte & 0x1f;
741 PPU.FixedColourRed = Byte & 0x1f;*/
747 if (Byte != Memory.FillRAM[0x2133])
751 missing.mode7_bgmode = 1;
753 missing.pseudo_512 = 1;
757 PPU.ScreenHeight = SNES_HEIGHT_EXTENDED;
759 missing.lines_239 = 1;
763 PPU.ScreenHeight = SNES_HEIGHT;
766 missing.sprite_double_height = 1;
769 missing.interlace = 1;
776 // Matrix 16bit x 8bit multiply result (read-only)
780 // Software latch for horizontal and vertical timers (read-only)
783 // OAM read data (read-only)
787 // VRAM read data (read-only)
790 // CG-RAM read data (read-only)
794 // Horizontal and vertical (low/high) read counter (read-only)
797 // PPU status (time over and range over)
800 // NTSC/PAL select and field (read-only)
867 _SPCInPB(Address & 3, Byte);
869 // CPU.Flags |= DEBUG_MODE_FLAG;
870 Memory.FillRAM[Address] = Byte;
871 IAPU.RAM[(Address & 3) + 0xf4] = Byte;
872 #ifdef SPC700_SHUTDOWN
873 CPU.APU_APUExecuting = Settings.APUEnabled;
887 PPU.WRAM |= Byte << 8;
891 PPU.WRAM |= Byte << 16;
901 if (Address >= 0x2200 && Address < 0x23ff)
902 S9xSetSA1(Byte, Address);
904 Memory.FillRAM[Address] = Byte;
909 // Dai Kaijyu Monogatari II
910 if (Address == 0x2801 && Settings.SRTC)
911 S9xSetSRTC(Byte, Address);
912 else if (Address < 0x3000 || Address >= 0x3000 + 768)
915 missing.unknownppu_write = Address;
916 if (Settings.TraceUnknownRegisters)
920 "Unknown register write: $%02X->$%04X\n",
923 S9xMessage(S9X_TRACE, S9X_PPU_TRACE, String);
929 if (!Settings.SuperFX)
935 if ((Memory.FillRAM[0x3030] ^ Byte) & FLG_G)
937 Memory.FillRAM[Address] = Byte;
938 // Go flag has been changed
945 Memory.FillRAM[Address] = Byte;
949 Memory.FillRAM[Address] = Byte;
952 Memory.FillRAM[Address] = Byte;
955 Memory.FillRAM[Address] = Byte & 0x7f;
958 Memory.FillRAM[Address] = Byte & 0x7f;
961 Memory.FillRAM[Address] = Byte;
964 Memory.FillRAM[Address] = Byte;
967 Memory.FillRAM[Address] = Byte;
970 Memory.FillRAM[Address] = Byte;
975 Memory.FillRAM[Address] = Byte;
978 Memory.FillRAM[Address] = Byte;
979 Memory.FillRAM[0x3000 + GSU_SFR] |= FLG_G;
984 Memory.FillRAM[Address] = Byte;
985 if (Address >= 0x3100)
987 FxCacheWriteAccess(Address);
994 Memory.FillRAM[Address] = Byte;
997 /**********************************************************************************************/
999 /* This function retrieves a PPU Register */
1000 /**********************************************************************************************/
1001 uint8 S9xGetPPU(uint16 Address)
1005 if (Address <= 0x2190)
1011 return (Memory.FillRAM[Address]);
1014 missing.oam_address_read = 1;
1016 return (uint8) (PPU.OAMAddr);
1018 return (((PPU.OAMAddr >> 8) & 1) | (PPU.OAMPriorityRotation << 7));
1028 return (Memory.FillRAM[Address]);
1038 missing.bg_offset_read = 1;
1040 return (Memory.FillRAM[Address]);
1042 return (Memory.FillRAM[Address]);
1044 return (uint8) (PPU.VMA.Address);
1046 return (PPU.VMA.Address >> 8);
1050 return (Memory.FillRAM[Address]);
1058 missing.matrix_read = 1;
1060 return (Memory.FillRAM[Address]);
1081 return (Memory.FillRAM[Address]);
1086 // 16bit x 8bit multiply read result.
1087 if (PPU.Need16x8Mulitply)
1089 int32 r = (int32) PPU.MatrixA * (int32) (PPU.MatrixB >> 8);
1091 Memory.FillRAM[0x2134] = (uint8) r;
1092 Memory.FillRAM[0x2135] = (uint8) (r >> 8);
1093 Memory.FillRAM[0x2136] = (uint8) (r >> 16);
1094 PPU.Need16x8Mulitply = FALSE;
1097 missing.matrix_multiply = 1;
1099 return (Memory.FillRAM[Address]);
1101 // Latch h and v counters
1103 missing.h_v_latch = 1;
1107 CPU.WaitAddress = CPU.PCAtOpcodeStart;
1110 PPU.HVBeamCounterLatched = 1;
1111 PPU.VBeamPosLatched = (uint16)
1113 PPU.HBeamPosLatched = (uint16) ((CPU.Cycles * SNES_HCOUNTER_MAX) / Settings.H_Max);
1115 // Causes screen flicker for Yoshi's Island if uncommented
1116 //CLEAR_IRQ_SOURCE (PPU_V_BEAM_IRQ_SOURCE | PPU_H_BEAM_IRQ_SOURCE);
1118 if (SNESGameFixes.NeedInit0x2137)
1119 PPU.VBeamFlip = 0; //jyam sword world sfc2 & godzill
1122 // Read OAM (sprite) control data
1123 if (!PPU.OAMReadFlip)
1125 byte = PPU.OAMData[PPU.OAMAddr << 1];
1129 byte = PPU.OAMData[(PPU.OAMAddr << 1) + 1];
1130 if (++PPU.OAMAddr >= 0x110)
1133 PPU.OAMReadFlip ^= 1;
1135 missing.oam_read = 1;
1140 // Read vram low byte
1142 missing.vram_read = 1;
1144 if (IPPU.FirstVRAMRead)
1145 byte = Memory.VRAM[PPU.VMA.Address << 1];
1146 else if (PPU.VMA.FullGraphicCount)
1148 uint32 addr = PPU.VMA.Address - 1;
1149 uint32 rem = addr & PPU.VMA.Mask1;
1151 (addr & ~PPU.VMA.Mask1)
1152 + (rem >> PPU.VMA.Shift)
1153 + ((rem & (PPU.VMA.FullGraphicCount - 1)) << 3);
1154 byte = Memory.VRAM[((address << 1) - 2) & 0xFFFF];
1157 byte = Memory.VRAM[((PPU.VMA.Address << 1) - 2) & 0xffff];
1161 PPU.VMA.Address += PPU.VMA.Increment;
1162 IPPU.FirstVRAMRead = FALSE;
1166 // Read vram high byte
1168 missing.vram_read = 1;
1170 if (IPPU.FirstVRAMRead)
1171 byte = Memory.VRAM[((PPU.VMA.Address << 1) + 1) & 0xffff];
1172 else if (PPU.VMA.FullGraphicCount)
1174 uint32 addr = PPU.VMA.Address - 1;
1175 uint32 rem = addr & PPU.VMA.Mask1;
1177 (addr & ~PPU.VMA.Mask1)
1178 + (rem >> PPU.VMA.Shift)
1179 + ((rem & (PPU.VMA.FullGraphicCount - 1)) << 3);
1180 byte = Memory.VRAM[((address << 1) - 1) & 0xFFFF];
1183 byte = Memory.VRAM[((PPU.VMA.Address << 1) - 1) & 0xFFFF];
1186 PPU.VMA.Address += PPU.VMA.Increment;
1187 IPPU.FirstVRAMRead = FALSE;
1192 // Read palette data
1194 missing.cgram_read = 1;
1197 byte = PPU.CGDATA[PPU.CGADD++] >> 8;
1199 byte = PPU.CGDATA[PPU.CGADD] & 0xff;
1201 PPU.CGFLIPRead ^= 1;
1205 // Horizontal counter value 0-339
1207 missing.h_counter_read = 1;
1210 byte = PPU.HBeamPosLatched >> 8;
1212 byte = (uint8) PPU.HBeamPosLatched;
1216 // Vertical counter value 0-262
1218 missing.v_counter_read = 1;
1221 byte = PPU.VBeamPosLatched >> 8;
1223 byte = (uint8) PPU.VBeamPosLatched;
1227 // PPU time and range over flags
1228 return (SNESGameFixes._0x213E_ReturnValue);
1231 // NTSC/PAL and which field flags
1232 PPU.VBeamFlip = PPU.HBeamFlip = 0;
1233 return ((Settings.PAL ? 0x10 : 0) | (Memory.FillRAM[0x213f] & 0xc0));
1300 return ((uint8) _SPCOutP[Address & 3]);
1302 // CPU.Flags |= DEBUG_MODE_FLAG;
1303 #ifdef SPC700_SHUTDOWN
1304 CPU.APU_APUExecuting = Settings.APUEnabled;
1307 if(Settings.APUEnabled)
1310 //CPU.WaitAddress = CPU.PCAtOpcodeStart;
1312 if(SNESGameFixes.APU_OutPorts_ReturnValueFix
1313 && Address >= 0x2140
1314 && Address <= 0x2143
1317 return (uint8) ((Address & 1) ?
1318 ((rand() & 0xff00) >> 8) : (rand() & 0xff));
1321 return (APU.OutPorts[Address & 3]);
1324 switch (Settings.SoundSkipMethod)
1328 CPU.BranchSkip = TRUE;
1333 CPU.BranchSkip = TRUE;
1336 if ((Address & 3) < 2)
1342 return ((Address & 3) == 1 ? 0xaa : 0xbb);
1344 return ((r >> 3) & 0xff);
1351 return ((r >> 3) & 0xff);
1353 return (Memory.FillRAM[Address]);
1359 missing.wram_read = 1;
1361 byte = Memory.RAM[PPU.WRAM++];
1362 PPU.WRAM &= 0x1FFFF;
1367 return (Memory.FillRAM[Address]);
1376 return (S9xGetSA1(Address));
1378 if (Address <= 0x2fff || Address >= 0x3000 + 768)
1387 // For Dai Kaijyu Monogatari II
1389 return (S9xGetSRTC(Address));
1394 missing.unknownppu_read = Address;
1395 if (Settings.TraceUnknownRegisters)
1397 sprintf(String, "Unknown register read: $%04X\n", Address);
1398 S9xMessage(S9X_TRACE, S9X_PPU_TRACE, String);
1402 return (0); //Memory.FillRAM[Address]);
1406 if (!Settings.SuperFX)
1408 byte = Memory.FillRAM[Address];
1410 //if (Address != 0x3030 && Address != 0x3031)
1411 //printf ("%04x\n", Address);
1413 if (Address == 0x3030)
1415 CPU.WaitAddress = CPU.PCAtOpcodeStart;
1419 if (Address == 0x3031)
1421 CLEAR_IRQ_SOURCE(GSU_IRQ_SOURCE);
1422 Memory.FillRAM[0x3031] = byte & 0x7f;
1430 /**********************************************************************************************/
1432 /* This function sets a CPU/DMA Register to a specific byte */
1433 /**********************************************************************************************/
1434 void S9xSetCPU(uint8 byte, uint16 Address)
1438 if (Address < 0x4200)
1441 CPU.Cycles += ONE_CYCLE;
1446 // S9xReset reading of old-style joypads
1447 if ((byte & 1) && !(Memory.FillRAM[Address] & 1))
1449 PPU.Joypad1ButtonReadPos = 0;
1450 PPU.Joypad2ButtonReadPos = 0;
1451 PPU.Joypad3ButtonReadPos = 0;
1458 missing.unknowncpu_write = Address;
1459 if (Settings.TraceUnknownRegisters)
1461 sprintf(String, "Unknown register register write: $%02X->$%04X\n", byte, Address);
1462 S9xMessage(S9X_TRACE, S9X_PPU_TRACE, String);
1472 // NMI, V & H IRQ and joypad reading enable flags
1474 && (!SNESGameFixes.umiharakawaseFix || PPU.IRQVBeamPos < 209))
1476 if (!PPU.VTimerEnabled)
1480 missing.virq_pos = PPU.IRQVBeamPos;
1482 PPU.VTimerEnabled = TRUE;
1483 if (PPU.HTimerEnabled)
1485 else if (PPU.IRQVBeamPos == CPU.V_Counter)
1486 S9xSetIRQ(PPU_V_BEAM_IRQ_SOURCE);
1491 PPU.VTimerEnabled = FALSE;
1492 #ifndef RC_OPTIMIZED
1493 if (SNESGameFixes.umiharakawaseFix)
1500 if (!PPU.HTimerEnabled)
1504 missing.hirq_pos = PPU.IRQHBeamPos;
1506 PPU.HTimerEnabled = TRUE;
1512 // No need to check for HTimer being disabled as the scanline
1513 // event trigger code won't trigger an H-IRQ unless its enabled.
1514 PPU.HTimerEnabled = FALSE;
1515 PPU.HTimerPosition = Settings.H_Max + 1;
1518 #ifndef RC_OPTIMIZED
1519 if (!Settings.DaffyDuck)
1520 CLEAR_IRQ_SOURCE(PPU_V_BEAM_IRQ_SOURCE | PPU_H_BEAM_IRQ_SOURCE);
1523 && !(Memory.FillRAM[0x4200] & 0x80)
1524 && CPU.V_Counter >= PPU.ScreenHeight + FIRST_VISIBLE_LINE
1525 && CPU.V_Counter <= PPU.ScreenHeight + (SNESGameFixes.alienVSpredetorFix ? 25 : 15)
1526 && //jyam 15->25 alien vs predetor
1527 // Panic Bomberman clears the NMI pending flag @ scanline 230 before enabling
1528 // NMIs again. The NMI routine crashes the CPU if it is called without the NMI
1529 // pending flag being set...
1530 (Memory.FillRAM[0x4210] & 0x80) && !CPU.NMIActive)
1532 CPU.Flags |= NMI_FLAG;
1533 CPU.NMIActive = TRUE;
1534 CPU.NMICycleCount = CPU.NMITriggerPoint;
1541 // Multiplier (for multply)
1546 uint32 res = Memory.FillRAM[0x4202] * byte;
1548 Memory.FillRAM[0x4216] = (uint8) res;
1549 Memory.FillRAM[0x4217] = (uint8) (res >> 8);
1554 // Low and high muliplier (for divide)
1560 Memory.FillRAM[0x4204] + (Memory.FillRAM[0x4205] << 8);
1561 uint16 div = byte ? a / byte : 0xffff;
1562 uint16 rem = byte ? a % byte : a;
1564 Memory.FillRAM[0x4214] = (uint8) div;
1565 Memory.FillRAM[0x4215] = div >> 8;
1566 Memory.FillRAM[0x4216] = (uint8) rem;
1567 Memory.FillRAM[0x4217] = rem >> 8;
1571 d = PPU.IRQHBeamPos;
1572 PPU.IRQHBeamPos = (PPU.IRQHBeamPos & 0xFF00) | byte;
1574 if (PPU.HTimerEnabled && PPU.IRQHBeamPos != d)
1579 d = PPU.IRQHBeamPos;
1580 PPU.IRQHBeamPos = (PPU.IRQHBeamPos & 0xFF) | ((byte & 1) << 8);
1582 if (PPU.HTimerEnabled && PPU.IRQHBeamPos != d)
1588 d = PPU.IRQVBeamPos;
1589 PPU.IRQVBeamPos = (PPU.IRQVBeamPos & 0xFF00) | byte;
1591 missing.virq_pos = PPU.IRQVBeamPos;
1593 if (PPU.VTimerEnabled && PPU.IRQVBeamPos != d)
1595 if (PPU.HTimerEnabled)
1599 if (PPU.IRQVBeamPos == CPU.V_Counter)
1600 S9xSetIRQ(PPU_V_BEAM_IRQ_SOURCE);
1606 d = PPU.IRQVBeamPos;
1607 PPU.IRQVBeamPos = (PPU.IRQVBeamPos & 0xFF) | ((byte & 1) << 8);
1609 missing.virq_pos = PPU.IRQVBeamPos;
1611 if (PPU.VTimerEnabled && PPU.IRQVBeamPos != d)
1613 if (PPU.HTimerEnabled)
1617 if (PPU.IRQVBeamPos == CPU.V_Counter)
1618 S9xSetIRQ(PPU_V_BEAM_IRQ_SOURCE);
1625 missing.dma_this_frame = byte;
1626 missing.dma_channels = byte;
1628 if ((byte & 0x01) != 0)
1630 if ((byte & 0x02) != 0)
1632 if ((byte & 0x04) != 0)
1634 if ((byte & 0x08) != 0)
1636 if ((byte & 0x10) != 0)
1638 if ((byte & 0x20) != 0)
1640 if ((byte & 0x40) != 0)
1642 if ((byte & 0x80) != 0)
1647 missing.hdma_this_frame |= byte;
1648 missing.hdma_channels |= byte;
1650 if (Settings.DisableHDMA)
1652 Memory.FillRAM[0x420c] = byte;
1657 // Cycle speed 0 - 2.68Mhz, 1 - 3.58Mhz (banks 0x80 +)
1658 if ((byte & 1) != (Memory.FillRAM[0x420d] & 1))
1662 CPU.FastROMSpeed = ONE_CYCLE;
1664 missing.fast_rom = 1;
1668 CPU.FastROMSpeed = SLOW_ONE_CYCLE;
1670 Memory.FixROMSpeed();
1678 // NMI ocurred flag (reset on read or write)
1679 Memory.FillRAM[0x4210] = 0;
1682 // IRQ ocurred flag (reset on read or write)
1683 CLEAR_IRQ_SOURCE(PPU_V_BEAM_IRQ_SOURCE | PPU_H_BEAM_IRQ_SOURCE);
1686 // v-blank, h-blank and joypad being scanned flags (read-only)
1688 // I/O Port (read-only)
1691 // Quotent of divide (read-only)
1694 // Multiply product (read-only)
1704 // Joypad values (read-only)
1715 d = (Address >> 4) & 0x7;
1716 DMA[d].TransferDirection = (byte & 128) != 0 ? 1 : 0;
1717 DMA[d].HDMAIndirectAddressing = (byte & 64) != 0 ? 1 : 0;
1718 DMA[d].AAddressDecrement = (byte & 16) != 0 ? 1 : 0;
1719 DMA[d].AAddressFixed = (byte & 8) != 0 ? 1 : 0;
1720 DMA[d].TransferMode = (byte & 7);
1731 DMA[((Address >> 4) & 0x7)].BAddress = byte;
1742 d = (Address >> 4) & 0x7;
1743 DMA[d].AAddress &= 0xFF00;
1744 DMA[d].AAddress |= byte;
1755 d = (Address >> 4) & 0x7;
1756 DMA[d].AAddress &= 0xFF;
1757 DMA[d].AAddress |= byte << 8;
1768 DMA[((Address >> 4) & 0x7)].ABank = byte;
1779 d = (Address >> 4) & 0x7;
1780 DMA[d].TransferBytes &= 0xFF00;
1781 DMA[d].TransferBytes |= byte;
1782 DMA[d].IndirectAddress &= 0xff00;
1783 DMA[d].IndirectAddress |= byte;
1794 d = (Address >> 4) & 0x7;
1795 DMA[d].TransferBytes &= 0xFF;
1796 DMA[d].TransferBytes |= byte << 8;
1797 DMA[d].IndirectAddress &= 0xff;
1798 DMA[d].IndirectAddress |= byte << 8;
1809 DMA[d = ((Address >> 4) & 0x7)].IndirectBank = byte;
1820 d = (Address >> 4) & 7;
1821 DMA[d].Address &= 0xff00;
1822 DMA[d].Address |= byte;
1833 d = (Address >> 4) & 0x7;
1834 DMA[d].Address &= 0xff;
1835 DMA[d].Address |= byte << 8;
1846 d = (Address >> 4) & 0x7;
1847 DMA[d].LineCount = byte & 0x7f;
1848 DMA[d].Repeat = !(byte & 0x80);
1855 //printf ("%02x->%04x\n", byte, Address);
1862 //printf ("%02x->%04x\n", byte, Address);
1864 S9xSetSDD1MemoryMap(Address - 0x4804, byte & 7);
1868 missing.unknowncpu_write = Address;
1869 if (Settings.TraceUnknownRegisters)
1873 "Unknown register write: $%02X->$%04X\n",
1876 S9xMessage(S9X_TRACE, S9X_PPU_TRACE, String);
1881 Memory.FillRAM[Address] = byte;
1884 /**********************************************************************************************/
1886 /* This function retrieves a CPU/DMA Register */
1887 /**********************************************************************************************/
1888 uint8 S9xGetCPU(uint16 Address)
1892 if (Address < 0x4200)
1895 CPU.Cycles += ONE_CYCLE;
1899 // Secret of the Evermore
1906 if (Memory.FillRAM[0x4016] & 1)
1908 if ((!Settings.SwapJoypads
1909 && IPPU.Controller == SNES_MOUSE_SWAPPED)
1910 || (Settings.SwapJoypads
1911 && IPPU.Controller == SNES_MOUSE))
1913 if (++PPU.MouseSpeed[0] > 2)
1914 PPU.MouseSpeed[0] = 0;
1919 int ind = Settings.SwapJoypads ? 1 : 0;
1920 byte = IPPU.Joypads[ind] >> (PPU.Joypad1ButtonReadPos ^ 15);
1921 PPU.Joypad1ButtonReadPos++;
1926 if (Memory.FillRAM[0x4016] & 1)
1928 // MultiPlayer5 adaptor is only allowed to be plugged into port 2
1929 switch (IPPU.Controller)
1931 case SNES_MULTIPLAYER5 :
1933 case SNES_MOUSE_SWAPPED :
1934 if (Settings.SwapJoypads
1935 && ++PPU.MouseSpeed[0] > 2)
1936 PPU.MouseSpeed[0] = 0;
1940 if (!Settings.SwapJoypads
1941 && ++PPU.MouseSpeed[0] > 2)
1942 PPU.MouseSpeed[0] = 0;
1948 int ind = Settings.SwapJoypads ? 0 : 1;
1950 if (IPPU.Controller == SNES_MULTIPLAYER5)
1952 if (Memory.FillRAM[0x4201] & 0x80)
1956 >> (PPU.Joypad2ButtonReadPos ^ 15))
1958 | (((IPPU.Joypads[2]
1959 >> (PPU.Joypad2ButtonReadPos ^ 15))
1962 PPU.Joypad2ButtonReadPos++;
1969 >> (PPU.Joypad3ButtonReadPos ^ 15))
1971 | (((IPPU.Joypads[4]
1972 >> (PPU.Joypad3ButtonReadPos ^ 15))
1975 PPU.Joypad3ButtonReadPos++;
1981 >> (PPU.Joypad2ButtonReadPos++ ^ 15))
1986 missing.unknowncpu_read = Address;
1987 if (Settings.TraceUnknownRegisters)
1989 sprintf(String, "Unknown register read: $%04X\n", Address);
1990 S9xMessage(S9X_TRACE, S9X_PPU_TRACE, String);
1995 return (Memory.FillRAM[Address]);
2000 // BS Dynami Tracer! needs to be able to check if NMIs are enabled
2001 // already, otherwise the game locks up.
2003 // NMI, h & v timers and joypad reading enable
2004 if (SNESGameFixes.Old_Read0x4200)
2007 CPU.WaitAddress = CPU.PCAtOpcodeStart;
2009 return (REGISTER_4212());
2012 // I/O port (output - write only?)
2015 // Multiplier and multiplicand (write)
2019 // Divisor and dividend (write)
2020 return (Memory.FillRAM[Address]);
2022 return (uint8) (PPU.IRQHBeamPos);
2024 return (PPU.IRQHBeamPos >> 8);
2026 return (uint8) (PPU.IRQVBeamPos);
2028 return (PPU.IRQVBeamPos >> 8);
2030 // General purpose DMA enable
2031 // Super Formation Soccer 95 della Serie A UCC Xaqua requires this
2032 // register should not always return zero.
2033 // .. But Aero 2 waits until this register goes zero..
2034 // Just keep toggling the value for now in the hope that it breaks
2035 // the game out of its wait loop...
2036 Memory.FillRAM[0x420b] = !Memory.FillRAM[0x420b];
2037 return (Memory.FillRAM[0x420b]);
2042 // Cycle speed 0 - 2.68Mhz, 1 - 3.58Mhz (banks 0x80 +)
2043 return (Memory.FillRAM[Address]);
2047 return (Memory.FillRAM[Address]);
2050 CPU.WaitAddress = CPU.PCAtOpcodeStart;
2052 byte = Memory.FillRAM[0x4210];
2053 Memory.FillRAM[0x4210] = 0;
2058 & (PPU_V_BEAM_IRQ_SOURCE | PPU_H_BEAM_IRQ_SOURCE))
2061 // Super Robot Wars Ex ROM bug requires this.
2062 byte |= CPU.Cycles >= Settings.HBlankStart ? 0x40 : 0;
2063 CLEAR_IRQ_SOURCE(PPU_V_BEAM_IRQ_SOURCE | PPU_H_BEAM_IRQ_SOURCE);
2066 // V-blank, h-blank and joypads being read flags (read-only)
2068 CPU.WaitAddress = CPU.PCAtOpcodeStart;
2070 return (REGISTER_4212());
2075 // Quotient of divide result
2078 // Multiplcation result (for multiply) or remainder of
2080 return (Memory.FillRAM[Address]);
2089 // Joypads 1-4 button and direction state.
2090 return (Memory.FillRAM[Address]);
2100 // DMA direction, address type, fixed flag,
2101 return (Memory.FillRAM[Address]);
2111 return (Memory.FillRAM[Address]);
2121 return (Memory.FillRAM[Address]);
2131 return (Memory.FillRAM[Address]);
2141 return (Memory.FillRAM[Address]);
2151 return (Memory.FillRAM[Address]);
2161 return (Memory.FillRAM[Address]);
2171 return (DMA[(Address >> 4) & 7].IndirectBank);
2181 return (Memory.FillRAM[Address]);
2191 return (Memory.FillRAM[Address]);
2202 int d = (Address & 0x70) >> 4;
2203 if (IPPU.HDMA & (1 << d))
2205 return (DMA[d].LineCount);
2207 return (Memory.FillRAM[Address]);
2211 missing.unknowncpu_read = Address;
2212 if (Settings.TraceUnknownRegisters)
2214 sprintf(String, "Unknown register read: $%04X\n", Address);
2215 S9xMessage(S9X_TRACE, S9X_PPU_TRACE, String);
2221 return (Memory.FillRAM[Address]);
2227 PPU.BG3Priority = 0;
2230 PPU.VMA.Increment = 1;
2231 PPU.VMA.Address = 0;
2232 PPU.VMA.FullGraphicCount = 0;
2235 for (uint8 B = 0; B != 4; B++)
2237 PPU.BG[B].SCBase = 0;
2238 PPU.BG[B].VOffset = 0;
2239 PPU.BG[B].HOffset = 0;
2240 PPU.BG[B].BGSize = 0;
2241 PPU.BG[B].NameBase = 0;
2242 PPU.BG[B].SCSize = 0;
2244 PPU.ClipCounts[B] = 0;
2245 PPU.ClipWindowOverlapLogic[B] = CLIP_OR;
2246 PPU.ClipWindow1Enable[B] = FALSE;
2247 PPU.ClipWindow2Enable[B] = FALSE;
2248 PPU.ClipWindow1Inside[B] = TRUE;
2249 PPU.ClipWindow2Inside[B] = TRUE;
2252 PPU.ClipCounts[4] = 0;
2253 PPU.ClipCounts[5] = 0;
2254 PPU.ClipWindowOverlapLogic[4] = PPU.ClipWindowOverlapLogic[5] = CLIP_OR;
2255 PPU.ClipWindow1Enable[4] = PPU.ClipWindow1Enable[5] = FALSE;
2256 PPU.ClipWindow2Enable[4] = PPU.ClipWindow2Enable[5] = FALSE;
2257 PPU.ClipWindow1Inside[4] = PPU.ClipWindow1Inside[5] = TRUE;
2258 PPU.ClipWindow2Inside[4] = PPU.ClipWindow2Inside[5] = TRUE;
2262 for (c = 0; c < 256; c++)
2264 IPPU.Red[c] = (c & 7) << 2;
2265 IPPU.Green[c] = ((c >> 3) & 7) << 2;
2266 IPPU.Blue[c] = ((c >> 6) & 2) << 3;
2268 IPPU.Red[c] | (IPPU.Green[c] << 5) | (IPPU.Blue[c] << 10);
2271 PPU.FirstSprite = 0;
2272 PPU.LastSprite = 127;
2273 for (int Sprite = 0; Sprite < 128; Sprite++)
2275 PPU.OBJ[Sprite].HPos = 0;
2276 PPU.OBJ[Sprite].VPos = 0;
2277 PPU.OBJ[Sprite].VFlip = 0;
2278 PPU.OBJ[Sprite].HFlip = 0;
2279 PPU.OBJ[Sprite].Priority = 0;
2280 PPU.OBJ[Sprite].Palette = 0;
2281 PPU.OBJ[Sprite].Name = 0;
2282 PPU.OBJ[Sprite].Size = 0;
2284 PPU.OAMPriorityRotation = 0;
2287 PPU.OAMTileAddress = 0;
2289 PPU.IRQVBeamPos = 0;
2290 PPU.IRQHBeamPos = 0;
2291 PPU.VBeamPosLatched = 0;
2292 PPU.HBeamPosLatched = 0;
2296 PPU.HVBeamCounterLatched = 0;
2298 PPU.MatrixA = PPU.MatrixB = PPU.MatrixC = PPU.MatrixD = 0;
2299 PPU.CentreX = PPU.CentreY = 0;
2300 PPU.Joypad1ButtonReadPos = 0;
2301 PPU.Joypad2ButtonReadPos = 0;
2302 PPU.Joypad3ButtonReadPos = 0;
2305 PPU.FixedColourRed = PPU.FixedColourGreen = PPU.FixedColourBlue = 0;
2306 PPU.SavedOAMAddr = 0;
2307 PPU.ScreenHeight = SNES_HEIGHT;
2310 PPU.ForcedBlanking = TRUE;
2311 PPU.OBJThroughMain = FALSE;
2312 PPU.OBJThroughSub = FALSE;
2313 PPU.OBJSizeSelect = 0;
2314 PPU.OBJNameSelect = 0;
2315 PPU.OBJNameBase = 0;
2316 PPU.OBJAddition = FALSE;
2317 PPU.OAMReadFlip = 0;
2318 ZeroMemory(PPU.OAMData, 512 + 32);
2320 PPU.VTimerEnabled = FALSE;
2321 PPU.HTimerEnabled = FALSE;
2322 PPU.HTimerPosition = Settings.H_Max + 1;
2324 PPU.BGMosaic[0] = PPU.BGMosaic[1] = FALSE;
2325 PPU.BGMosaic[2] = PPU.BGMosaic[3] = FALSE;
2326 PPU.Mode7HFlip = FALSE;
2327 PPU.Mode7VFlip = FALSE;
2328 PPU.Mode7Repeat = 0;
2329 PPU.Window1Left = 1;
2330 PPU.Window1Right = 0;
2331 PPU.Window2Left = 1;
2332 PPU.Window2Right = 0;
2333 PPU.RecomputeClipWindows = TRUE;
2335 PPU.Need16x8Mulitply = FALSE;
2336 PPU.MouseSpeed[0] = PPU.MouseSpeed[1] = 0;
2338 IPPU.ColorsChanged = TRUE;
2340 IPPU.HDMAStarted = FALSE;
2341 IPPU.MaxBrightness = 0;
2342 IPPU.LatchedBlanking = 0;
2343 IPPU.OBJChanged = TRUE;
2344 IPPU.RenderThisFrame = TRUE;
2345 IPPU.DirectColourMapsNeedRebuild = TRUE;
2346 IPPU.FrameCount = 0;
2347 IPPU.RenderedFramesCount = 0;
2348 IPPU.DisplayedRenderedFrameCount = 0;
2349 IPPU.SkippedFrames = 0;
2351 ZeroMemory(IPPU.TileCached[TILE_2BIT], MAX_2BIT_TILES);
2352 ZeroMemory(IPPU.TileCached[TILE_4BIT], MAX_4BIT_TILES);
2353 ZeroMemory(IPPU.TileCached[TILE_8BIT], MAX_8BIT_TILES);
2354 IPPU.FirstVRAMRead = FALSE;
2355 IPPU.LatchedInterlace = FALSE;
2356 IPPU.DoubleWidthPixels = FALSE;
2357 IPPU.RenderedScreenWidth = SNES_WIDTH;
2358 IPPU.RenderedScreenHeight = SNES_HEIGHT;
2360 for (c = 0; c < 256; c++)
2361 IPPU.ScreenColors[c] = c;
2362 S9xFixColourBrightness();
2363 IPPU.PreviousLine = IPPU.CurrentLine = 0;
2364 IPPU.Joypads[0] = IPPU.Joypads[1] = IPPU.Joypads[2] = 0;
2365 IPPU.Joypads[3] = IPPU.Joypads[4] = 0;
2366 IPPU.SuperScope = 0;
2367 IPPU.Mouse[0] = IPPU.Mouse[1] = 0;
2368 IPPU.PrevMouseX[0] = IPPU.PrevMouseX[1] = 256 / 2;
2369 IPPU.PrevMouseY[0] = IPPU.PrevMouseY[1] = 224 / 2;
2371 if (Settings.ControllerOption == 0)
2372 IPPU.Controller = SNES_MAX_CONTROLLER_OPTIONS - 1;
2374 IPPU.Controller = Settings.ControllerOption - 1;
2375 S9xNextController();
2377 for (c = 0; c < 2; c++)
2378 memset(& IPPU.Clip[c], 0, sizeof(struct ClipData));
2380 if (Settings.MouseMaster)
2385 for (c = 0; c < 0x8000; c += 0x100)
2386 memset(& Memory.FillRAM[c], c >> 8, 0x100);
2388 ZeroMemory(& Memory.FillRAM[0x2100], 0x100);
2389 ZeroMemory(& Memory.FillRAM[0x4200], 0x100);
2390 ZeroMemory(& Memory.FillRAM[0x4000], 0x100);
2391 // For BS Suttehakkun 2...
2392 ZeroMemory(& Memory.FillRAM[0x1000], 0x1000);
2395 void S9xProcessMouse(int which1)
2400 if ((IPPU.Controller == SNES_MOUSE
2401 || IPPU.Controller == SNES_MOUSE_SWAPPED)
2402 && S9xReadMousePosition(which1, x, y, buttons))
2404 int delta_x, delta_y;
2405 #define MOUSE_SIGNATURE 0x1
2406 IPPU.Mouse[which1] =
2408 | (PPU.MouseSpeed[which1] << 4)
2409 | ((buttons & 1) << 6)
2410 | ((buttons & 2) << 6);
2412 delta_x = x - IPPU.PrevMouseX[which1];
2413 delta_y = y - IPPU.PrevMouseY[which1];
2418 IPPU.PrevMouseX[which1] += 63;
2420 else if (delta_x < -63)
2423 IPPU.PrevMouseX[which1] -= 63;
2426 IPPU.PrevMouseX[which1] = x;
2431 IPPU.PrevMouseY[which1] += 63;
2433 else if (delta_y < -63)
2436 IPPU.PrevMouseY[which1] -= 63;
2439 IPPU.PrevMouseY[which1] = y;
2444 IPPU.Mouse[which1] |= (delta_x | 0x80) << 16;
2447 IPPU.Mouse[which1] |= delta_x << 16;
2452 IPPU.Mouse[which1] |= (delta_y | 0x80) << 24;
2455 IPPU.Mouse[which1] |= delta_y << 24;
2457 if (IPPU.Controller == SNES_MOUSE_SWAPPED)
2458 IPPU.Joypads[0] = IPPU.Mouse[which1];
2460 IPPU.Joypads[1] = IPPU.Mouse[which1];
2464 void ProcessSuperScope()
2469 if (IPPU.Controller == SNES_SUPERSCOPE
2470 && S9xReadSuperScopePosition(x, y, buttons))
2472 #define SUPERSCOPE_SIGNATURE 0x00ff
2476 SUPERSCOPE_SIGNATURE
2477 | ((buttons & 1) << (7 + 8))
2478 | ((buttons & 2) << (5 + 8))
2479 | ((buttons & 4) << (3 + 8))
2480 | ((buttons & 8) << (1 + 8));
2485 if (y > PPU.ScreenHeight - 1)
2486 y = PPU.ScreenHeight - 1;
2490 PPU.VBeamPosLatched = (uint16) (y + 1);
2491 PPU.HBeamPosLatched = (uint16) x;
2492 PPU.HVBeamCounterLatched = TRUE;
2493 Memory.FillRAM[0x213F] |= 0x40;
2494 IPPU.Joypads[1] = scope;
2498 void S9xNextController()
2500 switch (IPPU.Controller)
2502 case SNES_MULTIPLAYER5 :
2503 IPPU.Controller = SNES_JOYPAD;
2506 if (Settings.MouseMaster)
2508 IPPU.Controller = SNES_MOUSE_SWAPPED;
2511 case SNES_MOUSE_SWAPPED :
2512 if (Settings.MouseMaster)
2514 IPPU.Controller = SNES_MOUSE;
2518 if (Settings.SuperScopeMaster)
2520 IPPU.Controller = SNES_SUPERSCOPE;
2523 case SNES_SUPERSCOPE :
2524 if (Settings.MultiPlayer5Master)
2526 IPPU.Controller = SNES_MULTIPLAYER5;
2530 IPPU.Controller = SNES_JOYPAD;
2535 void S9xUpdateJoypads()
2542 for (i = 0; i < 5; i++)
2545 IPPU.Joypads[i] = S9xReadJoypad(i);
2546 if (IPPU.Joypads[i] & SNES_LEFT_MASK)
2547 IPPU.Joypads[i] &= ~SNES_RIGHT_MASK;
2548 if (IPPU.Joypads[i] & SNES_UP_MASK)
2549 IPPU.Joypads[i] &= ~SNES_DOWN_MASK;
2552 //touhaiden controller Fix
2553 if (SNESGameFixes.TouhaidenControllerFix
2554 && (IPPU.Controller == SNES_JOYPAD
2555 || IPPU.Controller == SNES_MULTIPLAYER5))
2557 for (i = 0; i < 5; i++)
2559 if (IPPU.Joypads[i])
2560 IPPU.Joypads[i] |= 0xffff0000;
2564 // Read mouse position if enabled
2565 if (Settings.MouseMaster)
2567 for (i = 0; i < 2; i++)
2571 // Read SuperScope if enabled
2572 if (Settings.SuperScopeMaster)
2573 ProcessSuperScope();
2575 if (Memory.FillRAM[0x4200] & 1)
2577 PPU.Joypad1ButtonReadPos = 16;
2578 if (Memory.FillRAM[0x4201] & 0x80)
2580 PPU.Joypad2ButtonReadPos = 16;
2581 PPU.Joypad3ButtonReadPos = 0;
2585 PPU.Joypad2ButtonReadPos = 0;
2586 PPU.Joypad3ButtonReadPos = 16;
2588 int ind = Settings.SwapJoypads ? 1 : 0;
2590 Memory.FillRAM[0x4218] = (uint8) IPPU.Joypads[ind];
2591 Memory.FillRAM[0x4219] = (uint8) (IPPU.Joypads[ind] >> 8);
2592 Memory.FillRAM[0x421a] = (uint8) IPPU.Joypads[ind ^ 1];
2593 Memory.FillRAM[0x421b] = (uint8) (IPPU.Joypads[ind ^ 1] >> 8);
2594 if (Memory.FillRAM[0x4201] & 0x80)
2596 Memory.FillRAM[0x421c] = (uint8) IPPU.Joypads[ind];
2597 Memory.FillRAM[0x421d] = (uint8) (IPPU.Joypads[ind] >> 8);
2598 Memory.FillRAM[0x421e] = (uint8) IPPU.Joypads[2];
2599 Memory.FillRAM[0x421f] = (uint8) (IPPU.Joypads[2] >> 8);
2603 Memory.FillRAM[0x421c] = (uint8) IPPU.Joypads[3];
2604 Memory.FillRAM[0x421d] = (uint8) (IPPU.Joypads[3] >> 8);
2605 Memory.FillRAM[0x421e] = (uint8) IPPU.Joypads[4];
2606 Memory.FillRAM[0x421f] = (uint8) (IPPU.Joypads[4] >> 8);
2612 void S9xSuperFXExec()
2615 if (Settings.SuperFX)
2617 if ((Memory.FillRAM[0x3000 + GSU_SFR] & FLG_G)
2618 && (Memory.FillRAM[0x3000 + GSU_SCMR] & 0x18) == 0x18)
2620 if (!Settings.WinterGold)
2622 else FxEmulate((Memory.FillRAM[0x3000 + GSU_CLSR] & 1) ? 700 : 350);
2623 int GSUStatus = Memory.FillRAM[0x3000
2624 + GSU_SFR] | (Memory.FillRAM[0x3000 + GSU_SFR + 1] << 8);
2625 if ((GSUStatus & (FLG_G | FLG_IRQ)) == FLG_IRQ)
2627 // Trigger a GSU IRQ.
2628 S9xSetIRQ(GSU_IRQ_SOURCE);
2633 uint32 tmp = (Memory.FillRAM[0x3034] << 16)
2634 + * (uint16 *) & Memory.FillRAM[0x301e];
2636 if (tmp == 0x018428)
2639 & SRAM[0x0064] = 0xbc00;
2640 * (uint16 *) & SRAM[0x002c] = 0x8000;
2643 if (tmp == -1) //0x018428) //0x01bfc3) //0x09edaf) //-1) //0x57edaf)
2645 while (Memory.FillRAM[0x3030] & 0x20)
2656 /* Make the string 32 chars long */
2657 if (strlen(tmp) < 32)
2659 memset(& tmp[strlen(tmp)], ' ', 32 - strlen(tmp));
2663 /* Copy registers (so we can see if any changed) */
2664 vColr = FxGetColorRegister();
2665 vPor = FxGetPlotOptionRegister();
2666 memcpy(avReg, SuperFX.pvRegisters, 0x40);
2668 /* Print the pipe string */
2671 /* Execute the instruction in the pipe */
2673 vError = FxEmulate(1);
2675 /* Check if any registers changed (and print them if they did) */
2676 for (i = 0; i < 16; i++)
2680 ((uint32) avReg[i * 2])
2681 | (((uint32) avReg[(i * 2) + 1]) << 8);
2683 (uint32) (SuperFX.pvRegisters[i * 2])
2684 | (((uint32) SuperFX.pvRegisters[(i * 2) + 1]) << 8);
2686 a = OPCODE_BYTES(vPipe);
2687 if (((r1 + a) & 0xffff) != r2)
2688 printf(" r%d=$%04x", i, r2);
2693 ((uint32) avReg[0x30]) | (((uint32) avReg[0x31]) << 8);
2695 (uint32) (SuperFX.pvRegisters[0x30])
2696 | (((uint32) SuperFX.pvRegisters[0x31]) << 8);
2697 if ((r1 & (1 << 1)) != (r2 & (1 << 1)))
2698 printf(" Z=%d", (uint32) (!!(r2 & (1 << 1))));
2699 if ((r1 & (1 << 2)) != (r2 & (1 << 2)))
2700 printf(" CY=%d", (uint32) (!!(r2 & (1 << 2))));
2701 if ((r1 & (1 << 3)) != (r2 & (1 << 3)))
2702 printf(" S=%d", (uint32) (!!(r2 & (1 << 3))));
2703 if ((r1 & (1 << 4)) != (r2 & (1 << 4)))
2704 printf(" OV=%d", (uint32) (!!(r2 & (1 << 4))));
2705 if ((r1 & (1 << 5)) != (r2 & (1 << 5)))
2706 printf(" G=%d", (uint32) (!!(r2 & (1 << 5))));
2707 if ((r1 & (1 << 6)) != (r2 & (1 << 6)))
2708 printf(" R=%d", (uint32) (!!(r2 & (1 << 6))));
2709 if ((r1 & (1 << 8)) != (r2 & (1 << 8)))
2710 printf(" ALT1=%d", (uint32) (!!(r2 & (1 << 8))));
2711 if ((r1 & (1 << 9)) != (r2 & (1 << 9)))
2712 printf(" ALT2=%d", (uint32) (!!(r2 & (1 << 9))));
2713 if ((r1 & (1 << 10)) != (r2 & (1 << 10)))
2714 printf(" IL=%d", (uint32) (!!(r2 & (1 << 10))));
2715 if ((r1 & (1 << 11)) != (r2 & (1 << 11)))
2716 printf(" IH=%d", (uint32) (!!(r2 & (1 << 11))));
2717 if ((r1 & (1 << 12)) != (r2 & (1 << 12)))
2718 printf(" B=%d", (uint32) (!!(r2 & (1 << 12))));
2719 if ((r1 & (1 << 15)) != (r2 & (1 << 15)))
2720 printf(" IRQ=%d", (uint32) (!!(r2 & (1 << 15))));
2724 uint32 r1 = ((uint32) avReg[0x34]);
2725 uint32 r2 = (uint32) (SuperFX.pvRegisters[0x34]);
2727 printf(" PBR=$%02x", r2);
2731 uint32 r1 = ((uint32) avReg[0x36]);
2732 uint32 r2 = (uint32) (SuperFX.pvRegisters[0x36]);
2734 printf(" ROMBR=$%02x", r2);
2738 uint32 r1 = ((uint32) avReg[0x3c]);
2739 uint32 r2 = (uint32) (SuperFX.pvRegisters[0x3c]);
2741 printf(" RAMBR=$%02x", r2);
2746 ((uint32) avReg[0x3e]) | (((uint32) avReg[0x3f]) << 8);
2748 (uint32) (SuperFX.pvRegisters[0x3e])
2749 | (((uint32) SuperFX.pvRegisters[0x3f]) << 8);
2751 printf(" CBR=$%04x", r2);
2755 if (vColr != FxGetColorRegister())
2756 printf(" COLR=$%02x", FxGetColorRegister());
2760 if (vPor != FxGetPlotOptionRegister())
2761 printf(" POR=$%02x", FxGetPlotOptionRegister());
2770 (Memory.FillRAM[0x3034] << 16)
2771 + (Memory.FillRAM[0x301f] << 8)
2772 + (Memory.FillRAM[0x301e] << 0);
2774 printf("%06x: %d\n", t, FxEmulate(2000000));
2775 // FxEmulate (2000000);
2778 if (!(CPU.Flags & TRACE_FLAG))
2780 static int z = 1; if (z == 0)
2782 extern FILE * trace;
2783 CPU.Flags |= TRACE_FLAG;
2784 trace = fopen("trace.log", "wb");
2790 Memory.FillRAM[0x3030] &= ~0x20;
2791 if (Memory.FillRAM[0x3031] & 0x80)
2793 S9xSetIRQ(GSU_IRQ_SOURCE);