... | ... |
@@ -13,54 +13,23 @@ WITH REGARD TO THIS SOFTWARE. |
13 | 13 |
|
14 | 14 |
#include "cpu.h" |
15 | 15 |
|
16 |
-#define FLAG_HALT 0x01 |
|
17 |
-#define FLAG_SHORT 0x02 |
|
18 |
-#define FLAG_SIGN 0x04 |
|
19 |
-#define FLAG_COND 0x08 |
|
20 |
- |
|
21 | 16 |
Cpu cpu; |
22 | 17 |
|
23 | 18 |
#pragma mark - Helpers |
24 | 19 |
|
25 | 20 |
void |
26 |
-setflag(Cpu *c, char flag, int b) |
|
21 |
+setflag(Uint8 *status, char flag, int b) |
|
27 | 22 |
{ |
28 | 23 |
if(b) |
29 |
- c->status |= flag; |
|
24 |
+ *status |= flag; |
|
30 | 25 |
else |
31 |
- c->status &= (~flag); |
|
26 |
+ *status &= (~flag); |
|
32 | 27 |
} |
33 | 28 |
|
34 | 29 |
int |
35 |
-getflag(Cpu *c, char flag) |
|
36 |
-{ |
|
37 |
- return c->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) |
|
30 |
+getflag(Uint8 *status, char flag) |
|
55 | 31 |
{ |
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"); |
|
32 |
+ return *status & flag; |
|
64 | 33 |
} |
65 | 34 |
|
66 | 35 |
#pragma mark - Operations |
... | ... |
@@ -77,23 +46,22 @@ Uint8 wspeek8(Cpu *c, Uint8 o) { return c->wst.dat[c->wst.ptr - o]; } |
77 | 46 |
Uint16 wspeek16(Cpu *c, Uint8 o) { return bytes2short(c->wst.dat[c->wst.ptr - o], c->wst.dat[c->wst.ptr - o + 1]); } |
78 | 47 |
Uint16 rspop16(Cpu *c) { return c->rst.dat[--c->rst.ptr]; } |
79 | 48 |
void rspush16(Cpu *c, Uint16 a) { c->rst.dat[c->rst.ptr++] = a; } |
80 |
- |
|
81 | 49 |
/* I/O */ |
82 |
-void op_brk(Cpu *c) { setflag(c,FLAG_HALT, 1); } |
|
50 |
+void op_brk(Cpu *c) { setflag(&c->status,FLAG_HALT, 1); } |
|
83 | 51 |
void op_lit(Cpu *c) { c->literal += c->ram.dat[c->ram.ptr++]; } |
84 |
-void op_nop(Cpu *c) { printf("NOP");} |
|
85 |
-void op_ldr(Cpu *c) { wspush8(c,c->ram.dat[wspop16(c)]); } |
|
52 |
+void op_nop(Cpu *c) { (void)c; printf("NOP");} |
|
53 |
+void op_ldr(Cpu *c) { wspush8(c, c->ram.dat[wspop16(c)]); } |
|
86 | 54 |
void op_str(Cpu *c) { c->ram.dat[wspop16(c)] = wspop8(c); } |
87 | 55 |
/* Logic */ |
88 | 56 |
void op_jmp(Cpu *c) { c->ram.ptr = wspop16(c); } |
89 |
-void op_jsr(Cpu *c) { rspush16(c,c->ram.ptr); c->ram.ptr = wspop16(c); } |
|
57 |
+void op_jsr(Cpu *c) { rspush16(c, c->ram.ptr); c->ram.ptr = wspop16(c); } |
|
90 | 58 |
void op_rts(Cpu *c) { c->ram.ptr = rspop16(c); } |
91 | 59 |
/* Stack */ |
92 | 60 |
void op_pop(Cpu *c) { wspop8(c); } |
93 | 61 |
void op_dup(Cpu *c) { wspush8(c,wspeek8(c,1)); } |
94 | 62 |
void op_swp(Cpu *c) { Uint8 b = wspop8(c), a = wspop8(c); wspush8(c,b); wspush8(c,a); } |
95 | 63 |
void op_ovr(Cpu *c) { Uint8 a = wspeek8(c,2); wspush8(c,a); } |
96 |
-void op_rot(Cpu *c) { Uint8 c1 = wspop8(c),b = wspop8(c),a = wspop8(c); wspush8(c,b); wspush8(c,c1); wspush8(c,a); } |
|
64 |
+void op_rot(Cpu *c) { Uint8 c1 = wspop8(c),b = wspop8(c),a = wspop8(c); wspush8(c,b); wspush8(c, c1); wspush8(c,a); } |
|
97 | 65 |
void op_and(Cpu *c) { Uint8 a = wspop8(c), b = wspop8(c); wspush8(c,a & b); } |
98 | 66 |
void op_ora(Cpu *c) { Uint8 a = wspop8(c), b = wspop8(c); wspush8(c,a | b); } |
99 | 67 |
void op_rol(Cpu *c) { Uint8 a = wspop8(c), b = wspop8(c); wspush8(c,a << b); } |
... | ... |
@@ -111,7 +79,7 @@ void op_pop16(Cpu *c) { wspop16(c); } |
111 | 79 |
void op_dup16(Cpu *c) { wspush16(c,wspeek16(c,2)); } |
112 | 80 |
void op_swp16(Cpu *c) { Uint16 b = wspop16(c), a = wspop16(c); wspush16(c,b); wspush16(c,a); } |
113 | 81 |
void op_ovr16(Cpu *c) { Uint16 a = wspeek16(c, 4); wspush16(c,a); } |
114 |
-void op_rot16(Cpu *c) { Uint16 c1 = wspop16(c), b = wspop16(c), a = wspop16(c); wspush16(c,b); wspush16(c,c1); wspush16(c,a); } |
|
82 |
+void op_rot16(Cpu *c) { Uint16 c1 = wspop16(c), b = wspop16(c), a = wspop16(c); wspush16(c,b); wspush16(c, c1); wspush16(c,a); } |
|
115 | 83 |
void op_and16(Cpu *c) { Uint16 a = wspop16(c), b = wspop16(c); wspush16(c,a & b); } |
116 | 84 |
void op_ora16(Cpu *c) { Uint16 a = wspop16(c), b = wspop16(c); wspush16(c,a | b); } |
117 | 85 |
void op_rol16(Cpu *c) { Uint16 a = wspop16(c), b = wspop16(c); wspush16(c,a << b); } |
... | ... |
@@ -148,7 +116,7 @@ Uint8 opr[][2] = { |
148 | 116 |
int |
149 | 117 |
error(Cpu *c, char *name, int id) |
150 | 118 |
{ |
151 |
- printf("Error: %s[%04x], at 0x%04x\n", name, id, c->counter); |
|
119 |
+ printf("Error: %s#%04x, at 0x%04x\n", name, id, c->counter); |
|
152 | 120 |
return 0; |
153 | 121 |
} |
154 | 122 |
|
... | ... |
@@ -176,16 +144,16 @@ int |
176 | 144 |
doopcode(Cpu *c, Uint8 instr) |
177 | 145 |
{ |
178 | 146 |
Uint8 op = instr & 0x1f; |
179 |
- setflag(c, FLAG_SHORT, (instr >> 5) & 1); |
|
180 |
- setflag(c, FLAG_SIGN, (instr >> 6) & 1); /* usused */ |
|
181 |
- setflag(c, FLAG_COND, (instr >> 7) & 1); |
|
182 |
- if(getflag(c, FLAG_SHORT)) |
|
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)) |
|
183 | 151 |
op += 16; |
184 | 152 |
if(c->wst.ptr < opr[op][0]) |
185 | 153 |
return error(c, "Stack underflow", op); |
186 | 154 |
if(c->wst.ptr + opr[op][1] - opr[op][0] >= 255) |
187 | 155 |
return error(c, "Stack overflow", instr); |
188 |
- if(!getflag(c, FLAG_COND) || (getflag(c, FLAG_COND) && wspop8(c))) |
|
156 |
+ if(!getflag(&c->status, FLAG_COND) || (getflag(&c->status, FLAG_COND) && wspop8(c))) |
|
189 | 157 |
(*ops[op])(c); |
190 | 158 |
dodevices(c); |
191 | 159 |
return 1; |
... | ... |
@@ -203,23 +171,15 @@ eval(Cpu *c) |
203 | 171 |
} |
204 | 172 |
|
205 | 173 |
int |
206 |
-load(Cpu *c, FILE *f) |
|
174 |
+load(Cpu *c, char *filepath) |
|
207 | 175 |
{ |
176 |
+ FILE *f; |
|
177 |
+ if(!(f = fopen(filepath, "rb"))) |
|
178 |
+ return error(c, "Missing input.", 0); |
|
208 | 179 |
fread(c->ram.dat, sizeof(c->ram.dat), 1, f); |
209 | 180 |
return 1; |
210 | 181 |
} |
211 | 182 |
|
212 |
-void |
|
213 |
-echof(Cpu *c) |
|
214 |
-{ |
|
215 |
- printf("ended @ %d steps | hf: %x sf: %x sf: %x cf: %x\n", |
|
216 |
- c->counter, |
|
217 |
- getflag(c, FLAG_HALT) != 0, |
|
218 |
- getflag(c, FLAG_SHORT) != 0, |
|
219 |
- getflag(c, FLAG_SIGN) != 0, |
|
220 |
- getflag(c, FLAG_COND) != 0); |
|
221 |
-} |
|
222 |
- |
|
223 | 183 |
void |
224 | 184 |
reset(Cpu *c) |
225 | 185 |
{ |
... | ... |
@@ -238,12 +198,12 @@ boot(Cpu *c) |
238 | 198 |
c->verror = mempeek16(c, 0xfffe); |
239 | 199 |
/* eval reset */ |
240 | 200 |
c->ram.ptr = c->vreset; |
241 |
- setflag(c, FLAG_HALT, 0); |
|
201 |
+ setflag(&c->status, FLAG_HALT, 0); |
|
242 | 202 |
while(!(c->status & FLAG_HALT) && eval(c)) |
243 | 203 |
c->counter++; |
244 |
- /*eval frame */ |
|
204 |
+ /* eval frame */ |
|
245 | 205 |
c->ram.ptr = c->vframe; |
246 |
- setflag(c, FLAG_HALT, 0); |
|
206 |
+ setflag(&c->status, FLAG_HALT, 0); |
|
247 | 207 |
while(!(c->status & FLAG_HALT) && eval(c)) |
248 | 208 |
c->counter++; |
249 | 209 |
return 1; |
... | ... |
@@ -14,6 +14,11 @@ WITH REGARD TO THIS SOFTWARE. |
14 | 14 |
typedef unsigned char Uint8; |
15 | 15 |
typedef unsigned short Uint16; |
16 | 16 |
|
17 |
+#define FLAG_HALT 0x01 |
|
18 |
+#define FLAG_SHORT 0x02 |
|
19 |
+#define FLAG_SIGN 0x04 |
|
20 |
+#define FLAG_COND 0x08 |
|
21 |
+ |
|
17 | 22 |
typedef struct { |
18 | 23 |
Uint8 ptr; |
19 | 24 |
Uint8 dat[256]; |
... | ... |
@@ -37,9 +42,8 @@ typedef struct { |
37 | 42 |
Memory ram; |
38 | 43 |
} Cpu; |
39 | 44 |
|
45 |
+void setflag(Uint8 *status, char flag, int b); |
|
46 |
+int getflag(Uint8 *status, char flag); |
|
40 | 47 |
int error(Cpu *c, char *name, int id); |
41 |
-int load(Cpu *c, FILE *f); |
|
48 |
+int load(Cpu *c, char *filepath); |
|
42 | 49 |
int boot(Cpu *c); |
43 |
-void echof(Cpu *c); |
|
44 |
-void echom(Memory *m, Uint8 len, char *name); |
|
45 |
-void echos(Stack8 *s, Uint8 len, char *name); |
... | ... |
@@ -12,16 +12,50 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
12 | 12 |
WITH REGARD TO THIS SOFTWARE. |
13 | 13 |
*/ |
14 | 14 |
|
15 |
+void |
|
16 |
+echos(Stack8 *s, Uint8 len, char *name) |
|
17 |
+{ |
|
18 |
+ int i; |
|
19 |
+ printf("\n%s\n", name); |
|
20 |
+ for(i = 0; i < len; ++i) { |
|
21 |
+ if(i % 16 == 0) |
|
22 |
+ printf("\n"); |
|
23 |
+ printf("%02x%c", s->dat[i], s->ptr == i ? '<' : ' '); |
|
24 |
+ } |
|
25 |
+ printf("\n\n"); |
|
26 |
+} |
|
27 |
+ |
|
28 |
+void |
|
29 |
+echom(Memory *m, Uint8 len, char *name) |
|
30 |
+{ |
|
31 |
+ int i; |
|
32 |
+ printf("\n%s\n", name); |
|
33 |
+ for(i = 0; i < len; ++i) { |
|
34 |
+ if(i % 16 == 0) |
|
35 |
+ printf("\n"); |
|
36 |
+ printf("%02x ", m->dat[i]); |
|
37 |
+ } |
|
38 |
+ printf("\n\n"); |
|
39 |
+} |
|
40 |
+ |
|
41 |
+void |
|
42 |
+echof(Cpu *c) |
|
43 |
+{ |
|
44 |
+ printf("ended @ %d steps | hf: %x sf: %x sf: %x cf: %x\n", |
|
45 |
+ c->counter, |
|
46 |
+ getflag(&c->status, FLAG_HALT) != 0, |
|
47 |
+ getflag(&c->status, FLAG_SHORT) != 0, |
|
48 |
+ getflag(&c->status, FLAG_SIGN) != 0, |
|
49 |
+ getflag(&c->status, FLAG_COND) != 0); |
|
50 |
+} |
|
51 |
+ |
|
15 | 52 |
int |
16 | 53 |
main(int argc, char *argv[]) |
17 | 54 |
{ |
18 |
- FILE *f; |
|
19 | 55 |
Cpu cpu; |
20 | 56 |
if(argc < 2) |
21 | 57 |
return error(&cpu, "No input.", 0); |
22 |
- if(!(f = fopen(argv[1], "rb"))) |
|
23 |
- return error(&cpu, "Missing input.", 0); |
|
24 |
- if(!load(&cpu, f)) |
|
58 |
+ if(!load(&cpu, argv[1])) |
|
25 | 59 |
return error(&cpu, "Load error", 0); |
26 | 60 |
if(!boot(&cpu)) |
27 | 61 |
return error(&cpu, "Boot error", 0); |