Files
SplitBit-Emulator/Source/Emulator/machine.h
T
AnachronautandClaude Opus 5 e3ef25e3b3 Split the machine from its front end, and add Voyager
The Segan Voyager is the same SplitBit with a screen and a speaker instead of a terminal,
and this is the rung that makes there be two of them at all. Everything that is actually
the machine - the CPU, the controller, the devices, the run loop, the reporting - moves to
machine.c, and each front end brings one file of its own. emulator.c is now sixty lines of
argument handling and a three line loop.

The machine runs in SLICES rather than to completion, because that is the cut a window
needs: run a slice, present a frame, run another. A terminal runs slices until the machine
stops. Both loops are three lines, which is why the cut is there rather than anywhere else.

At this stage Voyager's window is empty. There is no video device yet and inventing a
temporary way to draw would mean building something to throw away.

PLAIN MAKE STILL WORKS WITH NO GRAPHICS LIBRARY. Raylib is probed by compiling and linking
against it rather than by looking for a file, because a header with no library behind it
passes a file check and then fails at link time. Where it is missing, make says so once and
builds everything else - the machine, the assembler, the disk tool, the linter and the whole
suite. A project about a small understandable CPU should not need OpenGL to run its tests.
That nearly broke here: make strict globs Source/Emulator/*.c, so it would have tried to
compile voyager.c and failed on precisely the machines the split exists to support, and this
machine has Raylib so nothing would have caught it.

Tests/voyager.sh runs the WHOLE MANIFEST through Voyager and holds it to the recorded
results SplitBit is held to. Not that the two look alike: that one satisfies every recording
the other does, byte for byte, exit status included. It reuses run.sh, which now takes the
machine from SPLITBIT_EMULATOR, rather than keeping a second copy of the runner that would
drift. Voyager not being built is not a failure - it says so and passes.

Verified both ways. Made Voyager print one extra line, and 114 of 165 failed: exactly the
tests that run the emulator, with the 51 assemble-only and xfail cases correctly untouched.
Removed the binary, and the script skipped. Built with HAVE_RAYLIB=no, and everything else
still built and checked clean.

--headless is taken out of the arguments in voyager.c rather than in the shared parser,
which should not learn about a window only one binary has. It exists so the suite can run
this binary at all: a front end that could only be exercised by a person looking at it would
be a front end nothing checks.

loadFile takes a const char * now, which it always should have.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E2JrLzFvuFX9fgi1LDRjrW
2026-08-28 21:57:09 -04:00

74 lines
2.5 KiB
C

// machine.h
// The SplitBit machine, with nothing attached to look at it.
// Written by Anachronaut
#ifndef MACHINE_H
#define MACHINE_H
#include <stdint.h>
#include <time.h>
#include "cpu.h"
#include "utility.h"
// ---- Why the machine is not a main ----
//
// There are two front ends: SplitBit, which is a terminal, and Voyager, which is a window
// and a speaker. THEY ARE THE SAME MACHINE. Keeping it here means the difference between
// them is presentation and nothing else - neither can quietly acquire behaviour the other
// lacks, and the suite can hold them to it by running a program through both and comparing
// what came out.
//
// It also decides where the devices live. A video device that only existed in the windowed
// binary would be untestable, because the suite has no display; here it is part of the
// machine, advances on emulated cycles, and Raylib only presents what it already produced.
// How fast the machine runs when it is not being told to hurry.
#define CYCLE_RATE 1000000
typedef struct {
long long cycles_per_sec;
long long accumulator_ns;
struct timespec prev;
} CycleTimer;
void cycle_timer_init(CycleTimer *t, long long cycles_per_sec);
// Call once per host frame. Returns how many SplitBit cycles to execute.
int cycle_timer_tick(CycleTimer *t);
typedef struct {
CPURegisters cpu;
CycleTimer timer;
unsigned long cycleCount;
uint8_t limitReached;
uint8_t restartFailed;
const char *programFile;
EmulatorOptions options;
} Machine;
#define MACHINE_OK 0
#define MACHINE_ERROR 1
// No image and no disk. Told apart from an ordinary failure so that the front end can
// print its own help, which is the one thing here that knows what it is called.
#define MACHINE_NOTHING_TO_RUN 2
uint8_t machineStart(Machine *m, const EmulatorOptions *options, const char *programFile);
// Whether there is any more running to do. False once the machine has halted, faulted, or
// spent the cycle limit it was given.
int machineRunning(const Machine *m);
// ---- One slice, not the whole run ----
//
// A front end with a window has to get a turn: run a slice, present a frame, run another.
// A terminal simply runs slices until the machine stops. Both loops are three lines, which
// is the point of cutting it here rather than anywhere else.
void machineRunSlice(Machine *m);
void machineStop(Machine *m);
// Says how the run went and returns what the process should exit with.
int machineReport(const Machine *m);
#endif // MACHINE_H