Skip to content

Commit

Permalink
Abstract inputline
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanizag committed Aug 18, 2021
1 parent 2d778a7 commit 425d80c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
9 changes: 4 additions & 5 deletions bbz.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,12 @@ func RunMOS(env *environment) {
After an OSRDCH call: C=0 indicates that a valid character has
been read; C=1 flags an error condition, A contains an error number.
*/
if !env.in.Scan() {
ch, stop := env.in.readChar()
if stop {
env.stop = true
return
}
line := env.in.Text()
// TODO: capture keystrokes. We will just get the first chat of the line
// and ignore the rest.
ch := line[0]

pOut := p &^ 1 // Clear carry
env.cpu.SetAXYP(ch, x, y, pOut)

Expand Down
5 changes: 2 additions & 3 deletions environment.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"bufio"
"fmt"
"os"
"time"
Expand All @@ -13,7 +12,7 @@ type environment struct {
cpu *core6502.State
mem *acornMemory
vdu *vdu
in *bufio.Scanner
in input

// clock, used by OSWORD01 and 02
referenceTime time.Time
Expand All @@ -36,7 +35,7 @@ type environment struct {

func newEnvironment(cpuLog bool, apiLog bool, apiLogIO bool, memLog bool, panicOnErr bool) *environment {
var env environment
env.in = bufio.NewScanner(os.Stdin)
env.in = newInputSimple()
env.referenceTime = time.Now()
env.timer = 0
env.lastTimerUpdate = time.Now()
Expand Down
40 changes: 40 additions & 0 deletions input.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"bufio"
"os"
)

type input interface {
readline() (string, bool)
readChar() (uint8, bool)
}

type inputSimple struct {
in *bufio.Scanner
}

func newInputSimple() *inputSimple {
var i inputSimple
i.in = bufio.NewScanner(os.Stdin)
return &i
}

func (i *inputSimple) readline() (string, bool) {
if !i.in.Scan() {
return "", true
}
line := i.in.Text()
return line, false
}

func (i *inputSimple) readChar() (uint8, bool) {
// TODO: capture keystrokes. We will just get the first char of the line
// and ignore the rest.
s, stop := i.readline()
if s == "" {
return ' ', stop
} else {
return s[0], stop
}
}
4 changes: 2 additions & 2 deletions osWord.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ func execOSWORD(env *environment) {
line. C not equal to zero indicates that an escape condition terminated
entry. Y is set to the length of the line, excluding the CR if C=0.
*/
if !env.in.Scan() {
line, stop := env.in.readline()
if stop {
env.stop = true
return
}
line := env.in.Text()

// TODO: check max size
buffer := env.mem.peekWord(xy)
Expand Down

0 comments on commit 425d80c

Please sign in to comment.