Normal.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 static java.nio.charset.StandardCharsets.US_ASCII;

/** Unbanked CRT type 0: 8K/16K game, Ultimax, or inactive header pin configuration. */
public class Normal extends Cartridge {
    protected byte[] roml, romh;

    private boolean exrom, game;

    private final Bank romlBank = new Bank() {
        @Override
        public byte read(int address) {
            return roml == null ? pla.getDisconnectedBusBank().read(address) : roml[address & 0x1fff];
        }
        @Override
        public void write(int address, byte value) {
            // Ultimax maps CPU writes here; ROM ignores them without writing underlying RAM.
        }
    };

    private final Bank romhBank = new Bank() {
        @Override
        public byte read(int address) {
            return romh == null ? pla.getDisconnectedBusBank().read(address) : romh[address & 0x1fff];
        }
        @Override
        public void write(int address, byte value) {
            // Ultimax maps CPU writes here; ROM ignores them without writing underlying RAM.
        }
    };

    /** Compatibility entry point for CHIP-only streams, whose addresses imply the mode. */
    public Normal(DataInputStream dis, final PLA pla) throws IOException {
        super(pla);
        exrom = load(dis);
        game = !exrom && romh == null;
    }

    /** Complete CRT loading uses the physical GAME/EXROM levels from the file header. */
    public Normal(DataInputStream dis, final PLA pla, boolean game, boolean exrom) throws IOException {
        this(dis, pla);
        this.game = game;
        this.exrom = exrom;
    }

    private boolean load(DataInputStream input) throws IOException {
        byte[][] banks = new byte[2][];
        boolean[][] present = new boolean[2][2];
        byte[] header = new byte[16];
        boolean loaded = false;
        boolean ultimaxAddress = false;
        int headerLength;
        while ((headerLength = readChipHeader(input, header)) != 0) {
            if (headerLength != header.length) {
                throw new EOFException("Truncated Normal CRT CHIP header");
            }
            if (!"CHIP".equals(new String(header, 0, 4, US_ASCII))) {
                throw new IOException("Invalid Normal CRT 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);
            int region = address & 0xe000;
            int offset = address & 0x1fff;
            int maximum = address == 0x8000 ? 0x4000 : 0x2000 - offset;
            if (packetLength < header.length + size || (type != 0 && type != 2) || bank != 0
                    || (region != 0x8000 && region != 0xa000 && region != 0xe000)
                    || (address & 0xfff) != 0 || size == 0 || (size & 0xfff) != 0 || size > maximum) {
                throw new IOException("Invalid Normal CRT CHIP record");
            }
            int window = region == 0x8000 ? 0 : 1;
            int remaining = size;
            try {
                while (remaining > 0) {
                    int count = Math.min(remaining, 0x2000 - offset);
                    if (banks[window] == null) banks[window] = new byte[0x2000];
                    input.readFully(banks[window], offset, count);
                    for (int pos = offset; pos < offset + count; pos += 0x1000) present[window][pos >> 12] = true;
                    remaining -= count;
                    window = 1;
                    offset = 0;
                }
            } catch (EOFException e) {
                throw new EOFException("Truncated Normal CRT CHIP payload");
            }
            skipFully(input, packetLength - header.length - size, "Truncated Normal CRT CHIP padding");
            loaded = true;
            ultimaxAddress |= region == 0xe000;
        }
        if (!loaded) throw new IOException("Normal CRT contains no CHIP records");
        for (int window = 0; window < banks.length; window++) {
            // Mirror only a lone 4K half; never replace an explicitly supplied other half.
            if (present[window][0] != present[window][1]) {
                int from = present[window][0] ? 0 : 0x1000;
                System.arraycopy(banks[window], from, banks[window], from ^ 0x1000, 0x1000);
            }
        }
        roml = banks[0];
        romh = banks[1];
        return ultimaxAddress;
    }

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

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

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

    @Override
    public void reset() {
        super.reset();
        pla.setGameExrom(game, exrom);
    }
}