commit b762c14

Devine Lu Linvega  ·  2023-01-02 21:32:13 +0000 UTC
parent 09bc396
Implement full CALL opcodes stack
1 files changed,  +11, -6
+11, -6
 1@@ -1,7 +1,7 @@
 2 #include "uxn.h"
 3 
 4 /*
 5-Copyright (u) 2022 Devine Lu Linvega, Andrew Alderwick, Andrew Richards
 6+Copyright (u) 2022-2023 Devine Lu Linvega, Andrew Alderwick, Andrew Richards
 7 
 8 Permission to use, copy, modify, and distribute this software for any
 9 purpose with or without fee is hereby granted, provided that the above
10@@ -16,11 +16,9 @@ WITH REGARD TO THIS SOFTWARE.
11 	x,y: macro in params. d: macro in device. j: macro temp variables. o: macro out param. */
12 
13 #define HALT(c) { return uxn_halt(u, instr, (c), pc - 1); }
14-#define LITERAL { if(bs) { PEEK16(a, pc) PUSH16(src, a) pc += 2; } else { a = u->ram[pc]; PUSH8(src, a) pc += 1; } }
15-#define CALL { if(bs){ PEEK16(a, pc) PUSH16(u->rst, pc + 2) pc = a; } else { a = u->ram[pc]; PUSH16(u->rst, pc + 1) pc += (Sint8)a + 2; } }
16 #define JUMP(x) { if(bs) pc = (x); else pc += (Sint8)(x); }
17 #define PUSH8(s, x) { if(s->ptr == 0xff) HALT(2) s->dat[s->ptr++] = (x); }
18-#define PUSH16(s, x) { if((j = s->ptr) >= 0xfe) HALT(2) s->dat[j] = (x) >> 8; s->dat[j + 1] = (x); s->ptr = j + 2; }
19+#define PUSH16(s, x) { if((j = s->ptr) >= 0xfe) HALT(2) k = (x); s->dat[j] = k >> 8; s->dat[j + 1] = k; s->ptr = j + 2; }
20 #define PUSH(s, x) { if(bs) { PUSH16(s, (x)) } else { PUSH8(s, (x)) } }
21 #define POP8(o) { if(!(j = *sp)) HALT(1) o = (Uint16)src->dat[--j]; *sp = j; }
22 #define POP16(o) { if((j = *sp) <= 1) HALT(1) o = src->dat[j - 1]; o += src->dat[j - 2] << 8; *sp = j - 2; }
23@@ -34,7 +32,7 @@ WITH REGARD TO THIS SOFTWARE.
24 int
25 uxn_eval(Uxn *u, Uint16 pc)
26 {
27-	unsigned int a, b, c, j, bs, instr;
28+	unsigned int a, b, c, j, k, bs, instr;
29 	Uint8 kptr, *sp;
30 	Stack *src, *dst;
31 	if(!pc || u->dev[0x0f]) return 0;
32@@ -48,7 +46,14 @@ uxn_eval(Uxn *u, Uint16 pc)
33 		/* Short Mode */
34 		bs = instr & 0x20;
35 		switch(instr & 0x1f) {
36-		case 0x00: /* LIT */ if(instr & 0x80) { LITERAL } else { CALL } break;
37+		case 0x00:
38+		/* Literals/Calls */
39+		if(instr == 0x20)      /* JMI  */ { sp = &u->wst->ptr; PEEK16(a, pc) pc = a; }
40+		else if(instr == 0x40) /* JCI  */ { sp = &u->wst->ptr; src = u->wst; POP8(a) PEEK16(b, pc) pc = a ? (Uint16)b : pc + 2; }
41+		else if(instr == 0x60) /* JSI  */ { sp = &u->wst->ptr; PEEK16(a, pc) PUSH16(u->rst, pc + 2) pc = a; }
42+		else if(bs)            /* LIT2 */ { PEEK16(a, pc) PUSH16(src, a) pc = pc + 2; }
43+		else                   /* LITr */ { a = u->ram[pc++]; PUSH8(src, a) } break;
44+		/* ALU */
45 		case 0x01: /* INC */ POP(a) PUSH(src, a + 1) break;
46 		case 0x02: /* POP */ POP(a) break;
47 		case 0x03: /* NIP */ POP(a) POP(b) PUSH(src, a) break;