random bug fixes
[drnoksnes] / cpu.cpp
1 /*
2  * Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
3  *
4  * (c) Copyright 1996 - 2001 Gary Henderson (gary.henderson@ntlworld.com) and
5  *                           Jerremy Koot (jkoot@snes9x.com)
6  *
7  * Super FX C emulator code 
8  * (c) Copyright 1997 - 1999 Ivar (ivar@snes9x.com) and
9  *                           Gary Henderson.
10  * Super FX assembler emulator code (c) Copyright 1998 zsKnight and _Demo_.
11  *
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).
15  *
16  * DOS port code contains the works of other authors. See headers in
17  * individual files.
18  *
19  * Snes9x homepage: http://www.snes9x.com
20  *
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.
25  *
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.
29  *
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.
33  *
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
36  * in future versions.
37  *
38  * Super NES and Super Nintendo Entertainment System are trademarks of
39  * Nintendo Co., Limited and its subsidiary companies.
40  */
41 #include "snes9x.h"
42 #include "memmap.h"
43 #include "ppu.h"
44 #include "dsp1.h"
45 #include "cpuexec.h"
46 #include "debug.h"
47 #include "apu.h"
48 #include "dma.h"
49 #ifdef USE_SA1
50 #include "sa1.h"
51 #endif
52 //#include "cheats.h"
53 #include "srtc.h"
54 #include "sdd1.h"
55 //#include "spc7110.h"
56
57 #include "soundux.h"
58
59 #ifdef SUPER_FX
60 #include "fxemu.h"
61
62 extern struct FxInit_s SuperFX;
63
64 static void S9xResetSuperFX ()
65 {
66     SuperFX.vFlags = 0; //FX_FLAG_ROM_BUFFER;// | FX_FLAG_ADDRESS_CHECKING;
67     FxReset (&SuperFX);
68 }
69 #endif
70
71 void S9xResetCPU ()
72 {
73     Registers.PB = 0;
74     Registers.PC = S9xGetWord (0xFFFC);
75     Registers.D.W = 0;
76     Registers.DB = 0;
77     Registers.SH = 1;
78     Registers.SL = 0xFF;
79     Registers.XH = 0;
80     Registers.YH = 0;
81     Registers.P.W = 0;
82
83     ICPU.ShiftedPB = 0;
84     ICPU.ShiftedDB = 0;
85     SetFlags (MemoryFlag | IndexFlag | IRQ | Emulation);
86     ClearFlags (Decimal);
87
88     CPU.Flags = CPU.Flags & (DEBUG_MODE_FLAG | TRACE_FLAG);
89     CPU.BranchSkip = FALSE;
90     CPU.NMIActive = FALSE;
91     CPU.IRQActive = FALSE;
92     CPU.WaitingForInterrupt = FALSE;
93     CPU.InDMA = FALSE;
94     CPU.WhichEvent = HBLANK_START_EVENT;
95     CPU.PC = NULL;
96     CPU.PCBase = NULL;
97     CPU.PCAtOpcodeStart = NULL;
98     CPU.WaitAddress = NULL;
99     CPU.WaitCounter = 0;
100     CPU.Cycles = 0;
101     CPU.NextEvent = Settings.HBlankStart;
102     CPU.V_Counter = 0;
103     CPU.MemSpeed = SLOW_ONE_CYCLE;
104     CPU.MemSpeedx2 = SLOW_ONE_CYCLE * 2;
105     CPU.FastROMSpeed = SLOW_ONE_CYCLE;
106     CPU.AutoSaveTimer = 0;
107     CPU.SRAMModified = FALSE;
108     // CPU.NMITriggerPoint = 4; // Set when ROM image loaded
109     CPU.BRKTriggered = FALSE;
110     //CPU.TriedInterleavedMode2 = FALSE; // Reset when ROM image loaded
111     CPU.NMICycleCount = 0;
112     CPU.IRQCycleCount = 0;
113     S9xSetPCBase (Registers.PC);
114
115 #if !CONF_BUILD_ASM_CPU
116 #ifndef VAR_CYCLES
117     ICPU.Speed = S9xE1M1X1;
118 #endif
119     ICPU.S9xOpcodes = S9xOpcodesM1X1;
120     S9xUnpackStatus();
121 #endif
122
123     ICPU.CPUExecuting = TRUE;
124 }
125
126
127 void S9xReset (void)
128 {
129 #ifdef SUPER_FX
130     if (Settings.SuperFX)
131         S9xResetSuperFX ();
132 #endif
133
134     ZeroMemory (Memory.FillRAM, 0x8000);
135     memset (Memory.VRAM, 0x00, 0x10000);
136     memset (Memory.RAM, 0x55, 0x20000);
137
138 /*      if(Settings.SPC7110)
139                 S9xSpc7110Reset();*/
140     S9xResetCPU ();
141     S9xResetPPU ();
142     S9xResetSRTC ();
143     if (Settings.SDD1)
144         S9xResetSDD1 ();
145
146     S9xResetDMA ();
147     S9xResetAPU ();
148     S9xResetDSP1 ();
149 #ifdef USE_SA1
150     S9xSA1Init ();
151 #endif
152
153         if (Settings.C4)
154                 S9xInitC4 ();
155
156         S9xResetSound(1);
157
158     Settings.Paused = FALSE;
159     
160        //Init CPU Map & co
161     CPU.Memory_Map=(uint8*)&(Memory.Map);
162     CPU.Memory_WriteMap=(uint8*)&(Memory.WriteMap);
163     CPU.Memory_MemorySpeed=(uint8*)&(Memory.MemorySpeed);
164     CPU.Memory_BlockIsRAM=(uint8*)&(Memory.BlockIsRAM);
165     CPU.Memory_SRAM=Memory.SRAM;
166     CPU.Memory_BWRAM=Memory.BWRAM;
167 //    CPU.Memory_SRAMMask=Memory.SRAMMask;
168         
169 }