TimelineRecorder.java

package libsidutils.debug;

import libsidplay.C64;
import libsidplay.common.Event;
import libsidplay.common.Event.Phase;
import libsidplay.common.EventScheduler;
import libsidplay.components.mos656x.VIC;
import libsidplay.components.mos656x.VICInspection;
import libsidutils.debug.InspectionTimeline.Change;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * Scheduled only for an explicit recording. Unchanged probes allocate nothing; no guest reads or pixel copies.
 */
final class TimelineRecorder {
    private final C64 c64;
    private final EventScheduler scheduler;
    private final int generation, cyclesPerLine, rasterLines;
    private final Consumer<InspectionTimeline> listener;
    private final int[] state = new int[InspectionTimeline.FIELD_COUNT];
    private final List<Change> events = new ArrayList<>();
    private final Event probe = Event.of("Inspection one-frame timeline", event -> probe());
    private int[] initial = new int[0];
    private int previousRaster, startCycle, durationTicks;
    private long startTime = -1;
    private boolean done;

    TimelineRecorder(C64 c64, int generation, Consumer<InspectionTimeline> listener) {
        this.c64 = c64;
        this.generation = generation;
        this.listener = listener;
        scheduler = c64.getEventScheduler();
        cyclesPerLine = c64.getVIC().getCyclesPerLine();
        rasterLines = c64.getVIC().getCyclesPerFrame() / cyclesPerLine;
        previousRaster = VICInspection.raster(c64.getVIC());
    }

    void start() {
        listener.accept(snapshot("WAITING"));
        if (!done) scheduler.schedule(probe, 0, Phase.PHI1);
    }

    private void probe() {
        if (done) return;
        int raster = VICInspection.raster(c64.getVIC());
        if (startTime < 0) {
            if (raster == 0 && previousRaster != 0) {
                startTime = scheduler.getTime(scheduler.phase());
                startCycle = VICInspection.cycle(c64.getVIC());
                read(false);
                initial = state.clone();
                listener.accept(snapshot("RECORDING"));
            }
            previousRaster = raster;
        } else {
            durationTicks = tick();
            if (durationTicks >= cyclesPerLine * rasterLines * 2) {
                finish("COMPLETE", false);
                return;
            }
            if (!read(true)) {
                finish("LIMIT", false);
                return;
            }
        }
        if (!done) scheduler.schedule(probe, 0, scheduler.phase() == Phase.PHI1 ? Phase.PHI2 : Phase.PHI1);
    }

    void finish(String status, boolean observe) {
        if (done) return;
        if (observe && startTime >= 0) {
            durationTicks = tick();
            if (durationTicks >= cyclesPerLine * rasterLines * 2) status = "COMPLETE";
            else if (!read(true)) status = "LIMIT";
        }
        done = true;
        scheduler.cancel(probe);
        listener.accept(snapshot(status));
    }

    void discard() {
        done = true;
        scheduler.cancel(probe);
    }

    private int tick() {
        return (int) ((scheduler.getTime(scheduler.phase()) - startTime) * 2
                + (scheduler.phase() == Phase.PHI2 ? 1 : 0));
    }

    private boolean read(boolean changes) {
        VIC vic = c64.getVIC();
        byte[] registers = vic.getRegisters();
        for (int field = 0; field < InspectionTimeline.FIELD_COUNT; field++) {
            int value;
            if (field < 64) value = registers[field] & 255;
            else if (field == InspectionTimeline.DMA) value = VICInspection.dmaMask(vic);
            else if (field == InspectionTimeline.BANK) value = c64.getVicMemBase();
            else
                value = c64.getCartridge().pla.peekVicMemory(vic.getVideoMatrixBase() | 0x3f8 | (field - InspectionTimeline.POINTERS), false);
            if (changes && value != state[field]) {
                if (events.size() == InspectionTimeline.MAX_EVENTS) return false;
                events.add(new Change(durationTicks, VICInspection.raster(vic), VICInspection.cycle(vic),
                        scheduler.phase().name(), field, state[field], value));
            }
            state[field] = value;
        }
        return true;
    }

    private InspectionTimeline snapshot(String status) {
        return new InspectionTimeline(1, generation, status, c64.getModel().name(), c64.getConfiguredVicModel().name(),
                c64.getClock().name(), cyclesPerLine, rasterLines, startTime, startCycle, durationTicks,
                initial.clone(), List.copyOf(events));
    }
}