From 2a95e81cd206a834d45e9e95c23d899cc167998f Mon Sep 17 00:00:00 2001 From: neauoire Date: Thu, 9 Nov 2023 20:31:33 -0800 Subject: [PATCH] Using signed trits in asm/vm --- src/gyo.c | 6 +++--- src/gyoasm.c | 18 ++++++++++-------- src/gyoemu.c | 6 +++--- src/tern.c | 10 +++++----- src/tern.h | 7 +++++-- 5 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/gyo.c b/src/gyo.c index db06e58..07f2f45 100644 --- a/src/gyo.c +++ b/src/gyo.c @@ -21,9 +21,9 @@ gyo_eval(uc15 *rom, uc3 *ram, uc15 pc) pc = uc_inc(pc); switch(inst & 0x3) { /* LIT */ - case 0x2: dst[++dsp] = inst >> 0x2; break; + case BCP: dst[++dsp] = inst >> 0x2; break; /* JMP */ - case 0x1: + case BCZ: addr = inst >> 0x4; /* JSR,JCN,JMP */ switch((inst >> 2) & 0x3) { @@ -33,7 +33,7 @@ gyo_eval(uc15 *rom, uc3 *ram, uc15 pc) } break; /* OPC */ - case 0x0: + case BCN: opc = inst >> 0x2; t = dst[dsp], n = dst[dsp - 1]; /* (RST->PC,NOP,T->N) */ diff --git a/src/gyoasm.c b/src/gyoasm.c index 6c2d026..1252f05 100644 --- a/src/gyoasm.c +++ b/src/gyoasm.c @@ -83,15 +83,15 @@ asmtoken(char *token) Label *l; uc15 opc; /* word */ - if((l = findlabel(token))) return (intuc15(l->addr) << 4) | 0x0 | 0x1; + if((l = findlabel(token))) return (intuc15(l->addr) << 4) | 0x0 | BCZ; /* jump */ switch(token[0]) { - case '#': /* LIT */ return (hepuc15(token) << 2) | 0x2; - case '>': /* JMP */ return (l = findlabel(token + 1)) ? (intuc15(l->addr) << 4) | 0x8 | 0x1 : error("Missing label", token); - case '?': /* JCN */ return (l = findlabel(token + 1)) ? (intuc15(l->addr) << 4) | 0x4 | 0x1 : error("Missing label", token); + case '#': /* LIT */ return (hepuc15(token) << 2) | BCP; + case '>': /* JMP */ return (l = findlabel(token + 1)) ? (intuc15(l->addr) << 4) | 0x8 | BCZ : error("Missing label", token); + case '?': /* JCN */ return (l = findlabel(token + 1)) ? (intuc15(l->addr) << 4) | 0x4 | BCZ : error("Missing label", token); } /* opcode */ - opc = hepuc15("00MIL") | 0x0; // 00 00 00 00 00 00 01 01 01 01 00 00 01 01 00 + opc = hepuc15("00MIL") | BCN; // 00 00 00 00 00 00 01 01 01 01 00 00 01 01 00 while((c = *token++)) { switch(c) { case ';': opc &= ~(3 << 12); /* fall-through */ @@ -147,12 +147,14 @@ assemble(FILE *f) char w[0x40]; Label *main; makelabel(">", 1); - while(fscanf(f, "%62s", w) == 1) - if(slen(w) > 0x3d || !parse(w)) + while(fscanf(f, "%62s", w) == 1) { + if(slen(w) > 0x3d) return error("Invalid token", w); + parse(w); + } if(!(main = findlabel("main"))) return error("Missing label", "main"); - rom[0] = (intuc15(main->addr) << 4) | 0x9; + rom[0] = (intuc15(main->addr) << 4) | 0x8 | BCZ; return 1; } diff --git a/src/gyoemu.c b/src/gyoemu.c index b2b1088..f3341b2 100644 --- a/src/gyoemu.c +++ b/src/gyoemu.c @@ -28,10 +28,10 @@ char *opcodes[] = { void disinstr(uc15 inst) { - if((inst & 0x3) == 2) { + if((inst & 0x3) == BCP) { putchar('#'), phep(inst >> 2, 5); return; - } else if((inst & 0x3) == 1) { + } else if((inst & 0x3) == BCZ) { if(((inst >> 2) & 0x3) == 1) { putchar('?'), phep(inst >> 4, 5); return; @@ -41,7 +41,7 @@ disinstr(uc15 inst) } printf("call:"), phep(inst >> 4, 5); return; - } else { + } else if((inst & 0x3) == BCN) { /* (RST->PC,NOP,T->N) */ switch((inst >> 2) & 0x3) { case 0: putchar(';'); break; diff --git a/src/tern.c b/src/tern.c index 108952b..d2fae3a 100644 --- a/src/tern.c +++ b/src/tern.c @@ -53,7 +53,7 @@ tribc1(char c) switch(c) { case '+': return BCP; case '0': return BCZ; - case '-': return BCM; + case '-': return BCN; } return 0x2; } @@ -149,7 +149,7 @@ bc15sign(bc15 a) int i; for(i = 28; i >= 0; i -= 2) { bc3 x = (a >> i) & 0x3; - if (x != 0) return x; + if(x != 0) return x; } return 0; } @@ -176,11 +176,11 @@ bc_tnor(bc15 a, bc15 b) for(i = 0; i < 30; i += 2) { int x = (a >> i) & 0x3; int y = (b >> i) & 0x3; - if (x == y) { + if(x == y) { digit = (4 - x) & 0x3; } else { int t = ((x ^ y) + 1) & 0x3; - digit = t | (t>>1); + digit = t | (t >> 1); } acc |= (digit << i); } @@ -235,7 +235,7 @@ bc_cmp(bc15 a, bc15 b) /* altered repr: 0 (-) 1 (zero) 2 (+) */ bc3 x = ((a >> i) + 1) & 0x3; bc3 y = ((b >> i) + 1) & 0x3; - if (x != y) + if(x != y) return x < y ? 3 : 1; } return 0; diff --git a/src/tern.h b/src/tern.h index f42fa7e..31ea9ee 100644 --- a/src/tern.h +++ b/src/tern.h @@ -13,7 +13,11 @@ WITH REGARD TO THIS SOFTWARE. #define BCP 0x1 #define BCZ 0x0 -#define BCM 0x3 +#define BCN 0x3 + +#define UCP 0x2 +#define UCZ 0x1 +#define UCN 0x0 typedef uint32_t bc15; typedef uint32_t uc15; @@ -77,4 +81,3 @@ uc15 uc_bin(uc15 a, uc15 b, uc15 gate); char bc3hep(bc3 t); bc3 hepbc3(char c); uc15 hepuc15(char *str); - -- 2.45.2