Example #1
0
        /**
         * Fired when the interactive debugger GUI receives a keypress indicating a user
         * command to be processed (continue, single step, etc).
         */
        private static void Platform_OnDebugCommand(DebugCommandEventArgs eventArgs)
        {
            if (!_game.Debug)
            {
                return;
            }

            switch (eventArgs.Action)
            {
            case DebugAction.ResumeContinue:
                _game.Continue(singleStep: false);
                break;

            case DebugAction.ResumeStep:
                _game.Continue(singleStep: true);
                break;

            case DebugAction.ReverseStep:
                _game.ReverseStep();
                break;

            case DebugAction.AddBreakpoint:
                if (!_game.BreakAtAddresses.Contains(eventArgs.Address))
                {
                    _game.BreakAtAddresses.Add(eventArgs.Address);
                }
                break;

            case DebugAction.RemoveBreakpoint:
                if (_game.BreakAtAddresses.Contains(eventArgs.Address))
                {
                    _game.BreakAtAddresses.Remove(eventArgs.Address);
                }
                break;

            case DebugAction.SaveState:
            {
                var state = _game.SaveState();
                var json  = JsonConvert.SerializeObject(state);
                File.WriteAllText(eventArgs.FileName, json);
                break;
            }

            case DebugAction.LoadState:
            {
                var json  = File.ReadAllText(eventArgs.FileName);
                var state = JsonConvert.DeserializeObject <EmulatorState>(json);
                _game.LoadState(state);
                break;
            }
            }
        }
Example #2
0
        private void HandleDebuggerEventForLoadOrSaveStateState(SDL.SDL_Event sdlEvent)
        {
            HandleDebuggerInputText(sdlEvent);

            if (sdlEvent.type != SDL.SDL_EventType.SDL_KEYDOWN)
            {
                return;
            }

            var keycode = sdlEvent.key.keysym.sym;

            switch (keycode)
            {
            case SDL.SDL_Keycode.SDLK_UP:     // Up = Move selection up
            {
                if (_debuggerFileList.Count == 0)
                {
                    break;
                }

                var index = _debuggerFileList.IndexOf(_debuggerInputString);

                if (index > 0)
                {
                    index--;
                }

                if (index == -1)
                {
                    _debuggerInputString = _debuggerFileList.Count > 0 ? _debuggerFileList.Last() : String.Empty;
                }
                else
                {
                    _debuggerInputString = _debuggerFileList[index];
                }

                SignalDebuggerNeedsRendering();

                break;
            }

            case SDL.SDL_Keycode.SDLK_DOWN:     // Down = Move selection down
            {
                if (_debuggerFileList.Count == 0)
                {
                    break;
                }

                var index = _debuggerFileList.IndexOf(_debuggerInputString);

                if (index != -1 && index < _debuggerFileList.Count - 1)
                {
                    index++;
                }

                if (index == -1)
                {
                    _debuggerInputString = _debuggerFileList.Count > 0 ? _debuggerFileList.Last() : String.Empty;
                }
                else
                {
                    _debuggerInputString = _debuggerFileList[index];
                }

                SignalDebuggerNeedsRendering();

                break;
            }

            case SDL.SDL_Keycode.SDLK_RETURN:     // Return/Enter = Finish input and load/save state
            {
                var filePath = _debuggerInputString;
                _debuggerInputString = String.Empty;

                if (String.IsNullOrWhiteSpace(filePath))
                {
                    break;
                }

                if (!filePath.EndsWith(".state.json"))
                {
                    filePath += ".state.json";
                }

                if (_debuggerState == DebuggerState.LoadState && !File.Exists(filePath))
                {
                    var errorMessage = $"Error {(_debuggerState == DebuggerState.LoadState ? "Loading" : "Saving")} state; file does not exist: {filePath}.";
                    Console.WriteLine(errorMessage);
                    SDL2.SDL.SDL_ShowSimpleMessageBox(SDL.SDL_MessageBoxFlags.SDL_MESSAGEBOX_WARNING, $"{(_debuggerState == DebuggerState.LoadState ? "Load" : "Save")} State", errorMessage, _debugWindow);
                    SignalDebuggerNeedsRendering();
                    break;
                }

                try
                {
                    if (_debuggerState == DebuggerState.LoadState)
                    {
                        var json  = File.ReadAllText(filePath);
                        var state = JsonConvert.DeserializeObject <EmulatorState>(json);
                        _debuggerPcb.LoadState(state);
                    }
                    else
                    {
                        var state = _debuggerPcb.SaveState();
                        var json  = JsonConvert.SerializeObject(state);
                        File.WriteAllText(filePath, json);
                    }
                }
                catch (Exception exception)
                {
                    Console.WriteLine($"Error {(_debuggerState == DebuggerState.LoadState ? "Loading" : "Saving")} state for file: {filePath}", exception);
                    SDL2.SDL.SDL_ShowSimpleMessageBox(SDL.SDL_MessageBoxFlags.SDL_MESSAGEBOX_ERROR, $"{(_debuggerState == DebuggerState.LoadState ? "Load" : "Save")} State", $"Error {(_debuggerState == DebuggerState.LoadState ? "Loading" : "Saving")} state: {exception.Message}", _debugWindow);
                    SignalDebuggerNeedsRendering();
                    break;
                }

                var message = $"{(_debuggerState == DebuggerState.LoadState ? "Loading" : "Saving")} state was successful!";
                SDL2.SDL.SDL_ShowSimpleMessageBox(SDL.SDL_MessageBoxFlags.SDL_MESSAGEBOX_INFORMATION, $"{(_debuggerState == DebuggerState.LoadState ? "Load" : "Save")} State", message, _debugWindow);

                _debuggerInputString = String.Empty;
                _debuggerFileList.Clear();
                _debuggerState = DebuggerState.Breakpoint;
                SignalDebuggerNeedsRendering();
                break;
            }

            case SDL.SDL_Keycode.SDLK_ESCAPE:     // Escape = Cancel load/save state
                _debuggerInputString = String.Empty;
                _debuggerFileList.Clear();
                _debuggerState = DebuggerState.Breakpoint;
                SignalDebuggerNeedsRendering();
                break;
            }
        }