tis100

Reimplementation of Zachtronics TIS-100 as a TUI game
git clone https://git.sinitax.com/sinitax/tis100
Log | Files | Refs | sfeed.txt

commit 11d75bf41c7d22b6a6a76fa2dd4843d23a75fffb
parent c06c696a20fa8987bbb13da608153ccfc3cd55e7
Author: Louis Burda <quent.burda@gmail.com>
Date:   Wed, 27 Dec 2023 14:12:17 +0100

Fix instruction squishing to fit tpu row

Diffstat:
Masm.c | 34+++++++++++++++++++++++-----------
Masm.h | 2+-
Mtis100-curses.c | 12+++++++++---
3 files changed, 33 insertions(+), 15 deletions(-)

diff --git a/asm.c b/asm.c @@ -187,9 +187,9 @@ strlcat_op_name(char *buf, struct tpu_inst_op *op, size_t n) } size_t -asm_print_inst(char *buf, size_t n, struct tpu_inst *inst) +asm_print_inst(char *buf, size_t n, struct tpu_inst *inst, size_t max) { - size_t len; + size_t len, op; len = strdcpy(buf, inst_reprs[inst->type], n); if (inst->opcnt >= 1) { @@ -197,8 +197,14 @@ asm_print_inst(char *buf, size_t n, struct tpu_inst *inst) len += strlcat_op_name(buf, &inst->ops[0], n); } if (inst->opcnt >= 2) { - len += strdcat(buf, ", ", n); - len += strlcat_op_name(buf, &inst->ops[1], n); + op = strdcat(buf, ", ", n); + op += strlcat_op_name(buf, &inst->ops[1], n); + if (len + op > max && len < n) { + buf[len] = '\0'; + op = strdcat(buf, ",", n); + op += strlcat_op_name(buf, &inst->ops[1], n); + } + len += op; } return len; @@ -437,10 +443,12 @@ tis_load_asm(struct tis *tis, const char *filepath) TOK_NIL, TOK_LEFT, TOK_RIGHT, TOK_UP, TOK_DOWN, TOK_ANY, TOK_LAST, TOK_LIT, TOK_NAME, TOK_NL, -1); if (optok == TOK_NL) { - if (!tpu_add_inst(tpu, inst_type, 0, op1, op2)) + inst = tpu_add_inst(tpu, inst_type, 0, op1, op2); + if (!inst) { die("load: line %lu, invalid instruction", tokenizer.lineno-1); - break; + } + goto inst_check; } op1 = tok_to_op(&tokenizer, optok); @@ -448,10 +456,12 @@ tis_load_asm(struct tis *tis, const char *filepath) TOK_NIL, TOK_LEFT, TOK_RIGHT, TOK_UP, TOK_DOWN, TOK_ANY, TOK_LAST, TOK_LIT, TOK_NAME, TOK_NL, -1); if (optok == TOK_NL) { - if (!tpu_add_inst(tpu, inst_type, 1, op1, op2)) + inst = tpu_add_inst(tpu, inst_type, 1, op1, op2); + if (!inst) { die("load: line %lu, invalid instruction", tokenizer.lineno-1); - break; + } + goto inst_check; } op2 = tok_to_op(&tokenizer, optok); @@ -461,15 +471,17 @@ tis_load_asm(struct tis *tis, const char *filepath) if (!inst) die("load: line %lu, invalid instruction", tokenizer.lineno-1); +inst_check: tpu->rows += 1; if (tpu->inst_cnt > TPU_MAX_INST_CNT || tpu->rows > TPU_MAX_ROWS) die("load: line %lu, tpu has too many rows", tokenizer.lineno-1); - len = asm_print_inst(rowbuf, sizeof(rowbuf), inst); + len = asm_print_inst(rowbuf, sizeof(rowbuf), + inst, TPU_MAX_COLS - colsused); if (colsused + len > TPU_MAX_COLS) - die("load: line %lu, tpu row is too long", - tokenizer.lineno-1); + die("load: line %lu, tpu row is too long (%zu,%zu)", + tokenizer.lineno-1, colsused, len); colsused = 0; break; diff --git a/asm.h b/asm.h @@ -4,6 +4,6 @@ #include <stdio.h> -size_t asm_print_inst(char *buf, size_t n, struct tpu_inst *inst); +size_t asm_print_inst(char *buf, size_t n, struct tpu_inst *inst, size_t max); void tis_load_asm(struct tis *tis, const char *filepath); void tis_load(struct tis *tis, const char **argv); diff --git a/tis100-curses.c b/tis100-curses.c @@ -197,7 +197,8 @@ tui_draw_tpu(struct tpu *tpu) start = MAX(0, MIN(tpu->pc - 4, (int) tpu->inst_cnt - TPU_MAX_ROWS)); inst = start; for (offy = 0; offy < TPU_MAX_ROWS && inst < tpu->inst_cnt; ) { - asm_print_inst(rowbuf, sizeof(rowbuf), &tpu->insts[inst]); + asm_print_inst(rowbuf, sizeof(rowbuf), &tpu->insts[inst], + TPU_MAX_COLS - (size_t) offx); label = &tpu->label_map.labels[inst]; if (label->str) { len = strlen(label->str); @@ -211,9 +212,14 @@ tui_draw_tpu(struct tpu *tpu) offy += 1; } } + if ((size_t) offx + strlen(rowbuf) > TPU_MAX_COLS) + asm_print_inst(rowbuf, sizeof(rowbuf), &tpu->insts[inst], + TPU_MAX_COLS - (size_t) offx); tui_draw_text(sx + 1 + offx, sy + 1 + offy, - inst == tpu->pc ? A_STANDOUT | (tpu->idle ? A_DIM : 0) : 0, - "%-*.*s", TPU_MAX_COLS - offx, TPU_MAX_COLS, rowbuf); + inst == tpu->pc ? + A_STANDOUT | (tpu->idle ? A_DIM : 0) : 0, + "%-*.*s", TPU_MAX_COLS - offx, + TPU_MAX_COLS, rowbuf); inst += 1; offy += 1; offx = 0;