commit 8281131
Devine Lu Linvega
·
2024-06-29 19:07:09 +0000 UTC
parent 3135f61
Removed indirections in uxn11
6 files changed,
+58,
-70
+14,
-14
1@@ -116,26 +116,26 @@ draw_byte(Uint8 b, Uint16 x, Uint16 y, Uint8 color)
2 }
3
4 static void
5-screen_debugger(Uxn *u)
6+screen_debugger(void)
7 {
8 int i;
9 for(i = 0; i < 0x08; i++) {
10- Uint8 pos = u->wst.ptr - 4 + i;
11+ Uint8 pos = uxn.wst.ptr - 4 + i;
12 Uint8 color = i > 4 ? 0x01 : !pos ? 0xc :
13 i == 4 ? 0x8 :
14 0x2;
15- draw_byte(u->wst.dat[pos], i * 0x18 + 0x8, uxn_screen.height - 0x18, color);
16+ draw_byte(uxn.wst.dat[pos], i * 0x18 + 0x8, uxn_screen.height - 0x18, color);
17 }
18 for(i = 0; i < 0x08; i++) {
19- Uint8 pos = u->rst.ptr - 4 + i;
20+ Uint8 pos = uxn.rst.ptr - 4 + i;
21 Uint8 color = i > 4 ? 0x01 : !pos ? 0xc :
22 i == 4 ? 0x8 :
23 0x2;
24- draw_byte(u->rst.dat[pos], i * 0x18 + 0x8, uxn_screen.height - 0x10, color);
25+ draw_byte(uxn.rst.dat[pos], i * 0x18 + 0x8, uxn_screen.height - 0x10, color);
26 }
27 screen_1bpp(uxn_screen.fg, &arrow[0], 0x68, uxn_screen.height - 0x20, 3, 1, 1);
28 for(i = 0; i < 0x20; i++)
29- draw_byte(u->ram[i], (i & 0x7) * 0x18 + 0x8, ((i >> 3) << 3) + 0x8, 1 + !!u->ram[i]);
30+ draw_byte(uxn.ram[i], (i & 0x7) * 0x18 + 0x8, ((i >> 3) << 3) + 0x8, 1 + !!uxn.ram[i]);
31 }
32
33 void
34@@ -205,7 +205,7 @@ screen_resize(Uint16 width, Uint16 height, int scale)
35 }
36
37 void
38-screen_redraw(Uxn *u)
39+screen_redraw(void)
40 {
41 int i, x, y, k, l, s = uxn_screen.scale;
42 Uint8 *fg = uxn_screen.fg, *bg = uxn_screen.bg;
43@@ -215,8 +215,8 @@ screen_redraw(Uxn *u)
44 Uint32 palette[16], *pixels = uxn_screen.pixels;
45 uxn_screen.x1 = uxn_screen.y1 = 9000;
46 uxn_screen.x2 = uxn_screen.y2 = 0;
47- if(u->dev[0x0e])
48- screen_debugger(u);
49+ if(uxn.dev[0x0e])
50+ screen_debugger();
51 for(i = 0; i < 16; i++)
52 palette[i] = uxn_screen.palette[(i >> 2) ? (i >> 2) : (i & 3)];
53 for(y = y1; y < y2; y++) {
54@@ -238,7 +238,7 @@ screen_redraw(Uxn *u)
55 static int rX, rY, rA, rMX, rMY, rMA, rML, rDX, rDY;
56
57 Uint8
58-screen_dei(Uxn *u, Uint8 addr)
59+screen_dei(Uint8 addr)
60 {
61 switch(addr) {
62 case 0x22: return uxn_screen.width >> 8;
63@@ -251,12 +251,12 @@ screen_dei(Uxn *u, Uint8 addr)
64 case 0x2b: return rY;
65 case 0x2c: return rA >> 8;
66 case 0x2d: return rA;
67- default: return u->dev[addr];
68+ default: return uxn.dev[addr];
69 }
70 }
71
72 void
73-screen_deo(Uint8 *ram, Uint8 *d, Uint8 port)
74+screen_deo(Uint8 *d, Uint8 port)
75 {
76 switch(port) {
77 case 0x3: screen_resize(PEEK2(d + 2), uxn_screen.height, uxn_screen.scale); return;
78@@ -308,10 +308,10 @@ screen_deo(Uint8 *ram, Uint8 *d, Uint8 port)
79 int dxy = rDX * fy, dyx = rDY * fx, addr_incr = rMA << (1 + twobpp);
80 if(twobpp)
81 for(i = 0; i <= rML; i++, x += dyx, y += dxy, rA += addr_incr)
82- screen_2bpp(layer, &ram[rA], x, y, color, fx, fy);
83+ screen_2bpp(layer, &uxn.ram[rA], x, y, color, fx, fy);
84 else
85 for(i = 0; i <= rML; i++, x += dyx, y += dxy, rA += addr_incr)
86- screen_1bpp(layer, &ram[rA], x, y, color, fx, fy);
87+ screen_1bpp(layer, &uxn.ram[rA], x, y, color, fx, fy);
88 if(fx < 0)
89 x1 = x, x2 = rX;
90 else
+3,
-7
1@@ -9,10 +9,6 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
2 WITH REGARD TO THIS SOFTWARE.
3 */
4
5-#define SCREEN_VERSION 1
6-#define SCREEN_DEIMASK 0x003c
7-#define SCREEN_DEOMASK 0xc028
8-
9 typedef struct UxnScreen {
10 int width, height, x1, y1, x2, y2, scale;
11 Uint32 palette[4], *pixels;
12@@ -27,9 +23,9 @@ void screen_fill(Uint8 *layer, int color);
13 void screen_rect(Uint8 *layer, Uint16 x1, Uint16 y1, Uint16 x2, Uint16 y2, int color);
14 void screen_palette(Uint8 *addr);
15 void screen_resize(Uint16 width, Uint16 height, int scale);
16-void screen_redraw(Uxn *u);
17+void screen_redraw();
18
19-Uint8 screen_dei(Uxn *u, Uint8 addr);
20-void screen_deo(Uint8 *ram, Uint8 *d, Uint8 port);
21+Uint8 screen_dei(Uint8 addr);
22+void screen_deo(Uint8 *d, Uint8 port);
23
24 #define twos(v) (v & 0x8000 ? (int)v - 0x10000 : (int)v)
+6,
-13
1@@ -5,7 +5,7 @@
2 #include "system.h"
3
4 /*
5-Copyright (c) 2022-2023 Devine Lu Linvega, Andrew Alderwick
6+Copyright (c) 2022-2024 Devine Lu Linvega, Andrew Alderwick
7
8 Permission to use, copy, modify, and distribute this software for any
9 purpose with or without fee is hereby granted, provided that the above
10@@ -67,13 +67,6 @@ system_inspect(Uxn *u)
11 fprintf(stderr, "RST "), system_print(&u->rst);
12 }
13
14-int
15-system_version(char *name, char *date)
16-{
17- printf("%s, %s.\n", name, date);
18- return 0;
19-}
20-
21 void
22 system_reboot(Uxn *u, char *rom, int soft)
23 {
24@@ -84,12 +77,12 @@ system_reboot(Uxn *u, char *rom, int soft)
25 }
26
27 int
28-system_boot(Uxn *u, Uint8 *ram, char *rom)
29+system_boot(Uint8 *ram, char *rom)
30 {
31- u->ram = ram;
32- system_zero(u, 0);
33- if(!system_load(u, rom))
34- if(!system_load(u, "boot.rom"))
35+ uxn.ram = ram;
36+ system_zero(&uxn, 0);
37+ if(!system_load(&uxn, rom))
38+ if(!system_load(&uxn, "boot.rom"))
39 return system_error("Could not load rom", rom);
40 boot_rom = rom;
41 return 1;
+2,
-3
1@@ -1,5 +1,5 @@
2 /*
3-Copyright (c) 2022 Devine Lu Linvega, Andrew Alderwick
4+Copyright (c) 2024 Devine Lu Linvega, Andrew Alderwick
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8@@ -14,9 +14,8 @@ WITH REGARD TO THIS SOFTWARE.
9
10 void system_reboot(Uxn *u, char *rom, int soft);
11 void system_inspect(Uxn *u);
12-int system_version(char *emulator, char *date);
13 int system_error(char *msg, const char *err);
14-int system_boot(Uxn *u, Uint8 *ram, char *rom);
15+int system_boot(Uint8 *ram, char *rom);
16
17 Uint8 system_dei(Uxn *u, Uint8 addr);
18 void system_deo(Uxn *u, Uint8 *d, Uint8 port);
+31,
-31
1@@ -50,7 +50,7 @@ emu_dei(Uint8 addr)
2 switch(addr & 0xf0) {
3 case 0x00: return system_dei(&uxn, addr);
4 case 0x10: return console_dei(&uxn, addr);
5- case 0x20: return screen_dei(&uxn, addr);
6+ case 0x20: return screen_dei(addr);
7 case 0xc0: return datetime_dei(addr);
8 }
9 return uxn.dev[addr];
10@@ -68,7 +68,7 @@ emu_deo(Uint8 addr, Uint8 value)
11 screen_palette(&uxn.dev[0x8]);
12 break;
13 case 0x10: console_deo(&uxn, &uxn.dev[d], p); break;
14- case 0x20: screen_deo(uxn.ram, &uxn.dev[d], p); break;
15+ case 0x20: screen_deo(&uxn.dev[d], p); break;
16 case 0xa0: file_deo(0, uxn.ram, &uxn.dev[d], p); break;
17 case 0xb0: file_deo(1, uxn.ram, &uxn.dev[d], p); break;
18 }
19@@ -89,23 +89,23 @@ emu_resize(int w, int h)
20 }
21
22 static void
23-emu_restart(Uxn *u, char *rom, int soft)
24+emu_restart(char *rom, int soft)
25 {
26 screen_resize(WIDTH, HEIGHT, uxn_screen.scale);
27 screen_rect(uxn_screen.bg, 0, 0, uxn_screen.width, uxn_screen.height, 0);
28 screen_rect(uxn_screen.fg, 0, 0, uxn_screen.width, uxn_screen.height, 0);
29- system_reboot(u, rom, soft);
30+ system_reboot(&uxn, rom, soft);
31 }
32
33 static int
34-emu_end(Uxn *u)
35+emu_end(void)
36 {
37- free(u->ram);
38+ free(uxn.ram);
39 XDestroyImage(ximage);
40 XDestroyWindow(display, window);
41 XCloseDisplay(display);
42 exit(0);
43- return u->dev[0x0f] & 0x7f;
44+ return uxn.dev[0x0f] & 0x7f;
45 }
46
47 static Uint8
48@@ -134,7 +134,7 @@ toggle_scale(void)
49 }
50
51 static void
52-emu_event(Uxn *u)
53+emu_event(void)
54 {
55 XEvent ev;
56 XNextEvent(display, &ev);
57@@ -146,7 +146,7 @@ emu_event(Uxn *u)
58 XPutImage(display, window, DefaultGC(display, 0), ximage, 0, 0, PAD, PAD, w, h);
59 } break;
60 case ClientMessage:
61- u->dev[0x0f] = 0x80;
62+ uxn.dev[0x0f] = 0x80;
63 break;
64 case KeyPress: {
65 KeySym sym;
66@@ -154,41 +154,41 @@ emu_event(Uxn *u)
67 XLookupString((XKeyPressedEvent *)&ev, buf, 7, &sym, 0);
68 switch(sym) {
69 case XK_F1: toggle_scale(); break;
70- case XK_F2: u->dev[0x0e] = !u->dev[0x0e]; break;
71- case XK_F4: emu_restart(u, boot_rom, 0); break;
72- case XK_F5: emu_restart(u, boot_rom, 1); break;
73+ case XK_F2: uxn.dev[0x0e] = !uxn.dev[0x0e]; break;
74+ case XK_F4: emu_restart(boot_rom, 0); break;
75+ case XK_F5: emu_restart(boot_rom, 1); break;
76 }
77- controller_down(&u->dev[0x80], get_button(sym));
78- controller_key(&u->dev[0x80], sym < 0x80 ? sym : (Uint8)buf[0]);
79+ controller_down(&uxn.dev[0x80], get_button(sym));
80+ controller_key(&uxn.dev[0x80], sym < 0x80 ? sym : (Uint8)buf[0]);
81 } break;
82 case KeyRelease: {
83 KeySym sym;
84 char buf[7];
85 XLookupString((XKeyPressedEvent *)&ev, buf, 7, &sym, 0);
86- controller_up(&u->dev[0x80], get_button(sym));
87+ controller_up(&uxn.dev[0x80], get_button(sym));
88 } break;
89 case ButtonPress: {
90 XButtonPressedEvent *e = (XButtonPressedEvent *)&ev;
91 if(e->button == 4)
92- mouse_scroll(&u->dev[0x90], 0, 1);
93+ mouse_scroll(&uxn.dev[0x90], 0, 1);
94 else if(e->button == 5)
95- mouse_scroll(&u->dev[0x90], 0, -1);
96+ mouse_scroll(&uxn.dev[0x90], 0, -1);
97 else if(e->button == 6)
98- mouse_scroll(&u->dev[0x90], 1, 0);
99+ mouse_scroll(&uxn.dev[0x90], 1, 0);
100 else if(e->button == 7)
101- mouse_scroll(&u->dev[0x90], -1, 0);
102+ mouse_scroll(&uxn.dev[0x90], -1, 0);
103 else
104- mouse_down(&u->dev[0x90], 0x1 << (e->button - 1));
105+ mouse_down(&uxn.dev[0x90], 0x1 << (e->button - 1));
106 } break;
107 case ButtonRelease: {
108 XButtonPressedEvent *e = (XButtonPressedEvent *)&ev;
109- mouse_up(&u->dev[0x90], 0x1 << (e->button - 1));
110+ mouse_up(&uxn.dev[0x90], 0x1 << (e->button - 1));
111 } break;
112 case MotionNotify: {
113 XMotionEvent *e = (XMotionEvent *)&ev;
114 int x = clamp((e->x - PAD) / uxn_screen.scale, 0, uxn_screen.width - 1);
115 int y = clamp((e->y - PAD) / uxn_screen.scale, 0, uxn_screen.height - 1);
116- mouse_pos(&u->dev[0x90], x, y);
117+ mouse_pos(&uxn.dev[0x90], x, y);
118 } break;
119 }
120 }
121@@ -227,7 +227,7 @@ display_init(void)
122 }
123
124 static int
125-emu_run(Uxn *u)
126+emu_run(void)
127 {
128 int i = 1, n;
129 char expirations[8];
130@@ -241,18 +241,18 @@ emu_run(Uxn *u)
131 fds[2].fd = STDIN_FILENO;
132 fds[0].events = fds[1].events = fds[2].events = POLLIN;
133 /* main loop */
134- while(!u->dev[0x0f]) {
135+ while(!uxn.dev[0x0f]) {
136 if(poll(fds, 3, 1000) <= 0)
137 continue;
138 while(XPending(display))
139- emu_event(u);
140+ emu_event();
141 if(poll(&fds[1], 1, 0)) {
142 read(fds[1].fd, expirations, 8);
143- uxn_eval(u, u->dev[0x20] << 8 | u->dev[0x21]);
144+ uxn_eval(&uxn, uxn.dev[0x20] << 8 | uxn.dev[0x21]);
145 if(screen_changed()) {
146 int x = uxn_screen.x1 * uxn_screen.scale, y = uxn_screen.y1 * uxn_screen.scale;
147 int w = uxn_screen.x2 * uxn_screen.scale - x, h = uxn_screen.y2 * uxn_screen.scale - y;
148- screen_redraw(u);
149+ screen_redraw();
150 XPutImage(display, window, DefaultGC(display, 0), ximage, x, y, x + PAD, y + PAD, w, h);
151 }
152 }
153@@ -260,7 +260,7 @@ emu_run(Uxn *u)
154 n = read(fds[2].fd, coninp, CONINBUFSIZE - 1);
155 coninp[n] = 0;
156 for(i = 0; i < n; i++)
157- console_input(u, coninp[i], CONSOLE_STD);
158+ console_input(&uxn, coninp[i], CONSOLE_STD);
159 }
160 }
161 return 1;
162@@ -276,7 +276,7 @@ main(int argc, char **argv)
163 i++;
164 }
165 rom = i == argc ? "boot.rom" : argv[i++];
166- if(!system_boot(&uxn, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8)), rom)) {
167+ if(!system_boot((Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8)), rom)) {
168 fprintf(stdout, "usage: %s [-v] file.rom [args..]\n", argv[0]);
169 return 0;
170 }
171@@ -288,7 +288,7 @@ main(int argc, char **argv)
172 uxn.dev[0x17] = argc - i;
173 if(uxn_eval(&uxn, PAGE_PROGRAM)) {
174 console_listen(&uxn, i, argc, argv);
175- emu_run(&uxn);
176+ emu_run();
177 }
178- return emu_end(&uxn);
179+ return emu_end();
180 }
+2,
-2
1@@ -72,8 +72,8 @@ main(int argc, char **argv)
2 return system_error("usage", "uxncli [-v] file.rom [args..]");
3 /* Read flags */
4 if(argv[i][0] == '-' && argv[i][1] == 'v')
5- return system_version("Uxncli - Console Varvara Emulator", "29 Jun 2024");
6- if(!system_boot(&uxn, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8)), argv[i++]))
7+ return !printf("Uxncli - Console Varvara Emulator, 29 Jun 2024\n");
8+ if(!system_boot((Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8)), argv[i++]))
9 return system_error("Init", "Failed to initialize uxn.");
10 /* Game Loop */
11 uxn.dev[0x17] = argc - i;