...
|
...
|
@@ -1,7 +1,7 @@
|
1
|
1
|
#include <stdio.h>
|
2
|
2
|
|
3
|
3
|
/*
|
4
|
|
-Copyright (c) 2021 Devine Lu Linvega
|
|
4
|
+Copyright (u) 2021 Devine Lu Linvega
|
5
|
5
|
|
6
|
6
|
Permission to use, copy, modify, and distribute this software for any
|
7
|
7
|
purpose with or without fee is hereby granted, provided that the above
|
...
|
...
|
@@ -15,85 +15,70 @@ WITH REGARD TO THIS SOFTWARE.
|
15
|
15
|
|
16
|
16
|
Uxn cpu;
|
17
|
17
|
|
18
|
|
-#pragma mark - Helpers
|
19
|
|
-
|
20
|
|
-void
|
21
|
|
-setflag(Uint8 *status, char flag, int b)
|
22
|
|
-{
|
23
|
|
- if(b)
|
24
|
|
- *status |= flag;
|
25
|
|
- else
|
26
|
|
- *status &= (~flag);
|
27
|
|
-}
|
28
|
|
-
|
29
|
|
-int
|
30
|
|
-getflag(Uint8 *status, char flag)
|
31
|
|
-{
|
32
|
|
- return *status & flag;
|
33
|
|
-}
|
34
|
|
-
|
35
|
18
|
#pragma mark - Operations
|
36
|
19
|
|
37
|
20
|
/* clang-format off */
|
38
|
21
|
|
|
22
|
+void setflag(Uint8 *st, char flag, int b) { if(b) *st |= flag; else *st &= (~flag); }
|
|
23
|
+int getflag(Uint8 *st, char flag) { return *st & flag; }
|
39
|
24
|
Uint16 bytes2short(Uint8 a, Uint8 b) { return (a << 8) + b; }
|
40
|
|
-Uint16 mempeek16(Uxn *c, Uint16 s) { return (c->ram.dat[s] << 8) + (c->ram.dat[s+1] & 0xff); }
|
41
|
|
-void wspush8(Uxn *c, Uint8 b) { c->wst.dat[c->wst.ptr++] = b; }
|
42
|
|
-void wspush16(Uxn *c, Uint16 s) { wspush8(c,s >> 8); wspush8(c,s & 0xff); }
|
43
|
|
-Uint8 wspop8(Uxn *c) { return c->wst.dat[--c->wst.ptr]; }
|
44
|
|
-Uint16 wspop16(Uxn *c) { return wspop8(c) + (wspop8(c) << 8); }
|
45
|
|
-Uint8 wspeek8(Uxn *c, Uint8 o) { return c->wst.dat[c->wst.ptr - o]; }
|
46
|
|
-Uint16 wspeek16(Uxn *c, Uint8 o) { return bytes2short(c->wst.dat[c->wst.ptr - o], c->wst.dat[c->wst.ptr - o + 1]); }
|
47
|
|
-Uint16 rspop16(Uxn *c) { return c->rst.dat[--c->rst.ptr]; }
|
48
|
|
-void rspush16(Uxn *c, Uint16 a) { c->rst.dat[c->rst.ptr++] = a; }
|
|
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 bytes2short(u->wst.dat[u->wst.ptr - o], u->wst.dat[u->wst.ptr - o + 1]); }
|
|
31
|
+void rspush16(Uxn *u, Uint16 a) { u->rst.dat[u->rst.ptr++] = a; }
|
|
32
|
+Uint16 mempeek16(Uxn *u, Uint16 s) { return (u->ram.dat[s] << 8) + (u->ram.dat[s+1] & 0xff); }
|
|
33
|
+
|
49
|
34
|
/* I/O */
|
50
|
|
-void op_brk(Uxn *c) { setflag(&c->status,FLAG_HALT, 1); }
|
51
|
|
-void op_lit(Uxn *c) { c->literal += c->ram.dat[c->ram.ptr++]; }
|
52
|
|
-void op_nop(Uxn *c) { (void)c; printf("NOP");}
|
53
|
|
-void op_ldr(Uxn *c) { wspush8(c, c->ram.dat[wspop16(c)]); }
|
54
|
|
-void op_str(Uxn *c) { c->ram.dat[wspop16(c)] = wspop8(c); }
|
|
35
|
+void op_brk(Uxn *u) { setflag(&u->status,FLAG_HALT, 1); }
|
|
36
|
+void op_lit(Uxn *u) { u->literal += u->ram.dat[u->ram.ptr++]; }
|
|
37
|
+void op_nop(Uxn *u) { printf("NOP"); (void)u; }
|
|
38
|
+void op_ldr(Uxn *u) { wspush8(u, u->ram.dat[wspop16(u)]); }
|
|
39
|
+void op_str(Uxn *u) { u->ram.dat[wspop16(u)] = wspop8(u); }
|
55
|
40
|
/* Logic */
|
56
|
|
-void op_jmp(Uxn *c) { c->ram.ptr = wspop16(c); }
|
57
|
|
-void op_jsr(Uxn *c) { rspush16(c, c->ram.ptr); c->ram.ptr = wspop16(c); }
|
58
|
|
-void op_rts(Uxn *c) { c->ram.ptr = rspop16(c); }
|
|
41
|
+void op_jmp(Uxn *u) { u->ram.ptr = wspop16(u); }
|
|
42
|
+void op_jsr(Uxn *u) { u->rst.dat[u->rst.ptr++] = u->ram.ptr; u->ram.ptr = wspop16(u); }
|
|
43
|
+void op_rts(Uxn *u) { u->ram.ptr = u->rst.dat[--u->rst.ptr]; }
|
59
|
44
|
/* Stack */
|
60
|
|
-void op_pop(Uxn *c) { wspop8(c); }
|
61
|
|
-void op_dup(Uxn *c) { wspush8(c,wspeek8(c,1)); }
|
62
|
|
-void op_swp(Uxn *c) { Uint8 b = wspop8(c), a = wspop8(c); wspush8(c,b); wspush8(c,a); }
|
63
|
|
-void op_ovr(Uxn *c) { Uint8 a = wspeek8(c,2); wspush8(c,a); }
|
64
|
|
-void op_rot(Uxn *c) { Uint8 c1 = wspop8(c),b = wspop8(c),a = wspop8(c); wspush8(c,b); wspush8(c, c1); wspush8(c,a); }
|
65
|
|
-void op_and(Uxn *c) { Uint8 a = wspop8(c), b = wspop8(c); wspush8(c,a & b); }
|
66
|
|
-void op_ora(Uxn *c) { Uint8 a = wspop8(c), b = wspop8(c); wspush8(c,a | b); }
|
67
|
|
-void op_rol(Uxn *c) { Uint8 a = wspop8(c), b = wspop8(c); wspush8(c,a << b); }
|
|
45
|
+void op_pop(Uxn *u) { wspop8(u); }
|
|
46
|
+void op_dup(Uxn *u) { wspush8(u, wspeek8(u, 1)); }
|
|
47
|
+void op_swp(Uxn *u) { Uint8 b = wspop8(u), a = wspop8(u); wspush8(u, b); wspush8(u, a); }
|
|
48
|
+void op_ovr(Uxn *u) { Uint8 a = wspeek8(u,2); wspush8(u, a); }
|
|
49
|
+void op_rot(Uxn *u) { Uint8 c = wspop8(u), b = wspop8(u), a = wspop8(u); wspush8(u, b); wspush8(u, c); wspush8(u, a); }
|
|
50
|
+void op_and(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b & a); }
|
|
51
|
+void op_ora(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b | a); }
|
|
52
|
+void op_rol(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b << a); }
|
68
|
53
|
/* Arithmetic */
|
69
|
|
-void op_add(Uxn *c) { Uint8 a = wspop8(c), b = wspop8(c); wspush8(c,b + a); }
|
70
|
|
-void op_sub(Uxn *c) { Uint8 a = wspop8(c), b = wspop8(c); wspush8(c,b - a); }
|
71
|
|
-void op_mul(Uxn *c) { Uint8 a = wspop8(c), b = wspop8(c); wspush8(c,b * a); }
|
72
|
|
-void op_div(Uxn *c) { Uint8 a = wspop8(c), b = wspop8(c); wspush8(c,b / a); }
|
73
|
|
-void op_equ(Uxn *c) { Uint8 a = wspop8(c), b = wspop8(c); wspush8(c,b == a); }
|
74
|
|
-void op_neq(Uxn *c) { Uint8 a = wspop8(c), b = wspop8(c); wspush8(c,b != a); }
|
75
|
|
-void op_gth(Uxn *c) { Uint8 a = wspop8(c), b = wspop8(c); wspush8(c,b > a); }
|
76
|
|
-void op_lth(Uxn *c) { Uint8 a = wspop8(c), b = wspop8(c); wspush8(c,b < a); }
|
|
54
|
+void op_add(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b + a); }
|
|
55
|
+void op_sub(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b - a); }
|
|
56
|
+void op_mul(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b * a); }
|
|
57
|
+void op_div(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b / a); }
|
|
58
|
+void op_equ(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b == a); }
|
|
59
|
+void op_neq(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b != a); }
|
|
60
|
+void op_gth(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b > a); }
|
|
61
|
+void op_lth(Uxn *u) { Uint8 a = wspop8(u), b = wspop8(u); wspush8(u, b < a); }
|
77
|
62
|
/* Stack(16-bits) */
|
78
|
|
-void op_pop16(Uxn *c) { wspop16(c); }
|
79
|
|
-void op_dup16(Uxn *c) { wspush16(c,wspeek16(c,2)); }
|
80
|
|
-void op_swp16(Uxn *c) { Uint16 b = wspop16(c), a = wspop16(c); wspush16(c,b); wspush16(c,a); }
|
81
|
|
-void op_ovr16(Uxn *c) { Uint16 a = wspeek16(c, 4); wspush16(c,a); }
|
82
|
|
-void op_rot16(Uxn *c) { Uint16 c1 = wspop16(c), b = wspop16(c), a = wspop16(c); wspush16(c,b); wspush16(c, c1); wspush16(c,a); }
|
83
|
|
-void op_and16(Uxn *c) { Uint16 a = wspop16(c), b = wspop16(c); wspush16(c,a & b); }
|
84
|
|
-void op_ora16(Uxn *c) { Uint16 a = wspop16(c), b = wspop16(c); wspush16(c,a | b); }
|
85
|
|
-void op_rol16(Uxn *c) { Uint16 a = wspop16(c), b = wspop16(c); wspush16(c,a << b); }
|
|
63
|
+void op_pop16(Uxn *u) { wspop16(u); }
|
|
64
|
+void op_dup16(Uxn *u) { wspush16(u, wspeek16(u, 2)); }
|
|
65
|
+void op_swp16(Uxn *u) { Uint16 b = wspop16(u), a = wspop16(u); wspush16(u, b); wspush16(u, a); }
|
|
66
|
+void op_ovr16(Uxn *u) { Uint16 a = wspeek16(u, 4); wspush16(u, a); }
|
|
67
|
+void op_rot16(Uxn *u) { Uint16 c = wspop16(u), b = wspop16(u), a = wspop16(u); wspush16(u, b); wspush16(u, c); wspush16(u, a); }
|
|
68
|
+void op_and16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush16(u, b & a); }
|
|
69
|
+void op_ora16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush16(u, b | a); }
|
|
70
|
+void op_rol16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush16(u, b << a); }
|
86
|
71
|
/* Arithmetic(16-bits) */
|
87
|
|
-void op_add16(Uxn *c) { Uint16 a = wspop16(c), b = wspop16(c); wspush16(c,b + a); }
|
88
|
|
-void op_sub16(Uxn *c) { Uint16 a = wspop16(c), b = wspop16(c); wspush16(c,b - a); }
|
89
|
|
-void op_mul16(Uxn *c) { Uint16 a = wspop16(c), b = wspop16(c); wspush16(c,b * a); }
|
90
|
|
-void op_div16(Uxn *c) { Uint16 a = wspop16(c), b = wspop16(c); wspush16(c,b / a); }
|
91
|
|
-void op_equ16(Uxn *c) { Uint16 a = wspop16(c), b = wspop16(c); wspush8(c,b == a); }
|
92
|
|
-void op_neq16(Uxn *c) { Uint16 a = wspop16(c), b = wspop16(c); wspush8(c,b != a); }
|
93
|
|
-void op_gth16(Uxn *c) { Uint16 a = wspop16(c), b = wspop16(c); wspush8(c,b > a); }
|
94
|
|
-void op_lth16(Uxn *c) { Uint16 a = wspop16(c), b = wspop16(c); wspush8(c,b < a); }
|
95
|
|
-
|
96
|
|
-void (*ops[])(Uxn *c) = {
|
|
72
|
+void op_add16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush16(u, b + a); }
|
|
73
|
+void op_sub16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush16(u, b - a); }
|
|
74
|
+void op_mul16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush16(u, b * a); }
|
|
75
|
+void op_div16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush16(u, b / a); }
|
|
76
|
+void op_equ16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush8(u, b == a); }
|
|
77
|
+void op_neq16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush8(u, b != a); }
|
|
78
|
+void op_gth16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush8(u, b > a); }
|
|
79
|
+void op_lth16(Uxn *u) { Uint16 a = wspop16(u), b = wspop16(u); wspush8(u, b < a); }
|
|
80
|
+
|
|
81
|
+void (*ops[])(Uxn *u) = {
|
97
|
82
|
op_brk, op_lit, op_nop, op_nop, op_nop, op_nop, op_ldr, op_str,
|
98
|
83
|
op_jmp, op_jsr, op_nop, op_rts, op_nop, op_nop, op_nop, op_nop,
|
99
|
84
|
op_pop, op_dup, op_swp, op_ovr, op_rot, op_and, op_ora, op_rol,
|
...
|
...
|
@@ -114,97 +99,97 @@ Uint8 opr[][2] = {
|
114
|
99
|
/* clang-format on */
|
115
|
100
|
|
116
|
101
|
int
|
117
|
|
-error(Uxn *c, char *name, int id)
|
|
102
|
+error(Uxn *u, char *name, int id)
|
118
|
103
|
{
|
119
|
|
- printf("Error: %s#%04x, at 0x%04x\n", name, id, c->counter);
|
|
104
|
+ printf("Error: %s#%04x, at 0x%04x\n", name, id, u->counter);
|
120
|
105
|
return 0;
|
121
|
106
|
}
|
122
|
107
|
|
123
|
108
|
int
|
124
|
|
-doliteral(Uxn *c, Uint8 instr)
|
|
109
|
+doliteral(Uxn *u, Uint8 instr)
|
125
|
110
|
{
|
126
|
|
- if(c->wst.ptr >= 255)
|
127
|
|
- return error(c, "Stack overflow", instr);
|
128
|
|
- wspush8(c, instr);
|
129
|
|
- c->literal--;
|
|
111
|
+ if(u->wst.ptr >= 255)
|
|
112
|
+ return error(u, "Stack overflow", instr);
|
|
113
|
+ wspush8(u, instr);
|
|
114
|
+ u->literal--;
|
130
|
115
|
return 1;
|
131
|
116
|
}
|
132
|
117
|
|
133
|
118
|
int
|
134
|
|
-dodevices(Uxn *c) /* experimental */
|
|
119
|
+dodevices(Uxn *u) /* experimental */
|
135
|
120
|
{
|
136
|
|
- if(c->ram.dat[0xfff1]) {
|
137
|
|
- printf("%c", c->ram.dat[0xfff1]);
|
138
|
|
- c->ram.dat[0xfff1] = 0x00;
|
|
121
|
+ if(u->ram.dat[0xfff1]) {
|
|
122
|
+ printf("%c", u->ram.dat[0xfff1]);
|
|
123
|
+ u->ram.dat[0xfff1] = 0x00;
|
139
|
124
|
}
|
140
|
125
|
return 1;
|
141
|
126
|
}
|
142
|
127
|
|
143
|
128
|
int
|
144
|
|
-doopcode(Uxn *c, Uint8 instr)
|
|
129
|
+doopcode(Uxn *u, Uint8 instr)
|
145
|
130
|
{
|
146
|
131
|
Uint8 op = instr & 0x1f;
|
147
|
|
- setflag(&c->status, FLAG_SHORT, (instr >> 5) & 1);
|
148
|
|
- setflag(&c->status, FLAG_SIGN, (instr >> 6) & 1); /* usused */
|
149
|
|
- setflag(&c->status, FLAG_COND, (instr >> 7) & 1);
|
150
|
|
- if(getflag(&c->status, FLAG_SHORT))
|
|
132
|
+ setflag(&u->status, FLAG_SHORT, (instr >> 5) & 1);
|
|
133
|
+ setflag(&u->status, FLAG_SIGN, (instr >> 6) & 1); /* usused */
|
|
134
|
+ setflag(&u->status, FLAG_COND, (instr >> 7) & 1);
|
|
135
|
+ if(getflag(&u->status, FLAG_SHORT))
|
151
|
136
|
op += 16;
|
152
|
|
- if(c->wst.ptr < opr[op][0])
|
153
|
|
- return error(c, "Stack underflow", op);
|
154
|
|
- if(c->wst.ptr + opr[op][1] - opr[op][0] >= 255)
|
155
|
|
- return error(c, "Stack overflow", instr);
|
156
|
|
- if(!getflag(&c->status, FLAG_COND) || (getflag(&c->status, FLAG_COND) && wspop8(c)))
|
157
|
|
- (*ops[op])(c);
|
158
|
|
- dodevices(c);
|
|
137
|
+ if(u->wst.ptr < opr[op][0])
|
|
138
|
+ return error(u, "Stack underflow", op);
|
|
139
|
+ if(u->wst.ptr + opr[op][1] - opr[op][0] >= 255)
|
|
140
|
+ return error(u, "Stack overflow", instr);
|
|
141
|
+ if(!getflag(&u->status, FLAG_COND) || (getflag(&u->status, FLAG_COND) && wspop8(u)))
|
|
142
|
+ (*ops[op])(u);
|
|
143
|
+ dodevices(u);
|
159
|
144
|
return 1;
|
160
|
145
|
}
|
161
|
146
|
|
162
|
147
|
int
|
163
|
|
-eval(Uxn *c)
|
|
148
|
+eval(Uxn *u)
|
164
|
149
|
{
|
165
|
|
- Uint8 instr = c->ram.dat[c->ram.ptr++];
|
166
|
|
- if(c->literal > 0)
|
167
|
|
- return doliteral(c, instr);
|
|
150
|
+ Uint8 instr = u->ram.dat[u->ram.ptr++];
|
|
151
|
+ if(u->literal > 0)
|
|
152
|
+ return doliteral(u, instr);
|
168
|
153
|
else
|
169
|
|
- return doopcode(c, instr);
|
|
154
|
+ return doopcode(u, instr);
|
170
|
155
|
return 1;
|
171
|
156
|
}
|
172
|
157
|
|
173
|
158
|
int
|
174
|
|
-load(Uxn *c, char *filepath)
|
|
159
|
+load(Uxn *u, char *filepath)
|
175
|
160
|
{
|
176
|
161
|
FILE *f;
|
177
|
162
|
if(!(f = fopen(filepath, "rb")))
|
178
|
|
- return error(c, "Missing input.", 0);
|
179
|
|
- fread(c->ram.dat, sizeof(c->ram.dat), 1, f);
|
|
163
|
+ return error(u, "Missing input.", 0);
|
|
164
|
+ fread(u->ram.dat, sizeof(u->ram.dat), 1, f);
|
180
|
165
|
return 1;
|
181
|
166
|
}
|
182
|
167
|
|
183
|
168
|
void
|
184
|
|
-reset(Uxn *c)
|
|
169
|
+reset(Uxn *u)
|
185
|
170
|
{
|
186
|
171
|
size_t i;
|
187
|
|
- char *cptr = (char *)c;
|
188
|
|
- for(i = 0; i < sizeof c; i++)
|
|
172
|
+ char *cptr = (char *)u;
|
|
173
|
+ for(i = 0; i < sizeof u; i++)
|
189
|
174
|
cptr[i] = 0;
|
190
|
175
|
}
|
191
|
176
|
|
192
|
177
|
int
|
193
|
|
-boot(Uxn *c)
|
|
178
|
+boot(Uxn *u)
|
194
|
179
|
{
|
195
|
|
- reset(c);
|
196
|
|
- c->vreset = mempeek16(c, 0xfffa);
|
197
|
|
- c->vframe = mempeek16(c, 0xfffc);
|
198
|
|
- c->verror = mempeek16(c, 0xfffe);
|
|
180
|
+ reset(u);
|
|
181
|
+ u->vreset = mempeek16(u, 0xfffa);
|
|
182
|
+ u->vframe = mempeek16(u, 0xfffc);
|
|
183
|
+ u->verror = mempeek16(u, 0xfffe);
|
199
|
184
|
/* eval reset */
|
200
|
|
- c->ram.ptr = c->vreset;
|
201
|
|
- setflag(&c->status, FLAG_HALT, 0);
|
202
|
|
- while(!(c->status & FLAG_HALT) && eval(c))
|
203
|
|
- c->counter++;
|
|
185
|
+ u->ram.ptr = u->vreset;
|
|
186
|
+ setflag(&u->status, FLAG_HALT, 0);
|
|
187
|
+ while(!(u->status & FLAG_HALT) && eval(u))
|
|
188
|
+ u->counter++;
|
204
|
189
|
/* eval frame */
|
205
|
|
- c->ram.ptr = c->vframe;
|
206
|
|
- setflag(&c->status, FLAG_HALT, 0);
|
207
|
|
- while(!(c->status & FLAG_HALT) && eval(c))
|
208
|
|
- c->counter++;
|
|
190
|
+ u->ram.ptr = u->vframe;
|
|
191
|
+ setflag(&u->status, FLAG_HALT, 0);
|
|
192
|
+ while(!(u->status & FLAG_HALT) && eval(u))
|
|
193
|
+ u->counter++;
|
209
|
194
|
return 1;
|
210
|
195
|
}
|