diff --git a/Programs/testPrograms/colourTest.asm b/Programs/testPrograms/colourTest.asm new file mode 100644 index 0000000..68b64f9 --- /dev/null +++ b/Programs/testPrograms/colourTest.asm @@ -0,0 +1,82 @@ +; What the console's attribute register says down a serial line. +; +; The attribute has always meant something to the SCREEN and nothing to a terminal: its low +; nibble picks one of sixteen ink and paper pairs, and only videoPutCell ever read it. So a +; fault printed in red was red in the window and grey down the wire, and every program that +; chose a colour was choosing it for half its audience. +; +; This walks all sixteen schemes and then stops WITH ONE STILL SET, which is the second half +; of what is being checked: the machine has to put the terminal back to plain on its way out, +; and the reset has to come out AFTER the text rather than in front of it. An atexit handler +; runs before stdio flushes what is buffered, so a reset written straight to the file +; descriptor would arrive first and colour nothing. +; +; Written by Anachronaut + +#Include print.asm + +#Program + +start: + ; Banks 0 to 7 are ink on black. Nought is plain and is expected to say nothing at all: a + ; terminal is assumed to start plain, so a machine that never asks for a colour must be + ; silent rather than opening every session with a reset nobody wanted. + RSTA + SETD Scheme + STA + +inkNext: + SETD Scheme + LDA + OUTA 0x06 + SETD InkText + CALL printString + SETD Scheme + LDA + CALL printDigit + CALL lineFeed + + SETD Scheme + LDA + INCA + STA + INIB 0d16 + CCF + SUB + BNQ inkNext + + ; ---- And it stops in colour, on purpose ---- + ; + ; Fifteen is the last of them and nothing puts it back. What follows this in the recording + ; is whatever the machine says on its way out, which is the check. + HALT + +; A is nought to fifteen, said as ONE HEX CHARACTER. The scheme is a nibble, so a nibble is +; what it should read as - and one character means no scratch byte to hold the second digit +; while the first goes out, which is what the decimal version needed and got wrong. +printDigit: + INIB 0d10 + CCF + SUB + BRC digitLow ; Borrowed, so it is below ten. + MVQA ; What is left once the ten is taken off. + INIB 0d65 ; 'A', because the ten has already gone. + CCF + ADD + MVQA + OUTA 0x00 + RET +digitLow: + INIB 0d48 + CCF + ADD + MVQA + OUTA 0x00 + RET + +#Data + +InkText: +"scheme " +Scheme: + 0x00 diff --git a/Source/Emulator/io.c b/Source/Emulator/io.c index 6137777..67e1fba 100644 --- a/Source/Emulator/io.c +++ b/Source/Emulator/io.c @@ -61,6 +61,51 @@ static void consoleReleaseTerminal(void) { } } +// ---- And giving back the colour along with the terminal ---- +// +// A machine that stops with a scheme set would leave the shell that started it tinted, which +// is the same class of failure as leaving it with no echo: a state this program chose, +// outliving the program, with nothing to connect it back here. +// +// ---- Two of them, because the two endings have different rules ---- +// +// A machine STOPPING ON PURPOSE has a buffer of output still in stdio, and atexit handlers +// run BEFORE that buffer is flushed - so a reset written straight to the file descriptor +// would arrive in front of the text it is meant to follow. That one goes through the same +// buffer as everything else and comes out in order. +// +// A machine DYING ON A SIGNAL may be inside stdio at the moment the signal arrived, so it +// writes the four bytes directly and accepts that whatever was buffered may be lost. The +// process is ending either way, and a terminal left tinted is the failure being prevented. +// +// A TERMINAL IS ASSUMED TO START PLAIN, which is why this begins at nought rather than at +// "nothing said yet". A machine that never colours anything then says nothing at all, and +// the alternative was every session opening with a reset nobody asked for - CosmOS sets the +// scheme to nought on its way to the prompt, so the first character would have announced it. +// If the assumption is ever wrong, the first colour puts it right: every sequence begins +// with a reset. +// +// Setting it back to nought here is what makes a machine that carries on - resumed after a +// stop - say its colour again on the next character rather than believing it already has. +static uint8_t attributeTold = 0; + +static void consolePlainAgain(void) { + if (attributeTold == 0) { + return; + } + fputs("\033[0m", stdout); + attributeTold = 0; +} + +static void consolePlainAgainDying(void) { + if (attributeTold == 0) { + return; + } + ssize_t wrote = write(STDOUT_FILENO, "\033[0m", 4); + (void)wrote; + attributeTold = 0; +} + static void consoleTakeTerminal(void) { if (consoleTerminalRaw || !consoleTerminalSaved) { return; @@ -105,6 +150,7 @@ void consoleRestore(void) { // is waiting sees the signal it expected rather than a machine that exited quietly. static void consoleFatalSignal(int signalNumber) { consoleReleaseTerminal(); + consolePlainAgainDying(); signal(signalNumber, SIG_DFL); raise(signalNumber); } @@ -116,6 +162,7 @@ static void consoleContinueSignal(int signalNumber); // entitled to have its keys again when it is resumed. static void consoleStopSignal(int signalNumber) { consoleReleaseTerminal(); + consolePlainAgainDying(); signal(SIGCONT, consoleContinueSignal); signal(signalNumber, SIG_DFL); raise(signalNumber); @@ -302,6 +349,65 @@ static void consoleSayCursor(void) { consoleTellTerminal(sequence); } +// ---- Sixteen schemes, and what a terminal can make of them ---- +// +// The attribute's low nibble picks one of sixteen ink and paper pairs. Banks 0 to 7 are ink +// on black; banks 8 to 15 are those same colours as PAPER with black ink, which is the +// machine's one-bit highlight - attribute XOR 8 turns any of them inside out. ANSI says both +// of those, so what is needed here is a table rather than a translation. +// +// AND THE ORDER WAS ALREADY RIGHT, which is worth saying because it looks like a borrowing +// and is not. Both sets are an enumeration of a three bit colour, red green blue counted in +// binary: one is red in both, three is yellow in both, six is cyan in both. The same +// arithmetic was done twice, forty years apart. +// +// The one place they differ is slot 0, and that difference is forced: this screen is ink on +// black, so ink cannot be black, and slot 0 is grey where ANSI's is black. +// +// EVERY SEQUENCE BEGINS WITH A RESET. Setting only a foreground would leave the paper from +// whatever bank came before it, so going from bank 8 to bank 1 would have written red on +// grey. One extra byte, and the sequences stop depending on each other. +// +// Scheme 0 is sent as a bare reset rather than as grey on black, because plain text on +// somebody's terminal means THEIR plain text. Forcing grey on black would fight a terminal +// set up the other way round, which is the kind of thing this console exists not to do. +static const char *const schemeSequences[16] = { + "\033[0m", + "\033[0;31m", "\033[0;32m", "\033[0;33m", + "\033[0;34m", "\033[0;35m", "\033[0;36m", + "\033[0;97m", + "\033[0;47;30m", + "\033[0;41;30m", "\033[0;42;30m", "\033[0;43;30m", + "\033[0;44;30m", "\033[0;45;30m", "\033[0;46;30m", + "\033[0;107;30m", +}; + +// ---- Told once, and only when it matters ---- +// +// The same bargain the cursor gets, for the same reason: a register write only marks it and +// the next character sends it. A program that sets a scheme and prints nothing has said +// nothing, and one that sets the same scheme twice costs one sequence rather than two. +// +// Only the SCHEME nibble is looked at. The page bits say which tiles a cell draws from, +// which is a fact about the screen's own art and means nothing to a terminal. +static void consoleSayAttribute(void) { + const uint8_t scheme = consoleAttribute & VIDEO_ATTRIBUTE_SCHEME; + if (scheme == attributeTold) { + return; + } + // Installed on the first colour rather than at startup, so a run that never asks for one + // registers nothing at all - the same bargain the terminal guards make. + if (scheme != 0) { + static int plainGuardInstalled = 0; + if (!plainGuardInstalled) { + plainGuardInstalled = 1; + atexit(consolePlainAgain); + } + } + attributeTold = scheme; + consoleTellTerminal(schemeSequences[scheme]); +} + static void consoleDraw(uint8_t byte) { // ---- Nowhere to put a glyph ---- // @@ -1551,6 +1657,7 @@ uint8_t OutputHandler(uint8_t DataByte, uint8_t Address) { // since it was moved. Here rather than at the move, so setting a row and a // column costs one sequence rather than two. consoleSayCursor(); + consoleSayAttribute(); consoleDraw(DataByte); putchar(DataByte); break; diff --git a/SplitBit Programming Manual.md b/SplitBit Programming Manual.md index 5bbb31f..12725e0 100644 --- a/SplitBit Programming Manual.md +++ b/SplitBit Programming Manual.md @@ -931,6 +931,14 @@ The palette lives at 0xFC00 in video memory, four bytes an entry - red, green, b `Programs/Examples/colours.asm` does all of that in eighty lines and prints the result. It shows the sixteen pairs, shows what XOR 8 does to each, and then changes one of them by writing three bytes into the palette, so that the difference between using the colours a machine wakes up with and choosing your own is visible in one program. +**And it reaches a terminal too.** The attribute used to mean something to the screen and nothing to the serial line, so a fault printed in red was red in the window and grey down the wire. The console now says the scheme in ANSI as well, on the same terms as it says the cursor: a register write only marks it and the next character sends it, so setting a scheme and printing nothing says nothing, and setting the same scheme twice costs one sequence rather than two. + +The mapping is a table rather than a translation, because **the order was already right**. Both sets enumerate a three-bit colour - red, green and blue counted in binary - so one is red in both, three is yellow in both, six is cyan in both. The same arithmetic was done twice, forty years apart. The one place they differ is slot 0, and that difference is forced: this screen is ink on black, so ink cannot be black, and slot 0 is grey where ANSI's is black. + +Scheme 0 is sent as a bare reset rather than as grey on black, and a terminal is assumed to start plain - so a machine that never asks for a colour says nothing at all. **Plain text on somebody's terminal means their plain text**, and forcing grey on black would fight a terminal set up the other way round, which is the kind of thing this console exists not to do. A machine that stops with a scheme still set puts the terminal back to plain on its way out, however it stops. + +What does *not* cross is the page: the attribute's other nibble says which tiles a cell draws from, which is a fact about the screen's own art and means nothing to a terminal. + ### Moving The Cursor: Three more registers, because that is how this machine talks to everything else. diff --git a/SplitBit Test Manual.md b/SplitBit Test Manual.md index 33a0d8a..da0a850 100644 --- a/SplitBit Test Manual.md +++ b/SplitBit Test Manual.md @@ -125,7 +125,7 @@ from `make`, not from here. ### 1. Recorded output `Tests/run.sh` assembles each program named in `Tests/manifest`, runs it, and compares -everything it printed against a file in `Tests/expected`. 215 tests, of which 153 run, 35 +everything it printed against a file in `Tests/expected`. 216 tests, of which 154 run, 35 only assemble, 16 are expected to fail to assemble, and 11 boot from ROM with no image given at all. diff --git a/Tests/expected/colourTest.out b/Tests/expected/colourTest.out new file mode 100644 index 0000000..0801d70 --- /dev/null +++ b/Tests/expected/colourTest.out @@ -0,0 +1,18 @@ +scheme 0 +scheme 1 +scheme 2 +scheme 3 +scheme 4 +scheme 5 +scheme 6 +scheme 7 +scheme 8 +scheme 9 +scheme A +scheme B +scheme C +scheme D +scheme E +scheme F +Execution halted. +[exit 0] diff --git a/Tests/expected/colours.out b/Tests/expected/colours.out index bf8c81b..fef04c8 100644 --- a/Tests/expected/colours.out +++ b/Tests/expected/colours.out @@ -1,12 +1,12 @@ - ordinary highlighted - ordinary highlighted - ordinary highlighted - ordinary highlighted - ordinary highlighted - ordinary highlighted - ordinary highlighted - ordinary highlighted - + ordinary  highlighted  + ordinary  highlighted  + ordinary  highlighted  + ordinary  highlighted  + ordinary  highlighted  + ordinary  highlighted  + ordinary  highlighted  + ordinary  highlighted  + bank 2's ink is orange now, because this program said so Execution halted. -[exit 0] +[exit 0] diff --git a/Tests/expected/cosmosFault.out b/Tests/expected/cosmosFault.out index 61df993..e1b15e0 100644 --- a/Tests/expected/cosmosFault.out +++ b/Tests/expected/cosmosFault.out @@ -1,20 +1,20 @@ CosmOS > Crash opcode -that byte is not an instruction, at 5081 +that byte is not an instruction, at 5081 A 00 B 0F Q 00 -the program was stopped +the program was stopped > Crash service -nothing is installed at service 28, at 5084 +nothing is installed at service 28, at 5084 A 00 B 0F Q 00 -the program was stopped +the program was stopped > Crash bank -a bank that is not there, or an address past its end, at 509E +a bank that is not there, or an address past its end, at 509E A 01 B 0F Q 00 -the program was stopped +the program was stopped > Crash device -nothing is installed for the device on port 00, at 50A5 +nothing is installed for the device on port 00, at 50A5 A 02 B 0F Q 00 -the program was stopped +the program was stopped > Crash sideways Crash opcode | service | bank | device | blind finished diff --git a/Tests/expected/cosmosFaultSystem.out b/Tests/expected/cosmosFaultSystem.out index cac08e0..6a7566e 100644 --- a/Tests/expected/cosmosFaultSystem.out +++ b/Tests/expected/cosmosFaultSystem.out @@ -5,8 +5,8 @@ x examine, d disassemble, a assemble, s set, b bank, g go, exit leaves bank 00 * s 4F00 00 * g 4F00 -that byte is not an instruction, at 4F00 +that byte is not an instruction, at 4F00 A 01 B 7F Q 00 that was the system itself, so there is nowhere to carry on from. Start the machine again. Execution halted. -[exit 0] +[exit 0] diff --git a/Tests/expected/cosmosMonitor.out b/Tests/expected/cosmosMonitor.out index cac772c..3180d84 100644 --- a/Tests/expected/cosmosMonitor.out +++ b/Tests/expected/cosmosMonitor.out @@ -53,9 +53,9 @@ bank 00 800B 00 ? * g 8000 H -that byte is not an instruction, at 800A +that byte is not an instruction, at 800A A 85 B 3F Q 05 -the program was stopped +the program was stopped * d 8000 8000 26 48 INIA 48 8002 D1 00 OUTA 00 diff --git a/Tests/expected/cosmosMonitorRun.out b/Tests/expected/cosmosMonitorRun.out index 786da27..474890a 100644 --- a/Tests/expected/cosmosMonitorRun.out +++ b/Tests/expected/cosmosMonitorRun.out @@ -5,9 +5,9 @@ x examine, d disassemble, a assemble, s set, b bank, g go, exit leaves it says: hello from the monitor finished * Crash opcode -that byte is not an instruction, at 5081 +that byte is not an instruction, at 5081 A 00 B 0F Q 00 -the program was stopped +the program was stopped * nonsense I do not know: nonsense * b program diff --git a/Tests/manifest b/Tests/manifest index f81d0b3..cc3d41d 100644 --- a/Tests/manifest +++ b/Tests/manifest @@ -1202,6 +1202,21 @@ cosmosBlankDisk | CosmOS/Source/cosmos.asm | run | cosmosBla printDecimalTest | testPrograms/printDecimalTest.asm | xfail | - | - printDigitTest | testPrograms/printDigitTest.asm | xfail | - | - printHexTest | testPrograms/printHexTest.asm | xfail | - | - +# ---- What a colour means down a serial line ---- +# +# The console's attribute has always meant something to the SCREEN and nothing to a terminal: +# its low nibble picks one of sixteen ink and paper pairs, and only videoPutCell read it. So a +# fault printed in red was red in the window and grey down the wire. +# +# All sixteen, so the table is checked rather than sampled. Scheme 0 must say NOTHING - a +# terminal is assumed to start plain, and a machine that never asks for a colour opening every +# session with a reset would be noise in every recording here. +# +# AND IT STOPS IN COLOUR. What follows the halt line is the machine putting the terminal back, +# which is the other half: an atexit handler runs before stdio flushes, so a reset written +# straight to the file descriptor would come out in FRONT of the text it is meant to follow. +# The recording shows it after, which is the only place it is worth anything. +colourTest | testPrograms/colourTest.asm | run | - | 200000 # shiftTest calls printDecimal, which print.asm does not have. It looks like the # routine was renamed and this caller was never updated. shiftTest | testPrograms/shiftTest.asm | xfail | - | -