Browse code

Device events can now fire on read

neauoire authored on 24/04/2021 17:15:47
Showing 7 changed files
... ...
@@ -32,7 +32,7 @@ else
32 32
 fi
33 33
 
34 34
 echo "Assembling.."
35
-./bin/assembler projects/examples/devices/console.lib.usm bin/boot.rom
35
+./bin/assembler projects/examples/devices/screen.usm bin/boot.rom
36 36
 
37 37
 echo "Running.."
38 38
 if [ "${2}" = '--cli' ]; 
... ...
@@ -18,10 +18,10 @@
18 18
 
19 19
 |0100 ( -> )
20 20
 		
21
-	[ ;char-txt PRINT   ] #42 .Console/char DEO BR
22
-	[ ;byte-txt PRINT   ] #ab .Console/byte DEO BR
23
-	[ ;short-txt PRINT  ] #cdef .Console/short DEO2 BR
24
-	[ ;string-txt PRINT ] ;hello-word .Console/string DEO2 BR
21
+	;char-txt PRINT #42 .Console/char DEO BR
22
+	;byte-txt PRINT #ab .Console/byte DEO BR
23
+	;short-txt PRINT #cdef .Console/short DEO2 BR
24
+	;string-txt PRINT ;hello-word .Console/string DEO2 BR
25 25
 
26 26
 	;hello-word ;print JSR2
27 27
 	#ffff ;print-hexadecimal JSR2
28 28
new file mode 100644
... ...
@@ -0,0 +1,112 @@
1
+( dev/screen )
2
+
3
+%RTN { JMP2r }
4
+
5
+( devices )
6
+
7
+|00 @System     [ &vector $2 &pad      $6 &r      $2 &g     $2 &b      $2 ]
8
+|20 @Screen     [ &vector $2 &width    $2 &height $2 &pad   $2 &x      $2 &y     $2 &addr $2 &color $1 ]
9
+
10
+( variables )
11
+
12
+|0000
13
+
14
+@center [ &x $2 &y $2 ]
15
+
16
+( init )
17
+
18
+|0100 ( -> )
19
+	
20
+	( theme ) 
21
+	#530f .System/r DEO2
22
+	#f20f .System/g DEO2 
23
+	#ad0f .System/b DEO2 
24
+
25
+	( find screen center )
26
+	.Screen/width DEI2 #0002 DIV2 .center/x POK2
27
+	.Screen/height DEI2 #0002 DIV2 .center/y POK2
28
+
29
+	( draw )
30
+	;draw-background JSR2
31
+	;draw-sprites JSR2
32
+	
33
+BRK
34
+
35
+@draw-background ( -- )
36
+	
37
+	( draw hor line )
38
+	#0000 .Screen/x DEO2 .center/y PEK2 .Screen/y DEO2
39
+	#0000 .Screen/width DEI2 ( from/to )
40
+	&draw-hor
41
+		( draw ) #01 .Screen/color DEO
42
+		( incr ) SWP2 #0002 ADD2 DUP2 .Screen/x DEO2 SWP2
43
+		OVR2 OVR2 LTH2 ,&draw-hor JNZ
44
+	POP2 POP2
45
+
46
+	( draw ver line )
47
+	.center/x PEK2 .Screen/x DEO2 #0000 .Screen/y DEO2
48
+	#0000 .Screen/height DEI2 ( from/to )
49
+	&draw-ver
50
+		( draw ) #02 .Screen/color DEO
51
+		( incr ) SWP2 #0002 ADD2 DUP2 .Screen/y DEO2 SWP2
52
+		OVR2 OVR2 LTH2 ,&draw-ver JNZ
53
+	POP2 POP2
54
+
55
+RTN
56
+
57
+@draw-sprites ( -- )
58
+	
59
+	( draw blending modes )
60
+	;preview_icn .Screen/addr DEO2
61
+	#0010 .Screen/y DEO2
62
+	#00 #08
63
+	&draw-pixel1
64
+		( move ) OVR #00 SWP #0008 MUL2 #0010 ADD2 .Screen/x DEO2
65
+		( draw ) OVR .Screen/color DEO
66
+		( incr ) SWP #01 ADD SWP
67
+		DUP2 LTH ,&draw-pixel1 JNZ
68
+	POP POP
69
+	#0018 .Screen/y DEO2
70
+	#00 #08
71
+	&draw-pixel2
72
+		( move ) OVR #00 SWP #0008 MUL2 #0010 ADD2 .Screen/x DEO2
73
+		( draw ) OVR #08 ADD .Screen/color DEO
74
+		( incr ) SWP #01 ADD SWP
75
+		DUP2 LTH ,&draw-pixel2 JNZ
76
+	POP POP
77
+	#0020 .Screen/y DEO2
78
+	#00 #08
79
+	&draw-icn1
80
+		( move ) OVR #00 SWP #0008 MUL2 #0010 ADD2 .Screen/x DEO2
81
+		( draw ) OVR #20 ADD .Screen/color DEO
82
+		( incr ) SWP #01 ADD SWP
83
+		DUP2 LTH ,&draw-icn1 JNZ
84
+	POP POP
85
+	#0028 .Screen/y DEO2
86
+	#00 #08
87
+	&draw-icn2
88
+		( move ) OVR #00 SWP #0008 MUL2 #0010 ADD2 .Screen/x DEO2
89
+		( draw ) OVR #28 ADD .Screen/color DEO
90
+		( incr ) SWP #01 ADD SWP
91
+		DUP2 LTH ,&draw-icn2 JNZ
92
+	POP POP
93
+	#0030 .Screen/y DEO2
94
+	#00 #08
95
+	&draw-chr1
96
+		( move ) OVR #00 SWP #0008 MUL2 #0010 ADD2 .Screen/x DEO2
97
+		( draw ) OVR #40 ADD .Screen/color DEO
98
+		( incr ) SWP #01 ADD SWP
99
+		DUP2 LTH ,&draw-chr1 JNZ
100
+	POP POP
101
+	#0038 .Screen/y DEO2
102
+	#00 #08
103
+	&draw-chr2
104
+		( move ) OVR #00 SWP #0008 MUL2 #0010 ADD2 .Screen/x DEO2
105
+		( draw ) OVR #48 ADD .Screen/color DEO
106
+		( incr ) SWP #01 ADD SWP
107
+		DUP2 LTH ,&draw-chr2 JNZ
108
+	POP POP
109
+
110
+RTN
111
+
112
+@preview_icn  [ 183c 66db db66 3c18 0000 183c 3c18 0000 ]
0 113
\ No newline at end of file
... ...
@@ -38,23 +38,23 @@ printstack(Stack *s)
38 38
 #pragma mark - Devices
39 39
 
40 40
 void
41
-console_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw)
41
+console_talk(Device *d, Uint8 b0, Uint8 w)
42 42
 {
43
+	if(!w) return;
43 44
 	switch(b0) {
44
-	case 0x08: printf("%c", b1); break;
45
-	case 0x09: printf("0x%02x\n", b1); break;
46
-	case 0x0b: printf("0x%04x\n", (d->dat[0x0a] << 8) + b1); break;
45
+	case 0x8: printf("%c", d->dat[0x8]); break;
46
+	case 0x9: printf("0x%02x", d->dat[0x9]); break;
47
+	case 0xb: printf("0x%04x", mempeek16(d->dat, 0xa)); break;
48
+	case 0xd: printf("%s", &d->mem[mempeek16(d->dat, 0xc)]); break;
47 49
 	}
48 50
 	fflush(stdout);
49
-	(void)d;
50
-	(void)b0;
51 51
 }
52 52
 
53 53
 void
54
-file_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw)
54
+file_talk(Device *d, Uint8 b0, Uint8 w)
55 55
 {
56 56
 	Uint8 read = b0 == 0xd;
57
-	if(read || b0 == 0xf) {
57
+	if(w && (read || b0 == 0xf)) {
58 58
 		char *name = (char *)&d->mem[mempeek16(d->dat, 0x8)];
59 59
 		Uint16 result = 0, length = mempeek16(d->dat, 0xa);
60 60
 		Uint16 offset = mempeek16(d->dat, 0x4);
... ...
@@ -67,15 +67,14 @@ file_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw)
67 67
 		}
68 68
 		mempoke16(d->dat, 0x2, result);
69 69
 	}
70
-	(void)b1;
71 70
 }
72 71
 
73 72
 void
74
-nil_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw)
73
+nil_talk(Device *d, Uint8 b0, Uint8 w)
75 74
 {
76 75
 	(void)d;
77 76
 	(void)b0;
78
-	(void)b1;
77
+	(void)w;
79 78
 }
80 79
 
81 80
 #pragma mark - Generics
... ...
@@ -182,17 +182,18 @@ doctrl(Uxn *u, SDL_Event *event, int z)
182 182
 #pragma mark - Devices
183 183
 
184 184
 void
185
-system_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw)
185
+system_talk(Device *d, Uint8 b0, Uint8 w)
186 186
 {
187
+	if(!w) return;
187 188
 	putcolors(&ppu, &d->dat[0x8]);
188 189
 	reqdraw = 1;
189 190
 	(void)b0;
190
-	(void)b1;
191 191
 }
192 192
 
193 193
 void
194
-console_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw)
194
+console_talk(Device *d, Uint8 b0, Uint8 w)
195 195
 {
196
+	if(!w) return;
196 197
 	switch(b0) {
197 198
 	case 0x8: printf("%c", d->dat[0x8]); break;
198 199
 	case 0x9: printf("0x%02x", d->dat[0x9]); break;
... ...
@@ -203,27 +204,27 @@ console_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw)
203 204
 }
204 205
 
205 206
 void
206
-screen_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw)
207
+screen_talk(Device *d, Uint8 b0, Uint8 w)
207 208
 {
208
-	if(b0 == 0xe) {
209
+	if(w && b0 == 0xe) {
209 210
 		Uint16 x = mempeek16(d->dat, 0x8);
210 211
 		Uint16 y = mempeek16(d->dat, 0xa);
211 212
 		Uint8 *addr = &d->mem[mempeek16(d->dat, 0xc)];
212
-		Uint8 *layer = b1 >> 4 & 0x1 ? ppu.fg : ppu.bg;
213
-		switch(b1 >> 5) {
214
-		case 0: putpixel(&ppu, layer, x, y, b1 & 0x3); break;
215
-		case 1: puticn(&ppu, layer, x, y, addr, b1 & 0xf); break;
216
-		case 2: putchr(&ppu, layer, x, y, addr, b1 & 0xf); break;
213
+		Uint8 *layer = d->dat[0xe] >> 4 & 0x1 ? ppu.fg : ppu.bg;
214
+		switch(d->dat[0xe] >> 5) {
215
+		case 0: putpixel(&ppu, layer, x, y, d->dat[0xe] & 0x3); break;
216
+		case 1: puticn(&ppu, layer, x, y, addr, d->dat[0xe] & 0xf); break;
217
+		case 2: putchr(&ppu, layer, x, y, addr, d->dat[0xe] & 0xf); break;
217 218
 		}
218 219
 		reqdraw = 1;
219 220
 	}
220 221
 }
221 222
 
222 223
 void
223
-file_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw)
224
+file_talk(Device *d, Uint8 b0, Uint8 w)
224 225
 {
225 226
 	Uint8 read = b0 == 0xd;
226
-	if(read || b0 == 0xf) {
227
+	if(w && (read || b0 == 0xf)) {
227 228
 		char *name = (char *)&d->mem[mempeek16(d->dat, 0x8)];
228 229
 		Uint16 result = 0, length = mempeek16(d->dat, 0xa);
229 230
 		Uint16 offset = mempeek16(d->dat, 0x4);
... ...
@@ -236,17 +237,16 @@ file_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw)
236 237
 		}
237 238
 		mempoke16(d->dat, 0x2, result);
238 239
 	}
239
-	(void)b1;
240 240
 }
241 241
 
242 242
 static void
243
-audio_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw)
243
+audio_talk(Device *d, Uint8 b0, Uint8 w)
244 244
 {
245
-	if(b0 == 0xa) {
246
-		if(b1 >= apu.n_notes) apu.notes = SDL_realloc(apu.notes, (b1 + 1) * sizeof(Note));
247
-		while(b1 >= apu.n_notes) SDL_zero(apu.notes[apu.n_notes++]);
248
-		apu_play_note(&apu.notes[b1], mempeek16(d->dat, 0x0), mempeek16(d->dat, 0x2), d->dat[0x8], d->dat[0x9] & 0x7f, d->dat[0x9] > 0x7f);
249
-	} else if(b0 == 0xe && apu.queue != NULL) {
245
+	if(w && b0 == 0xa) {
246
+		if(d->dat[0xa] >= apu.n_notes) apu.notes = SDL_realloc(apu.notes, (d->dat[0xa] + 1) * sizeof(Note));
247
+		while(d->dat[0xa] >= apu.n_notes) SDL_zero(apu.notes[apu.n_notes++]);
248
+		apu_play_note(&apu.notes[d->dat[0xa]], mempeek16(d->dat, 0x0), mempeek16(d->dat, 0x2), d->dat[0x8], d->dat[0x9] & 0x7f, d->dat[0x9] > 0x7f);
249
+	} else if(w && b0 == 0xe && apu.queue != NULL) {
250 250
 		if(apu.queue->n == apu.queue->sz) {
251 251
 			apu.queue->sz = apu.queue->sz < 4 ? 4 : apu.queue->sz * 2;
252 252
 			apu.queue->dat = SDL_realloc(apu.queue->dat, apu.queue->sz * sizeof(*apu.queue->dat));
... ...
@@ -256,12 +256,12 @@ audio_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw)
256 256
 		else
257 257
 			apu.queue->dat[apu.queue->n++] = mempeek16(d->dat, 0xb) + 0x8000;
258 258
 		apu.queue->dat[apu.queue->n++] = mempeek16(d->dat, 0xd);
259
-	} else if(b0 == 0xf && apu.queue != NULL)
259
+	} else if(w && b0 == 0xf && apu.queue != NULL)
260 260
 		apu.queue->finishes = 1;
261 261
 }
262 262
 
263 263
 void
264
-datetime_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw)
264
+datetime_talk(Device *d, Uint8 b0, Uint8 w)
265 265
 {
266 266
 	time_t seconds = time(NULL);
267 267
 	struct tm *t = localtime(&seconds);
... ...
@@ -276,15 +276,15 @@ datetime_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw)
276 276
 	mempoke16(d->dat, 0x08, t->tm_yday);
277 277
 	d->dat[0xa] = t->tm_isdst;
278 278
 	(void)b0;
279
-	(void)b1;
279
+	(void)w;
280 280
 }
281 281
 
282 282
 void
283
-nil_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw)
283
+nil_talk(Device *d, Uint8 b0, Uint8 w)
284 284
 {
285 285
 	(void)d;
286 286
 	(void)b0;
287
-	(void)b1;
287
+	(void)w;
288 288
 }
289 289
 
290 290
 #pragma mark - Generics
... ...
@@ -21,8 +21,8 @@ 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(Device *d, Uint8 a, Uint8 b) { d->dat[a & 0xf] = b; d->talk(d, a & 0x0f, b, 1); }
25
-Uint8  devpeek8(Device *d, Uint8 a) { return d->dat[a & 0xf]; d->talk(d, a & 0x0f, 0, 0); }
24
+void   devpoke8(Device *d, Uint8 a, Uint8 b) { d->dat[a & 0xf] = b; d->talk(d, a & 0x0f, 1); }
25
+Uint8  devpeek8(Device *d, Uint8 a) { return d->dat[a & 0xf]; d->talk(d, a & 0x0f, 0); }
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); }
... ...
@@ -179,7 +179,7 @@ loaduxn(Uxn *u, char *filepath)
179 179
 }
180 180
 
181 181
 Device *
182
-portuxn(Uxn *u, Uint8 id, char *name, void (*talkfn)(Device *d, Uint8 b0, Uint8 b1, Uint8 rw))
182
+portuxn(Uxn *u, Uint8 id, char *name, void (*talkfn)(Device *d, Uint8 b0, Uint8 w))
183 183
 {
184 184
 	Device *d = &u->dev[id];
185 185
 	d->addr = id * 0x10;
... ...
@@ -32,7 +32,7 @@ struct Uxn;
32 32
 
33 33
 typedef struct Device {
34 34
 	Uint8 addr, dat[16], *mem;
35
-	void (*talk)(struct Device *d, Uint8, Uint8, Uint8);
35
+	void (*talk)(struct Device *d, 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, void (*talkfn)(Device *, Uint8, Uint8, Uint8));
51 50
\ No newline at end of file
51
+Device *portuxn(Uxn *u, Uint8 id, char *name, void (*talkfn)(Device *, Uint8, Uint8));
52 52
\ No newline at end of file