Teach the console the sequences the corpus already speaks, and let the status port see the window

Three things Snake found the moment somebody ran it in a window, and all three are the same
kind of mistake: the console grew a screen and kept asking the terminal.

IT COULD NOT CLEAR THE SCREEN. Every program here that moves a cursor does it with ANSI
escapes, because until there was a screen the thing on the other end was somebody's
terminal. The controller drew "[2J" as three letters and left the board underneath. It now
parses them, which is what a video terminal did - a VT100 is exactly this. The whole corpus
uses two, ESC[2J and ESC[H, and the general shape is recognised so anything else is
swallowed rather than drawn: a sequence nobody implemented should leave no marks. Cursor
positioning is in too, since it is the same parse and one line more.

IT DID NOT SEE KEYS FROM THE WINDOW, but did when the terminal behind it was focused, which
is the whole diagnosis in one sentence. Snake polls the READY bit and never blocks, and
consoleFetch - what the status port asks - was polling standard input regardless of whether
a front end had installed a hook. So a window's keys were invisible to every program that
looks before it reads, and a keystroke aimed at the terminal would be picked up instead.

The hook now takes a question. Zero is the status port looking, and must not present or
sleep: a program polling in a loop would otherwise be charged a frame for every glance. One
is the data port blocking, where presenting is exactly right, because a machine waiting for
a key is still a machine somebody is looking at. One value for both would have made either
polling ruinous or waiting dead.

AND IT RAN SLOWLY, which was the same bug wearing a hat: a game that never receives a
steering key is a game that only ever goes one way.

Six more checks in Tests/video.sh, to 32: that ESC[2J clears, that ESC[H goes to the corner
without disturbing what is drawn, that ESC[3;5H counts rows and columns from one, and that
an unknown sequence is swallowed and leaves nothing behind.

The hook itself is still the one thing here the suite cannot reach - it exists only when
there is a window, and this host has no display. It was found by a person playing Snake,
which is where the Test Manual says these go on being found.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E2JrLzFvuFX9fgi1LDRjrW
This commit is contained in:
Anachronaut
2026-08-28 22:43:27 -04:00
co-authored by Claude Opus 5
parent 6f8ad42277
commit 556a14b288
5 changed files with 183 additions and 8 deletions
+113 -3
View File
@@ -232,7 +232,100 @@ static void consoleNewLine(void) {
}
}
// ---- The sequences this machine already speaks ----
//
// Every program in the corpus that moves a cursor does it with ANSI escapes, because until
// now the thing on the other end was somebody's terminal. A display controller that did not
// understand them would draw "[2J" on the screen and leave the board underneath it, which is
// exactly what happened the first time Snake was run in a window.
//
// So the controller parses them, the way a video terminal did - that is what a VT100 was.
// The whole corpus uses two, ESC[2J and ESC[H, and the general shape is recognised so that
// anything else is SWALLOWED RATHER THAN DRAWN: a sequence nobody implemented should leave
// no marks, which is what a real terminal does with one it does not know.
#define SEQUENCE_PARAMS 2
static enum { DRAW_TEXT, DRAW_SAW_ESCAPE, DRAW_IN_SEQUENCE } drawState = DRAW_TEXT;
static int sequenceParam[SEQUENCE_PARAMS];
static int sequenceParams;
static void consoleClearScreen(int fromCursor) {
const int rows = videoRows();
const int columns = videoColumns();
for (int row = fromCursor ? cursorRow : 0; row < rows; row++) {
const int from = (fromCursor && row == cursorRow) ? cursorColumn : 0;
for (int column = from; column < columns; column++) {
videoPutCell(row, column, 0, 0);
}
}
}
// Returns 1 if the byte was part of a sequence and so is not a character to draw.
static int consoleSequence(uint8_t byte) {
switch (drawState) {
case DRAW_TEXT:
if (byte != 0x1B) {
return 0;
}
drawState = DRAW_SAW_ESCAPE;
return 1;
case DRAW_SAW_ESCAPE:
// Only the bracket form. An escape followed by anything else is not a sequence
// this machine has ever sent, and swallowing the escape alone is enough.
drawState = DRAW_TEXT;
if (byte == '[') {
drawState = DRAW_IN_SEQUENCE;
sequenceParams = 0;
sequenceParam[0] = 0;
sequenceParam[1] = 0;
}
return 1;
case DRAW_IN_SEQUENCE:
break;
}
if (byte >= '0' && byte <= '9') {
if (sequenceParams < SEQUENCE_PARAMS) {
sequenceParam[sequenceParams] = sequenceParam[sequenceParams] * 10 + (byte - '0');
}
return 1;
}
if (byte == ';') {
if (sequenceParams < SEQUENCE_PARAMS - 1) {
sequenceParams++;
}
return 1;
}
drawState = DRAW_TEXT;
switch (byte) {
case 'J':
// 2 is the whole screen, which is the one the corpus uses. Without a number it
// is from the cursor down, which is what the standard says and costs nothing.
consoleClearScreen(sequenceParam[0] != 2);
break;
case 'H': case 'f': {
// Row then column, one-based on the wire and zero-based here. Missing or zero
// means one, which is what makes a bare ESC[H the corner.
int row = sequenceParam[0];
int column = (sequenceParams >= 1) ? sequenceParam[1] : 0;
if (row < 1) row = 1;
if (column < 1) column = 1;
cursorRow = row - 1;
cursorColumn = column - 1;
if (cursorRow >= videoRows()) cursorRow = videoRows() - 1;
if (cursorColumn >= videoColumns()) cursorColumn = videoColumns() - 1;
}
break;
default:
// Recognised as a sequence and not implemented, so it leaves no marks.
break;
}
return 1;
}
static void consoleDraw(uint8_t byte) {
if (consoleSequence(byte)) {
return;
}
switch (byte) {
case '\n':
consoleNewLine();
@@ -266,9 +359,9 @@ static void consoleDraw(uint8_t byte) {
// stop drawing and stop answering. So a front end with a window installs a hook: called
// while the console has nothing, it gets to keep the window alive and hands back a byte
// when one is typed, or -1 to say the window has gone.
static int (*inputHook)(void) = NULL;
static int (*inputHook)(int mayWait) = NULL;
void consoleSetInputHook(int (*hook)(void)) {
void consoleSetInputHook(int (*hook)(int mayWait)) {
inputHook = hook;
}
@@ -286,7 +379,7 @@ uint8_t consoleReadByte(void) {
consoleShowWhatIsWritten();
if (inputHook != NULL) {
for (;;) {
int got = inputHook();
int got = inputHook(1);
if (got >= 0) {
// ---- The screen is the terminal now ----
//
@@ -348,6 +441,23 @@ static void consoleFetch(void) {
// just as entitled to have the drawing appear, and it never reaches the read that
// would otherwise have flushed for it.
consoleShowWhatIsWritten();
// ---- Where a byte comes from when there is no standard input ----
//
// A window's keys arrive through the hook, and THE STATUS PORT HAS TO ASK IT TOO. It did
// not, so a program polling READY in a window was asking a standard input nobody was
// typing at: Snake saw no keys at all, and would suddenly see one if the terminal behind
// the window happened to be focused. Asked without waiting, because a poll is a poll -
// the front end presents a frame when the console genuinely blocks, not when it looks.
if (inputHook != NULL) {
int got = inputHook(0);
if (got >= 0) {
consolePushback = got;
consoleAnnounce();
} else if (got == CONSOLE_GONE) {
consoleEnded = 1;
}
return;
}
struct pollfd waiting = { .fd = STDIN_FILENO, .events = POLLIN, .revents = 0 };
if (poll(&waiting, 1, 0) <= 0 || (waiting.revents & (POLLIN | POLLHUP)) == 0) {
return;