diff --git a/build.sh b/build.sh index f3cb731..8a582c4 100755 --- a/build.sh +++ b/build.sh @@ -32,7 +32,7 @@ else fi echo "Assembling.." -./bin/assembler projects/examples/devices/console.lib.usm bin/boot.rom +./bin/assembler projects/examples/devices/screen.usm bin/boot.rom echo "Running.." if [ "${2}" = '--cli' ]; diff --git a/projects/examples/devices/console.lib.usm b/projects/examples/devices/console.lib.usm index 84e4947..9ba7993 100644 --- a/projects/examples/devices/console.lib.usm +++ b/projects/examples/devices/console.lib.usm @@ -18,10 +18,10 @@ |0100 ( -> ) - [ ;char-txt PRINT ] #42 .Console/char DEO BR - [ ;byte-txt PRINT ] #ab .Console/byte DEO BR - [ ;short-txt PRINT ] #cdef .Console/short DEO2 BR - [ ;string-txt PRINT ] ;hello-word .Console/string DEO2 BR + ;char-txt PRINT #42 .Console/char DEO BR + ;byte-txt PRINT #ab .Console/byte DEO BR + ;short-txt PRINT #cdef .Console/short DEO2 BR + ;string-txt PRINT ;hello-word .Console/string DEO2 BR ;hello-word ;print JSR2 #ffff ;print-hexadecimal JSR2 diff --git a/projects/examples/devices/screen.usm b/projects/examples/devices/screen.usm new file mode 100644 index 0000000..f4fead7 --- /dev/null +++ b/projects/examples/devices/screen.usm @@ -0,0 +1,112 @@ +( dev/screen ) + +%RTN { JMP2r } + +( devices ) + +|00 @System [ &vector $2 &pad $6 &r $2 &g $2 &b $2 ] +|20 @Screen [ &vector $2 &width $2 &height $2 &pad $2 &x $2 &y $2 &addr $2 &color $1 ] + +( variables ) + +|0000 + +@center [ &x $2 &y $2 ] + +( init ) + +|0100 ( -> ) + + ( theme ) + #530f .System/r DEO2 + #f20f .System/g DEO2 + #ad0f .System/b DEO2 + + ( find screen center ) + .Screen/width DEI2 #0002 DIV2 .center/x POK2 + .Screen/height DEI2 #0002 DIV2 .center/y POK2 + + ( draw ) + ;draw-background JSR2 + ;draw-sprites JSR2 + +BRK + +@draw-background ( -- ) + + ( draw hor line ) + #0000 .Screen/x DEO2 .center/y PEK2 .Screen/y DEO2 + #0000 .Screen/width DEI2 ( from/to ) + &draw-hor + ( draw ) #01 .Screen/color DEO + ( incr ) SWP2 #0002 ADD2 DUP2 .Screen/x DEO2 SWP2 + OVR2 OVR2 LTH2 ,&draw-hor JNZ + POP2 POP2 + + ( draw ver line ) + .center/x PEK2 .Screen/x DEO2 #0000 .Screen/y DEO2 + #0000 .Screen/height DEI2 ( from/to ) + &draw-ver + ( draw ) #02 .Screen/color DEO + ( incr ) SWP2 #0002 ADD2 DUP2 .Screen/y DEO2 SWP2 + OVR2 OVR2 LTH2 ,&draw-ver JNZ + POP2 POP2 + +RTN + +@draw-sprites ( -- ) + + ( draw blending modes ) + ;preview_icn .Screen/addr DEO2 + #0010 .Screen/y DEO2 + #00 #08 + &draw-pixel1 + ( move ) OVR #00 SWP #0008 MUL2 #0010 ADD2 .Screen/x DEO2 + ( draw ) OVR .Screen/color DEO + ( incr ) SWP #01 ADD SWP + DUP2 LTH ,&draw-pixel1 JNZ + POP POP + #0018 .Screen/y DEO2 + #00 #08 + &draw-pixel2 + ( move ) OVR #00 SWP #0008 MUL2 #0010 ADD2 .Screen/x DEO2 + ( draw ) OVR #08 ADD .Screen/color DEO + ( incr ) SWP #01 ADD SWP + DUP2 LTH ,&draw-pixel2 JNZ + POP POP + #0020 .Screen/y DEO2 + #00 #08 + &draw-icn1 + ( move ) OVR #00 SWP #0008 MUL2 #0010 ADD2 .Screen/x DEO2 + ( draw ) OVR #20 ADD .Screen/color DEO + ( incr ) SWP #01 ADD SWP + DUP2 LTH ,&draw-icn1 JNZ + POP POP + #0028 .Screen/y DEO2 + #00 #08 + &draw-icn2 + ( move ) OVR #00 SWP #0008 MUL2 #0010 ADD2 .Screen/x DEO2 + ( draw ) OVR #28 ADD .Screen/color DEO + ( incr ) SWP #01 ADD SWP + DUP2 LTH ,&draw-icn2 JNZ + POP POP + #0030 .Screen/y DEO2 + #00 #08 + &draw-chr1 + ( move ) OVR #00 SWP #0008 MUL2 #0010 ADD2 .Screen/x DEO2 + ( draw ) OVR #40 ADD .Screen/color DEO + ( incr ) SWP #01 ADD SWP + DUP2 LTH ,&draw-chr1 JNZ + POP POP + #0038 .Screen/y DEO2 + #00 #08 + &draw-chr2 + ( move ) OVR #00 SWP #0008 MUL2 #0010 ADD2 .Screen/x DEO2 + ( draw ) OVR #48 ADD .Screen/color DEO + ( incr ) SWP #01 ADD SWP + DUP2 LTH ,&draw-chr2 JNZ + POP POP + +RTN + +@preview_icn [ 183c 66db db66 3c18 0000 183c 3c18 0000 ] \ No newline at end of file diff --git a/src/debugger.c b/src/debugger.c index bf4efe8..40785a1 100644 --- a/src/debugger.c +++ b/src/debugger.c @@ -38,23 +38,23 @@ printstack(Stack *s) #pragma mark - Devices void -console_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw) +console_talk(Device *d, Uint8 b0, Uint8 w) { + if(!w) return; switch(b0) { - case 0x08: printf("%c", b1); break; - case 0x09: printf("0x%02x\n", b1); break; - case 0x0b: printf("0x%04x\n", (d->dat[0x0a] << 8) + b1); break; + case 0x8: printf("%c", d->dat[0x8]); break; + case 0x9: printf("0x%02x", d->dat[0x9]); break; + case 0xb: printf("0x%04x", mempeek16(d->dat, 0xa)); break; + case 0xd: printf("%s", &d->mem[mempeek16(d->dat, 0xc)]); break; } fflush(stdout); - (void)d; - (void)b0; } void -file_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw) +file_talk(Device *d, Uint8 b0, Uint8 w) { Uint8 read = b0 == 0xd; - if(read || b0 == 0xf) { + if(w && (read || b0 == 0xf)) { char *name = (char *)&d->mem[mempeek16(d->dat, 0x8)]; Uint16 result = 0, length = mempeek16(d->dat, 0xa); Uint16 offset = mempeek16(d->dat, 0x4); @@ -67,15 +67,14 @@ file_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw) } mempoke16(d->dat, 0x2, result); } - (void)b1; } void -nil_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw) +nil_talk(Device *d, Uint8 b0, Uint8 w) { (void)d; (void)b0; - (void)b1; + (void)w; } #pragma mark - Generics diff --git a/src/emulator.c b/src/emulator.c index 3c7bbd5..af77e23 100644 --- a/src/emulator.c +++ b/src/emulator.c @@ -182,17 +182,18 @@ doctrl(Uxn *u, SDL_Event *event, int z) #pragma mark - Devices void -system_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw) +system_talk(Device *d, Uint8 b0, Uint8 w) { + if(!w) return; putcolors(&ppu, &d->dat[0x8]); reqdraw = 1; (void)b0; - (void)b1; } void -console_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw) +console_talk(Device *d, Uint8 b0, Uint8 w) { + if(!w) return; switch(b0) { case 0x8: printf("%c", d->dat[0x8]); break; case 0x9: printf("0x%02x", d->dat[0x9]); break; @@ -203,27 +204,27 @@ console_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw) } void -screen_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw) +screen_talk(Device *d, Uint8 b0, Uint8 w) { - if(b0 == 0xe) { + if(w && b0 == 0xe) { Uint16 x = mempeek16(d->dat, 0x8); Uint16 y = mempeek16(d->dat, 0xa); Uint8 *addr = &d->mem[mempeek16(d->dat, 0xc)]; - Uint8 *layer = b1 >> 4 & 0x1 ? ppu.fg : ppu.bg; - switch(b1 >> 5) { - case 0: putpixel(&ppu, layer, x, y, b1 & 0x3); break; - case 1: puticn(&ppu, layer, x, y, addr, b1 & 0xf); break; - case 2: putchr(&ppu, layer, x, y, addr, b1 & 0xf); break; + Uint8 *layer = d->dat[0xe] >> 4 & 0x1 ? ppu.fg : ppu.bg; + switch(d->dat[0xe] >> 5) { + case 0: putpixel(&ppu, layer, x, y, d->dat[0xe] & 0x3); break; + case 1: puticn(&ppu, layer, x, y, addr, d->dat[0xe] & 0xf); break; + case 2: putchr(&ppu, layer, x, y, addr, d->dat[0xe] & 0xf); break; } reqdraw = 1; } } void -file_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw) +file_talk(Device *d, Uint8 b0, Uint8 w) { Uint8 read = b0 == 0xd; - if(read || b0 == 0xf) { + if(w && (read || b0 == 0xf)) { char *name = (char *)&d->mem[mempeek16(d->dat, 0x8)]; Uint16 result = 0, length = mempeek16(d->dat, 0xa); Uint16 offset = mempeek16(d->dat, 0x4); @@ -236,17 +237,16 @@ file_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw) } mempoke16(d->dat, 0x2, result); } - (void)b1; } static void -audio_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw) +audio_talk(Device *d, Uint8 b0, Uint8 w) { - if(b0 == 0xa) { - if(b1 >= apu.n_notes) apu.notes = SDL_realloc(apu.notes, (b1 + 1) * sizeof(Note)); - while(b1 >= apu.n_notes) SDL_zero(apu.notes[apu.n_notes++]); - apu_play_note(&apu.notes[b1], mempeek16(d->dat, 0x0), mempeek16(d->dat, 0x2), d->dat[0x8], d->dat[0x9] & 0x7f, d->dat[0x9] > 0x7f); - } else if(b0 == 0xe && apu.queue != NULL) { + if(w && b0 == 0xa) { + if(d->dat[0xa] >= apu.n_notes) apu.notes = SDL_realloc(apu.notes, (d->dat[0xa] + 1) * sizeof(Note)); + while(d->dat[0xa] >= apu.n_notes) SDL_zero(apu.notes[apu.n_notes++]); + apu_play_note(&apu.notes[d->dat[0xa]], mempeek16(d->dat, 0x0), mempeek16(d->dat, 0x2), d->dat[0x8], d->dat[0x9] & 0x7f, d->dat[0x9] > 0x7f); + } else if(w && b0 == 0xe && apu.queue != NULL) { if(apu.queue->n == apu.queue->sz) { apu.queue->sz = apu.queue->sz < 4 ? 4 : apu.queue->sz * 2; apu.queue->dat = SDL_realloc(apu.queue->dat, apu.queue->sz * sizeof(*apu.queue->dat)); @@ -256,12 +256,12 @@ audio_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw) else apu.queue->dat[apu.queue->n++] = mempeek16(d->dat, 0xb) + 0x8000; apu.queue->dat[apu.queue->n++] = mempeek16(d->dat, 0xd); - } else if(b0 == 0xf && apu.queue != NULL) + } else if(w && b0 == 0xf && apu.queue != NULL) apu.queue->finishes = 1; } void -datetime_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw) +datetime_talk(Device *d, Uint8 b0, Uint8 w) { time_t seconds = time(NULL); struct tm *t = localtime(&seconds); @@ -276,15 +276,15 @@ datetime_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw) mempoke16(d->dat, 0x08, t->tm_yday); d->dat[0xa] = t->tm_isdst; (void)b0; - (void)b1; + (void)w; } void -nil_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw) +nil_talk(Device *d, Uint8 b0, Uint8 w) { (void)d; (void)b0; - (void)b1; + (void)w; } #pragma mark - Generics diff --git a/src/uxn.c b/src/uxn.c index 1cd44fc..666da89 100644 --- a/src/uxn.c +++ b/src/uxn.c @@ -21,8 +21,8 @@ Uint8 pop8(Stack *s) { if (s->ptr == 0) { s->error = 1; return 0; } return s->d Uint8 peek8(Stack *s, Uint8 a) { if (s->ptr < a + 1) s->error = 1; return s->dat[s->ptr - a - 1]; } void mempoke8(Uint8 *m, Uint16 a, Uint8 b) { m[a] = b; } Uint8 mempeek8(Uint8 *m, Uint16 a) { return m[a]; } -void devpoke8(Device *d, Uint8 a, Uint8 b) { d->dat[a & 0xf] = b; d->talk(d, a & 0x0f, b, 1); } -Uint8 devpeek8(Device *d, Uint8 a) { return d->dat[a & 0xf]; d->talk(d, a & 0x0f, 0, 0); } +void devpoke8(Device *d, Uint8 a, Uint8 b) { d->dat[a & 0xf] = b; d->talk(d, a & 0x0f, 1); } +Uint8 devpeek8(Device *d, Uint8 a) { return d->dat[a & 0xf]; d->talk(d, a & 0x0f, 0); } void push16(Stack *s, Uint16 a) { push8(s, a >> 8); push8(s, a); } Uint16 pop16(Stack *s) { return pop8(s) + (pop8(s) << 8); } Uint16 peek16(Stack *s, Uint8 a) { return peek8(s, a * 2) + (peek8(s, a * 2 + 1) << 8); } @@ -179,7 +179,7 @@ loaduxn(Uxn *u, char *filepath) } Device * -portuxn(Uxn *u, Uint8 id, char *name, void (*talkfn)(Device *d, Uint8 b0, Uint8 b1, Uint8 rw)) +portuxn(Uxn *u, Uint8 id, char *name, void (*talkfn)(Device *d, Uint8 b0, Uint8 w)) { Device *d = &u->dev[id]; d->addr = id * 0x10; diff --git a/src/uxn.h b/src/uxn.h index 5ff7673..d1f9edd 100644 --- a/src/uxn.h +++ b/src/uxn.h @@ -32,7 +32,7 @@ struct Uxn; typedef struct Device { Uint8 addr, dat[16], *mem; - void (*talk)(struct Device *d, Uint8, Uint8, Uint8); + void (*talk)(struct Device *d, Uint8, Uint8); } Device; typedef struct Uxn { @@ -47,4 +47,4 @@ int evaluxn(Uxn *u, Uint16 vec); void mempoke16(Uint8 *m, Uint16 a, Uint16 b); Uint16 mempeek16(Uint8 *m, Uint16 a); -Device *portuxn(Uxn *u, Uint8 id, char *name, void (*talkfn)(Device *, Uint8, Uint8, Uint8)); \ No newline at end of file +Device *portuxn(Uxn *u, Uint8 id, char *name, void (*talkfn)(Device *, Uint8, Uint8)); \ No newline at end of file