commit 7603ddf
Devine Lu Linvega
·
2023-01-29 00:47:41 +0000 UTC
parent 4020917
Added MMU operation
4 files changed,
+53,
-15
+40,
-10
1@@ -1,10 +1,11 @@
2 #include <stdio.h>
3+#include <stdlib.h>
4
5 #include "../uxn.h"
6 #include "system.h"
7
8 /*
9-Copyright (c) 2022 Devine Lu Linvega, Andrew Alderwick
10+Copyright (c) 2022-2023 Devine Lu Linvega, Andrew Alderwick
11
12 Permission to use, copy, modify, and distribute this software for any
13 purpose with or without fee is hereby granted, provided that the above
14@@ -42,27 +43,56 @@ int
15 uxn_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr)
16 {
17 Uint8 *d = &u->dev[0x00];
18- if(instr & 0x40)
19- u->rst->err = err;
20- else
21- u->wst->err = err;
22- if(GETVEC(d))
23- uxn_eval(u, GETVEC(d));
24- else {
25+ Uint16 handler = GETVEC(d);
26+ if(handler) {
27+ u->wst->ptr = 4;
28+ u->wst->dat[0] = addr >> 0x8;
29+ u->wst->dat[1] = addr & 0xff;
30+ u->wst->dat[2] = instr;
31+ u->wst->dat[3] = err;
32+ return uxn_eval(u, handler);
33+ } else {
34 system_inspect(u);
35 fprintf(stderr, "%s %s, by %02x at 0x%04x.\n", (instr & 0x40) ? "Return-stack" : "Working-stack", errors[err - 1], instr, addr);
36 }
37 return 0;
38 }
39
40+/* MMU */
41+
42+Uint8 *
43+mmu_init(Mmu *m, Uint16 pages)
44+{
45+ m->length = pages;
46+ m->pages = (Uint8 *)calloc(0x10000 * pages, sizeof(Uint8));
47+ return m->pages;
48+}
49+
50+void
51+mmu_eval(Uint8 *ram, Uint16 addr)
52+{
53+ Uint16 a = addr, i = 0;
54+ Uint8 o = ram[a++];
55+ if(o == 1) {
56+ Uint16 length = (ram[a++] << 8) + ram[a++];
57+ Uint16 src_page = ((ram[a++] << 8) + ram[a++]) % 16, src_addr = (ram[a++] << 8) + ram[a++];
58+ Uint16 dst_page = ((ram[a++] << 8) + ram[a++]) % 16, dst_addr = (ram[a++] << 8) + ram[a];
59+ for(i = 0; i < length; i++)
60+ ram[dst_page * 0x10000 + dst_addr + i] = ram[src_page * 0x10000 + src_addr + i];
61+ }
62+}
63+
64 /* IO */
65
66 void
67 system_deo(Uxn *u, Uint8 *d, Uint8 port)
68 {
69+ Uint16 a;
70 switch(port) {
71- case 0x2: u->wst = (Stack *)(u->ram + (d[port] ? (d[port] * 0x100) : 0x10000)); break;
72- case 0x3: u->rst = (Stack *)(u->ram + (d[port] ? (d[port] * 0x100) : 0x10100)); break;
73+ case 0x3:
74+ PEKDEV(a, 0x2);
75+ mmu_eval(u->ram, a);
76+ break;
77 case 0xe:
78 if(u->wst->ptr || u->rst->ptr) system_inspect(u);
79 break;
+6,
-0
1@@ -11,3 +11,9 @@ WITH REGARD TO THIS SOFTWARE.
2
3 void system_inspect(Uxn *u);
4 void system_deo(Uxn *u, Uint8 *d, Uint8 port);
5+
6+typedef struct {
7+ Uint8 length, *pages;
8+} Mmu;
9+
10+Uint8 *mmu_init(Mmu *m, Uint16 pages);
+2,
-1
1@@ -225,6 +225,7 @@ int
2 main(int argc, char **argv)
3 {
4 Uxn u;
5+ Mmu m;
6 int i;
7 char expirations[8];
8 struct pollfd fds[2];
9@@ -232,7 +233,7 @@ main(int argc, char **argv)
10 if(argc < 2)
11 return emu_error("Usage", "uxn11 game.rom args");
12 rom_path = argv[1];
13- if(!uxn_boot(&u, (Uint8 *)calloc(0x10300, sizeof(Uint8)), emu_dei, emu_deo))
14+ if(!uxn_boot(&u, mmu_init(&m, 16), emu_dei, emu_deo))
15 return emu_error("Boot", "Failed");
16 /* start sequence */
17 if(!emu_start(&u, rom_path))
+5,
-4
1@@ -7,7 +7,7 @@
2 #include "devices/datetime.h"
3
4 /*
5-Copyright (c) 2021 Devine Lu Linvega
6+Copyright (c) 2021-2023 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@@ -37,8 +37,8 @@ console_input(Uxn *u, char c)
11 static void
12 console_deo(Uint8 *d, Uint8 port)
13 {
14- FILE *fd = port == 0x8 ? stdout : port == 0x9 ? stderr :
15- 0;
16+ FILE *fd = port == 0x8 ? stdout : port == 0x9 ? stderr
17+ : 0;
18 if(fd) {
19 fputc(d[port], fd);
20 fflush(fd);
21@@ -78,9 +78,10 @@ main(int argc, char **argv)
22 {
23 Uxn u;
24 int i;
25+ Mmu mmu;
26 if(argc < 2)
27 return emu_error("Usage", "uxncli game.rom args");
28- if(!uxn_boot(&u, (Uint8 *)calloc(0x10300, sizeof(Uint8)), emu_dei, emu_deo))
29+ if(!uxn_boot(&u, mmu_init(&mmu, 16), emu_dei, emu_deo))
30 return emu_error("Boot", "Failed");
31 if(!load_rom(&u, argv[1]))
32 return emu_error("Load", "Failed");