G64Recording.java
package libsidplay.components.c1541;
import java.util.Arrays;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
/** Collect one write gate at encoder clock times, retaining only the latest physical revolution. */
final class G64Recording implements GCR.Recording {
private static final int REVOLUTION_CLOCKS = 3_200_000;
private static final int MAX_CELLS = GCR.NUM_MAX_BYTES_TRACK * 8 + 16;
private final BiConsumer<byte[], byte[]> completed;
private final Consumer<String> failed;
private final byte[] original, originalSpeeds;
private final long startPhase;
private final int maximumLength;
private long[] clocks = new long[512];
private byte[] cells = new byte[512];
private int first, count;
G64Recording(G64 image, int slot, byte[] original, byte[] speeds, long phase) {
this(original, speeds, phase, G64.MAX_TRACK_LENGTH, (bytes, zones) -> image.recordingFinished(slot, bytes, zones),
message -> image.recordingFailed(slot, message));
}
G64Recording(byte[] original, byte[] speeds, long phase, BiConsumer<byte[], byte[]> completed, Consumer<String> failed) {
this(original, speeds, phase, GCR.NUM_MAX_BYTES_TRACK, completed, failed);
}
private G64Recording(byte[] original, byte[] speeds, long phase, int maximumLength,
BiConsumer<byte[], byte[]> completed, Consumer<String> failed) {
this.maximumLength = maximumLength;
this.completed = completed;
this.failed = failed;
this.original = original;
originalSpeeds = speeds;
startPhase = phase;
}
@Override
public void writeBit(boolean value, int density, long elapsed) {
discardBefore(elapsed - REVOLUTION_CLOCKS);
if (count == clocks.length) {
int capacity = Math.min(MAX_CELLS, clocks.length * 2);
if (capacity == clocks.length) {
throw new IllegalStateException("GCR encoder exceeded the 16 MHz divider's maximum cell rate");
}
long[] times = new long[capacity];
byte[] data = new byte[capacity];
for (int i = 0; i < count; i++) {
int index = (first + i) % clocks.length;
times[i] = clocks[index];
data[i] = cells[index];
}
clocks = times;
cells = data;
first = 0;
}
int index = (first + count++) % clocks.length;
clocks[index] = elapsed;
cells[index] = (byte) ((density << 1) | (value ? 1 : 0));
}
private void discardBefore(long earliest) {
while (count > 0 && clocks[first] <= earliest) {
first = (first + 1) % clocks.length;
count--;
}
}
@Override
public void finish(long elapsed) {
discardBefore(elapsed - REVOLUTION_CLOCKS);
if (count == 0) {
return;
}
int[] events = new int[count + original.length * 8];
int used = 0;
if (elapsed < REVOLUTION_CLOCKS) {
long weight = 0;
for (byte speed : originalSpeeds) weight += (16 - speed) * 8L;
long position = 0;
for (int bit = 0; bit < original.length * 8; bit++) {
int density = originalSpeeds[bit >> 3];
position += 16 - density;
int phase = (int) ((position * REVOLUTION_CLOCKS + weight - 1) / weight);
long distance = phase - startPhase;
if (distance <= 0) distance += REVOLUTION_CLOCKS;
if (distance > elapsed) {
int value = (original[bit >> 3] >>> (7 - (bit & 7))) & 1;
events[used++] = (phase << 3) | (density << 1) | value;
}
}
}
for (int i = 0; i < count; i++) {
int index = (first + i) % clocks.length;
int phase = (int) ((startPhase + clocks[index]) % REVOLUTION_CLOCKS);
if (phase == 0) phase = REVOLUTION_CLOCKS;
events[used++] = (phase << 3) | cells[index];
}
Arrays.sort(events, 0, used);
int length = (used + 7) / 8;
if (length > maximumLength) {
failed.accept("GCR recording exceeds the supported track capacity: " + length);
return;
}
byte[] bytes = new byte[length], speeds = new byte[length];
for (int bit = 0; bit < used; bit++) {
bytes[bit >> 3] |= (byte) ((events[bit] & 1) << (7 - (bit & 7)));
// G64 represents one density per byte. The last cell supplies it at a partial-byte splice.
speeds[bit >> 3] = (byte) ((events[bit] >>> 1) & 3);
}
// A whole-byte G64 record must not create a zero/flux gap at the physical seam.
// Continue the circular data for its at most seven padding cells; keep the last recorded density.
for (int bit = used; bit < bytes.length * 8; bit++) {
bytes[bit >> 3] |= (byte) ((events[(bit - used) % used] & 1) << (7 - (bit & 7)));
}
completed.accept(bytes, speeds);
}
}