C1571.java

/*
 * 1571 wiring follows VICE memiec.c, via1d1541.c and cia1571d.c.
 * Copyright (C) the VICE project. GNU GPL version 2 or later.
 * Reference: d928b850b7ef4c1b1826a6efd175d8eaa80344d8.
 */
package libsidplay.components.c1541;

import libsidplay.common.CIAChipModel;
import libsidplay.common.Event;
import libsidplay.components.iec.IECBus;
import libsidplay.components.mos6526.MOS6526;

/**
 * 1571 drive with the 1541's two-VIA GCR electronics, a CIA, a WD1770 and a second head.
 */
public final class C1571 extends C1541 {
    /**
     * Active 32KB drive ROM, including any custom ROM.
     */
    private final byte[] driveRom;
    /**
     * Stock ROM restored when the custom ROM is removed.
     */
    private final byte[] defaultDriveRom;
    /**
     * CIA for fast serial transfers and its interrupt source.
     */
    private final MOS6526 cia;
    /**
     * MFM controller registers; mounted GCR media belongs to the VIA disk controller.
     */
    private final WD1770 wd1770;
    /**
     * IEC bus shared with the C64 and other drives.
     */
    private final IECBus bus;
    /**
     * Current drive CPU frequency in MHz (1 or 2).
     */
    private int clockMHz = 1;
    /**
     * Last CIA interrupt level, used to count changes on the shared CPU IRQ line.
     */
    private boolean ciaIrq;
    /**
     * Callback that updates the C64/drive clock divider after a frequency change.
     */
    private Runnable clockChanged = () -> {
    };

    /**
     * Create a 1571 with the 1541 GCR electronics and additional CIA and MFM controller.
     *
     * @param bus     IEC bus
     * @param id      floppy device number (8-11)
     * @param rom     32KB 1571 ROM
     * @param c1541   16KB 1541 ROM for the base electronics
     * @param c1541ii 16KB 1541-II ROM for the base electronics
     * @throws IllegalArgumentException 1571 ROM has an unsupported size
     */
    public C1571(IECBus bus, int id, byte[] rom, byte[] c1541, byte[] c1541ii) {
        super(bus, id, FloppyType.C1541, c1541, c1541ii);
        if (rom.length != 0x8000) throw new IllegalArgumentException("1571 ROM must contain 32768 bytes");
        driveRom = rom.clone();
        defaultDriveRom = rom.clone();
        this.bus = bus;
        wd1770 = new WD1770(() -> getEventScheduler().getTime(Event.Phase.PHI2), false);
        cia = new MOS6526(getEventScheduler(), CIAChipModel.MOS6526) {
            @Override
            public void interrupt(boolean state) {
                if (ciaIrq != state) {
                    ciaIrq = state;
                    signalIRQ(state);
                }
            }

            @Override
            public void pulse() {
            }

            @Override
            public byte readPRA() {
                return (byte) 0xff;
            }

            @Override
            public byte readPRB() {
                return (byte) 0xff;
            }

            @Override
            public void writePRA(byte value, boolean ddrChange) {
            }

            @Override
            public void writePRB(byte value) {
            }

            @Override
            protected void serialPins(boolean sp, boolean cnt) {
                bus.writeFastSerial(id, sp, cnt);
            }
        };
        bus.connectFastSerial(id, cia);
    }

    @Override
    public FloppyType getFloppyType() {
        return FloppyType.C1571;
    }

    @Override
    public MOS6526 getCIA() {
        return cia;
    }

    @Override
    public byte[] getROM() {
        return driveRom;
    }

    /**
     * Get the MFM register controller.
     *
     * @return MFM controller
     */
    public WD1770 getMFMController() {
        return wd1770;
    }

    @Override
    public double getClockFrequency() {
        return clockMHz * 1_000_000.;
    }

    /**
     * Set the callback invoked when the drive CPU switches between 1 and 2 MHz.
     *
     * @param callback clock divider update, executed on the drive emulation thread
     */
    public void setClockChanged(Runnable callback) {
        clockChanged = callback;
    }

    @Override
    protected byte readDriveMemory(int address) {
        if (address >= 0x8000) return cpuLastData = driveRom[address & 0x7fff];
        if (address < 0x1000) return cpuLastData = getRAM()[address & 0x7ff];
        if (address >= 0x1800 && address < 0x1c00) return cpuLastData = getBusController().read(address & 15);
        if (address >= 0x1c00 && address < 0x2000) return cpuLastData = getDiskController().read(address & 15);
        if (address >= 0x2000 && address < 0x3000) return cpuLastData = wd1770.read(address & 3);
        if (address >= 0x4000) return cpuLastData = cia.read(address & 15);
        return cpuLastData;
    }

    @Override
    protected void writeDriveMemory(int address, byte value) {
        cpuLastData = value;
        if (address < 0x1000) getRAM()[address & 0x7ff] = value;
        else if (address >= 0x1800 && address < 0x1c00) getBusController().write(address & 15, value);
        else if (address >= 0x1c00 && address < 0x2000) getDiskController().write(address & 15, value);
        else if (address >= 0x2000 && address < 0x3000) wd1770.write(address & 3, value);
        else if (address >= 0x4000 && address < 0x8000) cia.write(address & 15, value);
    }

    @Override
    protected void storeBusPortA(int address, byte value) {
        // VIA port A selects the CPU clock (PA5), head (PA2) and fast serial direction (PA1).
        int frequency = (value & 0x20) == 0 ? 1 : 2;
        if (clockMHz != frequency) {
            getDiskController().setClockMHz(frequency);
            clockMHz = frequency;
            clockChanged.run();
        }
        getDiskController().setSide(value >> 2 & 1);
        bus.setFastSerialDirection(getID(), (value & 2) != 0);
    }

    @Override
    protected byte readBusPortA() {
        getDiskController().rotateDisk();
        int pins = (getDiskController().isByteReadyLevel() ? 0 : 0x80)
                | (getDiskController().getHalfTrack() == 2 ? 0 : 1);
        byte[] via = getBusController().via;
        return (byte) ((pins & ~via[VIACore.VIA_DDRA]) | (via[VIACore.VIA_PRA] & via[VIACore.VIA_DDRA]));
    }

    @Override
    public void reset() {
        super.reset();
        clockMHz = 1;
        getDiskController().setClockMHz(1);
        getDiskController().setSide(0);
        wd1770.reset();
        ciaIrq = false;
        cia.reset();
        bus.setFastSerialDirection(getID(), false);
        bus.writeFastSerial(getID(), true, true);
        clockChanged.run();
    }

    @Override
    public void setRamExpansion(int bank, boolean enabled) {
        // The 1541 expansion banks are not part of the 1571 memory map.
    }

    @Override
    public void setCustomKernalRom(byte[] rom) {
        if (rom != null && rom.length != driveRom.length)
            throw new IllegalArgumentException("1571 ROM must contain 32768 bytes");
        System.arraycopy(rom != null ? rom : defaultDriveRom, 0, driveRom, 0, driveRom.length);
    }
}