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.
57 #if !CONF_BUILD_ASM_CPU
66 #if CONF_BUILD_ASM_CPU
67 #include "os9x_asm_cpu.h"
72 #if defined(__showframe__)
76 void S9xMainLoop (void)
78 #if defined(__showframe__)
81 sprintf(stra,"framecpt : %d",framecpt);
85 #if CONF_BUILD_ASM_CPU
91 if (CPU.Flags & NMI_FLAG) {
92 if (--CPU.NMICycleCount == 0) {
93 CPU.Flags &= ~NMI_FLAG;
94 if (CPU.WaitingForInterrupt) {
95 CPU.WaitingForInterrupt = FALSE;
102 if (CPU.Flags & IRQ_PENDING_FLAG) {
103 if (CPU.IRQCycleCount == 0) {
104 if (CPU.WaitingForInterrupt) {
105 CPU.WaitingForInterrupt = FALSE;
108 if (CPU.IRQActive && !Settings.DisableIRQ) {
112 CPU.Flags &= ~IRQ_PENDING_FLAG;
119 if (CPU.Flags & SCAN_KEYS_FLAG)
124 CPU.PCAtOpcodeStart = CPU.PC;
126 CPU.Cycles += CPU.MemSpeed;
128 (*ICPU.S9xOpcodes[*CPU.PC++].S9xOpcode) ();
139 Registers.PC = CPU.PC - CPU.PCBase;
140 #if !CONF_BUILD_ASM_CPU
145 if (CPU.Flags & SCAN_KEYS_FLAG)
147 CPU.Flags &= ~SCAN_KEYS_FLAG;
150 if (CPU.BRKTriggered && Settings.SuperFX && !CPU.TriedInterleavedMode2)
152 CPU.TriedInterleavedMode2 = TRUE;
153 CPU.BRKTriggered = FALSE;
154 S9xDeinterleaveMode2 ();
158 void S9xSetIRQ (uint32 source)
160 CPU.IRQActive |= source;
161 CPU.Flags |= IRQ_PENDING_FLAG;
162 CPU.IRQCycleCount = 3;
163 if (CPU.WaitingForInterrupt)
165 // Force IRQ to trigger immediately after WAI -
166 // Final Fantasy Mystic Quest crashes without this.
167 CPU.IRQCycleCount = 0;
168 CPU.WaitingForInterrupt = FALSE;
173 void S9xClearIRQ (uint32 source)
175 CLEAR_IRQ_SOURCE (source);
179 void S9xDoHBlankProcessing ()
184 switch (CPU.WhichEvent)
186 case HBLANK_START_EVENT:
187 if (IPPU.HDMA && CPU.V_Counter <= PPU.ScreenHeight)
188 IPPU.HDMA = S9xDoHDMA (IPPU.HDMA);
191 case HBLANK_END_EVENT:
192 APU_EXECUTE(3); // notaz: run spc700 in sound 'speed hack' mode
197 CPU.Cycles -= Settings.H_Max;
198 if (/*IAPU.APUExecuting*/CPU.APU_APUExecuting)
199 CPU.APU_Cycles -= Settings.H_Max;
206 if (++CPU.V_Counter > (Settings.PAL ? SNES_MAX_PAL_VCOUNTER : SNES_MAX_NTSC_VCOUNTER))
208 PPU.OAMAddr = PPU.SavedOAMAddr;
211 CPU.NMIActive = FALSE;
213 PPU.HVBeamCounterLatched = 0;
214 CPU.Flags |= SCAN_KEYS_FLAG;
218 if (PPU.VTimerEnabled && !PPU.HTimerEnabled &&
219 CPU.V_Counter == PPU.IRQVBeamPos)
221 S9xSetIRQ (PPU_V_BEAM_IRQ_SOURCE);
224 if (CPU.V_Counter == PPU.ScreenHeight + FIRST_VISIBLE_LINE)
227 S9xEndScreenRefresh ();
230 // Bits 7 and 6 of $4212 are computed when read in S9xGetPPU.
231 missing.dma_this_frame = 0;
232 IPPU.MaxBrightness = PPU.Brightness;
233 PPU.ForcedBlanking = (Memory.FillRAM [0x2100] >> 7) & 1;
235 Memory.FillRAM[0x4210] = 0x80;
236 if (Memory.FillRAM[0x4200] & 0x80)
238 CPU.NMIActive = TRUE;
239 CPU.Flags |= NMI_FLAG;
240 CPU.NMICycleCount = CPU.NMITriggerPoint;
244 if (CPU.V_Counter == PPU.ScreenHeight + 3)
247 if (CPU.V_Counter == FIRST_VISIBLE_LINE)
249 Memory.FillRAM[0x4210] = 0;
250 CPU.Flags &= ~NMI_FLAG;
251 S9xStartScreenRefresh ();
253 if (CPU.V_Counter >= FIRST_VISIBLE_LINE &&
254 CPU.V_Counter < PPU.ScreenHeight + FIRST_VISIBLE_LINE)
256 RenderLine (CPU.V_Counter - FIRST_VISIBLE_LINE);
258 // Use TimerErrorCounter to skip update of SPC700 timers once
259 // every 128 updates. Needed because this section of code is called
260 // once every emulated 63.5 microseconds, which coresponds to
261 // 15.750KHz, but the SPC700 timers need to be updated at multiples
262 // of 8KHz, hence the error correction.
263 // IAPU.TimerErrorCounter++;
264 // if (IAPU.TimerErrorCounter >= )
265 // IAPU.TimerErrorCounter = 0;
268 if (APU.TimerEnabled [2])
271 while (APU.Timer [2] >= APU.TimerTarget [2])
273 IAPU.RAM [0xff] = (IAPU.RAM [0xff] + 1) & 0xf;
274 APU.Timer [2] -= APU.TimerTarget [2];
275 #ifdef SPC700_SHUTDOWN
277 /*IAPU.APUExecuting*/CPU.APU_APUExecuting= TRUE;
281 if (CPU.V_Counter & 1)
283 if (APU.TimerEnabled [0])
286 if (APU.Timer [0] >= APU.TimerTarget [0])
288 IAPU.RAM [0xfd] = (IAPU.RAM [0xfd] + 1) & 0xf;
290 #ifdef SPC700_SHUTDOWN
292 /*IAPU.APUExecuting*/CPU.APU_APUExecuting = TRUE;
296 if (APU.TimerEnabled [1])
299 if (APU.Timer [1] >= APU.TimerTarget [1])
301 IAPU.RAM [0xfe] = (IAPU.RAM [0xfe] + 1) & 0xf;
303 #ifdef SPC700_SHUTDOWN
305 /*IAPU.APUExecuting*/CPU.APU_APUExecuting = TRUE;
312 case HTIMER_BEFORE_EVENT:
313 case HTIMER_AFTER_EVENT:
314 if (PPU.HTimerEnabled &&
315 (!PPU.VTimerEnabled || CPU.V_Counter == PPU.IRQVBeamPos))
317 S9xSetIRQ (PPU_H_BEAM_IRQ_SOURCE);