| ... | ... |
@@ -56,7 +56,7 @@ getflag(char flag) |
| 56 | 56 |
} |
| 57 | 57 |
|
| 58 | 58 |
void |
| 59 |
-echo(Stack *s, Uint8 len, char *name) |
|
| 59 |
+echos(Stack *s, Uint8 len, char *name) |
|
| 60 | 60 |
{
|
| 61 | 61 |
int i; |
| 62 | 62 |
printf("%s\n", name);
|
| ... | ... |
@@ -68,21 +68,32 @@ echo(Stack *s, Uint8 len, char *name) |
| 68 | 68 |
printf("\n\n");
|
| 69 | 69 |
} |
| 70 | 70 |
|
| 71 |
+void |
|
| 72 |
+echom(Memory *m, Uint8 len, char *name) |
|
| 73 |
+{
|
|
| 74 |
+ int i; |
|
| 75 |
+ printf("%s\n", name);
|
|
| 76 |
+ for(i = 0; i < len; ++i) {
|
|
| 77 |
+ if(i % 16 == 0) |
|
| 78 |
+ printf("\n");
|
|
| 79 |
+ printf("%02x ", m->dat[i]);
|
|
| 80 |
+ } |
|
| 81 |
+ printf("\n\n");
|
|
| 82 |
+} |
|
| 83 |
+ |
|
| 71 | 84 |
#pragma mark - Operations |
| 72 | 85 |
|
| 73 | 86 |
/* clang-format off */ |
| 74 | 87 |
|
| 75 |
-void wspush(Uint8 v) { cpu.wst.dat[cpu.wst.ptr++] = v; }
|
|
| 88 |
+void wspush(Uint8 b) { cpu.wst.dat[cpu.wst.ptr++] = b; }
|
|
| 76 | 89 |
Uint8 wspop(void) { return cpu.wst.dat[--cpu.wst.ptr]; }
|
| 77 |
-Uint16 wspop16(void) {
|
|
| 78 |
- |
|
| 79 |
- Uint8 a = cpu.wst.dat[--cpu.wst.ptr]; |
|
| 80 |
- Uint8 b = cpu.wst.dat[--cpu.wst.ptr]; |
|
| 81 |
- return a + (b << 8); |
|
| 90 |
+void wspush16(Uint16 s) {
|
|
| 82 | 91 |
|
| 92 |
+ printf("0x%04x\n", s);
|
|
| 83 | 93 |
} |
| 94 |
+Uint16 wspop16(void) { return wspop() + (wspop() << 8); }
|
|
| 84 | 95 |
Uint8 wspeek(void) { return cpu.wst.dat[cpu.wst.ptr - 1]; }
|
| 85 |
-void rspush(Uint8 v) { cpu.rst.dat[cpu.rst.ptr++] = v; }
|
|
| 96 |
+void rspush(Uint8 b) { cpu.rst.dat[cpu.rst.ptr++] = b; }
|
|
| 86 | 97 |
Uint8 rspop(void) { return cpu.rst.dat[--cpu.rst.ptr]; }
|
| 87 | 98 |
|
| 88 | 99 |
void op_brk() { setflag(FLAG_HALT, 1); }
|
| ... | ... |
@@ -109,13 +120,8 @@ void op_add() { wspush(wspop() + wspop()); }
|
| 109 | 120 |
void op_sub() { wspush(wspop() - wspop()); }
|
| 110 | 121 |
void op_mul() { wspush(wspop() * wspop()); }
|
| 111 | 122 |
void op_div() { wspush(wspop() / wspop()); }
|
| 112 |
-void op_ldr() { }
|
|
| 113 |
-void op_str() {
|
|
| 114 |
- |
|
| 115 |
- Uint8 b = wspop(); |
|
| 116 |
- Uint16 addr = wspop16(); |
|
| 117 |
- printf("store: %02x @ %04x\n", b, addr);
|
|
| 118 |
-} |
|
| 123 |
+void op_ldr() { wspush16(wspop16()); }
|
|
| 124 |
+void op_str() { cpu.ram.dat[wspop16()] = wspop(); }
|
|
| 119 | 125 |
|
| 120 | 126 |
void (*ops[])(void) = {
|
| 121 | 127 |
op_brk, op_rts, op_lit, op_drp, op_dup, op_swp, op_ovr, op_rot, |
| ... | ... |
@@ -215,6 +221,7 @@ main(int argc, char *argv[]) |
| 215 | 221 |
load(f); |
| 216 | 222 |
run(); |
| 217 | 223 |
/* print result */ |
| 218 |
- echo(&cpu.wst, 0x40, "stack"); |
|
| 224 |
+ echos(&cpu.wst, 0x40, "stack"); |
|
| 225 |
+ echom(&cpu.ram, 0x40, "ram"); |
|
| 219 | 226 |
return 0; |
| 220 | 227 |
} |
| ... | ... |
@@ -148,27 +148,6 @@ pushshort(Uint16 s, int lit) |
| 148 | 148 |
pushbyte(s & 0xff, 0); |
| 149 | 149 |
} |
| 150 | 150 |
|
| 151 |
-#pragma mark - Parser |
|
| 152 |
- |
|
| 153 |
-Uint8 |
|
| 154 |
-findop(char *s) |
|
| 155 |
-{
|
|
| 156 |
- int i; |
|
| 157 |
- for(i = 0; i < 32; ++i) |
|
| 158 |
- if(scmp(opcodes[i], s)) |
|
| 159 |
- return i; |
|
| 160 |
- return 0; |
|
| 161 |
-} |
|
| 162 |
- |
|
| 163 |
-void |
|
| 164 |
-makelabel(char *id, Uint8 addr) |
|
| 165 |
-{
|
|
| 166 |
- Label *l = &labels[labelslen++]; |
|
| 167 |
- scpy(suca(id), l->name, 64); |
|
| 168 |
- l->addr = addr; |
|
| 169 |
- printf("New label: %s[0x%02x]\n", l->name, l->addr);
|
|
| 170 |
-} |
|
| 171 |
- |
|
| 172 | 151 |
Label * |
| 173 | 152 |
findlabel(char *s) |
| 174 | 153 |
{
|
| ... | ... |
@@ -184,10 +163,33 @@ findlabel(char *s) |
| 184 | 163 |
int |
| 185 | 164 |
error(char *name, char *id) |
| 186 | 165 |
{
|
| 187 |
- printf("Error: %s - %s\n", name, id);
|
|
| 166 |
+ printf("Error: %s(%s)\n", name, id);
|
|
| 167 |
+ return 0; |
|
| 168 |
+} |
|
| 169 |
+ |
|
| 170 |
+Uint8 |
|
| 171 |
+findop(char *s) |
|
| 172 |
+{
|
|
| 173 |
+ int i; |
|
| 174 |
+ for(i = 0; i < 32; ++i) |
|
| 175 |
+ if(scmp(opcodes[i], s)) |
|
| 176 |
+ return i; |
|
| 188 | 177 |
return 0; |
| 189 | 178 |
} |
| 190 | 179 |
|
| 180 |
+int |
|
| 181 |
+makelabel(char *id, Uint8 addr) |
|
| 182 |
+{
|
|
| 183 |
+ Label *l; |
|
| 184 |
+ if(findlabel(id)) |
|
| 185 |
+ return error("Label duplicate", id);
|
|
| 186 |
+ l = &labels[labelslen++]; |
|
| 187 |
+ scpy(id, l->name, 64); |
|
| 188 |
+ l->addr = addr; |
|
| 189 |
+ printf("New label: %s[0x%02x]\n", l->name, l->addr);
|
|
| 190 |
+ return 1; |
|
| 191 |
+} |
|
| 192 |
+ |
|
| 191 | 193 |
int |
| 192 | 194 |
pass1(FILE *f) |
| 193 | 195 |
{
|
| ... | ... |
@@ -195,8 +197,11 @@ pass1(FILE *f) |
| 195 | 197 |
char w[64]; |
| 196 | 198 |
while(fscanf(f, "%s", w) == 1) {
|
| 197 | 199 |
if(iscomment(w, &skip)) continue; |
| 198 |
- if(w[0] == ':') makelabel(w + 1, addr); |
|
| 199 |
- if(w[0] == ';') makelabel(w + 1, vars++); |
|
| 200 |
+ suca(w); |
|
| 201 |
+ if(w[0] == ':' && !makelabel(w + 1, addr)) |
|
| 202 |
+ return error("Pass1 failed", w);
|
|
| 203 |
+ if(w[0] == ';' && !makelabel(w + 1, vars++)) |
|
| 204 |
+ return error("Pass1 failed", w);
|
|
| 200 | 205 |
/* move addr ptr */ |
| 201 | 206 |
if(findop(w) || scmp(w, "BRK")) |
| 202 | 207 |
addr += 1; |
| ... | ... |
@@ -213,7 +218,7 @@ pass1(FILE *f) |
| 213 | 218 |
else if(ismarker(w)) |
| 214 | 219 |
addr += 0; |
| 215 | 220 |
else |
| 216 |
- return error("Unknown label(pass1)", w);
|
|
| 221 |
+ return error("Unknown label", w);
|
|
| 217 | 222 |
} |
| 218 | 223 |
rewind(f); |
| 219 | 224 |
return 1; |
| ... | ... |
@@ -242,7 +247,7 @@ pass2(FILE *f) |
| 242 | 247 |
else if(sihx(w + 1) && slen(w + 1) == 4) |
| 243 | 248 |
pushshort(shex(w + 1), w[0] == ','); |
| 244 | 249 |
else |
| 245 |
- return error("Unknown label(pass2)", w);
|
|
| 250 |
+ return error("Unknown label", w);
|
|
| 246 | 251 |
} |
| 247 | 252 |
return 1; |
| 248 | 253 |
} |