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 ( import (
"bufio" "bufio"
"fmt"
"go6502/core6502" "go6502/core6502"
"os" "os"
"time" "time"
@ -87,20 +88,34 @@ func (a *Apple2) SetKeyboardProvider(kb KeyboardProvider) {
a.io.setKeyboardProvider(kb) 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 // SendCommand enqueues a command to the emulator thread
func (a *Apple2) SendCommand(command int) { func (a *Apple2) SendCommand(command int) {
a.commandChannel <- command a.commandChannel <- command
} }
func (a *Apple2) executeCommand(command int) { 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 // Run starts the Apple2 emulation
func (a *Apple2) Run(log bool) { func (a *Apple2) Run(log bool) {
// Start the processor // Start the processor
a.cpu.Reset() a.cpu.Reset()
startTime := time.Now() referenceTime := time.Now()
for { for {
// Run a 6502 step // Run a 6502 step
a.cpu.ExecuteInstruction(log) a.cpu.ExecuteInstruction(log)
@ -118,9 +133,14 @@ func (a *Apple2) Run(log bool) {
if a.cycleDurationNs != 0 { if a.cycleDurationNs != 0 {
// Wait until next 6502 step has to run // Wait until next 6502 step has to run
clockDuration := time.Since(startTime) clockDuration := time.Since(referenceTime)
simulatedDurationNs := time.Duration(float64(a.cpu.GetCycles()) * a.cycleDurationNs) simulatedDuration := time.Duration(float64(a.cpu.GetCycles()) * a.cycleDurationNs)
waitDuration := simulatedDurationNs - clockDuration 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 { if waitDuration > 0 {
time.Sleep(waitDuration) time.Sleep(waitDuration)
} }

View File

@ -21,7 +21,7 @@ func SDLRun(a *apple2.Apple2) {
defer renderer.Destroy() defer renderer.Destroy()
window.SetTitle("Apple2") window.SetTitle("Apple2")
kp := newSDLKeyBoard() kp := newSDLKeyBoard(a)
a.SetKeyboardProvider(&kp) a.SetKeyboardProvider(&kp)
go a.Run(false) go a.Run(false)

View File

@ -1,6 +1,7 @@
package apple2sdl package apple2sdl
import ( import (
"go6502/apple2"
"unicode/utf8" "unicode/utf8"
"github.com/veandco/go-sdl2/sdl" "github.com/veandco/go-sdl2/sdl"
@ -8,11 +9,13 @@ import (
type sdlKeyboard struct { type sdlKeyboard struct {
keyChannel chan uint8 keyChannel chan uint8
a *apple2.Apple2
} }
func newSDLKeyBoard() sdlKeyboard { func newSDLKeyBoard(a *apple2.Apple2) sdlKeyboard {
var k sdlKeyboard var k sdlKeyboard
k.keyChannel = make(chan uint8, 100) k.keyChannel = make(chan uint8, 100)
k.a = a
return k return k
} }
@ -84,6 +87,10 @@ func (k *sdlKeyboard) putKey(keyEvent *sdl.KeyboardEvent) {
result = 31 result = 31
case sdl.K_DOWN: case sdl.K_DOWN:
result = 10 result = 10
// Control of the emulator
case sdl.K_F5:
k.a.SendCommand(apple2.CommandToggleSpeed)
} }
// Missing values 91 to 95. Usually control for [\]^_ // Missing values 91 to 95. Usually control for [\]^_