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.
62 #include "os9x_asm_cpu.h"
69 #if defined(__showframe__)
74 void S9xMainLoop (void)
76 #if defined(__showframe__)
79 sprintf(stra,"framecpt : %d",framecpt);
86 Registers.PC = CPU.PC - CPU.PCBase;
87 //S9xPackStatus (); // not needed
90 if (CPU.Flags & SCAN_KEYS_FLAG)
92 CPU.Flags &= ~SCAN_KEYS_FLAG;
95 if (CPU.BRKTriggered && Settings.SuperFX && !CPU.TriedInterleavedMode2)
97 CPU.TriedInterleavedMode2 = TRUE;
98 CPU.BRKTriggered = FALSE;
99 S9xDeinterleaveMode2 ();
103 void S9xSetIRQ (uint32 source)
105 CPU.IRQActive |= source;
106 CPU.Flags |= IRQ_PENDING_FLAG;
107 CPU.IRQCycleCount = 3;
108 if (CPU.WaitingForInterrupt)
110 // Force IRQ to trigger immediately after WAI -
111 // Final Fantasy Mystic Quest crashes without this.
112 CPU.IRQCycleCount = 0;
113 CPU.WaitingForInterrupt = FALSE;
118 void S9xClearIRQ (uint32 source)
120 CLEAR_IRQ_SOURCE (source);
124 void S9xDoHBlankProcessing ()
129 switch (CPU.WhichEvent)
131 case HBLANK_START_EVENT:
132 if (IPPU.HDMA && CPU.V_Counter <= PPU.ScreenHeight)
133 IPPU.HDMA = S9xDoHDMA (IPPU.HDMA);
136 case HBLANK_END_EVENT:
137 APU_EXECUTE(3); // notaz: run spc700 in sound 'speed hack' mode
142 CPU.Cycles -= Settings.H_Max;
143 if (/*IAPU.APUExecuting*/CPU.APU_APUExecuting)
144 CPU.APU_Cycles -= Settings.H_Max;
151 if (++CPU.V_Counter > (Settings.PAL ? SNES_MAX_PAL_VCOUNTER : SNES_MAX_NTSC_VCOUNTER))
153 PPU.OAMAddr = PPU.SavedOAMAddr;
156 CPU.NMIActive = FALSE;
158 PPU.HVBeamCounterLatched = 0;
159 CPU.Flags |= SCAN_KEYS_FLAG;
163 if (PPU.VTimerEnabled && !PPU.HTimerEnabled &&
164 CPU.V_Counter == PPU.IRQVBeamPos)
166 S9xSetIRQ (PPU_V_BEAM_IRQ_SOURCE);
169 if (CPU.V_Counter == PPU.ScreenHeight + FIRST_VISIBLE_LINE)
172 S9xEndScreenRefresh ();
175 // Bits 7 and 6 of $4212 are computed when read in S9xGetPPU.
176 missing.dma_this_frame = 0;
177 IPPU.MaxBrightness = PPU.Brightness;
178 PPU.ForcedBlanking = (Memory.FillRAM [0x2100] >> 7) & 1;
180 Memory.FillRAM[0x4210] = 0x80;
181 if (Memory.FillRAM[0x4200] & 0x80)
183 CPU.NMIActive = TRUE;
184 CPU.Flags |= NMI_FLAG;
185 CPU.NMICycleCount = CPU.NMITriggerPoint;
188 #ifdef OLD_SNAPSHOT_CODE
189 if (CPU.Flags & SAVE_SNAPSHOT_FLAG)
191 CPU.Flags &= ~SAVE_SNAPSHOT_FLAG;
192 Registers.PC = CPU.PC - CPU.PCBase;
193 //S9xPackStatus (); // not needed
200 if (CPU.V_Counter == PPU.ScreenHeight + 3)
203 if (CPU.V_Counter == FIRST_VISIBLE_LINE)
205 Memory.FillRAM[0x4210] = 0;
206 CPU.Flags &= ~NMI_FLAG;
207 S9xStartScreenRefresh ();
209 if (CPU.V_Counter >= FIRST_VISIBLE_LINE &&
210 CPU.V_Counter < PPU.ScreenHeight + FIRST_VISIBLE_LINE)
212 RenderLine (CPU.V_Counter - FIRST_VISIBLE_LINE);
214 // Use TimerErrorCounter to skip update of SPC700 timers once
215 // every 128 updates. Needed because this section of code is called
216 // once every emulated 63.5 microseconds, which coresponds to
217 // 15.750KHz, but the SPC700 timers need to be updated at multiples
218 // of 8KHz, hence the error correction.
219 // IAPU.TimerErrorCounter++;
220 // if (IAPU.TimerErrorCounter >= )
221 // IAPU.TimerErrorCounter = 0;
224 if (APU.TimerEnabled [2])
227 while (APU.Timer [2] >= APU.TimerTarget [2])
229 IAPU.RAM [0xff] = (IAPU.RAM [0xff] + 1) & 0xf;
230 APU.Timer [2] -= APU.TimerTarget [2];
231 #ifdef SPC700_SHUTDOWN
233 /*IAPU.APUExecuting*/CPU.APU_APUExecuting= TRUE;
237 if (CPU.V_Counter & 1)
239 if (APU.TimerEnabled [0])
242 if (APU.Timer [0] >= APU.TimerTarget [0])
244 IAPU.RAM [0xfd] = (IAPU.RAM [0xfd] + 1) & 0xf;
246 #ifdef SPC700_SHUTDOWN
248 /*IAPU.APUExecuting*/CPU.APU_APUExecuting = TRUE;
252 if (APU.TimerEnabled [1])
255 if (APU.Timer [1] >= APU.TimerTarget [1])
257 IAPU.RAM [0xfe] = (IAPU.RAM [0xfe] + 1) & 0xf;
259 #ifdef SPC700_SHUTDOWN
261 /*IAPU.APUExecuting*/CPU.APU_APUExecuting = TRUE;
268 case HTIMER_BEFORE_EVENT:
269 case HTIMER_AFTER_EVENT:
270 if (PPU.HTimerEnabled &&
271 (!PPU.VTimerEnabled || CPU.V_Counter == PPU.IRQVBeamPos))
273 S9xSetIRQ (PPU_H_BEAM_IRQ_SOURCE);