Updating TrackSectorDevice to push the buffer all the way though and to ensure the buffer is a multiple of the sector size.

This commit is contained in:
a2geek committed 2026-11-02 14:05:57 -06:00
1 parent 42a144ffed
commit f696fba51a
6 files changed
+56 -28

No files matched your search

@@ -95,9 +95,9 @@ public class DumpCommand extends ReadOnlyDiskContextCommandOptions {
}
@Override
public DataBuffer readSector(int track, int sector) {
public void readSector(int track, int sector, int offset, DataBuffer data) {
assert track == 0 && sector == 0;
return blockDevice.readBlock(0).slice(0, SECTOR_SIZE);
data.put(offset, blockDevice.readBlock(0).slice(0, SECTOR_SIZE));
}
@Override
@@ -70,12 +70,12 @@ public class BlockToTrackSectorAdapter implements TrackSectorDevice {
return SectorOrder.DOS;
}
/// Read a single sector from the disk into the given `DataBuffer` at the given offset.
@Override
public DataBuffer readSector(int track, int sector) {
public void readSector(int track, int sector, int offset, DataBuffer data) {
int block = strategy.computeBlock(track, sector);
DataBuffer blockData = device.readBlock(block);
int offset = strategy.computeOffset(track, sector);
return blockData.slice(offset, SECTOR_SIZE);
data.put(offset, blockData.slice(strategy.computeOffset(track, sector), SECTOR_SIZE));
}
@Override
@@ -52,7 +52,6 @@ public class OrderedTrackSectorDevice implements TrackSectorDevice {
}
}
@Override
public <T> Optional<T> get(Class<T> iface) {
return Container.get(iface, source);
@@ -74,15 +73,29 @@ public class OrderedTrackSectorDevice implements TrackSectorDevice {
return sectorOrder ;
}
/// Read a single sector from the disk into the given `DataBuffer` at the given offset.
@Override
public DataBuffer readSector(int track, int sector) {
return source.readBytes(calculateOffset(track,sector), SECTOR_SIZE);
public void readSector(int track, int sector, int offset, DataBuffer data) {
data.put(offset, source.readBytes(calculateOffset(track,sector), SECTOR_SIZE));
}
/// Read a range of sectors.
@Override
public DataBuffer readRange(int track, int sector, int totalSectors) {
return source.readBytes(calculateOffset(track,sector), totalSectors*SECTOR_SIZE);
}
@Override
public void writeSector(int track, int sector, DataBuffer data) {
assert(data.limit() == SECTOR_SIZE);
source.writeBytes(calculateOffset(track,sector), data);
source.writeBytes(calculateOffset(track,sector), sizeBuffer(data));
}
/// Write a range of sectors to the disk. Note that the data buffer is
/// modified to be a multiple of sector size.
@Override
public void writeRange(int track, int sector, DataBuffer data) {
source.writeBytes(calculateOffset(track,sector), sizeBuffer(data));
}
public int calculateOffset(int track, int sector) {
@@ -163,10 +163,11 @@ public class SkewedTrackSectorDevice implements TrackSectorDevice {
return sectorOrder;
}
/// Read a single sector from the disk into the given `DataBuffer` at the given offset.
@Override
public DataBuffer readSector(int track, int sector) {
assert(sector >= 0 && sector < sectorSkew.length);
return device.readSector(track, sectorSkew[sector]);
public void readSector(int track, int sector, int offset, DataBuffer data) {
assert sector >= 0 && sector < sectorSkew.length;
device.readSector(track, sectorSkew[sector], offset, data);
}
@Override
@@ -63,14 +63,20 @@ public interface TrackSectorDevice extends Device {
};
}
/// Read a single sector from the disk.
DataBuffer readSector(int track, int sector);
default DataBuffer readSector(int track, int sector) {
DataBuffer data = DataBuffer.create(SECTOR_SIZE);
readSector(track, sector, 0, data);
return data;
}
/// Read a single sector from the disk into the given `DataBuffer` at the given offset.
void readSector(int track, int sector, int offset, DataBuffer data);
/// Read a range of sectors.
default DataBuffer readRange(int track, int sector, int totalSectors) {
DataBuffer rangeData = DataBuffer.create(totalSectors * SECTOR_SIZE);
performRange(track, sector, totalSectors, (t,s,o) -> rangeData.put(o, readSector(t,s)));
performRange(track, sector, totalSectors, (t,s,o) -> readSector(t,s,o,rangeData));
return rangeData;
}
@@ -81,14 +87,9 @@ public interface TrackSectorDevice extends Device {
/// modified to be a multiple of sector size.
default void writeRange(int track, int sector, DataBuffer data) {
int totalSectors = calculateRequiredSectors(data.limit());
DataBuffer newBuffer = sizeBuffer(data);
performRange(track, sector, totalSectors, (t,s,o) -> {
if (data.limit() < totalSectors * SECTOR_SIZE) {
DataBuffer newData = DataBuffer.create(totalSectors * SECTOR_SIZE);
newData.put(0, data);
writeSector(t, s, newData.slice(o, SECTOR_SIZE));
} else {
writeSector(t, s, data.slice(o, SECTOR_SIZE));
}
writeSector(t, s, newBuffer.slice(o, SECTOR_SIZE));
});
}
@@ -108,6 +109,17 @@ public interface TrackSectorDevice extends Device {
}
}
/// Size the given `DataBuffer` to be a multiple of the sector size.
default DataBuffer sizeBuffer(DataBuffer dataBuffer) {
int totalSectors = calculateRequiredSectors(dataBuffer.limit());
if (dataBuffer.limit() < totalSectors * SECTOR_SIZE) {
DataBuffer newDataBuffer = DataBuffer.create(totalSectors * SECTOR_SIZE);
newDataBuffer.put(0, dataBuffer);
return newDataBuffer;
}
return dataBuffer;
}
/// A handy/common function to calculate the number of sectors needed for the given size.
default int calculateRequiredSectors(int size) {
return (size + SECTOR_SIZE - 1) / SECTOR_SIZE;
@@ -252,19 +252,21 @@ public class TrackSectorNibbleDevice implements TrackSectorDevice {
return offset;
}
/// Read a single sector from the disk into the given `DataBuffer` at the given offset.
@Override
public DataBuffer readSector(int track, int sector) {
public void readSector(int track, int sector, int offset, DataBuffer data) {
final DataBuffer trackData = trackReaderWriter.readTrackData(track);
// Locate address field for this track and sector
int offset = findSector(trackData, track, sector);
int sectorOffset = findSector(trackData, track, sector);
// Read data field that immediately follows the address field and the checksum byte
if (offset != -1) {
if (sectorOffset != -1) {
DiskMarker diskMarker = diskMarkers[track % diskMarkers.length];
NibbleDiskCodec dataCodec = diskMarker.codec();
DataBuffer dataField = DataBuffer.create(dataCodec.encodedSize() + 1);
offset = locateField(diskMarker.dataProlog(), trackData, dataField, offset);
if (offset != -1) {
return dataCodec.decode(dataField);
sectorOffset = locateField(diskMarker.dataProlog(), trackData, dataField, sectorOffset);
if (sectorOffset != -1) {
data.put(offset, dataCodec.decode(dataField));
return;
}
}
// We found the sector but couldn't read the sector, fail horribly