a5dfd704998be0cd2a34fc3eb551d224ea1d7533
21
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
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.
|
||
|
|
c417304140 |
term: a line-editing terminal -- buffer, backspace and command dispatch
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. |
||
|
|
0c218abf32 |
calc: a 3 cell RPN stack -- nesting now works
The two register machine (A accumulator + P pending operand) could not nest:
"1 2 + 3 4 * + ." printed 0007 because the second operator overwrote the
first. The calculator now keeps a 3 cell stack (S0 is the top), operators run
immediately (pop two, push one), '.' is a real peek (repeated '.' shows the
same value, and it no longer flushes anything), and ',' clears both the stack
and the depth.
Depth is counted in DEPTH because the unary minus rule needs it: '-' is unary
iff the stack is empty. The previous "the previous character was an operator"
rule is wrong for a stack machine -- in "1 2 + -" the minus has to be a binary
subtraction (popping an empty cell yields 0, and 0 - top is exactly a
negation); treating it as unary would silently swallow the sign. The
"have == 0" rule from the original plan was already wrong for the old machine
("2 * -5" would lose its minus).
Sizing was driven by the tape: every push/pop is one copy per stack cell
(~500 characters), and the file has to stay under 65536. So 3 cells instead
of the 16 the TODO dreamed of: push is two copies (S2<-S1, S1<-S0), an
operator is two more (S1<-S2, then the result), and the new number is written
straight into clb_undigits' target cell (it erases N itself) so the extra
"result -> top" copy disappears -- that one was worth 540 characters. S2 sits
in cell 33, the freed OP cell: putting it at 51 cost 3.7k in walking alone.
64983 characters (was 62624) and 246142 steps for "12 34 + ." (was 280645 --
the old settle-the-pending-OP block is gone). The file header carries the
measured session list, the nesting cases, the stack move order (high to low,
or the source gets overwritten) and the older "2047 3 / ." caveat; docs/calc.md
gets the stack section, and SPEC 5.4/7.10 now point at DEPTH instead of NEED.
|
||
|
|
a30a19690b |
calc: signed display, signed division and a unary minus
'/' now goes through clb_divmod_signed and '.' through clb_digits_signed, so the accumulator can be negative: "3 5 - ." prints "> -0002", "-5 2 * ." prints "> -0010" and "-7 -2 / ." prints "> 0003". Add, subtract and multiply needed nothing -- mod 2^W arithmetic is already two's complement correct. The one new piece of state is NEED, "a new operand is expected": expression start, just settled, just after an operator, just after ','. A '-' with NEED == 1 is unary: set NEG, clear ISCMD (so step 3 leaves OP alone), and the next number is negated in the flush. NEED == 0 means P already holds an operand waiting to be settled, so '-' stays the binary minus. The original plan was "HAVE == 0 means unary", which is wrong: HAVE only says "digits are being accumulated", so after "2 *" it is 0 too and the minus in "2 * -5 ." would be parsed as a subtraction. Two consequences worth knowing: 2047 is now -1, so "2047 3 / ." correctly prints "> 0000" instead of the old unsigned 0682 (write "1023 3 / ." for 341), and the printable range is -1024..1023. The tape grows 59519 -> 62624 characters: the two wrappers plus the NEED/NEG judging. The file header carries the measured sessions with their step counts and the three cells the signed macros needed (T1 48, T2 49, SG 50), and examples_test still checks the "12 34 + ." line. |
||
|
|
1f3f453e88 |
signed: the two two's complement wrappers, with exhaustive sweeps
clb_digits_signed N REG S T F prints '-' (45) and then |N| as the same four zero padded digits; clb_divmod_signed N D Q R S T1 T2 F is the C style truncated division (q = trunc(n/d), r = n - q*d, remainder follows the dividend). Both are wrappers: clb_digits / clb_divmod still do the work, so the tape only grows by the sign handling -- 572 characters for the display (12151 vs 11579 in the same driver layout) and 1191 for the division (6018 vs 4827). The sign bit is read with rol + bit_copy + ror, 49 characters. clb_bit_mov is deliberately NOT used: it eats the source bit, so N comes back as N - HALF and 1024 -- whose magnitude 1024 only fits the unsigned range -- prints as 0000. A clb_sub_nc against a HALF constant was the other candidate: 765 characters for the call plus ~60 for the constant cell. A flag has to outlive the block that consumes it, because biz/rnz only exit once the body clears the condition bit. So sign(N) is moved into T2, sign(D) into F, and the remainder block flips F into sign(N)^sign(D) on its way out; a single conditional negation of the quotient then carries the xor. The negation scratch is S+6: the only cell in S with a "clean on entry" contract, and clb_neg_ip leaves it zero again for clb_divmod. Verified by r8forge_byte_op_test: all 2048 values for the display (worst 117606 steps) and 4202 division pairs -- every |a|,|b| <= 32 plus the 0/+-1/+-1023/+-1024 boundary combinations -- compared against C's / and %. examples/signed_demo.r8asm is the single point regression that runs as a program (the flagship cannot carry them: +12.1k / +5.6k on a 62351 tape). |
||
|
|
55ab5dfebd |
examples: the terminal tape -- prompt, echo, newline, backspace, EOF
examples/term.r8asm is the "type at it and it answers" program: it prints "> ", then
echoes every character the machine reads, prints a fresh prompt after each newline,
and answers a backspace with space + backspace so the cursor erases the glyph.
EOF (the 0 that inp returns when the input is gone) erases the loop quota, so the
loop ends and the tape runs off its end after one final newline.
tape 3467 characters, 4743 steps for a two line session (the calculator is 62585)
Measured (derive and tape_run, then the reference machine's serial output):
in hi out 62 32 104 105 10 "> hi\n"
in hi<LF>ok out 62 32 104 105 10 62 32 111 107 10 two prompts
in h<BS>i out 62 32 104 8 32 8 105 10 the 8 32 8 pair
All three character streams agree with ROT-8-viz byte for byte (bar CRLF).
r8forge_examples_test passes all 18 examples.
One thing worth recording: my brief for this said "a body entered on LSB==0 needs no
clear" -- that is backwards, `[` *jumps* on LSB==0, so bodies are entered on LSB==1.
Following it inverted the dispatch (EOF fired with C=0x68) and it took a
`tape_run --trace` to see it; the file now documents the real rule next to the loop.
|
||
|
|
f7ac81b06f |
asm: clb_puts and load -- printing a sentence, and the constant helper
clb_puts REG LEN prints LEN consecutive cells as characters (the low 7 bits, which
is what serial OUT sends). LEN has to be an assembly time constant: .tp takes a
literal address here, so "print until the cell is zero" is not expressible -- the
walk would leave the data pointer somewhere else. Each character costs two .tp
plus one out (three or four tape characters), uses no extra cell and leaves the
source cells alone.
load N V is the helper every driver was writing by hand (erase then .xor): .xor is
an exclusive or, so a second load without the erase would mix with the old value.
drv/puts.r8asm 198 chars, in-family check: out 72 73 33 72 (HI! plus the
untouched source cell)
examples/hello.r8asm 727 chars, out 72 69 76 76 79 44 32 82 56 33, and the
reference machine prints "HELLO, R8!"
Both are documented in SPEC 5.2; the macro count goes 48 -> 50 and the examples 16
-> 17. The flagship tape is untouched (64513) because asm/test.r8asm still has its
own local load -- same body, and lib's definition comes first, so nothing changed.
|
||
|
|
b7de3cad23 |
calc: the digit window follows _CELL_SIZE
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. |
||
|
|
b497bf0637 |
examples: the desk calculator -- 12 34 + . -> > 0046
The five stage drivers are now one program. examples/calc.r8asm is a two
register desk calculator: digits build a number, + - * / set the pending
operator, . prints the accumulator (`> 0046` + newline) and , clears it. EOF (the
0 that inp returns when the input runs out) erases the loop counter, so the loop
ends and the tape simply runs off its end.
in 12 34 + . out 62 32 48 48 52 54 10 (> 0046) 314777 steps
in 2047 3 / . out 62 32 48 54 56 50 10 (> 0682) 362281
in 7 6 * . out 62 32 48 48 52 50 10 (> 0042) 299352
in 9 4 - . out 62 32 48 48 48 53 10 (> 0005) 292030
both of the first two in one session 659759 steps
62585 characters, under ROT-8-viz's 65536 for the first time in this series, so
the whole calculator can be cross-checked against the reference machine later.
tape_run and derive agree, r8forge_examples_test passes all 16 examples, ctest 8/8.
Four deliberate departures from docs/calc.md, each forced by a measurement:
* settle before loading the new operand, and `.` settles too -- the literal
order printed 0034 for 12 34 + . because the space arrives before the + and
overwrites A; * the operator is one-hot (1 2 4 8) so settlement is four flat
mutually exclusive biz blocks; * the loop quota is refilled each round
(clb_ones can only make _CELL_SIZE ones, 11 here) instead of a fixed 128;
* three cell moves worth ~24k of tape: clb_undigits scratch 35..45 -> 5..15
(next to the window it decodes), result/display target 0 -> 20 (adjacent
scratch makes the %synth blocks shorter), 35..45 reused as one shot flags.
Also found a trap worth remembering: a biz body that ended with `.tp B1; set_0`
(condition cell is FLAG) has net displacement +2, and only on the skipped path
does it desynchronise every later .tp -- the symptom was `-` and `,` sending the
tape into the weeds. Net displacement 0 on both paths, always.
|
||
|
|
ae268b9442 |
Interactive I/O: teach the VMs INP, and let examples declare their input
The library can read characters now that the viz defaults to serial ASCII, but
R8Forge's own machines could not: r8forge_derive, r8forge_tape_run and the
examples harness all skipped ',' ("input is not modelled"), so an interactive
program could only be run through ROT-8-viz -- the wrong end of the dev loop,
and nothing in CI could cover it.
Three small pieces:
* INP (',') reads the next scripted character (low 7 bits) and 0 once the
text runs out, in tests/tape_run.cc, src/derive.cc and the examples VM.
r8forge_tape_run and r8forge_derive take -i/--input "<text>", so an
interactive program is run and checked exactly like any other:
r8forge_tape_run t.rot8 -i "12+34="
r8forge_derive calc.r8asm 0 -i "12+34="
* examples_test learns an optional `; in ...` header next to `; out`:
the characters after the marker are fed to INP before the run. The marker
must be the first word after ';' so "include" in a comment is not eaten.
* examples/echo_chars.r8asm: the smallest interactive program -- read four
characters, print them back. in R8F! -> out 82 56 70 33, 8 characters,
8 steps.
Verified the same tape three ways: tape_run -i "R8F!" -> 82 56 70 33,
derive 0 -i "R8F!" -> same, rot8_test --input R8F! -> prints R8F. With no
input at all INP reads 0, which is how every non-interactive example keeps
running unchanged.
Baseline: examples_test is now 15 examples + asm/test.r8asm.
|
||
|
|
c4048132cc |
asm/undigits: the inverse of clb_digits (ASCII decimal -> integer)
clb_digits could turn a number into four ASCII digits but nothing could turn
them back, so a program that stored or received a decimal string had no way
home. clb_undigits REG N S is that way back:
clb_digits N REG S ; 1234 -> 49 50 51 52
clb_undigits REG N S ; 49 50 51 52 -> 1234
Same number of digit cells as clb_digits writes (floor(W*log10 2) + 1, four at
11 bits), each read as its low 7 bits, so leading zeros are fine and garbage in
bits 7 and above is ignored. No validity checking, exactly like clb_digits: a
cell that is not '0'..'9' yields a deterministic but meaningless number, and the
documentation says so.
Horner in a runtime loop, one clb_mul in the whole tape:
N = 0 ; repeat 4: N = N*10 + digit ; shift the digit cells down one
9642 characters (minimum driver) -- smaller than clb_digits, because the reverse
direction needs no division.
Three things worth remembering, all found by testing rather than reasoning:
* c ^ 48 == c - 48 for '0'..'9' (the top two bits cancel, the low four are
identical), so subtracting '0' is one assembly time `.xor 48`: no
subtraction, no constant cell.
* The low-7-bit mask is `ror` 7, then set_0+ror per bit for the rest: the total
rotation is W, so the cell ends up facing the same way. The first version
wrote `rol; set_0; ror` in a %rep and cleared the same bit four times -- it
looks right and is undetectable on inputs whose high bits are already 0
(measured: 0|5<<7 came through as 688).
* The round counter must be erased before it is built: set_1 only clears the
bit it rotates to, so high ones left in the cell turn into extra rounds
(measured: dirty scratch gave 1234 -> 1564).
Tests and docs:
* r8forge_byte_op_test gains test_undigits: all 2048 values x {clean, garbage
in bits 7+} = 4096 cases, worst 36,481 steps.
* examples/undigits_demo.r8asm: "1234" -> 1234, "0007" -> 7, and a
digits -> undigits round trip on 2047; examples_test runs it like the rest.
* asm/test.r8asm calls it too (round trip inside the digits section). That
pushed the file over ROT-8-viz's 65536 instruction limit, so the redundant
second raw_add case, the reversed raw_sub case and the standalone divmod
section are gone -- every macro is still called at least once, and the file
is 64513 characters / 236439 steps / 93 outputs now.
* SPEC 7.9 (contract + the three traps), 10.2, 10.3 and 11, the AGENTS byte
table / verified list / naming table / baselines, and the README example
table all updated.
|
||
|
|
b4ea79ba91 |
%synth: the cost model is a run option, not a per block metric line
`metric chars|flat|blocks` inside %synth meant the same truth table could be
searched twice for two call sites, and every library block had to spell out
`metric flat` to avoid the default. The cost model belongs to the whole
assembly, so it is one now:
r8forge <file.r8asm> flat (layout free), the default
r8forge --synth-chars <file.r8asm> charge the real .tp walk
flat stays the default because a library block is expanded at dozens of call
sites with different layouts: only a layout free result can share one cache
entry, and it is usually within a few percent (full_add: 95 vs 90 characters).
The chars search is far more expensive (full_add: 415M states, ~21 minutes,
~10 GB), which is exactly why it should be asked for on the command line.
`metric blocks` goes with it: block count is a poor proxy for tape length (and
it was only another way to ask for mode layers).
Implementation: the metric lives in the assembler (ast::synth_metric, default
1) and is set through r8asm::set_synth_metric() before import_file; the CLI in
tests/asm_test.cc parses --synth-chars / --synth-flat. The cache key already
carried metric, so the committed entries still hit: assembling the library and
asm/test.r8asm gives byte identical tapes (28729 characters, byte_op_test all
green, all 13 examples green, synth_test still 8/8).
A `metric` line now fails with a message that names the flag instead of being
silently ignored. examples/full_add_flat.r8asm and full_add_flat_spaced.r8asm
are removed: they only existed to demonstrate the per block metric (the flat
default makes them copies of full_add.r8asm).
Also repairs the mojibake in README's metric section: an earlier tool wrote
\x0c / \x08 / \x0d where \f, \b and \r belonged, which ate characters out of
"flat", "blocks", "biz/flb/rnz" and "rnz". The section is rewritten for the
flag, and the %synth numbers in README/SPEC/AGENTS now match what the tree
prints.
New cache entry c16bf083bb0ccbc8.txt is full_add's stat table with mode layers
+ flat, which full_add_stat.r8asm now produces.
|
||
|
|
3f4149c9d0 |
examples: one runnable routine per macro group
SPEC is the contract and drv/ is what the tests sweep, but there was no place
that showed a reader how a group is actually used. examples/ now has one stand
alone program per group, each ending in output calls:
base_macros.r8asm S5 set_0 vs erase, rol/ror, copy vs bit_copy vs
clb_bit_mov, step
bit_adder.r8asm S6 4 bit ripple carry adder built from
clb_bit_add_nc, then bit_raw_add for the
preserving variant
byte_raw_ops.r8asm S7.1 raw_add/raw_inc/raw_and/raw_or/raw_xor/raw_sub,
printing the operands afterwards to show the
whole point of the family: they survive
byte_clobber.r8asm S7.2 clb_add/sub/neg/inc_nc (operands eaten) and the
S7.3 clb_and residue (240 & 204 -> B = 12)
byte_in_place.r8asm S8 not/inc/neg/dec/add_ip and clb_nonzero
sum_loop.r8asm S5.1 clb_ones + loop/endloop: 1+2+...+N, count from
data rather than from the assembler
mul_demo.r8asm S9 82*141 and 2047*2047
divmod_demo.r8asm S7.6 2047/10 and 2047/1 (the case that used to hang),
reusing the scratch block on purpose
digits_demo.r8asm S7.7 1234 and 7 -> four ASCII digits
Every header carries a `; out 1 2 3` line: the expected OUT stream, so the
file itself says what it prints. The new r8forge_examples_test target runs all
of examples/*.r8asm and compares that line with the real output (a file without
the marker counts as a failure), printing the tape length and step count on the
way. That makes examples the second place after byte_op_test that checks
behaviour instead of just "it assembles", and keeps the examples from rotting as
the macros change. The six %synth examples get the marker too: their inputs are
all zero, so they print 0 0.
All 15 examples pass. The values in the headers were checked with
r8forge_tape_run (the second interpreter) as well as the test's own machine.
Docs: SPEC S10.3 has the table and the convention, S11 gets the test row, and
README gets a short section plus the layout list. AGENTS records the examples
in the handoff baseline and the "verified" table.
|
||
|
|
e30725247e |
Delete the one bit per cell layer
It was written from a wrong diagnosis. Because the interpreters in this repo
read biz/rnz as a whole cell zero test, every packed byte program looked hung,
and the conclusion was "byte level arithmetic has to be one bit per cell".
With the interpreters fixed (
|
||
|
|
43390efe8a |
Byte level add, one bit per cell: 65536 combinations ok
The rotation based byte layer never ran. Measured with r8forge_derive -2:
raw_add A=0,B=0 / 1+1 / 3+5 / 1+0 out empty (step limit hit)
raw_add A=127,B=130 out 0 0 0 0 0, everything destroyed
raw_and 0xF0 and 0xCC, 1 and 1, 0 and 5 out empty
copy A=5 out empty (A=0 returns fine)
Not "only operand preservation is missing": set_0 (= [-]) cannot clear a cell
that holds more than one bit, and biz only answers "is this whole cell zero",
because ror does not change zero-ness. So the bits of a packed byte are
unreadable, and the whole ror + set_0 layer has no future. The docs now say
that instead of blaming operand preservation.
What replaces it is one bit per cell:
asm/byte_add.r8asm 8 bit adder. Bit i owns four adjacent cells at base+4i:
A, B, carry (in -> out), scratch. The synthesised four
cell slice bit_raw_add_nc does the bit, clb_bit_mov moves
the carry into the next quartet. A doubles as the
accumulator, so calling byte_add again continues the sum.
asm/pack_byte.r8asm eight flags -> one byte, the only direction that works.
bit_raw_flip_if consumes a flag into flipping bit 0 of an
accumulator that may already hold bits (no set_0 there).
examples/add8.r8asm a+b, 1308 characters. A shared carry cell layout needs
only 25 cells but was measured at 2517: paying .tp travel
eight times beats saving seven carry cells.
examples/add8_inc.r8asm add 1 three times, proving the block composes.
tests/byte_add_test.cc r8forge_byte_add_test: runs the tapes on the ROT-8
machine over every input pair, 0.3 s.
Also: AGENTS.md gains the measurement table, the third instruction set
constraint (packed bytes are unreadable), the new files, and the note that
build/ has to be rebuilt with the VS bundled cmake -- scoop's 4.4 reconfigures
it and dies with "No preprocessor test for PellesC".
|
||
|
|
b1a8ca5617 |
Unmangle the two flat examples
Full_add_flat.r8asm and full_add_flat_spaced.r8asm had their comments turned into question marks: they were written through a PowerShell Set-Content with ASCII encoding, which cannot represent Chinese and substitutes '?'. Rewritten as UTF-8, with the comments saying what the files are for. Both still assemble from cache (95 characters) and still produce the same tape, which is the point of the layout free metric. A scan of the whole tree for '???' now comes back empty. |
||
|
|
da16c111cf |
Add a layout independent metric so a result can be cached and reused
%synth could optimise the real tape characters, but that price depends on where the cells sit, so the answer was only good for one layout and the cache could only be shared between call sites with the same addresses. metric chars tape characters including the .tp walk layout dependent metric flat characters without the walk layout independent metric blocks block count layout independent 'flat' is the step-free mode: it counts biz/flb/rnz and the ops, ignores how far apart the cells are, so one search result and one cache entry serve every layout. The price is a slightly longer tape when the layout happens to suit a different program: 90 characters for chars, 95 for flat on the full adder. Checked: examples/full_add_flat.r8asm hits the cache on the second run, and the same block with 'layout 0 2 4 6 8 10' added hits the *same* entry and emits a byte-identical tape (the key drops the layout for the layout-free metrics). Both pass the eight combination check. |
||
|
|
9a760f2a41 |
Drop the inputs/outputs declarations: the table already says it
A synth block used to spell out which parameter is an input and which is an output. That was redundant: with map, 2^K entries already mean %1..%K are the inputs and the value's bits land on the parameters after them; with a raw table, nothing needs saying at all. So 'inputs' and 'outputs' now fail with a message pointing at map/stat, and the map form derives both. Added 'stat' as the fully explicit form: 2^params values, one per input combination, which is exactly the state table the search consumes. examples/full_add_stat.r8asm is the same adder written that way and produces a byte-identical tape to the map version (95 characters), both pass the eight combination check. |
||
|
|
6f2ecf0e22 |
Add a mode switch and a getting-started section
%synth used to always run both searches, so every assembly of the full adder sat there for twenty minutes. A block can now say 'mode layers' to stop after the layer search (7.7 seconds) or 'mode tape' (the default) to pay for the shortest tape. examples/full_add_layers.r8asm is the fast one, 95 characters and still checked on the machine. The lexer also no longer chokes on a UTF-8 BOM. Every Windows editor writes one, and the first token came out as a garbage prefix, which crashed the assembler with an access violation rather than saying anything useful. README grew a getting-started walkthrough: build, a complete adder.r8asm, what the two stderr lines mean, how to drop a synth block into a library, and the three things that bite (inputs first, don't call them in/out, map length). |
||
|
|
548c239a85 |
Tape-length search beats the layer search on the full adder
Five layers cost 95 characters; six layers cost 90. Both are in examples/ and
both pass the eight-combination check on the ROT-8 machine in
tests/synth_test.cc (which now takes a file argument).
sacrifice B, FLIP {S,T}
sacrifice A, FLIP {B}
sacrifice B, FLIP {A,Ci,S}
sacrifice Ci, FLIP {B,Co,T}
sacrifice S, SET1 {Ci,Co}
sacrifice T, FLIP {B,Ci,S,Co}
Cost 75 in the model, 90 on the tape once the inter-block hops and the two
output lines are counted. The goal was popped at 75 only after everything
below 75 had been expanded, so 75 is the minimum of this cost model.
Trading a layer for a shorter tape is not a contradiction: the layer search
cannot see that a small, cheap block can be worth more than one wide, expensive
one.
|
||
|
|
19cbfb36f6 |
R8Forge: assembler and searcher in one repo, %synth directive
The searcher stops being a separate program: %synth is an assembly-time directive. It describes a truth table, the assembler runs the search while parsing, and the block list comes back as ready-to-paste r8asm. Merging is what makes the cost model honest. The price of a layer is cost = 2 (biz + rnz) + sum(move distance + op cost) and the move distance depends on the call site's cell layout, not on anything the assembler knows in advance: the same five-layer adder is 87 characters with the cells adjacent and 136 with them two apart. Only the side that sees the layout can price it, which is now the assembler itself. Working: the assembler (asm/test.r8asm still assembles to the same 1714 characters), the searcher (bit planes + backward one-step lookup, full_add in 5 layers, proven shortest in 7.2s), and %synth parsing plus the search itself. Not working yet: splicing the synthesised body into the tape -- a synth call fails loudly with 'unknown macro' instead of emitting anything, so nothing can silently assemble wrong. The two ways to finish it are in the README. Also carries over the CMake fix: no set(CMAKE_CXX_FLAGS ...), which used to drop /EHsc, push -O3 into Debug and fight /RTC1. |