NIBBitCycle.java
package libsidplay.components.c1541;
import java.util.Arrays;
/** Recover no-SYNC captures whose byte-ready phase does not repeat on a byte boundary. */
final class NIBBitCycle {
private static final int[] VORPAL_NIBBLES = new int[32];
private static final int VORPAL_PACKET_BITS = 17 + 1290 + 9;
static {
Arrays.fill(VORPAL_NIBBLES, -1);
int[] symbols = {9, 10, 11, 13, 14, 15, 18, 19, 21, 22, 23, 25, 26, 27, 29, 30};
for (int i = 0; i < symbols.length; i++) VORPAL_NIBBLES[symbols[i]] = i;
VORPAL_NIBBLES[12] = 5;
VORPAL_NIBBLES[20] = 10;
VORPAL_NIBBLES[5] = 14;
VORPAL_NIBBLES[6] = 15;
}
static byte[] extract(byte[] source, int minimumBytes) {
// A first byte sampled before the shifter settles is not overlap evidence.
// Every subsequent overlapping bit must agree; do not repair differing data.
byte[][] shifted = new byte[8][source.length];
shifted[0] = source;
for (int phase = 1; phase < 8; phase++)
for (int i = 0; i < source.length; i++)
shifted[phase][i] = (byte) ((source[i] & 255) << phase
| (i + 1 < source.length ? (source[i + 1] & 255) >>> (8 - phase) : 0));
boolean[] seen = new boolean[256];
int[] distinct = new int[source.length];
for (int i = 1; i < source.length; i++) {
int value = source[i] & 255;
distinct[i] = distinct[i - 1] + (seen[value] ? 0 : 1);
seen[value] = true;
}
int period = 0;
for (int bytes = minimumBytes; bytes < source.length - 7; bytes++) {
for (int phase = 0; phase < 8; phase++) {
int whole = source.length - bytes - 1 - (phase == 0 ? 0 : 1);
if (whole < 7 || distinct[whole] < 7) continue;
int i = 1;
while (i <= whole && source[i] == shifted[phase][bytes + i]) i++;
if (i <= whole) continue;
int mask = (255 << phase) & 255;
if (phase != 0 && ((source[i] ^ shifted[phase][bytes + i]) & mask) != 0) continue;
if (period != 0) return null; // Ambiguous evidence does not establish a period.
period = bytes * 8 + phase;
}
}
if (period == 0) return null;
if ((period & 7) == 0) return Arrays.copyOfRange(source, 1, 1 + period / 8);
// GCR surfaces have whole bytes. Put the unavoidable 1..7 clock bits
// in a gap before a checksum-verified Vorpal marker. Sector identifiers
// can share bits with the next marker, so that marker alone is not a gap.
int marker = vorpalGap(source, period);
if (marker < 0) return null;
byte[] result = new byte[(period + 7) / 8];
for (int i = 0; i < result.length * 8; i++) {
int bit = i < period ? bit(source, marker + i, period) : (i - period) & 1;
result[i / 8] |= (byte) (bit << (7 - (i & 7)));
}
return result;
}
private static int vorpalGap(byte[] source, int period) {
int window = 0, first = -1, previous = -1;
boolean firstValid = false;
for (int i = 0; i < period + 16; i++) {
window = (window << 1 | bit(source, i, period)) & 65535;
if (i < 16 || window != 0x7faa) continue;
int marker = i - 16;
if (previous < 0) {
first = marker;
firstValid = validVorpalPacket(source, i + 1, period);
} else if (marker - previous >= VORPAL_PACKET_BITS && validVorpalPacket(source, i + 1, period)) {
return marker;
}
// Even a marker whose following checksum is damaged blocks padding
// inside that field. Check the last-to-first interval as well.
previous = marker;
}
return firstValid && first + period - previous >= VORPAL_PACKET_BITS ? first : -1;
}
private static boolean validVorpalPacket(byte[] source, int start, int period) {
int parity = 0;
for (int n = 0; n < 129; n++) {
int high = VORPAL_NIBBLES[code(source, start + n * 10, period)];
int low = VORPAL_NIBBLES[code(source, start + n * 10 + 5, period)];
if (high < 0 || low < 0) return false;
parity ^= high * 16 + low;
}
int sector = 0;
for (int position : new int[]{0, 2, 3, 5, 6, 8})
sector = sector << 1 | bit(source, start + 1290 + position, period);
return parity == 0 && sector < 47;
}
private static int code(byte[] source, int position, int period) {
int code = 0;
for (int i = 0; i < 5; i++) code = code << 1 | bit(source, position + i, period);
return code;
}
private static int bit(byte[] source, int position, int period) {
int index = 8 + position % period;
return (source[index / 8] >>> (7 - (index & 7))) & 1;
}
private NIBBitCycle() { }
}