F5 to toggle fast/slow mode

This commit is contained in:
Ivan Izaguirre 2019-05-05 12:38:24 +02:00
parent 78ff401ff0
commit f2c935305b
3 changed files with 34 additions and 7 deletions

View File

@ -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)
}

View File

@ -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)

View File

@ -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 [\]^_