joeylib2/tools/stsim/stsim.c

238 lines
8.5 KiB
C

// stsim -- host-side driver for the Space Taxi simulation core.
//
// Loads an STL4 level, attaches a recorded demo input stream and runs
// the game tick, printing one line of state per tick in the same shape
// the VICE tracer records (see stuff/spacetaxi and the compare script),
// so the port's engine can be diffed against the real machine.
//
// stsim <level.dat> <level letter> <ticks>
//
// H, W, T and X use the C64's own attract recordings; every other
// letter uses a synthesised gate fixture from gateStreams.h.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../../examples/spacetaxi/stSim.h"
#include "../../examples/spacetaxi/stCels.h"
#include "../../examples/spacetaxi/stDemoStreams.h"
#include "gateStreams.h"
static StLevelT gLevel;
static StSimT gSim;
void stAudioSfx(const uint8_t *program9) { (void)program9; }
void stAudioNoise(bool on) { (void)on; }
void stAudioThrustSweep(uint8_t value) { (void)value; }
void stAudioSpeech(uint8_t ch) { (void)ch; }
void stAudioSpeechPhrase(const uint8_t *speech) { (void)speech; }
void stAudioSilence(void) { }
void stAudioVoice2(uint8_t freqLo, uint8_t freqHi, uint8_t ctrl) { (void)freqLo; (void)freqHi; (void)ctrl; }
void stAudioVoice1Freq(uint8_t value) { (void)value; }
// FNV-1a over a byte span: the picture the player actually sees. The
// hooks that animate the screen (O, R, V, W, X) change screen, colour or
// charset bytes and nothing else this line used to capture.
static uint32_t hashBytes(const uint8_t *p, size_t n) {
uint32_t h = 2166136261u;
size_t i;
for (i = 0u; i < n; i++) {
h = (h ^ p[i]) * 16777619u;
}
return h;
}
// The charset the game would be showing. The sim no longer holds a
// contiguous 2 KB copy -- edited glyphs live in an override table -- so
// rebuild the effective image here. Hashing this keeps the trace value
// identical to the pre-override captures, which is what lets the goldens
// prove the change is behaviour-preserving.
static uint32_t charsetHash(const StSimT *sim) {
uint8_t image[ST_CHARSET_CHARS * 8u];
uint16_t ch;
for (ch = 0u; ch < ST_CHARSET_CHARS; ch++) {
memcpy(&image[ch * 8u], stSimGlyph(sim, (uint8_t)ch), 8u);
}
return hashBytes(image, sizeof(image));
}
// STSIM_CELS=1: every level cel (ptr below the baked set) a ride
// displays, as (ptr, colour, multi, mc0, mc1), against what
// stLevelCelList bakes for the level -- a cel shown but not listed is
// built and drawn interpreted mid-ride on the real ports.
#define CENSUS_MAX 64u
static StCelDefT gCensus[CENSUS_MAX];
static uint8_t gCensusCount;
static void celCensusTick(const StSimT *sim) {
uint8_t i;
for (i = 1u; i < ST_HW_SPRITES; i++) {
StCelDefT d;
uint8_t k;
if ((sim->frame.enableMask & kStBit[i]) == 0u || sim->frame.ptr[i] >= ST_SPRITE_PTR_FIRST) {
continue;
}
// A pointer with no bitmap (a hook enables a sprite a tick before
// aiming it) draws nothing on every port -- not a cel.
if (stSimSpriteBitmap(sim, sim->frame.ptr[i]) == 0) {
continue;
}
d.ptr = sim->frame.ptr[i];
d.color = sim->frame.color[i];
d.multi = (sim->frame.multiMask & kStBit[i]) != 0u ? 1u : 0u;
d.mc0 = d.multi ? sim->spriteMc0 : 0u;
d.mc1 = d.multi ? sim->spriteMc1 : 0u;
for (k = 0u; k < gCensusCount; k++) {
if (memcmp(&gCensus[k], &d, sizeof(d)) == 0) {
break;
}
}
if (k == gCensusCount && gCensusCount < CENSUS_MAX) {
gCensus[gCensusCount++] = d;
}
}
}
static void celCensusReport(const StLevelT *level) {
StCelDefT listed[ST_LEVEL_CELS_MAX];
uint8_t n = stLevelCelList(level, listed, ST_LEVEL_CELS_MAX);
uint8_t k;
uint8_t j;
for (k = 0u; k < gCensusCount; k++) {
for (j = 0u; j < n; j++) {
if (memcmp(&listed[j], &gCensus[k], sizeof(StCelDefT)) == 0) {
break;
}
}
fprintf(stderr, "CEL %s ptr=%02X color=%u multi=%u mc=%u/%u\n", (j < n) ? "listed " : "MISSING", gCensus[k].ptr, gCensus[k].color, gCensus[k].multi, gCensus[k].mc0, gCensus[k].mc1);
}
fprintf(stderr, "CENSUS shown=%u listed=%u\n", gCensusCount, n);
}
static void celCensusReport(const StLevelT *level);
static void celCensusTick(const StSimT *sim);
static void printState(const StSimT *sim, uint32_t tick) {
uint8_t k;
printf("{\"tick\":%u,\"x\":\"%02X%02X.%02X\",\"y\":\"%02X.%02X\",\"vx\":\"%04X\",\"vy\":\"%04X\","
"\"pad\":%u,\"st\":%u,\"ph\":%u,\"in\":\"%02X\",\"dir\":\"%02X\",\"ptr0\":\"%02X\",\"ptr1\":\"%02X\","
"\"s1\":\"%02X%02X.%02X\",\"en\":\"%02X\",\"t4740\":\"%02X\",\"off\":%u,\"rng\":\"%02X%02X\","
"\"fuel\":%u,\"slots\":%u,\"asi\":%u,\"score\":\"",
tick, sim->posXmsb, sim->posXcol, sim->posXlo, sim->posYrow, sim->posYlo,
(uint16_t)sim->velX, (uint16_t)sim->velY, sim->activePad, sim->stage, sim->collisionPhase,
sim->inputMask, sim->dirMask, sim->spr[0].ptr, sim->spr[1].ptr,
sim->spr[1].msb, sim->spr[1].col, sim->spr[1].row, sim->frame.enableMask,
sim->demoTimer, sim->demoOff, sim->rngT1, sim->rngT2, sim->fuelCells, sim->fareSlotCount, sim->activeSpriteIdx);
for (k = 0u; k < ST_NUMBER_CHARS; k++) {
printf("%02X", sim->screen[ST_CELL_SCORE + k]);
}
printf("\",\"fare\":\"");
for (k = 0u; k < ST_NUMBER_CHARS; k++) {
printf("%02X", sim->screen[ST_CELL_FARE + k]);
}
printf("\",\"fuelc\":\"");
for (k = 0u; k < ST_FUEL_CELLS; k++) {
printf("%02X", sim->screen[ST_CELL_FUEL + k]);
}
printf("\",\"sprx\":\"");
for (k = 0u; k < ST_HW_SPRITES; k++) {
printf("%03X%02X", sim->frame.x[k], sim->frame.y[k]);
}
printf("\",\"scr\":\"%08X\",\"col\":\"%08X\",\"chr\":\"%08X\",\"bd\":\"%02X\",\"bg\":\"%02X\"}\n",
hashBytes(sim->screen, ST_SCREEN_CELLS),
hashBytes(sim->color, ST_SCREEN_CELLS),
charsetHash(sim),
sim->borderColor, sim->bgColor);
}
int main(int argc, char **argv) {
FILE *fp;
const uint8_t *stream = kDemoStreamH;
uint16_t len = (uint16_t)sizeof(kDemoStreamH);
uint8_t want;
uint32_t ticks;
uint32_t t;
if (argc < 4) {
fprintf(stderr, "usage: stsim <level.dat> <level letter> <ticks>\n");
return 2;
}
fp = fopen(argv[1], "rb");
if (fp == NULL || !stLevelParse(&gLevel, fp)) {
fprintf(stderr, "stsim: cannot load %s\n", argv[1]);
return 1;
}
fclose(fp);
want = (uint8_t)argv[2][0];
switch (want) {
case 'W': stream = kDemoStreamW; len = (uint16_t)sizeof(kDemoStreamW); break;
case 'T': stream = kDemoStreamT; len = (uint16_t)sizeof(kDemoStreamT); break;
case 'X': stream = kDemoStreamX; len = (uint16_t)sizeof(kDemoStreamX); break;
case 'H': break;
default: {
// A synthesised gate fixture for a level the C64 has no
// attract recording of (see gateStreams.h).
uint8_t g;
for (g = 0u; g < ST_GATE_STREAMS; g++) {
if (kGateStreams[g].level == want) {
stream = kGateStreams[g].stream;
len = kGateStreams[g].length;
break;
}
}
if (g == ST_GATE_STREAMS) {
fprintf(stderr, "stsim: no stream for '%c'\n", (char)want);
return 2;
}
break;
}
}
ticks = (uint32_t)strtoul(argv[3], NULL, 0);
stSimNewGame(&gSim, 1u, true);
stSimSeedDemoBuffer(&gSim, kDemoBufferInit, ST_DEMO_BUFFER_BYTES);
if (argc > 4) {
// carried-over state: flame parity, wave index, walk parity
gSim.flameParity = (uint8_t)strtoul(argv[4], NULL, 16);
}
if (argc > 5) {
gSim.waveIdx = (uint8_t)strtoul(argv[5], NULL, 16);
}
if (argc > 6) {
gSim.walkParity = (uint8_t)strtoul(argv[6], NULL, 16);
}
stSimEnterLevel(&gSim, &gLevel);
stSimSetDemoStream(&gSim, stream, len);
for (t = 0u; t < ticks; t++) {
StTickResultE r;
printState(&gSim, t);
r = stSimTick(&gSim);
if (getenv("STSIM_CELS") != NULL) {
celCensusTick(&gSim);
}
if (r != ST_TICK_CONTINUE) {
printf("{\"tick\":%u,\"end\":%d}\n", t, (int)r);
break;
}
}
if (getenv("STSIM_CELS") != NULL) {
celCensusReport(&gLevel);
}
return 0;
}