also fake tty
[ginge.git] / loader / emu.c
CommitLineData
499bf01c 1/*
2 * GINGE - GINGE Is Not Gp2x Emulator
fea1465c 3 * (C) notaz, 2010-2011,2016
499bf01c 4 *
5 * This work is licensed under the MAME license, see COPYING file for details.
6 */
830ba1bf 7// a "gentle" reminder
8#ifdef __ARM_EABI__
9#error loader is meant to be OABI!
10#endif
11913091 11#include <stdio.h>
12#include <stdlib.h>
3d295a9f 13#include <string.h>
c1ffd5ba 14#include <alloca.h>
158ddd8d 15#include <ctype.h>
11913091 16#include <sys/types.h>
17#include <sys/stat.h>
18#include <fcntl.h>
19#include <sys/mman.h>
20#include <sys/types.h>
21#include <unistd.h>
22#include <signal.h>
23#include <asm/ucontext.h>
c1ffd5ba 24#include <errno.h>
25#include <time.h>
d0b9b0df 26#include <sched.h>
c1ffd5ba 27#include <sys/resource.h>
7fd42181 28#include <sys/ioctl.h>
d0b9b0df 29#include <sys/syscall.h>
7fd42181 30#include <linux/soundcard.h>
31#include <linux/fb.h>
d0b9b0df 32#include <linux/futex.h>
11913091 33
34#include "header.h"
7fd42181 35#include "../common/host_fb.h"
36#include "../common/cmn.h"
d0b9b0df 37#include "syscalls.h"
7fd42181 38#include "realfuncs.h"
d0b9b0df 39#include "llibc.h"
11913091 40
3d3fa63b 41#if (DBG & 2) && !(DBG & 4)
7fd42181 42#define LOG_IO_UNK
43#endif
3d3fa63b 44#if (DBG & 4)
7fd42181 45#define LOG_IO
46#endif
6720e4e4 47//#define LOG_SEGV
48
49#ifdef LOG_IO
50#define iolog log_io
51#else
86418a84 52#define iolog(...)
6720e4e4 53#endif
54
c1ffd5ba 55#ifdef LOG_IO_UNK
6720e4e4 56#define iolog_unh log_io
57#else
58#define iolog_unh(...)
59#endif
60
61#ifdef LOG_SEGV
d0b9b0df 62#define segvlog g_printf
6720e4e4 63#else
0881206b 64#define segvlog(...)
6720e4e4 65#endif
66
c1ffd5ba 67#if defined(LOG_IO) || defined(LOG_IO_UNK)
6720e4e4 68#include "mmsp2-regs.h"
69#endif
11913091 70
3d3fa63b 71typedef unsigned long long u64;
11913091 72typedef unsigned int u32;
73typedef unsigned short u16;
74typedef unsigned char u8;
75
d0b9b0df 76#define THREAD_STACK_SIZE 0x200000
77
78static int fb_sync_thread_paused;
79static int fb_sync_thread_futex;
c1ffd5ba 80
890c1dc5 81static int emu_is_dl;
82
3d295a9f 83static struct {
84 u32 dstctrl;
85 u32 dstaddr;
86 u32 dststride;
87 u32 srcctrl;
88 u32 srcaddr; //
89 u32 srcstride;
90 u32 srcforcolor;
91 u32 srcbackcolor;
92 u32 patctrl; //
93 u32 patforcolor;
94 u32 patbackcolor;
95 u32 size;
96 u32 ctrl; //
97 u32 run;
98 u32 intc;
99 u32 srcfifo;
100} blitter;
101
0881206b 102#define SRCCTRL_INVIDEO (1 << 8)
103#define SRCCTRL_SRCENB (1 << 7)
104#define CTRL_TRANSPARENCYENB (1 << 11)
105
3d295a9f 106static struct {
7000b522 107 // mmsp2
6720e4e4 108 u16 mlc_stl_cntl;
3d295a9f 109 union {
7000b522 110 u32 mlc_stl_adr; // mlcaddress for pollux
3d295a9f 111 struct {
6720e4e4 112 u16 mlc_stl_adrl;
113 u16 mlc_stl_adrh;
3d295a9f 114 };
115 };
6720e4e4 116 u16 mlc_stl_pallt_a;
117 union {
118 u16 mlc_stl_pallt_d[256*2];
119 u32 mlc_stl_pallt_d32[256];
120 };
121
7000b522 122 // pollux
123 u32 mlccontrol;
124 u16 mlcpalette[256];
125
6720e4e4 126 // state
3d3fa63b 127 void *umem;
6720e4e4 128 u32 old_mlc_stl_adr;
7000b522 129 u32 btn_state; // as seen through /dev/GPIO: 0PVdVu YXBA RLSeSt 0Ri0Dn 0Le0Up
130 struct {
131 u32 width, height;
132 u32 stride;
133 u32 bpp;
134 u32 dirty_pal:2;
135 } v;
3d295a9f 136} mmsp2;
7000b522 137#define pollux mmsp2 // so that code doesn't look that weird
138enum {
139 DIRTY_PAL_MMSP2 = 1,
140 DIRTY_PAL_POLLUX = 2,
141};
3d295a9f 142
11913091 143
c1ffd5ba 144#if defined(LOG_IO) || defined(LOG_IO_UNK)
6720e4e4 145static void log_io(const char *pfx, u32 a, u32 d, int size)
146{
147 const char *fmt, *reg = "";
148 switch (size) {
c1ffd5ba 149 case 8: fmt = "%s %08x %02x %s\n"; d &= 0xff; break;
150 case 32: fmt = "%s %08x %08x %s\n"; break;
151 default: fmt = "%s %08x %04x %s\n"; d &= 0xffff; break;
6720e4e4 152 }
153
154 if ((a & ~0xffff) == 0x7f000000)
155 reg = regnames[a & 0xffff];
156
d0b9b0df 157 g_printf(fmt, pfx, a, d, reg);
6720e4e4 158}
159#endif
160
0881206b 161static void memset16(void *dst, u32 pattern, int count)
162{
163 u32 *dl;
164 u16 *d;
165
166 d = (u16 *)((long)dst & ~1);
167 if ((long)d & 2) {
168 *d++ = pattern;
169 count--;
170 }
171 dl = (void *)d;
172 pattern |= pattern << 16;
173
174 while (count >= 2) {
175 *dl++ = pattern;
176 count -= 2;
177 }
178 if (count)
179 *(u16 *)dl = pattern;
180}
181
182static void blt_tr(void *dst, void *src, u32 trc, int w)
183{
184 u16 *d = (u16 *)((long)dst & ~1);
185 u16 *s = (u16 *)((long)src & ~1);
186
187 // XXX: optimize
188 for (; w > 0; d++, s++, w--)
189 if (*s != trc)
190 *d = *s;
191}
192
193#define dump_blitter() \
194{ \
195 u32 *r = &blitter.dstctrl; \
196 int i; \
197 for (i = 0; i < 4*4; i++, r++) { \
d0b9b0df 198 g_printf("%08x ", *r); \
0881206b 199 if ((i & 3) == 3) \
d0b9b0df 200 g_printf("\n"); \
0881206b 201 } \
202}
203
6720e4e4 204static void *uppermem_lookup(u32 addr, u8 **mem_end)
11913091 205{
3d3fa63b 206 // XXX: maybe support mirroring?
207 if ((addr & 0xfe000000) != 0x02000000)
208 return NULL;
3d295a9f 209
3d3fa63b 210 *mem_end = (u8 *)mmsp2.umem + 0x02000000;
211 return (u8 *)mmsp2.umem - 0x02000000 + addr;
11913091 212}
213
3d295a9f 214static void blitter_do(void)
11913091 215{
0881206b 216 u8 *dst, *dste, *src = NULL, *srce = NULL;
3d295a9f 217 int w, h, sstrd, dstrd;
6720e4e4 218 int to_screen = 0;
dc90c02d 219 u32 bpp, addr;
3d295a9f 220
221 w = blitter.size & 0x7ff;
222 h = (blitter.size >> 16) & 0x7ff;
223 sstrd = blitter.srcstride;
224 dstrd = blitter.dststride;
225
0881206b 226 // XXX: need to confirm this..
227 addr = (blitter.dstaddr & ~3) | ((blitter.dstctrl & 0x1f) >> 3);
c1ffd5ba 228
dc90c02d 229 // use dst bpp.. How does it do blits with different src bpp?
230 bpp = (blitter.dstctrl & 0x20) ? 16 : 8;
231
c1ffd5ba 232 // maybe the screen?
dc90c02d 233 if (((w == 320 && h == 240) || // blit whole screen
234 (w * h >= 320*240/2)) && // ..or at least half of the area
235 mmsp2.mlc_stl_adr <= addr && addr < mmsp2.mlc_stl_adr + 320*240*2)
c1ffd5ba 236 to_screen = 1;
237
238 dst = uppermem_lookup(addr, &dste);
0881206b 239
240 // XXX: assume fill if no SRCENB, but it could be pattern blit..
241 if (blitter.srcctrl & SRCCTRL_SRCENB) {
242 if (!(blitter.srcctrl & SRCCTRL_INVIDEO))
243 goto bad_blit;
244
245 addr = (blitter.srcaddr & ~3) | ((blitter.srcctrl & 0x1f) >> 3);
c1ffd5ba 246 src = uppermem_lookup(addr, &srce);
0881206b 247 if (src == NULL)
248 goto bad_blit;
249
250 if (src + sstrd * h > srce) {
6720e4e4 251 err("blit %08x->%08x %dx%d did not fit src\n",
0881206b 252 blitter.srcaddr, blitter.dstaddr, w, h);
253 h = (srce - src) / sstrd;
254 }
3d295a9f 255 }
256
dc90c02d 257 if (dst == NULL)
258 goto bad_blit;
259
3d295a9f 260 if (dst + dstrd * h > dste) {
6720e4e4 261 err("blit %08x->%08x %dx%d did not fit dst\n",
3d295a9f 262 blitter.srcaddr, blitter.dstaddr, w, h);
263 h = (dste - dst) / dstrd;
264 }
265
0881206b 266 if (src != NULL) {
267 // copy
dc90c02d 268 if (bpp == 16 && (blitter.ctrl & CTRL_TRANSPARENCYENB)) {
0881206b 269 u32 trc = blitter.ctrl >> 16;
270 for (; h > 0; h--, dst += dstrd, src += sstrd)
271 blt_tr(dst, src, trc, w);
272 }
273 else {
274 for (; h > 0; h--, dst += dstrd, src += sstrd)
dc90c02d 275 memcpy(dst, src, w * bpp / 8);
0881206b 276 }
3d295a9f 277 }
0881206b 278 else {
279 // fill. Assume the pattern is cleared and bg color is used
280 u32 bgc = blitter.patbackcolor & 0xffff;
dc90c02d 281 if (bpp == 16) {
282 for (; h > 0; h--, dst += dstrd)
283 memset16(dst, bgc, w);
284 }
285 else {
286 for (; h > 0; h--, dst += dstrd)
287 memset(dst, bgc, w); // bgc?
288 }
0881206b 289 }
6720e4e4 290
d0b9b0df 291 if (to_screen) {
292 fb_sync_thread_futex = 1;
293 g_futex_raw(&fb_sync_thread_futex, FUTEX_WAKE, 1, NULL);
294 }
0881206b 295 return;
3d295a9f 296
0881206b 297bad_blit:
6720e4e4 298 err("blit %08x->%08x %dx%d translated to %p->%p\n",
0881206b 299 blitter.srcaddr, blitter.dstaddr, w, h, src, dst);
300 dump_blitter();
3d295a9f 301}
302
4d045184 303// FIXME: pass real dimensions to blitters
7000b522 304static void mlc_flip(void *src, int bpp, int stride)
6720e4e4 305{
6ca08393 306 static int old_bpp;
6720e4e4 307
4d045184 308 // only pass pal to host if it's dirty
7000b522 309 if (bpp <= 8 && mmsp2.v.dirty_pal) {
310 if (mmsp2.v.dirty_pal == DIRTY_PAL_MMSP2)
311 host_video_update_pal32(mmsp2.mlc_stl_pallt_d32);
312 else
313 host_video_update_pal16(mmsp2.mlcpalette);
314 mmsp2.v.dirty_pal = 0;
6720e4e4 315 }
316
6ca08393 317 if (bpp != old_bpp) {
318 host_video_change_bpp(bpp);
319 old_bpp = bpp;
320 }
321
6720e4e4 322 switch (bpp) {
323 case 4:
7000b522 324 host_video_blit4(src, 320, 240, stride);
6720e4e4 325 break;
326
327 case 8:
7000b522 328 host_video_blit8(src, 320, 240, stride);
6720e4e4 329 break;
330
331 case 16:
7000b522 332 host_video_blit16(src, 320, 240, stride);
6720e4e4 333 break;
334
335 case 24:
336 // TODO
337 break;
338 }
6720e4e4 339}
340
c1ffd5ba 341static void *fb_sync_thread(void *arg)
342{
d0b9b0df 343 unsigned long sigmask[2] = { ~0ul, ~0ul };
344 struct timespec ts = { 0, 0 };
c1ffd5ba 345 int invalid_fb_addr = 1;
346 int manual_refresh = 0;
7000b522 347 int frame_counter = 0;
d0b9b0df 348 int wait_ret;
349
350 // this thread can't run any signal handlers since the
351 // app's stack/tls stuff will never be set up here
352 sigmask[0] &= ~(1ul << (SIGSEGV - 1));
353 g_rt_sigprocmask_raw(SIG_SETMASK, sigmask, NULL, sizeof(sigmask));
c1ffd5ba 354
c1ffd5ba 355 //ret = setpriority(PRIO_PROCESS, 0, -1);
356 //log("setpriority %d\n", ret);
357
d0b9b0df 358 // tell the main thread we're done init
359 fb_sync_thread_futex = 0;
360 g_futex_raw(&fb_sync_thread_futex, FUTEX_WAKE, 1, NULL);
c1ffd5ba 361
362 while (1) {
363 u8 *gp2x_fb, *gp2x_fb_end;
c1ffd5ba 364
d0b9b0df 365 wait_ret = g_futex_raw(&fb_sync_thread_futex, FUTEX_WAIT, 0, &ts);
dc90c02d 366
d0b9b0df 367 // this is supposed to be done atomically, but to make life
368 // easier ignore it for now, race impact is low anyway
369 fb_sync_thread_futex = 0;
370
371 if (wait_ret != 0 && wait_ret != -EWOULDBLOCK
372 && wait_ret != -ETIMEDOUT)
373 {
374 err("fb_thread: futex error: %d\n", wait_ret);
b4f4cb40 375 sleep(1);
7000b522 376 goto check_keys;
c1ffd5ba 377 }
db9191ed 378 if (fb_sync_thread_paused) {
d0b9b0df 379 ts.tv_nsec = 100000000;
7000b522 380 goto check_keys;
db9191ed 381 }
c1ffd5ba 382
d0b9b0df 383 if (wait_ret == 0) {
384 ts.tv_nsec = 50000000;
c1ffd5ba 385 manual_refresh++;
386 if (manual_refresh == 2)
4d045184 387 dbg("fb_thread: switch to manual refresh\n");
c1ffd5ba 388 } else {
d0b9b0df 389 ts.tv_nsec = 16666667;
c1ffd5ba 390 if (manual_refresh > 1)
4d045184 391 dbg("fb_thread: switch to auto refresh\n");
c1ffd5ba 392 manual_refresh = 0;
393 }
394
c1ffd5ba 395 gp2x_fb = uppermem_lookup(mmsp2.mlc_stl_adr, &gp2x_fb_end);
7000b522 396 if (gp2x_fb == NULL || gp2x_fb + 320*240 * mmsp2.v.bpp / 8 > gp2x_fb_end) {
c1ffd5ba 397 if (!invalid_fb_addr) {
398 err("fb_thread: %08x is out of range\n", mmsp2.mlc_stl_adr);
399 invalid_fb_addr = 1;
400 }
401 continue;
402 }
403
7000b522 404 invalid_fb_addr = 0;
405 mlc_flip(gp2x_fb, mmsp2.v.bpp, mmsp2.v.stride);
406
407 frame_counter++;
408 if (frame_counter & 0x0f)
409 continue;
410
411check_keys:
412 // this is to check for kill key, in case main thread hung
413 // or something else went wrong.
414 pollux.btn_state = host_read_btns();
c1ffd5ba 415 }
416}
417
db9191ed 418static void fb_thread_pause(void)
419{
420 fb_sync_thread_paused = 1;
421 // wait until it finishes last refresh
422 // that it might be doing now
423 usleep(10000);
424}
425
426static void fb_thread_resume(void)
427{
428 fb_sync_thread_paused = 0;
429}
430
7000b522 431static u32 xread32_io_cmn(u32 a, u32 *handled)
432{
26d2801c 433 struct timespec ts;
7000b522 434 u32 d = 0;
435
436 *handled = 1;
437 switch (a) {
438 // Wiz stuff
26d2801c 439 case 0x1980: // TIMER3 TMRCOUNT
440 // assume the timer is set up for microsec time
441 g_clock_gettime_raw(CLOCK_REALTIME, &ts);
442 d = ts.tv_sec * 1000000 + ((u64)(u32)ts.tv_nsec * 4294968 >> 32);
443 break;
7000b522 444 case 0x402c: // MLCVSTRIDE0
445 case 0x4060: // MLCVSTRIDE1
446 d = pollux.v.stride;
447 break;
448 case 0x4038: // MLCADDRESS0
449 case 0x406c: // MLCADDRESS1
450 d = pollux.mlc_stl_adr;
451 break;
452 // wiz_lib reads:
453 // ???? ???? YXBA DURiLe ???? VdVuMS LR?? ????
454 // | GPIOC[31:16] | GPIOB[31:16] |
455 case 0xa058: // GPIOBPAD
830ba1bf 456 d = (pollux.btn_state >> 1) & 0x0100;
457 d |= (pollux.btn_state << 1) & 0x0200;
7000b522 458 d |= (pollux.btn_state >> 3) & 0x0080;
459 d |= (pollux.btn_state >> 5) & 0x0040;
460 d |= (pollux.btn_state >> 6) & 0x0c00;
461 d <<= 16;
462 d = ~d;
463 break;
464 case 0xa098: // GPIOCPAD
465 pollux.btn_state = host_read_btns();
466 d = (pollux.btn_state >> 8) & 0x00f0;
467 d |= (pollux.btn_state >> 1) & 0x0008;
468 d |= (pollux.btn_state << 2) & 0x0004;
469 d |= (pollux.btn_state >> 5) & 0x0002;
470 d |= (pollux.btn_state >> 2) & 0x0001;
471 d <<= 16;
472 d = ~d;
473 break;
474 default:
475 *handled = 0;
476 break;
477 }
478
479 return d;
480}
481
3d295a9f 482static u32 xread8(u32 a)
483{
6720e4e4 484 iolog("r8 ", a, 0, 8);
485 iolog_unh("r8 ", a, 0, 8);
3d295a9f 486 return 0;
11913091 487}
488
489static u32 xread16(u32 a)
490{
6720e4e4 491 static u32 fudge, old_a;
492 u32 d = 0, t;
493
494 if ((a & 0xffff0000) == 0x7f000000) {
495 u32 a_ = a & 0xffff;
496 switch (a_) {
497 case 0x0910: // FPLL
498 case 0x0912:
499 d = 0x9407;
500 break;
501 // minilib reads as:
7000b522 502 // 0000 P000 VuVd00 0000 YXBA RLSeSt 0Ri0D 0Le0U
503 // | GPIOD |GPIOC[8:15]|GPIOM[0:7] |
6720e4e4 504 // /dev/GPIO:
505 // ... 0PVdVu ...
506 case 0x1184: // GPIOC
507 d = ~mmsp2.btn_state & 0xff00;
508 d |= 0x00ff;
509 break;
510 case 0x1186: // GPIOD
511 t = ~mmsp2.btn_state;
512 d = (t >> 9) & 0x0080;
513 d |= (t >> 11) & 0x0040;
514 d |= (t >> 7) & 0x0800;
515 d |= 0x373b;
516 break;
517 case 0x1198: // GPIOM
518 mmsp2.btn_state = host_read_btns();
519 d = ~mmsp2.btn_state & 0xff;
520 d |= 0x01aa;
521 break;
c1ffd5ba 522 case 0x1836: // reserved
523 d = 0x2330;
524 break;
525 case 0x2816: // DPC_X_MAX
526 d = 319;
527 break;
528 case 0x2818: // DPC_Y_MAX
529 d = 239;
530 break;
6720e4e4 531 case 0x28da:
532 d = mmsp2.mlc_stl_cntl;
533 break;
c1ffd5ba 534 case 0x290e:
535 case 0x2912:
536 d = mmsp2.mlc_stl_adrl;
537 break;
538 case 0x2910:
539 case 0x2914:
540 d = mmsp2.mlc_stl_adrh;
541 break;
6720e4e4 542 case 0x2958:
543 d = mmsp2.mlc_stl_pallt_a;
544 break;
7000b522 545
6720e4e4 546 default:
7000b522 547 d = xread32_io_cmn(a_, &t);
548 if (!t)
549 goto unk;
550 if (!(a_ & 2))
551 d >>= 16;
552 break;
6720e4e4 553 }
554 goto out;
555 }
556
7000b522 557unk:
6720e4e4 558 if (a == old_a) {
559 d = fudge;
560 fudge = ~fudge;
561 }
562 old_a = a;
563 iolog_unh("r16", a, d & 0xffff, 16);
564
565out:
566 d &= 0xffff;
567 iolog("r16", a, d, 16);
568 return d;
11913091 569}
570
571static u32 xread32(u32 a)
572{
3d295a9f 573 u32 d = 0;
dc90c02d 574 if ((a & 0xfff00000) == 0x7f000000) {
575 u32 a_ = a & 0xffff;
3d3fa63b 576 struct timespec ts;
577 u64 t64;
7000b522 578 u32 t;
3d3fa63b 579
dc90c02d 580 switch (a_) {
581 case 0x0a00: // TCOUNT, 1/7372800s
d0b9b0df 582 g_clock_gettime_raw(CLOCK_REALTIME, &ts);
3d3fa63b 583 t64 = (u64)ts.tv_sec * 1000000000 + ts.tv_nsec;
584 // t * 7372800.0 / 1000000000 * 0x100000000 ~= t * 31665935
585 t64 *= 31665935;
586 d = t64 >> 32;
dc90c02d 587 break;
7000b522 588
589 default:
590 d = xread32_io_cmn(a_, &t);
591 if (!t)
592 goto unh;
593 break;
dc90c02d 594 }
7000b522 595 goto out;
dc90c02d 596 }
3d295a9f 597 if ((a & 0xfff00000) == 0x7f100000) {
598 u32 *bl = &blitter.dstctrl;
6720e4e4 599 u32 a_ = a & 0xfff;
600 if (a_ < 0x40) {
601 d = bl[a_ / 4];
602 if (a_ == 0x34)
603 d = 0; // not busy
604 goto out;
605 }
3d295a9f 606 }
7000b522 607
608unh:
6720e4e4 609 iolog_unh("r32", a, d, 32);
610
611out:
612 iolog("r32", a, d, 32);
3d295a9f 613 return d;
11913091 614}
615
616static void xwrite8(u32 a, u32 d)
617{
6720e4e4 618 iolog("w8 ", a, d, 8);
619 iolog_unh("w8 ", a, d, 8);
11913091 620}
621
622static void xwrite16(u32 a, u32 d)
623{
6720e4e4 624 iolog("w16", a, d, 16);
3d295a9f 625 if ((a & 0xfff00000) == 0x7f000000) {
6720e4e4 626 u32 a_ = a & 0xffff;
627 switch (a_) {
7000b522 628 case 0x28da: {
629 int mode;
630 mmsp2.mlc_stl_cntl = d | 0xaa;
631 mode = (d >> 9) & 3;
632 mmsp2.v.bpp = mode ? mode * 8 : 4;
633 break;
634 }
635 case 0x290c:
636 mmsp2.v.stride = d;
637 return;
638 case 0x290e:
639 case 0x2910:
640 // odd addresses don't affect LCD. What about TV?
641 return;
642 case 0x2912:
643 mmsp2.mlc_stl_adrl = d;
644 return;
645 case 0x2914:
646 mmsp2.mlc_stl_adrh = d;
d0b9b0df 647 if (mmsp2.mlc_stl_adr != mmsp2.old_mlc_stl_adr) {
7000b522 648 // ask for refresh
d0b9b0df 649 fb_sync_thread_futex = 1;
650 g_futex_raw(&fb_sync_thread_futex, FUTEX_WAKE, 1, NULL);
651 }
7000b522 652 mmsp2.old_mlc_stl_adr = mmsp2.mlc_stl_adr;
653 return;
f01759de 654 case 0x2958: // MLC_STL_PALLT_A
7000b522 655 mmsp2.mlc_stl_pallt_a = d & 0x1ff;
656 return;
f01759de 657 case 0x295a: // MLC_STL_PALLT_D
7000b522 658 mmsp2.mlc_stl_pallt_d[mmsp2.mlc_stl_pallt_a++] = d;
659 mmsp2.mlc_stl_pallt_a &= 0x1ff;
660 mmsp2.v.dirty_pal = DIRTY_PAL_MMSP2;
661 return;
3d295a9f 662 }
3d295a9f 663 }
6720e4e4 664 iolog_unh("w16", a, d, 16);
11913091 665}
666
667static void xwrite32(u32 a, u32 d)
668{
6720e4e4 669 iolog("w32", a, d, 32);
670
7000b522 671 if ((a & 0xfff00000) == 0x7f000000) {
672 u32 a_ = a & 0xffff;
673 switch (a_) {
f01759de 674 // GP2X
675 case 0x295a: // MLC_STL_PALLT_D
676 // special unaligned 32bit write, allegro seems to rely on it
677 mmsp2.mlc_stl_pallt_d[mmsp2.mlc_stl_pallt_a++ & 0x1ff] = d;
678 mmsp2.mlc_stl_pallt_d[mmsp2.mlc_stl_pallt_a++ & 0x1ff] = d >> 16;
679 mmsp2.mlc_stl_pallt_a &= 0x1ff;
680 mmsp2.v.dirty_pal = DIRTY_PAL_MMSP2;
681 return;
7000b522 682 // Wiz
683 case 0x4024: // MLCCONTROL0
684 case 0x4058: // MLCCONTROL1
685 pollux.mlccontrol = d;
686 if (!(d & 0x20))
687 return; // layer not enabled
688 if ((d >> 16) == 0x443A)
689 pollux.v.bpp = 8;
690 else
691 pollux.v.bpp = 16;
692 return;
693 case 0x402c: // MLCVSTRIDE0
694 case 0x4060: // MLCVSTRIDE1
695 pollux.v.stride = d;
696 return;
697 case 0x4038: // MLCADDRESS0
698 case 0x406c: // MLCADDRESS1
699 pollux.mlc_stl_adr = d;
d0b9b0df 700 if (d != mmsp2.old_mlc_stl_adr) {
7000b522 701 // ask for refresh
d0b9b0df 702 fb_sync_thread_futex = 1;
703 g_futex_raw(&fb_sync_thread_futex, FUTEX_WAKE, 1, NULL);
704 }
7000b522 705 mmsp2.old_mlc_stl_adr = d;
706 return;
707 case 0x403c: // MLCPALETTE0
708 case 0x4070: // MLCPALETTE1
709 pollux.mlcpalette[d >> 24] = d;
710 pollux.v.dirty_pal = DIRTY_PAL_POLLUX;
711 return;
712 }
713 }
3d295a9f 714 if ((a & 0xfff00000) == 0x7f100000) {
715 u32 *bl = &blitter.dstctrl;
6720e4e4 716 u32 a_ = a & 0xfff;
717 if (a_ < 0x40) {
718 bl[a_ / 4] = d;
719 if (a_ == 0x34 && (d & 1))
720 blitter_do();
721 return;
722 }
3d295a9f 723 }
6720e4e4 724 iolog_unh("w32", a, d, 32);
11913091 725}
726
6720e4e4 727#define LINKPAGE_SIZE 0x1000
728#define LINKPAGE_COUNT 4
729#define LINKPAGE_ALLOC (LINKPAGE_SIZE * LINKPAGE_COUNT)
730
731struct op_context {
732 u32 pc;
733 u32 op;
734 u32 code[0];
735};
736
3d3fa63b 737struct op_linkpage {
890c1dc5 738 u32 *code_ptr;
6720e4e4 739 void (*handler)(struct op_context *op_ctx);
740 u32 code[0];
741};
742
3d3fa63b 743struct op_stackframe {
744 u32 saved_regs[15];
745 u32 cpsr;
746};
747
890c1dc5 748static struct op_linkpage *g_linkpages[2];
6720e4e4 749static int g_linkpage_count;
750
10c900a1 751enum opcond {
752 C_EQ, C_NE, C_CS, C_CC, C_MI, C_PL, C_VS, C_VC,
753 C_HI, C_LS, C_GE, C_LT, C_GT, C_LE, C_AL,
754};
755enum cpsr_cond {
756 CPSR_N = (1u << 31),
757 CPSR_Z = (1u << 30),
758 CPSR_C = (1u << 29),
759 CPSR_V = (1u << 28),
760};
761
11913091 762#define BIT_SET(v, b) (v & (1 << (b)))
763
3d3fa63b 764void emu_handle_op(struct op_context *op_ctx, struct op_stackframe *sframe)
11913091 765{
3d3fa63b 766 u32 *regs = sframe->saved_regs;
10c900a1 767 u32 cpsr = sframe->cpsr;
6720e4e4 768 u32 op = op_ctx->op;
11913091 769 u32 t, shift, ret, addr;
10c900a1 770 int i, rn, rd, cond;
11913091 771
fea1465c 772 cond = (op & 0xf0000000) >> 28;
11913091 773 rd = (op & 0x0000f000) >> 12;
774 rn = (op & 0x000f0000) >> 16;
775
10c900a1 776 if (cond != 0x0e) {
777 switch (cond) {
778 case C_EQ: if ( (cpsr & CPSR_Z)) break; return;
779 case C_NE: if (!(cpsr & CPSR_Z)) break; return;
780 case C_CS: if ( (cpsr & CPSR_C)) break; return;
781 case C_CC: if (!(cpsr & CPSR_C)) break; return;
782 case C_MI: if ( (cpsr & CPSR_N)) break; return;
783 case C_PL: if (!(cpsr & CPSR_N)) break; return;
784 case C_VS: if ( (cpsr & CPSR_V)) break; return;
785 case C_VC: if (!(cpsr & CPSR_V)) break; return;
786 default:
787 goto unhandled;
788 }
789 }
fea1465c 790
11913091 791 if ((op & 0x0f200090) == 0x01000090) { // AM3: LDRH, STRH
86418a84 792 if (!BIT_SET(op, 5)) // !H
793 goto unhandled;
794 if (BIT_SET(op, 6) && !BIT_SET(op, 20)) // S && !L
11913091 795 goto unhandled;
796
797 if (BIT_SET(op, 22)) // imm offset
798 t = ((op & 0xf00) >> 4) | (op & 0x0f);
799 else // reg offset
800 t = regs[op & 0x000f];
801
802 if (!BIT_SET(op, 23))
803 t = -t;
804 addr = regs[rn] + t;
805
806 if (BIT_SET(op, 20)) { // Load
807 ret = xread16(addr);
86418a84 808 if (BIT_SET(op, 6)) { // S
809 ret <<= 16;
810 ret = (signed int)ret >> 16;
811 }
11913091 812 regs[rd] = ret;
813 }
814 else
815 xwrite16(addr, regs[rd]);
816 }
fea1465c 817 else if ((op & 0x0c000000) == 0x04000000) { // load/store word/byte
818 if (BIT_SET(op, 21))
819 goto unhandled; // unprivileged
11913091 820 if (BIT_SET(op, 25)) { // reg offs
821 if (BIT_SET(op, 4))
fea1465c 822 goto unhandled; // nah it's media
11913091 823
824 t = regs[op & 0x000f];
825 shift = (op & 0x0f80) >> 7;
826 switch ((op & 0x0060) >> 5) {
827 case 0: t = t << shift; break;
828 case 1: t = t >> (shift + 1); break;
829 case 2: t = (signed int)t >> (shift + 1); break;
830 case 3: goto unhandled; // I'm just lazy
831 }
832 }
833 else // imm offs
834 t = op & 0x0fff;
835
836 if (!BIT_SET(op, 23))
837 t = -t;
fea1465c 838
839 addr = regs[rn];
840 if (BIT_SET(op, 24)) // pre-indexed
841 addr += t;
842 if (!BIT_SET(op, 24) || BIT_SET(op, 21))
843 regs[rn] += t; // writeback
11913091 844
845 if (BIT_SET(op, 20)) { // Load
846 if (BIT_SET(op, 22)) // Byte
847 ret = xread8(addr);
848 else
849 ret = xread32(addr);
850 regs[rd] = ret;
851 }
852 else {
853 if (BIT_SET(op, 22)) // Byte
854 xwrite8(addr, regs[rd]);
855 else
856 xwrite32(addr, regs[rd]);
857 }
858 }
859 else
860 goto unhandled;
861
86418a84 862#if 0
11913091 863 if (addr != addr_check) {
864 fprintf(stderr, "bad calculated addr: %08x vs %08x\n", addr, addr_check);
865 abort();
866 }
86418a84 867#endif
11913091 868 return;
869
870unhandled:
6720e4e4 871 err("unhandled IO op %08x @ %08x\n", op, op_ctx->pc);
10c900a1 872 for (i = 0; i < 8-1; i++)
873 err(" r%d=%08x r%-2d=%08x\n", i, regs[i], i+8, regs[i+8]);
874 err(" r%d=%08x cpsr=%08x\n", i, regs[i], cpsr);
fea1465c 875 abort();
86418a84 876}
877
878static u32 make_offset12(u32 *pc, u32 *target)
879{
880 int lp_offs, u = 1;
881
882 lp_offs = (char *)target - (char *)pc - 2*4;
883 if (lp_offs < 0) {
884 lp_offs = -lp_offs;
885 u = 0;
886 }
887 if (lp_offs >= LINKPAGE_SIZE) {
6720e4e4 888 err("linkpage too far: %d\n", lp_offs);
86418a84 889 abort();
890 }
891
892 return (u << 23) | lp_offs;
893}
894
6720e4e4 895static u32 make_jmp(u32 *pc, u32 *target, int bl)
86418a84 896{
897 int jmp_val;
898
899 jmp_val = target - pc - 2;
890c1dc5 900 if (jmp_val < (int)0xff800000 || jmp_val > 0x007fffff) {
6720e4e4 901 err("jump out of range (%p -> %p)\n", pc, target);
86418a84 902 abort();
903 }
904
6720e4e4 905 return 0xea000000 | (bl << 24) | (jmp_val & 0x00ffffff);
86418a84 906}
907
890c1dc5 908static void emit_op(struct op_linkpage *linkpage, u32 op)
86418a84 909{
890c1dc5 910 *linkpage->code_ptr++ = op;
86418a84 911}
912
890c1dc5 913static void emit_op_io(struct op_linkpage *linkpage,
914 u32 op, u32 *target)
86418a84 915{
890c1dc5 916 op |= make_offset12(linkpage->code_ptr, target);
917 emit_op(linkpage, op);
86418a84 918}
919
890c1dc5 920static void init_linkpage(struct op_linkpage *linkpage)
6720e4e4 921{
890c1dc5 922 linkpage->handler = emu_call_handle_op;
923 linkpage->code_ptr = linkpage->code;
6720e4e4 924}
925
86418a84 926static void segv_sigaction(int num, siginfo_t *info, void *ctx)
927{
6c888740 928 extern char _init, _end;
86418a84 929 struct ucontext *context = ctx;
930 u32 *regs = (u32 *)&context->uc_mcontext.arm_r0;
931 u32 *pc = (u32 *)regs[15];
6c888740 932 u32 self_start, self_end;
890c1dc5 933 struct op_linkpage *lp = NULL;
6720e4e4 934 struct op_context *op_ctx;
890c1dc5 935 int i, ret, lp_i, lp_size;
86418a84 936
6c888740 937 self_start = (u32)&_init & ~0xfff;
938 self_end = (u32)&_end;
939
890c1dc5 940 if ((self_start <= regs[15] && regs[15] <= self_end) || // PC is in our segment or
941 !((regs[15] ^ (u32)g_linkpages[0]) & ~(LINKPAGE_ALLOC - 1)) || // .. in linkpage
942 ((long)info->si_addr & 0xffe00000) != 0x7f000000) // faulting not where expected
86418a84 943 {
944 // real crash - time to die
6720e4e4 945 err("segv %d %p @ %08x\n", info->si_code, info->si_addr, regs[15]);
7fd42181 946 for (i = 0; i < 8; i++)
10c900a1 947 dbg(" r%d=%08x r%-2d=%08x\n", i, regs[i], i+8, regs[i+8]);
86418a84 948 signal(num, SIG_DFL);
949 raise(num);
c1ffd5ba 950 return;
86418a84 951 }
0881206b 952 segvlog("segv %d %p @ %08x\n", info->si_code, info->si_addr, regs[15]);
86418a84 953
890c1dc5 954 // find nearby linkpage
955 for (lp_i = 0; lp_i < ARRAY_SIZE(g_linkpages); lp_i++) {
956 if (g_linkpages[lp_i] == NULL)
957 continue;
958 i = g_linkpages[lp_i]->code_ptr + 2 - pc - 2;
959 if ((int)0xff800000 <= i && i <= 0x007fffff) {
960 lp = g_linkpages[lp_i];
961 break;
962 }
963 }
964
965 if (lp == NULL) {
966 err("fatal: no nearby linkpage for %08x\n", regs[15]);
967 abort();
968 }
969
970 if (emu_is_dl) {
971 ret = mprotect((void *)((long)pc & ~0xfff), 0x1000, PROT_READ|PROT_WRITE|PROT_EXEC);
972 if (ret != 0)
973 perror("warning: mprotect");
974 }
975
86418a84 976 // spit PC and op
890c1dc5 977 op_ctx = (void *)lp->code_ptr;
6720e4e4 978 op_ctx->pc = (u32)pc;
979 op_ctx->op = *pc;
890c1dc5 980 lp->code_ptr = &op_ctx->code[0];
86418a84 981
982 // emit jump to code ptr
890c1dc5 983 *pc = make_jmp(pc, lp->code_ptr, 0);
86418a84 984
985 // generate code:
890c1dc5 986 emit_op (lp, 0xe50d0000 + 0xf00 - 4 * 0); // str r0, [sp, #(-0xf00 + r0_offs)]
987 emit_op (lp, 0xe50de000 + 0xf00 - 4 * 14); // str lr, [sp, #(-0xf00 + lr_offs)]
988 emit_op (lp, 0xe24f0000 + (lp->code_ptr - (u32 *)op_ctx + 2) * 4); // sub r0, pc, #op_ctx
989 emit_op (lp, 0xe1a0e00f); // mov lr, pc
990 emit_op_io(lp, 0xe51ff000, (u32 *)&lp->handler); // ldr pc, =handle_op
991 emit_op (lp, 0xe51de000 + 0xf00 - 4 * 14); // ldr lr, [sp, #(-0xf00 + lr_offs)]
992 emit_op (lp, make_jmp(lp->code_ptr, pc + 1, 0)); // jmp <back>
86418a84 993
994 // sync caches
995 sys_cacheflush(pc, pc + 1);
890c1dc5 996 sys_cacheflush(lp, lp->code_ptr);
86418a84 997
890c1dc5 998 lp_size = (char *)lp->code_ptr - (char *)lp;
0881206b 999 segvlog("code #%d %d/%d\n", g_linkpage_count, lp_size, LINKPAGE_SIZE);
86418a84 1000
890c1dc5 1001 if (lp_size + 14*4 > LINKPAGE_SIZE) {
86418a84 1002 g_linkpage_count++;
1003 if (g_linkpage_count >= LINKPAGE_COUNT) {
6720e4e4 1004 err("too many linkpages needed\n");
86418a84 1005 abort();
1006 }
890c1dc5 1007 g_linkpages[lp_i] = (void *)((char *)g_linkpages[lp_i] + LINKPAGE_SIZE);
1008 init_linkpage(g_linkpages[lp_i]);
86418a84 1009 }
1010 //handle_op(regs[15], op, regs, (u32)info->si_addr);
1011 //regs[15] += 4;
1012}
11913091 1013
890c1dc5 1014void emu_init(void *map_bottom[2], int is_dl)
11913091 1015{
7fd42181 1016 sigaction_t segv_action = {
11913091 1017 .sa_sigaction = segv_sigaction,
1018 .sa_flags = SA_SIGINFO,
1019 };
6720e4e4 1020 void *pret;
890c1dc5 1021 int i, ret;
11913091 1022
830ba1bf 1023#ifdef PND
1024 if (geteuid() == 0) {
b4f4cb40 1025 err("don't try to run as root, device registers or memory "
1026 "might get trashed crashing the OS or even damaging the device.\n");
830ba1bf 1027 exit(1);
1028 }
1029#endif
1030
890c1dc5 1031 emu_is_dl = is_dl;
1032
1033 for (i = 0; i < 2; i++) {
1034 if (map_bottom[i] == NULL)
1035 continue;
1036 g_linkpages[i] = (void *)(((u32)map_bottom[i] - LINKPAGE_ALLOC) & ~0xfff);
1037 pret = mmap(g_linkpages[i], LINKPAGE_ALLOC, PROT_READ|PROT_WRITE,
1038 MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0);
1039 if (pret != g_linkpages[i]) {
1040 err("linkpage alloc @ %p: ", g_linkpages[i]);
1041 perror(NULL);
1042 exit(1);
1043 }
1044 log("linkpages @ %p\n", g_linkpages[i]);
1045 init_linkpage(g_linkpages[i]);
11913091 1046 }
11913091 1047
4d045184 1048 // host stuff
1049 ret = host_init();
1050 if (ret != 0) {
1051 err("can't init host\n");
3d3fa63b 1052 exit(1);
1053 }
1054
4d045184 1055 ret = host_video_init(NULL, 0);
2ce69bdf 1056 if (ret != 0) {
4d045184 1057 err("can't init host video\n");
1058 exit(1);
1059 }
1060
5853ddbd 1061 // TODO: check if this really fails on Wiz..
4d045184 1062 mmsp2.umem = mmap(NULL, 0x2000000, PROT_READ|PROT_WRITE|PROT_EXEC,
1063 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
5853ddbd 1064#ifdef WIZ
1065 if (mmsp2.umem == MAP_FAILED) {
1066 // we are short on memmory on Wiz, need special handling
1067 extern void *host_mmap_upper(void);
1068 mmsp2.umem = host_mmap_upper();
1069 }
4d045184 1070#endif
1071 if (mmsp2.umem == MAP_FAILED) {
1072 perror(PFX "mmap upper mem");
3d295a9f 1073 exit(1);
1074 }
c1ffd5ba 1075
d0b9b0df 1076 pret = mmap(NULL, THREAD_STACK_SIZE, PROT_READ|PROT_WRITE|PROT_EXEC,
1077 MAP_PRIVATE|MAP_ANONYMOUS|MAP_GROWSDOWN, -1, 0);
1078 if (mmsp2.umem == MAP_FAILED) {
1079 perror(PFX "mmap thread stack");
1080 exit(1);
1081 }
1082 fb_sync_thread_futex = 1;
1083 ret = g_clone(CLONE_VM | CLONE_FS | CLONE_FILES
1084 | CLONE_SIGHAND | CLONE_THREAD,
1085 (char *)pret + THREAD_STACK_SIZE, 0, 0, 0,
1086 fb_sync_thread);
1087 if (ret == 0 || ret == -1) {
1088 perror(PFX "start fb thread");
c1ffd5ba 1089 exit(1);
1090 }
d0b9b0df 1091 g_futex_raw(&fb_sync_thread_futex, FUTEX_WAIT, 1, NULL);
c1ffd5ba 1092
7000b522 1093 // defaults
c1ffd5ba 1094 mmsp2.mlc_stl_adr = 0x03101000; // fb2 is at 0x03381000
7fd42181 1095 mmsp2.mlc_stl_cntl = 0x4ab; // 16bpp, region 1 active
7000b522 1096 mmsp2.v.width = 320;
1097 mmsp2.v.height = 240;
1098 mmsp2.v.stride = 320*2;
1099 mmsp2.v.bpp = 16;
1100 mmsp2.v.dirty_pal = 1;
c1ffd5ba 1101
1102 sigemptyset(&segv_action.sa_mask);
1103 sigaction(SIGSEGV, &segv_action, NULL);
3d295a9f 1104}
1105
b4f4cb40 1106static long emu_mmap_dev(unsigned int length, int prot, int flags, unsigned int offset)
11913091 1107{
3d3fa63b 1108 u8 *umem, *umem_end;
11913091 1109
3d295a9f 1110 // SoC regs
7000b522 1111 if ((offset & ~0x1ffff) == 0xc0000000) {
b4f4cb40 1112 return g_mmap2_raw((void *)0x7f000000, length, PROT_NONE,
11913091 1113 MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED|MAP_NORESERVE, -1, 0);
1114 }
7000b522 1115 // MMSP2 blitter
11913091 1116 if ((offset & ~0xffff) == 0xe0020000) {
b4f4cb40 1117 return g_mmap2_raw((void *)0x7f100000, length, PROT_NONE,
11913091 1118 MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED|MAP_NORESERVE, -1, 0);
1119 }
3d295a9f 1120 // upper mem
3d3fa63b 1121 if ((offset & 0xfe000000) != 0x02000000) {
6720e4e4 1122 err("unexpected devmem mmap @ %08x\n", offset);
b4f4cb40 1123 return -EINVAL;
3d295a9f 1124 }
1125
3d3fa63b 1126 umem = uppermem_lookup(offset, &umem_end);
1127 if (umem + length > umem_end)
1128 err("warning: uppermem @ %08x overflows by %d bytes\n",
1129 offset, umem + length - umem_end);
1130
3ef07128 1131 dbg("upper mem @ %08x %x = %p\n", offset, length, umem);
b4f4cb40 1132 return (long)umem;
11913091 1133}
1134
b4f4cb40 1135long emu_do_mmap(unsigned int length, int prot, int flags, int fd,
1136 unsigned int offset)
dc90c02d 1137{
1138 if (fd == FAKEDEV_MEM)
1139 return emu_mmap_dev(length, prot, flags, offset);
1140
1141 if (fd == FAKEDEV_FB0)
1142 return emu_mmap_dev(length, prot, flags, offset + 0x03101000);
1143
1144 if (fd == FAKEDEV_FB1)
1145 return emu_mmap_dev(length, prot, flags, offset + 0x03381000);
1146
7fd42181 1147 err("bad/ni mmap(?, %d, %x, %x, %d, %08x)\n", length, prot, flags, fd, offset);
b4f4cb40 1148 return -EINVAL;
dc90c02d 1149}
1150
b4f4cb40 1151long emu_do_munmap(void *addr, unsigned int length)
3ef07128 1152{
1153 u8 *p = addr;
1154
1155 // don't allow to unmap upper mem
1156 if ((u8 *)mmsp2.umem <= p && p < (u8 *)mmsp2.umem + 0x2000000) {
1157 dbg("ignoring munmap: %p %x\n", addr, length);
1158 return 0;
1159 }
1160
1161 return -EAGAIN;
1162}
1163
7000b522 1164static void emu_sound_open(int fd)
1165{
1166#ifdef PND
1167 int ret, frag;
1168
1169 // set default buffer size to 16 * 1K
1170 frag = (16<<16) | 10; // 16K
b4f4cb40 1171 ret = g_ioctl_raw(fd, SNDCTL_DSP_SETFRAGMENT, &frag);
1172 if (ret != 0)
1173 err("snd ioctl SETFRAGMENT %08x: %d\n", frag, ret);
7000b522 1174#endif
1175}
1176
b4f4cb40 1177static long emu_sound_ioctl(int fd, int request, void *argp)
dc90c02d 1178{
1179 int *arg = argp;
1180
1181#if 0
1182 dbg("snd ioctl(%d, %08x, %p)", fd, request, argp);
1183 if (arg != NULL)
1184 dbg_c(" [%d]", *arg);
1185 dbg_c("\n");
1186#endif
1187
1188 /* People set strange frag settings on GP2X, which even manage
1189 * to break audio on pandora (causes writes to fail).
1190 * Catch this and set to something that works. */
7000b522 1191 switch(request) {
1192 case SNDCTL_DSP_SETFRAGMENT: {
b4f4cb40 1193 int bsize, frag, frag_cnt;
1194 long ret;
1195
7000b522 1196 if (arg == NULL)
1197 break;
dc90c02d 1198
7000b522 1199 frag = *arg & 0xffff;
1200 frag_cnt = *arg >> 16;
1201 bsize = frag_cnt << frag;
1202 if (frag < 10 || bsize < 4096*4 || bsize > 4096*4*2) {
1203 /*
1204 * ~4ms. gpSP wants small buffers or else it stutters
1205 * because of it's audio thread sync stuff
1206 * XXX: hardcoding, as low samplerates will result in small fragment size,
1207 * which itself causes ALSA stall and hangs the program.
1208 * Also some apps change samplerate without reopening /dev/dsp,
1209 * which causes ALSA to reject SNDCTL_DSP_SETFRAGMENT.
1210 */
1211 bsize = 44100 / 250 * 4;
1212
1213 for (frag = 0; bsize; bsize >>= 1, frag++)
1214 ;
1215
1216 frag_cnt = 16;
1217 }
dc90c02d 1218
7000b522 1219 frag |= frag_cnt << 16;
b4f4cb40 1220 ret = g_ioctl_raw(fd, SNDCTL_DSP_SETFRAGMENT, &frag);
1221 if (ret != 0)
1222 err("snd ioctl SETFRAGMENT %08x: %ld\n", frag, ret);
7000b522 1223 // indicate success even if we fail (because of ALSA mostly),
1224 // things like MikMod will bail out otherwise.
1225 return 0;
dc90c02d 1226 }
7000b522 1227 case SNDCTL_DSP_SYNC:
1228 // Franxis tends to use sync/write loops, bad idea under ALSA
1229 return 0;
1230 default:
1231 break;
dc90c02d 1232 }
dc90c02d 1233
b4f4cb40 1234 return g_ioctl_raw(fd, request, argp);
dc90c02d 1235}
1236
b4f4cb40 1237long emu_do_ioctl(int fd, int request, void *argp)
dc90c02d 1238{
1239 if (fd == emu_interesting_fds[IFD_SOUND].fd)
1240 return emu_sound_ioctl(fd, request, argp);
1241
7fd42181 1242 switch (fd) {
1243 /* *********************** */
1244 case FAKEDEV_FB0:
1245 case FAKEDEV_FB1:
1246 if (argp == NULL)
1247 goto fail;
dc90c02d 1248
dc90c02d 1249 switch (request) {
1250 case FBIOGET_FSCREENINFO: {
1251 struct fb_fix_screeninfo *fix = argp;
1252
1253 memset(fix, 0, sizeof(*fix));
1254 strcpy(fix->id, "mmsp2_RGB0");
1255 fix->type = FB_TYPE_PACKED_PIXELS;
1256 fix->accel = FB_ACCEL_NONE;
7fd42181 1257 fix->visual = FB_VISUAL_TRUECOLOR;
1258 fix->line_length = 320*2;
dc90c02d 1259 fix->smem_start = (fd == FAKEDEV_FB0) ? 0x03101000 : 0x03381000;
1260 fix->smem_len = 320*240*2;
1261 return 0;
1262 }
1263 case FBIOGET_VSCREENINFO: {
1264 struct fb_var_screeninfo *var = argp;
7fd42181 1265 static const struct fb_bitfield fbb_red = { offset: 11, length: 5, };
1266 static const struct fb_bitfield fbb_green = { offset: 5, length: 6, };
1267 static const struct fb_bitfield fbb_blue = { offset: 0, length: 5, };
dc90c02d 1268
1269 memset(var, 0, sizeof(*var));
1270 var->activate = FB_ACTIVATE_NOW;
1271 var->xres =
1272 var->xres_virtual = 320;
1273 var->yres =
1274 var->yres_virtual = 240;
1275 var->width =
1276 var->height = -1;
1277 var->vmode = FB_VMODE_NONINTERLACED;
1278 var->bits_per_pixel = 16;
1279 var->red = fbb_red;
1280 var->green = fbb_green;
1281 var->blue = fbb_blue;
1282 return 0;
1283 }
7fd42181 1284 case FBIOPUT_VSCREENINFO: {
1285 struct fb_var_screeninfo *var = argp;
1286 dbg(" put vscreen: %dx%d@%d\n", var->xres, var->yres, var->bits_per_pixel);
1287 if (var->xres != 320 || var->yres != 240 || var->bits_per_pixel != 16)
1288 return -1;
1289 return 0;
1290 }
dc90c02d 1291 }
7fd42181 1292
1293 /* *********************** */
cbd18fd2 1294 case FAKEDEV_TTY:
7fd42181 1295 case FAKEDEV_TTY0:
1296 // fake tty0 to make GPH SDL happy
1297 if (request == 0x4b46) // KDGKBENT
1298 return -1;
1299 return 0;
dc90c02d 1300 }
1301
1302fail:
7fd42181 1303 err("bad/ni ioctl(%d, %08x, %p)\n", fd, request, argp);
b4f4cb40 1304 return -EINVAL;
dc90c02d 1305}
1306
8cbb0278 1307static const char wm97xx_p[] =
1308 "5507 0 -831476 0 -4218 16450692 65536"; // from 4.0 fw
1309
88d814e3 1310long emu_do_read(int fd, void *buf, int count)
1311{
88d814e3 1312 int ret, pressed = 0, x, y;
1313 struct {
1314 u16 pressure, x, y;
1315 } wm97xx;
1316
1317 if (count < 0) {
1318 err("read(%d, %d)\n", fd, count);
1319 return -EINVAL;
1320 }
1321
1322 switch (fd) {
1323 case FAKEDEV_GPIO:
1324 mmsp2.btn_state = host_read_btns();
1325
1326 if (count > 4)
1327 count = 4;
1328 memcpy(buf, &mmsp2.btn_state, count);
1329 break;
1330 case FAKEDEV_WM97XX:
1331 ret = host_read_ts(&pressed, &x, &y);
1332 if (ret == 0 && pressed) {
8cbb0278 1333 wm97xx.pressure = 0x8001; // TODO: check the real thing
88d814e3 1334 wm97xx.x = x * 3750 / 1024 + 200;
1335 wm97xx.y = 3750 - y * 3750 / 1024 + 200;
1336 }
1337 else {
1338 wm97xx.pressure = 0;
1339 wm97xx.x = wm97xx.y = 200;
1340 }
1341
1342 if (count > sizeof(wm97xx))
1343 count = sizeof(wm97xx);
1344 memcpy(buf, &wm97xx, count);
1345 break;
1346 case FAKEDEV_WM97XX_P:
1347 if (count < sizeof(wm97xx_p))
1348 err("incomplete pointercal read\n");
1349 strncpy(buf, wm97xx_p, count);
1350 break;
1351 default:
8cbb0278 1352 dbg("read(%d, %d)\n", fd, count);
88d814e3 1353 return -EINVAL;
1354 }
1355 return count;
1356}
1357
7000b522 1358struct dev_fd_t emu_interesting_fds[] = {
1359 [IFD_SOUND] = { "/dev/dsp", -1, emu_sound_open },
1360 { NULL, 0, NULL },
1361};
1362
7fd42181 1363static const struct {
1364 const char *from;
1365 const char *to;
1366} path_map[] = {
158ddd8d 1367 { "/mnt/tmp", "./tmp" },
2692251d 1368 { "/mnt/sd", "./mntsd" },
7fd42181 1369};
1370
2692251d 1371const char *emu_wrap_path(const char *path)
7fd42181 1372{
158ddd8d 1373 char *buff, *p;
3d3fa63b 1374 size_t size;
7fd42181 1375 int i, len;
2692251d 1376 long ret;
7fd42181 1377
1378 // do only path mapping for now
1379 for (i = 0; i < ARRAY_SIZE(path_map); i++) {
158ddd8d 1380 p = strstr(path, path_map[i].from);
1381 if (p != NULL) {
3d3fa63b 1382 size = strlen(path) + strlen(path_map[i].to) + 1;
1383 buff = malloc(size);
1384 if (buff == NULL)
1385 break;
158ddd8d 1386 len = p - path;
1387 strncpy(buff, path, len);
1388 snprintf(buff + len, size - len, "%s%s", path_map[i].to,
1389 path + len + strlen(path_map[i].from));
3d3fa63b 1390 dbg("mapped path \"%s\" -> \"%s\"\n", path, buff);
2692251d 1391
1392 ret = g_mkdir_raw(path_map[i].to, 0666);
1393 if (ret != 0 && ret != -EEXIST)
1394 err("mkdir(%s): %ld\n", path_map[i].to, ret);
1395
3d3fa63b 1396 return buff;
7fd42181 1397 }
1398 }
1399
1400 return path;
1401}
1402
2692251d 1403void emu_wrap_path_free(const char *w_path, const char *old_path)
7000b522 1404{
1405 if (w_path != old_path)
1406 free((void *)w_path);
1407}
1408
7fd42181 1409void *emu_do_fopen(const char *path, const char *mode)
1410{
7000b522 1411 const char *w_path;
3d3fa63b 1412 FILE *ret;
7000b522 1413
8cbb0278 1414 if (strcmp(path, "/etc/pointercal") == 0) {
1415 // use local pontercal, not host's
1416 ret = fopen("pointercal", mode);
1417 if (ret == NULL) {
1418 ret = fopen("pointercal", "w");
1419 if (ret != NULL) {
1420 fwrite(wm97xx_p, 1, sizeof(wm97xx_p), ret);
1421 fclose(ret);
1422 }
1423 ret = fopen("pointercal", mode);
1424 }
1425 }
1426 else {
1427 w_path = emu_wrap_path(path);
1428 ret = fopen(w_path, mode);
1429 emu_wrap_path_free(w_path, path);
1430 }
7000b522 1431
3d3fa63b 1432 return ret;
7fd42181 1433}
1434
3d3fa63b 1435// FIXME: threads..
7fd42181 1436int emu_do_system(const char *command)
1437{
1438 static char tmp_path[512];
158ddd8d 1439 int need_ginge = 0;
3d3fa63b 1440 const char *p2;
1441 char *p;
db9191ed 1442 int ret;
7fd42181 1443
1444 if (command == NULL)
1445 return -1;
1446
158ddd8d 1447 for (p2 = command; *p2 && isspace(*p2); p2++)
1448 ;
7fd42181 1449
158ddd8d 1450 if (*p2 == '.') // relative path?
1451 need_ginge = 1;
1452 else if (*p2 == '/' && strncmp(p2, "/bin", 4) && strncmp(p2, "/lib", 4)
1453 && strncmp(p2, "/sbin", 4) && strncmp(p2, "/usr", 4))
1454 // absolute path, but not a system command
1455 need_ginge = 1;
7fd42181 1456
2692251d 1457 p2 = emu_wrap_path(command);
158ddd8d 1458 if (need_ginge) {
1459 make_local_path(tmp_path, sizeof(tmp_path), "ginge_prep");
1460 p = tmp_path + strlen(tmp_path);
1461
1462 snprintf(p, sizeof(tmp_path) - (p - tmp_path), " --nomenu %s", p2);
1463 }
1464 else
1465 snprintf(tmp_path, sizeof(tmp_path), "%s", p2);
2692251d 1466 emu_wrap_path_free(p2, command);
3d3fa63b 1467
7fd42181 1468 dbg("system: \"%s\"\n", tmp_path);
db9191ed 1469
1470 // the app might want the screen too..
1471 fb_thread_pause();
1472 ret = system(tmp_path);
1473 fb_thread_resume();
1474 return ret;
7fd42181 1475}
1476
78684356 1477long emu_do_execve(const char *filename, char * const argv[],
1478 char * const envp[])
7000b522 1479{
1480 const char **new_argv;
1481 char *prep_path;
78684356 1482 int i, argc;
1483 long ret;
7000b522 1484
1485 if (filename == NULL)
1486 return -1;
1487
78684356 1488 if (strstr(filename, "gp2xmenu") != NULL)
1489 host_forced_exit(0);
7000b522 1490
1491 for (i = 0; argv[i] != NULL; i++)
1492 ;
1493 argc = i + 1;
1494
1495 new_argv = calloc(argc + 2, sizeof(new_argv[0]));
1496 if (new_argv == NULL)
1497 return -1;
1498
1499 prep_path = malloc(512);
1500 if (prep_path == NULL)
1501 return -1;
1502
1503 make_local_path(prep_path, 512, "ginge_prep");
1504 new_argv[0] = prep_path;
1505 new_argv[1] = "--nomenu";
2692251d 1506 new_argv[2] = emu_wrap_path(filename);
7000b522 1507
1508 if (argv[0] != NULL)
1509 for (i = 1; argv[i] != NULL; i++)
1510 new_argv[i + 2] = argv[i];
1511
1512 dbg("execve \"%s\" %s \"%s\"\n", new_argv[0], new_argv[1], new_argv[2]);
1513 ret = execve(new_argv[0], (char **)new_argv, envp);
78684356 1514 err("execve(%s): %ld\n", new_argv[0], ret);
7000b522 1515 return ret;
1516}
1517
499bf01c 1518// vim:shiftwidth=2:expandtab