Files
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

74 lines
3.1 KiB
C

#ifndef ROT8_IO_H
#define ROT8_IO_H
#include "rot8/rot8.h"
void rot8_out_io(rot8_iter *it);
void rot8_inp_io(rot8_iter *it);
/* When muted, OUT puts nothing on stdout: a visualizer is drawing there. */
void rot8_io_set_quiet(bool quiet);
bool rot8_io_quiet(void);
/* Serial ASCII mode. With it on (and at least 8 bits per cell) OUT writes the
* low ROT8_SERIAL_BITS bits of the cell as one character, and INP reads one
* character into those same bits, leaving bit 7 and everything above it
* untouched -- so a tape can keep a flag, a length or a counter up there
* without the I/O noticing. Below 8 bits per cell there is no room for a
* character: leave the mode off. */
#define ROT8_SERIAL_BITS 7
#define ROT8_SERIAL_MASK ((rot8_data)0x7F)
/* Where the frame fields sit inside a cell in serial mode: bits 0..6 are the
* character, bit 7 the parity bit, bit 8 the stop bit. An 11 bit cell holds a
* whole 7E1 frame with two bits left over. */
#define ROT8_PARITY_BIT 7
#define ROT8_STOP_BIT 8
typedef enum {
ROT8_PARITY_NONE = 0, /* bit 7 belongs to the tape */
ROT8_PARITY_EVEN, /* 7E: bit 7 makes the number of 1s in 0..7 even */
ROT8_PARITY_ODD /* 7O: ... odd */
} rot8_parity;
void rot8_io_set_serial(bool serial);
bool rot8_io_serial(void);
/* True once OUT has actually written a character in serial mode; the CLI uses
* it to close the line it just put out. */
bool rot8_io_wrote_serial(void);
/* Parity and stop bit only mean something in serial mode. With parity on, INP
* writes bit 7 from the character it read and OUT *checks* it; with stop on,
* INP sets bit 8 and OUT checks it is 1. OUT keeps sending the low 7 bits
* either way, so a bad frame is reported but still goes out (like a UART that
* flags and keeps going). */
void rot8_io_set_parity(rot8_parity parity);
rot8_parity rot8_io_parity(void);
void rot8_io_set_stop(bool stop);
bool rot8_io_stop(void);
/* Frames whose parity or stop bit did not check out. */
unsigned long rot8_io_errors(void);
/* INP takes its value from the queue pushed here first, then from the hook,
* then from stdin. The queue is for scripts and tests (--input 1 2 3); the
* hook is for the live view, which has to stop and ask the person watching. */
typedef bool (*rot8_input_fn)(rot8_iter *it, void *ctx,
rot8_data *value);
void rot8_io_set_input_hook(rot8_input_fn fn, void *ctx);
void rot8_io_push_input(rot8_data value);
size_t rot8_io_pending_input(void);
/* Reads the instructions out of a tape into tape[0..size-1]. Returns false
* when the file cannot be opened; the tape is always NUL terminated. */
bool read_tape(const char *filename, char *tape, size_t size);
/* Reads a tape into a freshly allocated buffer sized to the file, so a tape is
* never silently truncated. Returns NULL when the file cannot be opened, holds
* no instructions, or is larger than ROT8_MAX_TAPE; *length is the instruction
* count and the caller frees the buffer. */
#define ROT8_MAX_TAPE ((size_t)16 << 20) /* 16 M instructions: roomy, not a stray binary */
char *read_tape_alloc(const char *filename, size_t *length);
#endif