...
|
...
|
@@ -19,10 +19,10 @@ WITH REGARD TO THIS SOFTWARE.
|
19
|
19
|
#pragma mark - Operations
|
20
|
20
|
|
21
|
21
|
/* clang-format off */
|
22
|
|
-static void push8(Stack *s, Uint8 a) { if(s->ptr == 0xff) { s->error = 2; return; } s->dat[s->ptr++] = a; }
|
23
|
|
-static Uint8 pop8_keep(Stack *s) { if(s->kptr == 0) { s->error = 1; return 0; } return s->dat[--s->kptr]; }
|
24
|
|
-static Uint8 pop8_nokeep(Stack *s) { if(s->ptr == 0) { s->error = 1; return 0; } return s->dat[--s->ptr]; }
|
25
|
|
-static Uint8 (*pop8)(Stack *s);
|
|
22
|
+static void push8(Stack *s, Uint16 a) { if(s->ptr == 0xff) { s->error = 2; return; } s->dat[s->ptr++] = a; }
|
|
23
|
+static Uint16 pop8_keep(Stack *s) { if(s->kptr == 0) { s->error = 1; return 0; } return s->dat[--s->kptr]; }
|
|
24
|
+static Uint16 pop8_nokeep(Stack *s) { if(s->ptr == 0) { s->error = 1; return 0; } return s->dat[--s->ptr]; }
|
|
25
|
+static Uint16 (*pop8)(Stack *s);
|
26
|
26
|
static void poke8(Uint8 *m, Uint16 a, Uint8 b) { m[a] = b; }
|
27
|
27
|
static Uint8 peek8(Uint8 *m, Uint16 a) { return m[a]; }
|
28
|
28
|
static void devw8(Device *d, Uint8 a, Uint8 b) { d->dat[a & 0xf] = b; d->talk(d, a & 0x0f, 1); }
|
...
|
...
|
@@ -33,6 +33,9 @@ void poke16(Uint8 *m, Uint16 a, Uint16 b) { poke8(m, a, b >> 8); poke8(m, a +
|
33
|
33
|
Uint16 peek16(Uint8 *m, Uint16 a) { return (peek8(m, a) << 8) + peek8(m, a + 1); }
|
34
|
34
|
static void devw16(Device *d, Uint8 a, Uint16 b) { devw8(d, a, b >> 8); devw8(d, a + 1, b); }
|
35
|
35
|
static Uint16 devr16(Device *d, Uint16 a) { return (devr8(d, a) << 8) + devr8(d, a + 1); }
|
|
36
|
+/* Utilities */
|
|
37
|
+static Uint16 (*pop)(Stack *s);
|
|
38
|
+static void (*push)(Stack *s, Uint16 a);
|
36
|
39
|
/* Stack */
|
37
|
40
|
static void op_lit(Uxn *u) { push8(u->src, peek8(u->ram.dat, u->ram.ptr++)); }
|
38
|
41
|
static void op_inc(Uxn *u) { Uint8 a = pop8(u->src); push8(u->src, a + 1); }
|
...
|
...
|
@@ -43,10 +46,10 @@ static void op_swp(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->
|
43
|
46
|
static void op_ovr(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b); push8(u->src, a); push8(u->src, b); }
|
44
|
47
|
static void op_rot(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src), c = pop8(u->src); push8(u->src, b); push8(u->src, a); push8(u->src, c); }
|
45
|
48
|
/* Logic */
|
46
|
|
-static void op_equ(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b == a); }
|
47
|
|
-static void op_neq(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b != a); }
|
48
|
|
-static void op_gth(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b > a); }
|
49
|
|
-static void op_lth(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b < a); }
|
|
49
|
+static void op_equ(Uxn *u) { Uint16 a = pop(u->src), b = pop(u->src); push8(u->src, b == a); }
|
|
50
|
+static void op_neq(Uxn *u) { Uint16 a = pop(u->src), b = pop(u->src); push8(u->src, b != a); }
|
|
51
|
+static void op_gth(Uxn *u) { Uint16 a = pop(u->src), b = pop(u->src); push8(u->src, b > a); }
|
|
52
|
+static void op_lth(Uxn *u) { Uint16 a = pop(u->src), b = pop(u->src); push8(u->src, b < a); }
|
50
|
53
|
static void op_jmp(Uxn *u) { Uint8 a = pop8(u->src); u->ram.ptr += (Sint8)a; }
|
51
|
54
|
static void op_jnz(Uxn *u) { Uint8 a = pop8(u->src); if(pop8(u->src)) u->ram.ptr += (Sint8)a; }
|
52
|
55
|
static void op_jsr(Uxn *u) { Uint8 a = pop8(u->src); push16(u->dst, u->ram.ptr); u->ram.ptr += (Sint8)a; }
|
...
|
...
|
@@ -61,14 +64,14 @@ static void op_sta(Uxn *u) { Uint16 a = pop16(u->src); Uint8 b = pop8(u->src); p
|
61
|
64
|
static void op_dei(Uxn *u) { Uint8 a = pop8(u->src); push8(u->src, devr8(&u->dev[a >> 4], a)); }
|
62
|
65
|
static void op_deo(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); devw8(&u->dev[a >> 4], a, b); }
|
63
|
66
|
/* Arithmetic */
|
64
|
|
-static void op_add(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b + a); }
|
65
|
|
-static void op_sub(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b - a); }
|
66
|
|
-static void op_mul(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b * a); }
|
67
|
|
-static void op_div(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); if(a == 0) { u->src->error = 3; a = 1; } push8(u->src, b / a); }
|
68
|
|
-static void op_and(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b & a); }
|
69
|
|
-static void op_ora(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b | a); }
|
70
|
|
-static void op_eor(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b ^ a); }
|
71
|
|
-static void op_sft(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b >> (a & 0x07) << ((a & 0x70) >> 4)); }
|
|
67
|
+static void op_add(Uxn *u) { Uint16 a = pop(u->src), b = pop(u->src); push(u->src, b + a); }
|
|
68
|
+static void op_sub(Uxn *u) { Uint16 a = pop(u->src), b = pop(u->src); push(u->src, b - a); }
|
|
69
|
+static void op_mul(Uxn *u) { Uint16 a = pop(u->src), b = pop(u->src); push(u->src, b * a); }
|
|
70
|
+static void op_div(Uxn *u) { Uint16 a = pop(u->src), b = pop(u->src); if(a == 0) { u->src->error = 3; a = 1; } push(u->src, b / a); }
|
|
71
|
+static void op_and(Uxn *u) { Uint16 a = pop(u->src), b = pop(u->src); push(u->src, b & a); }
|
|
72
|
+static void op_ora(Uxn *u) { Uint16 a = pop(u->src), b = pop(u->src); push(u->src, b | a); }
|
|
73
|
+static void op_eor(Uxn *u) { Uint16 a = pop(u->src), b = pop(u->src); push(u->src, b ^ a); }
|
|
74
|
+static void op_sft(Uxn *u) { Uint16 a = pop8(u->src), b = pop(u->src); push(u->src, b >> (a & 0x07) << ((a & 0x70) >> 4)); }
|
72
|
75
|
/* Stack(16-bits) */
|
73
|
76
|
static void op_lit16(Uxn *u) { push16(u->src, peek16(u->ram.dat, u->ram.ptr++)); u->ram.ptr++; }
|
74
|
77
|
static void op_inc16(Uxn *u) { Uint16 a = pop16(u->src); push16(u->src, a + 1); }
|
...
|
...
|
@@ -79,10 +82,6 @@ static void op_swp16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push
|
79
|
82
|
static void op_ovr16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b); push16(u->src, a); push16(u->src, b); }
|
80
|
83
|
static void op_rot16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src), c = pop16(u->src); push16(u->src, b); push16(u->src, a); push16(u->src, c); }
|
81
|
84
|
/* Logic(16-bits) */
|
82
|
|
-static void op_equ16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, b == a); }
|
83
|
|
-static void op_neq16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, b != a); }
|
84
|
|
-static void op_gth16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, b > a); }
|
85
|
|
-static void op_lth16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, b < a); }
|
86
|
85
|
static void op_jmp16(Uxn *u) { u->ram.ptr = pop16(u->src); }
|
87
|
86
|
static void op_jnz16(Uxn *u) { Uint16 a = pop16(u->src); if(pop8(u->src)) u->ram.ptr = a; }
|
88
|
87
|
static void op_jsr16(Uxn *u) { push16(u->dst, u->ram.ptr); u->ram.ptr = pop16(u->src); }
|
...
|
...
|
@@ -96,15 +95,6 @@ static void op_lda16(Uxn *u) { Uint16 a = pop16(u->src); push16(u->src, peek16(u
|
96
|
95
|
static void op_sta16(Uxn *u) { Uint16 a = pop16(u->src); Uint16 b = pop16(u->src); poke16(u->ram.dat, a, b); }
|
97
|
96
|
static void op_dei16(Uxn *u) { Uint8 a = pop8(u->src); push16(u->src, devr16(&u->dev[a >> 4], a)); }
|
98
|
97
|
static void op_deo16(Uxn *u) { Uint8 a = pop8(u->src); Uint16 b = pop16(u->src); devw16(&u->dev[a >> 4], a, b); }
|
99
|
|
-/* Arithmetic(16-bits) */
|
100
|
|
-static void op_add16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b + a); }
|
101
|
|
-static void op_sub16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b - a); }
|
102
|
|
-static void op_mul16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b * a); }
|
103
|
|
-static void op_div16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); if(a == 0) { u->src->error = 3; a = 1; } push16(u->src, b / a); }
|
104
|
|
-static void op_and16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b & a); }
|
105
|
|
-static void op_ora16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b | a); }
|
106
|
|
-static void op_eor16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b ^ a); }
|
107
|
|
-static void op_sft16(Uxn *u) { Uint8 a = pop8(u->src); Uint16 b = pop16(u->src); push16(u->src, b >> (a & 0x0f) << ((a & 0xf0) >> 4)); }
|
108
|
98
|
|
109
|
99
|
static void (*ops[])(Uxn *u) = {
|
110
|
100
|
op_lit, op_inc, op_pop, op_dup, op_nip, op_swp, op_ovr, op_rot,
|
...
|
...
|
@@ -113,9 +103,9 @@ static void (*ops[])(Uxn *u) = {
|
113
|
103
|
op_add, op_sub, op_mul, op_div, op_and, op_ora, op_eor, op_sft,
|
114
|
104
|
/* 16-bit */
|
115
|
105
|
op_lit16, op_inc16, op_pop16, op_dup16, op_nip16, op_swp16, op_ovr16, op_rot16,
|
116
|
|
- op_equ16, op_neq16, op_gth16, op_lth16, op_jmp16, op_jnz16, op_jsr16, op_sth16,
|
|
106
|
+ op_equ, op_neq, op_gth, op_lth, op_jmp16, op_jnz16, op_jsr16, op_sth16,
|
117
|
107
|
op_ldz16, op_stz16, op_ldr16, op_str16, op_lda16, op_sta16, op_dei16, op_deo16,
|
118
|
|
- op_add16, op_sub16, op_mul16, op_div16, op_and16, op_ora16, op_eor16, op_sft16
|
|
108
|
+ op_add, op_sub, op_mul, op_div, op_and, op_ora, op_eor, op_sft
|
119
|
109
|
};
|
120
|
110
|
|
121
|
111
|
/* clang-format on */
|
...
|
...
|
@@ -146,6 +136,14 @@ uxn_eval(Uxn *u, Uint16 vec)
|
146
|
136
|
} else {
|
147
|
137
|
pop8 = pop8_nokeep;
|
148
|
138
|
}
|
|
139
|
+ /* Short Mode */
|
|
140
|
+ if(instr & MODE_SHORT) {
|
|
141
|
+ pop = pop16;
|
|
142
|
+ push = push16;
|
|
143
|
+ } else {
|
|
144
|
+ pop = pop8;
|
|
145
|
+ push = push8;
|
|
146
|
+ }
|
149
|
147
|
(*ops[(instr & 0x1f) | ((instr & MODE_SHORT) >> 1)])(u);
|
150
|
148
|
if(u->wst.error)
|
151
|
149
|
return uxn_halt(u, u->wst.error, "Working-stack", instr);
|