Devices that take time, and a filesystem that waits for one
The disk's status has always had a bit meaning "still going", and the header beside it has always said to honour it. Nothing did, because nothing could: the host finished the transfer inside the instruction that asked for it, so the bit could never be seen up and asking about it was asking about something that cannot happen. --disk-cycles gives it a latency. The command is still checked at once, because a refusal is not work - a block that is not there fails before any head moves - but the transfer is remembered and done when the machine has run that far. Until then the buffer holds the block BEFORE this one. That last part is the point. A program that does not wait gets the wrong bytes rather than an error, which is the failure the bit exists to prevent and the one that would never have shown up. With a latency of two thousand, CosmOS could not even mount: sbfsMount reads block zero and looks straight at the buffer. deviceTick is the general shape rather than a disk feature. Called once per instruction with the machine's clock, it lets anything whose moment has come finish - which is what a display that refreshes, or a port that waits on the host, would want in exactly the same way. The filesystem watches the bit now, in one small routine reached with RCAL. That is not decoration: what it hands back is the settled status in A, and CALL puts A back the way it found it, so an ordinary call cannot carry the one thing this exists to carry. Two bytes of Stack rather than ten, in a routine that runs on every block the machine ever touches - the first place in the system where the new call is the right one rather than merely a cheaper one. The manifest takes a @N after a disk, the way it already takes :ro, so a test can ask for a slow one. cosmosSlowDisk lists a directory at two thousand cycles a block and gets the same listing as everything else, which is the whole assertion: a filesystem that did not wait would print nonsense rather than fail. Zero is the default and every other test runs at it. What waiting costs, on a directory heavy run: 229k cycles at zero, 275k at five hundred, 415k at two thousand, 1.16M at ten thousand. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01E2JrLzFvuFX9fgi1LDRjrW
This commit is contained in:
co-authored by
Claude Opus 5
parent
e0cf0a9a25
commit
d4cba36c5e
@@ -104,6 +104,7 @@ int main (int argc, char *argv[]) {
|
||||
}
|
||||
|
||||
CycleTimer timer;
|
||||
setDiskLatency(options.diskCycles);
|
||||
cycle_timer_init(&timer, CYCLE_RATE);
|
||||
|
||||
uint8_t limitReached = 0;
|
||||
@@ -137,6 +138,8 @@ int main (int argc, char *argv[]) {
|
||||
unsigned long took = cpu.busCycles - before;
|
||||
spent += (long)took;
|
||||
cycleCount += took;
|
||||
// Time has passed, so anything waiting on it may be finished.
|
||||
deviceTick(cycleCount);
|
||||
if (cpu.Status & STATUS_HALT) {
|
||||
// We've halted.
|
||||
break;
|
||||
|
||||
+55
-7
@@ -382,6 +382,22 @@ uint8_t refusingPort(void) {
|
||||
static FILE *diskImage = NULL;
|
||||
static uint32_t diskBlockCount = 0;
|
||||
static uint8_t diskBuffer[DISK_BLOCK_BYTES];
|
||||
|
||||
// ---- A disk that takes time ----
|
||||
//
|
||||
// The command is checked at once, because a refusal is not work: asking for a block that
|
||||
// is not there, or writing to a protected disk, fails before any head moves. What takes
|
||||
// time is the transfer, so that is remembered here and done when the machine has run far
|
||||
// enough - and until then the buffer holds the block BEFORE this one, which is exactly
|
||||
// what a program that ignores the busy bit deserves to read.
|
||||
// The machine's clock as devices see it, which the emulator advances as the CPU spends
|
||||
// cycles. A device says when it will be finished in these, and is believed.
|
||||
static unsigned long deviceNow = 0;
|
||||
static void diskTransfer(uint8_t command);
|
||||
|
||||
static unsigned long diskLatency = 0;
|
||||
static unsigned long diskReadyAt = 0;
|
||||
static uint8_t diskPending = 0;
|
||||
static uint16_t diskBlock = 0;
|
||||
static uint8_t diskStatus = 0;
|
||||
static uint8_t diskProtected = 0;
|
||||
@@ -458,23 +474,55 @@ static void diskCommand(uint8_t command) {
|
||||
raiseInterrupt(PORT_DISK);
|
||||
return;
|
||||
}
|
||||
size_t moved = 0;
|
||||
if (command == DISK_COMMAND_READ) {
|
||||
moved = fread(diskBuffer, 1, DISK_BLOCK_BYTES, diskImage);
|
||||
} else if (command == DISK_COMMAND_WRITE) {
|
||||
moved = fwrite(diskBuffer, 1, DISK_BLOCK_BYTES, diskImage);
|
||||
fflush(diskImage);
|
||||
} else {
|
||||
if (command != DISK_COMMAND_READ && command != DISK_COMMAND_WRITE) {
|
||||
diskStatus |= DISK_STATUS_ERROR;
|
||||
raiseInterrupt(PORT_DISK);
|
||||
return;
|
||||
}
|
||||
if (diskLatency == 0) {
|
||||
diskTransfer(command);
|
||||
return;
|
||||
}
|
||||
// It is going to take a while. Say so, and remember what to do when it is over.
|
||||
diskStatus |= DISK_STATUS_BUSY;
|
||||
diskPending = command;
|
||||
diskReadyAt = deviceNow + diskLatency;
|
||||
}
|
||||
|
||||
// The transfer itself, whenever it happens to happen. The seek is done here rather than at
|
||||
// the command, because nothing else may touch the image in between and doing it twice is
|
||||
// the same answer.
|
||||
static void diskTransfer(uint8_t command) {
|
||||
size_t moved = 0;
|
||||
long offset = (long)diskBlock * DISK_BLOCK_BYTES;
|
||||
if (fseek(diskImage, offset, SEEK_SET) != 0) {
|
||||
diskStatus |= DISK_STATUS_ERROR;
|
||||
} else if (command == DISK_COMMAND_READ) {
|
||||
moved = fread(diskBuffer, 1, DISK_BLOCK_BYTES, diskImage);
|
||||
} else {
|
||||
moved = fwrite(diskBuffer, 1, DISK_BLOCK_BYTES, diskImage);
|
||||
fflush(diskImage);
|
||||
}
|
||||
if (moved != DISK_BLOCK_BYTES) {
|
||||
diskStatus |= DISK_STATUS_ERROR;
|
||||
}
|
||||
diskStatus &= (uint8_t)~DISK_STATUS_BUSY;
|
||||
raiseInterrupt(PORT_DISK);
|
||||
}
|
||||
|
||||
void setDiskLatency(unsigned long cycles) {
|
||||
diskLatency = cycles;
|
||||
}
|
||||
|
||||
void deviceTick(unsigned long now) {
|
||||
deviceNow = now;
|
||||
if (diskPending && now >= diskReadyAt) {
|
||||
uint8_t command = diskPending;
|
||||
diskPending = 0;
|
||||
diskTransfer(command);
|
||||
}
|
||||
}
|
||||
|
||||
// ---- A device that brings memory ----
|
||||
//
|
||||
// The simplest thing that owns a bank. Writing to its port fills its memory with the
|
||||
|
||||
+22
-3
@@ -145,9 +145,12 @@ uint8_t consoleReadByte(void);
|
||||
#define DISK_COMMAND_READ 0x01
|
||||
#define DISK_COMMAND_WRITE 0x02
|
||||
|
||||
// Set while an operation is still going. It always reads clear here, because the host
|
||||
// finishes before the next instruction does, but a machine with a slower disk would set
|
||||
// it and a program that ignores it would break there. Honour it anyway.
|
||||
// Set while an operation is still going, and it now really is set: a disk given a latency
|
||||
// says busy, takes that many cycles, and finishes then. A program that does not wait gets
|
||||
// whatever was in the buffer before, which is what the hardware would give it.
|
||||
//
|
||||
// It reads clear the whole time when the latency is zero, which is the default and how
|
||||
// every test here has always run.
|
||||
#define DISK_STATUS_BUSY 0x01
|
||||
// Set when the disk cannot be written at all. Unlike the two bits above it, this is not
|
||||
// about the last operation: it is a standing property of the medium, readable before
|
||||
@@ -160,6 +163,22 @@ uint8_t consoleReadByte(void);
|
||||
// so it says so rather than stopping the machine.
|
||||
#define DISK_STATUS_ERROR 0x02
|
||||
|
||||
// ---- Devices that take time ----
|
||||
//
|
||||
// A real device does not finish inside the instruction that asked it to. It says it is
|
||||
// busy, takes as long as it takes, and is done when the machine has run that far - so the
|
||||
// emulator needs somewhere to notice that time has passed. That is this: called once per
|
||||
// instruction with the machine's clock, it lets any device whose moment has come finish.
|
||||
//
|
||||
// It is written for the disk and is not about the disk. Anything that will take time - a
|
||||
// display that refreshes, a port that waits on the host - wants exactly this shape.
|
||||
void deviceTick(unsigned long now);
|
||||
|
||||
// How many cycles a block read or write takes. Zero means the answer is there before the
|
||||
// next instruction is, which is what this machine has always done and what every recorded
|
||||
// test assumes.
|
||||
void setDiskLatency(unsigned long cycles);
|
||||
|
||||
// Attaches an image, making one if it is not there. A disk is read only if the host will
|
||||
// not let the file be written, or if writeProtect asks for it, which is the emulated
|
||||
// equivalent of the tab on the side of a floppy. Returns 1 if it could not attach.
|
||||
|
||||
@@ -18,6 +18,8 @@ void printHelp(const char *programName) {
|
||||
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(" -h, --help Display this help message.\n");
|
||||
@@ -30,6 +32,7 @@ uint8_t parseOptions(int argc, char *argv[], EmulatorOptions *options) {
|
||||
{"fast", no_argument, 0, 'f'},
|
||||
{"disk", required_argument, 0, 'D'},
|
||||
{"write-protect", no_argument, 0, 'W'},
|
||||
{"disk-cycles", required_argument, 0, 'L'},
|
||||
{"help", no_argument, 0, 'h'},
|
||||
{0, 0, 0, 0 }
|
||||
};
|
||||
@@ -41,9 +44,10 @@ uint8_t parseOptions(int argc, char *argv[], EmulatorOptions *options) {
|
||||
options->cycles = 0;
|
||||
options->disk = NULL;
|
||||
options->writeProtect = 0;
|
||||
options->diskCycles = 0;
|
||||
|
||||
// Parse options
|
||||
while ((opt = getopt_long(argc, argv, "dc:fhD:W", long_options, &option_index)) != -1) {
|
||||
while ((opt = getopt_long(argc, argv, "dc:fhD:WL:", long_options, &option_index)) != -1) {
|
||||
switch (opt) {
|
||||
case 'd':
|
||||
options->debug = 1;
|
||||
@@ -70,6 +74,9 @@ uint8_t parseOptions(int argc, char *argv[], EmulatorOptions *options) {
|
||||
case 'W':
|
||||
options->writeProtect = 1;
|
||||
break;
|
||||
case 'L':
|
||||
options->diskCycles = strtoul(optarg, NULL, 0);
|
||||
break;
|
||||
case 'h':
|
||||
printHelp(argv[0]);
|
||||
return OPTIONS_HELP;
|
||||
|
||||
@@ -19,6 +19,7 @@ typedef struct {
|
||||
uint8_t debug; // Step one instruction at a time, printing the registers.
|
||||
uint8_t fast; // Ignore the cycle rate and run as fast as the host allows.
|
||||
unsigned long cycles; // Stop after this many cycles. Zero means run until the program halts.
|
||||
unsigned long diskCycles; // How long a block move takes. Zero is instant, and the default.
|
||||
const char *disk; // Disk image to attach, or NULL for a machine with no disk.
|
||||
uint8_t writeProtect; // Attach the disk read only, the way a tab on a floppy would.
|
||||
} EmulatorOptions;
|
||||
|
||||
Reference in New Issue
Block a user