Rename the cell width to cell_size
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).
This commit is contained in:
1 parent
348e64ec97
commit
c298bdca3b
3 files changed
+23
-23
No files matched your search
+6
-6
@@ -5,13 +5,13 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/* 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
|
||||
|
||||
+11
-11
@@ -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);
|
||||
|
||||
+6
-6
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user