Replace the escape parser with cursor registers

The console had grown an ANSI parser, and that was the wrong shape. ANSI exists because a
screen used to be on the other end of a serial line and a byte stream was the only channel
there was. This screen is memory the program can already address, so reaching it by sending
characters for a state machine to take apart is a middleman for something the machine does
better - and it meant accepting an open protocol somebody else defines, in hardware, with no
natural end to it. Everything else on this machine is registers.

So the console gets three: cursor row at 0x03, cursor column at 0x04, and a command port at
0x05 where 1 clears the screen. Both cursor registers are READ as well as written, which is
the thing an escape cannot do without sending a query and parsing a reply - a routine that
wants to put the cursor back where it found it can now ask.

Clearing is one command against a thousand cells walked one at a time. Snake and Life are
smaller for it: 2,168 bytes to 2,163 and 1,410 to 1,396.

A HOST TERMINAL STILL SPEAKS ANSI, and bridging to the host is the emulator's job, the same
job it does reading standard input. So the escapes are now GENERATED, outbound, for the set
this device chooses, rather than parsed inbound as though the machine were a terminal. The
set cannot grow behind our backs because we are the ones saying it. The cursor is announced
lazily, at the next character rather than at the register write, so setting a row and a
column costs one sequence rather than two.

The console's block widens from three ports to six, which registryTest noticed: it had been
asking about port 0x05 precisely BECAUSE nothing was there, and the console had just moved
in. Re-blessing it would have left it checking nothing, so it asks about 0x80 instead -
clear of the console, the disk, the screen, the controller, and the sound device coming to
0x40.

Six checks in Tests/video.sh swapped from the sequences to the registers, including that the
cursor reads back and that one sent past the edge is clamped rather than refusing. Those
checks also stopped counting bytes from the ends of a file, which had quietly started
measuring an escape the moment the console began announcing the cursor.

SplitLint caught the one thing worth catching in the port: the clear command leaves A at 1
and key mode is also 1, so the second load looks redundant. Acting on it would tie a console
command to a console mode by coincidence, and break silently if either ever moved, so it is
suppressed with that reason rather than removed.

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 23:08:52 -04:00
co-authored by Claude Opus 5
parent bdb2d0d8e6
commit 43a05b3df1
17 changed files with 316 additions and 294 deletions
+29 -29
View File
@@ -1,4 +1,4 @@
 #
 #
#
###
@@ -14,7 +14,7 @@


# #
##
#
@@ -30,7 +30,7 @@


#
# #
##
@@ -46,7 +46,7 @@


#
##
##
@@ -62,7 +62,7 @@


#
#
###
@@ -78,7 +78,7 @@


# #
##
@@ -94,7 +94,7 @@


#
# #
@@ -110,7 +110,7 @@


#
##
@@ -126,7 +126,7 @@


#
#
@@ -142,7 +142,7 @@


# #
@@ -158,7 +158,7 @@


#
@@ -174,7 +174,7 @@


#
@@ -190,7 +190,7 @@


#
@@ -206,7 +206,7 @@


@@ -222,7 +222,7 @@


@@ -238,7 +238,7 @@


@@ -254,7 +254,7 @@


@@ -270,7 +270,7 @@


@@ -286,7 +286,7 @@


@@ -302,7 +302,7 @@


@@ -318,7 +318,7 @@


@@ -334,7 +334,7 @@


@@ -350,7 +350,7 @@


@@ -366,7 +366,7 @@


@@ -382,7 +382,7 @@


@@ -398,7 +398,7 @@


@@ -414,7 +414,7 @@


@@ -430,7 +430,7 @@


@@ -446,7 +446,7 @@


+30 -30
View File
@@ -1,4 +1,4 @@
 #
 #
#
###
@@ -14,7 +14,7 @@


# #
##
#
@@ -30,7 +30,7 @@


#
# #
##
@@ -46,7 +46,7 @@


#
##
##
@@ -62,7 +62,7 @@


#
#
###
@@ -78,7 +78,7 @@


# #
##
@@ -94,7 +94,7 @@


#
# #
@@ -110,7 +110,7 @@


#
##
@@ -126,7 +126,7 @@


#
#
@@ -142,7 +142,7 @@


# #
@@ -158,7 +158,7 @@


#
@@ -174,7 +174,7 @@


#
@@ -190,7 +190,7 @@


#
@@ -206,7 +206,7 @@


@@ -222,7 +222,7 @@


@@ -238,7 +238,7 @@


@@ -254,7 +254,7 @@


@@ -270,7 +270,7 @@


@@ -286,7 +286,7 @@


@@ -302,7 +302,7 @@


@@ -318,7 +318,7 @@


@@ -334,7 +334,7 @@


@@ -350,7 +350,7 @@


@@ -366,7 +366,7 @@


@@ -382,7 +382,7 @@


@@ -398,7 +398,7 @@


@@ -414,7 +414,7 @@


@@ -430,7 +430,7 @@


@@ -446,7 +446,7 @@


@@ -462,7 +462,7 @@


+54 -54
View File
@@ -1,6 +1,6 @@
CosmOS
> loaded, starting at 4000
>  #
>  #
#
###
@@ -16,7 +16,7 @@ CosmOS


# #
##
#
@@ -32,7 +32,7 @@ CosmOS


#
# #
##
@@ -48,7 +48,7 @@ CosmOS


#
##
##
@@ -64,7 +64,7 @@ CosmOS


#
#
###
@@ -80,7 +80,7 @@ CosmOS


# #
##
@@ -96,7 +96,7 @@ CosmOS


#
# #
@@ -112,7 +112,7 @@ CosmOS


#
##
@@ -128,7 +128,7 @@ CosmOS


#
#
@@ -144,7 +144,7 @@ CosmOS


# #
@@ -160,7 +160,7 @@ CosmOS


#
@@ -176,7 +176,7 @@ CosmOS


#
@@ -192,7 +192,7 @@ CosmOS


#
@@ -208,7 +208,7 @@ CosmOS


@@ -224,7 +224,7 @@ CosmOS


@@ -240,7 +240,7 @@ CosmOS


@@ -256,7 +256,7 @@ CosmOS


@@ -272,7 +272,7 @@ CosmOS


@@ -288,7 +288,7 @@ CosmOS


@@ -304,7 +304,7 @@ CosmOS


@@ -320,7 +320,7 @@ CosmOS


@@ -336,7 +336,7 @@ CosmOS


@@ -352,7 +352,7 @@ CosmOS


@@ -368,7 +368,7 @@ CosmOS


@@ -384,7 +384,7 @@ CosmOS


@@ -400,7 +400,7 @@ CosmOS


@@ -416,7 +416,7 @@ CosmOS


@@ -432,7 +432,7 @@ CosmOS


@@ -448,7 +448,7 @@ CosmOS


@@ -464,7 +464,7 @@ CosmOS


@@ -480,7 +480,7 @@ CosmOS


@@ -496,7 +496,7 @@ CosmOS


@@ -512,7 +512,7 @@ CosmOS


@@ -528,7 +528,7 @@ CosmOS


@@ -544,7 +544,7 @@ CosmOS


@@ -560,7 +560,7 @@ CosmOS


@@ -576,7 +576,7 @@ CosmOS


@@ -592,7 +592,7 @@ CosmOS


@@ -608,7 +608,7 @@ CosmOS


@@ -624,7 +624,7 @@ CosmOS


@@ -640,7 +640,7 @@ CosmOS


@@ -656,7 +656,7 @@ CosmOS


@@ -672,7 +672,7 @@ CosmOS
#


@@ -688,7 +688,7 @@ CosmOS
##


@@ -704,7 +704,7 @@ CosmOS
##


@@ -720,7 +720,7 @@ CosmOS
###


@@ -736,7 +736,7 @@ CosmOS
##
#


@@ -752,7 +752,7 @@ CosmOS
# #
##


@@ -768,7 +768,7 @@ CosmOS
##
##


@@ -784,7 +784,7 @@ CosmOS
#
###


@@ -800,7 +800,7 @@ CosmOS
# #
##
#


@@ -816,7 +816,7 @@ CosmOS
#
# #
##


@@ -832,7 +832,7 @@ CosmOS
#
#
##


@@ -848,7 +848,7 @@ CosmOS
#
##


+1 -1
View File
@@ -1,6 +1,6 @@
CosmOS
> loaded, starting at 4000
>  #
>  #
#
###
+2 -2
View File
@@ -2,8 +2,8 @@ CosmOS
> nothing is loaded
> greet.sbx 211
hello.sbx 53
Life.sbx 1410
Snake.sbx 2168
Life.sbx 1396
Snake.sbx 2163
Keys.sbx 664
Say.sbx 156
Break.sbx 149
+2 -2
View File
@@ -1,8 +1,8 @@
CosmOS
> greet.sbx 211
hello.sbx 53
Life.sbx 1410
Snake.sbx 2168
Life.sbx 1396
Snake.sbx 2163
Keys.sbx 664
Say.sbx 156
Break.sbx 149
+15 -15
View File
@@ -1,6 +1,6 @@
CosmOS
> loaded, starting at 4000
> +----------------+
> +----------------+
| |
| |
| |
@@ -19,7 +19,7 @@ CosmOS
| |
+----------------+
score 0 wasd steers, q stops
+----------------+
+----------------+
| |
| |
| |
@@ -38,7 +38,7 @@ CosmOS
| |
+----------------+
score 0 wasd steers, q stops
+----------------+
+----------------+
| |
| |
| |
@@ -57,7 +57,7 @@ CosmOS
| |
+----------------+
score 0 wasd steers, q stops
+----------------+
+----------------+
| |
| |
| |
@@ -76,7 +76,7 @@ CosmOS
| |
+----------------+
score 0 wasd steers, q stops
+----------------+
+----------------+
| |
| |
| |
@@ -95,7 +95,7 @@ CosmOS
| |
+----------------+
score 0 wasd steers, q stops
+----------------+
+----------------+
| |
| |
| |
@@ -114,7 +114,7 @@ CosmOS
| |
+----------------+
score 0 wasd steers, q stops
+----------------+
+----------------+
| |
| |
| |
@@ -133,7 +133,7 @@ CosmOS
| |
+----------------+
score 0 wasd steers, q stops
+----------------+
+----------------+
| |
| |
| |
@@ -152,7 +152,7 @@ CosmOS
| |
+----------------+
score 0 wasd steers, q stops
+----------------+
+----------------+
| |
| |
| |
@@ -171,7 +171,7 @@ CosmOS
| |
+----------------+
score 0 wasd steers, q stops
+----------------+
+----------------+
| |
| |
| |
@@ -190,7 +190,7 @@ CosmOS
| |
+----------------+
score 0 wasd steers, q stops
+----------------+
+----------------+
| |
| |
| |
@@ -209,7 +209,7 @@ CosmOS
| |
+----------------+
score 0 wasd steers, q stops
+----------------+
+----------------+
| |
| |
| |
@@ -228,7 +228,7 @@ CosmOS
| |
+----------------+
score 1 wasd steers, q stops
+----------------+
+----------------+
| |
| |
| |
@@ -247,7 +247,7 @@ CosmOS
| |
+----------------+
score 1 wasd steers, q stops
+----------------+
+----------------+
| |
| |
| |
@@ -266,7 +266,7 @@ CosmOS
| |
+----------------+
score 1 wasd steers, q stops
+----------------+
+----------------+
| |
| |
| |
+1 -1
View File
@@ -1,6 +1,6 @@
00 02 00
10 10 00
FF 01 00
05 00 00
80 00 00
Execution halted.
[exit 0]
+56 -30
View File
@@ -131,6 +131,22 @@ size() {
head -c 20 "$BUILD/$1.ppm" | sed -n '2p'
}
# ---- What a program said, as numbers ----
#
# With two things taken out that are not the program's: the cursor sequences the console
# generates to drive a host terminal, and the emulator's own halt line. Counting bytes from
# either end of the raw file worked until the console started announcing the cursor, and
# then quietly measured an escape.
said() {
python3 - "$BUILD/$1.out" <<'PY'
import re, sys
data = open(sys.argv[1], "rb").read()
data = re.sub(rb"\x1b\[[0-9;]*[A-Za-z]", b"", data)
data = re.sub(rb"Execution [^\n]*\n$", b"", data)
print(" ".join(str(byte) for byte in data))
PY
}
echo "Checking what the video device draws."
# ---- The machine wakes up able to show text ----
@@ -263,7 +279,7 @@ echo "Checking what the video device draws."
# The geometry is asked for rather than assumed, so a program can be written once and find
# out what it is running on.
{ prologue; show 0x32; show 0x33; epilogue; } | run geometry || exit 1
GOT="$(head -c 2 "$BUILD/geometry.out" | od -An -tu1 | tr -s ' ' | sed 's/^ //;s/ $//')"
GOT="$(said geometry)"
[ "$GOT" = "40 25" ] \
&& result ok "the ports say how big the screen is" "40 columns, 25 rows" \
|| result no "the ports say how big the screen is" "got \"$GOT\""
@@ -273,7 +289,7 @@ GOT="$(head -c 2 "$BUILD/geometry.out" | od -An -tu1 | tr -s ' ' | sed 's/^ //;s
# Not taken, and not fatal either. A screen is a poor place to stop the machine: a program
# that asked for something impossible still has the screen it had.
{ prologue; port 0x31 0x09; show 0x32; epilogue; } | run badmode || exit 1
GOT="$(head -c 1 "$BUILD/badmode.out" | od -An -tu1 | tr -d ' ')"
GOT="$(said badmode)"
[ "$GOT" = "40" ] \
&& result ok "an impossible mode is not taken" "still 40 columns" \
|| result no "an impossible mode is not taken" "got $GOT"
@@ -347,42 +363,52 @@ papered scrolled 2 1 \
&& result ok "the row that came into view is clear" "not what was there a ring ago" \
|| result no "the row that came into view is clear" "something at 2,1"
# ---- The sequences the corpus already speaks ----
# ---- Cursor registers, in place of a protocol ----
#
# 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. A controller that did not
# understand them drew "[2J" on the screen and left the board underneath, which is what Snake
# did the first time it was run in a window.
# The console used to be given escape sequences and parse them. It is not a terminal and the
# screen is not on the other end of a serial line, so it takes registers instead: rows and
# columns are written and READ BACK, which is the thing an escape sequence cannot do without
# sending a query and parsing a reply.
{ printf '#Program\nstart:\n'; say "A"; emit 27; say "[2J"; epilogue; } | run clearscreen || exit 1
papered clearscreen 2 1 \
&& result ok "ESC[2J clears the screen" "the letter is gone" \
|| result no "ESC[2J clears the screen" "still inked at 2,1"
{ printf '#Program\nstart:\n'; say "A"; port 0x05 0x01; epilogue; } | run clearcommand || exit 1
papered clearcommand 2 1 \
&& result ok "the clear command clears the screen" "the letter is gone" \
|| result no "the clear command clears the screen" "still inked at 2,1"
{ printf '#Program\nstart:\n'; emit 10; emit 10; say "A"; emit 27; say "[H"; say "A"
{ printf '#Program\nstart:\n'
emit 10; emit 10; say "A"
port 0x03 0x00; port 0x04 0x00
say "A"
epilogue
} | run home || exit 1
inked home 2 1 \
&& result ok "ESC[H goes back to the corner" "the second letter is at row 0" \
|| result no "ESC[H goes back to the corner" "nothing at 2,1"
inked home 2 17 \
} | run cursorhome || exit 1
inked cursorhome 2 1 \
&& result ok "the cursor goes where it is put" "row 0, column 0" \
|| result no "the cursor goes where it is put" "nothing at 2,1"
inked cursorhome 2 17 \
&& result ok "and leaves what was drawn alone" "the first is still on row 2" \
|| result no "and leaves what was drawn alone" "nothing at 2,17"
{ printf '#Program\nstart:\n'; emit 27; say "[3;5H"; say "A"; epilogue; } | run position || exit 1
inked position 34 17 \
&& result ok "ESC[3;5H puts the cursor there" "row 3, column 5, counting from one" \
|| result no "ESC[3;5H puts the cursor there" "nothing at 34,17"
{ printf '#Program\nstart:\n'; port 0x03 0x02; port 0x04 0x04; say "A"; epilogue
} | run cursorput || exit 1
inked cursorput 34 17 \
&& result ok "row and column are counted from zero" "row 2, column 4" \
|| result no "row and column are counted from zero" "nothing at 34,17"
# A sequence nobody implemented should leave no marks, which is what a real terminal does
# with one it does not know. Drawing it would be worse than ignoring it.
{ printf '#Program\nstart:\n'; emit 27; say "[9m"; say "A"; epilogue; } | run unknownseq || exit 1
inked unknownseq 2 1 \
&& result ok "an unknown sequence is swallowed" "the letter after it is at cell 0" \
|| result no "an unknown sequence is swallowed" "nothing at 2,1"
papered unknownseq 10 1 \
&& result ok "and leaves nothing behind" "cell 1 is untouched" \
|| result no "and leaves nothing behind" "something at 10,1"
# Readable, which is the point of them being registers. Three characters put the cursor at
# column 3, and asking says so.
{ printf '#Program\nstart:\n'; say "AAA"; show 0x04; show 0x03; epilogue; } | run cursorread || exit 1
GOT="$(said cursorread)"
[ "$GOT" = "65 65 65 3 0" ] \
&& result ok "and the cursor can be read back" "column 3, row 0" \
|| result no "and the cursor can be read back" "got \"$GOT\""
# A cursor asked to go off the screen has an obvious place to be, and stopping the machine
# over one would be a poor trade.
{ printf '#Program\nstart:\n'; port 0x04 0xFF; show 0x04; epilogue; } | run cursorclamp || exit 1
GOT="$(said cursorclamp)"
[ "$GOT" = "39" ] \
&& result ok "a cursor past the edge is clamped" "column 39, the last one" \
|| result no "a cursor past the edge is clamped" "got $GOT"
# And what scrolled off the top is still in the map, which is scrollback nothing had to keep.
{ printf '#Program\nstart:\n'