Extract stdin keyboard implementation from the io page simulator

This commit is contained in:
Ivan Izaguirre 2019-02-20 23:51:47 +01:00
parent 5f336810aa
commit 5172919649
3 changed files with 54 additions and 30 deletions

View File

@ -0,0 +1,39 @@
package apple2
import (
"bufio"
"os"
)
type ansiConsoleFrontend struct {
keyChannel chan uint8
}
func _stdinReader(c chan uint8) {
reader := bufio.NewReader(os.Stdin)
for {
byte, err := reader.ReadByte()
if err != nil {
panic(err)
}
c <- byte
}
}
func (fe *ansiConsoleFrontend) getKey() (key uint8, ok bool) {
if fe.keyChannel == nil {
fe.keyChannel = make(chan uint8, 100)
go _stdinReader(fe.keyChannel)
}
select {
case key = <-fe.keyChannel:
if key == 10 {
key = 13
}
ok = true
default:
ok = false
}
return
}

View File

@ -16,6 +16,9 @@ func Run(romFile string, log bool) {
s.Mem.SetPage(uint8(4+j), &(t.pages[j])) s.Mem.SetPage(uint8(4+j), &(t.pages[j]))
} }
var fe ansiConsoleFrontend
io.setKeyboardProvider(&fe)
// Start the processor // Start the processor
core6502.Reset(&s) core6502.Reset(&s)
t.prepare() t.prepare()

View File

@ -1,15 +1,17 @@
package apple2 package apple2
import ( import (
"bufio"
"fmt" "fmt"
"os"
) )
type ioC0Page struct { type ioC0Page struct {
ioFlags uint64 ioFlags uint64
data [1]uint8 data [1]uint8
keyChannel chan uint8 keyboard keyboardProvider
}
type keyboardProvider interface {
getKey() (key uint8, ok bool)
} }
// See https://www.kreativekorp.com/miscpages/a2info/iomemory.shtml // See https://www.kreativekorp.com/miscpages/a2info/iomemory.shtml
@ -92,41 +94,21 @@ func (p *ioC0Page) access(address uint8, isWrite bool, value uint8) uint8 {
return 0 return 0
} }
func (p *ioC0Page) initKeyboard() { func (p *ioC0Page) setKeyboardProvider(kb keyboardProvider) {
if p.keyChannel == nil { p.keyboard = kb
p.keyChannel = make(chan uint8)
go func(c chan uint8) {
reader := bufio.NewReader(os.Stdin)
for {
ch, err := reader.ReadByte()
if err != nil {
panic(err)
}
c <- ch
}
}(p.keyChannel)
}
} }
func (p *ioC0Page) getKey() uint8 { func (p *ioC0Page) getKey() uint8 {
p.initKeyboard()
strobed := (p.data[ioDataKeyboard] & (1 << 7)) == 0 strobed := (p.data[ioDataKeyboard] & (1 << 7)) == 0
if strobed { if strobed && p.keyboard != nil {
select { if key, ok := p.keyboard.getKey(); ok {
case ch := <-p.keyChannel: p.data[ioDataKeyboard] = key + (1 << 7)
if ch == 10 {
ch = 13
}
p.data[ioDataKeyboard] = ch + (1 << 7)
default:
// Nothing
} }
} }
return p.data[ioDataKeyboard] return p.data[ioDataKeyboard]
} }
func (p *ioC0Page) strobeKeyboard() uint8 { func (p *ioC0Page) strobeKeyboard() uint8 {
p.initKeyboard()
result := p.data[ioDataKeyboard] result := p.data[ioDataKeyboard]
p.data[ioDataKeyboard] &^= 1 << 7 p.data[ioDataKeyboard] &^= 1 << 7
return result return result