1/*****************************************************************************\
2 Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
3 This file is licensed under the Snes9x License.
4 For further information, consult the LICENSE file in the root directory.
5\*****************************************************************************/
6
7#include "snes9x.h"
8#include "memmap.h"
9#include "dma.h"
10#include "apu/apu.h"
11#include "fxemu.h"
12#include "sdd1.h"
13#include "srtc.h"
14#include "snapshot.h"
15#include "cheats.h"
16#include "logger.h"
17#ifdef DEBUGGER
18#include "debug.h"
19#endif
20
21static void S9xResetCPU (void);
22static void S9xSoftResetCPU (void);
23
24
25static void S9xResetCPU (void)
26{
27 S9xSoftResetCPU();
28 Registers.SL = 0xff;
29 Registers.P.W = 0;
30 Registers.A.W = 0;
31 Registers.X.W = 0;
32 Registers.Y.W = 0;
33 SetFlags(MemoryFlag | IndexFlag | IRQ | Emulation);
34 ClearFlags(Decimal);
35}
36
37static void S9xSoftResetCPU (void)
38{
39 CPU.Cycles = 182; // Or 188. This is the cycle count just after the jump to the Reset Vector.
40 CPU.PrevCycles = CPU.Cycles;
41 CPU.V_Counter = 0;
42 CPU.Flags = CPU.Flags & (DEBUG_MODE_FLAG | TRACE_FLAG);
43 CPU.PCBase = NULL;
44 CPU.NMIPending = FALSE;
45 CPU.IRQLine = FALSE;
46 CPU.IRQTransition = FALSE;
47 CPU.IRQExternal = FALSE;
48 CPU.MemSpeed = SLOW_ONE_CYCLE;
49 CPU.MemSpeedx2 = SLOW_ONE_CYCLE * 2;
50 CPU.FastROMSpeed = SLOW_ONE_CYCLE;
51 CPU.InDMA = FALSE;
52 CPU.InHDMA = FALSE;
53 CPU.InDMAorHDMA = FALSE;
54 CPU.InWRAMDMAorHDMA = FALSE;
55 CPU.HDMARanInDMA = 0;
56 CPU.CurrentDMAorHDMAChannel = -1;
57 CPU.WhichEvent = HC_RENDER_EVENT;
58 CPU.NextEvent = Timings.RenderPos;
59 CPU.WaitingForInterrupt = FALSE;
60 CPU.AutoSaveTimer = 0;
61 CPU.SRAMModified = FALSE;
62
63 Registers.PBPC = 0;
64 Registers.PB = 0;
65 Registers.PCw = S9xGetWord(0xfffc);
66 OpenBus = Registers.PCh;
67 Registers.D.W = 0;
68 Registers.DB = 0;
69 Registers.SH = 1;
70 Registers.SL -= 3;
71 Registers.XH = 0;
72 Registers.YH = 0;
73
74 ICPU.ShiftedPB = 0;
75 ICPU.ShiftedDB = 0;
76 SetFlags(MemoryFlag | IndexFlag | IRQ | Emulation);
77 ClearFlags(Decimal);
78
79 Timings.InterlaceField = FALSE;
80 Timings.H_Max = Timings.H_Max_Master;
81 Timings.V_Max = Timings.V_Max_Master;
82 Timings.NMITriggerPos = 0xffff;
83 Timings.NextIRQTimer = 0x0fffffff;
84 Timings.IRQFlagChanging = IRQ_NONE;
85
86 if (Model->_5A22 == 2)
87 Timings.WRAMRefreshPos = SNES_WRAM_REFRESH_HC_v2;
88 else
89 Timings.WRAMRefreshPos = SNES_WRAM_REFRESH_HC_v1;
90
91 S9xSetPCBase(Registers.PBPC);
92
93 ICPU.S9xOpcodes = S9xOpcodesE1;
94 ICPU.S9xOpLengths = S9xOpLengthsM1X1;
95
96 S9xUnpackStatus();
97}
98
99void S9xReset (void)
100{
101 S9xResetSaveTimer(FALSE);
102 S9xResetLogger();
103
104 memset(Memory.RAM, 0x55, 0x20000);
105 memset(Memory.VRAM, 0x00, 0x10000);
106 memset(Memory.FillRAM, 0, 0x8000);
107
108 S9xResetBSX();
109 S9xResetCPU();
110 S9xResetPPU();
111 S9xResetDMA();
112 S9xResetAPU();
113 S9xResetMSU();
114
115 if (Settings.DSP)
116 S9xResetDSP();
117 if (Settings.SuperFX)
118 S9xResetSuperFX();
119 if (Settings.SA1)
120 S9xSA1Init();
121 if (Settings.SDD1)
122 S9xResetSDD1();
123 if (Settings.SPC7110)
124 S9xResetSPC7110();
125 if (Settings.C4)
126 S9xInitC4();
127 if (Settings.OBC1)
128 S9xResetOBC1();
129 if (Settings.SRTC)
130 S9xResetSRTC();
131 if (Settings.MSU1)
132 S9xMSU1Init();
133
134 S9xInitCheatData();
135}
136
137void S9xSoftReset (void)
138{
139 S9xResetSaveTimer(FALSE);
140
141 memset(Memory.FillRAM, 0, 0x8000);
142
143 if (Settings.BS)
144 S9xResetBSX();
145
146 S9xSoftResetCPU();
147 S9xSoftResetPPU();
148 S9xResetDMA();
149 S9xSoftResetAPU();
150 S9xResetMSU();
151
152 if (Settings.DSP)
153 S9xResetDSP();
154 if (Settings.SuperFX)
155 S9xResetSuperFX();
156 if (Settings.SA1)
157 S9xSA1Init();
158 if (Settings.SDD1)
159 S9xResetSDD1();
160 if (Settings.SPC7110)
161 S9xResetSPC7110();
162 if (Settings.C4)
163 S9xInitC4();
164 if (Settings.OBC1)
165 S9xResetOBC1();
166 if (Settings.SRTC)
167 S9xResetSRTC();
168 if (Settings.MSU1)
169 S9xMSU1Init();
170
171 S9xInitCheatData();
172}
173