M src/gyo.c => src/gyo.c +5 -5
@@ 16,7 16,7 @@ int
gyo_eval(uc15 *rom, uc3 *ram, uc15 pc)
{
uc15 inst, addr, opc, acc, t, n;
- while((inst = rom[ter2bin(pc)])) {
+ while((inst = rom[uc15bin(pc)])) {
print_instruction(inst, pc);
pc = op_inc(pc);
switch(inst & 0x3) {
@@ 53,10 53,10 @@ gyo_eval(uc15 *rom, uc3 *ram, uc15 pc)
case OP_UN: acc = op_una(acc, opc >> 16); break;
case OP_BI: acc = op_bin(acc, n, opc >> 16); break;
case OP_AD: acc = op_add(acc, n); break;
- case OP_LS: acc = n << (ter2bin(acc) << 1); break;
- case OP_RS: acc = n >> (ter2bin(acc) << 1); break;
+ case OP_LS: acc = n << (uc15bin(acc) << 1); break;
+ case OP_RS: acc = n >> (uc15bin(acc) << 1); break;
case OP_ST: acc = rst[rsp]; break;
- case OP_ME: acc = ram[ter2bin(acc)]; break;
+ case OP_ME: acc = ram[uc15bin(acc)]; break;
}
/* d+dsp */ dsp += ((opc >> 8) & 0x3) - 1;
/* r+rsp */ rsp += ((opc >> 10) & 0x3) - 1;
@@ 69,7 69,7 @@ gyo_eval(uc15 *rom, uc3 *ram, uc15 pc)
/* N->(DEV,NOP,RAM) */
switch((opc >> 14) & 0x3) {
case 0: emu_event(n, acc); break;
- case 2: ram[ter2bin(n)] = acc; break;
+ case 2: ram[uc15bin(n)] = acc; break;
}
break;
}
M src/gyoasm.c => src/gyoasm.c +4 -4
@@ 83,12 83,12 @@ asmtoken(char *token)
Label *l;
uc15 opc;
/* word */
- if((l = findlabel(token))) return (bin2ter(l->addr) << 4) | 0x1;
+ if((l = findlabel(token))) return (binuc15(l->addr) << 4) | 0x1;
/* jump */
switch(token[0]) {
case '#': /* LIT */ return (hep2ter(token) << 2) | 0x2;
- case '>': /* JMP */ return (l = findlabel(token + 1)) ? (bin2ter(l->addr) << 4) | 0x9 : error("Missing label", token);
- case '?': /* JCN */ return (l = findlabel(token + 1)) ? (bin2ter(l->addr) << 4) | 0x5 : error("Missing label", token);
+ case '>': /* JMP */ return (l = findlabel(token + 1)) ? (binuc15(l->addr) << 4) | 0x9 : error("Missing label", token);
+ case '?': /* JCN */ return (l = findlabel(token + 1)) ? (binuc15(l->addr) << 4) | 0x5 : error("Missing label", token);
}
/* opcode */
opc = str2ter("------0000--00-");
@@ 152,7 152,7 @@ assemble(FILE *f)
return error("Invalid token", w);
if(!(main = findlabel("main")))
return error("Missing label", "main");
- rom[0] = (bin2ter(main->addr) << 4) | 0x9;
+ rom[0] = (binuc15(main->addr) << 4) | 0x9;
return 1;
}
M src/gyodis.c => src/gyodis.c +5 -5
@@ 23,13 23,13 @@ void
disinstr(uc15 inst)
{
if((inst & 0x3) == 2) {
- putchar('#'), print_heptavintimal(inst >> 2);
+ putchar('#'), phep(inst >> 2);
return;
} else if((inst & 0x3) == 1) {
switch((inst >> 2) & 0x3) {
- case 0: printf("%s:", "call"), print_heptavintimal(inst >> 4); break;
- case 1: putchar('?'), print_heptavintimal(inst >> 4); break;
- case 2: putchar('>'), print_heptavintimal(inst >> 4); break;
+ case 0: printf("%s:", "call"), phep(inst >> 4); break;
+ case 1: putchar('?'), phep(inst >> 4); break;
+ case 2: putchar('>'), phep(inst >> 4); break;
}
return;
} else {
@@ 83,7 83,7 @@ disassemble(FILE *f)
uc15 t;
while(!feof(f)) {
if(fread(&t, sizeof(uc15), 1, f))
- print_heptavintimal(i), printf(" | "), disinstr(t), putchar('\n');
+ phep(i), printf(" | "), disinstr(t), putchar('\n');
i++;
}
return 1;
M src/gyoemu.c => src/gyoemu.c +15 -15
@@ 62,21 62,21 @@ print_ternary(uc15 t)
void
print_decimal(uc15 t)
{
- printf("%d", ter2bin(t));
+ printf("%d", uc15bin(t));
}
void
disinstr(uc15 inst)
{
if((inst & 0x3) == 2) {
- putchar('#'), print_heptavintimal(inst >> 2), putchar('\n');
+ putchar('#'), phep(inst >> 2), putchar('\n');
return;
} else if((inst & 0x3) == 1) {
if(((inst >> 2) & 0x3) == 1) {
- putchar('?'), print_heptavintimal(inst >> 4), putchar('\n');
+ putchar('?'), phep(inst >> 4), putchar('\n');
return;
} else if(((inst >> 2) & 0x3) == 0) {
- putchar('>'), print_heptavintimal(inst >> 4), putchar('\n');
+ putchar('>'), phep(inst >> 4), putchar('\n');
return;
}
printf("CALL\n");
@@ 129,23 129,23 @@ void
print_instruction(uc15 inst, uc15 pc)
{
uc15 addr;
- print_heptavintimal(pc), putchar('=');
- print_heptavintimal(inst), putchar(' ');
+ phep(pc), putchar('=');
+ phep(inst), putchar(' ');
switch(inst & 0x3) {
case 0x2:
- printf("[ #"), print_heptavintimal(inst >> 2), printf(" ][+] ");
+ printf("[ #"), phep(inst >> 2), printf(" ][+] ");
break;
case 0x1:
addr = inst >> 0x4;
switch(inst & 0xf) {
case 0x9: /* JMP */
- printf("[ >"), print_heptavintimal(addr), printf(" ][+][0] ");
+ printf("[ >"), phep(addr), printf(" ][+][0] ");
break;
case 0x5: /* JCN */
- printf("[ ?"), print_heptavintimal(addr), printf(" ][0][0] ");
+ printf("[ ?"), phep(addr), printf(" ][0][0] ");
break;
case 0x1: /* JSR */
- printf("[ call:"), print_heptavintimal(addr), printf(" ][-][0] ");
+ printf("[ call:"), phep(addr), printf(" ][-][0] ");
break;
}
break;
@@ 159,13 159,13 @@ print_instruction(uc15 inst, uc15 pc)
uc3 x = (opc >> 12) & 0x3;
uc3 o = (opc >> 14) & 0x3;
uc15 ins = opc >> 16;
- printf("[ "), print_heptavintimal(ins);
+ printf("[ "), phep(ins);
printf(" ][%c][%c][%c][%c][ %s ][%c][%c][-] ",
trit2char(o),
trit2char(x),
trit2char(rsm),
trit2char(dsm),
- opcodes[ter2bin(alu)],
+ opcodes[uc15bin(alu)],
trit2char(imm),
trit2char(ret));
break;
@@ 181,7 181,7 @@ print_stack(uc15 *stack, int ptr)
if(!ptr)
printf("..");
for(i = 1; i <= ptr; i++)
- print_heptavintimal(stack[i]), printf(" ");
+ phep(stack[i]), printf(" ");
}
void
@@ 202,8 202,8 @@ push(uc15 inst)
int
emu_event(uc15 addr, uc15 value)
{
- ROM[ter2bin(addr)] = value;
- printf("<%d=%d> ", ter2bin(addr), ter2bin(value));
+ ROM[uc15bin(addr)] = value;
+ printf("<%d=%d> ", uc15bin(addr), uc15bin(value));
return 0;
}
M src/tern.c => src/tern.c +30 -27
@@ 13,6 13,35 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/
+void
+phep(uc15 n)
+{
+ int i;
+ for(i = 4; i > -1; --i) {
+ int t = uc15bin((n >> (i * 6)) & 0x3f);
+ putchar(t ? '@' + t : '0');
+ }
+}
+
+
+uc15
+binuc15(int n)
+{
+ uc15 sft = 0, acc = 0;
+ while(n > 0)
+ acc |= (n % 3) << sft, n /= 3, sft += 2;
+ return acc;
+}
+
+int
+uc15bin(uc15 t)
+{
+ int sft = 1, acc = 0;
+ while(t)
+ acc += sft * (t & 0x3), t >>= 2, sft *= 3;
+ return acc;
+}
+
uc3
chr2tri(char c)
{
@@ 93,14 122,6 @@ op_bin(uc15 a, uc15 b, uc15 gate)
return acc;
}
-int
-ter2bin(uc15 t)
-{
- int sft = 1, acc = 0;
- while(t)
- acc += sft * (t & 0x3), t >>= 2, sft *= 3;
- return acc;
-}
uc15
str2ter(char *str)
@@ 112,14 133,6 @@ str2ter(char *str)
return acc;
}
-uc15
-bin2ter(int n)
-{
- uc15 sft = 0, acc = 0;
- while(n > 0)
- acc |= (n % 3) << sft, n /= 3, sft += 2;
- return acc;
-}
uc15
hep2ter(char *str)
@@ 127,16 140,6 @@ hep2ter(char *str)
uc15 acc = 0;
char c;
while((c = *str++))
- acc <<= 6, acc |= bin2ter(c ? (c - 'A') + 1 : 0);
+ acc <<= 6, acc |= binuc15(c ? (c - 'A') + 1 : 0);
return acc;
}
-
-void
-print_heptavintimal(uc15 n)
-{
- int i;
- for(i = 4; i > -1; --i) {
- int t = ter2bin((n >> (i * 6)) & 0x3f);
- putchar(t ? '@' + t : '0');
- }
-}
M src/tern.h => src/tern.h +6 -3
@@ 14,16 14,19 @@ WITH REGARD TO THIS SOFTWARE.
typedef uint32_t uc15;
typedef uint_fast8_t uc3;
+void phep(uc15 n);
+
+int uc15bin(uc15 t);
+uc15 binuc15(int n);
+
uc15 op_inc(uc15 a);
uc15 op_add(uc15 a, uc15 b);
uc15 op_cmp(uc15 a, uc15 b);
uc15 op_una(uc15 a, uc3 gate);
uc15 op_bin(uc15 a, uc15 b, uc15 gate);
+
uc3 chr2tri(char c);
-int ter2bin(uc15 t);
uc15 str2ter(char *str);
uc15 hep2ter(char *str);
-uc15 bin2ter(int n);
-void print_heptavintimal(uc15 n);