private void StartInternal() { Utility.SetCurrentThreadAffinity(); // Set the verbose flag if (_cmdLn.verbose) { _verbose = true; } // 1) Create all the EmulatorComponents first and configure them properly based on the config file if (_cmdLn.useDefaultConfig) { LoadDefaultComponents(); } for (int iConfig = 0; iConfig < _cmdLn.configFiles.Count; iConfig++) { string configFileName = _cmdLn.configFiles[iConfig]; ApplyConfig(configFileName); } string defaultConfigFileName = Application.ExecutablePath + ".emulatorconfig"; if (File.Exists(defaultConfigFileName)) { ApplyConfig(defaultConfigFileName); } // 2) Calling Setup() on all EmulatorComponents to allow each component to check for the // validity of each properties, allocate memory, ... etc. SetupComponent(); CheckForRequiredComponents(); // 3) Now that the components are in the correct state. Register each component with its collection, // if there is one. RegisterComponentsWithCollection(); try { //Initialization of EmulatorNative required before EmulatorComponents //TimingServices, for example, is dependent on EmulatorNative initialization _emulatorNative.Initialize(this.ManagedHal); // 4) Call InitializeComponent() on all EmulatorComponents to put each component into a // working / running state. InitializeComponent(); // 5) Load the assemblies LoadAssemblies(); // 6) Get into the initial state if (_cmdLn.waitForDebuggerOnStartup) { _emulatorNative.WaitForDebuggerOnStartup(); } if (String.IsNullOrEmpty(_cmdLn.commandLineArguments) == false) { _emulatorNative.SetCommandLineArguments(_cmdLn.commandLineArguments); } _emulatorState = EmulatorState.Running; //start time -- this allows components to hook into events before execution starts TimingServices.ResumeTime(); // 7) We start the emulator. this.Run(); } catch (EmulatorException e) { // if we are shutting down and received the unexpected shut down exception, we'll handle the exception if (e.ErrorCode == CLR_ERRORCODE.CLR_E_SHUTTING_DOWN) { Debug.Assert(this.State >= EmulatorState.ShutDown); } else { // otherwise, re-throw. throw; } } finally { bool needUnintialization = (this.State >= EmulatorState.Initialize); Stop(); if (needUnintialization) { UninitializeComponent(); } } }
public void Start(EmulationContext context, IEventAggregator eventAggregator, EmulationMode mode) { _emulator = _emulatorRegistry.GetEmulator(context.EmulatedSystem, _handle); IRomSource loader = null; if (context.Game.RomPath.ToLower().EndsWith(".zip")) { loader = new ZipRomSource(context.Game.RomPath); } else { loader = new FileRomSource(context.Game.RomPath); } using (var romData = loader.GetRomData()) { if (!_emulator.IsRomValid(romData)) return; romData.Seek(0, System.IO.SeekOrigin.Begin); _emulator.LoadRom(romData, null); } eventAggregator.Publish(new EmulatorStartingEvent(InstanceId, this, context.Game, mode)); while (_bus.HasMessages) { _bus.GetCommand().Execute(_emulator); } _emulator.Initialize(eventAggregator); int pixelWidth, pixelHeight; Wren.Core.PixelFormats requestedPixelFormat; Int32 framePerSecond; // assemble rendering pipeline _emulator.GetSpecifications(out pixelWidth, out pixelHeight, out framePerSecond, out requestedPixelFormat); var rSource = _renderingSourceFactory.Create(pixelWidth, pixelHeight, requestedPixelFormat); eventAggregator.Publish(new RenderingSurfaceCreatedEvent(this.InstanceId, rSource.MemorySection, rSource.RenderingSurface, rSource.SurfaceInformation)); _emulator.SetRenderingSurface(rSource.RenderingSurface); var input = _inputPipeline.BuildInputSource(context); eventAggregator.Publish(new EmulatorStartedEvent(this.InstanceId)); FrameRateTimer fp = _frameRateTimerFactory.GetFrameRateTimer(framePerSecond); fp.ScheduleAction(() => { while (_bus.HasMessages) { _bus.GetCommand().Execute(_emulator); } _emulator.SetInput(input.GetCurrentInputState()); Boolean isRunning; try { isRunning = _emulator.Run(); } catch { isRunning = false; } eventAggregator.Publish(new FrameRenderedEvent(this.InstanceId)); if (!isRunning) { eventAggregator.Publish(new EmulatorQuitEvent(this.InstanceId)); input.Close(); } return isRunning; }); if (!fp.IsRunning) { fp.Start(); } }