| ... | ... |
@@ -1,9 +1,29 @@ |
| 1 |
-( comment ) |
|
| 1 |
+( define some variables in the zero-page ) |
|
| 2 | 2 |
|
| 3 | 3 |
;var1 |
| 4 | 4 |
;var2 |
| 5 | 5 |
;var3 |
| 6 | 6 |
|
| 7 |
-,ef ,var2 STR |
|
| 7 |
+,22 ,var1 STR ( store 0x22 in var1 ) |
|
| 8 |
+,var1 LDR ( load var2, put value on stack ) |
|
| 8 | 9 |
|
| 9 |
-:label1 |
|
| 10 | 10 |
\ No newline at end of file |
| 11 |
+@0100 ( store program reset after zero-page ) |
|
| 12 |
+ |
|
| 13 |
+:RESET ( --- ) |
|
| 14 |
+ BRK |
|
| 15 |
+ |
|
| 16 |
+@c000 ( much further.. ) |
|
| 17 |
+ |
|
| 18 |
+:FRAME ( --- ) |
|
| 19 |
+ BRK |
|
| 20 |
+ |
|
| 21 |
+@d000 ( further still.. ) |
|
| 22 |
+ |
|
| 23 |
+:ERROR ( --- ) |
|
| 24 |
+ BRK |
|
| 25 |
+ |
|
| 26 |
+@FFFA ( vectors, last 3 shorts ) |
|
| 27 |
+ |
|
| 28 |
+.RESET |
|
| 29 |
+.FRAME |
|
| 30 |
+.ERROR |
| ... | ... |
@@ -31,7 +31,7 @@ typedef struct {
|
| 31 | 31 |
|
| 32 | 32 |
typedef struct {
|
| 33 | 33 |
Uint8 literal, status; |
| 34 |
- Uint16 counter; |
|
| 34 |
+ Uint16 counter, vreset, vframe, verror; |
|
| 35 | 35 |
Stack wst, rst; |
| 36 | 36 |
Memory rom, ram; |
| 37 | 37 |
} Computer; |
| ... | ... |
@@ -85,6 +85,8 @@ echom(Memory *m, Uint8 len, char *name) |
| 85 | 85 |
|
| 86 | 86 |
/* clang-format off */ |
| 87 | 87 |
|
| 88 |
+Uint8 mempoke8(Uint16 p) { return cpu.rom.dat[p+1] & 0xff; }
|
|
| 89 |
+Uint16 mempoke16(Uint16 p) { return (cpu.rom.dat[p] << 8) + (cpu.rom.dat[p+1] & 0xff); }
|
|
| 88 | 90 |
void wspush(Uint8 b) { cpu.wst.dat[cpu.wst.ptr++] = b; }
|
| 89 | 91 |
Uint8 wspop(void) { return cpu.wst.dat[--cpu.wst.ptr]; }
|
| 90 | 92 |
void wspush16(Uint16 s) {
|
| ... | ... |
@@ -120,7 +122,7 @@ void op_add() { wspush(wspop() + wspop()); }
|
| 120 | 122 |
void op_sub() { wspush(wspop() - wspop()); }
|
| 121 | 123 |
void op_mul() { wspush(wspop() * wspop()); }
|
| 122 | 124 |
void op_div() { wspush(wspop() / wspop()); }
|
| 123 |
-void op_ldr() { wspush16(wspop16()); }
|
|
| 125 |
+void op_ldr() { wspush(cpu.ram.dat[wspop16()]); }
|
|
| 124 | 126 |
void op_str() { cpu.ram.dat[wspop16()] = wspop(); }
|
| 125 | 127 |
|
| 126 | 128 |
void (*ops[])(void) = {
|
| ... | ... |
@@ -134,8 +136,7 @@ Uint8 opr[][2] = {
|
| 134 | 136 |
{0,0}, {0,0}, {0,0}, {1,0}, {0,1}, {1,1}, {0,1}, {3,3},
|
| 135 | 137 |
{2,0}, {2,0}, {2,0}, {2,0}, {2,1}, {2,1}, {2,1}, {2,1},
|
| 136 | 138 |
{1,0}, {1,0}, {1,0}, {1,0}, {2,1}, {0,0}, {0,0}, {0,0},
|
| 137 |
- {2,1}, {2,1}, {2,1}, {2,1}, {2,1}, {2,1}, {2,1}, {2,1},
|
|
| 138 |
- {3,1}, {3,1}
|
|
| 139 |
+ {2,1}, {3,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}
|
|
| 139 | 140 |
}; |
| 140 | 141 |
|
| 141 | 142 |
/* clang-format on */ |
| ... | ... |
@@ -163,12 +164,6 @@ error(char *name) |
| 163 | 164 |
return 0; |
| 164 | 165 |
} |
| 165 | 166 |
|
| 166 |
-void |
|
| 167 |
-load(FILE *f) |
|
| 168 |
-{
|
|
| 169 |
- fread(cpu.rom.dat, sizeof(cpu.rom.dat), 1, f); |
|
| 170 |
-} |
|
| 171 |
- |
|
| 172 | 167 |
int |
| 173 | 168 |
eval() |
| 174 | 169 |
{
|
| ... | ... |
@@ -195,8 +190,13 @@ eval() |
| 195 | 190 |
} |
| 196 | 191 |
|
| 197 | 192 |
void |
| 198 |
-run(void) |
|
| 193 |
+start(FILE *f) |
|
| 199 | 194 |
{
|
| 195 |
+ reset(); |
|
| 196 |
+ fread(cpu.rom.dat, sizeof(cpu.rom.dat), 1, f); |
|
| 197 |
+ cpu.vreset = mempoke16(0xfffa); |
|
| 198 |
+ cpu.vframe = mempoke16(0xfffc); |
|
| 199 |
+ cpu.verror = mempoke16(0xfffe); |
|
| 200 | 200 |
while(!(cpu.status & FLAG_HALT) && eval(cpu)) |
| 201 | 201 |
; |
| 202 | 202 |
/* debug */ |
| ... | ... |
@@ -217,9 +217,7 @@ main(int argc, char *argv[]) |
| 217 | 217 |
return error("No input.");
|
| 218 | 218 |
if(!(f = fopen(argv[1], "rb"))) |
| 219 | 219 |
return error("Missing input.");
|
| 220 |
- reset(); |
|
| 221 |
- load(f); |
|
| 222 |
- run(); |
|
| 220 |
+ start(f); |
|
| 223 | 221 |
/* print result */ |
| 224 | 222 |
echos(&cpu.wst, 0x40, "stack"); |
| 225 | 223 |
echom(&cpu.ram, 0x40, "ram"); |
| ... | ... |
@@ -178,7 +178,7 @@ findop(char *s) |
| 178 | 178 |
} |
| 179 | 179 |
|
| 180 | 180 |
int |
| 181 |
-makelabel(char *id, Uint8 addr) |
|
| 181 |
+makelabel(char *id, Uint16 addr) |
|
| 182 | 182 |
{
|
| 183 | 183 |
Label *l; |
| 184 | 184 |
if(findlabel(id)) |
| ... | ... |
@@ -193,7 +193,8 @@ makelabel(char *id, Uint8 addr) |
| 193 | 193 |
int |
| 194 | 194 |
pass1(FILE *f) |
| 195 | 195 |
{
|
| 196 |
- int skip = 0, addr = 0, vars = 0; |
|
| 196 |
+ int skip = 0, vars = 0; |
|
| 197 |
+ Uint16 addr = 0; |
|
| 197 | 198 |
char w[64]; |
| 198 | 199 |
while(fscanf(f, "%s", w) == 1) {
|
| 199 | 200 |
if(iscomment(w, &skip)) continue; |
| ... | ... |
@@ -205,9 +206,9 @@ pass1(FILE *f) |
| 205 | 206 |
/* move addr ptr */ |
| 206 | 207 |
if(findop(w) || scmp(w, "BRK")) |
| 207 | 208 |
addr += 1; |
| 208 |
- else if(w[0] == '@') |
|
| 209 |
- addr += 0; |
|
| 210 |
- else if(w[0] == ':') |
|
| 209 |
+ else if(w[0] == '@') {
|
|
| 210 |
+ addr = shex(w + 1); |
|
| 211 |
+ } else if(w[0] == ':') |
|
| 211 | 212 |
addr += 0; |
| 212 | 213 |
else if(w[0] == ';') |
| 213 | 214 |
addr += 0; |
| ... | ... |
@@ -264,5 +265,6 @@ main(int argc, char *argv[]) |
| 264 | 265 |
return error("Assembly", "Failed");
|
| 265 | 266 |
fwrite(p.data, sizeof(p.data), 1, fopen(argv[2], "wb")); |
| 266 | 267 |
fclose(f); |
| 268 |
+ printf("Assembled %s.\n", argv[2]);
|
|
| 267 | 269 |
return 0; |
| 268 | 270 |
} |