... | ... |
@@ -3,6 +3,8 @@ |
3 | 3 |
# format code |
4 | 4 |
clang-format -i uxnasm.c |
5 | 5 |
clang-format -i uxn.c |
6 |
+clang-format -i cpu.h |
|
7 |
+clang-format -i cpu.c |
|
6 | 8 |
|
7 | 9 |
# remove old |
8 | 10 |
rm -f ./uxnasm |
... | ... |
@@ -11,8 +13,8 @@ rm -f ./boot.rom |
11 | 13 |
|
12 | 14 |
# debug(slow) |
13 | 15 |
cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined uxnasm.c -o uxnasm |
14 |
-cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined uxn.c -o uxn |
|
16 |
+cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined cpu.c uxn.c -o uxn |
|
15 | 17 |
|
16 | 18 |
# run |
17 |
-./uxnasm examples/test.usm boot.rom |
|
19 |
+./uxnasm examples/hello.usm boot.rom |
|
18 | 20 |
./uxn boot.rom |
19 | 21 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,240 @@ |
1 |
+#include <stdio.h> |
|
2 |
+ |
|
3 |
+#define FLAG_HALT 0x01 |
|
4 |
+#define FLAG_SHORT 0x02 |
|
5 |
+#define FLAG_SIGN 0x04 |
|
6 |
+#define FLAG_COND 0x08 |
|
7 |
+ |
|
8 |
+#include "cpu.h" |
|
9 |
+ |
|
10 |
+Computer cpu; |
|
11 |
+ |
|
12 |
+/* |
|
13 |
+Copyright (c) 2021 Devine Lu Linvega |
|
14 |
+ |
|
15 |
+Permission to use, copy, modify, and distribute this software for any |
|
16 |
+purpose with or without fee is hereby granted, provided that the above |
|
17 |
+copyright notice and this permission notice appear in all copies. |
|
18 |
+ |
|
19 |
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|
20 |
+WITH REGARD TO THIS SOFTWARE. |
|
21 |
+*/ |
|
22 |
+ |
|
23 |
+#pragma mark - Helpers |
|
24 |
+ |
|
25 |
+void |
|
26 |
+setflag(char flag, int b) |
|
27 |
+{ |
|
28 |
+ if(b) |
|
29 |
+ cpu.status |= flag; |
|
30 |
+ else |
|
31 |
+ cpu.status &= (~flag); |
|
32 |
+} |
|
33 |
+ |
|
34 |
+int |
|
35 |
+getflag(char flag) |
|
36 |
+{ |
|
37 |
+ return cpu.status & flag; |
|
38 |
+} |
|
39 |
+ |
|
40 |
+void |
|
41 |
+echos(Stack8 *s, Uint8 len, char *name) |
|
42 |
+{ |
|
43 |
+ int i; |
|
44 |
+ printf("\n%s\n", name); |
|
45 |
+ for(i = 0; i < len; ++i) { |
|
46 |
+ if(i % 16 == 0) |
|
47 |
+ printf("\n"); |
|
48 |
+ printf("%02x%c", s->dat[i], s->ptr == i ? '<' : ' '); |
|
49 |
+ } |
|
50 |
+ printf("\n\n"); |
|
51 |
+} |
|
52 |
+ |
|
53 |
+void |
|
54 |
+echom(Memory *m, Uint8 len, char *name) |
|
55 |
+{ |
|
56 |
+ int i; |
|
57 |
+ printf("\n%s\n", name); |
|
58 |
+ for(i = 0; i < len; ++i) { |
|
59 |
+ if(i % 16 == 0) |
|
60 |
+ printf("\n"); |
|
61 |
+ printf("%02x ", m->dat[i]); |
|
62 |
+ } |
|
63 |
+ printf("\n\n"); |
|
64 |
+} |
|
65 |
+ |
|
66 |
+#pragma mark - Operations |
|
67 |
+ |
|
68 |
+/* clang-format off */ |
|
69 |
+ |
|
70 |
+Uint16 bytes2short(Uint8 a, Uint8 b) { return (a << 8) + b; } |
|
71 |
+Uint16 mempeek16(Uint16 s) { return (cpu.ram.dat[s] << 8) + (cpu.ram.dat[s+1] & 0xff); } |
|
72 |
+void wspush8(Uint8 b) { cpu.wst.dat[cpu.wst.ptr++] = b; } |
|
73 |
+void wspush16(Uint16 s) { wspush8(s >> 8); wspush8(s & 0xff); } |
|
74 |
+Uint8 wspop8(void) { return cpu.wst.dat[--cpu.wst.ptr]; } |
|
75 |
+Uint16 wspop16(void) { return wspop8() + (wspop8() << 8); } |
|
76 |
+Uint8 wspeek8(Uint8 o) { return cpu.wst.dat[cpu.wst.ptr - o]; } |
|
77 |
+Uint16 wspeek16(Uint8 o) { return bytes2short(cpu.wst.dat[cpu.wst.ptr - o], cpu.wst.dat[cpu.wst.ptr - o + 1]); } |
|
78 |
+Uint16 rspop16(void) { return cpu.rst.dat[--cpu.rst.ptr]; } |
|
79 |
+void rspush16(Uint16 a) { cpu.rst.dat[cpu.rst.ptr++] = a; } |
|
80 |
+ |
|
81 |
+/* I/O */ |
|
82 |
+void op_brk() { setflag(FLAG_HALT, 1); } |
|
83 |
+void op_lit() { cpu.literal += cpu.ram.dat[cpu.ram.ptr++]; } |
|
84 |
+void op_nop() { printf("NOP");} |
|
85 |
+void op_ldr() { wspush8(cpu.ram.dat[wspop16()]); } |
|
86 |
+void op_str() { cpu.ram.dat[wspop16()] = wspop8(); } |
|
87 |
+/* Logic */ |
|
88 |
+void op_jmp() { cpu.ram.ptr = wspop16(); } |
|
89 |
+void op_jsr() { rspush16(cpu.ram.ptr); cpu.ram.ptr = wspop16(); } |
|
90 |
+void op_rts() { cpu.ram.ptr = rspop16(); } |
|
91 |
+/* Stack */ |
|
92 |
+void op_pop() { wspop8(); } |
|
93 |
+void op_dup() { wspush8(wspeek8(1)); } |
|
94 |
+void op_swp() { Uint8 b = wspop8(), a = wspop8(); wspush8(b); wspush8(a); } |
|
95 |
+void op_ovr() { Uint8 a = wspeek8(2); wspush8(a); } |
|
96 |
+void op_rot() { Uint8 c = wspop8(),b = wspop8(),a = wspop8(); wspush8(b); wspush8(c); wspush8(a); } |
|
97 |
+void op_and() { Uint8 a = wspop8(), b = wspop8(); wspush8(a & b); } |
|
98 |
+void op_ora() { Uint8 a = wspop8(), b = wspop8(); wspush8(a | b); } |
|
99 |
+void op_rol() { Uint8 a = wspop8(), b = wspop8(); wspush8(a << b); } |
|
100 |
+/* Arithmetic */ |
|
101 |
+void op_add() { Uint8 a = wspop8(), b = wspop8(); wspush8(b + a); } |
|
102 |
+void op_sub() { Uint8 a = wspop8(), b = wspop8(); wspush8(b - a); } |
|
103 |
+void op_mul() { Uint8 a = wspop8(), b = wspop8(); wspush8(b * a); } |
|
104 |
+void op_div() { Uint8 a = wspop8(), b = wspop8(); wspush8(b / a); } |
|
105 |
+void op_equ() { Uint8 a = wspop8(), b = wspop8(); wspush8(b == a); } |
|
106 |
+void op_neq() { Uint8 a = wspop8(), b = wspop8(); wspush8(b != a); } |
|
107 |
+void op_gth() { Uint8 a = wspop8(), b = wspop8(); wspush8(b > a); } |
|
108 |
+void op_lth() { Uint8 a = wspop8(), b = wspop8(); wspush8(b < a); } |
|
109 |
+/* Stack(16-bits) */ |
|
110 |
+void op_pop16() { wspop16(); } |
|
111 |
+void op_dup16() { wspush16(wspeek16(2)); } |
|
112 |
+void op_swp16() { Uint16 b = wspop16(), a = wspop16(); wspush16(b); wspush16(a); } |
|
113 |
+void op_ovr16() { Uint16 a = wspeek16(4); wspush16(a); } |
|
114 |
+void op_rot16() { Uint16 c = wspop16(), b = wspop16(), a = wspop16(); wspush16(b); wspush16(c); wspush16(a); } |
|
115 |
+void op_and16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a & b); } |
|
116 |
+void op_ora16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a | b); } |
|
117 |
+void op_rol16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a << b); } |
|
118 |
+/* Arithmetic(16-bits) */ |
|
119 |
+void op_add16() { Uint16 a = wspop16(), b = wspop16(); wspush16(b + a); } |
|
120 |
+void op_sub16() { Uint16 a = wspop16(), b = wspop16(); wspush16(b - a); } |
|
121 |
+void op_mul16() { Uint16 a = wspop16(), b = wspop16(); wspush16(b * a); } |
|
122 |
+void op_div16() { Uint16 a = wspop16(), b = wspop16(); wspush16(b / a); } |
|
123 |
+void op_equ16() { Uint16 a = wspop16(), b = wspop16(); wspush8(b == a); } |
|
124 |
+void op_neq16() { Uint16 a = wspop16(), b = wspop16(); wspush8(b != a); } |
|
125 |
+void op_gth16() { Uint16 a = wspop16(), b = wspop16(); wspush8(b > a); } |
|
126 |
+void op_lth16() { Uint16 a = wspop16(), b = wspop16(); wspush8(b < a); } |
|
127 |
+ |
|
128 |
+void (*ops[])() = { |
|
129 |
+ op_brk, op_lit, op_nop, op_nop, op_nop, op_nop, op_ldr, op_str, |
|
130 |
+ op_jmp, op_jsr, op_nop, op_rts, op_nop, op_nop, op_nop, op_nop, |
|
131 |
+ op_pop, op_dup, op_swp, op_ovr, op_rot, op_and, op_ora, op_rol, |
|
132 |
+ op_add, op_sub, op_mul, op_div, op_equ, op_neq, op_gth, op_lth, |
|
133 |
+ op_pop16, op_dup16, op_swp16, op_ovr16, op_rot16, op_and16, op_ora16, op_rol16, |
|
134 |
+ op_add16, op_sub16, op_mul16, op_div16, op_equ16, op_neq16, op_gth16, op_lth16 |
|
135 |
+}; |
|
136 |
+ |
|
137 |
+Uint8 opr[][2] = { |
|
138 |
+ {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {2,1}, {3,0}, |
|
139 |
+ {2,0}, {2,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, |
|
140 |
+ {1,0}, {1,2}, {2,2}, {3,3}, {3,3}, {2,1}, {2,1}, {2,1}, |
|
141 |
+ {2,1}, {2,1}, {2,1}, {2,1}, {2,1}, {2,1}, {2,1}, {2,1}, |
|
142 |
+ {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, /* TODO */ |
|
143 |
+ {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0} /* TODO */ |
|
144 |
+}; |
|
145 |
+ |
|
146 |
+/* clang-format on */ |
|
147 |
+ |
|
148 |
+int |
|
149 |
+error(char *name, int id) |
|
150 |
+{ |
|
151 |
+ printf("Error: %s[%04x], at 0x%04x\n", name, id, cpu.counter); |
|
152 |
+ return 0; |
|
153 |
+} |
|
154 |
+ |
|
155 |
+int |
|
156 |
+doliteral(Uint8 instr) |
|
157 |
+{ |
|
158 |
+ if(cpu.wst.ptr >= 255) |
|
159 |
+ return error("Stack overflow", instr); |
|
160 |
+ wspush8(instr); |
|
161 |
+ cpu.literal--; |
|
162 |
+ return 1; |
|
163 |
+} |
|
164 |
+ |
|
165 |
+int |
|
166 |
+dodevices(void) /* experimental */ |
|
167 |
+{ |
|
168 |
+ if(cpu.ram.dat[0xfff1]) { |
|
169 |
+ printf("%c", cpu.ram.dat[0xfff1]); |
|
170 |
+ cpu.ram.dat[0xfff1] = 0x00; |
|
171 |
+ } |
|
172 |
+ return 1; |
|
173 |
+} |
|
174 |
+ |
|
175 |
+int |
|
176 |
+doopcode(Uint8 instr) |
|
177 |
+{ |
|
178 |
+ Uint8 op = instr & 0x1f; |
|
179 |
+ setflag(FLAG_SHORT, (instr >> 5) & 1); |
|
180 |
+ setflag(FLAG_SIGN, (instr >> 6) & 1); /* usused */ |
|
181 |
+ setflag(FLAG_COND, (instr >> 7) & 1); |
|
182 |
+ if(getflag(FLAG_SHORT)) |
|
183 |
+ op += 16; |
|
184 |
+ if(cpu.wst.ptr < opr[op][0]) |
|
185 |
+ return error("Stack underflow", op); |
|
186 |
+ if(cpu.wst.ptr + opr[op][1] - opr[op][0] >= 255) |
|
187 |
+ return error("Stack overflow", instr); |
|
188 |
+ if(!getflag(FLAG_COND) || (getflag(FLAG_COND) && wspop8())) |
|
189 |
+ (*ops[op])(); |
|
190 |
+ dodevices(); |
|
191 |
+ return 1; |
|
192 |
+} |
|
193 |
+ |
|
194 |
+int |
|
195 |
+eval(void) |
|
196 |
+{ |
|
197 |
+ Uint8 instr = cpu.ram.dat[cpu.ram.ptr++]; |
|
198 |
+ if(cpu.literal > 0) |
|
199 |
+ return doliteral(instr); |
|
200 |
+ else |
|
201 |
+ return doopcode(instr); |
|
202 |
+ return 1; |
|
203 |
+} |
|
204 |
+ |
|
205 |
+int |
|
206 |
+load(FILE *f) |
|
207 |
+{ |
|
208 |
+ fread(cpu.ram.dat, sizeof(cpu.ram.dat), 1, f); |
|
209 |
+ return 1; |
|
210 |
+} |
|
211 |
+ |
|
212 |
+void |
|
213 |
+echof(void) |
|
214 |
+{ |
|
215 |
+ printf("ended @ %d steps | hf: %x sf: %x sf: %x cf: %x\n", |
|
216 |
+ cpu.counter, |
|
217 |
+ getflag(FLAG_HALT) != 0, |
|
218 |
+ getflag(FLAG_SHORT) != 0, |
|
219 |
+ getflag(FLAG_SIGN) != 0, |
|
220 |
+ getflag(FLAG_COND) != 0); |
|
221 |
+} |
|
222 |
+ |
|
223 |
+int |
|
224 |
+boot(void) |
|
225 |
+{ |
|
226 |
+ cpu.vreset = mempeek16(0xfffa); |
|
227 |
+ cpu.vframe = mempeek16(0xfffc); |
|
228 |
+ cpu.verror = mempeek16(0xfffe); |
|
229 |
+ /* eval reset */ |
|
230 |
+ cpu.ram.ptr = cpu.vreset; |
|
231 |
+ setflag(FLAG_HALT, 0); |
|
232 |
+ while(!(cpu.status & FLAG_HALT) && eval()) |
|
233 |
+ cpu.counter++; |
|
234 |
+ /*eval frame */ |
|
235 |
+ cpu.ram.ptr = cpu.vframe; |
|
236 |
+ setflag(FLAG_HALT, 0); |
|
237 |
+ while(!(cpu.status & FLAG_HALT) && eval()) |
|
238 |
+ cpu.counter++; |
|
239 |
+ return 1; |
|
240 |
+} |
0 | 241 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,34 @@ |
1 |
+#include <stdio.h> |
|
2 |
+ |
|
3 |
+typedef unsigned char Uint8; |
|
4 |
+typedef unsigned short Uint16; |
|
5 |
+ |
|
6 |
+typedef struct { |
|
7 |
+ Uint8 ptr; |
|
8 |
+ Uint8 dat[256]; |
|
9 |
+} Stack8; |
|
10 |
+ |
|
11 |
+typedef struct { |
|
12 |
+ Uint8 ptr; |
|
13 |
+ Uint16 dat[256]; |
|
14 |
+} Stack16; |
|
15 |
+ |
|
16 |
+typedef struct { |
|
17 |
+ Uint16 ptr; |
|
18 |
+ Uint8 dat[65536]; |
|
19 |
+} Memory; |
|
20 |
+ |
|
21 |
+typedef struct { |
|
22 |
+ Uint8 literal, status; |
|
23 |
+ Uint16 counter, vreset, vframe, verror; |
|
24 |
+ Stack8 wst; |
|
25 |
+ Stack16 rst; |
|
26 |
+ Memory ram; |
|
27 |
+} Computer; |
|
28 |
+ |
|
29 |
+int error(char *name, int id); |
|
30 |
+int load(FILE *f); |
|
31 |
+int boot(void); |
|
32 |
+void echof(void); |
|
33 |
+void echom(Memory *m, Uint8 len, char *name); |
|
34 |
+void echos(Stack8 *s, Uint8 len, char *name); |
... | ... |
@@ -1,4 +1,5 @@ |
1 | 1 |
#include <stdio.h> |
2 |
+#include "cpu.h" |
|
2 | 3 |
|
3 | 4 |
/* |
4 | 5 |
Copyright (c) 2021 Devine Lu Linvega |
... | ... |
@@ -11,262 +12,11 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 | 12 |
WITH REGARD TO THIS SOFTWARE. |
12 | 13 |
*/ |
13 | 14 |
|
14 |
-#define FLAG_HALT 0x01 |
|
15 |
-#define FLAG_SHORT 0x02 |
|
16 |
-#define FLAG_SIGN 0x04 |
|
17 |
-#define FLAG_COND 0x08 |
|
18 |
- |
|
19 |
-typedef unsigned char Uint8; |
|
20 |
-typedef unsigned short Uint16; |
|
21 |
- |
|
22 |
-typedef struct { |
|
23 |
- Uint8 ptr; |
|
24 |
- Uint8 dat[256]; |
|
25 |
-} Stack8; |
|
26 |
- |
|
27 |
-typedef struct { |
|
28 |
- Uint8 ptr; |
|
29 |
- Uint16 dat[256]; |
|
30 |
-} Stack16; |
|
31 |
- |
|
32 |
-typedef struct { |
|
33 |
- Uint16 ptr; |
|
34 |
- Uint8 dat[65536]; |
|
35 |
-} Memory; |
|
36 |
- |
|
37 |
-typedef struct { |
|
38 |
- Uint8 literal, status; |
|
39 |
- Uint16 counter, vreset, vframe, verror; |
|
40 |
- Stack8 wst; |
|
41 |
- Stack16 rst; |
|
42 |
- Memory ram; |
|
43 |
-} Computer; |
|
44 |
- |
|
45 |
-Computer cpu; |
|
46 |
- |
|
47 |
-#pragma mark - Helpers |
|
48 |
- |
|
49 |
-void |
|
50 |
-setflag(char flag, int b) |
|
51 |
-{ |
|
52 |
- if(b) |
|
53 |
- cpu.status |= flag; |
|
54 |
- else |
|
55 |
- cpu.status &= (~flag); |
|
56 |
-} |
|
57 |
- |
|
58 |
-int |
|
59 |
-getflag(char flag) |
|
60 |
-{ |
|
61 |
- return cpu.status & flag; |
|
62 |
-} |
|
63 |
- |
|
64 |
-void |
|
65 |
-echos(Stack8 *s, Uint8 len, char *name) |
|
66 |
-{ |
|
67 |
- int i; |
|
68 |
- printf("\n%s\n", name); |
|
69 |
- for(i = 0; i < len; ++i) { |
|
70 |
- if(i % 16 == 0) |
|
71 |
- printf("\n"); |
|
72 |
- printf("%02x%c", s->dat[i], s->ptr == i ? '<' : ' '); |
|
73 |
- } |
|
74 |
- printf("\n\n"); |
|
75 |
-} |
|
76 |
- |
|
77 |
-void |
|
78 |
-echom(Memory *m, Uint8 len, char *name) |
|
79 |
-{ |
|
80 |
- int i; |
|
81 |
- printf("\n%s\n", name); |
|
82 |
- for(i = 0; i < len; ++i) { |
|
83 |
- if(i % 16 == 0) |
|
84 |
- printf("\n"); |
|
85 |
- printf("%02x ", m->dat[i]); |
|
86 |
- } |
|
87 |
- printf("\n\n"); |
|
88 |
-} |
|
89 |
- |
|
90 |
-#pragma mark - Operations |
|
91 |
- |
|
92 |
-/* clang-format off */ |
|
93 |
- |
|
94 |
-Uint16 bytes2short(Uint8 a, Uint8 b) { return (a << 8) + b; } |
|
95 |
-Uint16 mempeek16(Uint16 s) { return (cpu.ram.dat[s] << 8) + (cpu.ram.dat[s+1] & 0xff); } |
|
96 |
-void wspush8(Uint8 b) { cpu.wst.dat[cpu.wst.ptr++] = b; } |
|
97 |
-void wspush16(Uint16 s) { wspush8(s >> 8); wspush8(s & 0xff); } |
|
98 |
-Uint8 wspop8(void) { return cpu.wst.dat[--cpu.wst.ptr]; } |
|
99 |
-Uint16 wspop16(void) { return wspop8() + (wspop8() << 8); } |
|
100 |
-Uint8 wspeek8(Uint8 o) { return cpu.wst.dat[cpu.wst.ptr - o]; } |
|
101 |
-Uint16 wspeek16(Uint8 o) { return bytes2short(cpu.wst.dat[cpu.wst.ptr - o], cpu.wst.dat[cpu.wst.ptr - o + 1]); } |
|
102 |
-Uint16 rspop16(void) { return cpu.rst.dat[--cpu.rst.ptr]; } |
|
103 |
-void rspush16(Uint16 a) { cpu.rst.dat[cpu.rst.ptr++] = a; } |
|
104 |
- |
|
105 |
-/* I/O */ |
|
106 |
-void op_brk() { setflag(FLAG_HALT, 1); } |
|
107 |
-void op_lit() { cpu.literal += cpu.ram.dat[cpu.ram.ptr++]; } |
|
108 |
-void op_nop() { printf("NOP");} |
|
109 |
-void op_ldr() { wspush8(cpu.ram.dat[wspop16()]); } |
|
110 |
-void op_str() { cpu.ram.dat[wspop16()] = wspop8(); } |
|
111 |
-/* Logic */ |
|
112 |
-void op_jmp() { cpu.ram.ptr = wspop16(); } |
|
113 |
-void op_jsr() { rspush16(cpu.ram.ptr); cpu.ram.ptr = wspop16(); } |
|
114 |
-void op_rts() { cpu.ram.ptr = rspop16(); } |
|
115 |
-/* Stack */ |
|
116 |
-void op_pop() { wspop8(); } |
|
117 |
-void op_dup() { wspush8(wspeek8(1)); } |
|
118 |
-void op_swp() { Uint8 b = wspop8(), a = wspop8(); wspush8(b); wspush8(a); } |
|
119 |
-void op_ovr() { Uint8 a = wspeek8(2); wspush8(a); } |
|
120 |
-void op_rot() { Uint8 c = wspop8(),b = wspop8(),a = wspop8(); wspush8(b); wspush8(c); wspush8(a); } |
|
121 |
-void op_and() { Uint8 a = wspop8(), b = wspop8(); wspush8(a & b); } |
|
122 |
-void op_ora() { Uint8 a = wspop8(), b = wspop8(); wspush8(a | b); } |
|
123 |
-void op_rol() { Uint8 a = wspop8(), b = wspop8(); wspush8(a << b); } |
|
124 |
-/* Arithmetic */ |
|
125 |
-void op_add() { Uint8 a = wspop8(), b = wspop8(); wspush8(b + a); } |
|
126 |
-void op_sub() { Uint8 a = wspop8(), b = wspop8(); wspush8(b - a); } |
|
127 |
-void op_mul() { Uint8 a = wspop8(), b = wspop8(); wspush8(b * a); } |
|
128 |
-void op_div() { Uint8 a = wspop8(), b = wspop8(); wspush8(b / a); } |
|
129 |
-void op_equ() { Uint8 a = wspop8(), b = wspop8(); wspush8(b == a); } |
|
130 |
-void op_neq() { Uint8 a = wspop8(), b = wspop8(); wspush8(b != a); } |
|
131 |
-void op_gth() { Uint8 a = wspop8(), b = wspop8(); wspush8(b > a); } |
|
132 |
-void op_lth() { Uint8 a = wspop8(), b = wspop8(); wspush8(b < a); } |
|
133 |
-/* Stack(16-bits) */ |
|
134 |
-void op_pop16() { wspop16(); } |
|
135 |
-void op_dup16() { wspush16(wspeek16(2)); } |
|
136 |
-void op_swp16() { Uint16 b = wspop16(), a = wspop16(); wspush16(b); wspush16(a); } |
|
137 |
-void op_ovr16() { Uint16 a = wspeek16(4); wspush16(a); } |
|
138 |
-void op_rot16() { Uint16 c = wspop16(), b = wspop16(), a = wspop16(); wspush16(b); wspush16(c); wspush16(a); } |
|
139 |
-void op_and16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a & b); } |
|
140 |
-void op_ora16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a | b); } |
|
141 |
-void op_rol16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a << b); } |
|
142 |
-/* Arithmetic(16-bits) */ |
|
143 |
-void op_add16() { Uint16 a = wspop16(), b = wspop16(); wspush16(b + a); } |
|
144 |
-void op_sub16() { Uint16 a = wspop16(), b = wspop16(); wspush16(b - a); } |
|
145 |
-void op_mul16() { Uint16 a = wspop16(), b = wspop16(); wspush16(b * a); } |
|
146 |
-void op_div16() { Uint16 a = wspop16(), b = wspop16(); wspush16(b / a); } |
|
147 |
-void op_equ16() { Uint16 a = wspop16(), b = wspop16(); wspush8(b == a); } |
|
148 |
-void op_neq16() { Uint16 a = wspop16(), b = wspop16(); wspush8(b != a); } |
|
149 |
-void op_gth16() { Uint16 a = wspop16(), b = wspop16(); wspush8(b > a); } |
|
150 |
-void op_lth16() { Uint16 a = wspop16(), b = wspop16(); wspush8(b < a); } |
|
151 |
- |
|
152 |
-void (*ops[])() = { |
|
153 |
- op_brk, op_lit, op_nop, op_nop, op_nop, op_nop, op_ldr, op_str, |
|
154 |
- op_jmp, op_jsr, op_nop, op_rts, op_nop, op_nop, op_nop, op_nop, |
|
155 |
- op_pop, op_dup, op_swp, op_ovr, op_rot, op_and, op_ora, op_rol, |
|
156 |
- op_add, op_sub, op_mul, op_div, op_equ, op_neq, op_gth, op_lth, |
|
157 |
- op_pop16, op_dup16, op_swp16, op_ovr16, op_rot16, op_and16, op_ora16, op_rol16, |
|
158 |
- op_add16, op_sub16, op_mul16, op_div16, op_equ16, op_neq16, op_gth16, op_lth16 |
|
159 |
-}; |
|
160 |
- |
|
161 |
-Uint8 opr[][2] = { |
|
162 |
- {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {2,1}, {3,0}, |
|
163 |
- {2,0}, {2,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, |
|
164 |
- {1,0}, {1,2}, {2,2}, {3,3}, {3,3}, {2,1}, {2,1}, {2,1}, |
|
165 |
- {2,1}, {2,1}, {2,1}, {2,1}, {2,1}, {2,1}, {2,1}, {2,1}, |
|
166 |
- {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, /* TODO */ |
|
167 |
- {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0} /* TODO */ |
|
168 |
-}; |
|
169 |
- |
|
170 |
-/* clang-format on */ |
|
171 |
- |
|
172 |
-int |
|
173 |
-error(char *name, int id) |
|
174 |
-{ |
|
175 |
- printf("Error: %s[%04x], at 0x%04x\n", name, id, cpu.counter); |
|
176 |
- return 0; |
|
177 |
-} |
|
178 |
- |
|
179 |
-int |
|
180 |
-doliteral(Uint8 instr) |
|
181 |
-{ |
|
182 |
- if(cpu.wst.ptr >= 255) |
|
183 |
- return error("Stack overflow", instr); |
|
184 |
- wspush8(instr); |
|
185 |
- cpu.literal--; |
|
186 |
- return 1; |
|
187 |
-} |
|
188 |
- |
|
189 |
-int |
|
190 |
-dodevices(void) /* experimental */ |
|
191 |
-{ |
|
192 |
- if(cpu.ram.dat[0xfff1]) { |
|
193 |
- printf("%c", cpu.ram.dat[0xfff1]); |
|
194 |
- cpu.ram.dat[0xfff1] = 0x00; |
|
195 |
- } |
|
196 |
- return 1; |
|
197 |
-} |
|
198 |
- |
|
199 |
-int |
|
200 |
-doopcode(Uint8 instr) |
|
201 |
-{ |
|
202 |
- Uint8 op = instr & 0x1f; |
|
203 |
- setflag(FLAG_SHORT, (instr >> 5) & 1); |
|
204 |
- setflag(FLAG_SIGN, (instr >> 6) & 1); /* usused */ |
|
205 |
- setflag(FLAG_COND, (instr >> 7) & 1); |
|
206 |
- if(getflag(FLAG_SHORT)) |
|
207 |
- op += 16; |
|
208 |
- if(cpu.wst.ptr < opr[op][0]) |
|
209 |
- return error("Stack underflow", op); |
|
210 |
- if(cpu.wst.ptr + opr[op][1] - opr[op][0] >= 255) |
|
211 |
- return error("Stack overflow", instr); |
|
212 |
- if(!getflag(FLAG_COND) || (getflag(FLAG_COND) && wspop8())) |
|
213 |
- (*ops[op])(); |
|
214 |
- dodevices(); |
|
215 |
- return 1; |
|
216 |
-} |
|
217 |
- |
|
218 |
-int |
|
219 |
-eval(void) |
|
220 |
-{ |
|
221 |
- Uint8 instr = cpu.ram.dat[cpu.ram.ptr++]; |
|
222 |
- if(cpu.literal > 0) |
|
223 |
- return doliteral(instr); |
|
224 |
- else |
|
225 |
- return doopcode(instr); |
|
226 |
- return 1; |
|
227 |
-} |
|
228 |
- |
|
229 |
-int |
|
230 |
-load(FILE *f) |
|
231 |
-{ |
|
232 |
- fread(cpu.ram.dat, sizeof(cpu.ram.dat), 1, f); |
|
233 |
- return 1; |
|
234 |
-} |
|
235 |
- |
|
236 |
-void |
|
237 |
-echof(void) |
|
238 |
-{ |
|
239 |
- printf("ended @ %d steps | hf: %x sf: %x sf: %x cf: %x\n", |
|
240 |
- cpu.counter, |
|
241 |
- getflag(FLAG_HALT) != 0, |
|
242 |
- getflag(FLAG_SHORT) != 0, |
|
243 |
- getflag(FLAG_SIGN) != 0, |
|
244 |
- getflag(FLAG_COND) != 0); |
|
245 |
-} |
|
246 |
- |
|
247 |
-int |
|
248 |
-boot(void) |
|
249 |
-{ |
|
250 |
- cpu.vreset = mempeek16(0xfffa); |
|
251 |
- cpu.vframe = mempeek16(0xfffc); |
|
252 |
- cpu.verror = mempeek16(0xfffe); |
|
253 |
- /* eval reset */ |
|
254 |
- cpu.ram.ptr = cpu.vreset; |
|
255 |
- setflag(FLAG_HALT, 0); |
|
256 |
- while(!(cpu.status & FLAG_HALT) && eval()) |
|
257 |
- cpu.counter++; |
|
258 |
- /*eval frame */ |
|
259 |
- cpu.ram.ptr = cpu.vframe; |
|
260 |
- setflag(FLAG_HALT, 0); |
|
261 |
- while(!(cpu.status & FLAG_HALT) && eval()) |
|
262 |
- cpu.counter++; |
|
263 |
- return 1; |
|
264 |
-} |
|
265 |
- |
|
266 | 15 |
int |
267 | 16 |
main(int argc, char *argv[]) |
268 | 17 |
{ |
269 | 18 |
FILE *f; |
19 |
+ Computer cpu; |
|
270 | 20 |
if(argc < 2) |
271 | 21 |
return error("No input.", 0); |
272 | 22 |
if(!(f = fopen(argv[1], "rb"))) |