Zaxxon.java
package libsidplay.components.cart.supported;
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;
/**
* CRT 18, Zaxxon / Super Zaxxon: ROML reads select ROMH using address bit 12.
* The 4 KiB lower ROM is mirrored; 8 KiB images retain both lower halves.
* Reference: VICE vice/src/c64/cart/zaxxon.c.
*/
public class Zaxxon extends Cartridge {
protected final byte[] roml = new byte[0x2000];
protected final byte[][] romh = new byte[2][0x2000];
protected byte[] romhActive = romh[0];
private final Bank romlBank = new Bank() {
@Override
public byte read(int address) {
romhActive = romh[(address & 0x1000) >> 12];
return roml[address & 0x1fff];
}
@Override public void write(int address, byte value) { }
};
private final Bank romhBank = new Bank() {
@Override
public byte read(int address) {
return romhActive[address & 0x1fff];
}
@Override public void write(int address, byte value) { }
};
/**
* Load a Zaxxon cartridge.
*
* @param input stream to load from
* @param pla PLA to use
* @throws IOException load error
*/
public Zaxxon(final DataInputStream input, final PLA pla) throws IOException {
super(pla);
byte[] header = new byte[16];
boolean[] seen = new boolean[3];
int records = 0;
for (;;) {
int count = readChipHeader(input, header);
if (count == 0) break;
if (count != header.length) throw new EOFException("Truncated Zaxxon CHIP header");
if (header[0] != 'C' || header[1] != 'H' || header[2] != 'I' || header[3] != 'P') {
throw new IOException("Invalid Zaxxon CHIP signature");
}
if (++records > 3) throw new IOException("Too many Zaxxon CHIP records");
long length = (long) u16(header, 4) << 16 | u16(header, 6);
int type = u16(header, 8), bank = u16(header, 10);
int address = u16(header, 12), size = u16(header, 14);
boolean lower = address == 0x8000 && bank == 0 && (size == 0x1000 || size == 0x2000);
boolean upper = address == 0xa000 && bank < 2 && size == 0x2000;
if (length < header.length + (long) size || (type != 0 && type != 2) || (!lower && !upper)) {
throw new IOException("Invalid Zaxxon CHIP record");
}
int slot = lower ? 0 : bank + 1;
if (seen[slot]) throw new IOException("Duplicate Zaxxon CHIP record");
seen[slot] = true;
try {
input.readFully(lower ? roml : romh[bank], 0, size);
} catch (EOFException e) {
throw new EOFException("Truncated Zaxxon CHIP payload");
}
if (lower && size == 0x1000) System.arraycopy(roml, 0, roml, 0x1000, 0x1000);
skipFully(input, length - header.length - size, "Truncated Zaxxon CHIP padding");
}
if (records != 3) throw new IOException("Zaxxon requires ROML and both ROMH CHIP records");
}
private static int u16(byte[] bytes, int offset) {
return (bytes[offset] & 255) << 8 | bytes[offset + 1] & 255;
}
@Override
public Bank getRoml() {
return romlBank;
}
@Override
public Bank getRomh() {
return romhBank;
}
@Override
public void reset() {
super.reset();
romhActive = romh[0];
pla.setGameExrom(false, false);
}
@Override public void onDetach() { pla.setGameExrom(true, true); }
@Override public String toString() { return "Zaxxon / Super Zaxxon"; }
}