Apple2-IO-RPi/RaspberryPi/apple2driver/handlers/block.go

87 lines
1.7 KiB
Go
Raw Normal View History

2022-01-11 04:00:58 +00:00
// Copyright Terence J. Boldt (c)2020-2022
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
// This file is used for handling ProDOS image block reading and writing
2021-11-03 09:33:09 +00:00
2021-10-30 11:03:18 +00:00
package handlers
import (
"fmt"
"os"
"github.com/tjboldt/ProDOS-Utilities/prodos"
)
var oldFirmware = false
2021-11-03 09:33:09 +00:00
// ReadBlockCommand handles requests to read ProDOS blocks
2021-10-30 11:03:18 +00:00
func ReadBlockCommand(drive1 *os.File, drive2 *os.File) {
blockLow, _ := comm.ReadByte()
blockHigh, _ := comm.ReadByte()
2021-11-03 09:33:09 +00:00
var driveUnit byte
2021-10-30 11:03:18 +00:00
var err error
if !oldFirmware {
driveUnit, err = comm.ReadByte()
fmt.Printf("Drive unit: %0X\n", driveUnit)
if err != nil {
fmt.Printf("Drive unit not sent, assuming older firmware")
oldFirmware = true
}
}
file := drive1
if driveUnit >= 128 {
file = drive2
}
block := int(blockHigh)*256 + int(blockLow)
fmt.Printf("Read block %d\n", block)
buffer := prodos.ReadBlock(file, block)
err = comm.WriteBlock(buffer)
if err == nil {
fmt.Printf("Read block completed\n")
} else {
fmt.Printf("Failed to read block\n")
}
}
2021-11-03 09:33:09 +00:00
// WriteBlockCommand handles requests to write ProDOS blocks
2021-10-30 11:03:18 +00:00
func WriteBlockCommand(drive1 *os.File, drive2 *os.File) {
blockLow, _ := comm.ReadByte()
blockHigh, _ := comm.ReadByte()
2021-11-03 09:33:09 +00:00
var driveUnit byte
2021-10-30 11:03:18 +00:00
var err error
if !oldFirmware {
driveUnit, err = comm.ReadByte()
if err != nil {
fmt.Printf("Drive unit not sent, assuming older firmware")
oldFirmware = true
}
}
file := drive1
if driveUnit >= 128 {
file = drive2
}
buffer := make([]byte, 512)
block := int(blockHigh)*256 + int(blockLow)
fmt.Printf("Write block %d\n", block)
comm.ReadBlock(buffer)
prodos.WriteBlock(file, block, buffer)
fmt.Printf("Write block completed\n")
}