Browse code

Moved dev code out of uxn

neauoire authored on 09/02/2021 17:00:27
Showing 3 changed files
... ...
@@ -21,13 +21,18 @@ error(char *msg, const char *err)
21 21
 }
22 22
 
23 23
 void
24
-console_onread(void)
24
+console_onread(Uint8 *b)
25 25
 {
26
+	(void)b;
26 27
 }
27 28
 
28 29
 void
29
-console_onwrite(void)
30
+console_onwrite(Uint8 *b)
30 31
 {
32
+	if(b) {
33
+		printf("%c", *b);
34
+		*b = 0x00;
35
+	}
31 36
 }
32 37
 
33 38
 void
... ...
@@ -114,15 +114,12 @@ lituxn(Uxn *u, Uint8 instr)
114 114
 }
115 115
 
116 116
 int
117
-devuxn(Uxn *u) /* experimental */
117
+devuxn(Uxn *u)
118 118
 {
119 119
 	int i;
120 120
 	for(i = 0; i < u->devices; ++i) {
121
-		Uint16 addr = u->dev[i].w;
122
-		if(u->ram.dat[addr]) {
123
-			printf("%c", u->ram.dat[addr]);
124
-			u->ram.dat[addr] = 0;
125
-		}
121
+		u->dev[i].wfn(&u->ram.dat[u->dev[i].w]);
122
+		u->dev[i].rfn(&u->ram.dat[u->dev[i].r]);
126 123
 	}
127 124
 	return 1;
128 125
 }
... ...
@@ -147,10 +144,8 @@ opcuxn(Uxn *u, Uint8 instr)
147 144
 }
148 145
 
149 146
 int
150
-parse(Uxn *u) /* TODO: Rename */
147
+stepuxn(Uxn *u, Uint8 instr)
151 148
 {
152
-	Uint8 instr = u->ram.dat[u->ram.ptr++];
153
-	u->counter++;
154 149
 	if(u->literal > 0)
155 150
 		return lituxn(u, instr);
156 151
 	else
... ...
@@ -162,8 +157,12 @@ evaluxn(Uxn *u, Uint16 vec)
162 157
 {
163 158
 	u->ram.ptr = vec;
164 159
 	setflag(&u->status, FLAG_HALT, 0);
165
-	while(!(u->status & FLAG_HALT) && parse(u))
166
-		;
160
+	while(!(u->status & FLAG_HALT)) {
161
+		Uint8 instr = u->ram.dat[u->ram.ptr++];
162
+		if(!stepuxn(u, instr))
163
+			return 0;
164
+		u->counter++;
165
+	}
167 166
 	return 1;
168 167
 }
169 168
 
... ...
@@ -198,7 +197,7 @@ loaduxn(Uxn *u, char *filepath)
198 197
 /* to start: evaluxn(u, u->vreset); */
199 198
 
200 199
 int
201
-portuxn(Uxn *u, Uint16 r, Uint16 w, void (*onread)(), void (*onwrite)())
200
+portuxn(Uxn *u, Uint16 r, Uint16 w, void (*onread)(Uint8 *), void (*onwrite)(Uint8 *))
202 201
 {
203 202
 	Device *d = &u->dev[u->devices++];
204 203
 	d->r = r;
... ...
@@ -36,8 +36,8 @@ typedef struct {
36 36
 
37 37
 typedef struct {
38 38
 	Uint16 r, w;
39
-	void (*rfn)(void);
40
-	void (*wfn)(void);
39
+	void (*rfn)(Uint8 *);
40
+	void (*wfn)(Uint8 *);
41 41
 } Device;
42 42
 
43 43
 typedef struct {
... ...
@@ -54,4 +54,4 @@ int getflag(Uint8 *status, char flag);
54 54
 int loaduxn(Uxn *c, char *filepath);
55 55
 int bootuxn(Uxn *c);
56 56
 int evaluxn(Uxn *u, Uint16 vec);
57
-int portuxn(Uxn *u, Uint16 r, Uint16 w, void (*onread)(), void (*onwrite)());
57
+int portuxn(Uxn *u, Uint16 r, Uint16 w, void (*onread)(Uint8 *), void (*onwrite)(Uint8 *));