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.
58 extern struct FxInit_s SuperFX;
59 extern struct FxRegs_s GSU;
61 EXTERN_C void S9xSuperFXWriteReg (uint8, uint32);
62 EXTERN_C uint8 S9xSuperFXReadReg (uint32);
65 void S9xUpdateHTimer ()
67 if (PPU.HTimerEnabled)
70 missing.hirq_pos = PPU.IRQHBeamPos;
72 PPU.HTimerPosition = PPU.IRQHBeamPos * Settings.H_Max / SNES_HCOUNTER_MAX;
73 if (PPU.HTimerPosition == Settings.H_Max ||
74 PPU.HTimerPosition == Settings.HBlankStart)
79 if (!PPU.VTimerEnabled || CPU.V_Counter == PPU.IRQVBeamPos)
81 if (PPU.HTimerPosition < CPU.Cycles)
83 // Missed the IRQ on this line already
84 if (CPU.WhichEvent == HBLANK_END_EVENT ||
85 CPU.WhichEvent == HTIMER_AFTER_EVENT)
87 CPU.WhichEvent = HBLANK_END_EVENT;
88 CPU.NextEvent = Settings.H_Max;
92 CPU.WhichEvent = HBLANK_START_EVENT;
93 CPU.NextEvent = Settings.HBlankStart;
98 if (CPU.WhichEvent == HTIMER_BEFORE_EVENT ||
99 CPU.WhichEvent == HBLANK_START_EVENT)
101 if (PPU.HTimerPosition > Settings.HBlankStart)
103 // HTimer was to trigger before h-blank start,
104 // now triggers after start of h-blank
105 CPU.NextEvent = Settings.HBlankStart;
106 CPU.WhichEvent = HBLANK_START_EVENT;
110 CPU.NextEvent = PPU.HTimerPosition;
111 CPU.WhichEvent = HTIMER_BEFORE_EVENT;
116 CPU.WhichEvent = HTIMER_AFTER_EVENT;
117 CPU.NextEvent = PPU.HTimerPosition;
124 void S9xFixColourBrightness ()
126 IPPU.XB = mul_brightness [PPU.Brightness];
127 for (int i = 0; i < 256; i++)
129 IPPU.Red [i] = IPPU.XB [PPU.CGDATA [i] & 0x1f];
130 IPPU.Green [i] = IPPU.XB [(PPU.CGDATA [i] >> 5) & 0x1f];
131 IPPU.Blue [i] = IPPU.XB [(PPU.CGDATA [i] >> 10) & 0x1f];
132 IPPU.ScreenColors [i] = BUILD_PIXEL (IPPU.Red [i], IPPU.Green [i],
137 /**********************************************************************************************/
139 /* This function sets a PPU Register to a specific byte */
140 /**********************************************************************************************/
141 void S9xSetPPU(uint8 Byte, uint16 Address)
143 if (Address <= 0x2183)
148 // Brightness and screen blank bit
149 if (Byte != Memory.FillRAM[0x2100])
152 if (PPU.Brightness != (Byte & 0xF))
154 IPPU.ColorsChanged = TRUE;
155 IPPU.DirectColourMapsNeedRebuild = TRUE;
156 PPU.Brightness = Byte & 0xF;
157 S9xFixColourBrightness();
158 if (PPU.Brightness > IPPU.MaxBrightness)
159 IPPU.MaxBrightness = PPU.Brightness;
161 if ((Memory.FillRAM[0x2100] & 0x80) != (Byte & 0x80))
163 IPPU.ColorsChanged = TRUE;
164 PPU.ForcedBlanking = (Byte >> 7) & 1;
170 // Sprite (OBJ) tile address
171 if (Byte != Memory.FillRAM[0x2101])
174 PPU.OBJNameBase = (Byte & 3) << 14;
175 PPU.OBJNameSelect = ((Byte >> 3) & 3) << 13;
176 PPU.OBJSizeSelect = (Byte >> 5) & 7;
177 IPPU.OBJChanged = TRUE;
182 // Sprite write address (low)
186 PPU.SavedOAMAddr = PPU.OAMAddr;
187 if (PPU.OAMPriorityRotation)
189 PPU.FirstSprite = PPU.OAMAddr & 0x7f;
191 missing.sprite_priority_rotation = 1;
197 // Sprite register write address (high), sprite priority rotation
199 if ((PPU.OAMPriorityRotation = (Byte & 0x80) == 0 ? 0 : 1))
201 PPU.FirstSprite = PPU.OAMAddr & 0x7f;
203 missing.sprite_priority_rotation = 1;
206 // Only update the sprite write address top bit if the low byte has
207 // been written to first.
210 PPU.OAMAddr &= 0x00FF;
211 PPU.OAMAddr |= (Byte & 1) << 8;
215 PPU.SavedOAMAddr = PPU.OAMAddr;
219 // Sprite register write
225 // Screen mode (0 - 7), background tile sizes and background 3
227 if (Byte != Memory.FillRAM[0x2105])
230 PPU.BG[0].BGSize = (Byte >> 4) & 1;
231 PPU.BG[1].BGSize = (Byte >> 5) & 1;
232 PPU.BG[2].BGSize = (Byte >> 6) & 1;
233 PPU.BG[3].BGSize = (Byte >> 7) & 1;
234 PPU.BGMode = Byte & 7;
235 // BJ: BG3Priority only takes effect if BGMode==1 and the bit is set
236 PPU.BG3Priority = ((Byte & 0x0f) == 0x09);
238 missing.modes[PPU.BGMode] = 1;
244 // Mosaic pixel size and enable
245 if (Byte != Memory.FillRAM[0x2106])
249 if ((Byte & 0xf0) && (Byte & 0x0f))
252 PPU.Mosaic = (Byte >> 4) + 1;
253 PPU.BGMosaic[0] = (Byte & 1) && PPU.Mosaic > 1;
254 PPU.BGMosaic[1] = (Byte & 2) && PPU.Mosaic > 1;
255 PPU.BGMosaic[2] = (Byte & 4) && PPU.Mosaic > 1;
256 PPU.BGMosaic[3] = (Byte & 8) && PPU.Mosaic > 1;
259 case 0x2107 : // [BG0SC]
260 if (Byte != Memory.FillRAM[0x2107])
263 PPU.BG[0].SCSize = Byte & 3;
264 PPU.BG[0].SCBase = (Byte & 0x7c) << 8;
268 case 0x2108 : // [BG1SC]
269 if (Byte != Memory.FillRAM[0x2108])
272 PPU.BG[1].SCSize = Byte & 3;
273 PPU.BG[1].SCBase = (Byte & 0x7c) << 8;
277 case 0x2109 : // [BG2SC]
278 if (Byte != Memory.FillRAM[0x2109])
281 PPU.BG[2].SCSize = Byte & 3;
282 PPU.BG[2].SCBase = (Byte & 0x7c) << 8;
286 case 0x210A : // [BG3SC]
287 if (Byte != Memory.FillRAM[0x210a])
290 PPU.BG[3].SCSize = Byte & 3;
291 PPU.BG[3].SCBase = (Byte & 0x7c) << 8;
295 case 0x210B : // [BG01NBA]
296 if (Byte != Memory.FillRAM[0x210b])
299 PPU.BG[0].NameBase = (Byte & 7) << 12;
300 PPU.BG[1].NameBase = ((Byte >> 4) & 7) << 12;
304 case 0x210C : // [BG23NBA]
305 if (Byte != Memory.FillRAM[0x210c])
308 PPU.BG[2].NameBase = (Byte & 7) << 12;
309 PPU.BG[3].NameBase = ((Byte >> 4) & 7) << 12;
315 ((PPU.BG[0].HOffset >> 8) & 0xff) | ((uint16) Byte << 8);
320 ((PPU.BG[0].VOffset >> 8) & 0xff) | ((uint16) Byte << 8);
324 ((PPU.BG[1].HOffset >> 8) & 0xff) | ((uint16) Byte << 8);
329 ((PPU.BG[1].VOffset >> 8) & 0xff) | ((uint16) Byte << 8);
334 ((PPU.BG[2].HOffset >> 8) & 0xff) | ((uint16) Byte << 8);
339 ((PPU.BG[2].VOffset >> 8) & 0xff) | ((uint16) Byte << 8);
344 ((PPU.BG[3].HOffset >> 8) & 0xff) | ((uint16) Byte << 8);
349 ((PPU.BG[3].VOffset >> 8) & 0xff) | ((uint16) Byte << 8);
353 // VRAM byte/word access flag and increment
354 PPU.VMA.High = (Byte & 0x80) == 0 ? FALSE : TRUE;
358 PPU.VMA.Increment = 1;
361 PPU.VMA.Increment = 32;
364 PPU.VMA.Increment = 128;
367 PPU.VMA.Increment = 128;
372 missing.vram_inc = Byte & 3;
376 static uint16 IncCount[4] = { 0, 32, 64, 128 };
377 static uint16 Shift[4] = { 0, 5, 6, 7 };
379 missing.vram_full_graphic_inc =
382 PPU.VMA.Increment = 1;
383 uint8 i = (Byte & 0x0c) >> 2;
384 PPU.VMA.FullGraphicCount = IncCount[i];
385 PPU.VMA.Mask1 = IncCount[i] * 8 - 1;
386 PPU.VMA.Shift = Shift[i];
389 PPU.VMA.FullGraphicCount = 0;
393 // VRAM read/write address (low)
394 PPU.VMA.Address &= 0xFF00;
395 PPU.VMA.Address |= Byte;
396 IPPU.FirstVRAMRead = TRUE;
400 // VRAM read/write address (high)
401 PPU.VMA.Address &= 0x00FF;
402 PPU.VMA.Address |= Byte << 8;
403 IPPU.FirstVRAMRead = TRUE;
407 // VRAM write data (low)
408 IPPU.FirstVRAMRead = TRUE;
413 // VRAM write data (high)
414 IPPU.FirstVRAMRead = TRUE;
419 // Mode 7 outside rotation area display mode and flipping
420 if (Byte != Memory.FillRAM[0x211a])
423 PPU.Mode7Repeat = Byte >> 6;
424 PPU.Mode7VFlip = (Byte & 2) >> 1;
425 PPU.Mode7HFlip = Byte & 1;
429 // Mode 7 matrix A (low & high)
430 PPU.MatrixA = ((PPU.MatrixA >> 8) & 0xff) | (Byte << 8);
431 PPU.Need16x8Mulitply = TRUE;
434 // Mode 7 matrix B (low & high)
435 PPU.MatrixB = ((PPU.MatrixB >> 8) & 0xff) | (Byte << 8);
436 PPU.Need16x8Mulitply = TRUE;
439 // Mode 7 matrix C (low & high)
440 PPU.MatrixC = ((PPU.MatrixC >> 8) & 0xff) | (Byte << 8);
443 // Mode 7 matrix D (low & high)
444 PPU.MatrixD = ((PPU.MatrixD >> 8) & 0xff) | (Byte << 8);
447 // Mode 7 centre of rotation X (low & high)
448 PPU.CentreX = ((PPU.CentreX >> 8) & 0xff) | (Byte << 8);
451 // Mode 7 centre of rotation Y (low & high)
452 PPU.CentreY = ((PPU.CentreY >> 8) & 0xff) | (Byte << 8);
467 // Window 1 and 2 enable for backgrounds 1 and 2
468 if (Byte != Memory.FillRAM[0x2123])
470 if (Settings.os9x_hack&PPU_IGNORE_WINDOW) return;
473 PPU.ClipWindow1Enable[0] = !!(Byte & 0x02);
474 PPU.ClipWindow1Enable[1] = !!(Byte & 0x20);
475 PPU.ClipWindow2Enable[0] = !!(Byte & 0x08);
476 PPU.ClipWindow2Enable[1] = !!(Byte & 0x80);
477 PPU.ClipWindow1Inside[0] = !(Byte & 0x01);
478 PPU.ClipWindow1Inside[1] = !(Byte & 0x10);
479 PPU.ClipWindow2Inside[0] = !(Byte & 0x04);
480 PPU.ClipWindow2Inside[1] = !(Byte & 0x40);
481 PPU.RecomputeClipWindows = TRUE;
484 missing.window2[1] = 1;
486 missing.window1[1] = 1;
488 missing.window2[0] = 1;
490 missing.window1[0] = 1;
495 // Window 1 and 2 enable for backgrounds 3 and 4
496 if (Byte != Memory.FillRAM[0x2124])
498 if (Settings.os9x_hack&PPU_IGNORE_WINDOW) return;
501 PPU.ClipWindow1Enable[2] = !!(Byte & 0x02);
502 PPU.ClipWindow1Enable[3] = !!(Byte & 0x20);
503 PPU.ClipWindow2Enable[2] = !!(Byte & 0x08);
504 PPU.ClipWindow2Enable[3] = !!(Byte & 0x80);
505 PPU.ClipWindow1Inside[2] = !(Byte & 0x01);
506 PPU.ClipWindow1Inside[3] = !(Byte & 0x10);
507 PPU.ClipWindow2Inside[2] = !(Byte & 0x04);
508 PPU.ClipWindow2Inside[3] = !(Byte & 0x40);
509 PPU.RecomputeClipWindows = TRUE;
512 missing.window2[3] = 1;
514 missing.window1[3] = 1;
516 missing.window2[2] = 1;
518 missing.window1[2] = 1;
523 // Window 1 and 2 enable for objects and colour window
524 if (Byte != Memory.FillRAM[0x2125])
526 if (Settings.os9x_hack&PPU_IGNORE_WINDOW) return;
529 PPU.ClipWindow1Enable[4] = !!(Byte & 0x02);
530 PPU.ClipWindow1Enable[5] = !!(Byte & 0x20);
531 PPU.ClipWindow2Enable[4] = !!(Byte & 0x08);
532 PPU.ClipWindow2Enable[5] = !!(Byte & 0x80);
533 PPU.ClipWindow1Inside[4] = !(Byte & 0x01);
534 PPU.ClipWindow1Inside[5] = !(Byte & 0x10);
535 PPU.ClipWindow2Inside[4] = !(Byte & 0x04);
536 PPU.ClipWindow2Inside[5] = !(Byte & 0x40);
537 PPU.RecomputeClipWindows = TRUE;
540 missing.window2[5] = 1;
542 missing.window1[5] = 1;
544 missing.window2[4] = 1;
546 missing.window1[4] = 1;
551 // Window 1 left position
552 if (Byte != Memory.FillRAM[0x2126])
554 if (Settings.os9x_hack&PPU_IGNORE_WINDOW) return;
557 PPU.Window1Left = Byte;
558 PPU.RecomputeClipWindows = TRUE;
562 // Window 1 right position
563 if (Byte != Memory.FillRAM[0x2127])
565 if (Settings.os9x_hack&PPU_IGNORE_WINDOW) return;
568 PPU.Window1Right = Byte;
569 PPU.RecomputeClipWindows = TRUE;
573 // Window 2 left position
574 if (Byte != Memory.FillRAM[0x2128])
576 if (Settings.os9x_hack&PPU_IGNORE_WINDOW) return;
579 PPU.Window2Left = Byte;
580 PPU.RecomputeClipWindows = TRUE;
584 // Window 2 right position
585 if (Byte != Memory.FillRAM[0x2129])
587 if (Settings.os9x_hack&PPU_IGNORE_WINDOW) return;
590 PPU.Window2Right = Byte;
591 PPU.RecomputeClipWindows = TRUE;
595 // Windows 1 & 2 overlap logic for backgrounds 1 - 4
596 if (Byte != Memory.FillRAM[0x212a])
598 if (Settings.os9x_hack&PPU_IGNORE_WINDOW) return;
601 PPU.ClipWindowOverlapLogic[0] = (Byte & 0x03);
602 PPU.ClipWindowOverlapLogic[1] = (Byte & 0x0c) >> 2;
603 PPU.ClipWindowOverlapLogic[2] = (Byte & 0x30) >> 4;
604 PPU.ClipWindowOverlapLogic[3] = (Byte & 0xc0) >> 6;
605 PPU.RecomputeClipWindows = TRUE;
609 // Windows 1 & 2 overlap logic for objects and colour window
610 if (Byte != Memory.FillRAM[0x212b])
612 if (Settings.os9x_hack&PPU_IGNORE_WINDOW) return;
615 PPU.ClipWindowOverlapLogic[4] = Byte & 0x03;
616 PPU.ClipWindowOverlapLogic[5] = (Byte & 0x0c) >> 2;
617 PPU.RecomputeClipWindows = TRUE;
621 // Main screen designation (backgrounds 1 - 4 and objects)
622 if (Byte != Memory.FillRAM[0x212c])
626 PPU.RecomputeClipWindows = TRUE;
627 Memory.FillRAM[Address] = Byte;
632 // Sub-screen designation (backgrounds 1 - 4 and objects)
633 if (Byte != Memory.FillRAM[0x212d])
639 missing.subscreen = 1;
641 PPU.RecomputeClipWindows = TRUE;
642 Memory.FillRAM[Address] = Byte;
647 // Window mask designation for main screen ?
648 if (Byte != Memory.FillRAM[0x212e])
652 PPU.RecomputeClipWindows = TRUE;
656 // Window mask designation for sub-screen ?
657 if (Byte != Memory.FillRAM[0x212f])
661 PPU.RecomputeClipWindows = TRUE;
665 // Fixed colour addition or screen addition
666 if (Byte != Memory.FillRAM[0x2130])
668 if (Settings.os9x_hack&PPU_IGNORE_ADDSUB) return;
671 PPU.RecomputeClipWindows = TRUE;
673 if ((Byte & 1) && (PPU.BGMode == 3 || PPU.BGMode == 4
680 // Colour addition or subtraction select
681 if (Byte != Memory.FillRAM[0x2131])
683 if (Settings.os9x_hack&PPU_IGNORE_ADDSUB) return;
686 // Backgrounds 1 - 4, objects and backdrop colour add/sub enable
691 if (Memory.FillRAM[0x2130] & 0x02)
692 missing.subscreen_sub = 1;
694 missing.fixed_colour_sub = 1;
699 if (Memory.FillRAM[0x2130] & 0x02)
700 missing.subscreen_add = 1;
702 missing.fixed_colour_add = 1;
705 Memory.FillRAM[0x2131] = Byte;
709 if (Byte != Memory.FillRAM[0x2132])
713 // Colour data for fixed colour addition/subtraction
715 //PPU.FixedColourBlue = Byte & 0x1f;
716 new_fixedcol=(Byte & 0x1f);
717 if (new_fixedcol!=PPU.FixedColourBlue) {if (!(Settings.os9x_hack&PPU_IGNORE_FIXEDCOLCHANGES)) FLUSH_REDRAW();PPU.FixedColourBlue=new_fixedcol;}
720 //PPU.FixedColourGreen = Byte & 0x1f;
721 new_fixedcol=(Byte & 0x1f);
722 if (new_fixedcol!=PPU.FixedColourGreen) {if (!(Settings.os9x_hack&PPU_IGNORE_FIXEDCOLCHANGES)) FLUSH_REDRAW();PPU.FixedColourGreen=new_fixedcol;}
725 //PPU.FixedColourRed = Byte & 0x1f;
726 new_fixedcol=(Byte & 0x1f);
727 if (new_fixedcol!=PPU.FixedColourRed) {if (!(Settings.os9x_hack&PPU_IGNORE_FIXEDCOLCHANGES)) FLUSH_REDRAW();PPU.FixedColourRed=new_fixedcol;}
731 // Colour data for fixed colour addition/subtraction
733 PPU.FixedColourBlue = Byte & 0x1f;
735 PPU.FixedColourGreen = Byte & 0x1f;
737 PPU.FixedColourRed = Byte & 0x1f;*/
743 if (Byte != Memory.FillRAM[0x2133])
747 missing.mode7_bgmode = 1;
749 missing.pseudo_512 = 1;
753 PPU.ScreenHeight = SNES_HEIGHT_EXTENDED;
755 missing.lines_239 = 1;
759 PPU.ScreenHeight = SNES_HEIGHT;
762 missing.sprite_double_height = 1;
765 missing.interlace = 1;
772 // Matrix 16bit x 8bit multiply result (read-only)
776 // Software latch for horizontal and vertical timers (read-only)
779 // OAM read data (read-only)
783 // VRAM read data (read-only)
786 // CG-RAM read data (read-only)
790 // Horizontal and vertical (low/high) read counter (read-only)
793 // PPU status (time over and range over)
796 // NTSC/PAL select and field (read-only)
863 _SPCInPB(Address & 3, Byte);
865 // CPU.Flags |= DEBUG_MODE_FLAG;
866 Memory.FillRAM[Address] = Byte;
867 IAPU.RAM[(Address & 3) + 0xf4] = Byte;
868 #ifdef SPC700_SHUTDOWN
869 CPU.APU_APUExecuting = Settings.APUEnabled;
883 PPU.WRAM |= Byte << 8;
887 PPU.WRAM |= Byte << 16;
897 if (Address >= 0x2200 && Address < 0x23ff)
898 S9xSetSA1(Byte, Address);
900 Memory.FillRAM[Address] = Byte;
905 // Dai Kaijyu Monogatari II
906 if (Address == 0x2801 && Settings.SRTC)
907 S9xSetSRTC(Byte, Address);
908 else if (Address < 0x3000 || Address >= 0x3000 + 768)
911 missing.unknownppu_write = Address;
912 if (Settings.TraceUnknownRegisters)
916 "Unknown register write: $%02X->$%04X\n",
919 S9xMessage(S9X_TRACE, S9X_PPU_TRACE, String);
925 if (!Settings.SuperFX)
931 if ((Memory.FillRAM[0x3030] ^ Byte) & FLG_G)
933 Memory.FillRAM[Address] = Byte;
934 // Go flag has been changed
941 Memory.FillRAM[Address] = Byte;
945 Memory.FillRAM[Address] = Byte;
948 Memory.FillRAM[Address] = Byte;
951 Memory.FillRAM[Address] = Byte & 0x7f;
954 Memory.FillRAM[Address] = Byte & 0x7f;
957 Memory.FillRAM[Address] = Byte;
960 Memory.FillRAM[Address] = Byte;
963 Memory.FillRAM[Address] = Byte;
966 Memory.FillRAM[Address] = Byte;
971 Memory.FillRAM[Address] = Byte;
974 Memory.FillRAM[Address] = Byte;
975 Memory.FillRAM[0x3000 + GSU_SFR] |= FLG_G;
980 Memory.FillRAM[Address] = Byte;
981 if (Address >= 0x3100)
983 FxCacheWriteAccess(Address);
990 Memory.FillRAM[Address] = Byte;
993 /**********************************************************************************************/
995 /* This function retrieves a PPU Register */
996 /**********************************************************************************************/
997 uint8 S9xGetPPU(uint16 Address)
1001 if (Address <= 0x2190)
1007 return (Memory.FillRAM[Address]);
1010 missing.oam_address_read = 1;
1012 return (uint8) (PPU.OAMAddr);
1014 return (((PPU.OAMAddr >> 8) & 1) | (PPU.OAMPriorityRotation << 7));
1024 return (Memory.FillRAM[Address]);
1034 missing.bg_offset_read = 1;
1036 return (Memory.FillRAM[Address]);
1038 return (Memory.FillRAM[Address]);
1040 return (uint8) (PPU.VMA.Address);
1042 return (PPU.VMA.Address >> 8);
1046 return (Memory.FillRAM[Address]);
1054 missing.matrix_read = 1;
1056 return (Memory.FillRAM[Address]);
1077 return (Memory.FillRAM[Address]);
1082 // 16bit x 8bit multiply read result.
1083 if (PPU.Need16x8Mulitply)
1085 int32 r = (int32) PPU.MatrixA * (int32) (PPU.MatrixB >> 8);
1087 Memory.FillRAM[0x2134] = (uint8) r;
1088 Memory.FillRAM[0x2135] = (uint8) (r >> 8);
1089 Memory.FillRAM[0x2136] = (uint8) (r >> 16);
1090 PPU.Need16x8Mulitply = FALSE;
1093 missing.matrix_multiply = 1;
1095 return (Memory.FillRAM[Address]);
1097 // Latch h and v counters
1099 missing.h_v_latch = 1;
1103 CPU.WaitAddress = CPU.PCAtOpcodeStart;
1106 PPU.HVBeamCounterLatched = 1;
1107 PPU.VBeamPosLatched = (uint16)
1109 PPU.HBeamPosLatched = (uint16) ((CPU.Cycles * SNES_HCOUNTER_MAX) / Settings.H_Max);
1111 // Causes screen flicker for Yoshi's Island if uncommented
1112 //CLEAR_IRQ_SOURCE (PPU_V_BEAM_IRQ_SOURCE | PPU_H_BEAM_IRQ_SOURCE);
1114 if (SNESGameFixes.NeedInit0x2137)
1115 PPU.VBeamFlip = 0; //jyam sword world sfc2 & godzill
1118 // Read OAM (sprite) control data
1119 if (!PPU.OAMReadFlip)
1121 byte = PPU.OAMData[PPU.OAMAddr << 1];
1125 byte = PPU.OAMData[(PPU.OAMAddr << 1) + 1];
1126 if (++PPU.OAMAddr >= 0x110)
1129 PPU.OAMReadFlip ^= 1;
1131 missing.oam_read = 1;
1136 // Read vram low byte
1138 missing.vram_read = 1;
1140 if (IPPU.FirstVRAMRead)
1141 byte = Memory.VRAM[PPU.VMA.Address << 1];
1142 else if (PPU.VMA.FullGraphicCount)
1144 uint32 addr = PPU.VMA.Address - 1;
1145 uint32 rem = addr & PPU.VMA.Mask1;
1147 (addr & ~PPU.VMA.Mask1)
1148 + (rem >> PPU.VMA.Shift)
1149 + ((rem & (PPU.VMA.FullGraphicCount - 1)) << 3);
1150 byte = Memory.VRAM[((address << 1) - 2) & 0xFFFF];
1153 byte = Memory.VRAM[((PPU.VMA.Address << 1) - 2) & 0xffff];
1157 PPU.VMA.Address += PPU.VMA.Increment;
1158 IPPU.FirstVRAMRead = FALSE;
1162 // Read vram high byte
1164 missing.vram_read = 1;
1166 if (IPPU.FirstVRAMRead)
1167 byte = Memory.VRAM[((PPU.VMA.Address << 1) + 1) & 0xffff];
1168 else if (PPU.VMA.FullGraphicCount)
1170 uint32 addr = PPU.VMA.Address - 1;
1171 uint32 rem = addr & PPU.VMA.Mask1;
1173 (addr & ~PPU.VMA.Mask1)
1174 + (rem >> PPU.VMA.Shift)
1175 + ((rem & (PPU.VMA.FullGraphicCount - 1)) << 3);
1176 byte = Memory.VRAM[((address << 1) - 1) & 0xFFFF];
1179 byte = Memory.VRAM[((PPU.VMA.Address << 1) - 1) & 0xFFFF];
1182 PPU.VMA.Address += PPU.VMA.Increment;
1183 IPPU.FirstVRAMRead = FALSE;
1188 // Read palette data
1190 missing.cgram_read = 1;
1193 byte = PPU.CGDATA[PPU.CGADD++] >> 8;
1195 byte = PPU.CGDATA[PPU.CGADD] & 0xff;
1197 PPU.CGFLIPRead ^= 1;
1201 // Horizontal counter value 0-339
1203 missing.h_counter_read = 1;
1206 byte = PPU.HBeamPosLatched >> 8;
1208 byte = (uint8) PPU.HBeamPosLatched;
1212 // Vertical counter value 0-262
1214 missing.v_counter_read = 1;
1217 byte = PPU.VBeamPosLatched >> 8;
1219 byte = (uint8) PPU.VBeamPosLatched;
1223 // PPU time and range over flags
1224 return (SNESGameFixes._0x213E_ReturnValue);
1227 // NTSC/PAL and which field flags
1228 PPU.VBeamFlip = PPU.HBeamFlip = 0;
1229 return ((Settings.PAL ? 0x10 : 0) | (Memory.FillRAM[0x213f] & 0xc0));
1296 return ((uint8) _SPCOutP[Address & 3]);
1298 // CPU.Flags |= DEBUG_MODE_FLAG;
1299 #ifdef SPC700_SHUTDOWN
1300 CPU.APU_APUExecuting = Settings.APUEnabled;
1303 if(Settings.APUEnabled)
1306 //CPU.WaitAddress = CPU.PCAtOpcodeStart;
1308 if(SNESGameFixes.APU_OutPorts_ReturnValueFix
1309 && Address >= 0x2140
1310 && Address <= 0x2143
1313 return (uint8) ((Address & 1) ?
1314 ((rand() & 0xff00) >> 8) : (rand() & 0xff));
1317 return (APU.OutPorts[Address & 3]);
1320 switch (Settings.SoundSkipMethod)
1324 CPU.BranchSkip = TRUE;
1329 CPU.BranchSkip = TRUE;
1332 if ((Address & 3) < 2)
1338 return ((Address & 3) == 1 ? 0xaa : 0xbb);
1340 return ((r >> 3) & 0xff);
1347 return ((r >> 3) & 0xff);
1349 return (Memory.FillRAM[Address]);
1355 missing.wram_read = 1;
1357 byte = Memory.RAM[PPU.WRAM++];
1358 PPU.WRAM &= 0x1FFFF;
1363 return (Memory.FillRAM[Address]);
1372 return (S9xGetSA1(Address));
1374 if (Address <= 0x2fff || Address >= 0x3000 + 768)
1383 // For Dai Kaijyu Monogatari II
1385 return (S9xGetSRTC(Address));
1390 missing.unknownppu_read = Address;
1391 if (Settings.TraceUnknownRegisters)
1393 sprintf(String, "Unknown register read: $%04X\n", Address);
1394 S9xMessage(S9X_TRACE, S9X_PPU_TRACE, String);
1398 return (0); //Memory.FillRAM[Address]);
1402 if (!Settings.SuperFX)
1404 byte = Memory.FillRAM[Address];
1406 //if (Address != 0x3030 && Address != 0x3031)
1407 //printf ("%04x\n", Address);
1409 if (Address == 0x3030)
1411 CPU.WaitAddress = CPU.PCAtOpcodeStart;
1415 if (Address == 0x3031)
1417 CLEAR_IRQ_SOURCE(GSU_IRQ_SOURCE);
1418 Memory.FillRAM[0x3031] = byte & 0x7f;
1426 /**********************************************************************************************/
1428 /* This function sets a CPU/DMA Register to a specific byte */
1429 /**********************************************************************************************/
1430 void S9xSetCPU(uint8 byte, uint16 Address)
1434 if (Address < 0x4200)
1437 CPU.Cycles += ONE_CYCLE;
1442 // S9xReset reading of old-style joypads
1443 if ((byte & 1) && !(Memory.FillRAM[Address] & 1))
1445 PPU.Joypad1ButtonReadPos = 0;
1446 PPU.Joypad2ButtonReadPos = 0;
1447 PPU.Joypad3ButtonReadPos = 0;
1454 missing.unknowncpu_write = Address;
1455 if (Settings.TraceUnknownRegisters)
1457 sprintf(String, "Unknown register register write: $%02X->$%04X\n", byte, Address);
1458 S9xMessage(S9X_TRACE, S9X_PPU_TRACE, String);
1468 // NMI, V & H IRQ and joypad reading enable flags
1470 && (!SNESGameFixes.umiharakawaseFix || PPU.IRQVBeamPos < 209))
1472 if (!PPU.VTimerEnabled)
1476 missing.virq_pos = PPU.IRQVBeamPos;
1478 PPU.VTimerEnabled = TRUE;
1479 if (PPU.HTimerEnabled)
1481 else if (PPU.IRQVBeamPos == CPU.V_Counter)
1482 S9xSetIRQ(PPU_V_BEAM_IRQ_SOURCE);
1487 PPU.VTimerEnabled = FALSE;
1488 #ifndef RC_OPTIMIZED
1489 if (SNESGameFixes.umiharakawaseFix)
1496 if (!PPU.HTimerEnabled)
1500 missing.hirq_pos = PPU.IRQHBeamPos;
1502 PPU.HTimerEnabled = TRUE;
1508 // No need to check for HTimer being disabled as the scanline
1509 // event trigger code won't trigger an H-IRQ unless its enabled.
1510 PPU.HTimerEnabled = FALSE;
1511 PPU.HTimerPosition = Settings.H_Max + 1;
1514 #ifndef RC_OPTIMIZED
1515 if (!Settings.DaffyDuck)
1516 CLEAR_IRQ_SOURCE(PPU_V_BEAM_IRQ_SOURCE | PPU_H_BEAM_IRQ_SOURCE);
1519 && !(Memory.FillRAM[0x4200] & 0x80)
1520 && CPU.V_Counter >= PPU.ScreenHeight + FIRST_VISIBLE_LINE
1521 && CPU.V_Counter <= PPU.ScreenHeight + (SNESGameFixes.alienVSpredetorFix ? 25 : 15)
1522 && //jyam 15->25 alien vs predetor
1523 // Panic Bomberman clears the NMI pending flag @ scanline 230 before enabling
1524 // NMIs again. The NMI routine crashes the CPU if it is called without the NMI
1525 // pending flag being set...
1526 (Memory.FillRAM[0x4210] & 0x80) && !CPU.NMIActive)
1528 CPU.Flags |= NMI_FLAG;
1529 CPU.NMIActive = TRUE;
1530 CPU.NMICycleCount = CPU.NMITriggerPoint;
1537 // Multiplier (for multply)
1542 uint32 res = Memory.FillRAM[0x4202] * byte;
1544 Memory.FillRAM[0x4216] = (uint8) res;
1545 Memory.FillRAM[0x4217] = (uint8) (res >> 8);
1550 // Low and high muliplier (for divide)
1556 Memory.FillRAM[0x4204] + (Memory.FillRAM[0x4205] << 8);
1557 uint16 div = byte ? a / byte : 0xffff;
1558 uint16 rem = byte ? a % byte : a;
1560 Memory.FillRAM[0x4214] = (uint8) div;
1561 Memory.FillRAM[0x4215] = div >> 8;
1562 Memory.FillRAM[0x4216] = (uint8) rem;
1563 Memory.FillRAM[0x4217] = rem >> 8;
1567 d = PPU.IRQHBeamPos;
1568 PPU.IRQHBeamPos = (PPU.IRQHBeamPos & 0xFF00) | byte;
1570 if (PPU.HTimerEnabled && PPU.IRQHBeamPos != d)
1575 d = PPU.IRQHBeamPos;
1576 PPU.IRQHBeamPos = (PPU.IRQHBeamPos & 0xFF) | ((byte & 1) << 8);
1578 if (PPU.HTimerEnabled && PPU.IRQHBeamPos != d)
1584 d = PPU.IRQVBeamPos;
1585 PPU.IRQVBeamPos = (PPU.IRQVBeamPos & 0xFF00) | byte;
1587 missing.virq_pos = PPU.IRQVBeamPos;
1589 if (PPU.VTimerEnabled && PPU.IRQVBeamPos != d)
1591 if (PPU.HTimerEnabled)
1595 if (PPU.IRQVBeamPos == CPU.V_Counter)
1596 S9xSetIRQ(PPU_V_BEAM_IRQ_SOURCE);
1602 d = PPU.IRQVBeamPos;
1603 PPU.IRQVBeamPos = (PPU.IRQVBeamPos & 0xFF) | ((byte & 1) << 8);
1605 missing.virq_pos = PPU.IRQVBeamPos;
1607 if (PPU.VTimerEnabled && PPU.IRQVBeamPos != d)
1609 if (PPU.HTimerEnabled)
1613 if (PPU.IRQVBeamPos == CPU.V_Counter)
1614 S9xSetIRQ(PPU_V_BEAM_IRQ_SOURCE);
1621 missing.dma_this_frame = byte;
1622 missing.dma_channels = byte;
1624 if ((byte & 0x01) != 0)
1626 if ((byte & 0x02) != 0)
1628 if ((byte & 0x04) != 0)
1630 if ((byte & 0x08) != 0)
1632 if ((byte & 0x10) != 0)
1634 if ((byte & 0x20) != 0)
1636 if ((byte & 0x40) != 0)
1638 if ((byte & 0x80) != 0)
1643 missing.hdma_this_frame |= byte;
1644 missing.hdma_channels |= byte;
1646 if (Settings.DisableHDMA)
1648 Memory.FillRAM[0x420c] = byte;
1653 // Cycle speed 0 - 2.68Mhz, 1 - 3.58Mhz (banks 0x80 +)
1654 if ((byte & 1) != (Memory.FillRAM[0x420d] & 1))
1658 CPU.FastROMSpeed = ONE_CYCLE;
1660 missing.fast_rom = 1;
1664 CPU.FastROMSpeed = SLOW_ONE_CYCLE;
1666 Memory.FixROMSpeed();
1674 // NMI ocurred flag (reset on read or write)
1675 Memory.FillRAM[0x4210] = 0;
1678 // IRQ ocurred flag (reset on read or write)
1679 CLEAR_IRQ_SOURCE(PPU_V_BEAM_IRQ_SOURCE | PPU_H_BEAM_IRQ_SOURCE);
1682 // v-blank, h-blank and joypad being scanned flags (read-only)
1684 // I/O Port (read-only)
1687 // Quotent of divide (read-only)
1690 // Multiply product (read-only)
1700 // Joypad values (read-only)
1711 d = (Address >> 4) & 0x7;
1712 DMA[d].TransferDirection = (byte & 128) != 0 ? 1 : 0;
1713 DMA[d].HDMAIndirectAddressing = (byte & 64) != 0 ? 1 : 0;
1714 DMA[d].AAddressDecrement = (byte & 16) != 0 ? 1 : 0;
1715 DMA[d].AAddressFixed = (byte & 8) != 0 ? 1 : 0;
1716 DMA[d].TransferMode = (byte & 7);
1727 DMA[((Address >> 4) & 0x7)].BAddress = byte;
1738 d = (Address >> 4) & 0x7;
1739 DMA[d].AAddress &= 0xFF00;
1740 DMA[d].AAddress |= byte;
1751 d = (Address >> 4) & 0x7;
1752 DMA[d].AAddress &= 0xFF;
1753 DMA[d].AAddress |= byte << 8;
1764 DMA[((Address >> 4) & 0x7)].ABank = byte;
1775 d = (Address >> 4) & 0x7;
1776 DMA[d].TransferBytes &= 0xFF00;
1777 DMA[d].TransferBytes |= byte;
1778 DMA[d].IndirectAddress &= 0xff00;
1779 DMA[d].IndirectAddress |= byte;
1790 d = (Address >> 4) & 0x7;
1791 DMA[d].TransferBytes &= 0xFF;
1792 DMA[d].TransferBytes |= byte << 8;
1793 DMA[d].IndirectAddress &= 0xff;
1794 DMA[d].IndirectAddress |= byte << 8;
1805 DMA[d = ((Address >> 4) & 0x7)].IndirectBank = byte;
1816 d = (Address >> 4) & 7;
1817 DMA[d].Address &= 0xff00;
1818 DMA[d].Address |= byte;
1829 d = (Address >> 4) & 0x7;
1830 DMA[d].Address &= 0xff;
1831 DMA[d].Address |= byte << 8;
1842 d = (Address >> 4) & 0x7;
1843 DMA[d].LineCount = byte & 0x7f;
1844 DMA[d].Repeat = !(byte & 0x80);
1851 //printf ("%02x->%04x\n", byte, Address);
1858 //printf ("%02x->%04x\n", byte, Address);
1860 S9xSetSDD1MemoryMap(Address - 0x4804, byte & 7);
1864 missing.unknowncpu_write = Address;
1865 if (Settings.TraceUnknownRegisters)
1869 "Unknown register write: $%02X->$%04X\n",
1872 S9xMessage(S9X_TRACE, S9X_PPU_TRACE, String);
1877 Memory.FillRAM[Address] = byte;
1880 /**********************************************************************************************/
1882 /* This function retrieves a CPU/DMA Register */
1883 /**********************************************************************************************/
1884 uint8 S9xGetCPU(uint16 Address)
1888 if (Address < 0x4200)
1891 CPU.Cycles += ONE_CYCLE;
1895 // Secret of the Evermore
1902 if (Memory.FillRAM[0x4016] & 1)
1904 if ((!Settings.SwapJoypads
1905 && IPPU.Controller == SNES_MOUSE_SWAPPED)
1906 || (Settings.SwapJoypads
1907 && IPPU.Controller == SNES_MOUSE))
1909 if (++PPU.MouseSpeed[0] > 2)
1910 PPU.MouseSpeed[0] = 0;
1915 int ind = Settings.SwapJoypads ? 1 : 0;
1916 byte = IPPU.Joypads[ind] >> (PPU.Joypad1ButtonReadPos ^ 15);
1917 PPU.Joypad1ButtonReadPos++;
1922 if (Memory.FillRAM[0x4016] & 1)
1924 // MultiPlayer5 adaptor is only allowed to be plugged into port 2
1925 switch (IPPU.Controller)
1927 case SNES_MULTIPLAYER5 :
1929 case SNES_MOUSE_SWAPPED :
1930 if (Settings.SwapJoypads
1931 && ++PPU.MouseSpeed[0] > 2)
1932 PPU.MouseSpeed[0] = 0;
1936 if (!Settings.SwapJoypads
1937 && ++PPU.MouseSpeed[0] > 2)
1938 PPU.MouseSpeed[0] = 0;
1944 int ind = Settings.SwapJoypads ? 0 : 1;
1946 if (IPPU.Controller == SNES_MULTIPLAYER5)
1948 if (Memory.FillRAM[0x4201] & 0x80)
1952 >> (PPU.Joypad2ButtonReadPos ^ 15))
1954 | (((IPPU.Joypads[2]
1955 >> (PPU.Joypad2ButtonReadPos ^ 15))
1958 PPU.Joypad2ButtonReadPos++;
1965 >> (PPU.Joypad3ButtonReadPos ^ 15))
1967 | (((IPPU.Joypads[4]
1968 >> (PPU.Joypad3ButtonReadPos ^ 15))
1971 PPU.Joypad3ButtonReadPos++;
1977 >> (PPU.Joypad2ButtonReadPos++ ^ 15))
1982 missing.unknowncpu_read = Address;
1983 if (Settings.TraceUnknownRegisters)
1985 sprintf(String, "Unknown register read: $%04X\n", Address);
1986 S9xMessage(S9X_TRACE, S9X_PPU_TRACE, String);
1991 return (Memory.FillRAM[Address]);
1996 // BS Dynami Tracer! needs to be able to check if NMIs are enabled
1997 // already, otherwise the game locks up.
1999 // NMI, h & v timers and joypad reading enable
2000 if (SNESGameFixes.Old_Read0x4200)
2003 CPU.WaitAddress = CPU.PCAtOpcodeStart;
2005 return (REGISTER_4212());
2008 // I/O port (output - write only?)
2011 // Multiplier and multiplicand (write)
2015 // Divisor and dividend (write)
2016 return (Memory.FillRAM[Address]);
2018 return (uint8) (PPU.IRQHBeamPos);
2020 return (PPU.IRQHBeamPos >> 8);
2022 return (uint8) (PPU.IRQVBeamPos);
2024 return (PPU.IRQVBeamPos >> 8);
2026 // General purpose DMA enable
2027 // Super Formation Soccer 95 della Serie A UCC Xaqua requires this
2028 // register should not always return zero.
2029 // .. But Aero 2 waits until this register goes zero..
2030 // Just keep toggling the value for now in the hope that it breaks
2031 // the game out of its wait loop...
2032 Memory.FillRAM[0x420b] = !Memory.FillRAM[0x420b];
2033 return (Memory.FillRAM[0x420b]);
2038 // Cycle speed 0 - 2.68Mhz, 1 - 3.58Mhz (banks 0x80 +)
2039 return (Memory.FillRAM[Address]);
2043 return (Memory.FillRAM[Address]);
2046 CPU.WaitAddress = CPU.PCAtOpcodeStart;
2048 byte = Memory.FillRAM[0x4210];
2049 Memory.FillRAM[0x4210] = 0;
2054 & (PPU_V_BEAM_IRQ_SOURCE | PPU_H_BEAM_IRQ_SOURCE))
2057 // Super Robot Wars Ex ROM bug requires this.
2058 byte |= CPU.Cycles >= Settings.HBlankStart ? 0x40 : 0;
2059 CLEAR_IRQ_SOURCE(PPU_V_BEAM_IRQ_SOURCE | PPU_H_BEAM_IRQ_SOURCE);
2062 // V-blank, h-blank and joypads being read flags (read-only)
2064 CPU.WaitAddress = CPU.PCAtOpcodeStart;
2066 return (REGISTER_4212());
2071 // Quotient of divide result
2074 // Multiplcation result (for multiply) or remainder of
2076 return (Memory.FillRAM[Address]);
2085 // Joypads 1-4 button and direction state.
2086 return (Memory.FillRAM[Address]);
2096 // DMA direction, address type, fixed flag,
2097 return (Memory.FillRAM[Address]);
2107 return (Memory.FillRAM[Address]);
2117 return (Memory.FillRAM[Address]);
2127 return (Memory.FillRAM[Address]);
2137 return (Memory.FillRAM[Address]);
2147 return (Memory.FillRAM[Address]);
2157 return (Memory.FillRAM[Address]);
2167 return (DMA[(Address >> 4) & 7].IndirectBank);
2177 return (Memory.FillRAM[Address]);
2187 return (Memory.FillRAM[Address]);
2198 int d = (Address & 0x70) >> 4;
2199 if (IPPU.HDMA & (1 << d))
2201 return (DMA[d].LineCount);
2203 return (Memory.FillRAM[Address]);
2207 missing.unknowncpu_read = Address;
2208 if (Settings.TraceUnknownRegisters)
2210 sprintf(String, "Unknown register read: $%04X\n", Address);
2211 S9xMessage(S9X_TRACE, S9X_PPU_TRACE, String);
2217 return (Memory.FillRAM[Address]);
2223 PPU.BG3Priority = 0;
2226 PPU.VMA.Increment = 1;
2227 PPU.VMA.Address = 0;
2228 PPU.VMA.FullGraphicCount = 0;
2231 for (uint8 B = 0; B != 4; B++)
2233 PPU.BG[B].SCBase = 0;
2234 PPU.BG[B].VOffset = 0;
2235 PPU.BG[B].HOffset = 0;
2236 PPU.BG[B].BGSize = 0;
2237 PPU.BG[B].NameBase = 0;
2238 PPU.BG[B].SCSize = 0;
2240 PPU.ClipCounts[B] = 0;
2241 PPU.ClipWindowOverlapLogic[B] = CLIP_OR;
2242 PPU.ClipWindow1Enable[B] = FALSE;
2243 PPU.ClipWindow2Enable[B] = FALSE;
2244 PPU.ClipWindow1Inside[B] = TRUE;
2245 PPU.ClipWindow2Inside[B] = TRUE;
2248 PPU.ClipCounts[4] = 0;
2249 PPU.ClipCounts[5] = 0;
2250 PPU.ClipWindowOverlapLogic[4] = PPU.ClipWindowOverlapLogic[5] = CLIP_OR;
2251 PPU.ClipWindow1Enable[4] = PPU.ClipWindow1Enable[5] = FALSE;
2252 PPU.ClipWindow2Enable[4] = PPU.ClipWindow2Enable[5] = FALSE;
2253 PPU.ClipWindow1Inside[4] = PPU.ClipWindow1Inside[5] = TRUE;
2254 PPU.ClipWindow2Inside[4] = PPU.ClipWindow2Inside[5] = TRUE;
2258 for (c = 0; c < 256; c++)
2260 IPPU.Red[c] = (c & 7) << 2;
2261 IPPU.Green[c] = ((c >> 3) & 7) << 2;
2262 IPPU.Blue[c] = ((c >> 6) & 2) << 3;
2264 IPPU.Red[c] | (IPPU.Green[c] << 5) | (IPPU.Blue[c] << 10);
2267 PPU.FirstSprite = 0;
2268 PPU.LastSprite = 127;
2269 for (int Sprite = 0; Sprite < 128; Sprite++)
2271 PPU.OBJ[Sprite].HPos = 0;
2272 PPU.OBJ[Sprite].VPos = 0;
2273 PPU.OBJ[Sprite].VFlip = 0;
2274 PPU.OBJ[Sprite].HFlip = 0;
2275 PPU.OBJ[Sprite].Priority = 0;
2276 PPU.OBJ[Sprite].Palette = 0;
2277 PPU.OBJ[Sprite].Name = 0;
2278 PPU.OBJ[Sprite].Size = 0;
2280 PPU.OAMPriorityRotation = 0;
2283 PPU.OAMTileAddress = 0;
2285 PPU.IRQVBeamPos = 0;
2286 PPU.IRQHBeamPos = 0;
2287 PPU.VBeamPosLatched = 0;
2288 PPU.HBeamPosLatched = 0;
2292 PPU.HVBeamCounterLatched = 0;
2294 PPU.MatrixA = PPU.MatrixB = PPU.MatrixC = PPU.MatrixD = 0;
2295 PPU.CentreX = PPU.CentreY = 0;
2296 PPU.Joypad1ButtonReadPos = 0;
2297 PPU.Joypad2ButtonReadPos = 0;
2298 PPU.Joypad3ButtonReadPos = 0;
2301 PPU.FixedColourRed = PPU.FixedColourGreen = PPU.FixedColourBlue = 0;
2302 PPU.SavedOAMAddr = 0;
2303 PPU.ScreenHeight = SNES_HEIGHT;
2306 PPU.ForcedBlanking = TRUE;
2307 PPU.OBJThroughMain = FALSE;
2308 PPU.OBJThroughSub = FALSE;
2309 PPU.OBJSizeSelect = 0;
2310 PPU.OBJNameSelect = 0;
2311 PPU.OBJNameBase = 0;
2312 PPU.OBJAddition = FALSE;
2313 PPU.OAMReadFlip = 0;
2314 ZeroMemory(PPU.OAMData, 512 + 32);
2316 PPU.VTimerEnabled = FALSE;
2317 PPU.HTimerEnabled = FALSE;
2318 PPU.HTimerPosition = Settings.H_Max + 1;
2320 PPU.BGMosaic[0] = PPU.BGMosaic[1] = FALSE;
2321 PPU.BGMosaic[2] = PPU.BGMosaic[3] = FALSE;
2322 PPU.Mode7HFlip = FALSE;
2323 PPU.Mode7VFlip = FALSE;
2324 PPU.Mode7Repeat = 0;
2325 PPU.Window1Left = 1;
2326 PPU.Window1Right = 0;
2327 PPU.Window2Left = 1;
2328 PPU.Window2Right = 0;
2329 PPU.RecomputeClipWindows = TRUE;
2331 PPU.Need16x8Mulitply = FALSE;
2332 PPU.MouseSpeed[0] = PPU.MouseSpeed[1] = 0;
2334 IPPU.ColorsChanged = TRUE;
2336 IPPU.HDMAStarted = FALSE;
2337 IPPU.MaxBrightness = 0;
2338 IPPU.LatchedBlanking = 0;
2339 IPPU.OBJChanged = TRUE;
2340 IPPU.RenderThisFrame = TRUE;
2341 IPPU.DirectColourMapsNeedRebuild = TRUE;
2342 IPPU.FrameCount = 0;
2343 IPPU.RenderedFramesCount = 0;
2344 IPPU.DisplayedRenderedFrameCount = 0;
2345 IPPU.SkippedFrames = 0;
2347 ZeroMemory(IPPU.TileCached[TILE_2BIT], MAX_2BIT_TILES);
2348 ZeroMemory(IPPU.TileCached[TILE_4BIT], MAX_4BIT_TILES);
2349 ZeroMemory(IPPU.TileCached[TILE_8BIT], MAX_8BIT_TILES);
2350 IPPU.FirstVRAMRead = FALSE;
2351 IPPU.LatchedInterlace = FALSE;
2352 IPPU.DoubleWidthPixels = FALSE;
2353 IPPU.RenderedScreenWidth = SNES_WIDTH;
2354 IPPU.RenderedScreenHeight = SNES_HEIGHT;
2356 for (c = 0; c < 256; c++)
2357 IPPU.ScreenColors[c] = c;
2358 S9xFixColourBrightness();
2359 IPPU.PreviousLine = IPPU.CurrentLine = 0;
2360 IPPU.Joypads[0] = IPPU.Joypads[1] = IPPU.Joypads[2] = 0;
2361 IPPU.Joypads[3] = IPPU.Joypads[4] = 0;
2362 IPPU.SuperScope = 0;
2363 IPPU.Mouse[0] = IPPU.Mouse[1] = 0;
2364 IPPU.PrevMouseX[0] = IPPU.PrevMouseX[1] = 256 / 2;
2365 IPPU.PrevMouseY[0] = IPPU.PrevMouseY[1] = 224 / 2;
2367 if (Settings.ControllerOption == 0)
2368 IPPU.Controller = SNES_MAX_CONTROLLER_OPTIONS - 1;
2370 IPPU.Controller = Settings.ControllerOption - 1;
2371 S9xNextController();
2373 for (c = 0; c < 2; c++)
2374 memset(& IPPU.Clip[c], 0, sizeof(struct ClipData));
2376 if (Settings.MouseMaster)
2381 for (c = 0; c < 0x8000; c += 0x100)
2382 memset(& Memory.FillRAM[c], c >> 8, 0x100);
2384 ZeroMemory(& Memory.FillRAM[0x2100], 0x100);
2385 ZeroMemory(& Memory.FillRAM[0x4200], 0x100);
2386 ZeroMemory(& Memory.FillRAM[0x4000], 0x100);
2387 // For BS Suttehakkun 2...
2388 ZeroMemory(& Memory.FillRAM[0x1000], 0x1000);
2391 void S9xProcessMouse(int which1)
2396 if ((IPPU.Controller == SNES_MOUSE
2397 || IPPU.Controller == SNES_MOUSE_SWAPPED)
2398 && S9xReadMousePosition(which1, &x, &y, &buttons))
2400 int delta_x, delta_y;
2401 #define MOUSE_SIGNATURE 0x1
2402 IPPU.Mouse[which1] =
2404 | (PPU.MouseSpeed[which1] << 4)
2405 | ((buttons & 1) << 6)
2406 | ((buttons & 2) << 6);
2408 delta_x = x - IPPU.PrevMouseX[which1];
2409 delta_y = y - IPPU.PrevMouseY[which1];
2414 IPPU.PrevMouseX[which1] += 63;
2416 else if (delta_x < -63)
2419 IPPU.PrevMouseX[which1] -= 63;
2422 IPPU.PrevMouseX[which1] = x;
2427 IPPU.PrevMouseY[which1] += 63;
2429 else if (delta_y < -63)
2432 IPPU.PrevMouseY[which1] -= 63;
2435 IPPU.PrevMouseY[which1] = y;
2440 IPPU.Mouse[which1] |= (delta_x | 0x80) << 16;
2443 IPPU.Mouse[which1] |= delta_x << 16;
2448 IPPU.Mouse[which1] |= (delta_y | 0x80) << 24;
2451 IPPU.Mouse[which1] |= delta_y << 24;
2453 if (IPPU.Controller == SNES_MOUSE_SWAPPED)
2454 IPPU.Joypads[0] = IPPU.Mouse[which1];
2456 IPPU.Joypads[1] = IPPU.Mouse[which1];
2460 void ProcessSuperScope()
2465 if (IPPU.Controller == SNES_SUPERSCOPE
2466 && S9xReadSuperScopePosition(&x, &y, &buttons))
2468 #define SUPERSCOPE_SIGNATURE 0x00ff
2472 SUPERSCOPE_SIGNATURE
2473 | ((buttons & 1) << (7 + 8))
2474 | ((buttons & 2) << (5 + 8))
2475 | ((buttons & 4) << (3 + 8))
2476 | ((buttons & 8) << (1 + 8));
2481 if (y > PPU.ScreenHeight - 1)
2482 y = PPU.ScreenHeight - 1;
2486 PPU.VBeamPosLatched = (uint16) (y + 1);
2487 PPU.HBeamPosLatched = (uint16) x;
2488 PPU.HVBeamCounterLatched = TRUE;
2489 Memory.FillRAM[0x213F] |= 0x40;
2490 IPPU.Joypads[1] = scope;
2494 void S9xNextController()
2496 switch (IPPU.Controller)
2498 case SNES_MULTIPLAYER5 :
2499 IPPU.Controller = SNES_JOYPAD;
2502 if (Settings.MouseMaster)
2504 IPPU.Controller = SNES_MOUSE_SWAPPED;
2507 case SNES_MOUSE_SWAPPED :
2508 if (Settings.MouseMaster)
2510 IPPU.Controller = SNES_MOUSE;
2514 if (Settings.SuperScopeMaster)
2516 IPPU.Controller = SNES_SUPERSCOPE;
2519 case SNES_SUPERSCOPE :
2520 if (Settings.MultiPlayer5Master)
2522 IPPU.Controller = SNES_MULTIPLAYER5;
2526 IPPU.Controller = SNES_JOYPAD;
2531 void S9xUpdateJoypads()
2538 for (i = 0; i < 5; i++)
2541 IPPU.Joypads[i] = S9xReadJoypad(i);
2542 if (IPPU.Joypads[i] & SNES_LEFT_MASK)
2543 IPPU.Joypads[i] &= ~SNES_RIGHT_MASK;
2544 if (IPPU.Joypads[i] & SNES_UP_MASK)
2545 IPPU.Joypads[i] &= ~SNES_DOWN_MASK;
2548 //touhaiden controller Fix
2549 if (SNESGameFixes.TouhaidenControllerFix
2550 && (IPPU.Controller == SNES_JOYPAD
2551 || IPPU.Controller == SNES_MULTIPLAYER5))
2553 for (i = 0; i < 5; i++)
2555 if (IPPU.Joypads[i])
2556 IPPU.Joypads[i] |= 0xffff0000;
2560 // Read mouse position if enabled
2561 if (Settings.MouseMaster)
2563 for (i = 0; i < 2; i++)
2567 // Read SuperScope if enabled
2568 if (Settings.SuperScopeMaster)
2569 ProcessSuperScope();
2571 if (Memory.FillRAM[0x4200] & 1)
2573 PPU.Joypad1ButtonReadPos = 16;
2574 if (Memory.FillRAM[0x4201] & 0x80)
2576 PPU.Joypad2ButtonReadPos = 16;
2577 PPU.Joypad3ButtonReadPos = 0;
2581 PPU.Joypad2ButtonReadPos = 0;
2582 PPU.Joypad3ButtonReadPos = 16;
2584 int ind = Settings.SwapJoypads ? 1 : 0;
2586 Memory.FillRAM[0x4218] = (uint8) IPPU.Joypads[ind];
2587 Memory.FillRAM[0x4219] = (uint8) (IPPU.Joypads[ind] >> 8);
2588 Memory.FillRAM[0x421a] = (uint8) IPPU.Joypads[ind ^ 1];
2589 Memory.FillRAM[0x421b] = (uint8) (IPPU.Joypads[ind ^ 1] >> 8);
2590 if (Memory.FillRAM[0x4201] & 0x80)
2592 Memory.FillRAM[0x421c] = (uint8) IPPU.Joypads[ind];
2593 Memory.FillRAM[0x421d] = (uint8) (IPPU.Joypads[ind] >> 8);
2594 Memory.FillRAM[0x421e] = (uint8) IPPU.Joypads[2];
2595 Memory.FillRAM[0x421f] = (uint8) (IPPU.Joypads[2] >> 8);
2599 Memory.FillRAM[0x421c] = (uint8) IPPU.Joypads[3];
2600 Memory.FillRAM[0x421d] = (uint8) (IPPU.Joypads[3] >> 8);
2601 Memory.FillRAM[0x421e] = (uint8) IPPU.Joypads[4];
2602 Memory.FillRAM[0x421f] = (uint8) (IPPU.Joypads[4] >> 8);
2608 void S9xSuperFXExec()
2611 if (Settings.SuperFX)
2613 if ((Memory.FillRAM[0x3000 + GSU_SFR] & FLG_G)
2614 && (Memory.FillRAM[0x3000 + GSU_SCMR] & 0x18) == 0x18)
2616 if (!Settings.WinterGold)
2618 else FxEmulate((Memory.FillRAM[0x3000 + GSU_CLSR] & 1) ? 700 : 350);
2619 int GSUStatus = Memory.FillRAM[0x3000
2620 + GSU_SFR] | (Memory.FillRAM[0x3000 + GSU_SFR + 1] << 8);
2621 if ((GSUStatus & (FLG_G | FLG_IRQ)) == FLG_IRQ)
2623 // Trigger a GSU IRQ.
2624 S9xSetIRQ(GSU_IRQ_SOURCE);
2629 uint32 tmp = (Memory.FillRAM[0x3034] << 16)
2630 + * (uint16 *) & Memory.FillRAM[0x301e];
2632 if (tmp == 0x018428)
2635 & SRAM[0x0064] = 0xbc00;
2636 * (uint16 *) & SRAM[0x002c] = 0x8000;
2639 if (tmp == -1) //0x018428) //0x01bfc3) //0x09edaf) //-1) //0x57edaf)
2641 while (Memory.FillRAM[0x3030] & 0x20)
2652 /* Make the string 32 chars long */
2653 if (strlen(tmp) < 32)
2655 memset(& tmp[strlen(tmp)], ' ', 32 - strlen(tmp));
2659 /* Copy registers (so we can see if any changed) */
2660 vColr = FxGetColorRegister();
2661 vPor = FxGetPlotOptionRegister();
2662 memcpy(avReg, SuperFX.pvRegisters, 0x40);
2664 /* Print the pipe string */
2667 /* Execute the instruction in the pipe */
2669 vError = FxEmulate(1);
2671 /* Check if any registers changed (and print them if they did) */
2672 for (i = 0; i < 16; i++)
2676 ((uint32) avReg[i * 2])
2677 | (((uint32) avReg[(i * 2) + 1]) << 8);
2679 (uint32) (SuperFX.pvRegisters[i * 2])
2680 | (((uint32) SuperFX.pvRegisters[(i * 2) + 1]) << 8);
2682 a = OPCODE_BYTES(vPipe);
2683 if (((r1 + a) & 0xffff) != r2)
2684 printf(" r%d=$%04x", i, r2);
2689 ((uint32) avReg[0x30]) | (((uint32) avReg[0x31]) << 8);
2691 (uint32) (SuperFX.pvRegisters[0x30])
2692 | (((uint32) SuperFX.pvRegisters[0x31]) << 8);
2693 if ((r1 & (1 << 1)) != (r2 & (1 << 1)))
2694 printf(" Z=%d", (uint32) (!!(r2 & (1 << 1))));
2695 if ((r1 & (1 << 2)) != (r2 & (1 << 2)))
2696 printf(" CY=%d", (uint32) (!!(r2 & (1 << 2))));
2697 if ((r1 & (1 << 3)) != (r2 & (1 << 3)))
2698 printf(" S=%d", (uint32) (!!(r2 & (1 << 3))));
2699 if ((r1 & (1 << 4)) != (r2 & (1 << 4)))
2700 printf(" OV=%d", (uint32) (!!(r2 & (1 << 4))));
2701 if ((r1 & (1 << 5)) != (r2 & (1 << 5)))
2702 printf(" G=%d", (uint32) (!!(r2 & (1 << 5))));
2703 if ((r1 & (1 << 6)) != (r2 & (1 << 6)))
2704 printf(" R=%d", (uint32) (!!(r2 & (1 << 6))));
2705 if ((r1 & (1 << 8)) != (r2 & (1 << 8)))
2706 printf(" ALT1=%d", (uint32) (!!(r2 & (1 << 8))));
2707 if ((r1 & (1 << 9)) != (r2 & (1 << 9)))
2708 printf(" ALT2=%d", (uint32) (!!(r2 & (1 << 9))));
2709 if ((r1 & (1 << 10)) != (r2 & (1 << 10)))
2710 printf(" IL=%d", (uint32) (!!(r2 & (1 << 10))));
2711 if ((r1 & (1 << 11)) != (r2 & (1 << 11)))
2712 printf(" IH=%d", (uint32) (!!(r2 & (1 << 11))));
2713 if ((r1 & (1 << 12)) != (r2 & (1 << 12)))
2714 printf(" B=%d", (uint32) (!!(r2 & (1 << 12))));
2715 if ((r1 & (1 << 15)) != (r2 & (1 << 15)))
2716 printf(" IRQ=%d", (uint32) (!!(r2 & (1 << 15))));
2720 uint32 r1 = ((uint32) avReg[0x34]);
2721 uint32 r2 = (uint32) (SuperFX.pvRegisters[0x34]);
2723 printf(" PBR=$%02x", r2);
2727 uint32 r1 = ((uint32) avReg[0x36]);
2728 uint32 r2 = (uint32) (SuperFX.pvRegisters[0x36]);
2730 printf(" ROMBR=$%02x", r2);
2734 uint32 r1 = ((uint32) avReg[0x3c]);
2735 uint32 r2 = (uint32) (SuperFX.pvRegisters[0x3c]);
2737 printf(" RAMBR=$%02x", r2);
2742 ((uint32) avReg[0x3e]) | (((uint32) avReg[0x3f]) << 8);
2744 (uint32) (SuperFX.pvRegisters[0x3e])
2745 | (((uint32) SuperFX.pvRegisters[0x3f]) << 8);
2747 printf(" CBR=$%04x", r2);
2751 if (vColr != FxGetColorRegister())
2752 printf(" COLR=$%02x", FxGetColorRegister());
2756 if (vPor != FxGetPlotOptionRegister())
2757 printf(" POR=$%02x", FxGetPlotOptionRegister());
2766 (Memory.FillRAM[0x3034] << 16)
2767 + (Memory.FillRAM[0x301f] << 8)
2768 + (Memory.FillRAM[0x301e] << 0);
2770 printf("%06x: %d\n", t, FxEmulate(2000000));
2771 // FxEmulate (2000000);
2774 if (!(CPU.Flags & TRACE_FLAG))
2776 static int z = 1; if (z == 0)
2778 extern FILE * trace;
2779 CPU.Flags |= TRACE_FLAG;
2780 trace = fopen("trace.log", "wb");
2786 Memory.FillRAM[0x3030] &= ~0x20;
2787 if (Memory.FillRAM[0x3031] & 0x80)
2789 S9xSetIRQ(GSU_IRQ_SOURCE);