Files
R8Forge/tests/widths.ps1
T
dsh 5c414292d2 calc: a terminal front end -- echo, backspace editing and a prompt
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.
2026-11-19 14:27:30 +08:00

96 lines
5.3 KiB
PowerShell

# Build the toolchain at a set of cell widths and *check* what should hold there.
#
# powershell -File tests\widths.ps1 # 7,8,11,16,17,31,32
# powershell -File tests\widths.ps1 -Widths 11,16,32
#
# Per width, this asserts:
# * the `widthfree` ctest label passes (bit level slices, the synthesizer, the
# looping demo, the macro comment format);
# * examples/calc.r8asm assembles and `12 34 + .` prints D digits, where
# D = floor(W * 30103 / 100000) + 1 -- the same formula the library uses. A
# width that silently did not take effect fails this, because it would print
# the wrong number of digits. Since the calculator grew its terminal front
# end, the stream is also the echoed input plus a prompt on either side
# ("> 12 34 + .\n0046\n> \n"), so this checks the echo and the prompt too;
# * if the reference machine is built next door, it prints the same thing
# (`rot8_test --cell <W>`), which is the three way check. That one only
# runs for W >= 8: the viz switches to serial ASCII only when a cell is at
# least 8 bits wide, and below that it prints one number per OUT -- so a
# "> 12 34 + ." comparison at 7 bits would fail for a reason that has
# nothing to do with the tape (the calc itself is still checked there).
# Widths under 7 cannot hold '0' (48) in a cell, so they get the library tests
# only.
# A comma separated string rather than [int[]]: `powershell -File ... -Widths 11,16`
# binds as one string, and 11,16 would quietly become 1116.
param(
[string]$Widths = "7,8,11,16,17,31,32",
[string]$CMake = "C:/Program Files/Microsoft Visual Studio/18/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/bin/cmake.exe"
)
$ErrorActionPreference = "Continue"
$root = Split-Path -Parent $PSScriptRoot
$ctest = Join-Path (Split-Path $CMake) "ctest.exe"
$viz = Join-Path $root "..\ROT-8-viz\build\Release\rot8_test.exe"
$failed = @()
foreach ($w in ($Widths.Split(",") | ForEach-Object { [int]$_.Trim() })) {
$dir = Join-Path $root "build$w"
$d = [int][math]::Floor($w * 0.30103) + 1
# calc: prompt, the echoed "12 34 + .", newline, the D digit answer, newline,
# the next prompt, and the final newline.
$nums = @(62, 32) + @(49, 50, 32, 51, 52, 32, 43, 32, 46) + @(10) +
(@(48) * ($d - 2)) + @(52, 54) + @(10) + @(62, 32) + @(10)
$want = "out " + ($nums -join " ")
Write-Host "=== cell $w (D = $d)"
& $CMake -S $root -B $dir "-DR8FORGE_CELL_SIZE=$w" 2>$null | Out-Null
& $CMake --build $dir --config Release 2>$null | Out-Null
& $ctest --test-dir $dir -C Release -L widthfree 2>$null | Out-Null
if ($LASTEXITCODE -ne 0) { Write-Host " widthfree tests FAILED"; $failed += $w }
else { Write-Host " widthfree tests ok" }
$r8 = Join-Path $dir "Release\r8forge.exe"
$derive = Join-Path $dir "Release\r8forge_derive.exe"
$tape = (& $r8 "examples/calc.r8asm" --quiet 2>$null | Out-String).TrimEnd("`r", "`n")
Write-Host (" calc tape: " + $tape.Length + " chars")
$got = (& $derive "examples/calc.r8asm" 0 -i "12 34 + ." 2>$null | Out-String).Trim()
if ($got -ne $want) { Write-Host " calc FAILED: want '$want' got '$got'"; $failed += $w }
else { Write-Host " calc ok: $want" }
# The terminal tape: prompt, echo, EOF-commit and the 'l' command. None of
# these numbers depend on the cell width (the length is printed as '0'+LEN),
# so the expected stream is the same at every width -- which is exactly what
# makes it a good second program for the matrix.
$termWant = "out 62 32 108 104 105 10 51 10 62 32 10"
$termGot = (& $derive "examples/term.r8asm" 0 -i "lhi" 2>$null | Out-String).Trim()
if ($termGot -ne $termWant) { Write-Host " term FAILED: want '$termWant' got '$termGot'"; $failed += $w }
else { Write-Host " term ok: $termWant" }
if (Test-Path $viz) {
# The viz only speaks serial ASCII when a cell is at least 8 bits wide
# (bit 7 doubles as the 7E1 parity/stop bit; below that it prints one
# number per OUT). Comparing a 7 bit tape with "> 046" therefore says
# nothing: at 7 bits the same tape legitimately prints numbers. The
# calc check above already covers 7 bits, so skip the viz there.
if ($w -lt 8) {
Write-Host " viz skipped: serial I/O needs >= 8 bit cells"
} else {
$tmp = Join-Path $env:TEMP "calc$w.rot8"
[System.IO.File]::WriteAllText($tmp, $tape, (New-Object System.Text.UTF8Encoding($false)))
# The native side prints CRLF; compare normalized text.
$serial = ((& $viz "--cell" $w "--input" "12 34 + ." $tmp 2>$null | Out-String).Trim()) -replace "`r`n", "`n"
# Same stream as a display would show; Trim() eats the prompt's
# trailing space, so the expected text ends with '>' alone.
$wantSerial = "> 12 34 + .`n" + ("0" * ($d - 2)) + "46`n>"
if ($serial -ne $wantSerial) { Write-Host " viz FAILED: want '$wantSerial' got '$serial'"; $failed += $w }
else { Write-Host " viz ok: $wantSerial" }
Remove-Item $tmp -ErrorAction SilentlyContinue
}
}
Remove-Item -Recurse -Force $dir
}
if ($failed.Count -gt 0) { Write-Host ("FAILED at widths: " + ($failed -join ", ")); exit 1 }
Write-Host "all requested widths ok"