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
+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
|
||||
|
||||
Reference in New Issue
Block a user