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

/**
 * Super Games (CRT type 8): four 16 KiB ROM banks at $8000-$BFFF.
 * IO2 writes select bank bits 0-1, disable ROM with bit 2, and lock the
 * register until reset with bit 3. Bits 4-7 and the IO2 address are ignored.
 * Reference: https://github.com/VICE-Team/svn-mirror/blob/main/vice/src/c64/cart/supergames.c
 */
public class SuperGames extends Cartridge {
    private final byte[][] romBanks = new byte[4][0x4000];
    private int currentBank;
    private boolean locked;

    public SuperGames(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 loaded = false;
        int headerLength;
        while ((headerLength = readChipHeader(input, header)) != 0) {
            if (headerLength != header.length) {
                throw new EOFException("Truncated Super Games CHIP header");
            }
            if (!"CHIP".equals(new String(header, 0, 4, US_ASCII))) {
                throw new IOException("Invalid Super Games 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 || size != 0x4000) {
                throw new IOException("Invalid Super Games CHIP record");
            }
            try {
                input.readFully(romBanks[bank]);
            } catch (EOFException e) {
                throw new EOFException("Truncated Super Games CHIP payload");
            }
            skipFully(input, packetLength - header.length - size, "Truncated Super Games CHIP padding");
            loaded = true;
        }
        if (!loaded) throw new IOException("Super Games CRT contains no CHIP records");
    }

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

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

        @Override
        public void write(int address, byte value) {
            if (locked) return;
            currentBank = value & 3;
            boolean disabled = (value & 4) != 0;
            locked = (value & 8) != 0;
            pla.setGameExrom(disabled, 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. Mapped CPU writes reach RAM through the PLA.
        }
    };

    private final Bank romhBank = new Bank() {
        @Override
        public byte read(int address) {
            return romBanks[currentBank][0x2000 | address & 0x1fff];
        }

        @Override
        public void write(int address, byte value) {
            // Read-only ROM. Mapped CPU writes reach RAM through the PLA.
        }
    };

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

    @Override
    public Bank getRomh() { return romhBank; }

    @Override
    public Bank getIO2() { return io2Bank; }

    @Override
    public boolean isIO2ReadDriven(int address) { return false; }

    @Override
    public void reset() {
        super.reset();
        locked = false;
        io2Bank.write(0xdf00, (byte) 0);
    }
}