RGCD.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;

/**
 * Standard RGCD (CRT type 57, subtype 0): eight 8 KiB ROM banks at $8000-$9FFF.
 * IO1 writes select bank bits 0-2; bit 3 disables ROM and banking until reset.
 * Bits 4-7 and the IO1 address are ignored. Hucky uses a different bank order.
 * Reference: https://github.com/VICE-Team/svn-mirror/blob/main/vice/src/c64/cart/rgcd.c
 */
public class RGCD extends Cartridge {
    private final byte[][] romBanks = new byte[8][0x2000];
    private int currentBank;
    private boolean disabled;

    public RGCD(DataInputStream input, PLA pla) throws IOException {
        super(pla);
        for (byte[] bank : romBanks) Arrays.fill(bank, (byte) 0xff);
        load(input);
    }

    private void load(DataInputStream input) throws IOException {
        byte[] header = new byte[16];
        boolean hasBankSeven = false;
        int headerLength;
        while ((headerLength = readChipHeader(input, header)) != 0) {
            if (headerLength != header.length) throw new EOFException("Truncated RGCD CHIP header");
            if (!"CHIP".equals(new String(header, 0, 4, US_ASCII))) {
                throw new IOException("Invalid RGCD CHIP signature");
            }
            long packetLength = (long) u16(header, 4) << 16 | u16(header, 6);
            int type = u16(header, 8);
            int bank = u16(header, 10);
            int address = u16(header, 12);
            int size = u16(header, 14);
            if (packetLength < header.length + size || (type != 0 && type != 2)
                    || bank >= romBanks.length || (address != 0x8000 && address != 0xa000) || size != 0x2000) {
                throw new IOException("Invalid RGCD CHIP record");
            }
            try {
                input.readFully(romBanks[bank]);
            } catch (EOFException e) {
                throw new EOFException("Truncated RGCD CHIP payload");
            }
            skipFully(input, packetLength - header.length - size, "Truncated RGCD CHIP padding");
            hasBankSeven |= bank == 7;
        }
        // Standard RGCD has a fixed 64 KiB capacity; smaller EPROMs are a Hucky feature.
        if (!hasBankSeven) throw new IOException("RGCD CRT requires bank 7 (64 KiB capacity)");
    }

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

    private final Bank io1Bank = new Bank() {
        @Override public byte read(int address) {
            return pla.getDisconnectedBusBank().read(address);
        }

        @Override public void write(int address, byte value) {
            if (disabled) return;
            disabled = (value & 8) != 0;
            if (!disabled) currentBank = value & 7;
            pla.setGameExrom(true, disabled);
        }
    };

    private final Bank romlBank = new Bank() {
        @Override public byte read(int address) { return romBanks[currentBank][address & 0x1fff]; }
        @Override public void write(int address, byte value) {
            // Read-only ROM. Ordinary mapped CPU writes reach RAM through the PLA.
        }
    };

    @Override public Bank getRoml() { return romlBank; }
    @Override public Bank getIO1() { return io1Bank; }

    @Override public void reset() {
        super.reset();
        disabled = false;
        io1Bank.write(0xde00, (byte) 0);
    }
}