Browse code

Working toward bidirectional talk between devices and program

neauoire authored on 24/04/2021 16:43:30
Showing 7 changed files
... ...
@@ -32,7 +32,7 @@ else
32 32
 fi
33 33
 
34 34
 echo "Assembling.."
35
-./bin/assembler projects/demos/bifurcan.usm bin/boot.rom
35
+./bin/assembler projects/examples/devices/console.lib.usm bin/boot.rom
36 36
 
37 37
 echo "Running.."
38 38
 if [ "${2}" = '--cli' ]; 
39 39
new file mode 100644
... ...
@@ -0,0 +1,86 @@
1
+( dev/console )
2
+
3
+%RTN { JMP2r }
4
+%PRINT { .Console/string DEO2 }
5
+%BR { #0a .Console/char DEO }
6
+
7
+( devices )
8
+
9
+|10 @Console    [ &pad $8 &char $1 &byte $1 &short $2 &string $2 ]
10
+
11
+( variables )
12
+
13
+|0000
14
+
15
+@number [ &started $1 ]
16
+
17
+( init )
18
+
19
+|0100 ( -> )
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
25
+
26
+	;hello-word ;print JSR2
27
+	#ffff ;print-hexadecimal JSR2
28
+	;is-word ;print JSR2
29
+	#ffff ;print-decimal JSR2
30
+	
31
+BRK
32
+
33
+@print ( addr -- )
34
+	
35
+	&loop
36
+		( send ) DUP2 GET .Console/char DEO
37
+		( incr ) #0001 ADD2
38
+		( loop ) DUP2 GET ,&loop JNZ
39
+	POP2
40
+
41
+RTN
42
+
43
+@print-hexadecimal ( short -- )
44
+	LIT '0 .Console/char DEO
45
+	LIT 'x .Console/char DEO
46
+	DUP2 #000c SFT2 ,&digit JSR
47
+	DUP2 #0008 SFT2 ,&digit JSR
48
+	DUP2 #0004 SFT2 ,&digit JSR
49
+	                ,&digit JSR
50
+RTN
51
+
52
+	&digit
53
+	#0f AND DUP #0a LTH ,&not-alpha JNZ
54
+		#27 ADD
55
+	&not-alpha
56
+	LIT '0 ADD .Console/char DEO
57
+	POP
58
+RTN
59
+
60
+@print-decimal ( short -- )
61
+	#00 .number/started POK
62
+	DUP2 #2710 DIV2 DUP2 ,&digit JSR #2710 MUL2 SUB2
63
+	DUP2 #03e8 DIV2 DUP2 ,&digit JSR #03e8 MUL2 SUB2
64
+	DUP2 #0064 DIV2 DUP2 ,&digit JSR #0064 MUL2 SUB2
65
+	DUP2 #000a DIV2 DUP2 ,&digit JSR #000a MUL2 SUB2
66
+	                     ,&digit JSR
67
+	.number/started PEK ,&end JNZ
68
+	LIT '0 .Console/char DEO
69
+	&end
70
+RTN
71
+
72
+	&digit
73
+	SWP POP
74
+	DUP .number/started PEK ORA #02 JNZ
75
+	POP JMP2r
76
+	LIT '0 ADD .Console/char DEO
77
+	#01 .number/started POK
78
+RTN
79
+
80
+@char-txt "char: 20 $1
81
+@byte-txt "byte: 20 $1
82
+@short-txt "short: 20 $1
83
+@string-txt "string: 20 $1
84
+
85
+@hello-word "hello 20 "World! 0a 00
86
+@is-word 20 "is 20 00
... ...
@@ -6,20 +6,11 @@
6 6
 
7 7
 |10 @Console    [ &pad $8 &char $1 ]
8 8
 
9
-( variables )
10
-
11
-|0000
12
-
13
-@number [ &started $1 ]
14
-
15 9
 ( init )
16 10
 
17 11
 |0100 ( -> )
18 12
 	
19 13
 	;hello-word ;print JSR2
20
-	#ffff ;print-hexadecimal JSR2
21
-	;is-word ;print JSR2
22
-	#ffff ;print-decimal JSR2
23 14
 	
24 15
 BRK
25 16
 
... ...
@@ -28,48 +19,9 @@ BRK
28 19
 	&loop
29 20
 		( send ) DUP2 GET .Console/char DEO
30 21
 		( incr ) #0001 ADD2
31
-		( loop ) DUP2 GET ,&loop JNZ
22
+		( loop ) DUP2 GET #00 NEQ ,&loop JNZ
32 23
 	POP2
33 24
 
34 25
 RTN
35 26
 
36
-@print-hexadecimal ( short -- )
37
-	LIT '0 .Console/char DEO
38
-	LIT 'x .Console/char DEO
39
-	DUP2 #000c SFT2 ,&digit JSR
40
-	DUP2 #0008 SFT2 ,&digit JSR
41
-	DUP2 #0004 SFT2 ,&digit JSR
42
-	                ,&digit JSR
43
-RTN
44
-
45
-	&digit
46
-	#0f AND DUP #0a LTH ,&not-alpha JNZ
47
-		#27 ADD
48
-	&not-alpha
49
-	LIT '0 ADD .Console/char DEO
50
-	POP
51
-RTN
52
-
53
-@print-decimal ( short -- )
54
-	#00 .number/started POK
55
-	DUP2 #2710 DIV2 DUP2 ,&digit JSR #2710 MUL2 SUB2
56
-	DUP2 #03e8 DIV2 DUP2 ,&digit JSR #03e8 MUL2 SUB2
57
-	DUP2 #0064 DIV2 DUP2 ,&digit JSR #0064 MUL2 SUB2
58
-	DUP2 #000a DIV2 DUP2 ,&digit JSR #000a MUL2 SUB2
59
-	                     ,&digit JSR
60
-	.number/started PEK ,&end JNZ
61
-	LIT '0 .Console/char DEO
62
-	&end
63
-RTN
64
-
65
-	&digit
66
-	SWP POP
67
-	DUP .number/started PEK ORA #02 JNZ
68
-	POP JMP2r
69
-	LIT '0 ADD .Console/char DEO
70
-	#01 .number/started POK
71
-RTN
72
-
73
-@hello-word "hello 20 "World! 0a 00
74
-@is-word 20 "is 20 00
75
-
27
+@hello-word "hello 20 "World!
76 28
\ No newline at end of file
... ...
@@ -38,7 +38,7 @@ printstack(Stack *s)
38 38
 #pragma mark - Devices
39 39
 
40 40
 void
41
-console_poke(Device *d, Uint8 b0, Uint8 b1)
41
+console_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw)
42 42
 {
43 43
 	switch(b0) {
44 44
 	case 0x08: printf("%c", b1); break;
... ...
@@ -51,7 +51,7 @@ console_poke(Device *d, Uint8 b0, Uint8 b1)
51 51
 }
52 52
 
53 53
 void
54
-file_poke(Device *d, Uint8 b0, Uint8 b1)
54
+file_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw)
55 55
 {
56 56
 	Uint8 read = b0 == 0xd;
57 57
 	if(read || b0 == 0xf) {
... ...
@@ -71,7 +71,7 @@ file_poke(Device *d, Uint8 b0, Uint8 b1)
71 71
 }
72 72
 
73 73
 void
74
-ppnil(Device *d, Uint8 b0, Uint8 b1)
74
+nil_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw)
75 75
 {
76 76
 	(void)d;
77 77
 	(void)b0;
... ...
@@ -106,13 +106,13 @@ main(int argc, char **argv)
106 106
 	if(!loaduxn(&u, argv[1]))
107 107
 		return error("Load", "Failed");
108 108
 
109
-	portuxn(&u, 0x00, "console", console_poke);
110
-	portuxn(&u, 0x01, "empty", ppnil);
111
-	portuxn(&u, 0x02, "empty", ppnil);
112
-	portuxn(&u, 0x03, "empty", ppnil);
113
-	portuxn(&u, 0x04, "empty", ppnil);
114
-	portuxn(&u, 0x05, "empty", ppnil);
115
-	portuxn(&u, 0x06, "file", file_poke);
109
+	portuxn(&u, 0x00, "console", console_talk);
110
+	portuxn(&u, 0x01, "empty", nil_talk);
111
+	portuxn(&u, 0x02, "empty", nil_talk);
112
+	portuxn(&u, 0x03, "empty", nil_talk);
113
+	portuxn(&u, 0x04, "empty", nil_talk);
114
+	portuxn(&u, 0x05, "empty", nil_talk);
115
+	portuxn(&u, 0x06, "file", file_talk);
116 116
 	start(&u);
117 117
 
118 118
 	return 0;
... ...
@@ -182,7 +182,7 @@ doctrl(Uxn *u, SDL_Event *event, int z)
182 182
 #pragma mark - Devices
183 183
 
184 184
 void
185
-system_poke(Device *d, Uint8 b0, Uint8 b1)
185
+system_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw)
186 186
 {
187 187
 	putcolors(&ppu, &d->dat[0x8]);
188 188
 	reqdraw = 1;
... ...
@@ -191,19 +191,19 @@ system_poke(Device *d, Uint8 b0, Uint8 b1)
191 191
 }
192 192
 
193 193
 void
194
-console_poke(Device *d, Uint8 b0, Uint8 b1)
194
+console_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw)
195 195
 {
196 196
 	switch(b0) {
197
-	case 0x8: printf("%c", b1); break;
198
-	case 0x9: printf("0x%02x\n", b1); break;
199
-	case 0xb: printf("0x%04x\n", (d->dat[0xa] << 8) + b1); break;
200
-	case 0xd: printf("%s\n", &d->mem[(d->dat[0xc] << 8) + b1]); break;
197
+	case 0x8: printf("%c", d->dat[0x8]); break;
198
+	case 0x9: printf("0x%02x", d->dat[0x9]); break;
199
+	case 0xb: printf("0x%04x", mempeek16(d->dat, 0xa)); break;
200
+	case 0xd: printf("%s", &d->mem[mempeek16(d->dat, 0xc)]); break;
201 201
 	}
202 202
 	fflush(stdout);
203 203
 }
204 204
 
205 205
 void
206
-screen_poke(Device *d, Uint8 b0, Uint8 b1)
206
+screen_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw)
207 207
 {
208 208
 	if(b0 == 0xe) {
209 209
 		Uint16 x = mempeek16(d->dat, 0x8);
... ...
@@ -220,7 +220,7 @@ screen_poke(Device *d, Uint8 b0, Uint8 b1)
220 220
 }
221 221
 
222 222
 void
223
-file_poke(Device *d, Uint8 b0, Uint8 b1)
223
+file_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw)
224 224
 {
225 225
 	Uint8 read = b0 == 0xd;
226 226
 	if(read || b0 == 0xf) {
... ...
@@ -240,7 +240,7 @@ file_poke(Device *d, Uint8 b0, Uint8 b1)
240 240
 }
241 241
 
242 242
 static void
243
-audio_poke(Device *d, Uint8 b0, Uint8 b1)
243
+audio_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw)
244 244
 {
245 245
 	if(b0 == 0xa) {
246 246
 		if(b1 >= apu.n_notes) apu.notes = SDL_realloc(apu.notes, (b1 + 1) * sizeof(Note));
... ...
@@ -261,7 +261,7 @@ audio_poke(Device *d, Uint8 b0, Uint8 b1)
261 261
 }
262 262
 
263 263
 void
264
-datetime_poke(Device *d, Uint8 b0, Uint8 b1)
264
+datetime_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw)
265 265
 {
266 266
 	time_t seconds = time(NULL);
267 267
 	struct tm *t = localtime(&seconds);
... ...
@@ -280,7 +280,7 @@ datetime_poke(Device *d, Uint8 b0, Uint8 b1)
280 280
 }
281 281
 
282 282
 void
283
-ppnil(Device *d, Uint8 b0, Uint8 b1)
283
+nil_talk(Device *d, Uint8 b0, Uint8 b1, Uint8 rw)
284 284
 {
285 285
 	(void)d;
286 286
 	(void)b0;
... ...
@@ -350,22 +350,22 @@ main(int argc, char **argv)
350 350
 	if(!init(&u))
351 351
 		return error("Init", "Failed");
352 352
 
353
-	devsystem = portuxn(&u, 0x0, "system", system_poke);
354
-	portuxn(&u, 0x1, "console", console_poke);
355
-	devscreen = portuxn(&u, 0x2, "screen", screen_poke);
356
-	devapu = portuxn(&u, 0x3, "audio", audio_poke);
357
-	devctrl = portuxn(&u, 0x4, "controller", ppnil);
358
-	portuxn(&u, 0x5, "---", ppnil);
359
-	devmouse = portuxn(&u, 0x6, "mouse", ppnil);
360
-	devfile = portuxn(&u, 0x7, "file", file_poke);
361
-	portuxn(&u, 0x8, "---", ppnil);
362
-	portuxn(&u, 0x9, "midi", ppnil);
363
-	portuxn(&u, 0xa, "datetime", datetime_poke);
364
-	portuxn(&u, 0xb, "---", ppnil);
365
-	portuxn(&u, 0xc, "---", ppnil);
366
-	portuxn(&u, 0xd, "---", ppnil);
367
-	portuxn(&u, 0xe, "---", ppnil);
368
-	portuxn(&u, 0xf, "---", ppnil);
353
+	devsystem = portuxn(&u, 0x0, "system", system_talk);
354
+	portuxn(&u, 0x1, "console", console_talk);
355
+	devscreen = portuxn(&u, 0x2, "screen", screen_talk);
356
+	devapu = portuxn(&u, 0x3, "audio", audio_talk);
357
+	devctrl = portuxn(&u, 0x4, "controller", nil_talk);
358
+	portuxn(&u, 0x5, "---", nil_talk);
359
+	devmouse = portuxn(&u, 0x6, "mouse", nil_talk);
360
+	devfile = portuxn(&u, 0x7, "file", file_talk);
361
+	portuxn(&u, 0x8, "---", nil_talk);
362
+	portuxn(&u, 0x9, "midi", nil_talk);
363
+	portuxn(&u, 0xa, "datetime", datetime_talk);
364
+	portuxn(&u, 0xb, "---", nil_talk);
365
+	portuxn(&u, 0xc, "---", nil_talk);
366
+	portuxn(&u, 0xd, "---", nil_talk);
367
+	portuxn(&u, 0xe, "---", nil_talk);
368
+	portuxn(&u, 0xf, "---", nil_talk);
369 369
 
370 370
 	apu.channel_ptr = &devapu->dat[0xa];
371 371
 
... ...
@@ -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->poke(d, a & 0x0f, b); }
25
-Uint8  devpeek8(Device *d, Uint8 a) { return d->dat[a & 0xf]; }
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); }
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,12 +179,12 @@ loaduxn(Uxn *u, char *filepath)
179 179
 }
180 180
 
181 181
 Device *
182
-portuxn(Uxn *u, Uint8 id, char *name, void (*pofn)(Device *d, Uint8 b0, Uint8 b1))
182
+portuxn(Uxn *u, Uint8 id, char *name, void (*talkfn)(Device *d, Uint8 b0, Uint8 b1, Uint8 rw))
183 183
 {
184 184
 	Device *d = &u->dev[id];
185 185
 	d->addr = id * 0x10;
186 186
 	d->mem = u->ram.dat;
187
-	d->poke = pofn;
187
+	d->talk = talkfn;
188 188
 	printf("Device added #%02x: %s, at 0x%04x \n", id, name, d->addr);
189 189
 	return d;
190 190
 }
... ...
@@ -32,7 +32,7 @@ struct Uxn;
32 32
 
33 33
 typedef struct Device {
34 34
 	Uint8 addr, dat[16], *mem;
35
-	void (*poke)(struct Device *d, Uint8, Uint8);
35
+	void (*talk)(struct Device *d, Uint8, 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 (*pofn)(Device *, Uint8, Uint8));
51 50
\ No newline at end of file
51
+Device *portuxn(Uxn *u, Uint8 id, char *name, void (*talkfn)(Device *, Uint8, Uint8, Uint8));
52 52
\ No newline at end of file