| ... | ... |
@@ -20,5 +20,5 @@ cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werr |
| 20 | 20 |
# cc uxn.c emulator.c -std=c89 -Os -DNDEBUG -g0 -s -Wall -Wno-unknown-pragmas -L/usr/local/lib -lSDL2 -o bin/emulator |
| 21 | 21 |
|
| 22 | 22 |
# run |
| 23 |
-./bin/assembler examples/gui.hover.usm bin/boot.rom |
|
| 23 |
+./bin/assembler examples/gui.shapes.usm bin/boot.rom |
|
| 24 | 24 |
./bin/emulator bin/boot.rom |
| ... | ... |
@@ -324,6 +324,40 @@ doctrl(SDL_Event *event, int z) |
| 324 | 324 |
|
| 325 | 325 |
#pragma mark - Devices |
| 326 | 326 |
|
| 327 |
+Uint8 |
|
| 328 |
+console_poke(Uint8 *m, Uint8 b0, Uint8 b1) |
|
| 329 |
+{
|
|
| 330 |
+ printf("%c", b1);
|
|
| 331 |
+ fflush(stdout); |
|
| 332 |
+ return b1; |
|
| 333 |
+} |
|
| 334 |
+ |
|
| 335 |
+Uint8 |
|
| 336 |
+screen_poke(Uint8 *m, Uint8 b0, Uint8 b1) |
|
| 337 |
+{
|
|
| 338 |
+ if(b0 == 0x04) {
|
|
| 339 |
+ Uint16 x = (*(m + 2) << 8) + *(m + 3); |
|
| 340 |
+ Uint16 y = (*m << 8) + *(m + 1); |
|
| 341 |
+ paintpixel(b1 >> 4 & 0xf ? screen.fg : screen.bg, x, y, b1 & 0xf); |
|
| 342 |
+ screen.reqdraw = 1; |
|
| 343 |
+ } |
|
| 344 |
+ return b1; |
|
| 345 |
+} |
|
| 346 |
+ |
|
| 347 |
+Uint8 |
|
| 348 |
+peek1(Uint8 *m, Uint8 b0, Uint8 b1) |
|
| 349 |
+{
|
|
| 350 |
+ printf("PEEK! %02x\n", b1);
|
|
| 351 |
+ return b1; |
|
| 352 |
+} |
|
| 353 |
+ |
|
| 354 |
+Uint8 |
|
| 355 |
+poke1(Uint8 *m, Uint8 b0, Uint8 b1) |
|
| 356 |
+{
|
|
| 357 |
+ printf("POKE! %02x\n", b1);
|
|
| 358 |
+ return b1; |
|
| 359 |
+} |
|
| 360 |
+ |
|
| 327 | 361 |
Uint8 |
| 328 | 362 |
defaultrw(Device *d, Memory *m, Uint8 b) |
| 329 | 363 |
{
|
| ... | ... |
@@ -445,12 +479,12 @@ main(int argc, char **argv) |
| 445 | 479 |
if(!init()) |
| 446 | 480 |
return error("Init", "Failed");
|
| 447 | 481 |
|
| 448 |
- devconsole = portuxn(&u, "console", defaultrw, consolew); |
|
| 449 |
- devscreen = portuxn(&u, "screen", screenr, screenw); |
|
| 450 |
- devsprite = portuxn(&u, "sprite", screenr, spritew); |
|
| 451 |
- devcontroller = portuxn(&u, "controller", defaultrw, defaultrw); |
|
| 452 |
- devkey = portuxn(&u, "key", defaultrw, consolew); |
|
| 453 |
- devmouse = portuxn(&u, "mouse", defaultrw, defaultrw); |
|
| 482 |
+ devconsole = portuxn(&u, "console", defaultrw, consolew, peek1, console_poke); |
|
| 483 |
+ devscreen = portuxn(&u, "screen", screenr, screenw, peek1, screen_poke); |
|
| 484 |
+ devsprite = portuxn(&u, "sprite", screenr, spritew, peek1, poke1); |
|
| 485 |
+ devcontroller = portuxn(&u, "controller", defaultrw, defaultrw, peek1, poke1); |
|
| 486 |
+ devkey = portuxn(&u, "key", defaultrw, consolew, peek1, poke1); |
|
| 487 |
+ devmouse = portuxn(&u, "mouse", defaultrw, defaultrw, peek1, poke1); |
|
| 454 | 488 |
|
| 455 | 489 |
start(&u); |
| 456 | 490 |
quit(); |
| ... | ... |
@@ -1,10 +1,9 @@ |
| 1 | 1 |
( hello world ) |
| 2 | 2 |
|
| 3 |
-:dev/w fff9 ( const write port ) |
|
| 3 |
+&Console { stdio 1 }
|
|
| 4 | 4 |
|
| 5 | 5 |
|0100 @RESET |
| 6 | 6 |
|
| 7 |
- #00 =dev/w ( set dev/write to console ) |
|
| 8 | 7 |
,text1 ,print-label JSR ( print to console ) |
| 9 | 8 |
|
| 10 | 9 |
BRK |
| ... | ... |
@@ -12,7 +11,7 @@ BRK |
| 12 | 11 |
@print-label ( text ) |
| 13 | 12 |
|
| 14 | 13 |
@cliloop |
| 15 |
- DUP2 LDR IOW ( write pointer value to console ) |
|
| 14 |
+ DUP2 LDR =dev/console.stdio ( write pointer value to console ) |
|
| 16 | 15 |
#0001 ADD2 ( increment string pointer ) |
| 17 | 16 |
DUP2 LDR #00 NEQ ,cliloop ROT JMP? POP2 ( while *ptr!=0 goto loop ) |
| 18 | 17 |
POP2 |
| ... | ... |
@@ -24,5 +23,7 @@ RTS |
| 24 | 23 |
|c000 @FRAME |
| 25 | 24 |
|d000 @ERROR |
| 26 | 25 |
|
| 26 |
+|FF00 ;dev/console Console |
|
| 27 |
+ |
|
| 27 | 28 |
|FFF0 [ f3f0 f30b f30a ] ( palette ) |
| 28 | 29 |
|FFFA .RESET .FRAME .ERROR |
| 29 | 30 |
\ No newline at end of file |
| ... | ... |
@@ -3,21 +3,18 @@ |
| 3 | 3 |
:dev/r fff8 ( std read port ) |
| 4 | 4 |
:dev/w fff9 ( std write port ) |
| 5 | 5 |
|
| 6 |
-&Device { r 8 w 8 }
|
|
| 6 |
+&Console { stdout 1 }
|
|
| 7 | 7 |
|
| 8 | 8 |
|0100 @RESET |
| 9 | 9 |
|
| 10 |
- #aa =device1.r |
|
| 11 |
- ~device1.r NOP |
|
| 12 | 10 |
|
| 13 | 11 |
BRK |
| 14 | 12 |
|
| 15 | 13 |
|c000 @FRAME BRK |
| 16 | 14 |
|d000 @ERROR BRK |
| 17 | 15 |
|
| 18 |
-|FF00 |
|
| 19 |
-;device1 Device |
|
| 20 |
-;device2 Device |
|
| 16 |
+|FF00 ;dev/console Console |
|
| 17 |
+|FF08 ;device2 Device |
|
| 21 | 18 |
|
| 22 | 19 |
|FFF0 [ f2ac 35bb 2b53 ] ( palette ) |
| 23 | 20 |
|FFFA .RESET .FRAME .ERROR |
| ... | ... |
@@ -1,14 +1,11 @@ |
| 1 | 1 |
( draw routines ) |
| 2 | 2 |
|
| 3 |
-:dev/r fff8 ( std read port ) |
|
| 4 |
-:dev/w fff9 ( std write port ) |
|
| 3 |
+&Screen { y 2 x 2 color 1 }
|
|
| 5 | 4 |
|
| 6 | 5 |
;color 1 ;x1 2 ;x2 2 ;y1 2 ;y2 2 |
| 7 | 6 |
|
| 8 | 7 |
|0100 @RESET |
| 9 | 8 |
|
| 10 |
- #01 =dev/w ( set dev/write to screen ) |
|
| 11 |
- |
|
| 12 | 9 |
#01 =color |
| 13 | 10 |
#0010 #0020 #0040 #0060 ,fill-rect JSR |
| 14 | 11 |
#02 =color |
| ... | ... |
@@ -25,34 +22,18 @@ |
| 25 | 22 |
|
| 26 | 23 |
BRK |
| 27 | 24 |
|
| 28 |
-@line-ver ( x1 y1 y2 ) |
|
| 29 |
- =y2 =y1 =x1 |
|
| 30 |
- @line-ver-loop |
|
| 31 |
- ( draw ) ~x1 ~y1 IOW2 IOW2 ~color IOW |
|
| 32 |
- ( incr ) ~y1 #0001 ADD2 DUP2 =y1 |
|
| 33 |
- ~y2 NEQ2 ,line-ver-loop ROT JMP? POP2 |
|
| 34 |
-RTS |
|
| 35 |
- |
|
| 36 |
-@line-hor ( x1 y1 x2 ) |
|
| 37 |
- =x2 =y1 =x1 |
|
| 38 |
- @line-hor-loop |
|
| 39 |
- ( draw ) ~x1 ~y1 IOW2 IOW2 ~color IOW |
|
| 40 |
- ( incr ) ~x1 #0001 ADD2 DUP2 =x1 |
|
| 41 |
- ~x2 NEQ2 ,line-hor-loop ROT JMP? POP2 |
|
| 42 |
-RTS |
|
| 43 |
- |
|
| 44 | 25 |
@line-rect ( x1 y1 x2 y2 ) |
| 45 | 26 |
=y2 =x2 ( stash x1 y1 ) DUP2 WSR2 =y1 DUP2 WSR2 =x1 |
| 46 | 27 |
@line-rect-hor |
| 47 |
- ( draw ) ~x1 ~y1 IOW2 IOW2 ~color IOW |
|
| 48 |
- ( draw ) ~x1 ~y2 IOW2 IOW2 ~color IOW |
|
| 28 |
+ ( draw ) ~x1 ~y1 =dev/screen.y =dev/screen.x ~color =dev/screen.color |
|
| 29 |
+ ( draw ) ~x1 ~y2 =dev/screen.y =dev/screen.x ~color =dev/screen.color |
|
| 49 | 30 |
( incr ) ~x1 #0001 ADD2 DUP2 =x1 |
| 50 | 31 |
~x2 #0001 ADD2 LTH2 ,line-rect-hor ROT JMP? POP2 |
| 51 | 32 |
( restore x1 y1 ) RSW2 =x1 RSW2 =y1 |
| 52 | 33 |
@line-rect-ver |
| 53 | 34 |
( incr ) ~y1 #0001 ADD2 DUP2 =y1 |
| 54 |
- ( draw ) ~x1 ~y1 IOW2 IOW2 ~color IOW |
|
| 55 |
- ( draw ) ~x2 ~y1 IOW2 IOW2 ~color IOW |
|
| 35 |
+ ( draw ) ~x1 ~y1 =dev/screen.y =dev/screen.x ~color =dev/screen.color |
|
| 36 |
+ ( draw ) ~x2 ~y1 =dev/screen.y =dev/screen.x ~color =dev/screen.color |
|
| 56 | 37 |
~y2 #0001 SUB2 LTH2 ,line-rect-ver ROT JMP? POP2 |
| 57 | 38 |
RTS |
| 58 | 39 |
|
| ... | ... |
@@ -61,7 +42,7 @@ RTS |
| 61 | 42 |
@fill-rect-ver |
| 62 | 43 |
RSW2 DUP2 =x1 WSR2 |
| 63 | 44 |
@fill-rect-hor |
| 64 |
- ( draw ) ~x1 ~y1 IOW2 IOW2 ~color IOW |
|
| 45 |
+ ( draw ) ~x1 ~y1 =dev/screen.y =dev/screen.x ~color =dev/screen.color |
|
| 65 | 46 |
( incr ) ~x1 #0001 ADD2 DUP2 =x1 |
| 66 | 47 |
~x2 LTH2 ,fill-rect-hor ROT JMP? POP2 |
| 67 | 48 |
~y1 #0001 ADD2 DUP2 =y1 |
| ... | ... |
@@ -71,5 +52,8 @@ RTS |
| 71 | 52 |
|
| 72 | 53 |
|c000 @FRAME BRK |
| 73 | 54 |
|d000 @ERROR BRK |
| 55 |
+ |
|
| 56 |
+|FF08 ;dev/screen Screen |
|
| 57 |
+ |
|
| 74 | 58 |
|FFF0 [ 0f0f 0fff 0ff0 ] ( palette ) |
| 75 | 59 |
|FFFA .RESET .FRAME .ERROR ( vectors ) |
| 76 | 60 |
\ No newline at end of file |
| ... | ... |
@@ -18,13 +18,12 @@ WITH REGARD TO THIS SOFTWARE. |
| 18 | 18 |
/* clang-format off */ |
| 19 | 19 |
void setflag(Uint8 *a, char flag, int b) { if(b) *a |= flag; else *a &= (~flag); }
|
| 20 | 20 |
int getflag(Uint8 *a, char flag) { return *a & flag; }
|
| 21 |
-Uint8 devpoke8(Uxn *u, Uint8 id, Uint8 b0, Uint8 b1){ return id < u->devices ? u->dev[id].poke(b0, b1) : b1; }
|
|
| 22 |
-Uint8 devpeek8(Uxn *u, Uint8 id, Uint8 b0, Uint8 b1){ return id < u->devices ? u->dev[id].peek(b0, b1) : b1; }
|
|
| 23 |
-void mempoke8(Uxn *u, Uint16 a, Uint8 b) { u->ram.dat[a] = a >= 0xff00 ? devpoke8(u, (a & 0xff) >> 4, a & 0xf, b) : b; }
|
|
| 21 |
+Uint8 devpoke8(Uxn *u, Uint8 id, Uint8 b0, Uint8 b1){ return id < u->devices ? u->dev[id].poke(&u->ram.dat[0xff00 + id * 8], b0, b1) : b1; }
|
|
| 22 |
+Uint8 devpeek8(Uxn *u, Uint8 id, Uint8 b0, Uint8 b1){ return id < u->devices ? u->dev[id].peek(&u->ram.dat[0xff00 + id * 8], b0, b1) : b1; }
|
|
| 23 |
+void mempoke8(Uxn *u, Uint16 a, Uint8 b) { u->ram.dat[a] = a >= 0xff00 ? devpoke8(u, (a & 0xff) >> 3, (a & 0xf) % 8, b) : b; }
|
|
| 24 | 24 |
Uint8 mempeek8(Uxn *u, Uint16 a) { return a >= 0xff00 ? devpeek8(u, (a & 0xff) >> 4, a & 0xf, u->ram.dat[a]) : u->ram.dat[a]; }
|
| 25 | 25 |
void mempoke16(Uxn *u, Uint16 a, Uint16 b) { mempoke8(u, a, b >> 8); mempoke8(u, a + 1, b); }
|
| 26 | 26 |
Uint16 mempeek16(Uxn *u, Uint16 a) { return (mempeek8(u, a) << 8) + mempeek8(u, a + 1); }
|
| 27 |
- |
|
| 28 | 27 |
void push8(Stack *s, Uint8 a) { s->dat[s->ptr++] = a; }
|
| 29 | 28 |
Uint8 pop8(Stack *s) { return s->dat[--s->ptr]; }
|
| 30 | 29 |
Uint8 peek8(Stack *s, Uint8 a) { return s->dat[s->ptr - a - 1]; }
|
| ... | ... |
@@ -211,28 +210,14 @@ loaduxn(Uxn *u, char *filepath) |
| 211 | 210 |
return 1; |
| 212 | 211 |
} |
| 213 | 212 |
|
| 214 |
-Uint8 |
|
| 215 |
-peek1(Uint8 b, Uint8 m) |
|
| 216 |
-{
|
|
| 217 |
- printf("PEEK! %02x\n", b);
|
|
| 218 |
- return m; |
|
| 219 |
-} |
|
| 220 |
- |
|
| 221 |
-Uint8 |
|
| 222 |
-poke1(Uint8 b, Uint8 m) |
|
| 223 |
-{
|
|
| 224 |
- printf("POKE! %02x\n", b);
|
|
| 225 |
- return m; |
|
| 226 |
-} |
|
| 227 |
- |
|
| 228 | 213 |
Device * |
| 229 |
-portuxn(Uxn *u, char *name, Uint8 (*rfn)(Device *, Memory *, Uint8), Uint8 (*wfn)(Device *, Memory *, Uint8)) |
|
| 214 |
+portuxn(Uxn *u, char *name, Uint8 (*rfn)(Device *, Memory *, Uint8), Uint8 (*wfn)(Device *, Memory *, Uint8), Uint8 (*pefn)(Uint8 *m, Uint8 b0, Uint8 b1), Uint8 (*pofn)(Uint8 *m, Uint8 b0, Uint8 b1)) |
|
| 230 | 215 |
{
|
| 231 | 216 |
Device *d = &u->dev[u->devices++]; |
| 232 | 217 |
d->read = rfn; |
| 233 | 218 |
d->write = wfn; |
| 234 |
- d->peek = peek1; |
|
| 235 |
- d->poke = poke1; |
|
| 219 |
+ d->peek = pefn; |
|
| 220 |
+ d->poke = pofn; |
|
| 236 | 221 |
d->ptr = 0; |
| 237 | 222 |
printf("Device #%d: %s \n", u->devices - 1, name);
|
| 238 | 223 |
return d; |
| ... | ... |
@@ -35,8 +35,8 @@ typedef struct Device {
|
| 35 | 35 |
Uint8 ptr, mem[8]; |
| 36 | 36 |
Uint8 (*read)(struct Device *, Memory *, Uint8); |
| 37 | 37 |
Uint8 (*write)(struct Device *, Memory *, Uint8); |
| 38 |
- Uint8 (*peek)(Uint8, Uint8); |
|
| 39 |
- Uint8 (*poke)(Uint8, Uint8); |
|
| 38 |
+ Uint8 (*peek)(Uint8 *, Uint8, Uint8); |
|
| 39 |
+ Uint8 (*poke)(Uint8 *, Uint8, Uint8); |
|
| 40 | 40 |
} Device; |
| 41 | 41 |
|
| 42 | 42 |
typedef struct {
|
| ... | ... |
@@ -52,4 +52,4 @@ int getflag(Uint8 *status, char flag); |
| 52 | 52 |
int loaduxn(Uxn *c, char *filepath); |
| 53 | 53 |
int bootuxn(Uxn *c); |
| 54 | 54 |
int evaluxn(Uxn *u, Uint16 vec); |
| 55 |
-Device *portuxn(Uxn *u, char *name, Uint8 (*rfn)(Device *, Memory *, Uint8), Uint8 (*wfn)(Device *, Memory *, Uint8)); |
|
| 55 |
+Device *portuxn(Uxn *u, char *name, Uint8 (*rfn)(Device *, Memory *, Uint8), Uint8 (*wfn)(Device *, Memory *, Uint8), Uint8 (*pefn)(Uint8 *, Uint8, Uint8), Uint8 (*pofn)(Uint8 *, Uint8, Uint8)); |