Path: blob/main/misc/emulator/xnes/snes9x/cpuexec.cpp
28515 views
/***********************************************************************************1Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.23(c) Copyright 1996 - 2002 Gary Henderson ([email protected]),4Jerremy Koot ([email protected])56(c) Copyright 2002 - 2004 Matthew Kendora78(c) Copyright 2002 - 2005 Peter Bortas ([email protected])910(c) Copyright 2004 - 2005 Joel Yliluoma (http://iki.fi/bisqwit/)1112(c) Copyright 2001 - 2006 John Weidman ([email protected])1314(c) Copyright 2002 - 2006 funkyass ([email protected]),15Kris Bleakley ([email protected])1617(c) Copyright 2002 - 2010 Brad Jorsch ([email protected]),18Nach ([email protected]),1920(c) Copyright 2002 - 2011 zones ([email protected])2122(c) Copyright 2006 - 2007 nitsuja2324(c) Copyright 2009 - 2011 BearOso,25OV2262728BS-X C emulator code29(c) Copyright 2005 - 2006 Dreamer Nom,30zones3132C4 x86 assembler and some C emulation code33(c) Copyright 2000 - 2003 _Demo_ ([email protected]),34Nach,35zsKnight ([email protected])3637C4 C++ code38(c) Copyright 2003 - 2006 Brad Jorsch,39Nach4041DSP-1 emulator code42(c) Copyright 1998 - 2006 _Demo_,43Andreas Naive ([email protected]),44Gary Henderson,45Ivar ([email protected]),46John Weidman,47Kris Bleakley,48Matthew Kendora,49Nach,50neviksti ([email protected])5152DSP-2 emulator code53(c) Copyright 2003 John Weidman,54Kris Bleakley,55Lord Nightmare ([email protected]),56Matthew Kendora,57neviksti5859DSP-3 emulator code60(c) Copyright 2003 - 2006 John Weidman,61Kris Bleakley,62Lancer,63z80 gaiden6465DSP-4 emulator code66(c) Copyright 2004 - 2006 Dreamer Nom,67John Weidman,68Kris Bleakley,69Nach,70z80 gaiden7172OBC1 emulator code73(c) Copyright 2001 - 2004 zsKnight,74pagefault ([email protected]),75Kris Bleakley76Ported from x86 assembler to C by sanmaiwashi7778SPC7110 and RTC C++ emulator code used in 1.39-1.5179(c) Copyright 2002 Matthew Kendora with research by80zsKnight,81John Weidman,82Dark Force8384SPC7110 and RTC C++ emulator code used in 1.52+85(c) Copyright 2009 byuu,86neviksti8788S-DD1 C emulator code89(c) Copyright 2003 Brad Jorsch with research by90Andreas Naive,91John Weidman9293S-RTC C emulator code94(c) Copyright 2001 - 2006 byuu,95John Weidman9697ST010 C++ emulator code98(c) Copyright 2003 Feather,99John Weidman,100Kris Bleakley,101Matthew Kendora102103Super FX x86 assembler emulator code104(c) Copyright 1998 - 2003 _Demo_,105pagefault,106zsKnight107108Super FX C emulator code109(c) Copyright 1997 - 1999 Ivar,110Gary Henderson,111John Weidman112113Sound emulator code used in 1.5-1.51114(c) Copyright 1998 - 2003 Brad Martin115(c) Copyright 1998 - 2006 Charles Bilyue'116117Sound emulator code used in 1.52+118(c) Copyright 2004 - 2007 Shay Green ([email protected])119120SH assembler code partly based on x86 assembler code121(c) Copyright 2002 - 2004 Marcus Comstedt ([email protected])1221232xSaI filter124(c) Copyright 1999 - 2001 Derek Liauw Kie Fa125126HQ2x, HQ3x, HQ4x filters127(c) Copyright 2003 Maxim Stepin ([email protected])128129NTSC filter130(c) Copyright 2006 - 2007 Shay Green131132GTK+ GUI code133(c) Copyright 2004 - 2011 BearOso134135Win32 GUI code136(c) Copyright 2003 - 2006 blip,137funkyass,138Matthew Kendora,139Nach,140nitsuja141(c) Copyright 2009 - 2011 OV2142143Mac OS GUI code144(c) Copyright 1998 - 2001 John Stiles145(c) Copyright 2001 - 2011 zones146147148Specific ports contains the works of other authors. See headers in149individual files.150151152Snes9x homepage: http://www.snes9x.com/153154Permission to use, copy, modify and/or distribute Snes9x in both binary155and source form, for non-commercial purposes, is hereby granted without156fee, providing that this license information and copyright notice appear157with all copies and any derived work.158159This software is provided 'as-is', without any express or implied160warranty. In no event shall the authors be held liable for any damages161arising from the use of this software or it's derivatives.162163Snes9x is freeware for PERSONAL USE only. Commercial users should164seek permission of the copyright holders first. Commercial use includes,165but is not limited to, charging money for Snes9x or software derived from166Snes9x, including Snes9x or derivatives in commercial game bundles, and/or167using Snes9x as a promotion for your commercial product.168169The copyright holders request that bug fixes and improvements to the code170should be forwarded to them so everyone can benefit from the modifications171in future versions.172173Super NES and Super Nintendo Entertainment System are trademarks of174Nintendo Co., Limited and its subsidiary companies.175***********************************************************************************/176177178#include "snes9x.h"179#include "memmap.h"180#include "cpuops.h"181#include "dma.h"182#include "apu/apu.h"183#include "fxemu.h"184#include "snapshot.h"185#ifdef DEBUGGER186#include "debug.h"187#include "missing.h"188#endif189190static inline void S9xReschedule (void);191192193void S9xMainLoop (void)194{195// debugging variable?196// static int loop_times=0;197int loops2=0;198//printf("Enter S9xMainLoop %d %x\n", loop_times, Registers.PCw);199for (;;)200{201loops2++;202//printf("S9xMainLoop %d for loop %d flags=%d\n", loop_times, loops2, CPU.Flags);203if (CPU.NMILine)204{205//printf("case 1\n");206if (Timings.NMITriggerPos <= CPU.Cycles)207{208CPU.NMILine = FALSE;209Timings.NMITriggerPos = 0xffff;210if (CPU.WaitingForInterrupt)211{212CPU.WaitingForInterrupt = FALSE;213Registers.PCw++;214}215216S9xOpcode_NMI();217}218}219220if (CPU.IRQTransition || CPU.IRQExternal)221{222//printf("case 2\n");223if (CPU.IRQPending)224CPU.IRQPending--;225else226{227if (CPU.WaitingForInterrupt)228{229CPU.WaitingForInterrupt = FALSE;230Registers.PCw++;231}232233CPU.IRQTransition = FALSE;234CPU.IRQPending = Timings.IRQPendCount;235236if (!CheckFlag(IRQ))237S9xOpcode_IRQ();238}239}240241#ifdef DEBUGGER242if ((CPU.Flags & BREAK_FLAG) && !(CPU.Flags & SINGLE_STEP_FLAG))243{244for (int Break = 0; Break != 6; Break++)245{246if (S9xBreakpoint[Break].Enabled &&247S9xBreakpoint[Break].Bank == Registers.PB &&248S9xBreakpoint[Break].Address == Registers.PCw)249{250if (S9xBreakpoint[Break].Enabled == 2)251S9xBreakpoint[Break].Enabled = TRUE;252else253CPU.Flags |= DEBUG_MODE_FLAG;254}255}256}257258if (CPU.Flags & DEBUG_MODE_FLAG)259break;260261if (CPU.Flags & TRACE_FLAG)262S9xTrace();263264if (CPU.Flags & SINGLE_STEP_FLAG)265{266CPU.Flags &= ~SINGLE_STEP_FLAG;267CPU.Flags |= DEBUG_MODE_FLAG;268}269#endif270271if (CPU.Flags & SCAN_KEYS_FLAG)272break;273274register uint8 Op;275register Opcode *Opcodes;276277if (CPU.PCBase)278{279Op = CPU.PCBase[Registers.PCw];280281CPU.PrevCycles = CPU.Cycles;282CPU.Cycles += CPU.MemSpeed;283S9xCheckInterrupts();284Opcodes = ICPU.S9xOpcodes;285//printf("case 3 %x %x %d %d\n", Registers.PCw, Op, CPU.PrevCycles, CPU.Cycles);286}287else288{289//printf("case 4\n");290Op = S9xGetByte(Registers.PBPC);291OpenBus = Op;292Opcodes = S9xOpcodesSlow;293}294295if ((Registers.PCw & MEMMAP_MASK) + ICPU.S9xOpLengths[Op] >= MEMMAP_BLOCK_SIZE)296{297uint8 *oldPCBase = CPU.PCBase;298//printf("case 5\n");299300CPU.PCBase = S9xGetBasePointer(ICPU.ShiftedPB + ((uint16) (Registers.PCw + 4)));301if (oldPCBase != CPU.PCBase || (Registers.PCw & ~MEMMAP_MASK) == (0xffff & ~MEMMAP_MASK))302Opcodes = S9xOpcodesSlow;303}304//printf("case 3.1 %d\n", CPU.Cycles);305Registers.PCw++;306Opcodes[Op]();307//printf("case 3.2 %d\n", CPU.Cycles);308309if (Settings.SA1){310//printf("case 6\n");311S9xSA1MainLoop();312}313}314315S9xPackStatus();316317if (CPU.Flags & SCAN_KEYS_FLAG)318{319//printf("case 7\n");320#ifdef DEBUGGER321if (!(CPU.Flags & FRAME_ADVANCE_FLAG))322#endif323S9xSyncSpeed();324CPU.Flags &= ~SCAN_KEYS_FLAG;325}326//printf("Exit S9xMainLoop %d loops2=%d\n", loop_times++, loops2);327}328329static inline void S9xReschedule (void)330{331switch (CPU.WhichEvent)332{333case HC_HBLANK_START_EVENT:334CPU.WhichEvent = HC_HDMA_START_EVENT;335CPU.NextEvent = Timings.HDMAStart;336break;337338case HC_HDMA_START_EVENT:339CPU.WhichEvent = HC_HCOUNTER_MAX_EVENT;340CPU.NextEvent = Timings.H_Max;341break;342343case HC_HCOUNTER_MAX_EVENT:344CPU.WhichEvent = HC_HDMA_INIT_EVENT;345CPU.NextEvent = Timings.HDMAInit;346break;347348case HC_HDMA_INIT_EVENT:349CPU.WhichEvent = HC_RENDER_EVENT;350CPU.NextEvent = Timings.RenderPos;351break;352353case HC_RENDER_EVENT:354CPU.WhichEvent = HC_WRAM_REFRESH_EVENT;355CPU.NextEvent = Timings.WRAMRefreshPos;356break;357358case HC_WRAM_REFRESH_EVENT:359CPU.WhichEvent = HC_HBLANK_START_EVENT;360CPU.NextEvent = Timings.HBlankStart;361break;362}363}364365void S9xDoHEventProcessing (void)366{367#ifdef DEBUGGER368static char eventname[7][32] =369{370"",371"HC_HBLANK_START_EVENT",372"HC_HDMA_START_EVENT ",373"HC_HCOUNTER_MAX_EVENT",374"HC_HDMA_INIT_EVENT ",375"HC_RENDER_EVENT ",376"HC_WRAM_REFRESH_EVENT"377};378#endif379380#ifdef DEBUGGER381if (Settings.TraceHCEvent)382S9xTraceFormattedMessage("--- HC event processing (%s) expected HC:%04d executed HC:%04d",383eventname[CPU.WhichEvent], CPU.NextEvent, CPU.Cycles);384#endif385386switch (CPU.WhichEvent)387{388case HC_HBLANK_START_EVENT:389S9xReschedule();390break;391392case HC_HDMA_START_EVENT:393S9xReschedule();394395if (PPU.HDMA && CPU.V_Counter <= PPU.ScreenHeight)396{397#ifdef DEBUGGER398S9xTraceFormattedMessage("*** HDMA Transfer HC:%04d, Channel:%02x", CPU.Cycles, PPU.HDMA);399#endif400PPU.HDMA = S9xDoHDMA(PPU.HDMA);401}402403break;404405case HC_HCOUNTER_MAX_EVENT:406if (Settings.SuperFX)407{408if (!SuperFX.oneLineDone)409S9xSuperFXExec();410SuperFX.oneLineDone = FALSE;411}412413S9xAPUEndScanline();414CPU.Cycles -= Timings.H_Max;415CPU.PrevCycles -= Timings.H_Max;416S9xAPUSetReferenceTime(CPU.Cycles);417418if ((Timings.NMITriggerPos != 0xffff) && (Timings.NMITriggerPos >= Timings.H_Max))419Timings.NMITriggerPos -= Timings.H_Max;420421CPU.V_Counter++;422if (CPU.V_Counter >= Timings.V_Max) // V ranges from 0 to Timings.V_Max - 1423{424CPU.V_Counter = 0;425Timings.InterlaceField ^= 1;426427// From byuu:428// [NTSC]429// interlace mode has 525 scanlines: 263 on the even frame, and 262 on the odd.430// non-interlace mode has 524 scanlines: 262 scanlines on both even and odd frames.431// [PAL] <PAL info is unverified on hardware>432// interlace mode has 625 scanlines: 313 on the even frame, and 312 on the odd.433// non-interlace mode has 624 scanlines: 312 scanlines on both even and odd frames.434if (IPPU.Interlace && !Timings.InterlaceField)435Timings.V_Max = Timings.V_Max_Master + 1; // 263 (NTSC), 313?(PAL)436else437Timings.V_Max = Timings.V_Max_Master; // 262 (NTSC), 312?(PAL)438439Memory.FillRAM[0x213F] ^= 0x80;440PPU.RangeTimeOver = 0;441442// FIXME: reading $4210 will wait 2 cycles, then perform reading, then wait 4 more cycles.443Memory.FillRAM[0x4210] = Model->_5A22;444CPU.NMILine = FALSE;445Timings.NMITriggerPos = 0xffff;446447ICPU.Frame++;448PPU.HVBeamCounterLatched = 0;449CPU.Flags |= SCAN_KEYS_FLAG;450}451452// From byuu:453// In non-interlace mode, there are 341 dots per scanline, and 262 scanlines per frame.454// On odd frames, scanline 240 is one dot short.455// In interlace mode, there are always 341 dots per scanline. Even frames have 263 scanlines,456// and odd frames have 262 scanlines.457// Interlace mode scanline 240 on odd frames is not missing a dot.458if (CPU.V_Counter == 240 && !IPPU.Interlace && Timings.InterlaceField) // V=240459Timings.H_Max = Timings.H_Max_Master - ONE_DOT_CYCLE; // HC=1360460else461Timings.H_Max = Timings.H_Max_Master; // HC=1364462463if (Model->_5A22 == 2)464{465if (CPU.V_Counter != 240 || IPPU.Interlace || !Timings.InterlaceField) // V=240466{467if (Timings.WRAMRefreshPos == SNES_WRAM_REFRESH_HC_v2 - ONE_DOT_CYCLE) // HC=534468Timings.WRAMRefreshPos = SNES_WRAM_REFRESH_HC_v2; // HC=538469else470Timings.WRAMRefreshPos = SNES_WRAM_REFRESH_HC_v2 - ONE_DOT_CYCLE; // HC=534471}472}473else474Timings.WRAMRefreshPos = SNES_WRAM_REFRESH_HC_v1;475476if (CPU.V_Counter == PPU.ScreenHeight + FIRST_VISIBLE_LINE) // VBlank starts from V=225(240).477{478S9xEndScreenRefresh();479PPU.HDMA = 0;480// Bits 7 and 6 of $4212 are computed when read in S9xGetPPU.481#ifdef DEBUGGER482missing.dma_this_frame = 0;483#endif484IPPU.MaxBrightness = PPU.Brightness;485PPU.ForcedBlanking = (Memory.FillRAM[0x2100] >> 7) & 1;486487if (!PPU.ForcedBlanking)488{489PPU.OAMAddr = PPU.SavedOAMAddr;490491uint8 tmp = 0;492493if (PPU.OAMPriorityRotation)494tmp = (PPU.OAMAddr & 0xFE) >> 1;495if ((PPU.OAMFlip & 1) || PPU.FirstSprite != tmp)496{497PPU.FirstSprite = tmp;498IPPU.OBJChanged = TRUE;499}500501PPU.OAMFlip = 0;502}503504// FIXME: writing to $4210 will wait 6 cycles.505Memory.FillRAM[0x4210] = 0x80 | Model->_5A22;506if (Memory.FillRAM[0x4200] & 0x80)507{508// FIXME: triggered at HC=6, checked just before the final CPU cycle,509// then, when to call S9xOpcode_NMI()?510CPU.NMILine = TRUE;511Timings.NMITriggerPos = 6 + 6;512}513514}515516if (CPU.V_Counter == PPU.ScreenHeight + 3) // FIXME: not true517{518if (Memory.FillRAM[0x4200] & 1)519S9xDoAutoJoypad();520}521522if (CPU.V_Counter == FIRST_VISIBLE_LINE) // V=1523S9xStartScreenRefresh();524525S9xReschedule();526527break;528529case HC_HDMA_INIT_EVENT:530S9xReschedule();531532if (CPU.V_Counter == 0)533{534#ifdef DEBUGGER535S9xTraceFormattedMessage("*** HDMA Init HC:%04d, Channel:%02x", CPU.Cycles, PPU.HDMA);536#endif537S9xStartHDMA();538}539540break;541542case HC_RENDER_EVENT:543if (CPU.V_Counter >= FIRST_VISIBLE_LINE && CPU.V_Counter <= PPU.ScreenHeight)544RenderLine((uint8) (CPU.V_Counter - FIRST_VISIBLE_LINE));545546S9xReschedule();547548break;549550case HC_WRAM_REFRESH_EVENT:551#ifdef DEBUGGER552S9xTraceFormattedMessage("*** WRAM Refresh HC:%04d", CPU.Cycles);553#endif554555CPU.PrevCycles = CPU.Cycles;556CPU.Cycles += SNES_WRAM_REFRESH_CYCLES;557S9xCheckInterrupts();558559S9xReschedule();560561break;562}563564#ifdef DEBUGGER565if (Settings.TraceHCEvent)566S9xTraceFormattedMessage("--- HC event rescheduled (%s) expected HC:%04d current HC:%04d",567eventname[CPU.WhichEvent], CPU.NextEvent, CPU.Cycles);568#endif569}570571572