Browse code

Added Console/vector implementation with example

Andrew Alderwick authored on 27/06/2021 20:45:50
Showing 4 changed files
... ...
@@ -10,8 +10,8 @@
10 10
 
11 11
 ( devices )
12 12
 
13
-|00 @System     [ &vector $2 &wst      $1 &rst    $1 &pad   $4 &r      $2 &g     $2 &b      $2 ]
14
-|10 @Console    [ &pad    $8 &char     $1 &byte   $1 &short $2 &string $2 ]
13
+|00 @System     [ &vector $2 &wst      $1 &rst    $1 &pad   $4 &r      $2 &g      $2 &b     $2 ]
14
+|10 @Console    [ &vector $2 &pad      $6 &char   $1 &byte  $1 &short  $2 &string $2 ]
15 15
 |20 @Screen     [ &vector $2 &width    $2 &height $2 &pad   $2 &x      $2 &y      $2 &addr  $2 &color $1 ]
16 16
 |30 @Audio0     [ &vector $2 &position $2 &output $1 &pad   $3 &adsr   $2 &length $2 &addr  $2 &volume $1 &pitch $1 ]
17 17
 |40 @Audio1     [ &vector $2 &position $2 &output $1 &pad   $3 &adsr   $2 &length $2 &addr  $2 &volume $1 &pitch $1 ]
18 18
new file mode 100644
... ...
@@ -0,0 +1,14 @@
1
+( dev/console )
2
+
3
+|10 @Console [ &vector $2 &pad $6 &char $1 &byte $1 &short $2 &string $2 ]
4
+
5
+( init )
6
+
7
+|0100 ( -> )
8
+	;on-stdin .Console/vector DEO2
9
+BRK
10
+
11
+@on-stdin ( -> )
12
+	.Console/char DEI .Console/byte DEO
13
+	#0a .Console/char DEO
14
+BRK
0 15
\ No newline at end of file
... ...
@@ -1,5 +1,7 @@
1 1
 #include <stdio.h>
2 2
 #include <time.h>
3
+#include <unistd.h>
4
+#include <string.h>
3 5
 #include "uxn.h"
4 6
 
5 7
 /*
... ...
@@ -15,6 +17,8 @@ WITH REGARD TO THIS SOFTWARE.
15 17
 
16 18
 #pragma mark - Core
17 19
 
20
+static Device *devconsole;
21
+
18 22
 int
19 23
 error(char *msg, const char *err)
20 24
 {
... ...
@@ -41,14 +45,16 @@ printstack(Stack *s)
41 45
 void
42 46
 console_talk(Device *d, Uint8 b0, Uint8 w)
43 47
 {
48
+	char buffer[7], *p = buffer;
49
+	int len = 0;
44 50
 	if(!w) return;
45 51
 	switch(b0) {
46
-	case 0x8: printf("%c", d->dat[0x8]); break;
47
-	case 0x9: printf("0x%02x", d->dat[0x9]); break;
48
-	case 0xb: printf("0x%04x", mempeek16(d->dat, 0xa)); break;
49
-	case 0xd: printf("%s", &d->mem[mempeek16(d->dat, 0xc)]); break;
52
+	case 0x8: len = 1, p = (char *)&d->dat[0x8]; break;
53
+	case 0x9: len = sprintf(p, "0x%02x", d->dat[0x9]); break;
54
+	case 0xb: len = sprintf(p, "0x%04x", mempeek16(d->dat, 0xa)); break;
55
+	case 0xd: len = strlen(p = (char *)&d->mem[mempeek16(d->dat, 0xc)]); break;
50 56
 	}
51
-	fflush(stdout);
57
+	if(len) write(1, p, len);
52 58
 }
53 59
 
54 60
 void
... ...
@@ -106,6 +112,9 @@ start(Uxn *u)
106 112
 {
107 113
 	if(!evaluxn(u, PAGE_PROGRAM))
108 114
 		return error("Reset", "Failed");
115
+	while(mempeek16(devconsole->dat, 0))
116
+		while(read(0, &devconsole->dat[0x8], 1) > 0)
117
+			evaluxn(u, mempeek16(devconsole->dat, 0));
109 118
 	return 1;
110 119
 }
111 120
 
... ...
@@ -122,7 +131,7 @@ main(int argc, char **argv)
122 131
 		return error("Load", "Failed");
123 132
 
124 133
 	portuxn(&u, 0x0, "empty", nil_talk);
125
-	portuxn(&u, 0x1, "console", console_talk);
134
+	devconsole = portuxn(&u, 0x1, "console", console_talk);
126 135
 	portuxn(&u, 0x2, "empty", nil_talk);
127 136
 	portuxn(&u, 0x3, "empty", nil_talk);
128 137
 	portuxn(&u, 0x4, "empty", nil_talk);
... ...
@@ -1,6 +1,7 @@
1 1
 #include <stdio.h>
2 2
 #include <unistd.h>
3 3
 #include <time.h>
4
+#include <fcntl.h>
4 5
 #include "uxn.h"
5 6
 
6 7
 #pragma GCC diagnostic push
... ...
@@ -28,7 +29,7 @@ static SDL_Texture *fgTexture, *bgTexture;
28 29
 static SDL_Rect gRect;
29 30
 static Ppu ppu;
30 31
 static Apu apu[POLYPHONY];
31
-static Device *devscreen, *devmouse, *devctrl, *devaudio0;
32
+static Device *devscreen, *devmouse, *devctrl, *devaudio0, *devconsole;
32 33
 
33 34
 #define PAD 16
34 35
 
... ...
@@ -234,14 +235,16 @@ system_talk(Device *d, Uint8 b0, Uint8 w)
234 235
 void
235 236
 console_talk(Device *d, Uint8 b0, Uint8 w)
236 237
 {
238
+	char buffer[7], *p = buffer;
239
+	int len = 0;
237 240
 	if(!w) return;
238 241
 	switch(b0) {
239
-	case 0x8: write(1, &d->dat[0x8], 1); break;
240
-	case 0x9: fprintf(stdout, "0x%02x", d->dat[0x9]); break;
241
-	case 0xb: fprintf(stdout, "0x%04x", mempeek16(d->dat, 0xa)); break;
242
-	case 0xd: fprintf(stdout, "%s", &d->mem[mempeek16(d->dat, 0xc)]); break;
242
+	case 0x8: len = 1, p = (char *)&d->dat[0x8]; break;
243
+	case 0x9: len = sprintf(p, "0x%02x", d->dat[0x9]); break;
244
+	case 0xb: len = sprintf(p, "0x%04x", mempeek16(d->dat, 0xa)); break;
245
+	case 0xd: len = strlen(p = (char *)&d->mem[mempeek16(d->dat, 0xc)]); break;
243 246
 	}
244
-	fflush(stdout);
247
+	if(len) write(1, p, len);
245 248
 }
246 249
 
247 250
 void
... ...
@@ -374,6 +377,8 @@ start(Uxn *u)
374 377
 				break;
375 378
 			}
376 379
 		}
380
+		while(read(0, &devconsole->dat[0x8], 1) > 0)
381
+			evaluxn(u, mempeek16(devconsole->dat, 0));
377 382
 		evaluxn(u, mempeek16(devscreen->dat, 0));
378 383
 		if(reqdraw)
379 384
 			redraw(u);
... ...
@@ -391,6 +396,9 @@ main(int argc, char **argv)
391 396
 	Uxn u;
392 397
 	zoom = 2;
393 398
 
399
+	/* set stdin nonblocking */
400
+	fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) | O_NONBLOCK);
401
+
394 402
 	if(argc < 2)
395 403
 		return error("Input", "Missing");
396 404
 	if(!bootuxn(&u))
... ...
@@ -401,7 +409,7 @@ main(int argc, char **argv)
401 409
 		return error("Init", "Failed");
402 410
 
403 411
 	portuxn(&u, 0x0, "system", system_talk);
404
-	portuxn(&u, 0x1, "console", console_talk);
412
+	devconsole = portuxn(&u, 0x1, "console", console_talk);
405 413
 	devscreen = portuxn(&u, 0x2, "screen", screen_talk);
406 414
 	devaudio0 = portuxn(&u, 0x3, "audio0", audio_talk);
407 415
 	portuxn(&u, 0x4, "audio1", audio_talk);