tis100

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

commit 756e5b07f45852fcda14b58822ca66e1c0f05d0c
parent 565355c91601043b5e599bc7e79ee7f1d3140e21
Author: Louis Burda <quent.burda@gmail.com>
Date:   Wed, 26 Jul 2023 07:54:02 +0200

Remove jgz/jlz and add not/and instructions

Diffstat:
Masm.c | 16++++++++--------
Mtest/test.asm | 2+-
Mtpu.c | 34++++++++++++++++------------------
Mtpu.h | 6++----
4 files changed, 27 insertions(+), 31 deletions(-)

diff --git a/asm.c b/asm.c @@ -21,8 +21,8 @@ enum asm_tok { TOK_ANY, TOK_LAST, TOK_LIT, TOK_NAME, /* Instructions (order like INST_*) */ - TOK_NOP, TOK_MOV, TOK_SWP, TOK_SAV, TOK_ADD, TOK_SUB, - TOK_NEG, TOK_JMP, TOK_JEZ, TOK_JNZ, TOK_JGZ, TOK_JLZ, TOK_JRO, + TOK_NOP, TOK_MOV, TOK_SWP, TOK_SAV, TOK_ADD, TOK_SUB, TOK_NEG, + TOK_NOT, TOK_AND, TOK_JMP, TOK_JEZ, TOK_JNZ, TOK_JRO, /* Misc */ TOK_COMMENT, TOK_LABEL, TOK_XPOS, TOK_YPOS, TOK_NL, TOK_EOF @@ -46,8 +46,8 @@ static const char *tok_strs[] = { "any", "last", NULL, NULL, /* Instructions (order like INST_*) */ - "nop", "mov", "swp", "sav", "add", "sub", - "neg", "jmp", "jez", "jnz", "jgz", "jlz", "jro", + "nop", "mov", "swp", "sav", "add", "sub", "neg", + "not", "and", "jmp", "jez", "jnz", "jro", /* Misc */ NULL, NULL, NULL, NULL, NULL, NULL @@ -62,8 +62,8 @@ static const char *tok_reprs[] = { "'ANY'", "'LAST'", "<LIT>", "<NAME>", /* Instructions (order like INST_*) */ - "'NOP'", "'MOV'", "'SWP'", "'SAV'", "'ADD'", "'SUB'", - "'NEG'", "'JMP'", "'JEZ'", "'JNZ'", "'JGZ'", "'JLZ'", "'JRO'", + "'NOP'", "'MOV'", "'SWP'", "'SAV'", "'ADD'", "'SUB'", "'NEG'", + "'NOT'", "'AND'", "'JMP'", "'JEZ'", "'JNZ'", "'JRO'", /* Misc */ "#<COMMENT>", "<LABEL>:", "X<INT>", "Y<INT>", "<NL>", "<EOF>" @@ -348,8 +348,8 @@ tis_load(struct tis *tis, const char *filepath) tok_next_in(&tokenizer, TOK_NL, -1); break; case TOK_NOP: case TOK_MOV: case TOK_SWP: case TOK_SAV: - case TOK_ADD: case TOK_SUB: case TOK_NEG: case TOK_JMP: - case TOK_JEZ: case TOK_JNZ: case TOK_JGZ: case TOK_JLZ: + case TOK_ADD: case TOK_SUB: case TOK_NEG: case TOK_NOT: + case TOK_AND: case TOK_JMP: case TOK_JEZ: case TOK_JNZ: case TOK_JRO: if (!tpu) goto disallowed; inst = tok_to_inst(tok); diff --git a/test/test.asm b/test/test.asm @@ -7,7 +7,7 @@ end tpu X2 Y1 mov LEFT, ACC - add 1 + and 127 mov ACC, RIGHT end diff --git a/tpu.c b/tpu.c @@ -16,8 +16,8 @@ const char *status_reprs[] = { }; const char *inst_reprs[] = { - "NOP", "MOV", "SWP", "SAV", "ADD", "SUB", - "NEG", "JMP", "JEZ", "JNZ", "JGZ", "JLZ", "JRO" + "NOP", "MOV", "SWP", "SAV", "ADD", "SUB", "NEG", + "NOT", "AND", "JMP", "JEZ", "JNZ", "JGZ", "JLZ", "JRO" }; const char *op_reprs[] = { @@ -272,19 +272,19 @@ tpu_set_inst(struct tpu *tpu, uint8_t pc, enum tpu_inst_type inst_type, inst->ops[1] = op2; switch (inst->type) { - case INST_NOP: case INST_SAV: - case INST_SWP: case INST_NEG: + case INST_NOP: case INST_SAV: case INST_SWP: + case INST_NEG: case INST_NOT: if (inst->opcnt != 0) return false; break; - case INST_ADD: case INST_SUB: + case INST_ADD: case INST_SUB: case INST_AND: if (inst->opcnt != 1) return false; + if (inst->opcnt == OP_LABEL) return false; break; case INST_JRO: if (inst->opcnt != 1) return false; if (inst->ops[0].type != OP_LIT) return false; break; case INST_JMP: case INST_JEZ: case INST_JNZ: - case INST_JGZ: case INST_JLZ: if (inst->opcnt != 1) return false; if (inst->ops[0].type != OP_LABEL) return false; break; @@ -483,6 +483,16 @@ tpu_exec(struct tpu *tpu, struct tpu_inst *inst) tpu->acc = -tpu->acc; tpu->pc += 1; return STATUS_RUN; + case INST_NOT: + tpu->acc = ~tpu->acc; + tpu->pc += 1; + return STATUS_RUN; + case INST_AND: + val = tpu_exec_get(tpu, &inst->ops[0]); + if (val < 0) return STATUS_READ; + tpu->acc &= (uint8_t) val; + tpu->pc += 1; + return STATUS_RUN; case INST_JMP: tpu_jmp_label(tpu, inst->ops[0].val.label); return STATUS_RUN; @@ -498,18 +508,6 @@ tpu_exec(struct tpu *tpu, struct tpu_inst *inst) else tpu->pc += 1; return STATUS_RUN; - case INST_JGZ: - if (tpu->acc > 0) - tpu_jmp_label(tpu, inst->ops[0].val.label); - else - tpu->pc += 1; - return STATUS_RUN; - case INST_JLZ: - if (tpu->acc < 0) - tpu_jmp_label(tpu, inst->ops[0].val.label); - else - tpu->pc += 1; - return STATUS_RUN; case INST_JRO: tpu->pc += inst->ops[0].val.lit; if (tpu->pc >= tpu->inst_cnt) tpu->pc = 0; diff --git a/tpu.h b/tpu.h @@ -15,10 +15,8 @@ enum tpu_status { }; enum tpu_inst_type { - INST_NOP, INST_MOV, INST_SWP, INST_SAV, - INST_ADD, INST_SUB, INST_NEG, INST_JMP, - INST_JEZ, INST_JNZ, INST_JGZ, INST_JLZ, - INST_JRO + INST_NOP, INST_MOV, INST_SWP, INST_SAV, INST_ADD, INST_SUB, INST_NEG, + INST_NOT, INST_AND, INST_JMP, INST_JEZ, INST_JNZ, INST_JRO }; enum tpu_inst_op_type {