mirror of
https://github.com/freewilll/apple2-go.git
synced 2024-06-15 22:29:27 +00:00
Made a bunch of things private in disk.go
This commit is contained in:
parent
543bb6925c
commit
2aa16a20f0
124
mmu/disk.go
124
mmu/disk.go
|
@ -40,35 +40,35 @@ type disk struct {
|
||||||
var imagePath string // Loaded disk image path
|
var imagePath string // Loaded disk image path
|
||||||
var image disk // A loaded disk image
|
var image disk // A loaded disk image
|
||||||
var imageIsDirty bool // If an image has been written to and needs a flush
|
var imageIsDirty bool // If an image has been written to and needs a flush
|
||||||
var TrackData [trackDataBytes]uint8 // Converted image data as it it returned by the disk controller for a single track
|
var trackData [trackDataBytes]uint8 // Converted image data as it it returned by the disk controller for a single track
|
||||||
|
|
||||||
// vars to keep track of writes
|
// vars to keep track of writes
|
||||||
const (
|
const (
|
||||||
WaitingForDataPrologue byte = 1 + iota
|
waitingForDataPrologue byte = 1 + iota
|
||||||
ReceivingData
|
receivingData
|
||||||
)
|
)
|
||||||
|
|
||||||
const rawDataBufferSize = diskSectorBytes + 16
|
const rawDataBufferSize = diskSectorBytes + 16
|
||||||
|
|
||||||
type AddressField struct {
|
type addressField struct {
|
||||||
volume uint8
|
volume uint8
|
||||||
track uint8
|
track uint8
|
||||||
sector uint8
|
sector uint8
|
||||||
}
|
}
|
||||||
|
|
||||||
var lastReadAddress AddressField
|
var lastReadAddress addressField
|
||||||
var lastReadSectorDataPosition int
|
var lastReadSectorDataPosition int
|
||||||
|
|
||||||
var SectorWriteState struct {
|
var sectorWriteState struct {
|
||||||
State byte
|
State byte
|
||||||
RawData [rawDataBufferSize]uint8
|
RawData [rawDataBufferSize]uint8
|
||||||
RawDataPosition uint16
|
RawDataPosition uint16
|
||||||
Address AddressField
|
Address addressField
|
||||||
}
|
}
|
||||||
|
|
||||||
func ResetSectorWriteState() {
|
func resetsectorWriteState() {
|
||||||
SectorWriteState.State = WaitingForDataPrologue
|
sectorWriteState.State = waitingForDataPrologue
|
||||||
SectorWriteState.RawDataPosition = 0
|
sectorWriteState.RawDataPosition = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitDiskImage() {
|
func InitDiskImage() {
|
||||||
|
@ -116,7 +116,7 @@ func InitDiskImage() {
|
||||||
sixTwoDecoding[sixTwoEncoding[i]] = i
|
sixTwoDecoding[sixTwoEncoding[i]] = i
|
||||||
}
|
}
|
||||||
|
|
||||||
ResetSectorWriteState()
|
resetsectorWriteState()
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadDiskImage(path string) {
|
func ReadDiskImage(path string) {
|
||||||
|
@ -230,7 +230,7 @@ func sectorDataDecode(data []uint8) (sector [0x100]uint8) {
|
||||||
|
|
||||||
func clearTrackData() {
|
func clearTrackData() {
|
||||||
for i := 0; i < trackDataBytes; i++ {
|
for i := 0; i < trackDataBytes; i++ {
|
||||||
TrackData[i] = 0
|
trackData[i] = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -247,29 +247,29 @@ func makeSectorData(track uint8, physicalSector uint8) {
|
||||||
csL, csH := oddEvenEncode(checksum)
|
csL, csH := oddEvenEncode(checksum)
|
||||||
|
|
||||||
// Address field prologue
|
// Address field prologue
|
||||||
TrackData[offset+0] = 0xd5
|
trackData[offset+0] = 0xd5
|
||||||
TrackData[offset+1] = 0xaa
|
trackData[offset+1] = 0xaa
|
||||||
TrackData[offset+2] = 0x96
|
trackData[offset+2] = 0x96
|
||||||
|
|
||||||
// Volume, track, sector and checksum
|
// Volume, track, sector and checksum
|
||||||
TrackData[offset+3] = volL
|
trackData[offset+3] = volL
|
||||||
TrackData[offset+4] = volH
|
trackData[offset+4] = volH
|
||||||
TrackData[offset+5] = trL
|
trackData[offset+5] = trL
|
||||||
TrackData[offset+6] = trH
|
trackData[offset+6] = trH
|
||||||
TrackData[offset+7] = seL
|
trackData[offset+7] = seL
|
||||||
TrackData[offset+8] = seH
|
trackData[offset+8] = seH
|
||||||
TrackData[offset+9] = csL
|
trackData[offset+9] = csL
|
||||||
TrackData[offset+10] = csH
|
trackData[offset+10] = csH
|
||||||
|
|
||||||
// Address epilogue
|
// Address epilogue
|
||||||
TrackData[offset+11] = 0xde
|
trackData[offset+11] = 0xde
|
||||||
TrackData[offset+12] = 0xaa
|
trackData[offset+12] = 0xaa
|
||||||
TrackData[offset+13] = 0xeb
|
trackData[offset+13] = 0xeb
|
||||||
|
|
||||||
// Data field prologue
|
// Data field prologue
|
||||||
TrackData[offset+14] = 0xd5
|
trackData[offset+14] = 0xd5
|
||||||
TrackData[offset+15] = 0xaa
|
trackData[offset+15] = 0xaa
|
||||||
TrackData[offset+16] = 0xad
|
trackData[offset+16] = 0xad
|
||||||
|
|
||||||
sectorData := sectorDataEncode(image.tracks[track].sectors[logicalSector])
|
sectorData := sectorDataEncode(image.tracks[track].sectors[logicalSector])
|
||||||
|
|
||||||
|
@ -278,17 +278,17 @@ func makeSectorData(track uint8, physicalSector uint8) {
|
||||||
for i := 0; i < 0x56+0x100; i++ {
|
for i := 0; i < 0x56+0x100; i++ {
|
||||||
a ^= sectorData[i]
|
a ^= sectorData[i]
|
||||||
b := sixTwoEncoding[a]
|
b := sixTwoEncoding[a]
|
||||||
TrackData[offset+17+i] = b
|
trackData[offset+17+i] = b
|
||||||
a = sectorData[i]
|
a = sectorData[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the checksum byte
|
// Set the checksum byte
|
||||||
TrackData[offset+17+0x56+0x100] = sixTwoEncoding[a]
|
trackData[offset+17+0x56+0x100] = sixTwoEncoding[a]
|
||||||
|
|
||||||
// Data epilogue
|
// Data epilogue
|
||||||
TrackData[offset+17+0x56+0x100+1] = 0xde
|
trackData[offset+17+0x56+0x100+1] = 0xde
|
||||||
TrackData[offset+17+0x56+0x100+2] = 0xaa
|
trackData[offset+17+0x56+0x100+2] = 0xaa
|
||||||
TrackData[offset+17+0x56+0x100+3] = 0xeb
|
trackData[offset+17+0x56+0x100+3] = 0xeb
|
||||||
}
|
}
|
||||||
|
|
||||||
func MakeTrackData(armPosition uint8) {
|
func MakeTrackData(armPosition uint8) {
|
||||||
|
@ -303,14 +303,14 @@ func MakeTrackData(armPosition uint8) {
|
||||||
|
|
||||||
DriveState.BytePosition = 0 // Point the head at the first sector
|
DriveState.BytePosition = 0 // Point the head at the first sector
|
||||||
|
|
||||||
// For each sector, encode the data and add it to TrackData
|
// For each sector, encode the data and add it to trackData
|
||||||
for physicalSector := uint8(0); physicalSector < sectorsPerTrack; physicalSector++ {
|
for physicalSector := uint8(0); physicalSector < sectorsPerTrack; physicalSector++ {
|
||||||
makeSectorData(track, physicalSector)
|
makeSectorData(track, physicalSector)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecodeAddressField(data []uint8) AddressField {
|
func decodeAddressField(data []uint8) addressField {
|
||||||
var af AddressField
|
var af addressField
|
||||||
af.volume = oddEvenDecode(data[0], data[1])
|
af.volume = oddEvenDecode(data[0], data[1])
|
||||||
af.track = oddEvenDecode(data[2], data[3])
|
af.track = oddEvenDecode(data[2], data[3])
|
||||||
af.sector = oddEvenDecode(data[4], data[5])
|
af.sector = oddEvenDecode(data[4], data[5])
|
||||||
|
@ -319,15 +319,15 @@ func DecodeAddressField(data []uint8) AddressField {
|
||||||
|
|
||||||
// Read a byte from the disk head and spin the disk along
|
// Read a byte from the disk head and spin the disk along
|
||||||
func ReadTrackData() (result uint8) {
|
func ReadTrackData() (result uint8) {
|
||||||
result = TrackData[DriveState.BytePosition]
|
result = trackData[DriveState.BytePosition]
|
||||||
|
|
||||||
if DriveState.BytePosition >= 9 {
|
if DriveState.BytePosition >= 9 {
|
||||||
if TrackData[DriveState.BytePosition-9] == 0xd5 &&
|
if trackData[DriveState.BytePosition-9] == 0xd5 &&
|
||||||
TrackData[DriveState.BytePosition-8] == 0xaa &&
|
trackData[DriveState.BytePosition-8] == 0xaa &&
|
||||||
TrackData[DriveState.BytePosition-7] == 0x96 {
|
trackData[DriveState.BytePosition-7] == 0x96 {
|
||||||
var addressData []uint8
|
var addressData []uint8
|
||||||
addressData = TrackData[DriveState.BytePosition-6 : DriveState.BytePosition]
|
addressData = trackData[DriveState.BytePosition-6 : DriveState.BytePosition]
|
||||||
lastReadAddress = DecodeAddressField(addressData)
|
lastReadAddress = decodeAddressField(addressData)
|
||||||
lastReadSectorDataPosition = DriveState.BytePosition + 8
|
lastReadSectorDataPosition = DriveState.BytePosition + 8
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -353,32 +353,32 @@ func ReadTrackData() (result uint8) {
|
||||||
// The sector is decoded and updated in memory once the 0x156 data bytes have
|
// The sector is decoded and updated in memory once the 0x156 data bytes have
|
||||||
// been read. The image is flagged as dirty and flushed on exit.
|
// been read. The image is flagged as dirty and flushed on exit.
|
||||||
func WriteTrackData(value uint8) {
|
func WriteTrackData(value uint8) {
|
||||||
if SectorWriteState.State == WaitingForDataPrologue {
|
if sectorWriteState.State == waitingForDataPrologue {
|
||||||
if SectorWriteState.RawDataPosition >= 16 {
|
if sectorWriteState.RawDataPosition >= 16 {
|
||||||
ResetSectorWriteState()
|
resetsectorWriteState()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
SectorWriteState.RawData[SectorWriteState.RawDataPosition] = value
|
sectorWriteState.RawData[sectorWriteState.RawDataPosition] = value
|
||||||
SectorWriteState.RawDataPosition += 1
|
sectorWriteState.RawDataPosition += 1
|
||||||
|
|
||||||
// Check for address prologue
|
// Check for address prologue
|
||||||
if SectorWriteState.RawDataPosition > 2 && SectorWriteState.RawData[SectorWriteState.RawDataPosition-3] == 0xd5 &&
|
if sectorWriteState.RawDataPosition > 2 && sectorWriteState.RawData[sectorWriteState.RawDataPosition-3] == 0xd5 &&
|
||||||
SectorWriteState.RawData[SectorWriteState.RawDataPosition-2] == 0xaa &&
|
sectorWriteState.RawData[sectorWriteState.RawDataPosition-2] == 0xaa &&
|
||||||
SectorWriteState.RawData[SectorWriteState.RawDataPosition-1] == 0xad {
|
sectorWriteState.RawData[sectorWriteState.RawDataPosition-1] == 0xad {
|
||||||
|
|
||||||
// We got it, record the last read address field and reset RawDataPosition
|
// We got it, record the last read address field and reset RawDataPosition
|
||||||
SectorWriteState.State = ReceivingData
|
sectorWriteState.State = receivingData
|
||||||
SectorWriteState.Address = lastReadAddress
|
sectorWriteState.Address = lastReadAddress
|
||||||
SectorWriteState.RawDataPosition = 0
|
sectorWriteState.RawDataPosition = 0
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if SectorWriteState.State == ReceivingData {
|
} else if sectorWriteState.State == receivingData {
|
||||||
SectorWriteState.RawData[SectorWriteState.RawDataPosition] = value
|
sectorWriteState.RawData[sectorWriteState.RawDataPosition] = value
|
||||||
SectorWriteState.RawDataPosition += 1
|
sectorWriteState.RawDataPosition += 1
|
||||||
|
|
||||||
if SectorWriteState.RawDataPosition == 0x56+0x100 {
|
if sectorWriteState.RawDataPosition == 0x56+0x100 {
|
||||||
// We have the full sector data
|
// We have the full sector data
|
||||||
physicalSector := lastReadAddress.sector
|
physicalSector := lastReadAddress.sector
|
||||||
logicalSector := dos33SectorInterleaving[physicalSector]
|
logicalSector := dos33SectorInterleaving[physicalSector]
|
||||||
|
@ -386,19 +386,19 @@ func WriteTrackData(value uint8) {
|
||||||
// transform the data from disk bytes to 6-bytes and EOR it
|
// transform the data from disk bytes to 6-bytes and EOR it
|
||||||
a := uint8(0)
|
a := uint8(0)
|
||||||
for i := 0; i < 0x56+0x100; i++ {
|
for i := 0; i < 0x56+0x100; i++ {
|
||||||
b := sixTwoDecoding[SectorWriteState.RawData[i]]
|
b := sixTwoDecoding[sectorWriteState.RawData[i]]
|
||||||
a ^= b
|
a ^= b
|
||||||
SectorWriteState.RawData[i] = a
|
sectorWriteState.RawData[i] = a
|
||||||
}
|
}
|
||||||
|
|
||||||
// Transform the 0x156 bytes into the final 0x100 bytes
|
// Transform the 0x156 bytes into the final 0x100 bytes
|
||||||
sectorData := sectorDataDecode(SectorWriteState.RawData[0:0x156])
|
sectorData := sectorDataDecode(sectorWriteState.RawData[0:0x156])
|
||||||
|
|
||||||
// Save the data to memory & recreate the raw sector data
|
// Save the data to memory & recreate the raw sector data
|
||||||
image.tracks[lastReadAddress.track].sectors[logicalSector].data = sectorData
|
image.tracks[lastReadAddress.track].sectors[logicalSector].data = sectorData
|
||||||
makeSectorData(lastReadAddress.track, physicalSector)
|
makeSectorData(lastReadAddress.track, physicalSector)
|
||||||
|
|
||||||
ResetSectorWriteState()
|
resetsectorWriteState()
|
||||||
imageIsDirty = true
|
imageIsDirty = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user