From da7bb3ebf78baa0b8772ca4c3e4f275e5147482c Mon Sep 17 00:00:00 2001 From: Rob Greene Date: Fri, 13 Mar 2020 21:29:12 -0500 Subject: [PATCH] Adding pause capability. --- apple2.go | 18 +++++++++++++++++- apple2sdl/main.go | 10 ++++++++++ apple2sdl/sdlKeyboard.go | 2 ++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/apple2.go b/apple2.go index fc6705d..7545592 100644 --- a/apple2.go +++ b/apple2.go @@ -27,6 +27,7 @@ type Apple2 struct { fastRequestsCounter int profile bool showSpeed bool + paused bool } const ( @@ -48,7 +49,13 @@ func (a *Apple2) Run() { for { // Run a 6502 step - a.cpu.ExecuteInstruction() + if !a.paused { + a.cpu.ExecuteInstruction() + } else { + time.Sleep(200 * time.Millisecond) + referenceTime = time.Now() + speedReferenceTime = referenceTime + } // Execute meta commands commandsPending := true @@ -92,6 +99,11 @@ func (a *Apple2) Run() { } } +// IsPaused returns true when emulator is paused +func (a *Apple2) IsPaused() bool { + return a.paused +} + func (a *Apple2) setProfiling(value bool) { a.profile = value } @@ -122,6 +134,8 @@ const ( CommandKill // CommandReset executes a 6502 reset CommandReset + // CommandPauseUnpauseEmulator allows the Pause button to freeze the emulator for a coffee break + CommandPauseUnpauseEmulator ) // SendCommand enqueues a command to the emulator thread @@ -164,6 +178,8 @@ func (a *Apple2) executeCommand(command int) { a.cpu.SetTrace(!a.cpu.GetTrace()) case CommandReset: a.cpu.Reset() + case CommandPauseUnpauseEmulator: + a.paused = !a.paused } } diff --git a/apple2sdl/main.go b/apple2sdl/main.go index c64d60b..f9f5c4f 100644 --- a/apple2sdl/main.go +++ b/apple2sdl/main.go @@ -47,6 +47,7 @@ func SDLRun(a *apple2.Apple2) { go a.Run() + paused := false running := true for running { for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { @@ -66,6 +67,15 @@ func SDLRun(a *apple2.Apple2) { } } + if paused != a.IsPaused() { + if a.IsPaused() { + window.SetTitle(a.Name + " - PAUSED!") + } else { + window.SetTitle(a.Name) + } + paused = a.IsPaused() + } + img := apple2.Snapshot(a) if img != nil { surface, err := sdl.CreateRGBSurfaceFrom(unsafe.Pointer(&img.Pix[0]), diff --git a/apple2sdl/sdlKeyboard.go b/apple2sdl/sdlKeyboard.go index 5565700..6aca3eb 100644 --- a/apple2sdl/sdlKeyboard.go +++ b/apple2sdl/sdlKeyboard.go @@ -123,6 +123,8 @@ func (k *sdlKeyboard) putKey(keyEvent *sdl.KeyboardEvent) { } else { fmt.Println("Saving snapshot") } + case sdl.K_PAUSE: + k.a.SendCommand(apple2.CommandPauseUnpauseEmulator) } // Missing values 91 to 95. Usually control for [\]^_