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
132 lines
5.4 KiB
C
132 lines
5.4 KiB
C
// utility.c
|
|
// Utilities for the SplitBit CPU Emulator
|
|
// Written by Anachronaut
|
|
// 10/15/2024
|
|
|
|
#include "utility.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <getopt.h>
|
|
#include "../Assembler/assembly.h"
|
|
|
|
void printHelp(const char *programName) {
|
|
printf("Usage: %s [OPTIONS] [boot image]\n", programName);
|
|
printf("\n");
|
|
printf("Named an image, it is placed into memory and started, which is what a\n");
|
|
printf("debugger does and how the test suite runs. Given only a disk, the machine\n");
|
|
printf("starts the way hardware would: the built in ROM is shadowed into Program\n");
|
|
printf("Memory, and it reads the disk for everything else.\n");
|
|
printf("\n");
|
|
printf("Options:\n");
|
|
printf(" -d, --debug Enable debug mode.\n");
|
|
printf(" -c, --cycles N Stop after N cycles instead of running until the program halts.\n");
|
|
printf(" -f, --fast Run as fast as possible, ignoring the emulated cycle rate.\n");
|
|
printf(" -D, --disk FILE Attach a disk image, making one if it is not there.\n");
|
|
printf(" -L, --disk-cycles N How many cycles a block read or write takes. Zero, the\n");
|
|
printf(" default, finishes before the next instruction starts.\n");
|
|
printf(" -W, --write-protect Attach the disk read only. A disk the host will not let\n");
|
|
printf(" you write is read only whether you ask for this or not.\n");
|
|
printf(" -S, --screen FILE Save a picture of the screen, as a PPM, when the machine\n");
|
|
printf(" stops. Works with or without a window, which is how the\n");
|
|
printf(" tests look at a screen on a host that has no display.\n");
|
|
printf(" -h, --help Display this help message.\n");
|
|
}
|
|
|
|
uint8_t parseOptions(int argc, char *argv[], EmulatorOptions *options) {
|
|
static struct option long_options[] = {
|
|
{"debug", no_argument, 0, 'd'},
|
|
{"cycles", required_argument, 0, 'c'},
|
|
{"fast", no_argument, 0, 'f'},
|
|
{"disk", required_argument, 0, 'D'},
|
|
{"write-protect", no_argument, 0, 'W'},
|
|
{"disk-cycles", required_argument, 0, 'L'},
|
|
{"screen", required_argument, 0, 'S'},
|
|
{"help", no_argument, 0, 'h'},
|
|
{0, 0, 0, 0 }
|
|
};
|
|
int opt;
|
|
int option_index = 0;
|
|
|
|
options->debug = 0;
|
|
options->fast = 0;
|
|
options->cycles = 0;
|
|
options->disk = NULL;
|
|
options->writeProtect = 0;
|
|
options->diskCycles = 0;
|
|
options->screen = NULL;
|
|
|
|
// Parse options
|
|
while ((opt = getopt_long(argc, argv, "dc:fhD:WL:S:", long_options, &option_index)) != -1) {
|
|
switch (opt) {
|
|
case 'd':
|
|
options->debug = 1;
|
|
break;
|
|
case 'c': {
|
|
// The count has to be a plain positive number. Anything else is
|
|
// almost certainly a mistyped command line rather than a request
|
|
// to run zero cycles.
|
|
char *end;
|
|
long value = strtol(optarg, &end, 10);
|
|
if (*end != '\0' || value <= 0) {
|
|
fprintf(stderr, "Error: --cycles needs a positive number, not \"%s\".\n", optarg);
|
|
return OPTIONS_ERROR;
|
|
}
|
|
options->cycles = (unsigned long)value;
|
|
}
|
|
break;
|
|
case 'f':
|
|
options->fast = 1;
|
|
break;
|
|
case 'D':
|
|
options->disk = optarg;
|
|
break;
|
|
case 'W':
|
|
options->writeProtect = 1;
|
|
break;
|
|
case 'L':
|
|
options->diskCycles = strtoul(optarg, NULL, 0);
|
|
break;
|
|
case 'S':
|
|
options->screen = optarg;
|
|
break;
|
|
case 'h':
|
|
printHelp(argv[0]);
|
|
return OPTIONS_HELP;
|
|
default:
|
|
printHelp(argv[0]);
|
|
return OPTIONS_ERROR;
|
|
}
|
|
}
|
|
return OPTIONS_OK;
|
|
}
|
|
|
|
// Writes a byte out as eight binary digits, most significant first. printf's %b is
|
|
// a recent addition to C and not available everywhere, so this does it by hand.
|
|
// The buffer must have room for nine characters.
|
|
static void formatBinary(uint8_t value, char *out) {
|
|
for (int i = 0; i < 8; i++) {
|
|
out[i] = (value & (0x80 >> i)) ? '1' : '0';
|
|
}
|
|
out[8] = '\0';
|
|
}
|
|
|
|
void printRegisters(CPURegisters *cpu, uint8_t *Program, uint8_t *Data) {
|
|
char status[9];
|
|
formatBinary(cpu->Status, status);
|
|
printf("***** CPU Registers *****\n");
|
|
printf("A: 0x%02X\tB: 0x%02X\tQ: 0x%02X\tStatus: 0b%s\n", cpu->A, cpu->B, cpu->Q, status);
|
|
printf("Program Counter: 0x%04X Current Instruction: 0x%02X (%s)\n", cpu->ProgramCounter, Program[cpu->ProgramCounter],getMnemonic(Program[cpu->ProgramCounter]));
|
|
for (int i = 0; i < DATA_POINTERS; i++) {
|
|
printf(" Data Pointer %d: 0x%04X Current Data Value: 0x%02X%s\n",
|
|
i, cpu->DataPointer[i], Data[cpu->DataPointer[i]],
|
|
i >= PRESERVED_DATA_POINTERS ? " (volatile)" : "");
|
|
}
|
|
// The two casts keep these inside Data Memory. The Stack Pointer starts at the
|
|
// very top, so without them the display would read off the end of the array
|
|
// before a single byte has been pushed.
|
|
printf(" Stack Pointer: 0x%04X Current Value: (0x%02X) (0x%02X)\n", cpu->StackPointer,
|
|
Data[(uint16_t)(cpu->StackPointer + 1)], Data[(uint16_t)(cpu->StackPointer + 2)]);
|
|
}
|
|
|