Browse code

Naive mouse support

neauoire authored on 10/02/2021 01:22:52
Showing 7 changed files
... ...
@@ -196,9 +196,9 @@ pass2(FILE *f)
196 196
 			fscanf(f, "%s", w);
197 197
 		else if(w[0] == '"')
198 198
 			pushtext(w + 1);
199
-		else if(w[0] == '#') {
199
+		else if(w[0] == '#')
200 200
 			pushshort(shex(w + 1) & 0xff, 1);
201
-		} else if((l = findlabel(w + 1)))
201
+		else if((l = findlabel(w + 1)))
202 202
 			pushshort(l->addr, w[0] == ',');
203 203
 		else if((op = findoperator(w)) || scmp(w, "BRK"))
204 204
 			pushbyte(op, 0);
... ...
@@ -24,5 +24,5 @@ rm -f ./bin/emulator
24 24
 cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined uxn.c emulator.c -L/usr/local/lib -lSDL2 -o bin/emulator
25 25
 
26 26
 # run
27
-./bin/assembler examples/pixel.usm bin/boot.rom
27
+./bin/assembler examples/mouse.usm bin/boot.rom
28 28
 ./bin/emulator bin/boot.rom
... ...
@@ -37,6 +37,8 @@ SDL_Renderer *gRenderer;
37 37
 SDL_Texture *gTexture;
38 38
 Uint32 *pixels;
39 39
 
40
+Device *devscreen, *devmouse, *devkey;
41
+
40 42
 int
41 43
 error(char *msg, const char *err)
42 44
 {
... ...
@@ -143,8 +145,15 @@ echof(Uxn *c)
143 145
 void
144 146
 domouse(SDL_Event *event)
145 147
 {
146
-	(void)event;
147
-	/* printf("mouse\n"); */
148
+	int x = event->motion.x / ZOOM - PAD * 8;
149
+	int y = event->motion.y / ZOOM - PAD * 8;
150
+	switch(event->type) {
151
+	case SDL_MOUSEBUTTONUP:
152
+	case SDL_MOUSEBUTTONDOWN:
153
+		devmouse->mem[0] = x;
154
+		devmouse->mem[1] = y;
155
+		devmouse->mem[2] = event->button.button == SDL_BUTTON_LEFT;
156
+	}
148 157
 }
149 158
 
150 159
 void
... ...
@@ -157,7 +166,7 @@ dokey(SDL_Event *event)
157 166
 #pragma mark - Devices
158 167
 
159 168
 Uint8
160
-console_onread(Device *d, Uint8 b)
169
+consoler(Device *d, Uint8 b)
161 170
 {
162 171
 	(void)b;
163 172
 	(void)d;
... ...
@@ -165,7 +174,7 @@ console_onread(Device *d, Uint8 b)
165 174
 }
166 175
 
167 176
 Uint8
168
-console_onwrite(Device *d, Uint8 b)
177
+consolew(Device *d, Uint8 b)
169 178
 {
170 179
 	(void)d;
171 180
 	if(b)
... ...
@@ -175,7 +184,7 @@ console_onwrite(Device *d, Uint8 b)
175 184
 }
176 185
 
177 186
 Uint8
178
-ppur(Device *d, Uint8 b)
187
+screenr(Device *d, Uint8 b)
179 188
 {
180 189
 	(void)b;
181 190
 	(void)d;
... ...
@@ -183,7 +192,7 @@ ppur(Device *d, Uint8 b)
183 192
 }
184 193
 
185 194
 Uint8
186
-ppuw(Device *d, Uint8 b)
195
+screenw(Device *d, Uint8 b)
187 196
 {
188 197
 	d->mem[d->len++] = b;
189 198
 	if(d->len > 5) {
... ...
@@ -198,6 +207,32 @@ ppuw(Device *d, Uint8 b)
198 207
 	return 0;
199 208
 }
200 209
 
210
+Uint8
211
+mouser(Device *d, Uint8 b)
212
+{
213
+	return d->mem[b];
214
+}
215
+
216
+Uint8
217
+mousew(Device *d, Uint8 b)
218
+{
219
+	return 0;
220
+}
221
+
222
+Uint8
223
+keyr(Device *d, Uint8 b)
224
+{
225
+	return 0;
226
+}
227
+
228
+Uint8
229
+keyw(Device *d, Uint8 b)
230
+{
231
+	return 0;
232
+}
233
+
234
+#pragma mark - Generics
235
+
201 236
 int
202 237
 start(Uxn *u)
203 238
 {
... ...
@@ -240,8 +275,10 @@ main(int argc, char **argv)
240 275
 	if(!init())
241 276
 		return error("Init", "Failed");
242 277
 
243
-	portuxn(&u, "console", console_onread, console_onwrite);
244
-	portuxn(&u, "PPU", ppur, ppuw);
278
+	portuxn(&u, "console", consoler, consolew);
279
+	devscreen = portuxn(&u, "screen", screenr, screenw);
280
+	devmouse = portuxn(&u, "mouse", mouser, mousew);
281
+	devkey = portuxn(&u, "key", keyr, keyw);
245 282
 
246 283
 	start(&u);
247 284
 
248 285
new file mode 100644
... ...
@@ -0,0 +1,27 @@
1
+( draw pixel )
2
+
3
+|0100 @RESET
4
+	
5
+	( draw 3 pixels )
6
+	,00 ,01 ,0000 ,0000 ,putpixel JSR
7
+	,00 ,02 ,0001 ,0001 ,putpixel JSR
8
+	,01 ,03 ,0002 ,0002 ,putpixel JSR
9
+
10
+BRK
11
+
12
+|c000 @FRAME 
13
+	,01 ,01 ( redraw color )
14
+	,00 ,01 ,02 IOR ( grab y )
15
+	,00 ,00 ,02 IOR ( grab x )
16
+	,putpixel JSR
17
+BRK 
18
+
19
+@putpixel 
20
+	SWP ,01 IOW ,01 IOW ( y )
21
+	SWP ,01 IOW ,01 IOW ( x )
22
+	,01 IOW ( color )
23
+	,01 IOW ( redraw )
24
+	RTS
25
+
26
+|d000 @ERROR BRK 
27
+|FFFA .RESET .FRAME .ERROR
... ...
@@ -1,30 +1,21 @@
1 1
 ( draw pixel )
2 2
 
3 3
 |0100 @RESET
4
-
5
-	,0001 IOW ( x0 )
6
-	,0001 IOW ( x1 )
7
-	,0001 IOW ( y0 )
8
-	,0001 IOW ( y1 )
9
-	,0101 IOW ( clr )
10
-	,0001 IOW ( noredraw )
11
-
12
-	,0001 IOW ( x0 )
13
-	,0101 IOW ( x1 )
14
-	,0001 IOW ( y0 )
15
-	,0001 IOW ( y1 )
16
-	,0201 IOW ( clr )
17
-	,0001 IOW ( noredraw )
18
-
19
-	,0001 IOW ( x0 )
20
-	,0201 IOW ( x1 )
21
-	,0001 IOW ( y0 )
22
-	,0001 IOW ( y1 )
23
-	,0301 IOW ( clr )
24
-	,0101 IOW ( redraw! )
4
+	
5
+	( redraw - color - x - y )
6
+	,00 ,01 ,0000 ,0000 ,putpixel JSR
7
+	,00 ,02 ,0001 ,0001 ,putpixel JSR
8
+	,01 ,03 ,0002 ,0002 ,putpixel JSR
25 9
 
26 10
 BRK
27 11
 
12
+@putpixel 
13
+	SWP ,01 IOW ,01 IOW ( y )
14
+	SWP ,01 IOW ,01 IOW ( x )
15
+	,01 IOW ( color )
16
+	,01 IOW ( redraw )
17
+	RTS
18
+
28 19
 |c000 @FRAME BRK 
29 20
 |d000 @ERROR BRK 
30 21
 |FFFA .RESET .FRAME .ERROR
... ...
@@ -186,7 +186,7 @@ loaduxn(Uxn *u, char *filepath)
186 186
 
187 187
 /* to start: evaluxn(u, u->vreset); */
188 188
 
189
-int
189
+Device *
190 190
 portuxn(Uxn *u, char *name, Uint8 (*onread)(Device *, Uint8), Uint8 (*onwrite)(Device *, Uint8))
191 191
 {
192 192
 	Device *d = &u->dev[u->devices++];
... ...
@@ -194,5 +194,5 @@ portuxn(Uxn *u, char *name, Uint8 (*onread)(Device *, Uint8), Uint8 (*onwrite)(D
194 194
 	d->wfn = onwrite;
195 195
 	d->len = 0;
196 196
 	printf("Device#%d: %s \n", u->devices, name);
197
-	return 1;
197
+	return d;
198 198
 }
... ...
@@ -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, char *name, Uint8 (*onread)(Device *, Uint8), Uint8 (*onwrite)(Device *, Uint8));
57
+Device *portuxn(Uxn *u, char *name, Uint8 (*onread)(Device *, Uint8), Uint8 (*onwrite)(Device *, Uint8));