CircuitSID.java

package builder.resid.residfp.circuit;

import builder.resid.residfp.SID;
import libsidplay.common.ChipModel;
import libsidplay.config.IFilterSection;

/** ReSIDfp digital core with the modern libresidfp filter, mixer and volume models. */
public final class CircuitSID extends SID {
    private static final float CODE_RANGE = 4095f * 255;
    // Match playback levels while retaining the analogue model's full internal range.
    private static final float OUTPUT_GAIN = 0.8f;
    private static final float OUTPUT_SCALE = (2047f * 255 * 3 * 2) / 32768 * OUTPUT_GAIN;

    public CircuitSID() { super(new CircuitFilter(true), new CircuitFilter(false)); }

    public ChipModel chipModel() { return getChipModel(); }

    public void configure(IFilterSection settings) {
        ((CircuitFilter) getActiveFilter()).configure(settings);
        set6581VoiceNonlinearity(settings.getVoiceNonlinearity());
    }

    @Override
    protected float filterClock(float v1, float v2, float v3, float external) {
        int e1 = voice[0].envelope.analogEnvelopeLevel();
        int e2 = voice[1].envelope.analogEnvelopeLevel();
        int e3 = voice[2].envelope.analogEnvelopeLevel();
        if (getChipModel() == ChipModel.MOS6581) {
            // Remove the legacy multiplying-DAC offset and re-centre its waveform.
            v1 = (v1 - 0x800 * 255 - 0x480 * voice[0].envelope.output()) / CODE_RANGE;
            v2 = (v2 - 0x800 * 255 - 0x480 * voice[1].envelope.output()) / CODE_RANGE;
            v3 = (v3 - 0x800 * 255 - 0x480 * voice[2].envelope.output()) / CODE_RANGE;
        } else {
            // The 8580 voice signal is already centred; the circuit adds its own bias.
            v1 /= CODE_RANGE;
            v2 /= CODE_RANGE;
            v3 /= CODE_RANGE;
        }
        return ((CircuitFilter) getActiveFilter()).clockNormalized(v1, v2, v3, external / (48 * 65535f), e1, e2, e3)
                * OUTPUT_SCALE * (getChipModel() == ChipModel.MOS6581 ? 3 : 5);
    }
}