Skip to content

Commit

Permalink
Project reorg. Separate executables for the SDL and console versions
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanizag committed Jun 1, 2019
1 parent cf4a7e2 commit f9d213a
Show file tree
Hide file tree
Showing 40 changed files with 222 additions and 235 deletions.
3 changes: 2 additions & 1 deletion apple2/apple2.go → apple2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"bufio"
"encoding/binary"
"fmt"
"go6502/core6502"
"io"
"os"
"time"

"github.com/ivanizag/apple2/core6502"
)

// Apple2 represents all the components and state of the emulated machine
Expand Down
173 changes: 0 additions & 173 deletions apple2/ansiConsoleFrontend.go

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file removed apple2/romdumps/Apple2.rom
Binary file not shown.
Binary file removed apple2/romdumps/Apple2e.rom
Binary file not shown.
Binary file removed apple2/romdumps/Apple2e_Enhanced.rom
Binary file not shown.
Binary file removed apple2/romdumps/LanguageCard_3410020F8.bin
Binary file not shown.
20 changes: 1 addition & 19 deletions apple2/apple2Setup.go → apple2Setup.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package apple2

import (
"go6502/core6502"
)
import "github.com/ivanizag/apple2/core6502"

// NewApple2 instantiates an apple2
func NewApple2(romFile string, charRomFile string, clockMhz float64,
Expand Down Expand Up @@ -70,22 +68,6 @@ func (a *Apple2) AddCardInOut(slot int) {
a.insertCard(&cardInOut{}, slot)
}

// ConfigureStdConsole uses stdin and stdout to interface with the Apple2
func (a *Apple2) ConfigureStdConsole(stdinKeyboard bool, stdoutScreen bool) {
if !stdinKeyboard && !stdoutScreen {
return
}

// Init frontend
fe := newAnsiConsoleFrontend(a, stdinKeyboard)
if stdinKeyboard {
a.io.setKeyboardProvider(fe)
}
if stdoutScreen {
go fe.textModeGoRoutine()
}
}

// SetKeyboardProvider attaches an external keyboard provider
func (a *Apple2) SetKeyboardProvider(kb KeyboardProvider) {
a.io.setKeyboardProvider(kb)
Expand Down
Binary file added apple2console/apple2console
Binary file not shown.
112 changes: 112 additions & 0 deletions apple2console/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package main

import (
"bufio"
"fmt"
"os"
"strings"
"time"

apple2 "github.com/ivanizag/apple2"
)

func main() {
a := apple2.MainApple()
fe := &ansiConsoleFrontend{}
a.SetKeyboardProvider(fe)
go fe.textModeGoRoutine(a)

a.Run(false)
}

/*
Uses the console standard input and output to interface with the machine.
Input is buffered until the next CR. This avoids working in place, a line
for input is added at the end.
Outut is done in place using ANSI escape sequences.
Those tricks do not work with the Apple2e ROM
*/

type ansiConsoleFrontend struct {
keyChannel chan uint8
extraLineFeeds chan int
lastContent string
}

const refreshDelayMs = 100

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 {
key, err := reader.ReadByte()
if err != nil {
fmt.Println(err)
return
}

if key == 10 {
key = 13
if fe.extraLineFeeds != nil {
fe.extraLineFeeds <- 1
}
}

c <- key
}
}

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:
ok = true
default:
ok = false
}
return
}

func ansiCursorUp(steps int) string {
return fmt.Sprintf("\033[%vA", steps)
}

func (fe *ansiConsoleFrontend) textModeGoRoutine(a *apple2.Apple2) {
fe.extraLineFeeds = make(chan int, 100)

fmt.Printf(strings.Repeat("\n", 24+3))
for {
// Go up
content := ansiCursorUp(24 + 3)
done := false
for !done {
select {
case lineFeeds := <-fe.extraLineFeeds:
content += ansiCursorUp(lineFeeds)
default:
done = true
}
}

content += apple2.DumpTextModeAnsi(a)
content += "\033[KLine: "

if content != fe.lastContent {
fmt.Print(content)
fe.lastContent = content
}
time.Sleep(refreshDelayMs * time.Millisecond)
}
}
Loading

0 comments on commit f9d213a

Please sign in to comment.