The calculator now behaves like the terminal tape from the previous commit:
every character is echoed, backspace edits the digit window, Enter commits the
line and prints the next prompt, and '.' puts its result on its own line:
> 12 34 + .
0046
> 12<BS>3 .
0013
> ^D
It is not the terminal's line buffer moved over: the calculator consumes one
character at a time, and replaying a buffered line would mean unrolling the
engine once per buffer cell, which does not fit. Line editing here is the
digit window: backspace shifts it right by one, pads a '0' in front, echoes
"space, backspace" and keeps accumulating.
Three things had to be learned the hard way, all recorded in docs/calc.md ?8:
* a borrowed constant cell gets eaten. KBS (the 8 that backspace subtracts)
lived in SC+5, and the operator step erases SC..SC+6 wholesale, so the first
byte after an operator was treated as a backspace -- every character of
"12 34 + ." came out as an erase. It is now loaded right before each use.
It also must not borrow R: the EOF test writes R every round.
* set_1 only forces the low bit; cells lent to the classifier have to be
re-initialised each round rather than assumed clean.
* the prompt after Enter has to be printed before the command dispatch and the
operators run, because EH (the "this was Enter" flag) borrows SC+3, which
mul uses as scratch -- the first version printed a prompt after every
multiplication.
The tape went 64983 -> 73624 characters (+8.6k: three copies for the window
shift alone are ~4.4k, the two subtracts ~3k). That is past the old
ROT-8-viz INS_SIZE of 65536; the current VM sizes its buffer from the tape and
runs 112802 characters at 16 bits and 336304 at 32 bits, so only the
historical budget is gone, not the ability to run it. Ducking under 65536
again would mean dropping line editing: echo plus the Enter prompt alone is
about +2.5k, still over.
Verified: ctest 8/8 (examples_test checks the new header stream), the width
matrix at 11, 16 and 32 bits (calc stream, terminal tape and the viz's serial
output all agree; 73624 / 112802 / 336304 characters), and docs/calc.md records
step counts for the whole session list -- "12 34 + ." is 287589 steps, about
17% more than before the front end.
examples/term.r8asm was a per-character echo (prompt, echo, backspace erase,
newline, EOF). It is now a small shell: a 4 character line buffer, a backspace
that really removes a character, and Enter -- or EOF, for a half typed line --
commits the line and dispatches on its first character. 'l' prints the length,
'r' prints the buffer backwards, 'q' quits, anything else is ignored.
The header session "lhi" exercises the whole chain: EOF commits the pending
line, so examples_test sees the '3'. Three more sessions are documented in the
header: "rab" gives NUL+'b'+'a'+'r', "lab<BS>" gives '2' (the backspace really
dropped the 'b'), and "lhi<NL>rxy<NL>" prints '3' then the reversed second
line.
Three things ate most of the debugging time; all three are written up in the
file:
* "else" needs a flag cell. biz enters when the low bit is set, so "if not A
then B" cannot be inferred from "the last test missed": set FLAG=1 before the
test, clear it in the matched branch, look at FLAG afterwards. And biz tests
the CURRENT dp cell, so the order has to be "set FLAG, then .tp to the
condition cell, then biz" -- the wrong order made every character look like
a commit key.
* clb_sub_nc eats its operands, so the echo and the append both work from the
copy CE. The first version appended C (already zeroed by the comparison),
which left the line buffer empty: 'l' printed 0 and 'r' printed four NULs.
* the cell layout dominates the tape. copy / clb_sub_nc / clb_nonzero all emit
.tp internally, some of them inside %rep _CELL_SIZE, so a 20 cell gap costs
a couple of thousand characters: putting the constants at 22..28 gave 33067
characters, packing the hot cells into 0..27 gives 21721.
The buffer is left aligned with a length counter -- appending needs a dispatch
on LEN, but backspace is just LEN-- -- and 'r' prints all four cells instead of
dispatching on the length, which costs a NUL for the unused cells and is
documented.
tests/widths.ps1 now also checks term.r8asm at every width (its expected
stream is width independent, which is what makes it a good second program for
the matrix), and TODO/AGENTS/README record the item as done.
SPEC gains 7.10 with both contracts, the five design points and the measured
tape lengths; 5.4 stops saying "implementation pending" and spells out why the
range is asymmetric -- |N| can be outside the signed range while still fitting
unsigned, so 1024 has to print -1024 and the absolute value cannot be done by
clearing the sign bit.
Numbers re-measured on this machine, because some had drifted: the flagship is
62351 characters / 202159 steps (documented as 62513 / 206491), --opt takes it
to 56338, the calculator is 62624, the byte level test is 23 items over 22
macros, and asm/ has 55 macros in 23 files.
tests/widths.ps1 no longer compares the viz's serial output below 8 bit cells:
the viz prints one number per OUT there, so the 7 bit "viz FAILED" was the
script's expectation, not the tape -- the calculator itself passes at 7 bits
through r8forge_derive, and the same failure reproduces with the previous
calculator (checked against HEAD in a temp worktree). The TODO gains the open
question of whether the flagship should keep the 65536 limit at all, plus the
evidence that a 16 bit tape of 96052 characters runs fine in the viz.
The script used to build the toolchain per width, run the widthfree label and
print whatever the calculator produced. It now checks:
* widthfree ctest label passes;
* examples/calc.r8asm assembles, and `12 34 + .` prints exactly D digits where
D = floor(W * 30103 / 100000) + 1 -- the width's digit count, computed the same
way the library computes it. A build that silently ignored the width would
print four digits and fail here instead of looking green;
* if ROT-8-viz is built next door, it runs the same tape and prints the same
thing, which makes the three way check part of the matrix.
Also fixes the invocation: `-Widths` is a comma separated string now, because
`powershell -File ... -Widths 11,16` binds as one string and 11,16 became 1116.
Measured at cell 11 (the default): widthfree ok, tape 62585, calc ok
`out 62 32 48 48 52 54 10`, viz ok `> 0046`. Default list is 7,8,11,16,17,31,32;
each width is a full toolchain build, so the whole run is about twenty minutes.
The build option was plumbed all the way to the compiler (the generated vcxproj
carries R8FORGE_CELL_SIZE=17) but geometry.hh still said = 11, so every width
built an 11 bit toolchain -- which is why the first matrix run printed identical
tapes for all nine widths. My earlier '16 and 32 verified' claims were therefore
measurements of 11 bit binaries: synth_test and full_add are bit level blocks whose
tape does not depend on the width, so they never noticed.
Now at -DR8FORGE_CELL_SIZE=17: asm/test.r8asm 108789 characters (64513 at 11),
examples/calc.r8asm 112795 (62585), and one session prints '> 000046' -- D is 6 at
17 bits, which is exactly floor(17*log10 2)+1.
tests/widths.ps1 now prints the flagship and calculator tape lengths, because that
is the evidence that a width took effect; an unchanged 64513 means it did not.
The first run died at cell 1: with ErrorActionPreference=Stop a native command
whose stderr is captured through the pipeline becomes a terminating ErrorRecord,
so r8asm's harmless 'cached, 5 blocks' line killed the script. Native calls now
report through LASTEXITCODE and their stderr goes to \; the configure no
longer passes CMAKE_BUILD_TYPE, which the Visual Studio generator ignores anyway
(it printed a warning per width).
The desk calculator hardcoded a four cell window, which is only right at 11 bits:
clb_digits and clb_undigits use D = _CELL_SIZE * 30103 / 100000 + 1 digits (3 at 8
bits, 5 at 16, 10 at 32). D is now a .var, the window is REG..REG+D-1, every cell
after it moves by D-4 (zero at 11 bits, so the numeric map is unchanged), and the
startup fill, the shift chain, the flush reset and the D outputs are all %for
generated.
Two more width hardcodes turned up outside the window and are fixed: the C restore
after the four-ror bit 4 test was `%rep 7` (only 4+7=11 at the default), it is
`%rep _CELL_SIZE - 4` now -- which emits the same seven rors at 11 bits, so the
tape is byte for byte identical (62585 characters, every step count unchanged).
The literal descending shift was tried first and assembles fine but is wrong:
writing REG+D-2 before reading it clobbers the digit that is about to move, giving
"> 0350" for 12 34 + . instead of "> 0046". The chain goes ascending, which is the
exact generalisation of the old four copy chain.
tests/widths.ps1 builds the toolchain at 1 2 7 8 11 16 17 31 32 bits, runs the
widthfree label at each and, where the ASCII constants fit (>= 6), runs one
calculator session. 1 and 2 cannot: '0' is 48 and a two bit cell holds 3.
Verified at 11 bits: tape unchanged, all five sessions identical, derive agrees,
r8forge_examples_test 16/16.