Pagefox.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;
import java.util.Arrays;

import static java.nio.charset.StandardCharsets.US_ASCII;

/**
 * CRT 53: Pagefox, with two 32 KiB EPROMs and 32 KiB SRAM.
 * $DE80-$DEFF selects the chip (bits 2-3), its 16 KiB bank (bit 1),
 * and read disable (bit 4). SRAM writes remain active while reads are
 * disabled or hidden by the CPU port. Chip 3 is electrically empty.
 * References: VICE CRT specification and vice/src/c64/cart/pagefox.c.
 */
public class Pagefox extends Cartridge {
    private final byte[][] rom = new byte[4][0x4000];
    private final byte[] ram = new byte[0x8000];
    private int chipSelect, bankSelect;
    private boolean detached;

    public Pagefox(DataInputStream input, PLA pla) throws IOException {
        super(pla);
        for (byte[] bank : rom) Arrays.fill(bank, (byte) 0xff);
        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 Pagefox CHIP header");
            if (!"CHIP".equals(new String(header, 0, 4, US_ASCII))) {
                throw new IOException("Invalid Pagefox CHIP signature");
            }
            if (++records > 4) throw new IOException("Too many Pagefox CHIP records");
            long length = (long) u16(header, 4) << 16 | u16(header, 6);
            int type = u16(header, 8), bank = u16(header, 10), address = u16(header, 12), size = u16(header, 14);
            if (length < header.length + (long) size || (type != 0 && type != 2)
                    || bank >= 4 || address != 0x8000 || size != 0x4000) {
                throw new IOException("Invalid Pagefox CHIP record");
            }
            try {
                input.readFully(rom[bank]);
            } catch (EOFException e) {
                throw new EOFException("Truncated Pagefox CHIP payload");
            }
            skipFully(input, length - header.length - size, "Truncated Pagefox CHIP padding");
        }
        if (records != 4) throw new IOException("Pagefox requires four CHIP records");
    }

    private static int u16(byte[] bytes, int offset) {
        return (bytes[offset] & 255) << 8 | bytes[offset + 1] & 255;
    }

    private final Bank memory = new Bank() {
        @Override public byte read(int address) {
            int offset = address & 0x3fff;
            if (chipSelect == 2) return ram[(bankSelect << 14) | offset];
            if (chipSelect == 3) return pla.getDisconnectedBusBank().read(address);
            return rom[(chipSelect << 1) | bankSelect][offset];
        }
        @Override public void write(int address, byte value) { }
    };

    private final Bank io1 = new Bank() {
        @Override public byte read(int address) { return pla.getDisconnectedBusBank().read(address); }
        @Override public void write(int address, byte value) {
            if (detached || address < 0xde80 || address > 0xdeff) return;
            bankSelect = (value >>> 1) & 1;
            chipSelect = (value >>> 2) & 3;
            // Commit selection before the synchronous mapping rebuild installs write hooks.
            boolean disabled = (value & 0x10) != 0;
            pla.setGameExrom(disabled, disabled);
        }
    };

    @Override public void installBankHooks(Bank[] cpuReadMap, Bank[] cpuWriteMap) {
        if (detached || chipSelect != 2) return;
        for (int page = 8; page < 12; page++) {
            final Bank original = cpuWriteMap[page];
            cpuWriteMap[page] = new Bank() {
                @Override public byte read(int address) { return original.read(address); }
                @Override public void write(int address, byte value) {
                    original.write(address, value);
                    ram[(bankSelect << 14) | (address & 0x3fff)] = value;
                }
            };
        }
    }

    @Override public void reset() {
        super.reset();
        detached = false;
        chipSelect = bankSelect = 0;
        pla.setGameExrom(false, false);
    }

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

    @Override public Bank getRoml() { return memory; }
    @Override public Bank getRomh() { return memory; }
    @Override public Bank getIO1() { return io1; }
    @Override public String toString() { return "Pagefox"; }
}