...
|
...
|
@@ -19,87 +19,68 @@ WITH REGARD TO THIS SOFTWARE.
|
19
|
19
|
|
20
|
20
|
void setflag(Uint8 *st, char flag, int b) { if(b) *st |= flag; else *st &= (~flag); }
|
21
|
21
|
int getflag(Uint8 *st, char flag) { return *st & flag; }
|
22
|
|
-Uint8 mempeek8(Uxn *u, Uint16 s) { return u->ram.dat[s]; }
|
23
|
|
-Uint16 mempeek16(Uxn *u, Uint16 s) { return (u->ram.dat[s] << 8) + (u->ram.dat[s + 1] & 0xff); }
|
24
|
|
-
|
25
|
|
-void wspush8(Uxn *u, Uint8 b) { u->wst.dat[u->wst.ptr++] = b; }
|
26
|
|
-Uint8 wspop8(Uxn *u) { return u->wst.dat[--u->wst.ptr]; }
|
27
|
|
-Uint8 wspeek8(Uxn *u, Uint8 o) { return u->wst.dat[u->wst.ptr - o]; }
|
28
|
|
-void wspush16(Uxn *u, Uint16 s) { wspush8(u,s >> 8); wspush8(u,s & 0xff); }
|
29
|
|
-Uint16 wspop16(Uxn *u) { return wspop8(u) + (wspop8(u) << 8); }
|
30
|
|
-Uint16 wspeek16(Uxn *u, Uint8 o) { return (u->wst.dat[u->wst.ptr - o] << 8) + u->wst.dat[u->wst.ptr - o + 1]; }
|
31
|
|
-
|
32
|
|
-
|
33
|
|
-Uint8 rspop8(Uxn *u){
|
34
|
|
- return u->rst.dat[--u->rst.ptr];
|
35
|
|
-}
|
36
|
|
-
|
37
|
|
-void rspush16(Uxn *u, Uint16 a) {
|
38
|
|
-
|
39
|
|
-
|
40
|
|
- u->rst.dat[u->rst.ptr++] = (a >> 8) & 0xff;
|
41
|
|
- u->rst.dat[u->rst.ptr++] = a & 0xff;
|
42
|
|
-
|
43
|
|
-}
|
44
|
|
-Uint16 rspop16(Uxn *u) {
|
45
|
|
- return rspop8(u) + (rspop8(u) << 8);
|
46
|
|
-}
|
47
|
|
-
|
48
|
|
-
|
|
22
|
+Uint8 mempeek8(Memory *m, Uint16 a) { return m->dat[a]; }
|
|
23
|
+Uint16 mempeek16(Memory *m, Uint16 a) { return (mempeek8(m, a) << 8) + mempeek8(m, a + 1); }
|
|
24
|
+void push8(St8 *s, Uint8 a) { s->dat[s->ptr++] = a; }
|
|
25
|
+Uint8 pop8(St8 *s) { return s->dat[--s->ptr]; }
|
|
26
|
+Uint8 peek8(St8 *s, Uint8 a) { return s->dat[s->ptr - a]; }
|
|
27
|
+void push16(St8 *s, Uint16 a) { push8(s, a >> 8); push8(s, a); }
|
|
28
|
+Uint16 pop16(St8 *s) { return pop8(s) + (pop8(s) << 8); }
|
|
29
|
+Uint16 peek16(St8 *s, Uint8 a) { return (peek8(s, a) << 8) + peek8(s, a + 1); }
|
49
|
30
|
/* I/O */
|
50
|
31
|
void op_brk(Uxn *u) { setflag(&u->status,FLAG_HALT, 1); }
|
51
|
32
|
void op_li1(Uxn *u) { u->literal += 1; }
|
52
|
33
|
void op_lix(Uxn *u) { u->literal += u->ram.dat[u->ram.ptr++]; }
|
53
|
34
|
void op_nop(Uxn *u) { printf("NOP"); (void)u; }
|
54
|
|
-void op_ior(Uxn *u) { Device *dev = &u->dev[mempeek8(u, u->devr)]; if(dev) wspush8(u, dev->read(dev, wspop8(u))); }
|
55
|
|
-void op_iow(Uxn *u) { Uint8 a = wspop8(u); Device *dev = &u->dev[mempeek8(u, u->devw)]; if(dev) dev->write(dev, a); }
|
56
|
|
-void op_ldr(Uxn *u) { Uint16 a = wspop16(u); wspush8(u, u->ram.dat[a]); }
|
57
|
|
-void op_str(Uxn *u) { Uint16 a = wspop16(u); Uint8 b = wspop8(u); u->ram.dat[a] = b; }
|
|
35
|
+void op_ior(Uxn *u) { Device *dev = &u->dev[mempeek8(&u->ram, u->devr)]; if(dev) push8(&u->wst, dev->read(dev, pop8(&u->wst))); }
|
|
36
|
+void op_iow(Uxn *u) { Uint8 a = pop8(&u->wst); Device *dev = &u->dev[mempeek8(&u->ram, u->devw)]; if(dev) dev->write(dev, a); }
|
|
37
|
+void op_ldr(Uxn *u) { Uint16 a = pop16(&u->wst); push8(&u->wst, u->ram.dat[a]); }
|
|
38
|
+void op_str(Uxn *u) { Uint16 a = pop16(&u->wst); Uint8 b = pop8(&u->wst); u->ram.dat[a] = b; }
|
58
|
39
|
/* Logic */
|
59
|
|
-void op_jmp(Uxn *u) { u->ram.ptr = wspop16(u); }
|
60
|
|
-void op_jsr(Uxn *u) { rspush16(u, u->ram.ptr); u->ram.ptr = wspop16(u); }
|
61
|
|
-void op_rts(Uxn *u) { u->ram.ptr = rspop16(u); }
|
|
40
|
+void op_jmp(Uxn *u) { u->ram.ptr = pop16(&u->wst); }
|
|
41
|
+void op_jsr(Uxn *u) { push16(&u->rst, u->ram.ptr); u->ram.ptr = pop16(&u->wst); }
|
|
42
|
+void op_rts(Uxn *u) { u->ram.ptr = pop16(&u->rst); }
|
62
|
43
|
/* Stack */
|
63
|
|
-void op_pop(Uxn *u) { wspop8(u); }
|
64
|
|
-void op_dup(Uxn *u) { wspush8(u, wspeek8(u, 1)); }
|
65
|
|
-void op_swp(Uxn *u) { Uint8 b = wspop8(u), a = wspop8(u); wspush8(u, b); wspush8(u, a); }
|
66
|
|
-void op_ovr(Uxn *u) { Uint8 a = wspeek8(u,2); wspush8(u, a); }
|
67
|
|
-void op_rot(Uxn *u) { Uint8 c = wspop8(u), b = wspop8(u), a = wspop8(u); wspush8(u, b); wspush8(u, c); wspush8(u, a); }
|
68
|
|
-void op_and(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b & a); }
|
69
|
|
-void op_ora(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b | a); }
|
70
|
|
-void op_rol(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b << a); }
|
|
44
|
+void op_pop(Uxn *u) { pop8(&u->wst); }
|
|
45
|
+void op_dup(Uxn *u) { push8(&u->wst, peek8(&u->wst, 1)); }
|
|
46
|
+void op_swp(Uxn *u) { Uint8 b = pop8(&u->wst), a = pop8(&u->wst); push8(&u->wst, b); push8(&u->wst, a); }
|
|
47
|
+void op_ovr(Uxn *u) { Uint8 a = peek8(&u->wst,2); push8(&u->wst, a); }
|
|
48
|
+void op_rot(Uxn *u) { Uint8 c = pop8(&u->wst), b = pop8(&u->wst), a = pop8(&u->wst); push8(&u->wst, b); push8(&u->wst, c); push8(&u->wst, a); }
|
|
49
|
+void op_and(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b & a); }
|
|
50
|
+void op_ora(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b | a); }
|
|
51
|
+void op_rol(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b << a); }
|
71
|
52
|
/* Arithmetic */
|
72
|
|
-void op_add(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b + a); }
|
73
|
|
-void op_sub(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b - a); }
|
74
|
|
-void op_mul(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b * a); }
|
75
|
|
-void op_div(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b / a); }
|
76
|
|
-void op_equ(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b == a); }
|
77
|
|
-void op_neq(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b != a); }
|
78
|
|
-void op_gth(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b > a); }
|
79
|
|
-void op_lth(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b < a); }
|
|
53
|
+void op_add(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b + a); }
|
|
54
|
+void op_sub(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b - a); }
|
|
55
|
+void op_mul(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b * a); }
|
|
56
|
+void op_div(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b / a); }
|
|
57
|
+void op_equ(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b == a); }
|
|
58
|
+void op_neq(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b != a); }
|
|
59
|
+void op_gth(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b > a); }
|
|
60
|
+void op_lth(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b < a); }
|
80
|
61
|
/* --- */
|
81
|
|
-void op_ior16(Uxn *u) { Uint8 a = wspop8(u); Device *dev = &u->dev[mempeek8(u, u->devr)]; if(dev) wspush16(u, (dev->read(dev, a) << 8) + dev->read(dev, a + 1)); }
|
82
|
|
-void op_iow16(Uxn *u) { Uint8 a = wspop8(u); Uint8 b = wspop8(u); Device *dev = &u->dev[mempeek8(u, u->devw)]; if(dev) { dev->write(dev, b); dev->write(dev, a); } }
|
83
|
|
-void op_ldr16(Uxn *u) { Uint16 a = wspop16(u); wspush16(u, (u->ram.dat[a] << 8) + u->ram.dat[a + 1]); }
|
84
|
|
-void op_str16(Uxn *u) { Uint16 a = wspop16(u); Uint16 b = wspop16(u); u->ram.dat[a] = b >> 8; u->ram.dat[a + 1] = b & 0xff; }
|
|
62
|
+void op_ior16(Uxn *u) { Uint8 a = pop8(&u->wst); Device *dev = &u->dev[mempeek8(&u->ram, u->devr)]; if(dev) push16(&u->wst, (dev->read(dev, a) << 8) + dev->read(dev, a + 1)); }
|
|
63
|
+void op_iow16(Uxn *u) { Uint8 a = pop8(&u->wst); Uint8 b = pop8(&u->wst); Device *dev = &u->dev[mempeek8(&u->ram, u->devw)]; if(dev) { dev->write(dev, b); dev->write(dev, a); } }
|
|
64
|
+void op_ldr16(Uxn *u) { Uint16 a = pop16(&u->wst); push16(&u->wst, (u->ram.dat[a] << 8) + u->ram.dat[a + 1]); }
|
|
65
|
+void op_str16(Uxn *u) { Uint16 a = pop16(&u->wst); Uint16 b = pop16(&u->wst); u->ram.dat[a] = b >> 8; u->ram.dat[a + 1] = b & 0xff; }
|
85
|
66
|
/* Stack(16-bits) */
|
86
|
|
-void op_pop16(Uxn *u) { wspop16(u); }
|
87
|
|
-void op_dup16(Uxn *u) { wspush16(u, wspeek16(u, 2)); }
|
88
|
|
-void op_swp16(Uxn *u) { Uint16 b = wspop16(u), a = wspop16(u); wspush16(u, b); wspush16(u, a); }
|
89
|
|
-void op_ovr16(Uxn *u) { Uint16 a = wspeek16(u, 4); wspush16(u, a); }
|
90
|
|
-void op_rot16(Uxn *u) { Uint16 c = wspop16(u), b = wspop16(u), a = wspop16(u); wspush16(u, b); wspush16(u, c); wspush16(u, a); }
|
91
|
|
-void op_and16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush16(u, b & a); }
|
92
|
|
-void op_ora16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush16(u, b | a); }
|
93
|
|
-void op_rol16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush16(u, b << a); }
|
|
67
|
+void op_pop16(Uxn *u) { pop16(&u->wst); }
|
|
68
|
+void op_dup16(Uxn *u) { push16(&u->wst, peek16(&u->wst, 2)); }
|
|
69
|
+void op_swp16(Uxn *u) { Uint16 b = pop16(&u->wst), a = pop16(&u->wst); push16(&u->wst, b); push16(&u->wst, a); }
|
|
70
|
+void op_ovr16(Uxn *u) { Uint16 a = peek16(&u->wst, 4); push16(&u->wst, a); }
|
|
71
|
+void op_rot16(Uxn *u) { Uint16 c = pop16(&u->wst), b = pop16(&u->wst), a = pop16(&u->wst); push16(&u->wst, b); push16(&u->wst, c); push16(&u->wst, a); }
|
|
72
|
+void op_and16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, b & a); }
|
|
73
|
+void op_ora16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, b | a); }
|
|
74
|
+void op_rol16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, b << a); }
|
94
|
75
|
/* Arithmetic(16-bits) */
|
95
|
|
-void op_add16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush16(u, b + a); }
|
96
|
|
-void op_sub16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush16(u, b - a); }
|
97
|
|
-void op_mul16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush16(u, b * a); }
|
98
|
|
-void op_div16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush16(u, b / a); }
|
99
|
|
-void op_equ16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush8(u, b == a); }
|
100
|
|
-void op_neq16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush8(u, b != a); }
|
101
|
|
-void op_gth16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush8(u, b > a); }
|
102
|
|
-void op_lth16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush8(u, b < a); }
|
|
76
|
+void op_add16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, b + a); }
|
|
77
|
+void op_sub16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, b - a); }
|
|
78
|
+void op_mul16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, b * a); }
|
|
79
|
+void op_div16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, b / a); }
|
|
80
|
+void op_equ16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push8(&u->wst, b == a); }
|
|
81
|
+void op_neq16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push8(&u->wst, b != a); }
|
|
82
|
+void op_gth16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push8(&u->wst, b > a); }
|
|
83
|
+void op_lth16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push8(&u->wst, b < a); }
|
103
|
84
|
|
104
|
85
|
void (*ops[])(Uxn *u) = {
|
105
|
86
|
op_brk, op_nop, op_li1, op_lix, op_ior, op_iow, op_ldr, op_str,
|
...
|
...
|
@@ -139,7 +120,7 @@ lituxn(Uxn *u, Uint8 instr)
|
139
|
120
|
{
|
140
|
121
|
if(u->wst.ptr >= 255)
|
141
|
122
|
return haltuxn(u, "Stack overflow", instr);
|
142
|
|
- wspush8(u, instr);
|
|
123
|
+ push8(&u->wst, instr);
|
143
|
124
|
u->literal--;
|
144
|
125
|
return 1;
|
145
|
126
|
}
|
...
|
...
|
@@ -157,7 +138,7 @@ opcuxn(Uxn *u, Uint8 instr)
|
157
|
138
|
return haltuxn(u, "Stack underflow", op);
|
158
|
139
|
if(u->wst.ptr + opr[op][1] - opr[op][0] >= 255)
|
159
|
140
|
return haltuxn(u, "Stack overflow", instr);
|
160
|
|
- if(!getflag(&u->status, FLAG_COND) || (getflag(&u->status, FLAG_COND) && wspop8(u)))
|
|
141
|
+ if(!getflag(&u->status, FLAG_COND) || (getflag(&u->status, FLAG_COND) && pop8(&u->wst)))
|
161
|
142
|
(*ops[op])(u);
|
162
|
143
|
return 1;
|
163
|
144
|
}
|
...
|
...
|
@@ -205,9 +186,9 @@ loaduxn(Uxn *u, char *filepath)
|
205
|
186
|
fread(u->ram.dat, sizeof(u->ram.dat), 1, f);
|
206
|
187
|
u->devr = 0xfff8;
|
207
|
188
|
u->devw = 0xfff9;
|
208
|
|
- u->vreset = mempeek16(u, 0xfffa);
|
209
|
|
- u->vframe = mempeek16(u, 0xfffc);
|
210
|
|
- u->verror = mempeek16(u, 0xfffe);
|
|
189
|
+ u->vreset = mempeek16(&u->ram, 0xfffa);
|
|
190
|
+ u->vframe = mempeek16(&u->ram, 0xfffc);
|
|
191
|
+ u->verror = mempeek16(&u->ram, 0xfffe);
|
211
|
192
|
printf("Uxn loaded[%s] vrst:%04x vfrm:%04x verr:%04x.\n",
|
212
|
193
|
filepath,
|
213
|
194
|
u->vreset,
|