24 Commits
Author SHA1 Message Date
dsh 14b3acf66b live: a run of pointer moves is one frame, not one per cell
The live view drew a frame per instruction, so watching clb_digits walk its 13 cell
scratch block meant dozens of frames of "pointer moved one cell".  Now a run of
STP/BTP closes into a single frame whose status line says where the pointer went
(`ptr +5 cell 91`), and `--skip-moves` does not render moves at all, only counts
them until the next data instruction.

Both are view-only: src/rot8.c, rot8_mem.c, the instruction behaviour, the step
count and the tape reader are untouched (git diff shows rot8_viz.c, rot8_viz.h --
one bool -- and vm_test.c's option parsing).  The live path cannot be compared byte
for byte with a non-live run, because live mode sets quiet + replay_output=false on
purpose (the frames are the output); the checks are therefore:

  * non-live stdout before and after: `--input R8F tapes/serial_echo.rot8` -> R8F,
    the calculator tape at cell 11 with `12 34 + .` -> "> 0046" -- both unchanged;
  * executed instructions: 280616 for that calculator run, identical to before;
  * frames at -e 1 on that tape: 280616 -> 104423 (-63%) draws, and 65234 (-77%)
    with --skip-moves; 215382 of the 280616 instructions are moves, in 39189 runs;
  * at the default auto rate (every=1592 here) the draw count is 627 either way: a
    frame that falls due inside a run is deferred to the run's end, never dropped.

ROT8_VIZ_FRAMES=1 makes a run print `draws/steps/moves/runs` to stderr, which is how
the numbers above were taken; it is documented in the README next to --live.
2026-11-18 19:21:00 +08:00
dsh c3ab2f1ac0 viz: size the tape buffer from the file, and refuse a too long tape loudly
read_tape filled a fixed INS_SIZE buffer and stopped at the limit without saying
anything, so a longer tape ran half a program and produced a plausible wrong
answer (a 112797 character calculator tape from a 17 bit build, for instance).

read_tape_alloc now allocates from the file size, filters the instruction
characters and returns the length, so nothing is ever cut in half.  Verified the
short tapes are unaffected (the smoke checks still pass: serial R8F, -n
512 1536 512, 7E1 A).

It also found the next wall: a 97307 character tape **crashes** the machine with
0xC0000005 (access violation) even though rot8_ins_size is uint32_t -- rot8.c
still has a fixed size assumption of its own somewhere.  Until that is found and
widened, read_tape_alloc refuses anything above INS_SIZE and the CLI says so,
which is strictly better than the silent truncation.  TODO gets the rest.
2026-11-15 17:51:20 +08:00
dsh a083a3422c I/O: serial ASCII by default, numeric I/O behind -n/--numbers
The library can now print decimal digits (ROT-8's clb_digits/undigits), so a tape
usually wants characters out -- and serial was the one mode you had to remember
to ask for.  Flip that around:

    rot8_test tape          serial ASCII (cell >= 8 bits)
    rot8_test -n tape       the old decimal numbers

The default follows the geometry: a serial character needs 8 bits (7 data + 1
spare), so a 1..7 bit cell gets the numeric I/O instead -- 1-bit tapes keep
working with no extra flag.  Asking for -s/--serial (or --parity/--stop/--7e1) on
such a cell is still an error (exit 2), and -n together with a serial option is
now rejected as contradictory (exit 2).

--input follows the mode: text in serial, numbers with -n.  It was already
deferred until the whole command line was parsed, so order does not matter.

Docs: the README's I/O section is rewritten around the new default, the option
table gains -n/--numbers, the examples that feed numbers now pass -n, and the
INP/OUT note at the top explains the two modes.  --7e1 is unchanged apart from
being an explicit frame on top of a default that is already serial.

Also fixes the usage() printf: the examples had seven %s and only six program
arguments, one short (a stale bug, now matched up).
2026-11-09 16:25:02 +08:00
dsh 754f885a64 7E1 framing in serial mode: bit 7 parity, bit 8 stop
--7e1 is --serial --parity even --stop.  One cell carries a whole frame:
bits 0..6 the character, bit 7 the even parity bit, bit 8 the stop bit, which
leaves two bits of an 11 bit cell to the tape.

  * --parity none|even|odd: INP fills bit 7 from the character it read, OUT
    checks it.  --stop does the same for bit 8 being 1.  Both imply --serial,
    and a stop bit needs cell_size >= 9 (7+1+1).
  * OUT still sends the character when a frame is bad, like a UART flagging and
    carrying on, but says 'parity error' / 'framing error' on stderr, prints a
    summary at the end and exits 4, so a script can test the frame with \True.
  * The 7E1 round trip is checked: --7e1 --input A leaves 0x141 = 321 in the
    cell (0x41 data, parity 0 because 'A' has two bits set, stop 1) and OUT
    accepts it; flipping one data bit gives a parity error, and outputting a
    cell with bit 8 clear gives a framing error.
  * README: the frame layout, the switches, why the stop bit is one cell bit
    (no bit clock: one INP/OUT is a whole frame) and what parity costs in
    hardware -- one latching toggle driven by the read head, and the very same
    circuit checks a frame if you feed it all 8 bits and expect 0.
2026-11-07 16:14:30 +08:00
dsh 593e1c3b70 Serial ASCII mode: OUT writes one character, INP reads one
-s / --serial switches INP and OUT from decimal numbers to characters.  The
character is the low 7 bits of the cell, so the 8th bit stays the tape's own:
INP writes bits 0..6 and leaves everything from bit 7 up untouched, which is
where a flag, a length or a counter can live without the I/O trampling it.
Checked by hand: a cell preset to 0x80 that then reads 'A' ends at 0xC1, at 8
and at 11 bits per cell.

  * include/rot8/rot8_io.h, src/rot8_io.c: the mode itself, plus
    rot8_io_wrote_serial() so the CLI can close the line (a serial OUT writes
    no newline, that is the point); stdin is read as characters as well, and
    EOF feeds 0 instead of blocking.
  * src/rot8_viz.c: the frame shows characters in this mode -- the out/in
    boxes list them, the console prints output> 'R' 82, unprintables become
    \x0A so a frame stays one line of ASCII, and the live prompt takes the
    first character typed instead of parsing a number.
  * tests/vm_test.c: -s/--serial, a cell_size < 8 check (a 7 bit character
    plus a flag does not fit in fewer bits), --input becomes the text itself
    (one INP per character, which is why the arguments are now collected and
    pushed after the whole command line is known), and one trailing newline
    at the end because serial OUT has none.
  * tapes/serial_echo.rot8: ,.,.,. -- read three characters, echo them.
  * README: what the mode does, the bit 7 rule, and three ways to run it.

Decimal mode is unchanged: --input 7,5 still prints 7 and the existing demos
still print their numbers.
2026-11-07 15:58:06 +08:00
dsh c298bdca3b Rename the cell width to cell_size
The geometry was half renamed: mem_size was already lowercase, but the width
was still cell_bits, ROT8_DEFAULT_CELL_BITS and ROT8_MAX_CELL_BITS.  It is now
cell_size / ROT8_DEFAULT_CELL_SIZE / ROT8_MAX_CELL_SIZE, which is also what
R8Forge calls it on the C++ side, so one number has one name on both machines.

No behaviour change: 19 cell_bits, 4 ROT8_DEFAULT_CELL_BITS and 6
ROT8_MAX_CELL_BITS across include/rot8/rot8.h, src/rot8.c and tests/vm_test.c.
The CLI option stays --cell <bits>, because a bit count is what it takes, and
rot8_cell_width() keeps its name for the same reason: it answers "how wide",
it is not the geometry field.

Drive-by: the comment above parse_geometry claimed --cell only takes 8, 16 or
32; it accepts 1..ROT8_MAX_CELL_SIZE, and the README says so too.

Checked: the default geometry (11 bit cells, 259 cell tape) still runs
R8Forge's hash tape to 1322 1786 1600 5 94 127, which is exactly what the
assembler's own derive prints, and --cell 16 / --cell 8 --mem 256 still work
with the view laying itself out from the width (bits b15 .. b0, four hex
digits per cell).
2026-11-07 12:29:04 +08:00
dsh 5a23ffd64d Any cell width, any tape length, and the frame follows both
The previous commit still treated the geometry as a small menu: 8/16/32 bits and
a grid whose column count was hard wired per width.  ROT-8 does not work that
way -- the tape is a ring of any length and a cell is a ring of any width -- so
both are open now:

    --cell 1 .. 32     (any value, not just 8/16/32)
    --mem  1 .. 1048576 (any value, not just powers of two)

What had to change for that:

* A cell needs ceil(bits/4) hex digits, at least one, so a 1 bit cell prints
  "0x1" instead of nothing and a 12 bit cell prints three digits.
* The number of cells per dump row is computed from the space the gutter leaves
  (VIZ_INNER - addr_digits - 5) divided by one cell's field width, capped at 16.
  Every geometry therefore lays out inside the 56 character frame: 16 columns at
  8 bits, 9 at 16, 5 at 32, 12 at 12 bits, and a ragged last row just ends early.
* rot8_config_apply accepts any cell_bits in 1..32 instead of three values.

Live mode needed nothing beyond that, but it is worth stating why it stays
stable: the frame height depends only on the geometry -- rows are
min(ceil(cells/cols), 16), the window line exists exactly when the tape is taller
than that, and the caret row is always drawn because the pointer is always on the
tape (it is a ring).  So every frame of a run is the same height and the in place
redraw cannot smear.  Width is capped at 58 characters for the same reason.

Checked: --dump for and, or, full_adder, wave and test is still byte identical to
the build before any of this, and -++++ gives 0x10 / 0x1000 / 0x100 / 0x10000000
at 8 / 16 / 12 / 32 bits.
2026-11-07 10:38:56 +08:00
dsh 4ae4bea2dd Tape length and cell width are chosen at run time
MEM_SIZE and CELL_SIZE were compile time constants: 256 cells of 8 bits, with
rot8_data pinned to uint8_t.  Both now come from a rot8_config that a machine
takes a copy of when it is created:

    rot8_config cfg = {4096, 16};   /* cells, bits per cell */
    rot8_config_apply(&cfg);        /* machines from now on */

    rot8_test --mem 4096 --cell 16 --dump tapes/wave.rot8

8, 16 and 32 bit cells are supported.  ROR rotates inside the configured width
(four rotations of a set bit give 0x10, 0x1000 and 0x10000000), FLB, BIZ and RNZ
still work on the low bit, and INP masks what it is given (70000 becomes 4464 in
a 16 bit cell and 112 in an 8 bit one).  rot8_data is uint32_t now so that a
cell holds the widest case.

The tape is a ring: stepping off either end lands on the other, which is what
makes a short tape safe to watch and matches what ROR already does inside a
cell.

The view follows the geometry.  A cell takes 2, 4 or 8 hex digits and a row
holds 16, 8 or 4 of them, so the frame stays 56 characters wide; wider cells get
a nibble grouped bit string instead of one column per bit.  A tape longer than
16 rows is drawn as a window around the pointer, with its range on the last
line.

Nothing changes for the default geometry: --dump output for and, or,
full_adder, wave and test is byte identical to before, and so is plain OUT
output.  The places that printed a cell with %d/%u print unsigned long now, so a
32 bit cell cannot come out negative.
2026-11-07 10:38:56 +08:00
starcells 5783769db3 Console: records in order, prompt last, no duplicate replay
Three fixes from looking at a real run:

  * input> showed only the last value.  Both console lines now record every
    value (with the same '..' trim as the out box), so a run that read 123 then
    65 shows input> 123 65.
  * output> was printed above input> even though the output happened later.
    The order is now chronological: input record first, output record second,
    and the prompt is a separate empty 'input> ' line under both, so the cursor
    is always the very last thing and nothing can appear above it afterwards.
  * with the console showing output>, replaying every OUT value after the run
    was just noise: replay_output is off now (live still mutes OUT while the
    frame is being drawn, so the values only appear in the console).

Checked with tapes/echo2.rot8 (',.,.'), fed 123 then 65 through a pipe: while
waiting for the second value the console reads 'output> 123' then an empty
'input> ', and at the end 'input> 123 65' / 'output> 123 65' with nothing
printed after.  --input and --dump paths unchanged.
2026-11-06 20:52:25 +08:00
starcells 6475938e6c Add a place to give the machine its input
INP read straight from stdin with scanf, which the live view cannot offer: the
view owns the terminal, and a tape that wants input either ate a number nobody
typed or blocked on a pipe.

INP now takes its value from, in order:

  1. a queue filled by --input "7,5" (spaces or commas, scripts and tests)
  2. a hook the live view installs: it prints 'input> ' under the frame and
     reads a line, redraws on the next tick, and feeds 0 when stdin is done so
     a tape behind a pipe still finishes
  3. stdin, exactly as before

So nothing changes for a plain run, and --live can drive programs that need
input.  tapes/echo.rot8 (just ',.') is the smallest tape that exercises it.

    rot8_test --input 42 tapes/echo.rot8        -> 42
    rot8_test --live tapes/echo.rot8            -> asks under the view

Tests: --input with one value, with a list, a live run fed through a pipe
(prints 5), and a live run with stdin already closed (feeds 0, terminates).
The --dump path and the untouched tapes (and, or, full_adder, wave) still work.
2026-11-06 20:35:30 +08:00
starcells bda173b078 Make the live view work behind a pipe, and fit small terminals
A terminal can be a pipe: web terminals and IDE panels hand the program a
pipe for stdout, so isatty() says no, --live quietly fell back to printing
one frame after another and -f was ignored.  Add -t to say "there is a
terminal at the other end anyway" (and --no-tty for the opposite), and say
so on stderr when the fallback happens, so the flag can be found.

Frames no longer end with a newline, so a frame of N lines needs N rows
instead of N+1 and a 24 row terminal shows the whole compact frame.  The
size heuristics follow from that: shorter than 30 rows -> compact, size
unknown (behind a pipe) -> compact, LINES is honoured when the console
cannot be asked, and --full forces the tall frame.

Colour is now only switched off for a real console that cannot do VT, not
for every stream that is not a console: behind a pipe the far end may be
perfectly capable.
2026-11-05 21:48:58 +08:00
starcells b654bd644d Add a live memory view for the tape
The machine can now hand a hook the whole execution, and rot8_viz draws
what it sees: the 256 cell tape as a hex map, the cell under the pointer
with its eight bits, the program counter and the values OUT produced.

    rot8_test --live tapes/wave.rot8     # redraw in place while it runs
    rot8_test --dump tapes/and.rot8      # one static snapshot at the end

Frames are plain ASCII, so the view survives pipes, ssh and logs; colour
is ANSI and only used when the terminal can take it.  Running without
--live or --dump behaves exactly as before: the tape just runs.

rot8.h gains rot8_run_hooked() plus read-only accessors for the tape,
the program counter and the step count, so that tools do not have to
reach into rot8_iter.  read_tape() now reports a missing file and stops
at the end of the buffer instead of writing past it.  Also fixes the
ROR/FLB opcode comments, which had the BF characters the wrong way round.

tapes/wave.rot8 is a small demo: it walks a single set bit across 240
cells, which looks like a wave moving over the tape.
2026-11-05 21:25:42 +08:00
starcells 5a8cadcd9a 202606281508 2026-06-28 03:08:40 +08:00
starcells f29a644419 Safe OR. 2026-06-12 08:28:47 +08:00
starcells 004f13b795 [fix]202605160034 2026-05-16 00:35:11 +08:00
starcells c6cce2a9a5 Input completed. 2026-05-04 16:00:49 +08:00
starcells d7de27a54b [err]202605041103 2026-05-04 11:03:59 +08:00
starcells d4b16d3030 Standarized variable types. 2026-04-18 22:56:24 +08:00
starcells 954ed0828d 202604082015 2026-04-18 20:15:58 +08:00
starcells fd48b9b927 Fixed test. 2026-04-10 01:48:51 +08:00
starcells cfa296ee8f Restored test. 2026-04-09 22:16:48 +08:00
starcells c16c4d3e53 Changed RET to RIZ. 2026-04-09 18:01:27 +08:00
starcells 045eb4332c Added clang-format file. 2026-04-07 17:57:54 +08:00
starcells 68bfe71cd6 Formatted all files,adjusted cmakelists *2 2026-03-27 23:52:48 +08:00