diff --git a/include/rot8/rot8.h b/include/rot8/rot8.h index abc4739..22e99fa 100644 --- a/include/rot8/rot8.h +++ b/include/rot8/rot8.h @@ -5,13 +5,13 @@ #include #include -/* Geometry. A tape is mem_size cells and a cell is cell_bits wide; the +/* 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_BITS 11 -#define ROT8_MAX_CELL_BITS 32 +#define ROT8_DEFAULT_CELL_SIZE 11 +#define ROT8_MAX_CELL_SIZE 32 #define ROT8_MAX_MEM_SIZE (1u << 20) #define INS_SIZE 65536 @@ -38,7 +38,7 @@ typedef struct rot8_iter rot8_iter; * later only affects machines made afterwards. */ typedef struct rot8_config { size_t mem_size; /* cells on the tape */ - unsigned cell_bits; /* bits per cell: 8, 16 or 32 */ + unsigned cell_size; /* bits per cell: 8, 16 or 32 */ } rot8_config; void rot8_config_default(rot8_config *cfg); @@ -46,8 +46,8 @@ void rot8_config_default(rot8_config *cfg); * 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_bits of a value. */ -rot8_data rot8_cell_mask(unsigned cell_bits); +/* 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 diff --git a/src/rot8.c b/src/rot8.c index 8f82bad..2d453e9 100644 --- a/src/rot8.c +++ b/src/rot8.c @@ -12,7 +12,7 @@ struct rot8_iter { /* Geometry, copied from the config when the machine was made. */ size_t mem_size; - unsigned cell_bits; + unsigned cell_size; rot8_data mask; /* Not touched by the instruction set: kept so that hooks and tools can @@ -27,12 +27,12 @@ struct rot8_iter { /* The geometry machines are born with. rot8_config_apply replaces it. */ static rot8_config config_current = {ROT8_DEFAULT_MEM_SIZE, - ROT8_DEFAULT_CELL_BITS}; + ROT8_DEFAULT_CELL_SIZE}; void rot8_config_default(rot8_config *cfg) { if (cfg != NULL) { cfg->mem_size = ROT8_DEFAULT_MEM_SIZE; - cfg->cell_bits = ROT8_DEFAULT_CELL_BITS; + cfg->cell_size = ROT8_DEFAULT_CELL_SIZE; } } @@ -43,7 +43,7 @@ bool rot8_config_apply(const rot8_config *cfg) { if (cfg->mem_size == 0 || cfg->mem_size > ROT8_MAX_MEM_SIZE) { return (false); } - if (cfg->cell_bits == 0 || cfg->cell_bits > ROT8_MAX_CELL_BITS) { + if (cfg->cell_size == 0 || cfg->cell_size > ROT8_MAX_CELL_SIZE) { return (false); } config_current = *cfg; @@ -52,11 +52,11 @@ bool rot8_config_apply(const rot8_config *cfg) { const rot8_config *rot8_config_current(void) { return (&config_current); } -rot8_data rot8_cell_mask(unsigned cell_bits) { - if (cell_bits >= ROT8_MAX_CELL_BITS) { +rot8_data rot8_cell_mask(unsigned cell_size) { + if (cell_size >= ROT8_MAX_CELL_SIZE) { return ((rot8_data)~(rot8_data)0); } - return ((rot8_data)(((rot8_data)1 << cell_bits) - 1u)); + return ((rot8_data)(((rot8_data)1 << cell_size) - 1u)); } void step_ins(rot8_iter *iter) { @@ -154,8 +154,8 @@ void rot8_iter_init_cfg(rot8_iter *iter, rot8_data *mem, rot8_bytecode *ins, iter->hook = NULL; iter->hook_ctx = NULL; iter->mem_size = cfg != NULL ? cfg->mem_size : config_current.mem_size; - iter->cell_bits = cfg != NULL ? cfg->cell_bits : config_current.cell_bits; - iter->mask = rot8_cell_mask(iter->cell_bits); + iter->cell_size = cfg != NULL ? cfg->cell_size : config_current.cell_size; + iter->mask = rot8_cell_mask(iter->cell_size); } void rot8_iter_init(rot8_iter *iter, rot8_data *mem, rot8_bytecode *ins) { @@ -181,7 +181,7 @@ void rot8_btp(rot8_iter *iter) { void rot8_ror(rot8_iter *iter) { rot8_data value = *(iter->mem_now); - unsigned bits = iter->cell_bits; + unsigned bits = iter->cell_size; rot8_data rotated = (rot8_data)((value >> 1) | ((value & 1u) << (bits - 1))); *(iter->mem_now) = rotated & iter->mask; // Rotate right pointed data @@ -232,7 +232,7 @@ rot8_bytecode *rot8_iter_ins(rot8_iter *iter) { return (iter->ins_base); } size_t rot8_mem_cells(const rot8_iter *iter) { return (iter->mem_size); } -unsigned rot8_cell_width(const rot8_iter *iter) { return (iter->cell_bits); } +unsigned rot8_cell_width(const rot8_iter *iter) { return (iter->cell_size); } ptrdiff_t rot8_mem_offset(rot8_iter *iter) { return (iter->mem_now - iter->mem_base); diff --git a/tests/vm_test.c b/tests/vm_test.c index 445c74c..fd708e8 100644 --- a/tests/vm_test.c +++ b/tests/vm_test.c @@ -51,7 +51,7 @@ static void usage(const char *program) { " %s --cell 16 --dump tapes/wave.rot8\n" " %s --mem 4096 --live tapes/wave.rot8\n", program, (unsigned)ROT8_DEFAULT_MEM_SIZE, (unsigned)ROT8_MAX_MEM_SIZE, - (unsigned)ROT8_MAX_CELL_BITS, (unsigned)ROT8_DEFAULT_CELL_BITS, program, + (unsigned)ROT8_MAX_CELL_SIZE, (unsigned)ROT8_DEFAULT_CELL_SIZE, program, program, program, program, program); } @@ -69,7 +69,7 @@ static bool parse_uint(const char *text, unsigned int *out) { return (true); } -/* --mem takes anything up to the machine's limit, --cell only 8, 16 or 32. */ +/* --mem and --cell take anything from 1 up to the machine's limit. */ static bool parse_geometry(const char *text, unsigned long max, unsigned long *out) { char *end = NULL; @@ -162,13 +162,13 @@ int main(int argc, char **argv) { } else if (strcmp(arg, "-b") == 0 || strcmp(arg, "--cell") == 0) { unsigned long bits = 0; if (i + 1 >= argc || - !parse_geometry(argv[i + 1], (unsigned long)ROT8_MAX_CELL_BITS, + !parse_geometry(argv[i + 1], (unsigned long)ROT8_MAX_CELL_SIZE, &bits)) { fprintf(stderr, "%s: --cell needs 1..%u bits\n", program, - (unsigned)ROT8_MAX_CELL_BITS); + (unsigned)ROT8_MAX_CELL_SIZE); return (2); } - cfg.cell_bits = (unsigned)bits; + cfg.cell_size = (unsigned)bits; i++; } else if (strcmp(arg, "-f") == 0 || strcmp(arg, "--fps") == 0) { if (i + 1 >= argc || !parse_uint(argv[++i], &opts.fps)) { @@ -194,7 +194,7 @@ int main(int argc, char **argv) { if (!rot8_config_apply(&cfg)) { fprintf(stderr, "%s: %zu cells of %u bits is not a ROT-8 machine\n", - program, cfg.mem_size, cfg.cell_bits); + program, cfg.mem_size, cfg.cell_size); return (2); }