From 9ab4e449b87b0b8b7a4054927c743fa7dbe062d7 Mon Sep 17 00:00:00 2001 From: Ivan Izaguirre Date: Sun, 14 Apr 2019 20:46:40 +0200 Subject: [PATCH] Revert change on keyboard strobe, use it for flow control on the stdin keyboard provider --- apple2/ansiConsoleFrontend.go | 32 ++++++++++++++++++++------------ apple2/ioC0Page.go | 2 +- apple2/softSwitches2.go | 3 ++- apple2sdl/sdlKeyboard.go | 2 +- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/apple2/ansiConsoleFrontend.go b/apple2/ansiConsoleFrontend.go index 4fc02cd..5a2fa83 100644 --- a/apple2/ansiConsoleFrontend.go +++ b/apple2/ansiConsoleFrontend.go @@ -44,24 +44,32 @@ func (fe *ansiConsoleFrontend) subscribeToTextPages() { const refreshDelayMs = 100 -func (fe *ansiConsoleFrontend) GetKey() (key uint8, ok bool) { - stdinReader := func(c chan uint8) { - reader := bufio.NewReader(os.Stdin) - for { - byte, err := reader.ReadByte() - if err != nil { - fmt.Println(err) - return - } - c <- byte - } - } +func (fe *ansiConsoleFrontend) GetKey(strobed bool) (key uint8, ok bool) { + // Init the first time if fe.keyChannel == nil { + stdinReader := func(c chan uint8) { + reader := bufio.NewReader(os.Stdin) + for { + byte, err := reader.ReadByte() + if err != nil { + fmt.Println(err) + return + } + c <- byte + } + } + fe.keyChannel = make(chan uint8, 100) go stdinReader(fe.keyChannel) } + if !strobed { + // We must use the strobe to control the flow from stdin + ok = false + return + } + select { case key = <-fe.keyChannel: if key == 10 { diff --git a/apple2/ioC0Page.go b/apple2/ioC0Page.go index 3e50df2..3cccef8 100644 --- a/apple2/ioC0Page.go +++ b/apple2/ioC0Page.go @@ -16,7 +16,7 @@ type softSwitchR func(io *ioC0Page) uint8 type softSwitchW func(io *ioC0Page, value uint8) type KeyboardProvider interface { - GetKey() (key uint8, ok bool) + GetKey(strobe bool) (key uint8, ok bool) } // See https://www.kreativekorp.com/miscpages/a2info/iomemory.shtml diff --git a/apple2/softSwitches2.go b/apple2/softSwitches2.go index 8bad68d..0a66df9 100644 --- a/apple2/softSwitches2.go +++ b/apple2/softSwitches2.go @@ -93,8 +93,9 @@ func getSoftSwitch(ioFlag uint8, isSet bool) softSwitchR { } func getKeySoftSwitch(io *ioC0Page) uint8 { + strobed := (io.softSwitchesData[ioDataKeyboard] & (1 << 7)) == 0 if io.keyboard != nil { - if key, ok := io.keyboard.GetKey(); ok { + if key, ok := io.keyboard.GetKey(strobed); ok { io.softSwitchesData[ioDataKeyboard] = key + (1 << 7) } } diff --git a/apple2sdl/sdlKeyboard.go b/apple2sdl/sdlKeyboard.go index 965042b..bb35a73 100644 --- a/apple2sdl/sdlKeyboard.go +++ b/apple2sdl/sdlKeyboard.go @@ -93,7 +93,7 @@ func (k *sdlKeyboard) putChar(ch uint8) { k.keyChannel <- ch } -func (k *sdlKeyboard) GetKey() (key uint8, ok bool) { +func (k *sdlKeyboard) GetKey(_ bool) (key uint8, ok bool) { select { case key = <-k.keyChannel: ok = true