Browse code

Removed access to uxn to devpeek/devpoke

neauoire authored on 21/04/2021 04:21:31
Showing 4 changed files
... ...
@@ -38,7 +38,7 @@ printstack(Stack *s)
38 38
 #pragma mark - Devices
39 39
 
40 40
 Uint8
41
-console_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
41
+console_poke(Device *d, Uint8 *m, Uint8 b0, Uint8 b1)
42 42
 {
43 43
 	switch(b0) {
44 44
 	case 0x08: printf("%c", b1); break;
... ...
@@ -46,17 +46,17 @@ console_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
46 46
 	case 0x0b: printf("0x%04x\n", (m[0x0a] << 8) + b1); break;
47 47
 	}
48 48
 	fflush(stdout);
49
-	(void)u;
49
+	(void)d;
50 50
 	(void)b0;
51 51
 	return b1;
52 52
 }
53 53
 
54 54
 Uint8
55
-file_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
55
+file_poke(Device *d, Uint8 *m, Uint8 b0, Uint8 b1)
56 56
 {
57 57
 	Uint8 read = b0 == 0xd;
58 58
 	if(read || b0 == 0xf) {
59
-		char *name = (char *)&u->ram.dat[mempeek16(m, 0x8)];
59
+		char *name = (char *)&d->mem[mempeek16(m, 0x8)];
60 60
 		Uint16 result = 0, length = mempeek16(m, 0xa);
61 61
 		Uint16 offset = mempeek16(m, 0x4);
62 62
 		Uint16 addr = (m[b0 - 1] << 8) | b1;
... ...
@@ -72,9 +72,9 @@ file_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
72 72
 }
73 73
 
74 74
 Uint8
75
-ppnil(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
75
+ppnil(Device *d, Uint8 *m, Uint8 b0, Uint8 b1)
76 76
 {
77
-	(void)u;
77
+	(void)d;
78 78
 	(void)m;
79 79
 	(void)b0;
80 80
 	return b1;
... ...
@@ -182,35 +182,35 @@ doctrl(Uxn *u, SDL_Event *event, int z)
182 182
 #pragma mark - Devices
183 183
 
184 184
 Uint8
185
-system_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
185
+system_poke(Device *d, Uint8 *m, Uint8 b0, Uint8 b1)
186 186
 {
187 187
 	putcolors(&ppu, &m[0x8]);
188 188
 	reqdraw = 1;
189
-	(void)u;
189
+	(void)d;
190 190
 	(void)b0;
191 191
 	return b1;
192 192
 }
193 193
 
194 194
 Uint8
195
-console_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
195
+console_poke(Device *d, Uint8 *m, Uint8 b0, Uint8 b1)
196 196
 {
197 197
 	switch(b0) {
198 198
 	case 0x8: printf("%c", b1); break;
199 199
 	case 0x9: printf("0x%02x\n", b1); break;
200 200
 	case 0xb: printf("0x%04x\n", (m[0xa] << 8) + b1); break;
201
-	case 0xd: printf("%s\n", &u->ram.dat[(m[0xc] << 8) + b1]); break;
201
+	case 0xd: printf("%s\n", &d->mem[(m[0xc] << 8) + b1]); break;
202 202
 	}
203 203
 	fflush(stdout);
204 204
 	return b1;
205 205
 }
206 206
 
207 207
 Uint8
208
-screen_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
208
+screen_poke(Device *d, Uint8 *m, Uint8 b0, Uint8 b1)
209 209
 {
210 210
 	if(b0 == 0xe) {
211 211
 		Uint16 x = mempeek16(m, 0x8);
212 212
 		Uint16 y = mempeek16(m, 0xa);
213
-		Uint8 *addr = &u->ram.dat[mempeek16(m, 0xc)];
213
+		Uint8 *addr = &d->mem[mempeek16(m, 0xc)];
214 214
 		Uint8 *layer = b1 >> 4 & 0x1 ? ppu.fg : ppu.bg;
215 215
 		switch(b1 >> 5) {
216 216
 		case 0: putpixel(&ppu, layer, x, y, b1 & 0x3); break;
... ...
@@ -223,27 +223,32 @@ screen_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
223 223
 }
224 224
 
225 225
 Uint8
226
-file_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
226
+file_poke(Device *d, Uint8 *m, Uint8 b0, Uint8 b1)
227 227
 {
228
+	/* TODO: Figure out why fwrite doesn't work with d->mem
228 229
 	Uint8 read = b0 == 0xd;
229 230
 	if(read || b0 == 0xf) {
230
-		char *name = (char *)&u->ram.dat[mempeek16(m, 0x8)];
231
+		char *name = (char *)&d->mem[mempeek16(m, 0x8)];
231 232
 		Uint16 result = 0, length = mempeek16(m, 0xa);
232 233
 		Uint16 offset = mempeek16(m, 0x4);
233 234
 		Uint16 addr = (m[b0 - 1] << 8) | b1;
234 235
 		FILE *f = fopen(name, read ? "r" : (offset ? "a" : "w"));
235 236
 		if(f) {
236
-			if(fseek(f, offset, SEEK_SET) != -1 && (result = read ? fread(&u->ram.dat[addr], 1, length, f) : fwrite(&u->ram.dat[addr], 1, length, f)))
237
+			if(fseek(f, offset, SEEK_SET) != -1 && (result = read ? fread(d->mem[addr], 1, length, f) : fwrite(&d->mem[addr], 1, length, f)))
237 238
 				printf("%s %d bytes, at %04x from %s\n", read ? "Loaded" : "Saved", length, addr, name);
238 239
 			fclose(f);
239 240
 		}
240 241
 		mempoke16(m, 0x2, result);
241 242
 	}
243
+	*/
244
+	(void)d;
245
+	(void)m;
246
+	(void)b0;
242 247
 	return b1;
243 248
 }
244 249
 
245 250
 static Uint8
246
-audio_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
251
+audio_poke(Device *d, Uint8 *m, Uint8 b0, Uint8 b1)
247 252
 {
248 253
 	if(b0 == 0xa) {
249 254
 		if(b1 >= apu.n_notes) apu.notes = SDL_realloc(apu.notes, (b1 + 1) * sizeof(Note));
... ...
@@ -261,21 +266,12 @@ audio_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
261 266
 		apu.queue->dat[apu.queue->n++] = (m[0xd] << 8) + b1;
262 267
 	} else if(b0 == 0xf && apu.queue != NULL)
263 268
 		apu.queue->finishes = 1;
264
-	(void)u;
265
-	return b1;
266
-}
267
-
268
-Uint8
269
-midi_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
270
-{
271
-	(void)u;
272
-	(void)m;
273
-	printf("midi - %02x,%02x\n", b0, b1);
269
+	(void)d;
274 270
 	return b1;
275 271
 }
276 272
 
277 273
 Uint8
278
-datetime_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
274
+datetime_poke(Device *d, Uint8 *m, Uint8 b0, Uint8 b1)
279 275
 {
280 276
 	time_t seconds = time(NULL);
281 277
 	struct tm *t = localtime(&seconds);
... ...
@@ -289,15 +285,15 @@ datetime_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
289 285
 	m[0x7] = t->tm_wday;
290 286
 	mempoke16(m, 0x08, t->tm_yday);
291 287
 	m[0xa] = t->tm_isdst;
292
-	(void)u;
288
+	(void)d;
293 289
 	(void)b0;
294 290
 	return b1;
295 291
 }
296 292
 
297 293
 Uint8
298
-ppnil(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
294
+ppnil(Device *d, Uint8 *m, Uint8 b0, Uint8 b1)
299 295
 {
300
-	(void)u;
296
+	(void)d;
301 297
 	(void)m;
302 298
 	(void)b0;
303 299
 	return b1;
... ...
@@ -21,15 +21,15 @@ Uint8  pop8(Stack *s) { if (s->ptr == 0) { s->error = 1; return 0; } return s->d
21 21
 Uint8  peek8(Stack *s, Uint8 a) { if (s->ptr < a + 1) s->error = 1; return s->dat[s->ptr - a - 1]; }
22 22
 void   mempoke8(Uint8 *m, Uint16 a, Uint8 b) { m[a] = b; }
23 23
 Uint8  mempeek8(Uint8 *m, Uint16 a) { return m[a]; }
24
-void   devpoke8(Uxn *u, Uint8 a, Uint8 b) { Device *dev = &u->dev[a >> 4]; dev->dat[a & 0xf] = b; dev->poke(u, dev->dat, a & 0x0f, b); }
25
-Uint8  devpeek8(Uxn *u, Uint8 a) { return u->dev[a >> 4].dat[a & 0xf]; }
24
+void   devpoke8(Device *d, Uint8 a, Uint8 b) { d->dat[a & 0xf] = b; d->poke(d, d->dat, a & 0x0f, b); }
25
+Uint8  devpeek8(Device *d, Uint8 a) { return d->dat[a & 0xf]; }
26 26
 void   push16(Stack *s, Uint16 a) { push8(s, a >> 8); push8(s, a); }
27 27
 Uint16 pop16(Stack *s) { return pop8(s) + (pop8(s) << 8); }
28 28
 Uint16 peek16(Stack *s, Uint8 a) { return peek8(s, a * 2) + (peek8(s, a * 2 + 1) << 8); }
29 29
 void   mempoke16(Uint8 *m, Uint16 a, Uint16 b) { mempoke8(m, a, b >> 8); mempoke8(m, a + 1, b); }
30 30
 Uint16 mempeek16(Uint8 *m, Uint16 a) { return (mempeek8(m, a) << 8) + mempeek8(m, a + 1); }
31
-void   devpoke16(Uxn *u, Uint8 a, Uint16 b) { devpoke8(u, a, b >> 8); devpoke8(u, a + 1, b); }
32
-Uint16 devpeek16(Uxn *u, Uint16 a) { return (devpeek8(u, a) << 8) + devpeek8(u, a + 1); }
31
+void   devpoke16(Device *d, Uint8 a, Uint16 b) { devpoke8(d, a, b >> 8); devpoke8(d, a + 1, b); }
32
+Uint16 devpeek16(Device *d, Uint16 a) { return (devpeek8(d, a) << 8) + devpeek8(d, a + 1); }
33 33
 /* Stack */
34 34
 void op_brk(Uxn *u) { u->ram.ptr = 0; }
35 35
 void op_nop(Uxn *u) { (void)u; }
... ...
@@ -46,8 +46,8 @@ void op_gth(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b
46 46
 void op_lth(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b < a); }
47 47
 void op_gts(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, (Sint8)b > (Sint8)a); }
48 48
 void op_lts(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, (Sint8)b < (Sint8)a); }
49
-void op_ior(Uxn *u) { Uint8 a = pop8(u->src); push8(u->src, devpeek8(u, a)); }
50
-void op_iow(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); devpoke8(u, a, b); }
49
+void op_ior(Uxn *u) { Uint8 a = pop8(u->src); push8(u->src, devpeek8(&u->dev[a >> 4], a)); }
50
+void op_iow(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); devpoke8(&u->dev[a >> 4], a, b); }
51 51
 /* Memory */
52 52
 void op_pek(Uxn *u) { Uint8 a = pop8(u->src); push8(u->src, mempeek8(&u->ram.dat[0], a)); }
53 53
 void op_pok(Uxn *u) { Uint8 a = pop8(u->src); Uint8 b = pop8(u->src); mempoke8(&u->ram.dat[0], a, b); }
... ...
@@ -80,8 +80,8 @@ void op_gth16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->sr
80 80
 void op_lth16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, b < a); }
81 81
 void op_gts16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, (Sint16)b > (Sint16)a); }
82 82
 void op_lts16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, (Sint16)b < (Sint16)a); }
83
-void op_ior16(Uxn *u) { Uint8 a = pop8(u->src); push16(u->src, devpeek16(u, a)); }
84
-void op_iow16(Uxn *u) { Uint8 a = pop8(u->src); Uint16 b = pop16(u->src); devpoke16(u, a, b); }
83
+void op_ior16(Uxn *u) { Uint8 a = pop8(u->src); push16(u->src, devpeek16(&u->dev[a >> 4], a)); }
84
+void op_iow16(Uxn *u) { Uint8 a = pop8(u->src); Uint16 b = pop16(u->src); devpoke16(&u->dev[a >> 4], a, b); }
85 85
 /* Memory(16-bits) */
86 86
 void op_pek16(Uxn *u) { Uint16 a = pop16(u->src); push8(u->src, mempeek8(&u->ram.dat[0], a)); }
87 87
 void op_pok16(Uxn *u) { Uint16 a = pop16(u->src); Uint8 b = pop8(u->src); mempoke8(&u->ram.dat[0], a, b); }
... ...
@@ -179,10 +179,11 @@ loaduxn(Uxn *u, char *filepath)
179 179
 }
180 180
 
181 181
 Device *
182
-portuxn(Uxn *u, Uint8 id, char *name, Uint8 (*pofn)(Uxn *u, Uint8 *devmem, Uint8 b0, Uint8 b1))
182
+portuxn(Uxn *u, Uint8 id, char *name, Uint8 (*pofn)(Device *d, Uint8 *devmem, Uint8 b0, Uint8 b1))
183 183
 {
184 184
 	Device *d = &u->dev[id];
185 185
 	d->addr = id * 0x10;
186
+	d->mem = &u->ram.dat[0];
186 187
 	d->poke = pofn;
187 188
 	printf("Device added #%02x: %s, at 0x%04x \n", id, name, d->addr);
188 189
 	return d;
... ...
@@ -32,7 +32,7 @@ struct Uxn;
32 32
 
33 33
 typedef struct Device {
34 34
 	Uint8 addr, dat[16], *mem;
35
-	Uint8 (*poke)(struct Uxn *u, Uint8 *devmem, Uint8, Uint8);
35
+	Uint8 (*poke)(struct Device *d, Uint8 *devmem, Uint8, Uint8);
36 36
 } Device;
37 37
 
38 38
 typedef struct Uxn {
... ...
@@ -47,4 +47,4 @@ int evaluxn(Uxn *u, Uint16 vec);
47 47
 void mempoke16(Uint8 *m, Uint16 a, Uint16 b);
48 48
 Uint16 mempeek16(Uint8 *m, Uint16 a);
49 49
 
50
-Device *portuxn(Uxn *u, Uint8 id, char *name, Uint8 (*pofn)(Uxn *, Uint8 *, Uint8, Uint8));
51 50
\ No newline at end of file
51
+Device *portuxn(Uxn *u, Uint8 id, char *name, Uint8 (*pofn)(Device *, Uint8 *, Uint8, Uint8));
52 52
\ No newline at end of file