From 3c04e1ece1f148dd3a0a1ef527788c21b1732c4a Mon Sep 17 00:00:00 2001 From: neauoire Date: Fri, 26 Feb 2021 14:36:48 -0800 Subject: [PATCH] Started porting the screen device to memory --- build.sh | 2 +- emulator.c | 46 +++++++++++++++++++++++++++++++++++------ examples/devconsole.usm | 7 ++++--- examples/devices.usm | 9 +++----- examples/gui.shapes.usm | 34 ++++++++---------------------- uxn.c | 27 ++++++------------------ uxn.h | 6 +++--- 7 files changed, 66 insertions(+), 65 deletions(-) diff --git a/build.sh b/build.sh index facb081..54028c9 100755 --- a/build.sh +++ b/build.sh @@ -20,5 +20,5 @@ cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werr # cc uxn.c emulator.c -std=c89 -Os -DNDEBUG -g0 -s -Wall -Wno-unknown-pragmas -L/usr/local/lib -lSDL2 -o bin/emulator # run -./bin/assembler examples/gui.hover.usm bin/boot.rom +./bin/assembler examples/gui.shapes.usm bin/boot.rom ./bin/emulator bin/boot.rom diff --git a/emulator.c b/emulator.c index 58049b6..c8e858b 100644 --- a/emulator.c +++ b/emulator.c @@ -324,6 +324,40 @@ doctrl(SDL_Event *event, int z) #pragma mark - Devices +Uint8 +console_poke(Uint8 *m, Uint8 b0, Uint8 b1) +{ + printf("%c", b1); + fflush(stdout); + return b1; +} + +Uint8 +screen_poke(Uint8 *m, Uint8 b0, Uint8 b1) +{ + if(b0 == 0x04) { + Uint16 x = (*(m + 2) << 8) + *(m + 3); + Uint16 y = (*m << 8) + *(m + 1); + paintpixel(b1 >> 4 & 0xf ? screen.fg : screen.bg, x, y, b1 & 0xf); + screen.reqdraw = 1; + } + return b1; +} + +Uint8 +peek1(Uint8 *m, Uint8 b0, Uint8 b1) +{ + printf("PEEK! %02x\n", b1); + return b1; +} + +Uint8 +poke1(Uint8 *m, Uint8 b0, Uint8 b1) +{ + printf("POKE! %02x\n", b1); + return b1; +} + Uint8 defaultrw(Device *d, Memory *m, Uint8 b) { @@ -445,12 +479,12 @@ main(int argc, char **argv) if(!init()) return error("Init", "Failed"); - devconsole = portuxn(&u, "console", defaultrw, consolew); - devscreen = portuxn(&u, "screen", screenr, screenw); - devsprite = portuxn(&u, "sprite", screenr, spritew); - devcontroller = portuxn(&u, "controller", defaultrw, defaultrw); - devkey = portuxn(&u, "key", defaultrw, consolew); - devmouse = portuxn(&u, "mouse", defaultrw, defaultrw); + devconsole = portuxn(&u, "console", defaultrw, consolew, peek1, console_poke); + devscreen = portuxn(&u, "screen", screenr, screenw, peek1, screen_poke); + devsprite = portuxn(&u, "sprite", screenr, spritew, peek1, poke1); + devcontroller = portuxn(&u, "controller", defaultrw, defaultrw, peek1, poke1); + devkey = portuxn(&u, "key", defaultrw, consolew, peek1, poke1); + devmouse = portuxn(&u, "mouse", defaultrw, defaultrw, peek1, poke1); start(&u); quit(); diff --git a/examples/devconsole.usm b/examples/devconsole.usm index e289dde..675c1ce 100644 --- a/examples/devconsole.usm +++ b/examples/devconsole.usm @@ -1,10 +1,9 @@ ( hello world ) -:dev/w fff9 ( const write port ) +&Console { stdio 1 } |0100 @RESET - #00 =dev/w ( set dev/write to console ) ,text1 ,print-label JSR ( print to console ) BRK @@ -12,7 +11,7 @@ BRK @print-label ( text ) @cliloop - DUP2 LDR IOW ( write pointer value to console ) + DUP2 LDR =dev/console.stdio ( write pointer value to console ) #0001 ADD2 ( increment string pointer ) DUP2 LDR #00 NEQ ,cliloop ROT JMP? POP2 ( while *ptr!=0 goto loop ) POP2 @@ -24,5 +23,7 @@ RTS |c000 @FRAME |d000 @ERROR +|FF00 ;dev/console Console + |FFF0 [ f3f0 f30b f30a ] ( palette ) |FFFA .RESET .FRAME .ERROR \ No newline at end of file diff --git a/examples/devices.usm b/examples/devices.usm index 9dd8b59..1a51c17 100644 --- a/examples/devices.usm +++ b/examples/devices.usm @@ -3,21 +3,18 @@ :dev/r fff8 ( std read port ) :dev/w fff9 ( std write port ) -&Device { r 8 w 8 } +&Console { stdout 1 } |0100 @RESET - #aa =device1.r - ~device1.r NOP BRK |c000 @FRAME BRK |d000 @ERROR BRK -|FF00 -;device1 Device -;device2 Device +|FF00 ;dev/console Console +|FF08 ;device2 Device |FFF0 [ f2ac 35bb 2b53 ] ( palette ) |FFFA .RESET .FRAME .ERROR diff --git a/examples/gui.shapes.usm b/examples/gui.shapes.usm index 14292d9..881ffd8 100644 --- a/examples/gui.shapes.usm +++ b/examples/gui.shapes.usm @@ -1,14 +1,11 @@ ( draw routines ) -:dev/r fff8 ( std read port ) -:dev/w fff9 ( std write port ) +&Screen { y 2 x 2 color 1 } ;color 1 ;x1 2 ;x2 2 ;y1 2 ;y2 2 |0100 @RESET - #01 =dev/w ( set dev/write to screen ) - #01 =color #0010 #0020 #0040 #0060 ,fill-rect JSR #02 =color @@ -25,34 +22,18 @@ BRK -@line-ver ( x1 y1 y2 ) - =y2 =y1 =x1 - @line-ver-loop - ( draw ) ~x1 ~y1 IOW2 IOW2 ~color IOW - ( incr ) ~y1 #0001 ADD2 DUP2 =y1 - ~y2 NEQ2 ,line-ver-loop ROT JMP? POP2 -RTS - -@line-hor ( x1 y1 x2 ) - =x2 =y1 =x1 - @line-hor-loop - ( draw ) ~x1 ~y1 IOW2 IOW2 ~color IOW - ( incr ) ~x1 #0001 ADD2 DUP2 =x1 - ~x2 NEQ2 ,line-hor-loop ROT JMP? POP2 -RTS - @line-rect ( x1 y1 x2 y2 ) =y2 =x2 ( stash x1 y1 ) DUP2 WSR2 =y1 DUP2 WSR2 =x1 @line-rect-hor - ( draw ) ~x1 ~y1 IOW2 IOW2 ~color IOW - ( draw ) ~x1 ~y2 IOW2 IOW2 ~color IOW + ( draw ) ~x1 ~y1 =dev/screen.y =dev/screen.x ~color =dev/screen.color + ( draw ) ~x1 ~y2 =dev/screen.y =dev/screen.x ~color =dev/screen.color ( incr ) ~x1 #0001 ADD2 DUP2 =x1 ~x2 #0001 ADD2 LTH2 ,line-rect-hor ROT JMP? POP2 ( restore x1 y1 ) RSW2 =x1 RSW2 =y1 @line-rect-ver ( incr ) ~y1 #0001 ADD2 DUP2 =y1 - ( draw ) ~x1 ~y1 IOW2 IOW2 ~color IOW - ( draw ) ~x2 ~y1 IOW2 IOW2 ~color IOW + ( draw ) ~x1 ~y1 =dev/screen.y =dev/screen.x ~color =dev/screen.color + ( draw ) ~x2 ~y1 =dev/screen.y =dev/screen.x ~color =dev/screen.color ~y2 #0001 SUB2 LTH2 ,line-rect-ver ROT JMP? POP2 RTS @@ -61,7 +42,7 @@ RTS @fill-rect-ver RSW2 DUP2 =x1 WSR2 @fill-rect-hor - ( draw ) ~x1 ~y1 IOW2 IOW2 ~color IOW + ( draw ) ~x1 ~y1 =dev/screen.y =dev/screen.x ~color =dev/screen.color ( incr ) ~x1 #0001 ADD2 DUP2 =x1 ~x2 LTH2 ,fill-rect-hor ROT JMP? POP2 ~y1 #0001 ADD2 DUP2 =y1 @@ -71,5 +52,8 @@ RTS |c000 @FRAME BRK |d000 @ERROR BRK + +|FF08 ;dev/screen Screen + |FFF0 [ 0f0f 0fff 0ff0 ] ( palette ) |FFFA .RESET .FRAME .ERROR ( vectors ) \ No newline at end of file diff --git a/uxn.c b/uxn.c index 27422f6..d407af5 100644 --- a/uxn.c +++ b/uxn.c @@ -18,13 +18,12 @@ WITH REGARD TO THIS SOFTWARE. /* clang-format off */ void setflag(Uint8 *a, char flag, int b) { if(b) *a |= flag; else *a &= (~flag); } int getflag(Uint8 *a, char flag) { return *a & flag; } -Uint8 devpoke8(Uxn *u, Uint8 id, Uint8 b0, Uint8 b1){ return id < u->devices ? u->dev[id].poke(b0, b1) : b1; } -Uint8 devpeek8(Uxn *u, Uint8 id, Uint8 b0, Uint8 b1){ return id < u->devices ? u->dev[id].peek(b0, b1) : b1; } -void mempoke8(Uxn *u, Uint16 a, Uint8 b) { u->ram.dat[a] = a >= 0xff00 ? devpoke8(u, (a & 0xff) >> 4, a & 0xf, b) : b; } +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; } +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; } +void mempoke8(Uxn *u, Uint16 a, Uint8 b) { u->ram.dat[a] = a >= 0xff00 ? devpoke8(u, (a & 0xff) >> 3, (a & 0xf) % 8, b) : b; } Uint8 mempeek8(Uxn *u, Uint16 a) { return a >= 0xff00 ? devpeek8(u, (a & 0xff) >> 4, a & 0xf, u->ram.dat[a]) : u->ram.dat[a]; } void mempoke16(Uxn *u, Uint16 a, Uint16 b) { mempoke8(u, a, b >> 8); mempoke8(u, a + 1, b); } Uint16 mempeek16(Uxn *u, Uint16 a) { return (mempeek8(u, a) << 8) + mempeek8(u, a + 1); } - void push8(Stack *s, Uint8 a) { s->dat[s->ptr++] = a; } Uint8 pop8(Stack *s) { return s->dat[--s->ptr]; } Uint8 peek8(Stack *s, Uint8 a) { return s->dat[s->ptr - a - 1]; } @@ -211,28 +210,14 @@ loaduxn(Uxn *u, char *filepath) return 1; } -Uint8 -peek1(Uint8 b, Uint8 m) -{ - printf("PEEK! %02x\n", b); - return m; -} - -Uint8 -poke1(Uint8 b, Uint8 m) -{ - printf("POKE! %02x\n", b); - return m; -} - Device * -portuxn(Uxn *u, char *name, Uint8 (*rfn)(Device *, Memory *, Uint8), Uint8 (*wfn)(Device *, Memory *, Uint8)) +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)) { Device *d = &u->dev[u->devices++]; d->read = rfn; d->write = wfn; - d->peek = peek1; - d->poke = poke1; + d->peek = pefn; + d->poke = pofn; d->ptr = 0; printf("Device #%d: %s \n", u->devices - 1, name); return d; diff --git a/uxn.h b/uxn.h index dc5f1ae..1c783f7 100644 --- a/uxn.h +++ b/uxn.h @@ -35,8 +35,8 @@ typedef struct Device { Uint8 ptr, mem[8]; Uint8 (*read)(struct Device *, Memory *, Uint8); Uint8 (*write)(struct Device *, Memory *, Uint8); - Uint8 (*peek)(Uint8, Uint8); - Uint8 (*poke)(Uint8, Uint8); + Uint8 (*peek)(Uint8 *, Uint8, Uint8); + Uint8 (*poke)(Uint8 *, Uint8, Uint8); } Device; typedef struct { @@ -52,4 +52,4 @@ int getflag(Uint8 *status, char flag); int loaduxn(Uxn *c, char *filepath); int bootuxn(Uxn *c); int evaluxn(Uxn *u, Uint16 vec); -Device *portuxn(Uxn *u, char *name, Uint8 (*rfn)(Device *, Memory *, Uint8), Uint8 (*wfn)(Device *, Memory *, Uint8)); +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));