A tile engine on ports 0x30 to 0x3F, bringing one bank of video memory registered the way the disk's buffer is. The CPU writes cell indices and the device turns them into pixels, which is the whole reason a screen is affordable at a megahertz: a frame is 16,667 cycles, a full 320 by 200 picture is 64,000 bytes, and a 40 by 25 map is 2,000. A program that changes two cells writes four bytes. The cost of a screen becomes the number of cells that changed rather than the number of pixels on it. Which makes colour depth free, so the tiles are eight bits: an 8 by 8 cell is 64 pixels and each picks independently out of 256 colours, with no per-cell limit of the kind that made a Spectrum two and C64 multicolour four. The low nibble of a cell's attribute is ADDED to every index in its tile, sixteen at a time, so a tile drawn in 0 to 15 appears in any of sixteen schemes without a second copy in tile memory - and a tile wanting all 256 leaves the nibble at zero and gets them. Neither use costs the other anything. Two decisions are arithmetic rather than taste, and both come from the machine having no multiply. A map row is a page whether the mode fills it or not, so a cell address is the row number as the high byte and the doubled column as the low byte with no arithmetic at all; otherwise every cursor move on a 40 column screen would cost a row-times-40 in software. And a palette entry is four bytes rather than three, so entry n is at n times four, a shift. THE MAP IS A RING and the Scroll register says which of its 128 rows is on top. Scrolling moves a register and no memory: blitting a 40 by 25 screen up one line is 1,920 bytes inside one bank, which is twelve percent of a frame even with the controller widened, and a program printing one page would spend six frames shuffling memory. It is now one port write - and the rows that scrolled off are still there, which is where a terminal gets scrollback it never had. The device is part of the machine rather than part of the window. It renders into a buffer that is a pure function of video memory, so the same program draws the same picture with nobody watching; Voyager puts that buffer on the glass and decides nothing. Both binaries take --screen, which saves a PPM when the machine stops, and that is what makes a screen checkable on a host with no display at all. Tests/video.sh checks fourteen named behaviours rather than comparing a recorded image, because a recorded image would say "something changed" and leave which of the palette, the tile, the attribute, the map or the scroll register broke to be found by hand. Verified by breaking three things in turn: the additive nibble failed exactly one check, the scroll origin exactly two, and moving every cell one pixel sideways exactly the four about placement. Tests/docs.sh could not count past nine, which is how a suite of ten scripts reported itself as wrong for the wrong reason. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01E2JrLzFvuFX9fgi1LDRjrW
252 lines
11 KiB
C
252 lines
11 KiB
C
// machine.c
|
|
// The SplitBit machine: everything both front ends share.
|
|
// Written by Anachronaut
|
|
|
|
#include "machine.h"
|
|
#include "rom.h"
|
|
#include "bootstrap.h"
|
|
#include "cpu.h"
|
|
#include "controller.h"
|
|
#include "io.h"
|
|
#include "video.h"
|
|
#include "utility.h"
|
|
#include "../Assembler/assembly.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
// nanoseconds per second
|
|
#define NS_PER_SEC 1000000000LL
|
|
|
|
static inline long long timespec_diff_ns(struct timespec a, struct timespec b) {
|
|
return (a.tv_sec - b.tv_sec) * NS_PER_SEC + (a.tv_nsec - b.tv_nsec);
|
|
}
|
|
|
|
void cycle_timer_init(CycleTimer *t, long long cycles_per_sec) {
|
|
t->cycles_per_sec = cycles_per_sec;
|
|
t->accumulator_ns = 0;
|
|
clock_gettime(CLOCK_MONOTONIC, &t->prev);
|
|
}
|
|
|
|
// Call once per host frame. Returns how many SplitBit cycles to execute.
|
|
int cycle_timer_tick(CycleTimer *t) {
|
|
struct timespec now;
|
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
|
|
|
long long elapsed = timespec_diff_ns(now, t->prev);
|
|
t->prev = now;
|
|
|
|
// optional: clamp to avoid spiral-of-death on hitches
|
|
if (elapsed > NS_PER_SEC / 10) elapsed = NS_PER_SEC / 10;
|
|
|
|
t->accumulator_ns += elapsed;
|
|
|
|
long long period_ns = NS_PER_SEC / t->cycles_per_sec;
|
|
int cycles = (int)(t->accumulator_ns / period_ns);
|
|
t->accumulator_ns %= period_ns;
|
|
return cycles;
|
|
}
|
|
|
|
// How many cycles to run between glances at the wall clock. In fast mode there is
|
|
// no clock to keep pace with, so run a large batch before looking up.
|
|
#define FAST_BATCH 65536
|
|
|
|
// Memory Banks. Static, because a front end has no business reaching into them: what it
|
|
// needs to know about the machine it asks the machine.
|
|
static uint8_t Program[0x10000], Data[0x10000];
|
|
|
|
// How the run is reported. The idle half is mentioned only when there is one, so that
|
|
// every program written before WAIT existed prints exactly the line it always did.
|
|
//
|
|
// THE TWO ARE NOT THE SAME KIND OF TIME. A bus cycle is the machine using memory; an idle
|
|
// cycle is the machine stopped in a WAIT while a device catches up. Added together they
|
|
// are elapsed time, which is what a cycle limit measures; told apart they say whether a
|
|
// program was working or waiting.
|
|
static void reportCycles(const CPURegisters *cpu, unsigned long cycleCount) {
|
|
if (cpu->idleCycles > 0) {
|
|
printf("Execution halted after %lu cycles, %lu of them waiting.\n",
|
|
cycleCount, cpu->idleCycles);
|
|
} else {
|
|
printf("Execution halted after %lu cycles.\n", cycleCount);
|
|
}
|
|
}
|
|
|
|
|
|
uint8_t machineStart(Machine *m, const EmulatorOptions *options, const char *programFile) {
|
|
m->options = *options;
|
|
m->programFile = programFile;
|
|
m->cycleCount = 0;
|
|
m->limitReached = 0;
|
|
m->restartFailed = 0;
|
|
|
|
// ---- Where the machine's first instruction comes from ----
|
|
//
|
|
// Named an image, it is placed into memory and started - which is what a debugger
|
|
// does, and is how every test here runs. That path is not a shortcut to apologise
|
|
// for: placing memory from outside is a real thing real machines allow.
|
|
//
|
|
// Named none, the machine starts the way hardware would: the ROM is shadowed into
|
|
// Program Memory and it reads the disk for the rest. There has to be a disk for that
|
|
// to mean anything, and no image and no disk is a machine with nothing to run.
|
|
if (programFile == NULL && options->disk == NULL) {
|
|
return MACHINE_NOTHING_TO_RUN;
|
|
}
|
|
if (programFile != NULL) {
|
|
if (loadFile(programFile, Program, Data)) {
|
|
fprintf(stderr, "Error: Couldn't read file: %s\n", programFile);
|
|
return MACHINE_ERROR;
|
|
}
|
|
} else if (loadROM(bootROM, bootROMBytes, Program, Data)) {
|
|
fprintf(stderr, "Error: The boot ROM is not a boot image.\n");
|
|
return MACHINE_ERROR;
|
|
}
|
|
if (options->disk != NULL && attachDisk(options->disk, options->writeProtect)) {
|
|
return MACHINE_ERROR;
|
|
}
|
|
// The screen starts blank, and starts blank again on a warm restart: video memory is
|
|
// the device's, and a reset that left last program's screen up would be a reset that
|
|
// did not happen.
|
|
videoReset();
|
|
// The controller has to know where the memories are before anything can reach
|
|
// them through it. Banks 0 and 1 are those two arrays.
|
|
initializeController(Program, Data);
|
|
initializeCPU(&m->cpu, Program, Data);
|
|
if (m->options.debug) {
|
|
printRegisters(&m->cpu, Program, Data);
|
|
}
|
|
setDiskLatency(m->options.diskCycles);
|
|
cycle_timer_init(&m->timer, CYCLE_RATE);
|
|
return MACHINE_OK;
|
|
}
|
|
|
|
int machineRunning(const Machine *m) {
|
|
return !(m->cpu.Status & STATUS_HALT) && !m->limitReached && !m->restartFailed;
|
|
}
|
|
|
|
void machineRunSlice(Machine *m) {
|
|
if (m->options.debug) {
|
|
// Wait before advancing, not after, so that a keypress is what moves the
|
|
// machine on rather than something that happens once it already has.
|
|
// Through the console rather than getchar, so that everything reading standard
|
|
// input reads it the same way and the console's pushback stays the only place
|
|
// a byte can be sitting.
|
|
consoleReadByte();
|
|
}
|
|
int cycles;
|
|
if (m->options.debug) {
|
|
// Debug mode advances one instruction per keypress, so the wall clock
|
|
// has no say in how many cycles to run.
|
|
cycles = 1;
|
|
} else if (m->options.fast) {
|
|
cycles = FAST_BATCH;
|
|
} else {
|
|
cycles = cycle_timer_tick(&m->timer);
|
|
}
|
|
// ---- Spending a budget of cycles, not running a count of instructions ----
|
|
//
|
|
// An instruction costs what it touches, so a batch is finished when the cycles are
|
|
// gone rather than after so many steps. In debug mode the budget is one, and any
|
|
// instruction costs at least the fetch of its own opcode, so one step still runs.
|
|
for (long spent = 0; spent < cycles; ) {
|
|
// Both kinds of cycle, because both are time passing. A step that waits
|
|
// spends no bus at all, and a budget measured only in bus cycles would never
|
|
// be spent - the machine would sit inside one batch forever and the device it
|
|
// was waiting for would never be given a moment to finish.
|
|
unsigned long before = m->cpu.busCycles + m->cpu.idleCycles;
|
|
stepCPU(&m->cpu);
|
|
unsigned long took = (m->cpu.busCycles + m->cpu.idleCycles) - before;
|
|
spent += (long)took;
|
|
m->cycleCount += took;
|
|
// Time has passed, so anything waiting on it may be finished.
|
|
deviceTick(m->cycleCount);
|
|
|
|
// ---- Starting over ----
|
|
//
|
|
// Between instructions, which is the only place it can happen: a device cannot
|
|
// restart the machine from inside the instruction that asked for it.
|
|
//
|
|
// WHAT A RESET REPEATS IS HOW THIS MACHINE STARTED. Named an image, it is
|
|
// placed again; named none, the ROM is shadowed again and reads the disk for
|
|
// the rest. Anything else would mean a reset changed what the machine is,
|
|
// which is the one thing a reset must not do.
|
|
//
|
|
// The disk is not unplugged and its image keeps everything written to it. That
|
|
// is what warm means: the machine starts again, the world it starts into does
|
|
// not.
|
|
if (takeResetRequest()) {
|
|
// The vector table goes, and that is a deliberate departure from leaving
|
|
// memory alone. A vector points into whatever installed it, and after this
|
|
// that program is not running - so a handler left behind would aim an
|
|
// interrupt at an address belonging to something gone. It is the argument
|
|
// CosmOS already makes when it takes a program's vectors back at exit.
|
|
memset(Program + SOFTWARE_VECTOR_BASE, 0,
|
|
(size_t)(0x10000 - SOFTWARE_VECTOR_BASE));
|
|
uint8_t failed = (m->programFile != NULL)
|
|
? loadFile(m->programFile, Program, Data)
|
|
: loadROM(bootROM, bootROMBytes, Program, Data);
|
|
if (failed) {
|
|
fprintf(stderr, "Error: The machine could not be started again.\n");
|
|
m->restartFailed = 1;
|
|
return;
|
|
}
|
|
videoReset();
|
|
initializeCPU(&m->cpu, Program, Data);
|
|
break; // Out of this batch; the loop above carries on with a new CPU.
|
|
}
|
|
if (m->cpu.Status & STATUS_HALT) {
|
|
// We've halted.
|
|
break;
|
|
}
|
|
if (m->options.cycles && m->cycleCount >= m->options.cycles) {
|
|
m->limitReached = 1;
|
|
break;
|
|
}
|
|
}
|
|
if (m->options.debug) {
|
|
printRegisters(&m->cpu, Program, Data);
|
|
printf("Cycle: %lu\n", m->cycleCount);
|
|
}
|
|
}
|
|
|
|
void machineStop(Machine *m) {
|
|
// ---- Saving the screen ----
|
|
//
|
|
// Written when the machine stops, and it is what makes the screen testable at all: a
|
|
// suite has no display, so the only way to check what was drawn is to be handed it. A
|
|
// picture out of a headless run is also the quickest way for a person to see what a
|
|
// program actually put on the screen without sitting and watching it happen.
|
|
if (m->options.screen != NULL) {
|
|
videoWriteImage(m->options.screen);
|
|
}
|
|
detachDisk();
|
|
}
|
|
|
|
int machineReport(const Machine *m) {
|
|
if (m->restartFailed) {
|
|
return 1;
|
|
}
|
|
if (m->limitReached) {
|
|
printf("Execution stopped after %lu cycles. (cycle limit reached)\n", m->cycleCount);
|
|
} else if (m->cpu.Status & STATUS_FAULT) {
|
|
// The Program Counter is still pointing at whatever the CPU could not get past.
|
|
reportCycles(&m->cpu, m->cycleCount);
|
|
if (m->cpu.Fault == FAULT_NO_HANDLER) {
|
|
fprintf(stderr, "Fault: Software vector %u, dispatched from Program Address 0x%04X, has no handler installed.\n",
|
|
m->cpu.FaultVector, m->cpu.ProgramCounter);
|
|
} else if (m->cpu.Fault == FAULT_DEVICE_REFUSED) {
|
|
fprintf(stderr, "Fault: The device on port %u refused the access at Program Address 0x%04X, and nothing is installed to deal with it.\n",
|
|
m->cpu.FaultVector, m->cpu.ProgramCounter);
|
|
} else if (m->cpu.Fault == FAULT_NO_DEVICE_HANDLER) {
|
|
fprintf(stderr, "Fault: The device on port %u interrupted at Program Address 0x%04X, and hardware vector %u has no handler installed.\n",
|
|
m->cpu.FaultVector, m->cpu.ProgramCounter, m->cpu.FaultVector);
|
|
} else {
|
|
fprintf(stderr, "Fault: 0x%02X at Program Address 0x%04X is not an instruction.\n",
|
|
Program[m->cpu.ProgramCounter], m->cpu.ProgramCounter);
|
|
}
|
|
return 1;
|
|
} else {
|
|
reportCycles(&m->cpu, m->cycleCount);
|
|
}
|
|
return 0;
|
|
}
|