Stage 0: the emulator carries the ROM, so a disk is enough
./SplitBit --disk system.img stage two CosmOS > No boot image named. The emulator shadows its built in stage one into Program Memory - boot vector included - and the CPU then does exactly what it has always done: reads the boot vector and starts where it points. NOTHING ABOUT THE CPU CHANGED to make a machine that starts itself, which is what picking shadowing over a mapped ROM bought. The ROM is generated from Programs/Boot/stage1.asm by the makefile rather than committed beside it, because a copy of a program kept next to the program is a copy that goes stale. That makes the assembler a real dependency of the emulator, which it always sort of was and now says so. od and awk rather than xxd, which is not everywhere, or python, which the README does not ask anybody to install in order to build this. loadROM is loadFile given bytes instead of a path: both go through one reader over an fmemopen stream, because a ROM is a boot image and there is no reason for the machine to have two ways of understanding one. Naming an image still works and is what every other test here does. That path is not a shortcut to apologise for - placing memory from outside is a real thing real machines allow, and it is a debugger. The help says so now. No image and no disk is the one case with nothing to run, and it says that rather than printing a usage message about a missing file. run.sh gained a "rom" mode which hands the emulator a disk and nothing else. The source column still names stage1.asm, because that is what is IN the ROM: assembling it there says the thing the emulator carries is a thing that still assembles.
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include "bootstrap.h"
|
||||
#include "../Assembler/assembly.h" // For the boot image format, which both tools share.
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
// Reads a number of the given width, most significant byte first.
|
||||
@@ -144,18 +145,46 @@ static uint8_t readVectorSegment(FILE *file, uint8_t *Program) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The Program Segment must come first, then the Data Segment. Short circuiting here means
|
||||
// there is one exit, and so only one place that has to close whatever it was reading.
|
||||
static uint8_t readImage(FILE *file, uint8_t *Program, uint8_t *Data) {
|
||||
return readFileHeader(file)
|
||||
|| readSegment(file, "PRG", Program)
|
||||
|| readSegment(file, "DAT", Data)
|
||||
|| readVectorSegment(file, Program);
|
||||
}
|
||||
|
||||
// ---- Waking up in ROM ----
|
||||
//
|
||||
// The same reader as a named file, given the bytes instead of a path, because a ROM is a
|
||||
// boot image and there is no reason for the machine to have two ways of understanding one.
|
||||
//
|
||||
// THIS IS SHADOWING, which is a real technique rather than a convenience: reset copies the
|
||||
// ROM into Program Memory, including its boot vector, and the CPU then does exactly what it
|
||||
// has always done - reads the boot vector and starts where it points. Nothing about the CPU
|
||||
// changes to make a machine that starts itself.
|
||||
//
|
||||
// And because it is a copy rather than a mapping, the bytes are ordinary Program Memory
|
||||
// once stage one has jumped away. The system may write over them; a reset puts them back.
|
||||
uint8_t loadROM(const unsigned char *bytes, unsigned long length,
|
||||
uint8_t *Program, uint8_t *Data) {
|
||||
FILE *file = fmemopen((void *)(uintptr_t)bytes, (size_t)length, "rb");
|
||||
if (file == NULL) {
|
||||
fprintf(stderr, "Error: Couldn't open the boot ROM.\n");
|
||||
return 1;
|
||||
}
|
||||
uint8_t failed = readImage(file, Program, Data);
|
||||
fclose(file);
|
||||
return failed;
|
||||
}
|
||||
|
||||
uint8_t loadFile(char *path, uint8_t *Program, uint8_t *Data) {
|
||||
FILE *file = fopen(path, "rb");
|
||||
if (file == NULL) {
|
||||
fprintf(stderr, "Error: Couldn't open file: %s\n", path);
|
||||
return 1;
|
||||
}
|
||||
// The Program Segment must come first, then the Data Segment. Short circuiting
|
||||
// here means there is one exit, and so only one place that has to close the file.
|
||||
uint8_t failed = readFileHeader(file)
|
||||
|| readSegment(file, "PRG", Program)
|
||||
|| readSegment(file, "DAT", Data)
|
||||
|| readVectorSegment(file, Program);
|
||||
uint8_t failed = readImage(file, Program, Data);
|
||||
fclose(file);
|
||||
return failed;
|
||||
}
|
||||
|
||||
@@ -10,4 +10,8 @@
|
||||
|
||||
uint8_t loadFile(char *path, uint8_t *Program, uint8_t *Data);
|
||||
|
||||
// The same, from bytes the emulator carries rather than a file it opens. See loadROM.
|
||||
uint8_t loadROM(const unsigned char *bytes, unsigned long length,
|
||||
uint8_t *Program, uint8_t *Data);
|
||||
|
||||
#endif // BOOTSTRAP_H
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
// Written by Anachronaut
|
||||
// 10/15/2024
|
||||
|
||||
#include "rom.h"
|
||||
#include "bootstrap.h"
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
@@ -94,17 +96,32 @@ int main (int argc, char *argv[]) {
|
||||
if (optind < argc) {
|
||||
programFile = argv[optind];
|
||||
optind++;
|
||||
} else {
|
||||
fprintf(stderr, "Error: No boot image specified.\n");
|
||||
printHelp(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
if (optind < argc) {
|
||||
fprintf(stderr, "Error: Unexpected argument: %s\n", argv[optind]);
|
||||
return 1;
|
||||
}
|
||||
if (loadFile(programFile, Program, Data)) {
|
||||
fprintf(stderr, "Error: Couldn't read file: %s\n", programFile);
|
||||
// ---- 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) {
|
||||
fprintf(stderr, "Error: No boot image and no disk, so there is nothing to run.\n");
|
||||
printHelp(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
if (programFile != NULL) {
|
||||
if (loadFile(programFile, Program, Data)) {
|
||||
fprintf(stderr, "Error: Couldn't read file: %s\n", programFile);
|
||||
return 1;
|
||||
}
|
||||
} else if (loadROM(bootROM, bootROMBytes, Program, Data)) {
|
||||
fprintf(stderr, "Error: The boot ROM is not a boot image.\n");
|
||||
return 1;
|
||||
}
|
||||
if (options.disk != NULL && attachDisk(options.disk, options.writeProtect)) {
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
// rom.h
|
||||
// The bytes the machine wakes up in.
|
||||
//
|
||||
// Stage one, built from Programs/Boot/stage1.asm by the makefile rather than kept here as
|
||||
// a copy, because a copy of a program stored beside the program is a copy that goes stale.
|
||||
//
|
||||
// It is an ordinary boot image, and that is the point: nothing about stage one changes
|
||||
// when it moves from a file into a ROM except who puts it in memory.
|
||||
//
|
||||
// Written by Anachronaut
|
||||
|
||||
#ifndef ROM_H
|
||||
#define ROM_H
|
||||
|
||||
extern const unsigned char bootROM[];
|
||||
extern const unsigned long bootROMBytes;
|
||||
|
||||
#endif // ROM_H
|
||||
@@ -11,7 +11,12 @@
|
||||
#include "../Assembler/assembly.h"
|
||||
|
||||
void printHelp(const char *programName) {
|
||||
printf("Usage: %s [OPTIONS] <boot image>\n", 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");
|
||||
|
||||
Reference in New Issue
Block a user