From f2c935305bfbeacad995a27332fd1659b2b3295a Mon Sep 17 00:00:00 2001 From: Ivan Izaguirre Date: Sun, 5 May 2019 12:38:24 +0200 Subject: [PATCH] F5 to toggle fast/slow mode --- apple2/apple2.go | 30 +++++++++++++++++++++++++----- apple2sdl/run.go | 2 +- apple2sdl/sdlKeyboard.go | 9 ++++++++- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/apple2/apple2.go b/apple2/apple2.go index b74ff59..6c3c3fd 100644 --- a/apple2/apple2.go +++ b/apple2/apple2.go @@ -2,6 +2,7 @@ package apple2 import ( "bufio" + "fmt" "go6502/core6502" "os" "time" @@ -87,20 +88,34 @@ func (a *Apple2) SetKeyboardProvider(kb KeyboardProvider) { a.io.setKeyboardProvider(kb) } +const ( + // CommandToggleSpeed toggles cpu speed between full speed and actual Apple II speed + CommandToggleSpeed = iota + 1 +) + // SendCommand enqueues a command to the emulator thread func (a *Apple2) SendCommand(command int) { a.commandChannel <- command } func (a *Apple2) executeCommand(command int) { - //TODO + switch command { + case CommandToggleSpeed: + if a.cycleDurationNs == 0 { + fmt.Println("Slow") + a.cycleDurationNs = 1000.0 / CpuClockMhz + } else { + fmt.Println("Fast") + a.cycleDurationNs = 0 + } + } } // Run starts the Apple2 emulation func (a *Apple2) Run(log bool) { // Start the processor a.cpu.Reset() - startTime := time.Now() + referenceTime := time.Now() for { // Run a 6502 step a.cpu.ExecuteInstruction(log) @@ -118,9 +133,14 @@ func (a *Apple2) Run(log bool) { if a.cycleDurationNs != 0 { // Wait until next 6502 step has to run - clockDuration := time.Since(startTime) - simulatedDurationNs := time.Duration(float64(a.cpu.GetCycles()) * a.cycleDurationNs) - waitDuration := simulatedDurationNs - clockDuration + clockDuration := time.Since(referenceTime) + simulatedDuration := time.Duration(float64(a.cpu.GetCycles()) * a.cycleDurationNs) + waitDuration := simulatedDuration - clockDuration + if waitDuration > 1*time.Second { + // We have to wait too long. Let's fast forward + referenceTime = referenceTime.Add(-waitDuration) + waitDuration = 0 + } if waitDuration > 0 { time.Sleep(waitDuration) } diff --git a/apple2sdl/run.go b/apple2sdl/run.go index 3135675..d66ddcb 100644 --- a/apple2sdl/run.go +++ b/apple2sdl/run.go @@ -21,7 +21,7 @@ func SDLRun(a *apple2.Apple2) { defer renderer.Destroy() window.SetTitle("Apple2") - kp := newSDLKeyBoard() + kp := newSDLKeyBoard(a) a.SetKeyboardProvider(&kp) go a.Run(false) diff --git a/apple2sdl/sdlKeyboard.go b/apple2sdl/sdlKeyboard.go index 7707def..8b65cad 100644 --- a/apple2sdl/sdlKeyboard.go +++ b/apple2sdl/sdlKeyboard.go @@ -1,6 +1,7 @@ package apple2sdl import ( + "go6502/apple2" "unicode/utf8" "github.com/veandco/go-sdl2/sdl" @@ -8,11 +9,13 @@ import ( type sdlKeyboard struct { keyChannel chan uint8 + a *apple2.Apple2 } -func newSDLKeyBoard() sdlKeyboard { +func newSDLKeyBoard(a *apple2.Apple2) sdlKeyboard { var k sdlKeyboard k.keyChannel = make(chan uint8, 100) + k.a = a return k } @@ -84,6 +87,10 @@ func (k *sdlKeyboard) putKey(keyEvent *sdl.KeyboardEvent) { result = 31 case sdl.K_DOWN: result = 10 + + // Control of the emulator + case sdl.K_F5: + k.a.SendCommand(apple2.CommandToggleSpeed) } // Missing values 91 to 95. Usually control for [\]^_