2016-11-04 00:49:49 +00:00
|
|
|
// Copyright © 2016 Zellyn Hunter <zellyn@gmail.com>
|
|
|
|
|
|
|
|
// marshal.go contains helpers for marshaling sector structs to/from
|
|
|
|
// disk.
|
|
|
|
|
|
|
|
package disk
|
|
|
|
|
|
|
|
// SectorSource is the interface for types that can marshal to sectors.
|
|
|
|
type SectorSource interface {
|
|
|
|
// ToSector marshals the sector struct to exactly 256 bytes.
|
2016-11-13 03:25:42 +00:00
|
|
|
ToSector() ([]byte, error)
|
2016-11-04 00:49:49 +00:00
|
|
|
// GetTrack returns the track that a sector struct was loaded from.
|
|
|
|
GetTrack() byte
|
|
|
|
// GetSector returns the sector that a sector struct was loaded from.
|
|
|
|
GetSector() byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// SectorSink is the interface for types that can unmarshal from sectors.
|
|
|
|
type SectorSink interface {
|
|
|
|
// FromSector unmarshals the sector struct from bytes. Input is
|
|
|
|
// expected to be exactly 256 bytes.
|
2016-11-13 03:25:42 +00:00
|
|
|
FromSector(data []byte) error
|
2016-11-04 00:49:49 +00:00
|
|
|
// SetTrack sets the track that a sector struct was loaded from.
|
|
|
|
SetTrack(track byte)
|
|
|
|
// SetSector sets the sector that a sector struct was loaded from.
|
|
|
|
SetSector(sector byte)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalLogicalSector reads a sector from a SectorDisk, and
|
|
|
|
// unmarshals it into a SectorSink, setting its track and sector.
|
2016-11-13 03:15:45 +00:00
|
|
|
func UnmarshalLogicalSector(d LogicalSectorDisk, ss SectorSink, track, sector byte) error {
|
2016-11-04 00:49:49 +00:00
|
|
|
bytes, err := d.ReadLogicalSector(track, sector)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-13 03:25:42 +00:00
|
|
|
if err := ss.FromSector(bytes); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-04 00:49:49 +00:00
|
|
|
ss.SetTrack(track)
|
|
|
|
ss.SetSector(sector)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalLogicalSector marshals a SectorSource to its sector on a
|
|
|
|
// SectorDisk.
|
2016-11-13 03:15:45 +00:00
|
|
|
func MarshalLogicalSector(d LogicalSectorDisk, ss SectorSource) error {
|
2016-11-04 00:49:49 +00:00
|
|
|
track := ss.GetTrack()
|
|
|
|
sector := ss.GetSector()
|
2016-11-13 03:25:42 +00:00
|
|
|
bytes, err := ss.ToSector()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-04 00:49:49 +00:00
|
|
|
return d.WriteLogicalSector(track, sector, bytes)
|
|
|
|
}
|