Rex.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 12, Rex Utility: one 8 KiB ROM. IO2 reads select ROM on ($DFC0-$DFFF)
 * or off ($DF00-$DFBF), without driving the data bus. Writes are passive.
 * Reference: VICE vice/src/c64/cart/rexutility.c.
 */
public class Rex extends Cartridge {
    protected final byte[] romL = new byte[0x2000];
    private boolean romEnabled;
    private boolean detached;

    public Rex(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 Rex Utility CHIP header");
            if (header[0] != 'C' || header[1] != 'H' || header[2] != 'I' || header[3] != 'P') {
                throw new IOException("Invalid Rex Utility CHIP signature");
            }
            if (++records > 1) throw new IOException("Too many Rex Utility 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 cannot change the physical $8000 decoder.
            if (length < header.length + (long) size || (type != 0 && type != 2)
                    || bank != 0 || size != romL.length) {
                throw new IOException("Invalid Rex Utility CHIP record");
            }
            try {
                input.readFully(romL);
            } catch (EOFException e) {
                throw new EOFException("Truncated Rex Utility CHIP payload");
            }
            skipFully(input, length - header.length - size, "Truncated Rex Utility CHIP padding");
        }
        if (records != 1) throw new IOException("Rex Utility 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 (!detached && romEnabled != enabled) {
            romEnabled = enabled;
            pla.setGameExrom(true, !enabled);
        }
    }

    private final Bank romlBank = new Bank() {
        @Override public byte read(int address) { return romL[address & 0x1fff]; }
        @Override public void write(int address, byte value) { }
    };

    private final Bank io2Bank = new Bank() {
        @Override public byte read(int address) {
            enableRom((address & 0xff) >= 0xc0);
            return pla.getDisconnectedBusBank().read(address);
        }
        @Override public void write(int address, byte value) { }
    };

    @Override public void reset() {
        super.reset();
        detached = false;
        romEnabled = true;
        // PLA reset may have reset the pins even when ROM was already enabled.
        pla.setGameExrom(true, false);
    }

    @Override public void onDetach() {
        detached = true;
        romEnabled = false;
        pla.setGameExrom(true, true);
    }

    @Override public Bank getRoml() { return romlBank; }
    @Override public Bank getIO2() { return io2Bank; }
    @Override public boolean isIO2ReadDriven(int address) { return false; }
    @Override public String toString() { return "Rex Utility"; }
}