Browse code

Started work on the hypervisor

neauoire authored on 06/01/2022 03:48:51
Showing 5 changed files
... ...
@@ -45,8 +45,8 @@ Uint8
45 45
 system_dei(Device *d, Uint8 port)
46 46
 {
47 47
 	switch(port) {
48
-	case 0x2: return d->u->wst.ptr;
49
-	case 0x3: return d->u->rst.ptr;
48
+	case 0x2: return d->u->wst->ptr;
49
+	case 0x3: return d->u->rst->ptr;
50 50
 	default: return d->dat[port];
51 51
 	}
52 52
 }
... ...
@@ -56,8 +56,8 @@ system_deo(Device *d, Uint8 port)
56 56
 {
57 57
 	switch(port) {
58 58
 	case 0x1: DEVPEEK16(d->vector, 0x0); break;
59
-	case 0x2: d->u->wst.ptr = d->dat[port]; break;
60
-	case 0x3: d->u->rst.ptr = d->dat[port]; break;
59
+	case 0x2: d->u->wst->ptr = d->dat[port]; break;
60
+	case 0x3: d->u->rst->ptr = d->dat[port]; break;
61 61
 	default: system_deo_special(d, port);
62 62
 	}
63 63
 }
... ...
@@ -39,13 +39,13 @@ uxn_eval(Uxn *u, Uint16 pc)
39 39
 	Stack *src, *dst;
40 40
 	Device *dev;
41 41
 	if(!pc || u->dev[0].dat[0xf]) return 0;
42
-	if(u->wst.ptr > 0xf8) u->wst.ptr = 0xf8;
42
+	if(u->wst->ptr > 0xf8) u->wst->ptr = 0xf8;
43 43
 	while((instr = u->ram[pc++])) {
44 44
 		/* Return Mode */
45 45
 		if(instr & 0x40) {
46
-			src = &u->rst; dst = &u->wst;
46
+			src = u->rst; dst = u->wst;
47 47
 		} else {
48
-			src = &u->wst; dst = &u->rst;
48
+			src = u->wst; dst = u->rst;
49 49
 		}
50 50
 		/* Keep Mode */
51 51
 		if(instr & 0x80) {
... ...
@@ -108,12 +108,14 @@ err:
108 108
 /* clang-format on */
109 109
 
110 110
 int
111
-uxn_boot(Uxn *u, Uint8 *memory)
111
+uxn_boot(Uxn *u, Stack *wst, Stack *rst, Uint8 *memory)
112 112
 {
113 113
 	Uint32 i;
114 114
 	char *cptr = (char *)u;
115 115
 	for(i = 0; i < sizeof(*u); i++)
116 116
 		cptr[i] = 0x00;
117
+	u->wst = wst;
118
+	u->rst = rst;
117 119
 	u->ram = memory;
118 120
 	return 1;
119 121
 }
... ...
@@ -38,12 +38,12 @@ typedef struct Device {
38 38
 } Device;
39 39
 
40 40
 typedef struct Uxn {
41
-	Stack wst, rst;
41
+	Stack *wst, *rst;
42 42
 	Uint8 *ram;
43 43
 	Device dev[16];
44 44
 } Uxn;
45 45
 
46
-int uxn_boot(Uxn *c, Uint8 *memory);
46
+int uxn_boot(Uxn *u, Stack *wst, Stack *rst, Uint8 *memory);
47 47
 int uxn_eval(Uxn *u, Uint16 pc);
48 48
 int uxn_halt(Uxn *u, Uint8 error, Uint16 addr);
49 49
 Device *uxn_port(Uxn *u, Uint8 id, Uint8 (*deifn)(Device *, Uint8), void (*deofn)(Device *, Uint8));
... ...
@@ -50,8 +50,8 @@ void
50 50
 system_deo_special(Device *d, Uint8 port)
51 51
 {
52 52
 	if(port == 0xe) {
53
-		inspect(&d->u->wst, "Working-stack");
54
-		inspect(&d->u->rst, "Return-stack");
53
+		inspect(d->u->wst, "Working-stack");
54
+		inspect(d->u->rst, "Return-stack");
55 55
 	}
56 56
 }
57 57
 
... ...
@@ -133,7 +133,7 @@ load(Uxn *u, char *filepath)
133 133
 	return 1;
134 134
 }
135 135
 
136
-static Uint8 *memory;
136
+static Uint8 *shadow, *memory;
137 137
 
138 138
 int
139 139
 main(int argc, char **argv)
... ...
@@ -141,8 +141,9 @@ main(int argc, char **argv)
141 141
 	Uxn u;
142 142
 	int i, loaded = 0;
143 143
 
144
+	shadow = (Uint8 *)calloc(0xffff, sizeof(Uint8));
144 145
 	memory = (Uint8 *)calloc(0xffff, sizeof(Uint8));
145
-	if(!uxn_boot(&u, memory))
146
+	if(!uxn_boot(&u, (Stack *)(shadow + 0x200), (Stack *)(shadow + 0x400), memory))
146 147
 		return error("Boot", "Failed");
147 148
 
148 149
 	/* system   */ devsystem = uxn_port(&u, 0x0, system_dei, system_deo);
... ...
@@ -44,6 +44,7 @@ static SDL_Rect gRect;
44 44
 static Device *devsystem, *devscreen, *devmouse, *devctrl, *devaudio0, *devconsole;
45 45
 static Uint8 zoom = 1;
46 46
 static Uint32 stdin_event, audio0_event;
47
+static Uxn hypervisor;
47 48
 
48 49
 static int
49 50
 clamp(int val, int min, int max)
... ...
@@ -126,7 +127,7 @@ static void
126 127
 redraw(Uxn *u)
127 128
 {
128 129
 	if(devsystem->dat[0xe])
129
-		screen_debug(&uxn_screen, u->wst.dat, u->wst.ptr, u->rst.ptr, u->ram);
130
+		screen_debug(&uxn_screen, u->wst->dat, u->wst->ptr, u->rst->ptr, u->ram);
130 131
 	screen_redraw(&uxn_screen, uxn_screen.pixels);
131 132
 	if(SDL_UpdateTexture(gTexture, NULL, uxn_screen.pixels, uxn_screen.width * sizeof(Uint32)) != 0)
132 133
 		error("SDL_UpdateTexture", SDL_GetError());
... ...
@@ -272,13 +273,17 @@ load(Uxn *u, char *rom)
272 273
 	return 1;
273 274
 }
274 275
 
275
-static Uint8 *memory;
276
+static Uint8 *shadow, *memory;
276 277
 
277 278
 static int
278 279
 start(Uxn *u, char *rom)
279 280
 {
280 281
 	memory = (Uint8 *)calloc(0xffff, sizeof(Uint8));
281
-	if(!uxn_boot(u, memory))
282
+	shadow = (Uint8 *)calloc(0xffff, sizeof(Uint8));
283
+
284
+	if(!uxn_boot(&hypervisor, (Stack *)(shadow + 0x600), (Stack *)(shadow + 0x800), shadow))
285
+		return error("Boot", "Failed to start uxn.");
286
+	if(!uxn_boot(u, (Stack *)(shadow + 0x200), (Stack *)(shadow + 0x400), memory))
282 287
 		return error("Boot", "Failed to start uxn.");
283 288
 	if(!load(u, rom))
284 289
 		return error("Boot", "Failed to load rom.");