SFXSoundExpander.java

package libsidplay.components.cart.supported;

import libsidplay.common.Event;
import libsidplay.common.EventScheduler;
import libsidplay.components.cart.Cartridge;
import libsidplay.components.cart.MusicCartridge;
import libsidplay.components.cart.MusicCartridgeAudioProcessor;
import libsidplay.components.pla.Bank;
import libsidplay.components.pla.PLA;

import java.io.DataInputStream;
import java.util.function.IntSupplier;

/**
 * I/O-only Sound Expander (YM3526) / FM-YAM (YM3812). A7 is not decoded:
 * DF40/DFC0 selects a register, DF50/DFD0 writes data, DF60/DFE0 reads status.
 * The chip's interrupt output is not connected to the C64 IRQ line.
 * Reference: VICE vice/src/c64/cart/sfx_soundexpander.c.
 *
 * @author ken
 */
public class SFXSoundExpander extends Cartridge implements MusicCartridge {

    private final EventScheduler context;

    private final int type;

    private ISfxSoundExpander sfxSoundExpanderImpl;

    private long lastTime;
    private boolean started;
    private boolean detached;

    public boolean isStarted() {
        return started;
    }

    /**
     * <B>sizeKB is misused as type here: 0=OPL1 (YM3526), 1=OPL2 (YM3812)</B>
     */
    public SFXSoundExpander(DataInputStream dis, PLA pla, int sizeKB) {
        super(pla);
        type = sizeKB;
        context = pla.getCPU().getEventScheduler();
        reset();
    }

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

    @Override
    public void reset() {
        started = false;
        lastTime = context.getTime(Event.Phase.PHI2);
        if (detached) return;
        super.reset();
        if (sfxSoundExpanderImpl != null) {
            if (sfxSoundExpanderImpl instanceof ReSfxSoundExpander local)
                local.setCpuRate((int) context.getCyclesPerSecond());
            sfxSoundExpanderImpl.reset();
        }
    }

    private final Bank io2Bank = new Bank() {

        @Override
        public byte read(int addr) {
            clock();
            return ready() && (addr & 0x7f) == 0x60
                    ? sfxSoundExpanderImpl.read(0xdf60) : pla.getDisconnectedBusBank().read(addr);
        }

        @Override
        public void write(int addr, byte val) {
            clock();
            int port = addr & 0x7f;
            if (ready() && (port == 0x40 || port == 0x50)) {
                sfxSoundExpanderImpl.write(0xdf00 | port, val);
            }
        }
    };

    @Override
    public void clock() {
        long cycles = clocksSinceLastAccess();
        if (!ready()) return;
        // A mixer may not consume PCM during the player's initial warm-up.
        // Timers and register accesses still follow the C64 clock.
        while (cycles > 0) {
            int piece = (int) Math.min(cycles, 32768);
            if (sfxSoundExpanderImpl instanceof ReSfxSoundExpander local) local.clock(piece, started);
            else sfxSoundExpanderImpl.clock(piece);
            cycles -= piece;
        }
    }

    public void start() {
        clock();
        if (!ready()) return;
        sfxSoundExpanderImpl.start();
        started = true;
    }

    @Override
    public void prepare(int sampleRate, int bufferSize) {
        started = false;
        lastTime = context.getTime(Event.Phase.PHI2);
        if (!detached && sfxSoundExpanderImpl != null) {
            if (sfxSoundExpanderImpl instanceof ReSfxSoundExpander local)
                local.setCpuRate((int) context.getCyclesPerSecond());
            sfxSoundExpanderImpl.prepare(sampleRate, bufferSize);
        }
    }

    @Override
    public MusicCartridgeAudioProcessor createMusicCartridgeProcessor(IntSupplier ffBitMaskSupplier, IntSupplier ffFactorSupplier) {
        setSfxSoundExpanderImpl(new ReSfxSoundExpander(type, (int) context.getCyclesPerSecond(), ffBitMaskSupplier, ffFactorSupplier));
        return (MusicCartridgeAudioProcessor) sfxSoundExpanderImpl;
    }

    public ReSfxSoundExpander getEmulator() {
        return sfxSoundExpanderImpl instanceof ReSfxSoundExpander emulator ? emulator : null;
    }

    public void setSfxSoundExpanderImpl(ISfxSoundExpander sfxSoundExpanderImpl) {
        if (this.sfxSoundExpanderImpl instanceof ReSfxSoundExpander local) local.setObserver(null);
        started = false;
        lastTime = context.getTime(Event.Phase.PHI2);
        this.sfxSoundExpanderImpl = sfxSoundExpanderImpl;
    }

    @Override
    public void onDetach() {
        detached = true;
        started = false;
        if (sfxSoundExpanderImpl instanceof ReSfxSoundExpander local) {
            local.setObserver(null);
            local.reset();
        }
    }

    private boolean ready() {
        return !detached && sfxSoundExpanderImpl != null
                && (!(sfxSoundExpanderImpl instanceof ReSfxSoundExpander local) || local.isPrepared());
    }

    private long clocksSinceLastAccess() {
        final long now = context.getTime(Event.Phase.PHI2);
        long diff = Math.max(0, now - lastTime);
        lastTime = now;
        return diff;
    }

    @Override
    public String toString() {
        return "FM-YAM " + (type == 0 ? "OPL1 (YM3526)" : "OPL2 (YM3812)");
    }

}