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).
94 lines
3.7 KiB
C
94 lines
3.7 KiB
C
#ifndef ROT8_H
|
|
#define ROT8_H
|
|
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
/* Geometry. A tape is mem_size cells and a cell is cell_size bits wide; the
|
|
* defaults are 259 x 11 (they were the historical 256 x 8). Both are picked
|
|
* at run time (rot8_config_apply), so one binary can watch a 259 x 11 tape and
|
|
* a 4096 x 32 one. rot8_data is always wide enough for the largest cell. */
|
|
#define ROT8_DEFAULT_MEM_SIZE 259
|
|
#define ROT8_DEFAULT_CELL_SIZE 11
|
|
#define ROT8_MAX_CELL_SIZE 32
|
|
#define ROT8_MAX_MEM_SIZE (1u << 20)
|
|
#define INS_SIZE 65536
|
|
|
|
typedef uint32_t rot8_data;
|
|
typedef uint8_t rot8_loop_depth;
|
|
typedef uint32_t rot8_ins_size;
|
|
|
|
#define ROT8_BC_TO_CHAR(bc) ("><+-[].,"[(bc)])
|
|
|
|
typedef enum {
|
|
STP, // Step Tape Pointer,'>'
|
|
BTP, // Back Tape Pointer,'<'
|
|
ROR, // ROtate Right,'+'
|
|
FLB, // Flip Last Bit,'-'
|
|
BIZ, // Branch If Zero,'['
|
|
RNZ, // Return Not Zero,']'
|
|
OUT, // OUTput byte,'.'
|
|
INP, // INPut byte,','
|
|
} rot8_bytecode;
|
|
|
|
typedef struct rot8_iter rot8_iter;
|
|
|
|
/* Tape geometry. A machine copies these when it is created, so changing them
|
|
* later only affects machines made afterwards. */
|
|
typedef struct rot8_config {
|
|
size_t mem_size; /* cells on the tape */
|
|
unsigned cell_size; /* bits per cell: 8, 16 or 32 */
|
|
} rot8_config;
|
|
|
|
void rot8_config_default(rot8_config *cfg);
|
|
/* Make cfg the geometry for machines created from now on. Returns false and
|
|
* changes nothing when the numbers are out of range. */
|
|
bool rot8_config_apply(const rot8_config *cfg);
|
|
const rot8_config *rot8_config_current(void);
|
|
/* Mask that keeps the low cell_size of a value. */
|
|
rot8_data rot8_cell_mask(unsigned cell_size);
|
|
|
|
/* Called once after every executed instruction (it already points at the
|
|
* next one). ctx is whatever rot8_run_hooked was handed, so a debugger or
|
|
* a visualizer can carry its own state without touching the machine. */
|
|
typedef void (*rot8_hook)(rot8_iter *iter, void *ctx);
|
|
|
|
void rot8_iter_init(rot8_iter *iter, rot8_data *mem, rot8_bytecode *ins);
|
|
/* Same, with an explicit geometry instead of the default one. */
|
|
void rot8_iter_init_cfg(rot8_iter *iter, rot8_data *mem, rot8_bytecode *ins,
|
|
const rot8_config *cfg);
|
|
// void rot8_iter_free(rot8_iter *iter);
|
|
|
|
void step_ins(rot8_iter *iter);
|
|
|
|
void rot8_stp(rot8_iter *iter);
|
|
void rot8_btp(rot8_iter *iter);
|
|
void rot8_ror(rot8_iter *iter);
|
|
void rot8_flb(rot8_iter *iter);
|
|
void rot8_biz(rot8_iter *iter);
|
|
void rot8_rnz(rot8_iter *iter);
|
|
rot8_data rot8_out(rot8_iter *iter);
|
|
void rot8_inp(rot8_iter *iter, rot8_data input);
|
|
|
|
void rot8_run(char raw_ins[], rot8_ins_size step);
|
|
void rot8_run_hooked(char raw_ins[], rot8_ins_size step, rot8_hook hook,
|
|
void *ctx);
|
|
|
|
/* Read-only view of the machine state. They exist so that tools (and
|
|
* anything else that is not an instruction) can look at the tape and at the
|
|
* program counter without knowing how rot8_iter is laid out. */
|
|
rot8_data *rot8_iter_mem(rot8_iter *iter); /* base of the tape */
|
|
rot8_data *rot8_iter_cell(rot8_iter *iter); /* cell under the pointer */
|
|
rot8_bytecode *rot8_iter_ins(rot8_iter *iter); /* base of the program */
|
|
size_t rot8_mem_cells(const rot8_iter *iter); /* tape length, in cells */
|
|
unsigned rot8_cell_width(const rot8_iter *iter); /* bits in a cell */
|
|
ptrdiff_t rot8_mem_offset(rot8_iter *iter); /* pointer - tape base */
|
|
ptrdiff_t rot8_ins_offset(rot8_iter *iter); /* pc - program base */
|
|
size_t rot8_ins_length(rot8_iter *iter); /* instructions loaded */
|
|
size_t rot8_step_count(rot8_iter *iter); /* instructions executed */
|
|
rot8_loop_depth rot8_loop_depth_of(rot8_iter *iter);
|
|
rot8_bytecode rot8_ins_at(rot8_iter *iter, size_t index);
|
|
const char *rot8_mnemonic(rot8_bytecode bc);
|
|
|
|
#endif |