Seventy becomes seventy one: a machine that can wait
HALT is terminal - stepCPU returns at once when the Halt Flag is up, so a halted machine does not execute, service devices, or take an interrupt - and that has to stay true, because every test ends with a halt and "halted" is how a program says it has finished. The consequence was that SplitBit had no way to wait at all. Every wait was a spin, and a spin is bus traffic: 11.5% of Type over a 14K file on a disk of ten thousand cycles, after read-ahead had already hidden three quarters of the latency. WAIT is 0xFE, one byte, no operands, sitting under HALT where the instruction that almost stops the machine belongs. Three decisions in it: - A line already standing means there is nothing to wait for, so WAIT does nothing. That is what makes test-then-wait race-free. - Any line ends the wait, masked or not, so a program can sleep on a device it has no handler for and read its status afterwards. Masking says who answers a request, not whether it happened. - A line that wakes the CPU without being dispatched is taken down by the WAIT. Left standing it would be found by the next WAIT, which would return at once - the program would spin exactly as before while looking as though it slept. Waiting is NOT a Status bit, and that is the trap avoided rather than a gap: Status rides into the interrupt frame and comes back out, so a machine interrupted mid-wait would return from its handler still waiting, and wait again for what it had already been given. An internal field instead. Idle cycles are counted apart from bus cycles and the halt line says so when there are any, which is what makes the difference observable at all - with the line-clearing removed the total moves by ONE cycle, 20,100 against 20,099, and only the idle half changes, halving to 9,976. A test on totals could never have seen it. Tests/terminal.sh asks that question, being the file for things a recorded output cannot see, and fails with the clear removed while "both reads finished" still passes. Three collisions, all found by building it: - 0xFE was the assembler's "not an instruction" sentinel. getOpcode now answers a negative NOT_AN_OPCODE, which is outside the range of every possible answer instead of inside the unused part of it. - 0xFE was also what faultTest and faultResumeTest executed to provoke a fault. They now use 0xFD and say why, because they did not fail when it became an instruction - they HUNG, having started sleeping instead. - Keys.asm has had a label called "wait" for a year, and mnemonics are matched uppercased. What that reported was "Branch without label" at the BRQ thirty lines away. The assembler now refuses a label that is already an instruction, at the label, by name; every instruction added takes a word out of the space of label names, so this will happen again.
This commit is contained in:
@@ -122,11 +122,11 @@ int checkIfInstruction(intermediateElement *currentElement) {
|
||||
dot = strchr(dot, '.');
|
||||
}
|
||||
|
||||
uint8_t opcode = getOpcode(token);
|
||||
if (opcode == 0xFE) {
|
||||
// It's not an instruction.
|
||||
int found = getOpcode(token);
|
||||
if (found == NOT_AN_OPCODE) {
|
||||
return 0;
|
||||
}
|
||||
uint8_t opcode = (uint8_t)found;
|
||||
|
||||
int selectorsWanted = dataPointerOperands(opcode);
|
||||
if (selectorsGiven > selectorsWanted) {
|
||||
|
||||
@@ -92,6 +92,7 @@ Instruction instruction_set[] = {
|
||||
{0xE1, "INB"},
|
||||
// Special Operations:
|
||||
{0xF0, "NOP"},
|
||||
{0xFE, "WAIT"},
|
||||
{0xFF, "HALT"}
|
||||
};
|
||||
|
||||
@@ -139,13 +140,18 @@ int dataPointerOperands(uint8_t opcode) {
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t getOpcode(char* mnemonic) {
|
||||
int getOpcode(char* mnemonic) {
|
||||
for (int i = 0; i < num_instructions; i++) {
|
||||
if (strcmp(instruction_set[i].mnemonic, mnemonic) == 0) {
|
||||
return instruction_set[i].opcode;
|
||||
}
|
||||
}
|
||||
return 0xFE; // FE is an unused instruction, we'll use it to indicate an error.
|
||||
// NOT_AN_OPCODE, and it is negative on purpose. This used to answer 0xFE on the
|
||||
// grounds that 0xFE was unused - which was true until WAIT was given that opcode, at
|
||||
// which point the assembler would have read WAIT as a word it did not recognise. A
|
||||
// sentinel picked from the unused half of a range stops being a sentinel the moment
|
||||
// somebody uses the range, so this one is outside the range altogether.
|
||||
return NOT_AN_OPCODE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -119,7 +119,11 @@
|
||||
|
||||
const char* getMnemonic(uint8_t opcode);
|
||||
|
||||
uint8_t getOpcode(char* mnemonic);
|
||||
// The opcode a mnemonic assembles to, or NOT_AN_OPCODE if the word is not one. The
|
||||
// return is an int rather than a byte so that the answer "no" cannot be confused with any
|
||||
// of the 256 answers "yes" - see the note in getOpcode.
|
||||
#define NOT_AN_OPCODE (-1)
|
||||
int getOpcode(char* mnemonic);
|
||||
|
||||
// How many Data Pointer selector bytes follow the given opcode. Never more than two.
|
||||
#define MAX_DATA_POINTER_OPERANDS 2
|
||||
|
||||
@@ -81,6 +81,34 @@ void addLabel(char *labelName, uint16_t address, int type, const char *fileName,
|
||||
cleanedLabel[len - 1] = '\0'; // Remove the colon
|
||||
}
|
||||
|
||||
// A NAME THAT IS ALREADY AN INSTRUCTION IS REFUSED, and the error is here rather
|
||||
// than at the branch that could not find it. Mnemonics are matched with the token
|
||||
// uppercased, so a label called "wait" and the instruction WAIT are the same word
|
||||
// - and when WAIT was added, Keys.asm had been using that label for a year. What
|
||||
// it reported was "Branch without label" at the BRQ, thirty lines from the cause
|
||||
// and naming nothing that had changed.
|
||||
//
|
||||
// This will happen again. Every instruction added takes a word out of the space
|
||||
// of label names, so the check belongs where the name is claimed.
|
||||
{
|
||||
char upper[8];
|
||||
int n = 0;
|
||||
for (; cleanedLabel[n] != '\0' && n < (int)sizeof(upper) - 1; n++) {
|
||||
upper[n] = (char)toupper((unsigned char)cleanedLabel[n]);
|
||||
}
|
||||
upper[n] = '\0';
|
||||
// Only a name short enough to BE a mnemonic can collide with one, and the
|
||||
// longest is four characters. A longer name is truncated by the loop above
|
||||
// and would not match anything, which is the right answer.
|
||||
if (cleanedLabel[n] == '\0' && getOpcode(upper) != NOT_AN_OPCODE) {
|
||||
fprintf(stderr, RED "Error: \"%s\" is an instruction, so it cannot also be"
|
||||
" a label.\n" RESET, cleanedLabel);
|
||||
printf("File: %s at line %d.\n", fileName, lineNumber);
|
||||
free(cleanedLabel);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// A name may only be defined once. Without this check a reference quietly
|
||||
// resolves to whichever definition came first, so a typo or a name that two
|
||||
// libraries both happen to use is very hard to track down.
|
||||
|
||||
Reference in New Issue
Block a user