aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/main.c104
1 files changed, 61 insertions, 43 deletions
diff --git a/src/main.c b/src/main.c
index 0e56897..06d2d5b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -135,27 +135,45 @@ screen_talk(Device *d, u8 b0, u8 w) {
135 u8 *addr = &d->mem[mempeek16(d->dat, 0xc)]; 135 u8 *addr = &d->mem[mempeek16(d->dat, 0xc)];
136 u8 *layer = d->dat[0xe] >> 4 & 0x1 ? ppu.fg : ppu.bg; 136 u8 *layer = d->dat[0xe] >> 4 & 0x1 ? ppu.fg : ppu.bg;
137 u8 mode = d->dat[0xe] >> 5; 137 u8 mode = d->dat[0xe] >> 5;
138 u8 color = d->dat[0xf] & 0xf;
138 if(!mode) { 139 if(!mode) {
139 ppu_pixel(layer, x, y, d->dat[0xe] & 0x3); 140 ppu_pixel(layer, x, y, d->dat[0xe] & 0x3);
140 } else if(mode-- & 0x1) { 141 } else if(mode-- & 0x1) {
141 ppu_1bpp(layer, x, y, addr, d->dat[0xe] & 0xf, mode & 0x2, mode & 0x4); 142 u8 flipx = mode & 0x2;
143 u8 flipy = mode & 0x4;
144 ppu_1bpp(layer, x, y, addr, color, flipx, flipy);
142 } else { 145 } else {
143 ppu_2bpp(layer, x, y, addr, d->dat[0xe] & 0xf, mode & 0x2, mode & 0x4); 146 u8 flipx = mode & 0x2;
147 u8 flipy = mode & 0x4;
148 ppu_2bpp(layer, x, y, addr, color, flipx, flipy);
149 }
150 } else if(w && b0 == 0xf) {
151 u16 x = mempeek16(d->dat, 0x8);
152 u16 y = mempeek16(d->dat, 0xa);
153 u8 *addr = &d->mem[mempeek16(d->dat, 0xc)];
154 u8 *layer = d->dat[0xf] >> 6 & 0x1 ? ppu.fg : ppu.bg;
155 u8 color = d->dat[0xf] & 0xf;
156 u8 flipx = (d->dat[0xf] >> 0x4) & 0x1;
157 u8 flipy = (d->dat[0xf] >> 0x5) & 0x1;
158 if(d->dat[0xf] >> 0x7 & 0x1) {
159 ppu_2bpp(layer, x, y, addr, color, flipx, flipy);
160 } else {
161 ppu_1bpp(layer, x, y, addr, color, flipx, flipy);
144 } 162 }
145 } 163 }
146} 164}
147 165
148static void 166static void
149audio_talk(Device *d, u8 b0, u8 w) { 167audio_talk(Device *d, u8 b0, u8 w) {
150 AudioChannel *c = &channels[d - devaudio]; 168 AudioChannel *c = &channels[d - devaudio];
151 if(!w) { 169 if(!w) {
152 if(b0 == 0x2) { 170 if(b0 == 0x2) {
153 mempoke16(d->dat, 0x2, c->pos); 171 mempoke16(d->dat, 0x2, c->pos);
154 c->pos <<= 12; // fixed point. 172 c->pos <<= 12; // fixed point.
155 } else if(b0 == 0x4) { 173 } else if(b0 == 0x4) {
156 // d->dat[0x4] = apu_get_vu(c); 174 // d->dat[0x4] = apu_get_vu(c);
157 } 175 }
158 } else if(b0 == 0xf) { 176 } else if(b0 == 0xf) {
159 u16 length = mempeek16(d->dat, 0xa); 177 u16 length = mempeek16(d->dat, 0xa);
160 u8 *data = &d->mem[mempeek16(d->dat, 0xc)]; 178 u8 *data = &d->mem[mempeek16(d->dat, 0xc)];
161 u8 pitch = d->dat[0xf] & 0x7f; 179 u8 pitch = d->dat[0xf] & 0x7f;
@@ -163,20 +181,20 @@ audio_talk(Device *d, u8 b0, u8 w) {
163 u32 vol = MAX(d->dat[0xe] >> 4, d->dat[0xe] & 0xf) * 4 / 3; 181 u32 vol = MAX(d->dat[0xe] >> 4, d->dat[0xe] & 0xf) * 4 / 3;
164 bool loop = !(d->dat[0xf] & 0x80); 182 bool loop = !(d->dat[0xf] & 0x80);
165 update_channel(c, data, length, pitch, adsr, vol, loop); 183 update_channel(c, data, length, pitch, adsr, vol, loop);
166 } 184 }
167} 185}
168 186
169void 187void
170datetime_talk(Device *d, u8 b0, u8 w) { 188datetime_talk(Device *d, u8 b0, u8 w) {
171 (void)d; 189 (void)d;
172 (void)b0; 190 (void)b0;
173 (void)w; 191 (void)w;
174} 192}
175 193
176void 194void
177file_talk(Device *d, u8 b0, u8 w) { 195file_talk(Device *d, u8 b0, u8 w) {
178 u8 read = b0 == 0xd; 196 u8 read = b0 == 0xd;
179 if(w && (read || b0 == 0xf)) { 197 if(w && (read || b0 == 0xf)) {
180 char *name = (char *)&d->mem[mempeek16(d->dat, 0x8)]; 198 char *name = (char *)&d->mem[mempeek16(d->dat, 0x8)];
181 u16 result = 0, length = mempeek16(d->dat, 0xa); 199 u16 result = 0, length = mempeek16(d->dat, 0xa);
182 u16 offset = mempeek16(d->dat, 0x4); 200 u16 offset = mempeek16(d->dat, 0x4);
@@ -187,7 +205,7 @@ file_talk(Device *d, u8 b0, u8 w) {
187 } 205 }
188 File file = fs_open_file(name, mode); 206 File file = fs_open_file(name, mode);
189 if (file.index != FS_NULL) { 207 if (file.index != FS_NULL) {
190 if(fs_seek(&file, offset, SEEK_SET) != -1) { 208 if(fs_seek(&file, offset, SEEK_SET) != -1) {
191 if (read) { 209 if (read) {
192 result = fs_read(&d->mem[addr], length, &file); 210 result = fs_read(&d->mem[addr], length, &file);
193 } else { 211 } else {
@@ -195,8 +213,8 @@ file_talk(Device *d, u8 b0, u8 w) {
195 } 213 }
196 } 214 }
197 } 215 }
198 mempoke16(d->dat, 0x2, result); 216 mempoke16(d->dat, 0x2, result);
199 } 217 }
200} 218}
201 219
202void 220void
@@ -219,10 +237,10 @@ init_uxn(Uxn *u) {
219 portuxn(u, 0x5, "audio2", audio_talk); 237 portuxn(u, 0x5, "audio2", audio_talk);
220 portuxn(u, 0x6, "audio3", audio_talk); 238 portuxn(u, 0x6, "audio3", audio_talk);
221 portuxn(u, 0x7, "---", nil_talk); 239 portuxn(u, 0x7, "---", nil_talk);
222 devctrl = portuxn(u, 0x8, "controller", nil_talk); 240 devctrl = portuxn(u, 0x8, "controller", nil_talk);
223 devmouse = portuxn(u, 0x9, "mouse", nil_talk); 241 devmouse = portuxn(u, 0x9, "mouse", nil_talk);
224 portuxn(u, 0xa, "file", file_talk); 242 portuxn(u, 0xa, "file", file_talk);
225 portuxn(u, 0xb, "datetime", datetime_talk); 243 portuxn(u, 0xb, "datetime", datetime_talk);
226 portuxn(u, 0xc, "---", nil_talk); 244 portuxn(u, 0xc, "---", nil_talk);
227 portuxn(u, 0xd, "---", nil_talk); 245 portuxn(u, 0xd, "---", nil_talk);
228 portuxn(u, 0xe, "---", nil_talk); 246 portuxn(u, 0xe, "---", nil_talk);
@@ -235,33 +253,33 @@ IWRAM_CODE
235void 253void
236handle_input(Uxn *u) { 254handle_input(Uxn *u) {
237 poll_keys(); 255 poll_keys();
238 if (key_tap(KEY_SELECT)) { 256 if (key_tap(KEY_SELECT)) {
239 // Reset control variables on method switch. 257 // Reset control variables on method switch.
240 switch (ctrl_methods[ctrl_idx]) { 258 switch (ctrl_methods[ctrl_idx]) {
241 case CONTROL_CONTROLLER: { 259 case CONTROL_CONTROLLER: {
242 devctrl->dat[2] = 0; 260 devctrl->dat[2] = 0;
243 evaluxn(u, mempeek16(devctrl->dat, 0)); 261 evaluxn(u, mempeek16(devctrl->dat, 0));
244 devctrl->dat[3] = 0; 262 devctrl->dat[3] = 0;
245 } break; 263 } break;
246 case CONTROL_MOUSE: { 264 case CONTROL_MOUSE: {
247 devmouse->dat[6] = 0; 265 devmouse->dat[6] = 0;
248 devmouse->dat[7] = 0; 266 devmouse->dat[7] = 0;
249 mempoke16(devmouse->dat, 0x2, -10); 267 mempoke16(devmouse->dat, 0x2, -10);
250 mempoke16(devmouse->dat, 0x4, -10); 268 mempoke16(devmouse->dat, 0x4, -10);
251 evaluxn(u, mempeek16(devmouse->dat, 0)); 269 evaluxn(u, mempeek16(devmouse->dat, 0));
252 } break; 270 } break;
253 case CONTROL_KEYBOARD: { 271 case CONTROL_KEYBOARD: {
254 toggle_keyboard(); 272 toggle_keyboard();
255 } break; 273 } break;
256 } 274 }
257 275
258 // Update ctrl_idx. 276 // Update ctrl_idx.
259 ctrl_idx = (ctrl_idx + 1 > (int)LEN(ctrl_methods) - 1) ? 0 : ctrl_idx + 1; 277 ctrl_idx = (ctrl_idx + 1 > (int)LEN(ctrl_methods) - 1) ? 0 : ctrl_idx + 1;
260 278
261 // Initialize controller variables here. 279 // Initialize controller variables here.
262 if (ctrl_methods[ctrl_idx] == CONTROL_KEYBOARD) { 280 if (ctrl_methods[ctrl_idx] == CONTROL_KEYBOARD) {
263 toggle_keyboard(); 281 toggle_keyboard();
264 } 282 }
265 } 283 }
266 284
267 if (ctrl_methods[ctrl_idx] == CONTROL_CONTROLLER) { 285 if (ctrl_methods[ctrl_idx] == CONTROL_CONTROLLER) {
@@ -313,7 +331,7 @@ handle_input(Uxn *u) {
313 devctrl->dat[3] = 0; 331 devctrl->dat[3] = 0;
314 } else if (ctrl_methods[ctrl_idx] == CONTROL_MOUSE) { 332 } else if (ctrl_methods[ctrl_idx] == CONTROL_MOUSE) {
315 // Detect "mouse key press". 333 // Detect "mouse key press".
316 u8 flag = devmouse->dat[6]; 334 u8 flag = devmouse->dat[6];
317 if (key_tap(KEY_B)) { 335 if (key_tap(KEY_B)) {
318 flag |= 0x01; 336 flag |= 0x01;
319 } else if (key_released(KEY_B)) { 337 } else if (key_released(KEY_B)) {
@@ -327,11 +345,11 @@ handle_input(Uxn *u) {
327 345
328 // Handle chording. 346 // Handle chording.
329 devmouse->dat[6] = flag; 347 devmouse->dat[6] = flag;
330 if(flag == 0x10 && (devmouse->dat[6] & 0x01)) { 348 if(flag == 0x10 && (devmouse->dat[6] & 0x01)) {
331 devmouse->dat[7] = 0x01; 349 devmouse->dat[7] = 0x01;
332 } 350 }
333 if(flag == 0x01 && (devmouse->dat[6] & 0x10)) { 351 if(flag == 0x01 && (devmouse->dat[6] & 0x10)) {
334 devmouse->dat[7] = 0x10; 352 devmouse->dat[7] = 0x10;
335 } 353 }
336 354
337 // Detect mouse movement. 355 // Detect mouse movement.
@@ -349,7 +367,7 @@ handle_input(Uxn *u) {
349 // Eval mouse. 367 // Eval mouse.
350 mempoke16(devmouse->dat, 0x2, mouse.x); 368 mempoke16(devmouse->dat, 0x2, mouse.x);
351 mempoke16(devmouse->dat, 0x4, mouse.y); 369 mempoke16(devmouse->dat, 0x4, mouse.y);
352 evaluxn(u, mempeek16(devmouse->dat, 0)); 370 evaluxn(u, mempeek16(devmouse->dat, 0));
353 } else if (ctrl_methods[ctrl_idx] == CONTROL_KEYBOARD) { 371 } else if (ctrl_methods[ctrl_idx] == CONTROL_KEYBOARD) {
354 if (key_tap(KEY_LEFT)) { 372 if (key_tap(KEY_LEFT)) {
355 update_cursor(cursor_position - 1); 373 update_cursor(cursor_position - 1);