G64.java

/**
 * G64 disk image.
 *
 * @author Ken Händel
 */
package libsidplay.components.c1541;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Ken Händel
 */
public class G64 extends GCRDiskImage {
    @Override public Format getFormat() { return Format.G64; }

    /**
     * Expected G64 image header.
     */
    static final String IMAGE_HEADER = "GCR-1541";
    /** Unsigned 16-bit payload length in a G64 track record. */
    static final int MAX_TRACK_LENGTH = 0xffff;
    /**
     * File offset of the half-track offset table.
     */
    private static final int BEGIN_TRACK_DATA_OFFSETS = 12;
    /**
     * Declared half-track count and maximum payload length, not fixed slot sizes.
     */
    private int halfTracks, maximumTrackSize;
    /**
     * Unsigned file offsets and the last successfully stored lengths.
     */
    private long[] trackOffsets, speedZoneOffsets;
    private int[] storedLengths;
    /**
     * Speed zone (0..3) for every byte of each emulated full track.
     */
    private final int[] speedZoneMap = new int[GCR.MAX_GCR_TRACKS * GCR.NUM_MAX_BYTES_TRACK];
    /** Recorded odd tracks have separate buffers; full tracks and the second-side bank keep their layout. */
    private byte[][] halfTrackData = new byte[MAX_TRACKS_1541][];
    /** Cumulative per-byte cell weights for nonuniform full- and half-track speed maps. */
    private int[][] trackTimings = new int[MAX_TRACKS_1541 * 2][];
    /** Live densities and last published packed maps are separate from the file's offset descriptors. */
    private byte[][] recordedSpeeds = new byte[MAX_TRACKS_1541 * 2][];
    private byte[][] storedSpeedMaps = new byte[MAX_TRACKS_1541 * 2][];
    private final boolean[] pendingWrites = new boolean[MAX_TRACKS_1541 * 2];
    private final IOException[] recordingErrors = new IOException[MAX_TRACKS_1541 * 2];
    private int selectedSlot = -1;

    public G64(final GCR gcr, final String fileName, final RandomAccessFile fd, final boolean readOnly) {
        super(gcr, fileName, fd, readOnly);
    }

    /**
     * Validate and stage all declared records before replacing any GCR surface.
     *
     * @throws IOException invalid header, offsets, lengths or truncated input
     */
    @Override
    protected void attach() throws IOException {
        byte[] header = new byte[IMAGE_HEADER.length()];
        fd.readFully(header);
        if (!new String(header, StandardCharsets.ISO_8859_1).equals(IMAGE_HEADER)) {
            throw new IOException("GCR image is not GCR-1541 format.");
        }
        int version = fd.readUnsignedByte();
        if (version != 0) {
            throw new IOException(String.format("Unknown GCR image version %d.", version));
        }
        halfTracks = fd.readUnsignedByte();
        if (halfTracks < 1 || halfTracks > MAX_TRACKS_1541 * 2) {
            throw new IOException("Invalid G64 half-track count: " + halfTracks);
        }
        tracks = (halfTracks + 1) / 2;
        maximumTrackSize = readLength();
        // The format's allocation maximum is not the emulated surface size.
        // Validate each actual payload separately before copying it to GCR.
        if (maximumTrackSize < 1) {
            throw new IOException("Unsupported G64 maximum track size: " + maximumTrackSize);
        }
        trackOffsets = new long[MAX_TRACKS_1541 * 2];
        speedZoneOffsets = new long[MAX_TRACKS_1541 * 2];
        storedLengths = new int[MAX_TRACKS_1541 * 2];
        for (int i = 0; i < halfTracks; i++) {
            trackOffsets[i] = readOffset();
        }
        for (int i = 0; i < halfTracks; i++) {
            speedZoneOffsets[i] = readOffset();
        }

        long fileLength = fd.length();
        byte[][] trackBytes = new byte[halfTracks][], speedBytes = new byte[halfTracks][];
        for (int i = 0; i < halfTracks; i++) {
            if (trackOffsets[i] == 0) {
                continue;
            }
            requireExtent(trackOffsets[i], 2, fileLength);
            fd.seek(trackOffsets[i]);
            int length = readLength();
            if (length < 1 || length > maximumTrackSize) {
                throw new IOException("Invalid G64 track length: " + length);
            }
            requireExtent(trackOffsets[i], 2L + length, fileLength);
            storedLengths[i] = length;
            trackBytes[i] = new byte[length];
            fd.readFully(trackBytes[i]);
            if (speedZoneOffsets[i] > 3) {
                int speedLength = (length + 3) / 4;
                requireExtent(speedZoneOffsets[i], speedLength, fileLength);
                speedBytes[i] = new byte[speedLength];
                fd.seek(speedZoneOffsets[i]);
                fd.readFully(speedBytes[i]);
            }
        }

        int[][] timings = new int[MAX_TRACKS_1541 * 2][];
        byte[][] speeds = new byte[MAX_TRACKS_1541 * 2][];
        for (int slot = 0; slot < halfTracks; slot++) {
            if (trackBytes[slot] == null) {
                continue;
            }
            speeds[slot] = new byte[storedLengths[slot]];
            for (int i = 0; i < speeds[slot].length; i++) {
                speeds[slot][i] = (byte) (speedBytes[slot] == null ? speedZoneOffsets[slot]
                        : (speedBytes[slot][i / 4] >>> (6 - (i % 4) * 2)) & 3);
            }
            timings[slot] = trackTiming(speeds[slot]);
        }
        byte[][] halfSurfaces = new byte[MAX_TRACKS_1541][];
        for (int slot = 1; slot < halfTracks; slot += 2) {
            halfSurfaces[slot / 2] = trackBytes[slot];
        }
        for (int track = 1; track <= MAX_TRACKS_1541; track++) {
            int slot = (track - 1) * 2, start = (track - 1) * GCR.NUM_MAX_BYTES_TRACK;
            gcr.setTrackData(start, GCR.NUM_MAX_BYTES_TRACK, (byte) 0);
            trackSize[track - 1] = Math.min(RAW_TRACK_SIZE[SPEED_MAP_1541[track - 1]], maximumTrackSize);
            int speed = slot < halfTracks && speedZoneOffsets[slot] <= 3
                    ? (int) speedZoneOffsets[slot] : SPEED_MAP_1541[track - 1];
            Arrays.fill(speedZoneMap, start, start + GCR.NUM_MAX_BYTES_TRACK, speed);
            if (slot >= halfTracks || trackBytes[slot] == null) {
                continue;
            }
            int length = storedLengths[slot];
            trackSize[track - 1] = length;
            gcr.setTrackData(trackBytes[slot], start, length);
            if (speedBytes[slot] != null) {
                for (int i = 0; i < Math.min(length, GCR.NUM_MAX_BYTES_TRACK); i++) {
                    speedZoneMap[start + i] = (speedBytes[slot][i / 4] >>> (6 - (i % 4) * 2)) & 3;
                }
            }
        }
        halfTrackData = halfSurfaces;
        trackTimings = timings;
        recordedSpeeds = speeds;
        storedSpeedMaps = Arrays.copyOf(speedBytes, MAX_TRACKS_1541 * 2);
    }

    /** The four recorded cell periods are proportional to 16, 15, 14 and 13, independently of decoder density. */
    private static int[] trackTiming(byte[] speeds) {
        int[] timing = new int[speeds.length + 1];
        int first = speeds[0];
        boolean variable = false;
        for (int i = 0; i < speeds.length; i++) {
            int zone = speeds[i];
            timing[i + 1] = timing[i] + 8 * (16 - zone);
            variable |= zone != first;
        }
        // A constant map cancels out of the normalized revolution, retaining the uniform path exactly.
        return variable ? timing : null;
    }

    private byte[] recordedHalfTrack(int halfTrack, int side) {
        return side == 0 && (halfTrack & 1) != 0 ? halfTrackData[halfTrack / 2 - 1] : null;
    }

    @Override
    int getHalfTrackSize(int halfTrack, int side) {
        byte[] data = recordedHalfTrack(halfTrack, side);
        return data != null ? data.length : super.getHalfTrackSize(halfTrack, side);
    }

    @Override
    void selectHalfTrack(int halfTrack, int side, int oldSize, int newSize) {
        byte[] data = recordedHalfTrack(halfTrack, side);
        if (data != null) {
            gcr.setHalfTrackData(data, oldSize, newSize);
        } else {
            super.selectHalfTrack(halfTrack, side, oldSize, newSize);
        }
        selectedSlot = side == 0 ? halfTrack - 2 : -1;
        if (selectedSlot >= 0) {
            int slot = selectedSlot;
            if ((slot & 1) != 0 && data == null) {
                int target = slot;
                gcr.setWritePreparation(() -> createHalfTrack(target));
                slot--; // An absent odd capture uses the lower full track's data and timing until written.
            }
            gcr.setTrackTiming(trackTimings[slot]);
        }
    }

    @Override
    GCR.Recording beginGCRRecording(int halfTrack, int side, long phase) {
        if (side != 0 || isReadOnly()) {
            return null;
        }
        int slot = halfTrack - 2;
        byte[] half = recordedHalfTrack(halfTrack, side);
        int length = getHalfTrackSize(halfTrack, side);
        byte[] bytes = half == null ? gcr.getTrackData(slot / 2 * GCR.NUM_MAX_BYTES_TRACK, length) : half.clone();
        int source = (slot & 1) != 0 && half == null ? slot - 1 : slot;
        return new G64Recording(this, slot, bytes, surfaceSpeeds(source, length), phase);
    }

    void recordingFinished(int slot, byte[] bytes, byte[] speeds) {
        int oldSize = getHalfTrackSize(slot + 2, 0);
        if ((slot & 1) == 0) {
            gcr.setTrackData(slot / 2 * GCR.NUM_MAX_BYTES_TRACK, GCR.NUM_MAX_BYTES_TRACK, (byte) 0);
            gcr.setTrackData(bytes, slot / 2 * GCR.NUM_MAX_BYTES_TRACK, bytes.length);
            trackSize[slot / 2] = bytes.length;
            for (int i = 0; i < Math.min(bytes.length, GCR.NUM_MAX_BYTES_TRACK); i++) {
                speedZoneMap[slot / 2 * GCR.NUM_MAX_BYTES_TRACK + i] = speeds[i];
            }
        } else {
            halfTrackData[slot / 2] = bytes;
        }
        recordedSpeeds[slot] = speeds;
        trackTimings[slot] = trackTiming(speeds);
        pendingWrites[slot] = true;
        recordingErrors[slot] = null;
        tracks = Math.max(tracks, slot / 2 + 1);
        if (selectedSlot == slot) {
            selectHalfTrack(slot + 2, 0, oldSize, bytes.length);
        }
    }

    void recordingFailed(int slot, String message) {
        recordingErrors[slot] = new IOException(message);
        pendingWrites[slot] = true;
    }

    @Override
    boolean hasPendingGCRWrites() {
        for (boolean pending : pendingWrites) {
            if (pending) return true;
        }
        return false;
    }

    @Override
    void flushPendingGCRWrites() throws IOException {
        for (int slot = 0; slot < pendingWrites.length; slot++) {
            if (pendingWrites[slot] && !writeSlot(slot)) {
                throw new IOException("G64 track extension was declined");
            }
        }
    }

    private void createHalfTrack(int slot) {
        if (halfTrackData[slot / 2] == null) {
            int length = trackSize[slot / 2];
            halfTrackData[slot / 2] = gcr.getTrackData(slot / 2 * GCR.NUM_MAX_BYTES_TRACK, length);
            recordedSpeeds[slot] = surfaceSpeeds(slot - 1, length);
            trackTimings[slot] = trackTiming(recordedSpeeds[slot]);
            pendingWrites[slot] = true;
            tracks = Math.max(tracks, slot / 2 + 1);
            gcr.setHalfTrackData(halfTrackData[slot / 2], length, length);
            gcr.setTrackTiming(trackTimings[slot]);
        }
    }

    private byte[] surfaceSpeeds(int slot, int length) {
        byte[] previous = recordedSpeeds[slot];
        byte[] result = previous == null ? new byte[length] : Arrays.copyOf(previous, length);
        int from = previous == null ? 0 : Math.min(previous.length, length);
        int speed = previous == null ? speedZoneMap[slot / 2 * GCR.NUM_MAX_BYTES_TRACK] : previous[previous.length - 1];
        Arrays.fill(result, from, length, (byte) speed);
        return result;
    }

    @Override
    void gcrHalfTrackWriteback(int halfTrack, int side) throws IOException {
        int slot = halfTrack - 2;
        if (side != 0 || slot < 0 || slot >= MAX_TRACKS_1541 * 2) {
            throw new IOException("Invalid G64 recording position: " + halfTrack + "/" + side);
        }
        if (isReadOnly()) {
            throw new IOException("Disk is write protected");
        }
        if ((slot & 1) != 0) {
            createHalfTrack(slot);
        }
        if (!writeSlot(slot)) {
            throw new IOException("G64 track extension was declined");
        }
    }

    @Override
    public void gcrDataWriteback(final int track) throws IOException {
        if (track < 1 || track > tracks) {
            throw new IOException("Invalid G64 track: " + track);
        }
        writeSlot((track - 1) * 2);
    }

    /** Publish data, maps and any enlarged table together, retaining the old records for rollback and retry. */
    private boolean writeSlot(int slot) throws IOException {
        if (isReadOnly()) {
            throw new IOException("Disk is write protected");
        }
        if (recordingErrors[slot] != null) {
            throw recordingErrors[slot];
        }
        int length = (slot & 1) == 0 ? trackSize[slot / 2] : halfTrackData[slot / 2].length;
        if (length < 1 || length > MAX_TRACK_LENGTH
                || (slot & 1) == 0 && length > gcr.getTrackCapacity(slot / 2 * GCR.NUM_MAX_BYTES_TRACK)) {
            throw new IOException("Invalid G64 track length: " + length);
        }
        byte[] data = (slot & 1) == 0 ? gcr.getTrackData(slot / 2 * GCR.NUM_MAX_BYTES_TRACK, length)
                : halfTrackData[slot / 2];
        long previous = trackOffsets[slot], fileLength = fd.length();
        if (previous == 0 && extendImageListener != null) {
            int recordedTracks = MIN_TRACKS_1541;
            for (int i = 0; i < halfTracks; i++) {
                if (trackOffsets[i] != 0) {
                    recordedTracks = Math.max(recordedTracks, i / 2 + 1);
                }
            }
            if (slot / 2 + 1 > recordedTracks && !extendImageListener.isAllowed()) {
                return false;
            }
        }
        if (previous != 0) {
            requireExtent(previous, 2L + storedLengths[slot], fileLength);
        }
        byte[] speeds = surfaceSpeeds(slot, length), oldMap = storedSpeedMaps[slot];
        byte[] packed = packSpeeds(speeds, oldMap);
        boolean sameMap = speedZoneOffsets[slot] > 3 && oldMap != null && length <= storedLengths[slot]
                && packed.length <= oldMap.length
                && Arrays.equals(packed, 0, packed.length, oldMap, 0, packed.length);
        boolean uniform = true;
        for (byte speed : speeds) {
            uniform &= speed == speeds[0];
        }
        boolean appendMap = !sameMap && !uniform;
        int count = Math.max(halfTracks, slot + 1), maximum = Math.max(maximumTrackSize, length);
        int tableEnd = BEGIN_TRACK_DATA_OFFSETS + count * 8;
        long[] offsets = trackOffsets.clone(), speedOffsets = speedZoneOffsets.clone();
        List<Write> appended = new ArrayList<>();
        long cursor = Math.max(fileLength, tableEnd);
        for (int i = 0; i < halfTracks; i++) {
            if (trackOffsets[i] == 0) {
                continue;
            }
            if (i != slot && trackOffsets[i] < tableEnd) {
                byte[] record = readAt(trackOffsets[i], storedLengths[i] + 2);
                offsets[i] = cursor;
                appended.add(new Write(cursor, record));
                cursor += record.length;
            }
            if (speedZoneOffsets[i] > 3 && speedZoneOffsets[i] < tableEnd && (i != slot || sameMap)) {
                byte[] map = readAt(speedZoneOffsets[i], (storedLengths[i] + 3) / 4);
                speedOffsets[i] = cursor;
                appended.add(new Write(cursor, map));
                cursor += map.length;
            }
        }
        if (appendMap) {
            speedOffsets[slot] = cursor;
            appended.add(new Write(cursor, packed));
            cursor += packed.length;
        } else if (!sameMap) {
            speedOffsets[slot] = speeds[0];
        }
        byte[] record = new byte[length + 2];
        record[0] = (byte) length;
        record[1] = (byte) (length >>> 8);
        System.arraycopy(data, 0, record, 2, length);
        boolean relocate = previous == 0 || length > storedLengths[slot] || previous < tableEnd
                || sharedExtent(slot, previous, record.length);
        byte[] oldRecord = relocate ? null : readAt(previous, record.length);
        if (relocate) {
            offsets[slot] = cursor;
            appended.add(new Write(cursor, record));
        }
        for (Write write : appended) {
            if (write.offset > 0xffffffffL) {
                throw new IOException("G64 offset exceeds its 32-bit field");
            }
        }
        byte[] header = header(count, maximum, offsets, speedOffsets);
        byte[] oldHeader = readAt(0, (int) Math.min(fileLength, tableEnd));
        boolean publish = !Arrays.equals(header, oldHeader), recordAttempted = false, headerAttempted = false;
        try {
            for (Write write : appended) {
                writeAt(write.offset, write.bytes);
            }
            if (!relocate) {
                recordAttempted = true;
                writeAt(previous, record);
            }
            if (publish) {
                headerAttempted = true;
                writeAt(0, header);
            }
        } catch (IOException failure) {
            try {
                if (headerAttempted) {
                    writeAt(0, oldHeader);
                }
                if (recordAttempted) {
                    writeAt(previous, oldRecord);
                }
                fd.setLength(fileLength);
            } catch (IOException rollback) {
                failure.addSuppressed(rollback);
            }
            throw failure;
        }
        trackOffsets = offsets;
        speedZoneOffsets = speedOffsets;
        storedLengths[slot] = length;
        storedSpeedMaps[slot] = sameMap ? oldMap : appendMap ? packed : null;
        recordedSpeeds[slot] = speeds;
        trackTimings[slot] = trackTiming(speeds);
        halfTracks = count;
        maximumTrackSize = maximum;
        tracks = Math.max(tracks, slot / 2 + 1);
        pendingWrites[slot] = false;
        if ((slot & 1) == 0) {
            for (int i = 0; i < Math.min(length, GCR.NUM_MAX_BYTES_TRACK); i++) {
                speedZoneMap[slot / 2 * GCR.NUM_MAX_BYTES_TRACK + i] = speeds[i];
            }
        }
        if (selectedSlot == slot) {
            gcr.setTrackTiming(trackTimings[slot]);
        }
        return true;
    }

    private static byte[] packSpeeds(byte[] speeds, byte[] previous) {
        byte[] packed = new byte[(speeds.length + 3) / 4];
        int tail = packed.length - 1, unused = (4 - speeds.length % 4) % 4 * 2;
        if (previous != null && tail < previous.length) {
            packed[tail] = (byte) (previous[tail] & ((1 << unused) - 1));
        }
        for (int i = 0; i < speeds.length; i++) {
            packed[i / 4] |= (byte) (speeds[i] << (6 - (i % 4) * 2));
        }
        return packed;
    }

    private static byte[] header(int count, int maximum, long[] offsets, long[] speeds) {
        byte[] bytes = new byte[BEGIN_TRACK_DATA_OFFSETS + count * 8];
        System.arraycopy(IMAGE_HEADER.getBytes(StandardCharsets.ISO_8859_1), 0, bytes, 0, IMAGE_HEADER.length());
        bytes[9] = (byte) count;
        bytes[10] = (byte) maximum;
        bytes[11] = (byte) (maximum >>> 8);
        for (int slot = 0; slot < count; slot++) {
            for (int i = 0; i < 4; i++) {
                bytes[BEGIN_TRACK_DATA_OFFSETS + slot * 4 + i] = (byte) (offsets[slot] >>> (i * 8));
                bytes[BEGIN_TRACK_DATA_OFFSETS + count * 4 + slot * 4 + i] = (byte) (speeds[slot] >>> (i * 8));
            }
        }
        return bytes;
    }

    private byte[] readAt(long offset, int length) throws IOException {
        byte[] bytes = new byte[length];
        fd.seek(offset);
        fd.readFully(bytes);
        return bytes;
    }

    private void writeAt(long offset, byte[] bytes) throws IOException {
        fd.seek(offset);
        fd.write(bytes);
    }

    private record Write(long offset, byte[] bytes) { }

    private boolean sharedExtent(int slot, long start, long length) {
        for (int i = 0; i < halfTracks; i++) {
            if (trackOffsets[i] == 0) {
                continue;
            }
            if (i != slot && overlaps(start, length, trackOffsets[i], storedLengths[i] + 2L)) {
                return true;
            }
            if (speedZoneOffsets[i] > 3 && overlaps(start, length, speedZoneOffsets[i], (storedLengths[i] + 3L) / 4)) {
                return true;
            }
        }
        return false;
    }

    private static boolean overlaps(long start, long length, long other, long otherLength) {
        return start < other + otherLength && other < start + length;
    }

    private void requireExtent(long offset, long length, long fileLength) throws IOException {
        long tableEnd = BEGIN_TRACK_DATA_OFFSETS + halfTracks * 8L;
        if (offset < tableEnd || length > fileLength || offset > fileLength - length) {
            throw new IOException("G64 record outside image data: offset=" + offset + ", length=" + length);
        }
    }

    private int readLength() throws IOException {
        return fd.readUnsignedByte() | fd.readUnsignedByte() << 8;
    }

    private long readOffset() throws IOException {
        return Integer.toUnsignedLong(Integer.reverseBytes(fd.readInt()));
    }
}