OceanType1.java
package libsidplay.components.cart.supported;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.util.Arrays;
import libsidplay.components.cart.Cartridge;
import libsidplay.components.pla.Bank;
import libsidplay.components.pla.PLA;
import static java.nio.charset.StandardCharsets.US_ASCII;
/**
* <PRE>
* "Ocean Type 1" Cartridge (CRT type 5)
*
* - 16KiB to 512KiB, in 2 to 64 banks of 8KiB.
* - Type A (up to 256KiB): the selected bank is mirrored at
* $8000-$9FFF and $A000-$BFFF in 16KiB game mode.
* - Type B (512KiB): the selected bank is at $8000-$9FFF in 8KiB game mode.
* - Capacity is inferred from the highest CHIP bank, not header GAME/EXROM.
*
* - One write-only register throughout IO1 ($DE00-$DEFF):
*
* bit 0-5 bank number (unconnected address lines mirror smaller ROMs)
* bit 6-7 ignored; the cartridge cannot be disabled
* </PRE>
*
* @author Ken Händel
*
*/
public class OceanType1 extends Cartridge {
private static final int BANK_SIZE = 0x2000;
private static final int BANK_COUNT = 64;
/**
* Currently active ROM bank.
*/
protected int currentRomBank;
/**
* ROML banks 0..63 (each of size 0x2000), with absent banks filled with FF.
*/
protected final byte[][] romLBanks;
private final int bankMask;
public OceanType1(final DataInputStream dis, final PLA pla) throws IOException {
super(pla);
romLBanks = new byte[BANK_COUNT][BANK_SIZE];
for (byte[] bank : romLBanks) {
Arrays.fill(bank, (byte) 0xff);
}
bankMask = load(dis);
}
private int load(DataInputStream input) throws IOException {
byte[] header = new byte[16];
int highestBank = 0;
int headerLength;
while ((headerLength = readChipHeader(input, header)) != 0) {
if (headerLength != header.length) {
throw new EOFException("Truncated Ocean Type 1 CHIP header");
}
if (!"CHIP".equals(new String(header, 0, 4, US_ASCII))) {
throw new IOException("Invalid Ocean Type 1 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 >= BANK_COUNT || (address != 0x8000 && address != 0xa000) || size != BANK_SIZE) {
throw new IOException("Invalid Ocean Type 1 CHIP record");
}
try {
input.readFully(romLBanks[bank]);
} catch (EOFException e) {
throw new EOFException("Truncated Ocean Type 1 CHIP payload");
}
skipFully(input, packetLength - header.length - size, "Truncated Ocean Type 1 CHIP padding");
highestBank = Math.max(highestBank, bank);
}
// CRT records do not declare ROM capacity; round up to 2, 4, ..., 64 banks.
int mask = 1;
while (mask < highestBank) {
mask = (mask << 1) | 1;
}
return mask;
}
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) {
currentRomBank = value & bankMask;
pla.setGameExrom(bankMask == BANK_COUNT - 1, false);
}
};
private final Bank romlBank = new Bank() {
@Override
public byte read(int address) {
return romLBanks[currentRomBank][address & 0x1fff];
}
};
@Override
public Bank getRoml() {
return romlBank;
}
@Override
public Bank getRomh() {
return bankMask == BANK_COUNT - 1 ? pla.getDisconnectedBusBank() : romlBank;
}
@Override
public Bank getIO1() {
return io1Bank;
}
@Override
public void reset() {
super.reset();
io1Bank.write(0xde00, (byte) 0x00);
}
}