VicePALEmulation.java
package libsidplay.components.mos656x;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.util.Arrays;
/**
* Native-resolution VICE PAL adapter for desktop BGRA/ARGB frame consumers.
*/
public final class VicePALEmulation implements IPALEmulation {
public static final int WIDTH = VIC.MAX_WIDTH;
public static final int HEIGHT = MOS6569.BORDER_HEIGHT;
/**
* Alpha channel of ARGB pixel data.
*/
private static final int ALPHA = 0xff000000;
/**
* RGB pixel data (MSB to LSB). VIC colors without PAL emulation. Use this palette for VIC colors 0-15.
* <a href="https://www.pepto.de/projects/colorvic/2001/">Pepto</a>
*/
private final int[] vicPaletteNoPal = new int[]{0x000000, 0xFFFFFF, 0x68372B, 0x70A4B2, 0x6F3D86, 0x588D43,
0x352879, 0xB8C76F, 0x6F4F25, 0x433900, 0x9A6759, 0x444444, 0x6C6C6C, 0x9AD284, 0x6C5EB5, 0x959595,};
private final VicePALRenderer renderer = new VicePALRenderer(WIDTH);
/**
* VIC color indices for the current raster line.
*/
private final byte[] lineColors = new byte[WIDTH];
/**
* Decoded RGBA pixels for the current raster line, before conversion to desktop ARGB.
*/
private final int[] linePixels = new int[WIDTH];
/**
* Current raster line.
*/
private int currentRasterY;
/**
* Index into the current line.
*/
private int linePixelIndex;
/**
* Use PAL emulation? Changes take effect at frame start.
*/
private boolean palEmulationEnable = true;
private boolean palEmulationEnablePending = true;
/**
* Output ARGB screen buffer, including the recorder's bottom padding.
*/
private final ByteBuffer pixels = ByteBuffer.allocate(WIDTH * VIC.MAX_HEIGHT * Integer.BYTES)
.order(ByteOrder.LITTLE_ENDIAN);
private final IntBuffer packedPixels = pixels.asIntBuffer();
@Override
public void setPalEmulationEnable(boolean palEmulationEnablePending) {
this.palEmulationEnablePending = palEmulationEnablePending;
}
@Override
public void setVicPaletteNoPal(int[] vicPaletteNoPal) {
System.arraycopy(vicPaletteNoPal, 0, this.vicPaletteNoPal, 0, this.vicPaletteNoPal.length);
}
/**
* Gets the currently used palette.
*
* @return The fixed VICE palette settings.
*/
@Override
public IPalette getPalette() {
return IPalette.FIXED;
}
@Override
public void updatePalette() {
}
/**
* Determine palette for 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;
renderer.reset();
packedPixels.clear();
pixels.clear();
}
currentRasterY = rasterY;
linePixelIndex = 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) {
for (int shift = 28; shift >= 0; shift -= 4) {
lineColors[linePixelIndex++] = (byte) (graphicsDataBuffer >>> shift & 15);
}
if (linePixelIndex == WIDTH) {
if (palEmulationEnable) {
renderer.renderLine(lineColors, currentRasterY, linePixels);
for (int rgba : linePixels) {
packedPixels.put(Integer.rotateRight(Integer.reverseBytes(rgba), 8));
}
} else {
for (byte color : lineColors) {
packedPixels.put(ALPHA | vicPaletteNoPal[color]);
}
}
pixels.position(packedPixels.position() << 2);
linePixelIndex = 0;
}
}
/**
* @return Output ARGB screen buffer as an int32 array. MSB to LSB -> alpha, red, green, blue
*/
@Override
public ByteBuffer getPixels() {
return pixels;
}
@Override
public FrameBuffer getFrameBuffer() {
return new FrameBuffer(pixels, 0, pixels.position() >>> 2, false, true);
}
@Override
public void reset() {
renderer.reset();
linePixelIndex = 0;
// clear the screen, including the recorder's bottom padding
packedPixels.clear();
Arrays.fill(linePixels, ALPHA);
while (packedPixels.hasRemaining()) {
packedPixels.put(linePixels);
}
packedPixels.clear();
pixels.clear();
}
}