Apple2-IO-RPi/RaspberryPi/Driver.go

452 lines
9.4 KiB
Go
Raw Normal View History

2020-12-10 04:58:53 +00:00
package main
import (
2020-12-23 22:19:42 +00:00
"bytes"
"errors"
"fmt"
"os"
"os/exec"
2020-12-10 04:58:53 +00:00
"periph.io/x/periph/conn/gpio"
"periph.io/x/periph/conn/gpio/gpioreg"
"periph.io/x/periph/host"
"strings"
2020-12-16 02:05:12 +00:00
"time"
2020-12-10 04:58:53 +00:00
)
2020-12-23 22:19:42 +00:00
2020-12-16 02:05:12 +00:00
var edgeTimeout time.Duration
2020-12-10 04:58:53 +00:00
var out_write gpio.PinIO
var out_read gpio.PinIO
2021-01-16 20:55:32 +00:00
var out_reserved2 gpio.PinIO
var out_reserved1 gpio.PinIO
var out_bit7 gpio.PinIO
var out_bit6 gpio.PinIO
var out_bit5 gpio.PinIO
var out_bit4 gpio.PinIO
2020-12-10 04:58:53 +00:00
var out_bit3 gpio.PinIO
var out_bit2 gpio.PinIO
var out_bit1 gpio.PinIO
var out_bit0 gpio.PinIO
var in_write gpio.PinIO
var in_read gpio.PinIO
2021-01-16 20:55:32 +00:00
var in_reserved2 gpio.PinIO
var in_reserved1 gpio.PinIO
var in_bit7 gpio.PinIO
var in_bit6 gpio.PinIO
var in_bit5 gpio.PinIO
var in_bit4 gpio.PinIO
2020-12-10 04:58:53 +00:00
var in_bit3 gpio.PinIO
var in_bit2 gpio.PinIO
var in_bit1 gpio.PinIO
var in_bit0 gpio.PinIO
2020-12-16 02:05:12 +00:00
const ReadBlockCommand = 1
const WriteBlockCommand = 2
const GetTimeCommand = 3
2020-12-23 22:19:42 +00:00
const ChangeDriveCommand = 4
const ExecCommand = 5
2021-03-28 14:25:30 +00:00
const LoadFileCommand = 6
const SaveFileCommand = 7
2020-12-23 22:19:42 +00:00
var debug bool = false
func main() {
host.Init()
initGpio()
if len(os.Args) == 3 && os.Args[2] == "--debug" {
debug = true
}
fmt.Printf("Starting Apple II RPi...\n")
fileName := os.Args[1]
file, err := os.OpenFile(fileName, os.O_RDWR, 0755)
if err != nil {
fmt.Printf("ERROR: %s", err.Error())
os.Exit(1)
}
for {
if debug {
fmt.Printf("Check for command\n")
}
command, err := readByte()
if err != nil {
//fmt.Printf("Timed out waiting for command\n")
} else {
switch command {
case ReadBlockCommand:
handleReadBlockCommand(file)
case WriteBlockCommand:
handleWriteBlockCommand(file)
case GetTimeCommand:
handleGetTimeCommand()
case ExecCommand:
handleExecCommand()
}
}
}
}
func handleReadBlockCommand(file *os.File) {
blockLow, _ := readByte()
blockHigh, _ := readByte()
buffer := make([]byte, 512)
var block int64
block = int64(blockHigh)*256 + int64(blockLow)
fmt.Printf("Read block %d\n", block)
file.ReadAt(buffer, int64(block)*512)
//dumpBlock(buffer)
2021-01-16 22:12:17 +00:00
err := readBlock(buffer)
if err == nil {
fmt.Printf("Read block completed\n")
} else {
fmt.Printf("Failed to read block\n")
}
2020-12-23 22:19:42 +00:00
}
func handleWriteBlockCommand(file *os.File) {
blockLow, _ := readByte()
blockHigh, _ := readByte()
buffer := make([]byte, 512)
var block int64
block = int64(blockHigh)*256 + int64(blockLow)
fmt.Printf("Write block %d\n", block)
writeBlock(buffer)
file.WriteAt(buffer, int64(block)*512)
file.Sync()
2021-01-16 22:12:17 +00:00
fmt.Printf("Write block completed\n")
2020-12-23 22:19:42 +00:00
}
func handleExecCommand() {
2020-12-24 03:53:20 +00:00
fmt.Printf("Reading command to execute...\n")
2021-03-20 23:25:28 +00:00
linuxCommand, err := readString()
2020-12-24 03:53:20 +00:00
fmt.Printf("Command to run: %s\n", linuxCommand)
2020-12-23 22:19:42 +00:00
cmd := exec.Command("bash", "-c", linuxCommand)
cmdOut, err := cmd.Output()
if err != nil {
fmt.Printf("Failed to execute command\n")
2020-12-24 03:53:20 +00:00
writeString("Failed to execute command")
return
}
fmt.Printf("Command output: %s\n", cmdOut)
apple2string := strings.Replace(string(cmdOut), "\n", "\r", -1)
err = writeString(apple2string)
2020-12-24 03:53:20 +00:00
if err != nil {
fmt.Printf("Failed to send command output\n")
return
2020-12-23 22:19:42 +00:00
}
}
func handleGetTimeCommand() {
2021-01-16 22:12:17 +00:00
fmt.Printf("Sending date/time...\n")
2020-12-23 22:19:42 +00:00
/* 49041 ($BF91) 49040 ($BF90)
7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
DATE: | year | month | day |
+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
TIME: | hour | | minute |
+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
49043 ($BF93) 49042 ($BF92)
*/
now := time.Now()
year := now.Year() % 100
month := now.Month()
day := now.Day()
hour := now.Hour()
minute := now.Minute()
bf91 := (byte(year) << 1) + (byte(month) >> 3)
bf90 := ((byte(month) & 15) << 5) + byte(day)
bf93 := byte(hour)
bf92 := byte(minute)
writeByte(bf90)
writeByte(bf91)
writeByte(bf92)
writeByte(bf93)
2021-01-16 22:12:17 +00:00
fmt.Printf("Send time complete\n")
2020-12-23 22:19:42 +00:00
}
func readBlock(buffer []byte) error {
for i := 0; i < 512; i++ {
err := writeByte(buffer[i])
if err != nil {
return err
}
}
return nil
}
func writeBlock(buffer []byte) error {
var err error
for i := 0; i < 512; i++ {
buffer[i], err = readByte()
if err != nil {
return err
}
}
return nil
}
func initGpio() {
2021-01-16 20:55:32 +00:00
out_write = gpioreg.ByName("GPIO24")
out_read = gpioreg.ByName("GPIO25")
out_reserved2 = gpioreg.ByName("GPIO7") //note GPIO7 and CPIO8 require extra effort to use
out_reserved1 = gpioreg.ByName("GPIO8")
out_bit7 = gpioreg.ByName("GPIO5")
out_bit6 = gpioreg.ByName("GPIO11")
out_bit5 = gpioreg.ByName("GPIO9")
out_bit4 = gpioreg.ByName("GPIO10")
2020-12-23 22:19:42 +00:00
out_bit3 = gpioreg.ByName("GPIO22")
out_bit2 = gpioreg.ByName("GPIO27")
out_bit1 = gpioreg.ByName("GPIO17")
out_bit0 = gpioreg.ByName("GPIO4")
2021-01-16 20:55:32 +00:00
in_write = gpioreg.ByName("GPIO23")
in_read = gpioreg.ByName("GPIO18")
in_reserved2 = gpioreg.ByName("GPIO14")
in_reserved1 = gpioreg.ByName("GPIO15")
in_bit7 = gpioreg.ByName("GPIO12")
in_bit6 = gpioreg.ByName("GPIO16")
in_bit5 = gpioreg.ByName("GPIO20")
in_bit4 = gpioreg.ByName("GPIO21")
2020-12-23 22:19:42 +00:00
in_bit3 = gpioreg.ByName("GPIO26")
in_bit2 = gpioreg.ByName("GPIO19")
in_bit1 = gpioreg.ByName("GPIO13")
in_bit0 = gpioreg.ByName("GPIO6")
in_write.In(gpio.PullDown, gpio.BothEdges)
in_read.In(gpio.PullDown, gpio.BothEdges)
2021-01-16 20:55:32 +00:00
out_reserved1.Out(gpio.High)
out_reserved2.Out(gpio.High)
out_read.Out(gpio.High)
out_write.Out(gpio.High)
out_bit7.Out(gpio.Low)
out_bit6.Out(gpio.Low)
out_bit5.Out(gpio.Low)
out_bit4.Out(gpio.Low)
out_bit3.Out(gpio.Low)
out_bit2.Out(gpio.Low)
out_bit1.Out(gpio.Low)
out_bit0.Out(gpio.Low)
2020-12-23 22:19:42 +00:00
edgeTimeout = time.Second * 5
}
func dumpBlock(buffer []byte) {
for i := 0; i < 512; i++ {
fmt.Printf("%02X ", buffer[i])
}
}
func readString() (string, error) {
var inBytes bytes.Buffer
for {
2021-03-20 23:25:28 +00:00
inByte, err := readByte()
2020-12-23 22:19:42 +00:00
if err != nil {
return "", err
}
if inByte == 0 {
break
}
2020-12-23 22:19:42 +00:00
inBytes.WriteByte(inByte)
}
return string(inBytes.Bytes()), nil
}
2020-12-24 03:53:20 +00:00
func writeString(outString string) error {
for _, character := range outString {
2021-03-20 23:25:28 +00:00
err := writeByte(byte(character) | 128)
2020-12-23 22:19:42 +00:00
if err != nil {
2020-12-24 03:53:20 +00:00
fmt.Printf("Failed to write string\n")
2020-12-23 22:19:42 +00:00
return err
}
}
writeByte(0)
return nil
}
func readByte() (byte, error) {
2020-12-10 04:58:53 +00:00
// let the Apple II know we are ready to read
2020-12-23 22:19:42 +00:00
if debug {
fmt.Printf("let the Apple II know we are ready to read\n")
}
2020-12-10 04:58:53 +00:00
out_read.Out(gpio.Low)
// wait for the Apple II to write
2020-12-23 22:19:42 +00:00
if debug {
fmt.Printf("wait for the Apple II to write\n")
}
2020-12-10 04:58:53 +00:00
for in_write.Read() == gpio.High {
2020-12-16 02:05:12 +00:00
if !in_write.WaitForEdge(edgeTimeout) {
2020-12-23 22:19:42 +00:00
if debug {
fmt.Printf("Timed out reading byte -- write stuck high\n")
2020-12-23 22:19:42 +00:00
}
return 0, errors.New("Timed out reading byte -- write stuck high\n")
2020-12-16 02:05:12 +00:00
}
2020-12-10 04:58:53 +00:00
}
// get a nibble of data
2020-12-23 22:19:42 +00:00
if debug {
fmt.Printf("get a byte of data\n")
}
var data byte
data = 0
bit7 := in_bit7.Read()
bit6 := in_bit6.Read()
bit5 := in_bit5.Read()
bit4 := in_bit4.Read()
2020-12-10 04:58:53 +00:00
bit3 := in_bit3.Read()
bit2 := in_bit2.Read()
bit1 := in_bit1.Read()
bit0 := in_bit0.Read()
if bit7 == gpio.High {
data += 128
}
if bit6 == gpio.High {
data += 64
}
if bit5 == gpio.High {
data += 32
}
if bit4 == gpio.High {
data += 16
}
2020-12-10 04:58:53 +00:00
if bit3 == gpio.High {
data += 8
2020-12-10 04:58:53 +00:00
}
if bit2 == gpio.High {
data += 4
2020-12-10 04:58:53 +00:00
}
if bit1 == gpio.High {
data += 2
2020-12-10 04:58:53 +00:00
}
if bit0 == gpio.High {
data += 1
2020-12-10 04:58:53 +00:00
}
// let the Apple II know we are done reading
2020-12-11 03:44:03 +00:00
//fmt.Printf("let the Apple II know we are done reading\n")
2020-12-10 04:58:53 +00:00
out_read.Out(gpio.High)
// wait for the Apple II to finish writing
2020-12-11 03:44:03 +00:00
//fmt.Printf("wait for the Apple II to finish writing\n")
2020-12-10 04:58:53 +00:00
for in_write.Read() == gpio.Low {
2020-12-16 02:05:12 +00:00
if !in_write.WaitForEdge(edgeTimeout) {
2020-12-23 22:19:42 +00:00
if debug {
fmt.Printf("Timed out reading byte -- write stuck low\n")
2020-12-23 22:19:42 +00:00
}
return 0, errors.New("Timed out reading byte -- write stuck low")
2020-12-16 02:05:12 +00:00
}
2020-12-10 04:58:53 +00:00
}
return data, nil
2020-12-10 04:58:53 +00:00
}
func writeByte(data byte) error {
2020-12-10 04:58:53 +00:00
// wait for the Apple II to be ready to read
2020-12-23 22:19:42 +00:00
if debug {
fmt.Printf("wait for the Apple II to be ready to read\n")
}
2020-12-10 04:58:53 +00:00
for in_read.Read() == gpio.High {
2020-12-16 02:05:12 +00:00
if !in_read.WaitForEdge(edgeTimeout) {
2020-12-23 22:19:42 +00:00
if debug {
fmt.Printf("Timed out writing byte -- read stuck high\n")
2020-12-23 22:19:42 +00:00
}
return errors.New("Timed out writing byte -- read stuck high")
2020-12-16 02:05:12 +00:00
}
2020-12-10 04:58:53 +00:00
}
bit7 := gpio.Low
bit6 := gpio.Low
bit5 := gpio.Low
bit4 := gpio.Low
2020-12-10 04:58:53 +00:00
bit3 := gpio.Low
bit2 := gpio.Low
bit1 := gpio.Low
bit0 := gpio.Low
if ((data & 128) >> 7) == 1 {
bit7 = gpio.High
}
out_bit7.Out(bit7)
if ((data & 64) >> 6) == 1 {
bit6 = gpio.High
}
out_bit6.Out(bit6)
if ((data & 32) >> 5) == 1 {
bit5 = gpio.High
}
out_bit5.Out(bit5)
if ((data & 16) >> 4) == 1 {
bit4 = gpio.High
}
out_bit4.Out(bit4)
2020-12-10 04:58:53 +00:00
if ((data & 8) >> 3) == 1 {
bit3 = gpio.High
}
out_bit3.Out(bit3)
if ((data & 4) >> 2) == 1 {
bit2 = gpio.High
}
out_bit2.Out(bit2)
if ((data & 2) >> 1) == 1 {
bit1 = gpio.High
}
out_bit1.Out(bit1)
if (data & 1) == 1 {
bit0 = gpio.High
}
out_bit0.Out(bit0)
// let Apple II know we're writing
2020-12-23 22:19:42 +00:00
if debug {
fmt.Printf("let Apple II know we're writing\n")
}
2020-12-10 04:58:53 +00:00
out_write.Out(gpio.Low)
// wait for the Apple II to finsih reading
2020-12-11 03:44:03 +00:00
//fmt.Printf("wait for the Apple II to finsih reading\n")
2020-12-10 04:58:53 +00:00
for in_read.Read() == gpio.Low {
2020-12-16 02:05:12 +00:00
if !in_read.WaitForEdge(edgeTimeout) {
2020-12-23 22:19:42 +00:00
if debug {
fmt.Printf("Timed out writing byte -- read stuck low\n")
2020-12-23 22:19:42 +00:00
}
return errors.New("Timed out writing byte -- read stuck low")
2020-12-16 02:05:12 +00:00
}
2020-12-10 04:58:53 +00:00
}
// let the Apple II know we are done writing
2020-12-23 22:19:42 +00:00
if debug {
fmt.Printf("let the Apple II know we are done writing\n")
2020-12-11 03:44:03 +00:00
}
2020-12-23 22:19:42 +00:00
out_write.Out(gpio.High)
2020-12-16 02:05:12 +00:00
return nil
2020-12-10 04:58:53 +00:00
}