mem: size the bytecode buffer from the tape, not from INS_SIZE

INS_SIZE was not a bracket table -- the run loop is the primitive for plus the
counter scan, as designed -- but rot8_ins_init allocated the *translated* tape
with a fixed 65536 entries, and rot8_run_hooked then wrote one entry per
instruction.  A 97307 character tape (examples/calc.r8asm assembled at cell 16)
therefore wrote 32000 entries past the end of the heap block and died with
0xC0000005, which is why the wide calculators could not be cross checked here.

rot8_ins_init now takes the length and mallocs it, rot8_run_hooked passes `step`,
and read_tape_alloc's guard is back to ROT8_MAX_TAPE (16 M instructions) instead
of INS_SIZE.  Measured:

  cell 11, 62585 chars   ->  > 0046
  cell 16, 97307 chars   ->  > 00046   (same as derive's 62 32 48 48 48 52 54 10)
  smoke unchanged: serial R8F, -n 512 1536 512

So the reference machine and both R8Forge machines now agree at a non default
width as well, which is the first time the three implementations could be
compared above 11 bits.
This commit is contained in:
dsh committed 2026-11-15 18:14:14 +08:00
1 parent e2faeba011
commit c18021bdf7
4 files changed
+8 -9

No files matched your search

+1 -1
View File
@@ -6,7 +6,7 @@ void rot8_mem_init(rot8_data **mem); /* the configured number of cells */
void rot8_mem_init_size(rot8_data **mem, size_t cells);
void rot8_mem_free(rot8_data *mem);
void rot8_ins_init(rot8_bytecode **ins);
void rot8_ins_init(rot8_bytecode **ins, rot8_ins_size length);
void rot8_ins_free(rot8_bytecode *ins);
#endif
+1 -1
View File
@@ -124,7 +124,7 @@ void rot8_run_hooked(char raw_ins[], rot8_ins_size step, rot8_hook hook,
rot8_data *mem;
rot8_iter it;
rot8_mem_init(&mem);
rot8_ins_init(&ins);
rot8_ins_init(&ins, step);
rot8_iter_init(&it, mem, ins);
it.ins_length = step;
it.hook = hook;
+4 -5
View File
@@ -204,11 +204,10 @@ char *read_tape_alloc(const char *filename, size_t *length) {
}
size = ftell(fp);
rewind(fp);
/* Anything longer than INS_SIZE is refused rather than truncated: rot8_run
* still has a fixed size assumption of its own, and a half tape silently
* produces a wrong answer (a 97k tape crashes it outright). Widening that
* is the next job; until then the caller gets a message, not a surprise. */
if (size <= 0 || (unsigned long)size > (unsigned long)INS_SIZE) {
/* The machine sizes its bytecode buffer from the tape now, so the only
* limit left is this one: roomy enough for any real tape, small enough to
* reject a stray binary. */
if (size <= 0 || (unsigned long)size > (unsigned long)ROT8_MAX_TAPE) {
fclose(fp);
return (NULL);
}
+2 -2
View File
@@ -22,8 +22,8 @@ void rot8_mem_init(rot8_data **mem) {
void rot8_mem_free(rot8_data *mem) { free(mem); }
void rot8_ins_init(rot8_bytecode **ins) {
*ins = malloc(sizeof(**ins) * INS_SIZE);
void rot8_ins_init(rot8_bytecode **ins, rot8_ins_size length) {
*ins = malloc(sizeof(**ins) * (length == 0 ? 1 : length));
}
void rot8_ins_free(rot8_bytecode *ins) { free(ins); }