commit a593db3

neauoire  ·  2022-03-28 18:03:02 +0000 UTC
parent 21d8f34
Use the file device load for roms
6 files changed,  +28, -40
+1, -1
1@@ -7,7 +7,7 @@ An emulator for the [Uxn stack-machine](https://wiki.xxiivv.com/site/uxn.html),
2 All you need is X11.
3 
4 ```
5-gcc src/uxn.c src/devices/system.c src/devices/screen.c src/devices/controller.c src/devices/mouse.c src/devices/file.c src/devices/datetime.c src/uxn11.c -DNDEBUG -Os -g0 -s -o bin/uxn11 -lX11
6+gcc src/uxn.c src/devices/system.c src/devices/screen.c src/devices/controller.c src/devices/mouse.c src/devices/file.c src/devices/datetime.c src/uxn11.c -D_POSIX_C_SOURCE=199309L -DNDEBUG -Os -g0 -s -o bin/uxn11 -lX11
7 ```
8 
9 ## Terminal
+1, -1
1@@ -12,7 +12,7 @@ mkdir -p bin
2 gcc -std=c89 -D_POSIX_C_SOURCE=199309L -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 src/uxn.c src/devices/system.c src/devices/screen.c src/devices/controller.c src/devices/mouse.c src/devices/file.c src/devices/datetime.c src/uxn11.c -o bin/uxn11 -lX11
3 
4 # Build(release)
5-# gcc src/uxn.c src/devices/system.c src/devices/screen.c src/devices/controller.c src/devices/mouse.c src/devices/file.c src/devices/datetime.c src/uxn11.c -o bin/uxn11 -lX11
6+# gcc src/uxn.c src/devices/system.c src/devices/screen.c src/devices/controller.c src/devices/mouse.c src/devices/file.c src/devices/datetime.c src/uxn11.c -D_POSIX_C_SOURCE=199309L -DNDEBUG -Os -g0 -s -o bin/uxn11 -lX11
7 
8 echo "Running.."
9 bin/uxn11 ~/roms/left.rom # etc/mouse.rom
+12, -0
 1@@ -215,3 +215,15 @@ file_dei(Device *d, Uint8 port)
 2 	}
 3 	return d->dat[port];
 4 }
 5+
 6+/* Boot */
 7+
 8+int
 9+load_rom(Uxn *u, char *filename)
10+{
11+	int ret;
12+	file_init(uxn_file, filename, strlen(filename) + 1);
13+	ret = file_read(uxn_file, &u->ram[PAGE_PROGRAM], 0x10000 - PAGE_PROGRAM);
14+	reset(uxn_file);
15+	return ret;
16+}
+1, -0
1@@ -15,3 +15,4 @@ WITH REGARD TO THIS SOFTWARE.
2 
3 void file_deo(Device *d, Uint8 port);
4 Uint8 file_dei(Device *d, Uint8 port);
5+int load_rom(Uxn *u, char *filename);
+11, -24
 1@@ -2,6 +2,7 @@
 2 #include <stdlib.h>
 3 #include <X11/Xlib.h>
 4 #include <X11/Xutil.h>
 5+#include <X11/keysymdef.h>
 6 #include <sys/timerfd.h>
 7 #include <unistd.h>
 8 #include <poll.h>
 9@@ -70,19 +71,6 @@ nil_deo(Device *d, Uint8 port)
10 	(void)port;
11 }
12 
13-static int
14-load(Uxn *u, char *filepath)
15-{
16-	FILE *f;
17-	int r;
18-	if(!(f = fopen(filepath, "rb"))) return 0;
19-	r = fread(u->ram + PAGE_PROGRAM, 1, 0x10000 - PAGE_PROGRAM, f);
20-	fclose(f);
21-	if(r < 1) return 0;
22-	fprintf(stderr, "Loaded %s\n", filepath);
23-	return 1;
24-}
25-
26 static void
27 redraw(void)
28 {
29@@ -105,20 +93,18 @@ hide_cursor(void)
30 	XFreePixmap(display, bitmap);
31 }
32 
33-/* usr/include/X11/keysymdef.h */
34-
35 static Uint8
36 get_button(KeySym sym)
37 {
38 	switch(sym) {
39-	case 0xff52: return 0x10; /* Up */
40-	case 0xff54: return 0x20; /* Down */
41-	case 0xff51: return 0x40; /* Left */
42-	case 0xff53: return 0x80; /* Right */
43-	case 0xffe3: return 0x01; /* Control */
44-	case 0xffe9: return 0x02; /* Alt */
45-	case 0xffe1: return 0x04; /* Shift */
46-	case 0xff50: return 0x08; /* Home */
47+	case XK_Up: return 0x10;
48+	case XK_Down: return 0x20;
49+	case XK_Left: return 0x40;
50+	case XK_Right: return 0x80;
51+	case XK_Control_L: return 0x01;
52+	case XK_Alt_L: return 0x02;
53+	case XK_Shift_L: return 0x04;
54+	case XK_Home: return 0x08;
55 	}
56 	return 0x00;
57 }
58@@ -171,8 +157,9 @@ start(Uxn *u, char *rom)
59 {
60 	if(!uxn_boot(u, (Uint8 *)calloc(0x10000, sizeof(Uint8))))
61 		return error("Boot", "Failed");
62-	if(!load(u, rom))
63+	if(!load_rom(u, rom))
64 		return error("Load", "Failed");
65+	fprintf(stderr, "Loaded %s\n", rom);
66 	/* system   */ uxn_port(u, 0x0, system_dei, system_deo);
67 	/* console  */ uxn_port(u, 0x1, nil_dei, console_deo);
68 	/* screen   */ devscreen = uxn_port(u, 0x2, screen_dei, screen_deo);
+2, -14
 1@@ -74,19 +74,6 @@ run(Uxn *u)
 2 	}
 3 }
 4 
 5-static int
 6-load(Uxn *u, char *filepath)
 7-{
 8-	FILE *f;
 9-	int r;
10-	if(!(f = fopen(filepath, "rb"))) return 0;
11-	r = fread(u->ram + PAGE_PROGRAM, 1, 0x10000 - PAGE_PROGRAM, f);
12-	fclose(f);
13-	if(r < 1) return 0;
14-	fprintf(stderr, "Loaded %s\n", filepath);
15-	return 1;
16-}
17-
18 int
19 uxn_interrupt(void)
20 {
21@@ -126,8 +113,9 @@ main(int argc, char **argv)
22 		return error("Usage", "uxncli game.rom args");
23 	if(!start(&u))
24 		return error("Start", "Failed");
25-	if(!load(&u, argv[1]))
26+	if(!load_rom(&u, argv[1]))
27 		return error("Load", "Failed");
28+	fprintf(stderr, "Loaded %s\n", argv[1]);
29 	if(!uxn_eval(&u, PAGE_PROGRAM))
30 		return error("Init", "Failed");
31 	for(i = 2; i < argc; i++) {