commit 347b1055f654a2619bf502fcc6767ede24873a27
parent 17578d8bbb44bb4aecb2f487b04acdac8af01e69
Author: neauoire <aliceffekt@gmail.com>
Date: Tue, 9 Feb 2021 17:22:52 -0800
Naive mouse support
Diffstat:
7 files changed, 90 insertions(+), 35 deletions(-)
diff --git a/assembler.c b/assembler.c
@@ -196,9 +196,9 @@ pass2(FILE *f)
fscanf(f, "%s", w);
else if(w[0] == '"')
pushtext(w + 1);
- else if(w[0] == '#') {
+ else if(w[0] == '#')
pushshort(shex(w + 1) & 0xff, 1);
- } else if((l = findlabel(w + 1)))
+ else if((l = findlabel(w + 1)))
pushshort(l->addr, w[0] == ',');
else if((op = findoperator(w)) || scmp(w, "BRK"))
pushbyte(op, 0);
diff --git a/build.sh b/build.sh
@@ -24,5 +24,5 @@ rm -f ./bin/emulator
cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined uxn.c emulator.c -L/usr/local/lib -lSDL2 -o bin/emulator
# run
-./bin/assembler examples/pixel.usm bin/boot.rom
+./bin/assembler examples/mouse.usm bin/boot.rom
./bin/emulator bin/boot.rom
diff --git a/emulator.c b/emulator.c
@@ -37,6 +37,8 @@ SDL_Renderer *gRenderer;
SDL_Texture *gTexture;
Uint32 *pixels;
+Device *devscreen, *devmouse, *devkey;
+
int
error(char *msg, const char *err)
{
@@ -143,8 +145,15 @@ echof(Uxn *c)
void
domouse(SDL_Event *event)
{
- (void)event;
- /* printf("mouse\n"); */
+ int x = event->motion.x / ZOOM - PAD * 8;
+ int y = event->motion.y / ZOOM - PAD * 8;
+ switch(event->type) {
+ case SDL_MOUSEBUTTONUP:
+ case SDL_MOUSEBUTTONDOWN:
+ devmouse->mem[0] = x;
+ devmouse->mem[1] = y;
+ devmouse->mem[2] = event->button.button == SDL_BUTTON_LEFT;
+ }
}
void
@@ -157,7 +166,7 @@ dokey(SDL_Event *event)
#pragma mark - Devices
Uint8
-console_onread(Device *d, Uint8 b)
+consoler(Device *d, Uint8 b)
{
(void)b;
(void)d;
@@ -165,7 +174,7 @@ console_onread(Device *d, Uint8 b)
}
Uint8
-console_onwrite(Device *d, Uint8 b)
+consolew(Device *d, Uint8 b)
{
(void)d;
if(b)
@@ -175,7 +184,7 @@ console_onwrite(Device *d, Uint8 b)
}
Uint8
-ppur(Device *d, Uint8 b)
+screenr(Device *d, Uint8 b)
{
(void)b;
(void)d;
@@ -183,7 +192,7 @@ ppur(Device *d, Uint8 b)
}
Uint8
-ppuw(Device *d, Uint8 b)
+screenw(Device *d, Uint8 b)
{
d->mem[d->len++] = b;
if(d->len > 5) {
@@ -198,6 +207,32 @@ ppuw(Device *d, Uint8 b)
return 0;
}
+Uint8
+mouser(Device *d, Uint8 b)
+{
+ return d->mem[b];
+}
+
+Uint8
+mousew(Device *d, Uint8 b)
+{
+ return 0;
+}
+
+Uint8
+keyr(Device *d, Uint8 b)
+{
+ return 0;
+}
+
+Uint8
+keyw(Device *d, Uint8 b)
+{
+ return 0;
+}
+
+#pragma mark - Generics
+
int
start(Uxn *u)
{
@@ -240,8 +275,10 @@ main(int argc, char **argv)
if(!init())
return error("Init", "Failed");
- portuxn(&u, "console", console_onread, console_onwrite);
- portuxn(&u, "PPU", ppur, ppuw);
+ portuxn(&u, "console", consoler, consolew);
+ devscreen = portuxn(&u, "screen", screenr, screenw);
+ devmouse = portuxn(&u, "mouse", mouser, mousew);
+ devkey = portuxn(&u, "key", keyr, keyw);
start(&u);
diff --git a/examples/mouse.usm b/examples/mouse.usm
@@ -0,0 +1,27 @@
+( draw pixel )
+
+|0100 @RESET
+
+ ( draw 3 pixels )
+ ,00 ,01 ,0000 ,0000 ,putpixel JSR
+ ,00 ,02 ,0001 ,0001 ,putpixel JSR
+ ,01 ,03 ,0002 ,0002 ,putpixel JSR
+
+BRK
+
+|c000 @FRAME
+ ,01 ,01 ( redraw color )
+ ,00 ,01 ,02 IOR ( grab y )
+ ,00 ,00 ,02 IOR ( grab x )
+ ,putpixel JSR
+BRK
+
+@putpixel
+ SWP ,01 IOW ,01 IOW ( y )
+ SWP ,01 IOW ,01 IOW ( x )
+ ,01 IOW ( color )
+ ,01 IOW ( redraw )
+ RTS
+
+|d000 @ERROR BRK
+|FFFA .RESET .FRAME .ERROR
diff --git a/examples/pixel.usm b/examples/pixel.usm
@@ -1,30 +1,21 @@
( draw pixel )
|0100 @RESET
-
- ,0001 IOW ( x0 )
- ,0001 IOW ( x1 )
- ,0001 IOW ( y0 )
- ,0001 IOW ( y1 )
- ,0101 IOW ( clr )
- ,0001 IOW ( noredraw )
-
- ,0001 IOW ( x0 )
- ,0101 IOW ( x1 )
- ,0001 IOW ( y0 )
- ,0001 IOW ( y1 )
- ,0201 IOW ( clr )
- ,0001 IOW ( noredraw )
-
- ,0001 IOW ( x0 )
- ,0201 IOW ( x1 )
- ,0001 IOW ( y0 )
- ,0001 IOW ( y1 )
- ,0301 IOW ( clr )
- ,0101 IOW ( redraw! )
+
+ ( redraw - color - x - y )
+ ,00 ,01 ,0000 ,0000 ,putpixel JSR
+ ,00 ,02 ,0001 ,0001 ,putpixel JSR
+ ,01 ,03 ,0002 ,0002 ,putpixel JSR
BRK
+@putpixel
+ SWP ,01 IOW ,01 IOW ( y )
+ SWP ,01 IOW ,01 IOW ( x )
+ ,01 IOW ( color )
+ ,01 IOW ( redraw )
+ RTS
+
|c000 @FRAME BRK
|d000 @ERROR BRK
|FFFA .RESET .FRAME .ERROR
diff --git a/uxn.c b/uxn.c
@@ -186,7 +186,7 @@ loaduxn(Uxn *u, char *filepath)
/* to start: evaluxn(u, u->vreset); */
-int
+Device *
portuxn(Uxn *u, char *name, Uint8 (*onread)(Device *, Uint8), Uint8 (*onwrite)(Device *, Uint8))
{
Device *d = &u->dev[u->devices++];
@@ -194,5 +194,5 @@ portuxn(Uxn *u, char *name, Uint8 (*onread)(Device *, Uint8), Uint8 (*onwrite)(D
d->wfn = onwrite;
d->len = 0;
printf("Device#%d: %s \n", u->devices, name);
- return 1;
+ return d;
}
diff --git a/uxn.h b/uxn.h
@@ -54,4 +54,4 @@ int getflag(Uint8 *status, char flag);
int loaduxn(Uxn *c, char *filepath);
int bootuxn(Uxn *c);
int evaluxn(Uxn *u, Uint16 vec);
-int portuxn(Uxn *u, char *name, Uint8 (*onread)(Device *, Uint8), Uint8 (*onwrite)(Device *, Uint8));
+Device *portuxn(Uxn *u, char *name, Uint8 (*onread)(Device *, Uint8), Uint8 (*onwrite)(Device *, Uint8));