Started porting the screen device to memory

This commit is contained in:
neauoire 2021-02-26 14:36:48 -08:00
parent b140e86198
commit 3c04e1ece1
7 changed files with 66 additions and 65 deletions

View File

@ -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 # cc uxn.c emulator.c -std=c89 -Os -DNDEBUG -g0 -s -Wall -Wno-unknown-pragmas -L/usr/local/lib -lSDL2 -o bin/emulator
# run # run
./bin/assembler examples/gui.hover.usm bin/boot.rom ./bin/assembler examples/gui.shapes.usm bin/boot.rom
./bin/emulator bin/boot.rom ./bin/emulator bin/boot.rom

View File

@ -324,6 +324,40 @@ doctrl(SDL_Event *event, int z)
#pragma mark - Devices #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 Uint8
defaultrw(Device *d, Memory *m, Uint8 b) defaultrw(Device *d, Memory *m, Uint8 b)
{ {
@ -445,12 +479,12 @@ main(int argc, char **argv)
if(!init()) if(!init())
return error("Init", "Failed"); return error("Init", "Failed");
devconsole = portuxn(&u, "console", defaultrw, consolew); devconsole = portuxn(&u, "console", defaultrw, consolew, peek1, console_poke);
devscreen = portuxn(&u, "screen", screenr, screenw); devscreen = portuxn(&u, "screen", screenr, screenw, peek1, screen_poke);
devsprite = portuxn(&u, "sprite", screenr, spritew); devsprite = portuxn(&u, "sprite", screenr, spritew, peek1, poke1);
devcontroller = portuxn(&u, "controller", defaultrw, defaultrw); devcontroller = portuxn(&u, "controller", defaultrw, defaultrw, peek1, poke1);
devkey = portuxn(&u, "key", defaultrw, consolew); devkey = portuxn(&u, "key", defaultrw, consolew, peek1, poke1);
devmouse = portuxn(&u, "mouse", defaultrw, defaultrw); devmouse = portuxn(&u, "mouse", defaultrw, defaultrw, peek1, poke1);
start(&u); start(&u);
quit(); quit();

View File

@ -1,10 +1,9 @@
( hello world ) ( hello world )
:dev/w fff9 ( const write port ) &Console { stdio 1 }
|0100 @RESET |0100 @RESET
#00 =dev/w ( set dev/write to console )
,text1 ,print-label JSR ( print to console ) ,text1 ,print-label JSR ( print to console )
BRK BRK
@ -12,7 +11,7 @@ BRK
@print-label ( text ) @print-label ( text )
@cliloop @cliloop
DUP2 LDR IOW ( write pointer value to console ) DUP2 LDR =dev/console.stdio ( write pointer value to console )
#0001 ADD2 ( increment string pointer ) #0001 ADD2 ( increment string pointer )
DUP2 LDR #00 NEQ ,cliloop ROT JMP? POP2 ( while *ptr!=0 goto loop ) DUP2 LDR #00 NEQ ,cliloop ROT JMP? POP2 ( while *ptr!=0 goto loop )
POP2 POP2
@ -24,5 +23,7 @@ RTS
|c000 @FRAME |c000 @FRAME
|d000 @ERROR |d000 @ERROR
|FF00 ;dev/console Console
|FFF0 [ f3f0 f30b f30a ] ( palette ) |FFF0 [ f3f0 f30b f30a ] ( palette )
|FFFA .RESET .FRAME .ERROR |FFFA .RESET .FRAME .ERROR

View File

@ -3,21 +3,18 @@
:dev/r fff8 ( std read port ) :dev/r fff8 ( std read port )
:dev/w fff9 ( std write port ) :dev/w fff9 ( std write port )
&Device { r 8 w 8 } &Console { stdout 1 }
|0100 @RESET |0100 @RESET
#aa =device1.r
~device1.r NOP
BRK BRK
|c000 @FRAME BRK |c000 @FRAME BRK
|d000 @ERROR BRK |d000 @ERROR BRK
|FF00 |FF00 ;dev/console Console
;device1 Device |FF08 ;device2 Device
;device2 Device
|FFF0 [ f2ac 35bb 2b53 ] ( palette ) |FFF0 [ f2ac 35bb 2b53 ] ( palette )
|FFFA .RESET .FRAME .ERROR |FFFA .RESET .FRAME .ERROR

View File

@ -1,14 +1,11 @@
( draw routines ) ( draw routines )
:dev/r fff8 ( std read port ) &Screen { y 2 x 2 color 1 }
:dev/w fff9 ( std write port )
;color 1 ;x1 2 ;x2 2 ;y1 2 ;y2 2 ;color 1 ;x1 2 ;x2 2 ;y1 2 ;y2 2
|0100 @RESET |0100 @RESET
#01 =dev/w ( set dev/write to screen )
#01 =color #01 =color
#0010 #0020 #0040 #0060 ,fill-rect JSR #0010 #0020 #0040 #0060 ,fill-rect JSR
#02 =color #02 =color
@ -25,34 +22,18 @@
BRK 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 ) @line-rect ( x1 y1 x2 y2 )
=y2 =x2 ( stash x1 y1 ) DUP2 WSR2 =y1 DUP2 WSR2 =x1 =y2 =x2 ( stash x1 y1 ) DUP2 WSR2 =y1 DUP2 WSR2 =x1
@line-rect-hor @line-rect-hor
( draw ) ~x1 ~y1 IOW2 IOW2 ~color IOW ( draw ) ~x1 ~y1 =dev/screen.y =dev/screen.x ~color =dev/screen.color
( draw ) ~x1 ~y2 IOW2 IOW2 ~color IOW ( draw ) ~x1 ~y2 =dev/screen.y =dev/screen.x ~color =dev/screen.color
( incr ) ~x1 #0001 ADD2 DUP2 =x1 ( incr ) ~x1 #0001 ADD2 DUP2 =x1
~x2 #0001 ADD2 LTH2 ,line-rect-hor ROT JMP? POP2 ~x2 #0001 ADD2 LTH2 ,line-rect-hor ROT JMP? POP2
( restore x1 y1 ) RSW2 =x1 RSW2 =y1 ( restore x1 y1 ) RSW2 =x1 RSW2 =y1
@line-rect-ver @line-rect-ver
( incr ) ~y1 #0001 ADD2 DUP2 =y1 ( incr ) ~y1 #0001 ADD2 DUP2 =y1
( draw ) ~x1 ~y1 IOW2 IOW2 ~color IOW ( draw ) ~x1 ~y1 =dev/screen.y =dev/screen.x ~color =dev/screen.color
( draw ) ~x2 ~y1 IOW2 IOW2 ~color IOW ( draw ) ~x2 ~y1 =dev/screen.y =dev/screen.x ~color =dev/screen.color
~y2 #0001 SUB2 LTH2 ,line-rect-ver ROT JMP? POP2 ~y2 #0001 SUB2 LTH2 ,line-rect-ver ROT JMP? POP2
RTS RTS
@ -61,7 +42,7 @@ RTS
@fill-rect-ver @fill-rect-ver
RSW2 DUP2 =x1 WSR2 RSW2 DUP2 =x1 WSR2
@fill-rect-hor @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 ( incr ) ~x1 #0001 ADD2 DUP2 =x1
~x2 LTH2 ,fill-rect-hor ROT JMP? POP2 ~x2 LTH2 ,fill-rect-hor ROT JMP? POP2
~y1 #0001 ADD2 DUP2 =y1 ~y1 #0001 ADD2 DUP2 =y1
@ -71,5 +52,8 @@ RTS
|c000 @FRAME BRK |c000 @FRAME BRK
|d000 @ERROR BRK |d000 @ERROR BRK
|FF08 ;dev/screen Screen
|FFF0 [ 0f0f 0fff 0ff0 ] ( palette ) |FFF0 [ 0f0f 0fff 0ff0 ] ( palette )
|FFFA .RESET .FRAME .ERROR ( vectors ) |FFFA .RESET .FRAME .ERROR ( vectors )

27
uxn.c
View File

@ -18,13 +18,12 @@ WITH REGARD TO THIS SOFTWARE.
/* clang-format off */ /* clang-format off */
void setflag(Uint8 *a, char flag, int b) { if(b) *a |= flag; else *a &= (~flag); } void setflag(Uint8 *a, char flag, int b) { if(b) *a |= flag; else *a &= (~flag); }
int getflag(Uint8 *a, char flag) { return *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 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(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) >> 4, a & 0xf, b) : b; } 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]; } 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); } 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); } 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; } void push8(Stack *s, Uint8 a) { s->dat[s->ptr++] = a; }
Uint8 pop8(Stack *s) { return s->dat[--s->ptr]; } Uint8 pop8(Stack *s) { return s->dat[--s->ptr]; }
Uint8 peek8(Stack *s, Uint8 a) { return s->dat[s->ptr - a - 1]; } Uint8 peek8(Stack *s, Uint8 a) { return s->dat[s->ptr - a - 1]; }
@ -211,28 +210,14 @@ loaduxn(Uxn *u, char *filepath)
return 1; 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 * 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++]; Device *d = &u->dev[u->devices++];
d->read = rfn; d->read = rfn;
d->write = wfn; d->write = wfn;
d->peek = peek1; d->peek = pefn;
d->poke = poke1; d->poke = pofn;
d->ptr = 0; d->ptr = 0;
printf("Device #%d: %s \n", u->devices - 1, name); printf("Device #%d: %s \n", u->devices - 1, name);
return d; return d;

6
uxn.h
View File

@ -35,8 +35,8 @@ typedef struct Device {
Uint8 ptr, mem[8]; Uint8 ptr, mem[8];
Uint8 (*read)(struct Device *, Memory *, Uint8); Uint8 (*read)(struct Device *, Memory *, Uint8);
Uint8 (*write)(struct Device *, Memory *, Uint8); Uint8 (*write)(struct Device *, Memory *, Uint8);
Uint8 (*peek)(Uint8, Uint8); Uint8 (*peek)(Uint8 *, Uint8, Uint8);
Uint8 (*poke)(Uint8, Uint8); Uint8 (*poke)(Uint8 *, Uint8, Uint8);
} Device; } Device;
typedef struct { typedef struct {
@ -52,4 +52,4 @@ int getflag(Uint8 *status, char flag);
int loaduxn(Uxn *c, char *filepath); int loaduxn(Uxn *c, char *filepath);
int bootuxn(Uxn *c); int bootuxn(Uxn *c);
int evaluxn(Uxn *u, Uint16 vec); 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));