/[pcsx2_0.9.7]/trunk/pcsx2/System/SysCoreThread.cpp
ViewVC logotype

Annotation of /trunk/pcsx2/System/SysCoreThread.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 62 - (hide annotations) (download)
Tue Sep 7 11:08:22 2010 UTC (10 years, 1 month ago) by william
File size: 7801 byte(s)
Auto Commited Import of: pcsx2-0.9.7-r3738-debug in ./trunk
1 william 31 /* PCSX2 - PS2 Emulator for PCs
2     * Copyright (C) 2002-2010 PCSX2 Dev Team
3     *
4     * PCSX2 is free software: you can redistribute it and/or modify it under the terms
5     * of the GNU Lesser General Public License as published by the Free Software Found-
6     * ation, either version 3 of the License, or (at your option) any later version.
7     *
8     * PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10     * PURPOSE. See the GNU General Public License for more details.
11     *
12     * You should have received a copy of the GNU General Public License along with PCSX2.
13     * If not, see <http://www.gnu.org/licenses/>.
14     */
15    
16     #include "PrecompiledHeader.h"
17     #include "Common.h"
18    
19 william 62 #include "IopBios.h"
20    
21 william 31 #include "Counters.h"
22     #include "GS.h"
23     #include "Elfheader.h"
24     #include "Patch.h"
25     #include "PageFaultSource.h"
26     #include "SysThreads.h"
27    
28     #include "Utilities/TlsVariable.inl"
29    
30     #ifdef __WXMSW__
31     # include <wx/msw/wrapwin.h>
32     #endif
33    
34     #include <xmmintrin.h>
35    
36     // --------------------------------------------------------------------------------------
37     // SysCoreThread *External Thread* Implementations
38     // (Called from outside the context of this thread)
39     // --------------------------------------------------------------------------------------
40    
41     SysCoreThread::SysCoreThread()
42     {
43     m_name = L"EE Core";
44     m_resetRecompilers = true;
45     m_resetProfilers = true;
46     m_resetVsyncTimers = true;
47     m_resetVirtualMachine = true;
48    
49     m_hasActiveMachine = false;
50     }
51    
52     SysCoreThread::~SysCoreThread() throw()
53     {
54     SysCoreThread::Cancel();
55     }
56    
57     void SysCoreThread::Cancel( bool isBlocking )
58     {
59     m_hasActiveMachine = false;
60     _parent::Cancel();
61     }
62    
63     bool SysCoreThread::Cancel( const wxTimeSpan& span )
64     {
65     m_hasActiveMachine = false;
66     if( _parent::Cancel( span ) )
67     return true;
68    
69     return false;
70     }
71    
72     void SysCoreThread::OnStart()
73     {
74     _parent::OnStart();
75     }
76    
77     void SysCoreThread::Start()
78     {
79     if( !GetCorePlugins().AreLoaded() ) return;
80     GetCorePlugins().Init();
81     _parent::Start();
82     }
83    
84     // Resumes the core execution state, or does nothing is the core is already running. If
85     // settings were changed, resets will be performed as needed and emulation state resumed from
86     // memory savestates.
87     //
88     // Exceptions (can occur on first call only):
89     // PluginInitError - thrown if a plugin fails init (init is performed on the current thread
90     // on the first time the thread is resumed from it's initial idle state)
91     // ThreadCreationError - Insufficient system resources to create thread.
92     //
93     void SysCoreThread::OnResumeReady()
94     {
95     if( m_resetVirtualMachine )
96     m_hasActiveMachine = false;
97    
98     if( !m_hasActiveMachine )
99     m_resetRecompilers = true;
100     }
101    
102     // This function *will* reset the emulator in order to allow the specified elf file to
103     // take effect. This is because it really doesn't make sense to change the elf file outside
104     // the context of a reset/restart.
105     void SysCoreThread::SetElfOverride( const wxString& elf )
106     {
107     //pxAssertDev( !m_hasValidMachine, "Thread synchronization error while assigning ELF override." );
108     m_elf_override = elf;
109 william 62
110    
111     Hle_SetElfPath(elf.ToUTF8());
112 william 31 }
113    
114     void SysCoreThread::Reset()
115     {
116     Suspend();
117     m_resetVirtualMachine = true;
118     m_hasActiveMachine = false;
119     }
120    
121     // Applies a full suite of new settings, which will automatically facilitate the necessary
122     // resets of the core and components (including plugins, if needed). The scope of resetting
123     // is determined by comparing the current settings against the new settings, so that only
124     // real differences are applied.
125     void SysCoreThread::ApplySettings( const Pcsx2Config& src )
126     {
127     if( src == EmuConfig ) return;
128    
129     if( !pxAssertDev( IsPaused(), "CoreThread is not paused; settings cannot be applied." ) ) return;
130    
131     m_resetRecompilers = ( src.Cpu != EmuConfig.Cpu ) || ( src.Recompiler != EmuConfig.Recompiler ) ||
132     ( src.Gamefixes != EmuConfig.Gamefixes ) || ( src.Speedhacks != EmuConfig.Speedhacks );
133     m_resetProfilers = ( src.Profiler != EmuConfig.Profiler );
134     m_resetVsyncTimers = ( src.GS != EmuConfig.GS );
135    
136     const_cast<Pcsx2Config&>(EmuConfig) = src;
137     }
138    
139     void SysCoreThread::UploadStateCopy( const VmStateBuffer& copy )
140     {
141     if( !pxAssertDev( IsPaused(), "CoreThread is not paused; new VM state cannot be uploaded." ) ) return;
142    
143     SysClearExecutionCache();
144     memLoadingState( copy ).FreezeAll();
145     m_resetVirtualMachine = false;
146     }
147    
148     // --------------------------------------------------------------------------------------
149     // SysCoreThread *Worker* Implementations
150     // (Called from the context of this thread only)
151     // --------------------------------------------------------------------------------------
152     bool SysCoreThread::HasPendingStateChangeRequest() const
153     {
154     return !m_hasActiveMachine || GetMTGS().HasPendingException() || _parent::HasPendingStateChangeRequest();
155     }
156    
157     void SysCoreThread::_reset_stuff_as_needed()
158     {
159 william 62 // Note that resetting recompilers along with the virtual machine is only really needed
160     // because of changes to the TLB. We don't actually support the TLB, however, so rec
161     // resets aren't in fact *needed* ... yet. But might as well, no harm. --air
162    
163 william 31 if( m_resetVirtualMachine || m_resetRecompilers || m_resetProfilers )
164     {
165     SysClearExecutionCache();
166     memBindConditionalHandlers();
167 william 62 SetCPUState( EmuConfig.Cpu.sseMXCSR, EmuConfig.Cpu.sseVUMXCSR );
168    
169 william 31 m_resetRecompilers = false;
170     m_resetProfilers = false;
171     }
172    
173     if( m_resetVirtualMachine )
174     {
175     DoCpuReset();
176 william 62
177 william 31 m_resetVirtualMachine = false;
178 william 62 m_resetVsyncTimers = false;
179 william 31 }
180    
181     if( m_resetVsyncTimers )
182     {
183     UpdateVSyncRate();
184     frameLimitReset();
185 william 62
186 william 31 m_resetVsyncTimers = false;
187     }
188     }
189    
190     void SysCoreThread::DoCpuReset()
191     {
192     AffinityAssert_AllowFromSelf( pxDiagSpot );
193     cpuReset();
194     }
195    
196     // This is called from the PS2 VM at the start of every vsync (either 59.94 or 50 hz by PS2
197     // clock scale, which does not correlate to the actual host machine vsync).
198     //
199     // Default tasks: Updates PADs and applies vsync patches. Derived classes can override this
200     // to change either PAD and/or Patching behaviors.
201     //
202     // [TODO]: Should probably also handle profiling and debugging updates, once those are
203     // re-implemented.
204     //
205     void SysCoreThread::VsyncInThread()
206     {
207     if (EmuConfig.EnablePatches) ApplyPatch();
208     if (EmuConfig.EnableCheats) ApplyCheat();
209     }
210    
211 william 62 void SysCoreThread::GameStartingInThread()
212 william 31 {
213 william 62 GetMTGS().SendGameCRC(ElfCRC);
214 william 31
215 william 62 if (EmuConfig.EnablePatches) ApplyPatch(0);
216     if (EmuConfig.EnableCheats) ApplyCheat(0);
217 william 31 }
218    
219 william 62 bool SysCoreThread::StateCheckInThread()
220     {
221     GetMTGS().RethrowException();
222     return _parent::StateCheckInThread() && (_reset_stuff_as_needed(), true);
223     }
224    
225 william 31 // Runs CPU cycles indefinitely, until the user or another thread requests execution to break.
226     // Rationale: This very short function allows an override point and solves an SEH
227     // "exception-type boundary" problem (can't mix SEH and C++ exceptions in the same function).
228     void SysCoreThread::DoCpuExecute()
229     {
230     m_hasActiveMachine = true;
231     Cpu->Execute();
232     }
233    
234     void SysCoreThread::ExecuteTaskInThread()
235     {
236     Threading::EnableHiresScheduler();
237     m_sem_event.WaitWithoutYield();
238    
239     m_mxcsr_saved.bitmask = _mm_getcsr();
240    
241     PCSX2_PAGEFAULT_PROTECT {
242     while(true) {
243     StateCheckInThread();
244     DoCpuExecute();
245     }
246     } PCSX2_PAGEFAULT_EXCEPT;
247     }
248    
249     void SysCoreThread::OnSuspendInThread()
250     {
251     GetCorePlugins().Close();
252     }
253    
254     void SysCoreThread::OnResumeInThread( bool isSuspended )
255     {
256     GetCorePlugins().Open();
257     }
258    
259    
260     // Invoked by the pthread_exit or pthread_cancel.
261     void SysCoreThread::OnCleanupInThread()
262     {
263 william 62 m_ExecMode = ExecMode_Closing;
264 william 31
265 william 62 m_hasActiveMachine = false;
266     m_resetVirtualMachine = true;
267    
268 william 31 GetCorePlugins().Close();
269     GetCorePlugins().Shutdown();
270    
271     _mm_setcsr( m_mxcsr_saved.bitmask );
272     Threading::DisableHiresScheduler();
273     _parent::OnCleanupInThread();
274 william 62
275     m_ExecMode = ExecMode_NoThreadYet;
276 william 31 }
277    

  ViewVC Help
Powered by ViewVC 1.1.22