PALEmulationTeaVM.java

package client.teavm.common.video;

import client.teavm.common.IImportedApi;
import libsidplay.common.CPUClock;
import libsidplay.components.mos656x.IPALEmulation;
import libsidplay.components.mos656x.IPalette;
import libsidplay.components.mos656x.MOS6567;
import libsidplay.components.mos656x.VIC;

import java.nio.ByteBuffer;
import java.util.Arrays;

import static libsidplay.common.CPUClock.PAL;
import static libsidplay.components.mos656x.MOS6569.BORDER_HEIGHT;

/**
 * RGBA (MSB to LSB) pixel data big endian.
 */
public class PALEmulationTeaVM implements IPALEmulation {

    /**
     * RGBA pixel data (MSB to LSB) big endian. VIC colors without PAL emulation. Use this palette for VIC colors 0-15.
     * <a href="https://www.pepto.de/projects/colorvic/2001/">...</a>
     */
    private final int[] vicPaletteNoPal = new int[]{0x000000FF, 0xFFFFFFFF, 0x68372BFF, 0x70A4B2FF, 0x6F3D86FF,
            0x588D43FF, 0x352879FF, 0xB8C76FFF, 0x6F4F25FF, 0x433900FF, 0x9A6759FF, 0x444444FF, 0x6C6C6CFF, 0x9AD284FF,
            0x6C5EB5FF, 0x959595FF,};

    private final IImportedApi importedApi;

    private final int nthFrame;
    /**
     * Table for looking up color using a packed 2x8 value for even rasterlines
     */
    private final int[] combinedLinesEven;
    /**
     * Table for looking up color using a packed 2x8 value for odd rasterlines
     */
    private final int[] combinedLinesOdd;
    /**
     * VIC color palette for even rasterlines
     */
    private final byte[] linePaletteEven;
    /**
     * VIC color palette for odd rasterlines
     */
    private final byte[] linePaletteOdd;
    /**
     * Last line's color
     */
    private final byte[] previousLineDecodedColor = new byte[65 * 8];
    /**
     * Prevailing table for looking up color for the current line (odd/even)
     */
    private int[] combinedLinesCurrent;
    /**
     * Prevailing VIC color palette for the current line (odd/even)
     */
    private byte[] linePaletteCurrent;
    /**
     * Index into the last line
     */
    private int previousLineIndex;
    /**
     * Previous sequencer data
     */
    private int oldGraphicsData;

    private int n;
    private int frameOffset;
    private boolean inspectionRendering, frameRendered = true;
    private final ByteBuffer pixels;

    private boolean palEmulationEnable, palEmulationEnablePending;

    public PALEmulationTeaVM(IImportedApi importedApi, int nthFrame, CPUClock cpuClock, int[] combinedLinesEven,
                             int[] combinedLinesOdd, byte[] linePaletteEven, byte[] linePaletteOdd) {
        this.importedApi = importedApi;
        this.nthFrame = nthFrame;
        this.combinedLinesEven = combinedLinesEven;
        this.combinedLinesOdd = combinedLinesOdd;
        this.linePaletteEven = linePaletteEven;
        this.linePaletteOdd = linePaletteOdd;
        combinedLinesCurrent = combinedLinesEven;
        linePaletteCurrent = linePaletteEven;
        pixels = importedApi.initFrameBuffer(VIC.MAX_WIDTH, cpuClock == PAL ? BORDER_HEIGHT : MOS6567.MAX_RASTERS);
    }

    /**
     * Determine a palette for the current raster line.
     *
     * @param rasterY      current raster line
     * @param isFrameStart a new frame is about to start?
     */
    @Override
    public void determineCurrentPalette(int rasterY, boolean isFrameStart) {
        if (isFrameStart) {
            // apply new setting, only at frame start
            palEmulationEnable = palEmulationEnablePending;
            if (++n == nthFrame) {
                n = 0;
            }
            frameRendered = n == 0 || inspectionRendering;
            if (frameRendered) {
                importedApi.prepareFrame(pixels);
                frameOffset = pixels.position();
                if (palEmulationEnable) {
                    /* Current row odd? -> start with even, init, swap */
                    linePaletteCurrent = (rasterY & 1) != 0 ? linePaletteEven : linePaletteOdd;
                    combinedLinesCurrent = (rasterY & 1) != 0 ? combinedLinesEven : combinedLinesOdd;
                    Arrays.fill(previousLineDecodedColor, linePaletteCurrent[0]);
                }
            }
        }
        if (palEmulationEnable && frameRendered) {
            linePaletteCurrent = linePaletteCurrent == linePaletteOdd ? linePaletteEven : linePaletteOdd;
            combinedLinesCurrent = combinedLinesCurrent == combinedLinesOdd ? combinedLinesEven : combinedLinesOdd;
            oldGraphicsData = 0;
            previousLineIndex = 0;
        }
    }

    /**
     * Draw eight pixels at once. Pixels arrive in 0x12345678 order (MSB to LSB).
     *
     * @param graphicsDataBuffer eight pixels each of 4 bits (VIC color value range 0x0-0xF)
     */
    @Override
    public void drawPixels(int graphicsDataBuffer) {
        if (!frameRendered) {
            return;
        }
        /* Pixels arrive in 0x12345678 order. */
        if (palEmulationEnable) {
            for (int j = 0; j < 2; j++) {
                oldGraphicsData |= graphicsDataBuffer >>> 16;
                for (int i = 0; i < 4; i++) {
                    oldGraphicsData <<= 4;
                    final byte lineColor = linePaletteCurrent[oldGraphicsData >>> 16];
                    final byte previousLineColor = previousLineDecodedColor[previousLineIndex];
                    previousLineDecodedColor[previousLineIndex++] = lineColor;
                    pixels.putInt(
                            (combinedLinesCurrent[lineColor & 0xff | previousLineColor << 8 & 0xff00] << 8) | 0xff);
                }
                graphicsDataBuffer <<= 16;
            }
        } else {
            for (int j = 0; j < 2; j++) {
                oldGraphicsData |= graphicsDataBuffer >>> 16;
                for (int i = 0; i < 4; i++) {
                    oldGraphicsData <<= 4;
                    pixels.putInt(vicPaletteNoPal[(oldGraphicsData >>> 16) & 0x0f]);
                }
                graphicsDataBuffer <<= 16;
            }
        }
    }

    @Override
    public void updatePalette() {
    }

    @Override
    public void setPalEmulationEnable(boolean palEmulationEnablePending) {
        this.palEmulationEnablePending = palEmulationEnablePending;
    }

    @Override
    public void setVicPaletteNoPal(int[] vicPaletteNoPal) {
        assert vicPaletteNoPal.length == 16;

        System.arraycopy(vicPaletteNoPal, 0, this.vicPaletteNoPal, 0, vicPaletteNoPal.length);
    }

    @Override
    public IPalette getPalette() {
        return IPalette.FIXED;
    }

    /**
     * @return Output RGBA screen buffer as an int32 array. MSB to LSB -&gt; red, green, blue, alpha
     */
    @Override
    public ByteBuffer getPixels() {
        return pixels;
    }

    @Override
    public FrameBuffer getFrameBuffer() {
        return new FrameBuffer(pixels, frameOffset, (pixels.position() - frameOffset) >>> 2, true, frameRendered);
    }

    @Override
    public void setInspectionRendering(boolean enabled) {
        inspectionRendering = enabled;
    }

    @Override
    public void reset() {
        n = 0;
        frameOffset = 0;
        frameRendered = true;
        // clear the screen
        pixels.clear();
        for (int i = 0; i < pixels.capacity() >> 2; i++) {
            pixels.putInt(0x000000FF);
        }
        pixels.clear();
    }
}