Comal80.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 21: black Commodore COMAL-80 (64/96/128 KiB), or grey COMAL-80 (64 KiB).
 * IO1 is write-only. The grey board latches bank bits 0-1, EXROM bit 5 and
 * GAME bit 6; the black board latches bank bits 0-2 and disconnect bit 6.
 * References: VICE CRT specification, vice/src/c64/cart/comal80.c, and
 * Terry Ricketts' original 2.0 black-cartridge schematic.
 */
public class Comal80 extends Cartridge {
    protected int currentRomBank;
    protected final byte[][] romLBanks = new byte[8][0x2000];
    protected final byte[][] romHBanks = new byte[8][0x2000];
    private final boolean grey;
    private boolean detached;

    /** Existing callers retain the default black-board revision. */
    public Comal80(DataInputStream input, PLA pla) throws IOException {
        this(input, pla, false);
    }

    public Comal80(DataInputStream input, PLA pla, boolean grey) throws IOException {
        super(pla);
        this.grey = grey;
        for (byte[] bank : romLBanks) Arrays.fill(bank, (byte) 0xff);
        for (byte[] bank : romHBanks) Arrays.fill(bank, (byte) 0xff);
        byte[] header = new byte[16];
        int records = 0, highestBank = 0;
        for (;;) {
            int count = readChipHeader(input, header);
            if (count == 0) break;
            if (count != header.length) throw new EOFException("Truncated COMAL-80 CHIP header");
            if (!"CHIP".equals(new String(header, 0, 4, US_ASCII))) {
                throw new IOException("Invalid COMAL-80 CHIP signature");
            }
            if (++records > (grey ? 4 : 8)) throw new IOException("Too many COMAL-80 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 >= (grey ? 4 : 8) || address != 0x8000 || size != 0x4000) {
                throw new IOException("Invalid COMAL-80 CHIP record");
            }
            try {
                input.readFully(romLBanks[bank]);
                input.readFully(romHBanks[bank]);
            } catch (EOFException e) {
                throw new EOFException("Truncated COMAL-80 CHIP payload");
            }
            skipFully(input, length - header.length - size, "Truncated COMAL-80 CHIP padding");
            highestBank = Math.max(highestBank, bank);
        }
        if (records != 4 && (grey || records != 6 && records != 8)) {
            throw new IOException(grey ? "Grey COMAL-80 requires four CHIP records" : "COMAL-80 requires 4, 6 or 8 CHIP records");
        }
        if (highestBank >= records) throw new IOException("COMAL-80 bank exceeds ROM capacity");
    }

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

    private final Bank romlBank = new Bank() {
        @Override public byte read(int address) { return romLBanks[currentRomBank][address & 0x1fff]; }
        @Override public void write(int address, byte value) { }
    };
    private final Bank romhBank = new Bank() {
        @Override public byte read(int address) { return romHBanks[currentRomBank][address & 0x1fff]; }
        @Override public void write(int address, byte value) { }
    };
    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 (detached) return;
            currentRomBank = value & (grey ? 3 : 7);
            // These are latched physical pins, so both phases see the same mode.
            pla.setGameExrom((value & 0x40) != 0, (value & (grey ? 0x20 : 0x40)) != 0);
        }
    };

    @Override public void reset() {
        super.reset();
        detached = false;
        currentRomBank = 0;
        pla.setGameExrom(false, false);
    }
    @Override public void onDetach() {
        detached = true;
        pla.setGameExrom(true, true);
    }
    @Override public Bank getRoml() { return romlBank; }
    @Override public Bank getRomh() { return romhBank; }
    @Override public Bank getIO1() { return io1Bank; }
    @Override public String toString() { return grey ? "COMAL-80 (grey)" : "COMAL-80"; }
}