InspectionImage.java
package libsidutils.debug;
import java.util.ArrayList;
import java.util.List;
/**
* Memory visualizations for an inspection instant. These deliberately do not recreate earlier raster activity.
*/
public final class InspectionImage {
public static final int[] PALETTE = {0xff000000, 0xffffffff, 0xff813338, 0xff75cec8,
0xff8e3c97, 0xff56ac4d, 0xff2e2c9b, 0xffedf171, 0xff8e5029, 0xff553800,
0xffc46c71, 0xff4a4a4a, 0xff7b7b7b, 0xffa9ff9f, 0xff706deb, 0xffb2b2b2};
private static final int UNKNOWN = 0xff56616b;
private InspectionImage() {
}
public record SpriteBounds(int index, int x, int y, int width, int height) {
}
/**
* Current enabled-sprite register positions; these are viewport guides, not a pixel history.
*/
public static List<SpriteBounds> spriteBounds(InspectionSnapshot snapshot) {
List<SpriteBounds> boxes = new ArrayList<>();
InspectionFrame frame = snapshot.video();
if (frame == null) return boxes;
byte[] r = snapshot.vic().registers();
int linePixels = snapshot.cyclesPerLine() * 8;
for (int sprite = 0; sprite < 8; sprite++) {
int bit = 1 << sprite;
if ((r[0x15] & bit) == 0) continue;
int x = (r[sprite * 2] & 255) + ((r[0x10] & bit) != 0 ? 256 : 0);
if (x >= linePixels) continue;
x = (x + 8) % linePixels;
int width = (r[0x1d] & bit) != 0 ? 48 : 24;
int height = (r[0x17] & bit) != 0 ? 42 : 21;
int yRegister = r[sprite * 2 + 1] & 255;
for (int match = yRegister; match < snapshot.rasterLines(); match += 256) {
int y = (match + 1 - frame.firstRaster() + snapshot.rasterLines()) % snapshot.rasterLines();
for (int py = y; py + height > 0; py -= snapshot.rasterLines()) {
for (int px = x; px + width > 0; px -= linePixels) {
if (px < frame.width() && py < frame.height())
boxes.add(new SpriteBounds(sprite, px, py, width, height));
}
}
}
}
return boxes;
}
/**
* The same frozen presentation is used by the video screen and inspector; JSON retains untouched source pixels.
*/
public static int[] video(InspectionFrame frame) {
int width = frame.width(), height = frame.height();
int[] pixels = new int[width * height];
for (int i = 0; i < pixels.length; i++) {
pixels[i] = i < frame.pixels().length ? frame.pixels()[i]
: ((i % width / 8 + i / width / 8) & 1) == 0 ? 0xff202b35 : 0xff293844;
}
if (frame.beamY() >= 0 && frame.beamY() < height) {
int row = frame.beamY() * width;
for (int x = 0; x < width; x++) {
if ((x & 7) < 4) pixels[row + x] = 0xfff4b37f;
}
if (frame.beamX() >= 0) {
int x = Math.min(width - 1, frame.beamX());
for (int y = Math.max(0, frame.beamY() - 2); y <= Math.min(height - 1, frame.beamY() + 2); y++) {
pixels[y * width + x] = 0xffffffff;
}
}
}
return pixels;
}
public static int[] sprite(InspectionSnapshot snapshot, int sprite) {
int[] pixels = new int[24 * 21];
if (snapshot.sprites().length != 512) return pixels;
byte[] registers = snapshot.vic().registers();
boolean multicolor = (registers[0x1c] & 1 << sprite) != 0;
int[] colors = {0, PALETTE[registers[0x25] & 15], PALETTE[registers[0x27 + sprite] & 15],
PALETTE[registers[0x26] & 15]};
for (int y = 0; y < 21; y++)
for (int x = 0; x < 24; x++) {
int data = snapshot.sprites()[sprite * 64 + y * 3 + x / 8];
pixels[y * 24 + x] = data < 0 ? UNKNOWN : multicolor ? colors[data >> (6 - (x & 6)) & 3]
: (data & 128 >> (x & 7)) == 0 ? 0 : colors[2];
}
return pixels;
}
public static int[] characters(InspectionSnapshot snapshot) {
int[] pixels = new int[128 * 128];
if (snapshot.characters().length != 2048) return pixels;
for (int glyph = 0; glyph < 256; glyph++)
for (int y = 0; y < 8; y++)
for (int x = 0; x < 8; x++) {
int data = snapshot.characters()[glyph * 8 + y];
pixels[(glyph / 16 * 8 + y) * 128 + glyph % 16 * 8 + x] = data < 0 ? UNKNOWN
: (data & 128 >> x) != 0 ? 0xffb3a2f6 : 0xff10171e;
}
return pixels;
}
/**
* 40x25 memory cells, without border, scrolling, sprite compositing or recorded raster changes.
*/
public static int[] screen(InspectionSnapshot snapshot) {
int[] pixels = new int[320 * 200];
if (snapshot.screen().length != 1000 || snapshot.colors().length != 1000) return pixels;
byte[] registers = snapshot.vic().registers();
int mode = (registers[0x11] & 0x60) >> 4 | (registers[0x16] & 0x10) >> 4;
for (int cell = 0; cell < 1000; cell++)
for (int row = 0; row < 8; row++)
for (int x = 0; x < 8; x++) {
int code = snapshot.screen()[cell], color = snapshot.colors()[cell];
int data;
if (code < 0) data = -1;
else if ((mode & 2) != 0)
data = snapshot.bitmap().length == 8000 ? snapshot.bitmap()[cell * 8 + row] : -1;
else data = snapshot.characters().length == 2048
? snapshot.characters()[((mode & 4) != 0 ? code & 63 : code) * 8 + row] : -1;
int result;
if (data < 0) result = UNKNOWN;
else if (mode >= 5) result = PALETTE[0];
else if (mode == 3 || mode == 1 && (color & 8) != 0) {
int pair = data >> (6 - (x & 6)) & 3;
int index = mode == 3 ? switch (pair) {
case 0 -> registers[0x21] & 15;
case 1 -> code >> 4;
case 2 -> code & 15;
default -> color;
} : pair == 3 ? color & 7 : registers[0x21 + pair] & 15;
result = PALETTE[index & 15];
} else {
boolean set = (data & 128 >> x) != 0;
int foreground = mode == 2 ? code >> 4 : mode == 1 ? color & 7 : color;
int background = mode == 2 ? code & 15 : registers[0x21 + (mode == 4 ? code >> 6 : 0)] & 15;
result = PALETTE[(set ? foreground : background) & 15];
}
pixels[(cell / 40 * 8 + row) * 320 + cell % 40 * 8 + x] = result;
}
return pixels;
}
}