EpyxFastLoad.java
package libsidplay.components.cart.supported;
import libsidplay.common.Event;
import libsidplay.components.cart.Cartridge;
import libsidplay.components.pla.Bank;
import libsidplay.components.pla.PLA;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import static java.nio.charset.StandardCharsets.US_ASCII;
/**
* CRT 10: Epyx FastLoad. ROML and IO1 reads discharge the capacitor, enabling
* 8 KiB ROM for another 512 cycles. IO1 floats; IO2 always exposes the final
* ROM page. Reference: VICE vice/src/c64/cart/epyxfastload.c.
*/
public class EpyxFastLoad extends Cartridge {
protected final byte[] romL = new byte[0x2000];
private boolean romEnabled;
private boolean detached;
public EpyxFastLoad(final DataInputStream input, final PLA pla) throws IOException {
super(pla);
byte[] header = new byte[16];
int records = 0;
for (;;) {
int count = readChipHeader(input, header);
if (count == 0) break;
if (count != header.length) throw new EOFException("Truncated Epyx FastLoad CHIP header");
if (!"CHIP".equals(new String(header, 0, 4, US_ASCII))) {
throw new IOException("Invalid Epyx FastLoad CHIP signature");
}
if (++records > 1) throw new IOException("Too many Epyx FastLoad CHIP records");
long length = (long) u16(header, 4) << 16 | u16(header, 6);
int type = u16(header, 8), bank = u16(header, 10), size = u16(header, 14);
// Load-address metadata does not change the physical ROM decoder.
if (length < header.length + (long) size || (type != 0 && type != 2) || bank != 0 || size != romL.length) {
throw new IOException("Invalid Epyx FastLoad CHIP record");
}
try {
input.readFully(romL);
} catch (EOFException e) {
throw new EOFException("Truncated Epyx FastLoad CHIP payload");
}
skipFully(input, length - header.length - size, "Truncated Epyx FastLoad CHIP padding");
}
if (records != 1) throw new IOException("Epyx FastLoad requires one CHIP record");
}
private static int u16(byte[] bytes, int offset) {
return (bytes[offset] & 255) << 8 | bytes[offset + 1] & 255;
}
private void enableRom(boolean enabled) {
if (romEnabled != enabled) {
romEnabled = enabled;
pla.setGameExrom(true, !enabled);
}
}
private final Event capacitorCharged = Event.of("Epyx FastLoad capacitor charged", event -> {
if (!detached) enableRom(false);
});
private void dischargeCapacitor() {
if (detached) return;
var scheduler = pla.getCPU().getEventScheduler();
scheduler.cancel(capacitorCharged);
// Preserve the access phase: expiry is exactly 512 whole cycles later.
scheduler.schedule(capacitorCharged, 512);
enableRom(true);
}
private final Bank romlBank = new Bank() {
@Override public byte read(int address) {
dischargeCapacitor();
return romL[address & 0x1fff];
}
@Override public void write(int address, byte value) { }
};
private final Bank io1Bank = new Bank() {
@Override public byte read(int address) {
dischargeCapacitor();
return pla.getDisconnectedBusBank().read(address);
}
@Override public void write(int address, byte value) { }
};
private final Bank io2Bank = new Bank() {
@Override public byte read(int address) { return romL[0x1f00 | address & 0xff]; }
@Override public void write(int address, byte value) { }
};
@Override public void reset() {
super.reset();
detached = false;
// PLA reset also resets its pins, even if our previous state was enabled.
romEnabled = false;
dischargeCapacitor();
}
@Override public void onDetach() {
detached = true;
pla.getCPU().getEventScheduler().cancel(capacitorCharged);
romEnabled = false;
pla.setGameExrom(true, true);
}
@Override public Bank getRoml() { return romlBank; }
@Override public Bank getIO1() { return io1Bank; }
@Override public Bank getIO2() { return io2Bank; }
@Override public String toString() { return "Epyx FastLoad"; }
}