Browse code

Added memory to individual devices.

Andrew Alderwick authored on 20/04/2021 21:30:26
Showing 9 changed files
... ...
@@ -2,13 +2,9 @@
2 2
 
3 3
 %RTN { JMP2r }
4 4
 
5
-( devices )
6
-
7
-|0110 @Console    [ &pad $8 &char $1 ]
8
-
9 5
 ( program )
10 6
 
11
-|0200
7
+|0100
12 8
 	
13 9
 	;hello-word ;print JSR2
14 10
 	
... ...
@@ -25,3 +21,8 @@ BRK
25 21
 RTN
26 22
 
27 23
 @hello-word [ 48 65 6c 6c 6f 20 57 6f 72 6c 64 21 ]
24
+
25
+( devices )
26
+
27
+|ff10 @Console    [ &pad $8 &char $1 ]
28
+
... ...
@@ -10,14 +10,7 @@
10 10
 
11 11
 @slime $1
12 12
 
13
-( devices )
14
-
15
-|0100 @System     [ &vector $2 &pad    $6 &r      $2 &g     $2 &b      $2 ]
16
-|0110 @Console    [ &pad    $8 &char   $1 &byte   $1 &short $2 &string $2 ]
17
-|0120 @Screen     [ &vector $2 &width  $2 &height $2 &pad   $2 &x      $2 &y $2 &addr $2 &color $1 ]
18
-|0140 @Controller [ &vector $2 &button $1 &key    $1 ]
19
-
20
-|0200
13
+|0100
21 14
 	
22 15
 	( theme ) 
23 16
 	#0daf .System/r IOW2 
... ...
@@ -84,3 +77,11 @@ BRK
84 77
 @left_icn    [ 3c7e ef1f 1fef 7e3c ]
85 78
 @right_icn   [ 3c7e f7f8 f8f7 7e3c ]
86 79
 @slime_icn   [ 0000 183c 3c18 0000 ]
80
+
81
+( devices )
82
+
83
+|ff00 @System     [ &vector $2 &pad    $6 &r      $2 &g     $2 &b      $2 ]
84
+|ff10 @Console    [ &pad    $8 &char   $1 &byte   $1 &short $2 &string $2 ]
85
+|ff20 @Screen     [ &vector $2 &width  $2 &height $2 &pad   $2 &x      $2 &y $2 &addr $2 &color $1 ]
86
+|ff40 @Controller [ &vector $2 &button $1 &key    $1 ]
87
+
... ...
@@ -45,7 +45,7 @@ render_note(Apu *apu, Uxn *u, int note_i, Sint16 *samples, int n_samples)
45 45
 				if(apu->queue->i == apu->queue->n) {
46 46
 					apu->queue->i = apu->queue->n = 0;
47 47
 					if(!apu->queue->finishes) {
48
-						u->ram.dat[apu->channel_addr] = note_i;
48
+						*apu->channel_ptr = note_i;
49 49
 						evaluxn(u, wv->vector);
50 50
 					}
51 51
 				}
... ...
@@ -36,8 +36,8 @@ typedef struct {
36 36
 typedef struct {
37 37
 	Queue *queue;
38 38
 	Note *notes;
39
+	Uint8 *channel_ptr;
39 40
 	int n_notes;
40
-	Uint16 channel_addr;
41 41
 } Apu;
42 42
 
43 43
 void apu_render(Apu *apu, Uxn *u, Sint16 *samples, int n_samples);
... ...
@@ -11,7 +11,7 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 11
 WITH REGARD TO THIS SOFTWARE.
12 12
 */
13 13
 
14
-#define TRIM 0x0200
14
+#define TRIM 0x0100
15 15
 
16 16
 typedef unsigned char Uint8;
17 17
 typedef signed char Sint8;
... ...
@@ -38,51 +38,44 @@ printstack(Stack *s)
38 38
 #pragma mark - Devices
39 39
 
40 40
 Uint8
41
-console_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
41
+console_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
42 42
 {
43
-	Uint8 *m = u->ram.dat;
44 43
 	switch(b0) {
45 44
 	case 0x08: printf("%c", b1); break;
46 45
 	case 0x09: printf("0x%02x\n", b1); break;
47
-	case 0x0b: printf("0x%04x\n", (m[ptr + 0x0a] << 8) + b1); break;
46
+	case 0x0b: printf("0x%04x\n", (m[0x0a] << 8) + b1); break;
48 47
 	}
49 48
 	fflush(stdout);
50
-	(void)m;
51
-	(void)ptr;
49
+	(void)u;
52 50
 	(void)b0;
53 51
 	return b1;
54 52
 }
55 53
 
56 54
 Uint8
57
-file_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
55
+file_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
58 56
 {
59
-	Uint8 *m = u->ram.dat;
60
-	char *name = (char *)&m[(m[ptr + 8] << 8) + m[ptr + 8 + 1]];
61
-	Uint16 length = (m[ptr + 8 + 2] << 8) + m[ptr + 8 + 3];
62
-	Uint16 offset = (m[ptr + 0] << 8) + m[ptr + 1];
63
-	if(b0 == 0x0d) {
64
-		Uint16 addr = (m[ptr + 8 + 4] << 8) + b1;
65
-		FILE *f = fopen(name, "r");
66
-		if(f && fseek(f, offset, SEEK_SET) != -1 && fread(&m[addr], length, 1, f)) {
57
+	Uint8 read = b0 == 0xd;
58
+	if(read || b0 == 0xf) {
59
+		char *name = (char *)&u->ram.dat[genpeek16(m, 0x8)];
60
+		Uint16 result = 0, length = genpeek16(m, 0xa);
61
+		Uint16 offset = genpeek16(m, 0x4);
62
+		Uint16 addr = (m[b0 - 1] << 8) | b1;
63
+		FILE *f = fopen(name, read ? "r" : (offset ? "a" : "w"));
64
+		if(f) {
65
+			if(fseek(f, offset, SEEK_SET) != -1 && (result = read ? fread(&m[addr], 1, length, f) : fwrite(&m[addr], 1, length, f)))
66
+				printf("%s %d bytes, at %04x from %s\n", read ? "Loaded" : "Saved", length, addr, name);
67 67
 			fclose(f);
68
-			printf("Loaded %d bytes, at %04x from %s\n", length, addr, name);
69
-		}
70
-	} else if(b0 == 0x0f) {
71
-		Uint16 addr = (m[ptr + 8 + 6] << 8) + b1;
72
-		FILE *f = fopen(name, (m[ptr + 2] & 0x1) ? "a" : "w");
73
-		if(f && fseek(f, offset, SEEK_SET) != -1 && fwrite(&m[addr], length, 1, f)) {
74
-			fclose(f);
75
-			printf("Saved %d bytes, at %04x from %s\n", length, addr, name);
76 68
 		}
69
+		genpoke16(m, 0x2, result);
77 70
 	}
78 71
 	return b1;
79 72
 }
80 73
 
81 74
 Uint8
82
-ppnil(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
75
+ppnil(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
83 76
 {
84 77
 	(void)u;
85
-	(void)ptr;
78
+	(void)m;
86 79
 	(void)b0;
87 80
 	return b1;
88 81
 }
... ...
@@ -125,28 +125,28 @@ init(Uxn *u)
125 125
 }
126 126
 
127 127
 void
128
-domouse(Uxn *u, SDL_Event *event)
128
+domouse(SDL_Event *event)
129 129
 {
130 130
 	Uint8 flag = 0x00;
131 131
 	Uint16 x = clamp(event->motion.x / zoom - ppu.pad, 0, ppu.hor * 8 - 1);
132 132
 	Uint16 y = clamp(event->motion.y / zoom - ppu.pad, 0, ppu.ver * 8 - 1);
133
-	mempoke16(u, devmouse->addr + 2, x);
134
-	mempoke16(u, devmouse->addr + 4, y);
135
-	u->ram.dat[devmouse->addr + 7] = 0x00;
133
+	genpoke16(devmouse->dat, 0x2, x);
134
+	genpoke16(devmouse->dat, 0x4, y);
135
+	devmouse->dat[7] = 0x00;
136 136
 	switch(event->button.button) {
137 137
 	case SDL_BUTTON_LEFT: flag = 0x01; break;
138 138
 	case SDL_BUTTON_RIGHT: flag = 0x10; break;
139 139
 	}
140 140
 	switch(event->type) {
141 141
 	case SDL_MOUSEBUTTONDOWN:
142
-		u->ram.dat[devmouse->addr + 6] |= flag;
143
-		if(flag == 0x10 && (u->ram.dat[devmouse->addr + 6] & 0x01))
144
-			u->ram.dat[devmouse->addr + 7] = 0x01;
145
-		if(flag == 0x01 && (u->ram.dat[devmouse->addr + 6] & 0x10))
146
-			u->ram.dat[devmouse->addr + 7] = 0x10;
142
+		devmouse->dat[6] |= flag;
143
+		if(flag == 0x10 && (devmouse->dat[6] & 0x01))
144
+			devmouse->dat[7] = 0x01;
145
+		if(flag == 0x01 && (devmouse->dat[6] & 0x10))
146
+			devmouse->dat[7] = 0x10;
147 147
 		break;
148 148
 	case SDL_MOUSEBUTTONUP:
149
-		u->ram.dat[devmouse->addr + 6] &= (~flag);
149
+		devmouse->dat[6] &= (~flag);
150 150
 		break;
151 151
 	}
152 152
 }
... ...
@@ -172,45 +172,45 @@ doctrl(Uxn *u, SDL_Event *event, int z)
172 172
 	case SDLK_RIGHT: flag = 0x80; break;
173 173
 	}
174 174
 	if(flag && z)
175
-		u->ram.dat[devctrl->addr + 2] |= flag;
175
+		devctrl->dat[2] |= flag;
176 176
 	else if(flag)
177
-		u->ram.dat[devctrl->addr + 2] &= (~flag);
177
+		devctrl->dat[2] &= (~flag);
178 178
 	if(z && event->key.keysym.sym < 20)
179
-		u->ram.dat[devctrl->addr + 3] = event->key.keysym.sym;
179
+		devctrl->dat[3] = event->key.keysym.sym;
180 180
 }
181 181
 
182 182
 #pragma mark - Devices
183 183
 
184 184
 Uint8
185
-system_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
185
+system_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
186 186
 {
187
-	getcolors(&ppu, &u->ram.dat[ptr + 0x0008]);
187
+	getcolors(&ppu, &m[0x8]);
188 188
 	reqdraw = 1;
189
+	(void)u;
189 190
 	(void)b0;
190 191
 	return b1;
191 192
 }
192 193
 
193 194
 Uint8
194
-console_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
195
+console_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
195 196
 {
196
-	Uint8 *m = u->ram.dat;
197 197
 	switch(b0) {
198
-	case 0x08: printf("%c", b1); break;
199
-	case 0x09: printf("0x%02x\n", b1); break;
200
-	case 0x0b: printf("0x%04x\n", (m[ptr + 0x0a] << 8) + b1); break;
201
-	case 0x0d: printf("%s\n", &m[(m[ptr + 0x0c] << 8) + b1]); break;
198
+	case 0x8: printf("%c", b1); break;
199
+	case 0x9: printf("0x%02x\n", b1); break;
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;
202 202
 	}
203 203
 	fflush(stdout);
204 204
 	return b1;
205 205
 }
206 206
 
207 207
 Uint8
208
-screen_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
208
+screen_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
209 209
 {
210
-	if(b0 == 0x0e) {
211
-		Uint16 x = mempeek16(u, devscreen->addr + 0x08);
212
-		Uint16 y = mempeek16(u, devscreen->addr + 0x0a);
213
-		Uint8 *addr = &u->ram.dat[mempeek16(u, devscreen->addr + 0x0c)];
210
+	if(b0 == 0xe) {
211
+		Uint16 x = genpeek16(m, 0x8);
212
+		Uint16 y = genpeek16(m, 0xa);
213
+		Uint8 *addr = &u->ram.dat[genpeek16(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;
... ...
@@ -219,38 +219,36 @@ screen_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
219 219
 		}
220 220
 		reqdraw = 1;
221 221
 	}
222
-	(void)ptr;
223 222
 	return b1;
224 223
 }
225 224
 
226 225
 Uint8
227
-file_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
226
+file_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
228 227
 {
229
-	Uint8 *m = u->ram.dat, read = b0 == 0x0d;
230
-	if(read || b0 == 0x0f) {
231
-		char *name = (char *)&m[mempeek16(u, ptr + 8)];
232
-		Uint16 result = 0, length = mempeek16(u, ptr + 8 + 2);
233
-		Uint16 offset = mempeek16(u, ptr + 4);
234
-		Uint16 addr = (m[ptr + b0 - 1] << 8) | b1;
228
+	Uint8 read = b0 == 0xd;
229
+	if(read || b0 == 0xf) {
230
+		char *name = (char *)&u->ram.dat[genpeek16(m, 0x8)];
231
+		Uint16 result = 0, length = genpeek16(m, 0xa);
232
+		Uint16 offset = genpeek16(m, 0x4);
233
+		Uint16 addr = (m[b0 - 1] << 8) | b1;
235 234
 		FILE *f = fopen(name, read ? "r" : (offset ? "a" : "w"));
236 235
 		if(f) {
237 236
 			if(fseek(f, offset, SEEK_SET) != -1 && (result = read ? fread(&m[addr], 1, length, f) : fwrite(&m[addr], 1, length, f)))
238 237
 				printf("%s %d bytes, at %04x from %s\n", read ? "Loaded" : "Saved", length, addr, name);
239 238
 			fclose(f);
240 239
 		}
241
-		mempoke16(u, ptr + 2, result);
240
+		genpoke16(m, 0x2, result);
242 241
 	}
243 242
 	return b1;
244 243
 }
245 244
 
246 245
 static Uint8
247
-audio_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
246
+audio_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
248 247
 {
249
-	Uint8 *m = u->ram.dat + ptr;
250 248
 	if(b0 == 0xa) {
251 249
 		if(b1 >= apu.n_notes) apu.notes = SDL_realloc(apu.notes, (b1 + 1) * sizeof(Note));
252 250
 		while(b1 >= apu.n_notes) SDL_zero(apu.notes[apu.n_notes++]);
253
-		apu_play_note(&apu.notes[b1], (m[0x0] << 8) + m[0x1], (m[0x2] << 8) + m[0x3], m[0x8], m[0x9] & 0x7f, m[0x9] > 0x7f);
251
+		apu_play_note(&apu.notes[b1], genpeek16(m, 0x0), genpeek16(m, 0x2), m[0x8], m[0x9] & 0x7f, m[0x9] > 0x7f);
254 252
 	} else if(b0 == 0xe && apu.queue != NULL) {
255 253
 		if(apu.queue->n == apu.queue->sz) {
256 254
 			apu.queue->sz = apu.queue->sz < 4 ? 4 : apu.queue->sz * 2;
... ...
@@ -259,48 +257,50 @@ audio_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
259 257
 		if(apu.queue->is_envelope)
260 258
 			apu.queue->dat[apu.queue->n++] = (m[0xb] << 7) + (m[0xc] >> 1);
261 259
 		else
262
-			apu.queue->dat[apu.queue->n++] = (m[0xb] << 8) + m[0xc] + 0x8000;
260
+			apu.queue->dat[apu.queue->n++] = genpeek16(m, 0xb) + 0x8000;
263 261
 		apu.queue->dat[apu.queue->n++] = (m[0xd] << 8) + b1;
264 262
 	} else if(b0 == 0xf && apu.queue != NULL)
265 263
 		apu.queue->finishes = 1;
264
+	(void)u;
266 265
 	return b1;
267 266
 }
268 267
 
269 268
 Uint8
270
-midi_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
269
+midi_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
271 270
 {
272 271
 	(void)u;
273
-	printf("%04x - %02x,%02x\n", ptr, b0, b1);
272
+	(void)m;
273
+	printf("midi - %02x,%02x\n", b0, b1);
274 274
 	return b1;
275 275
 }
276 276
 
277 277
 Uint8
278
-datetime_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
278
+datetime_poke(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
279 279
 {
280
-	Uint8 *m = u->ram.dat;
281 280
 	time_t seconds = time(NULL);
282 281
 	struct tm *t = localtime(&seconds);
283 282
 	t->tm_year += 1900;
284
-	m[ptr + 0] = (t->tm_year & 0xff00) >> 8;
285
-	m[ptr + 1] = t->tm_year & 0xff;
286
-	m[ptr + 2] = t->tm_mon;
287
-	m[ptr + 3] = t->tm_mday;
288
-	m[ptr + 4] = t->tm_hour;
289
-	m[ptr + 5] = t->tm_min;
290
-	m[ptr + 6] = t->tm_sec;
291
-	m[ptr + 7] = t->tm_wday;
292
-	m[ptr + 8] = (t->tm_yday & 0xff00) >> 8;
293
-	m[ptr + 9] = t->tm_yday & 0xff;
294
-	m[ptr + 10] = t->tm_isdst;
283
+	m[0x0] = (t->tm_year & 0xff00) >> 8;
284
+	m[0x1] = t->tm_year & 0xff;
285
+	m[0x2] = t->tm_mon;
286
+	m[0x3] = t->tm_mday;
287
+	m[0x4] = t->tm_hour;
288
+	m[0x5] = t->tm_min;
289
+	m[0x6] = t->tm_sec;
290
+	m[0x7] = t->tm_wday;
291
+	m[0x8] = (t->tm_yday & 0xff00) >> 8;
292
+	m[0x9] = t->tm_yday & 0xff;
293
+	m[0xa] = t->tm_isdst;
294
+	(void)u;
295 295
 	(void)b0;
296 296
 	return b1;
297 297
 }
298 298
 
299 299
 Uint8
300
-ppnil(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
300
+ppnil(Uxn *u, Uint8 *m, Uint8 b0, Uint8 b1)
301 301
 {
302 302
 	(void)u;
303
-	(void)ptr;
303
+	(void)m;
304 304
 	(void)b0;
305 305
 	return b1;
306 306
 }
... ...
@@ -310,7 +310,7 @@ ppnil(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
310 310
 int
311 311
 start(Uxn *u)
312 312
 {
313
-	evaluxn(u, 0x0200);
313
+	evaluxn(u, 0x0100);
314 314
 	redraw(ppu.output, u);
315 315
 	while(1) {
316 316
 		SDL_Event event;
... ...
@@ -323,19 +323,19 @@ start(Uxn *u)
323 323
 				break;
324 324
 			case SDL_TEXTINPUT:
325 325
 				if(event.text.text[0] >= ' ' || event.text.text[0] <= '~')
326
-					u->ram.dat[devctrl->addr + 3] = event.text.text[0];
326
+					devctrl->dat[3] = event.text.text[0];
327 327
 				break;
328 328
 			case SDL_KEYDOWN:
329 329
 			case SDL_KEYUP:
330 330
 				doctrl(u, &event, event.type == SDL_KEYDOWN);
331
-				evaluxn(u, mempeek16(u, devctrl->addr));
332
-				u->ram.dat[devctrl->addr + 3] = 0;
331
+				evaluxn(u, genpeek16(devctrl->dat, 0));
332
+				devctrl->dat[3] = 0;
333 333
 				break;
334 334
 			case SDL_MOUSEBUTTONUP:
335 335
 			case SDL_MOUSEBUTTONDOWN:
336 336
 			case SDL_MOUSEMOTION:
337
-				domouse(u, &event);
338
-				evaluxn(u, mempeek16(u, devmouse->addr));
337
+				domouse(&event);
338
+				evaluxn(u, genpeek16(devmouse->dat, 0));
339 339
 				break;
340 340
 			case SDL_WINDOWEVENT:
341 341
 				if(event.window.event == SDL_WINDOWEVENT_EXPOSED)
... ...
@@ -343,7 +343,7 @@ start(Uxn *u)
343 343
 				break;
344 344
 			}
345 345
 		}
346
-		evaluxn(u, mempeek16(u, devscreen->addr));
346
+		evaluxn(u, genpeek16(devscreen->dat, 0));
347 347
 		SDL_UnlockAudioDevice(audio_id);
348 348
 		if(reqdraw)
349 349
 			redraw(ppu.output, u);
... ...
@@ -368,28 +368,28 @@ main(int argc, char **argv)
368 368
 	if(!init(&u))
369 369
 		return error("Init", "Failed");
370 370
 
371
-	devsystem = portuxn(&u, 0x00, "system", system_poke);
372
-	portuxn(&u, 0x01, "console", console_poke);
373
-	devscreen = portuxn(&u, 0x02, "screen", screen_poke);
374
-	devapu = portuxn(&u, 0x03, "audio", audio_poke);
375
-	devctrl = portuxn(&u, 0x04, "controller", ppnil);
376
-	portuxn(&u, 0x05, "---", ppnil);
377
-	devmouse = portuxn(&u, 0x06, "mouse", ppnil);
378
-	devfile = portuxn(&u, 0x07, "file", file_poke);
379
-	portuxn(&u, 0x08, "---", ppnil);
380
-	portuxn(&u, 0x09, "midi", ppnil);
381
-	portuxn(&u, 0x0a, "datetime", datetime_poke);
382
-	portuxn(&u, 0x0b, "---", ppnil);
383
-	portuxn(&u, 0x0c, "---", ppnil);
384
-	portuxn(&u, 0x0d, "---", ppnil);
385
-	portuxn(&u, 0x0e, "---", ppnil);
386
-	portuxn(&u, 0x0f, "---", ppnil);
387
-
388
-	apu.channel_addr = devapu->addr + 0xa;
371
+	devsystem = portuxn(&u, 0x0, "system", system_poke);
372
+	portuxn(&u, 0x1, "console", console_poke);
373
+	devscreen = portuxn(&u, 0x2, "screen", screen_poke);
374
+	devapu = portuxn(&u, 0x3, "audio", audio_poke);
375
+	devctrl = portuxn(&u, 0x4, "controller", ppnil);
376
+	portuxn(&u, 0x5, "---", ppnil);
377
+	devmouse = portuxn(&u, 0x6, "mouse", ppnil);
378
+	devfile = portuxn(&u, 0x7, "file", file_poke);
379
+	portuxn(&u, 0x8, "---", ppnil);
380
+	portuxn(&u, 0x9, "midi", ppnil);
381
+	portuxn(&u, 0xa, "datetime", datetime_poke);
382
+	portuxn(&u, 0xb, "---", ppnil);
383
+	portuxn(&u, 0xc, "---", ppnil);
384
+	portuxn(&u, 0xd, "---", ppnil);
385
+	portuxn(&u, 0xe, "---", ppnil);
386
+	portuxn(&u, 0xf, "---", ppnil);
387
+
388
+	apu.channel_ptr = &devapu->dat[0xa];
389 389
 
390 390
 	/* Write screen size to dev/screen */
391
-	mempoke16(&u, devscreen->addr + 2, ppu.hor * 8);
392
-	mempoke16(&u, devscreen->addr + 4, ppu.ver * 8);
391
+	genpoke16(devscreen->dat, 2, ppu.hor * 8);
392
+	genpoke16(devscreen->dat, 4, ppu.ver * 8);
393 393
 
394 394
 	start(&u);
395 395
 	quit();
... ...
@@ -21,11 +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(Uxn *u, Uint16 a, Uint8 b) { u->ram.dat[a] = b; }
23 23
 Uint8  mempeek8(Uxn *u, Uint16 a) { return u->ram.dat[a]; }
24
+void   devpoke8(Uxn *u, Uint8 a, Uint8 b) { Device *dev = &u->dev[a >> 4]; dev->dat[a & 0xf] = dev->poke(u, dev->dat, a & 0x0f, b); }
25
+Uint8  devpeek8(Uxn *u, Uint8 a) { return u->dev[a >> 4].dat[a & 0xf]; }
24 26
 void   push16(Stack *s, Uint16 a) { push8(s, a >> 8); push8(s, a); }
25 27
 Uint16 pop16(Stack *s) { return pop8(s) + (pop8(s) << 8); }
26 28
 Uint16 peek16(Stack *s, Uint8 a) { return peek8(s, a * 2) + (peek8(s, a * 2 + 1) << 8); }
27 29
 void   mempoke16(Uxn *u, Uint16 a, Uint16 b) { mempoke8(u, a, b >> 8); mempoke8(u, a + 1, b); }
28 30
 Uint16 mempeek16(Uxn *u, Uint16 a) { return (mempeek8(u, a) << 8) + mempeek8(u, 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); }
29 33
 /* Stack */
30 34
 void op_brk(Uxn *u) { u->ram.ptr = 0; }
31 35
 void op_nop(Uxn *u) { (void)u; }
... ...
@@ -42,8 +46,8 @@ void op_gth(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b
42 46
 void op_lth(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b < a); }
43 47
 void op_gts(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, (Sint8)b > (Sint8)a); }
44 48
 void op_lts(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, (Sint8)b < (Sint8)a); }
45
-void op_ior(Uxn *u) { Uint8 a = pop8(u->src); push8(u->src, mempeek8(u, PAGE_DEVICE + a)); }
46
-void op_iow(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); mempoke8(u, PAGE_DEVICE + a, b); u->dev[(a & 0xf0) >> 4].poke(u, PAGE_DEVICE + (a & 0xf0), a & 0x0f, b); }
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); }
47 51
 /* Memory */
48 52
 void op_pek(Uxn *u) { Uint8 a = pop8(u->src); push8(u->src, mempeek8(u, a)); }
49 53
 void op_pok(Uxn *u) { Uint8 a = pop8(u->src); Uint8 b = pop8(u->src); mempoke8(u, a, b); }
... ...
@@ -76,8 +80,8 @@ void op_gth16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->sr
76 80
 void op_lth16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, b < a); }
77 81
 void op_gts16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, (Sint16)b > (Sint16)a); }
78 82
 void op_lts16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, (Sint16)b < (Sint16)a); }
79
-void op_ior16(Uxn *u) { Uint8 a = pop8(u->src); push16(u->src, mempeek16(u, PAGE_DEVICE + a)); }
80
-void op_iow16(Uxn *u) { Uint8 a = pop8(u->src); Uint16 b = pop16(u->src); mempoke16(u, PAGE_DEVICE + a, b); u->dev[(a & 0xf0) >> 4].poke(u, PAGE_DEVICE + (a & 0xf0), (a & 0x0f)+1, b); }
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); }
81 85
 /* Memory(16-bits) */
82 86
 void op_pek16(Uxn *u) { Uint16 a = pop16(u->src); push8(u->src, mempeek8(u, a)); }
83 87
 void op_pok16(Uxn *u) { Uint16 a = pop16(u->src); Uint8 b = pop8(u->src); mempoke8(u, a, b); }
... ...
@@ -175,10 +179,10 @@ loaduxn(Uxn *u, char *filepath)
175 179
 }
176 180
 
177 181
 Device *
178
-portuxn(Uxn *u, Uint8 id, char *name, Uint8 (*pofn)(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1))
182
+portuxn(Uxn *u, Uint8 id, char *name, Uint8 (*pofn)(Uxn *u, Uint8 *devmem, Uint8 b0, Uint8 b1))
179 183
 {
180 184
 	Device *d = &u->dev[id];
181
-	d->addr = PAGE_DEVICE + id * 0x10;
185
+	d->addr = id * 0x10;
182 186
 	d->poke = pofn;
183 187
 	printf("Device added #%02x: %s, at 0x%04x \n", id, name, d->addr);
184 188
 	return d;
... ...
@@ -16,9 +16,15 @@ typedef signed char Sint8;
16 16
 typedef unsigned short Uint16;
17 17
 typedef signed short Sint16;
18 18
 
19
-#define PAGE_DEVICE 0x0100
20
-#define PAGE_PROGRAM 0x0200
21
-#define LOAD_OFFSET 0x0200
19
+#define PAGE_PROGRAM 0x0100
20
+#define LOAD_OFFSET 0x0100
21
+
22
+#define genpeek16(ptr, i) ((ptr[i] << 8) + ptr[i + 1])
23
+#define genpoke16(ptr, i, v) \
24
+	do { \
25
+		ptr[i] = v >> 8; \
26
+		ptr[i + 1] = v & 0xff; \
27
+	} while(0)
22 28
 
23 29
 typedef struct {
24 30
 	Uint8 ptr, error;
... ...
@@ -33,8 +39,8 @@ typedef struct {
33 39
 struct Uxn;
34 40
 
35 41
 typedef struct Device {
36
-	Uint16 addr;
37
-	Uint8 (*poke)(struct Uxn *, Uint16, Uint8, Uint8);
42
+	Uint8 addr, dat[16];
43
+	Uint8 (*poke)(struct Uxn *u, Uint8 *devmem, Uint8, Uint8);
38 44
 } Device;
39 45
 
40 46
 typedef struct Uxn {
... ...
@@ -49,4 +55,4 @@ int evaluxn(Uxn *u, Uint16 vec);
49 55
 void mempoke16(Uxn *u, Uint16 a, Uint16 b);
50 56
 Uint16 mempeek16(Uxn *u, Uint16 a);
51 57
 
52
-Device *portuxn(Uxn *u, Uint8 id, char *name, Uint8 (*pofn)(Uxn *, Uint16, Uint8, Uint8));
53 58
\ No newline at end of file
59
+Device *portuxn(Uxn *u, Uint8 id, char *name, Uint8 (*pofn)(Uxn *, Uint8 *, Uint8, Uint8));
54 60
\ No newline at end of file