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

105 lines
2.1 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"
"github.com/tjboldt/ProDOS-Utilities/prodos"
)
2021-11-03 09:33:09 +00:00
// ReadBlockCommand handles requests to read ProDOS blocks
2022-12-31 17:26:45 +00:00
func ReadBlockCommand(drive1 prodos.ReaderWriterAt, drive2 prodos.ReaderWriterAt) (int, error) {
2021-10-30 11:03:18 +00:00
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
2022-03-04 23:24:59 +00:00
driveUnit, err = comm.ReadByte()
2021-10-30 11:03:18 +00:00
2022-03-04 23:24:59 +00:00
if err != nil {
fmt.Printf("Failed to read block")
2022-12-31 17:26:45 +00:00
return 0, err
2021-10-30 11:03:18 +00:00
}
file := drive1
2022-03-04 23:24:59 +00:00
driveNumber := 1
2021-10-30 11:03:18 +00:00
if driveUnit >= 128 {
file = drive2
2022-03-04 23:24:59 +00:00
driveNumber = 2
2021-10-30 11:03:18 +00:00
}
2022-12-31 16:39:10 +00:00
slotNumber := driveUnit & 0x7F >> 4
2022-03-04 23:24:59 +00:00
2021-10-30 11:03:18 +00:00
block := int(blockHigh)*256 + int(blockLow)
2022-03-04 23:24:59 +00:00
fmt.Printf("Read block %04X in slot %d, drive %d...", block, slotNumber, driveNumber)
2021-10-30 11:03:18 +00:00
2022-12-31 16:39:10 +00:00
buffer, err := prodos.ReadBlock(file, block)
2022-03-04 23:24:59 +00:00
if err != nil {
2022-12-31 16:39:10 +00:00
fmt.Printf("failed %s\n", err)
2022-12-31 17:26:45 +00:00
return 0, err
2022-03-04 23:24:59 +00:00
}
2021-10-30 11:03:18 +00:00
err = comm.WriteBlock(buffer)
if err == nil {
2022-03-04 23:24:59 +00:00
fmt.Printf("succeeded\n")
2021-10-30 11:03:18 +00:00
} else {
2022-12-31 16:39:10 +00:00
fmt.Printf("failed %s\n", err)
2021-10-30 11:03:18 +00:00
}
2022-12-31 17:26:45 +00:00
return block, nil
2021-10-30 11:03:18 +00:00
}
2021-11-03 09:33:09 +00:00
// WriteBlockCommand handles requests to write ProDOS blocks
2022-12-31 17:26:45 +00:00
func WriteBlockCommand(drive1 prodos.ReaderWriterAt, drive2 prodos.ReaderWriterAt) error {
2021-10-30 11:03:18 +00:00
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
2022-03-04 23:24:59 +00:00
driveUnit, err = comm.ReadByte()
if err != nil {
fmt.Printf("Failed to write block")
2022-12-31 17:26:45 +00:00
return err
2021-10-30 11:03:18 +00:00
}
file := drive1
2022-03-04 23:24:59 +00:00
driveNumber := 1
2021-10-30 11:03:18 +00:00
if driveUnit >= 128 {
file = drive2
2022-03-04 23:24:59 +00:00
driveNumber = 2
2021-10-30 11:03:18 +00:00
}
buffer := make([]byte, 512)
block := int(blockHigh)*256 + int(blockLow)
2022-12-31 16:39:10 +00:00
slotNumber := driveUnit & 0x7F >> 4
2021-10-30 11:03:18 +00:00
2022-03-04 23:24:59 +00:00
fmt.Printf("Write block %04X in slot %d, drive %d...", block, slotNumber, driveNumber)
err = comm.ReadBlock(buffer)
if err != nil {
2022-12-31 16:39:10 +00:00
fmt.Printf("failed %s\n", err)
2022-12-31 17:26:45 +00:00
return err
2022-03-04 23:24:59 +00:00
}
fmt.Printf("...")
err = prodos.WriteBlock(file, block, buffer)
if err == nil {
fmt.Printf("succeeded\n")
} else {
fmt.Printf("failed\n")
}
2022-12-31 17:26:45 +00:00
return nil
2021-10-30 11:03:18 +00:00
}