Browse code

Rename u->ram.dat to u->ram, u->ram.ptr to pc.

Andrew Alderwick authored on 04/01/2022 01:36:21
Showing 4 changed files
... ...
@@ -14,7 +14,7 @@ WITH REGARD TO THIS SOFTWARE.
14 14
 /* clang-format off */
15 15
 
16 16
 /*	a,b,c: general use.  bs: byte/short bool. src, dst: stack ptrs, swapped in return mode.
17
-	sp: ptr to src stack ptr. kptr: "keep" mode copy of src stack ptr.
17
+	pc: program counter. sp: ptr to src stack ptr. kptr: "keep" mode copy of src stack ptr.
18 18
 	x,y: macro in params. d: macro in device. j,k,dev: macro temp variables. o: macro out param. */
19 19
 
20 20
 #define PUSH8(s, x) { if(s->ptr == 0xff) { errcode = 2; goto err; } s->dat[s->ptr++] = (x); }
... ...
@@ -23,25 +23,24 @@ WITH REGARD TO THIS SOFTWARE.
23 23
 #define POP8(o) { if(!(j = *sp)) { errcode = 1; goto err; } o = (Uint16)src->dat[--j]; *sp = j; }
24 24
 #define POP16(o) { if((j = *sp) <= 1) { errcode = 1; goto err; } o = src->dat[j - 1]; o += src->dat[j - 2] << 8; *sp = j - 2; }
25 25
 #define POP(o) { if(bs) { POP16(o) } else { POP8(o) } }
26
-#define POKE(x, y) { if(bs) { u->ram.dat[(x)] = (y) >> 8; u->ram.dat[(x) + 1] = (y); } else { u->ram.dat[(x)] = y; } }
27
-#define PEEK16(o, x) { o = (u->ram.dat[(x)] << 8) + u->ram.dat[(x) + 1]; }
28
-#define PEEK(o, x) { if(bs) { PEEK16(o, x) } else { o = u->ram.dat[(x)]; } }
26
+#define POKE(x, y) { if(bs) { u->ram[(x)] = (y) >> 8; u->ram[(x) + 1] = (y); } else { u->ram[(x)] = y; } }
27
+#define PEEK16(o, x) { o = (u->ram[(x)] << 8) + u->ram[(x) + 1]; }
28
+#define PEEK(o, x) { if(bs) { PEEK16(o, x) } else { o = u->ram[(x)]; } }
29 29
 #define DEVR(o, d, x) { dev = (d); o = dev->dei(dev, (x) & 0x0f); if(bs) { o = (o << 8) + dev->dei(dev, ((x) + 1) & 0x0f); } }
30 30
 #define DEVW8(x, y) { dev->dat[(x) & 0xf] = y; dev->deo(dev, (x) & 0x0f); }
31 31
 #define DEVW(d, x, y) { dev = (d); if(bs) { DEVW8((x), (y) >> 8); DEVW8((x) + 1, (y)); } else { DEVW8((x), (y)) } }
32
-#define WARP(x) { if(bs) u->ram.ptr = (x); else u->ram.ptr += (Sint8)(x); }
32
+#define WARP(x) { if(bs) pc = (x); else pc += (Sint8)(x); }
33 33
 
34 34
 int
35
-uxn_eval(Uxn *u, Uint16 vec)
35
+uxn_eval(Uxn *u, Uint16 pc)
36 36
 {
37 37
 	unsigned int a, b, c, j, k, bs, instr, errcode;
38 38
 	Uint8 kptr, *sp;
39 39
 	Stack *src, *dst;
40 40
 	Device *dev;
41
-	if(!vec || u->dev[0].dat[0xf]) return 0;
42
-	u->ram.ptr = vec;
41
+	if(!pc || u->dev[0].dat[0xf]) return 0;
43 42
 	if(u->wst.ptr > 0xf8) u->wst.ptr = 0xf8;
44
-	while((instr = u->ram.dat[u->ram.ptr++])) {
43
+	while((instr = u->ram[pc++])) {
45 44
 		/* Return Mode */
46 45
 		if(instr & 0x40) {
47 46
 			src = &u->rst; dst = &u->wst;
... ...
@@ -59,8 +58,8 @@ uxn_eval(Uxn *u, Uint16 vec)
59 58
 		bs = instr & 0x20 ? 1 : 0;
60 59
 		switch(instr & 0x1f) {
61 60
 		/* Stack */
62
-		case 0x00: /* LIT */ if(bs) { PEEK16(a, u->ram.ptr) PUSH16(src, a) u->ram.ptr += 2; }
63
-		                     else   { a = u->ram.dat[u->ram.ptr++]; PUSH8(src, a) } break;
61
+		case 0x00: /* LIT */ if(bs) { PEEK16(a, pc) PUSH16(src, a) pc += 2; }
62
+		                     else   { a = u->ram[pc]; PUSH8(src, a) ++pc; } break;
64 63
 		case 0x01: /* INC */ POP(a) PUSH(src, a + 1) break;
65 64
 		case 0x02: /* POP */ POP(a) break;
66 65
 		case 0x03: /* DUP */ POP(a) PUSH(src, a) PUSH(src, a) break;
... ...
@@ -75,13 +74,13 @@ uxn_eval(Uxn *u, Uint16 vec)
75 74
 		case 0x0b: /* LTH */ POP(a) POP(b) PUSH8(src, b < a) break;
76 75
 		case 0x0c: /* JMP */ POP(a) WARP(a) break;
77 76
 		case 0x0d: /* JCN */ POP(a) POP8(b) if(b) WARP(a) break;
78
-		case 0x0e: /* JSR */ POP(a) PUSH16(dst, u->ram.ptr) WARP(a) break;
77
+		case 0x0e: /* JSR */ POP(a) PUSH16(dst, pc) WARP(a) break;
79 78
 		case 0x0f: /* STH */ POP(a) PUSH(dst, a) break;
80 79
 		/* Memory */
81 80
 		case 0x10: /* LDZ */ POP8(a) PEEK(b, a) PUSH(src, b) break;
82 81
 		case 0x11: /* STZ */ POP8(a) POP(b) POKE(a, b) break;
83
-		case 0x12: /* LDR */ POP8(a) PEEK(b, u->ram.ptr + (Sint8)a) PUSH(src, b) break;
84
-		case 0x13: /* STR */ POP8(a) POP(b) c = u->ram.ptr + (Sint8)a; POKE(c, b) break;
82
+		case 0x12: /* LDR */ POP8(a) PEEK(b, pc + (Sint8)a) PUSH(src, b) break;
83
+		case 0x13: /* STR */ POP8(a) POP(b) c = pc + (Sint8)a; POKE(c, b) break;
85 84
 		case 0x14: /* LDA */ POP16(a) PEEK(b, a) PUSH(src, b) break;
86 85
 		case 0x15: /* STA */ POP16(a) POP(b) POKE(a, b) break;
87 86
 		case 0x16: /* DEI */ POP8(a) DEVR(b, &u->dev[a >> 4], a) PUSH(src, b) break;
... ...
@@ -100,7 +99,7 @@ uxn_eval(Uxn *u, Uint16 vec)
100 99
 	return 1;
101 100
 
102 101
 err:
103
-	return uxn_halt(u, errcode, "Stack", instr);
102
+	return uxn_halt(u, errcode, "Stack", pc - 1);
104 103
 }
105 104
 
106 105
 /* clang-format on */
... ...
@@ -112,7 +111,7 @@ uxn_boot(Uxn *u, Uint8 *memory)
112 111
 	char *cptr = (char *)u;
113 112
 	for(i = 0; i < sizeof(*u); ++i)
114 113
 		cptr[i] = 0x00;
115
-	u->ram.dat = memory;
114
+	u->ram = memory;
116 115
 	return 1;
117 116
 }
118 117
 
... ...
@@ -122,7 +121,7 @@ uxn_port(Uxn *u, Uint8 id, Uint8 (*deifn)(Device *d, Uint8 port), void (*deofn)(
122 121
 	Device *d = &u->dev[id];
123 122
 	d->addr = id * 0x10;
124 123
 	d->u = u;
125
-	d->mem = u->ram.dat;
124
+	d->mem = u->ram;
126 125
 	d->dei = deifn;
127 126
 	d->deo = deofn;
128 127
 	return d;
... ...
@@ -29,11 +29,6 @@ typedef struct {
29 29
 	Uint8 dat[256];
30 30
 } Stack;
31 31
 
32
-typedef struct {
33
-	Uint16 ptr;
34
-	Uint8 *dat;
35
-} Memory;
36
-
37 32
 typedef struct Device {
38 33
 	struct Uxn *u;
39 34
 	Uint8 addr, dat[16], *mem;
... ...
@@ -44,11 +39,11 @@ typedef struct Device {
44 39
 
45 40
 typedef struct Uxn {
46 41
 	Stack wst, rst;
47
-	Memory ram;
42
+	Uint8 *ram;
48 43
 	Device dev[16];
49 44
 } Uxn;
50 45
 
51 46
 int uxn_boot(Uxn *c, Uint8 *memory);
52
-int uxn_eval(Uxn *u, Uint16 vec);
53
-int uxn_halt(Uxn *u, Uint8 error, char *name, int id);
47
+int uxn_eval(Uxn *u, Uint16 pc);
48
+int uxn_halt(Uxn *u, Uint8 error, char *name, Uint16 addr);
54 49
 Device *uxn_port(Uxn *u, Uint8 id, Uint8 (*deifn)(Device *, Uint8), void (*deofn)(Device *, Uint8));
... ...
@@ -118,9 +118,9 @@ nil_deo(Device *d, Uint8 port)
118 118
 static const char *errors[] = {"underflow", "overflow", "division by zero"};
119 119
 
120 120
 int
121
-uxn_halt(Uxn *u, Uint8 error, char *name, int id)
121
+uxn_halt(Uxn *u, Uint8 error, char *name, Uint16 addr)
122 122
 {
123
-	fprintf(stderr, "Halted: %s %s#%04x, at 0x%04x\n", name, errors[error - 1], id, u->ram.ptr);
123
+	fprintf(stderr, "Halted: %s %s#%04x, at 0x%04x\n", name, errors[error - 1], u->ram[addr], addr);
124 124
 	return 0;
125 125
 }
126 126
 
... ...
@@ -138,7 +138,6 @@ run(Uxn *u)
138 138
 	Device *d = devconsole;
139 139
 	while((!u->dev[0].dat[0xf]) && (read(0, &devconsole->dat[0x2], 1) > 0)) {
140 140
 		DEVPEEK16(vec, 0);
141
-		if(!vec) vec = u->ram.ptr; /* continue after last BRK */
142 141
 		uxn_eval(u, vec);
143 142
 	}
144 143
 }
... ...
@@ -149,7 +148,7 @@ load(Uxn *u, char *filepath)
149 148
 	FILE *f;
150 149
 	int r;
151 150
 	if(!(f = fopen(filepath, "rb"))) return 0;
152
-	r = fread(u->ram.dat + PAGE_PROGRAM, 1, 0xffff - PAGE_PROGRAM, f);
151
+	r = fread(u->ram + PAGE_PROGRAM, 1, 0xffff - PAGE_PROGRAM, f);
153 152
 	fclose(f);
154 153
 	if(r < 1) return 0;
155 154
 	fprintf(stderr, "Loaded %s\n", filepath);
... ...
@@ -125,7 +125,7 @@ static void
125 125
 redraw(Uxn *u)
126 126
 {
127 127
 	if(devsystem->dat[0xe])
128
-		screen_debug(&uxn_screen, u->wst.dat, u->wst.ptr, u->rst.ptr, u->ram.dat);
128
+		screen_debug(&uxn_screen, u->wst.dat, u->wst.ptr, u->rst.ptr, u->ram);
129 129
 	screen_redraw(&uxn_screen, uxn_screen.pixels);
130 130
 	if(SDL_UpdateTexture(gTexture, &gRect, uxn_screen.pixels, uxn_screen.width * sizeof(Uint32)) != 0)
131 131
 		error("SDL_UpdateTexture", SDL_GetError());
... ...
@@ -276,7 +276,7 @@ load(Uxn *u, char *rom)
276 276
 	SDL_RWops *f;
277 277
 	int r;
278 278
 	if(!(f = SDL_RWFromFile(rom, "rb"))) return 0;
279
-	r = f->read(f, u->ram.dat + PAGE_PROGRAM, 1, 0xffff - PAGE_PROGRAM);
279
+	r = f->read(f, u->ram + PAGE_PROGRAM, 1, 0xffff - PAGE_PROGRAM);
280 280
 	f->close(f);
281 281
 	if(r < 1) return 0;
282 282
 	fprintf(stderr, "Loaded %s\n", rom);
... ...
@@ -413,9 +413,9 @@ do_shortcut(Uxn *u, SDL_Event *event)
413 413
 static const char *errors[] = {"underflow", "overflow", "division by zero"};
414 414
 
415 415
 int
416
-uxn_halt(Uxn *u, Uint8 error, char *name, int id)
416
+uxn_halt(Uxn *u, Uint8 error, char *name, Uint16 addr)
417 417
 {
418
-	fprintf(stderr, "Halted: %s %s#%04x, at 0x%04x\n", name, errors[error - 1], id, u->ram.ptr);
418
+	fprintf(stderr, "Halted: %s %s#%04x, at 0x%04x\n", name, errors[error - 1], u->ram[addr], addr);
419 419
 	return 0;
420 420
 }
421 421