2 * GINGE - GINGE Is Not Gp2x Emulator
3 * (C) notaz, 2010-2011,2016
5 * This work is licensed under the MAME license, see COPYING file for details.
9 #error loader is meant to be OABI!
16 #include <sys/types.h>
20 #include <sys/types.h>
23 #include <asm/ucontext.h>
27 #include <sys/resource.h>
28 #include <sys/ioctl.h>
29 #include <linux/soundcard.h>
33 #include "../common/host_fb.h"
34 #include "../common/cmn.h"
35 #include "sys_cacheflush.h"
36 #include "realfuncs.h"
38 #if (DBG & 2) && !(DBG & 4)
53 #define iolog_unh log_io
55 #define iolog_unh(...)
59 #define segvlog printf
64 #if defined(LOG_IO) || defined(LOG_IO_UNK)
65 #include "mmsp2-regs.h"
68 typedef unsigned long long u64;
69 typedef unsigned int u32;
70 typedef unsigned short u16;
71 typedef unsigned char u8;
73 static pthread_mutex_t fb_mutex = PTHREAD_MUTEX_INITIALIZER;
74 static pthread_cond_t fb_cond = PTHREAD_COND_INITIALIZER;
95 #define SRCCTRL_INVIDEO (1 << 8)
96 #define SRCCTRL_SRCENB (1 << 7)
97 #define CTRL_TRANSPARENCYENB (1 << 11)
103 u32 mlc_stl_adr; // mlcaddress for pollux
111 u16 mlc_stl_pallt_d[256*2];
112 u32 mlc_stl_pallt_d32[256];
122 u32 btn_state; // as seen through /dev/GPIO: 0PVdVu YXBA RLSeSt 0Ri0Dn 0Le0Up
130 #define pollux mmsp2 // so that code doesn't look that weird
133 DIRTY_PAL_POLLUX = 2,
137 #if defined(LOG_IO) || defined(LOG_IO_UNK)
138 static void log_io(const char *pfx, u32 a, u32 d, int size)
140 const char *fmt, *reg = "";
142 case 8: fmt = "%s %08x %02x %s\n"; d &= 0xff; break;
143 case 32: fmt = "%s %08x %08x %s\n"; break;
144 default: fmt = "%s %08x %04x %s\n"; d &= 0xffff; break;
147 if ((a & ~0xffff) == 0x7f000000)
148 reg = regnames[a & 0xffff];
150 printf(fmt, pfx, a, d, reg);
154 static void memset16(void *dst, u32 pattern, int count)
159 d = (u16 *)((long)dst & ~1);
165 pattern |= pattern << 16;
172 *(u16 *)dl = pattern;
175 static void blt_tr(void *dst, void *src, u32 trc, int w)
177 u16 *d = (u16 *)((long)dst & ~1);
178 u16 *s = (u16 *)((long)src & ~1);
181 for (; w > 0; d++, s++, w--)
186 #define dump_blitter() \
188 u32 *r = &blitter.dstctrl; \
190 for (i = 0; i < 4*4; i++, r++) { \
191 printf("%08x ", *r); \
197 static void *uppermem_lookup(u32 addr, u8 **mem_end)
199 // XXX: maybe support mirroring?
200 if ((addr & 0xfe000000) != 0x02000000)
203 *mem_end = (u8 *)mmsp2.umem + 0x02000000;
204 return (u8 *)mmsp2.umem - 0x02000000 + addr;
207 static void blitter_do(void)
209 u8 *dst, *dste, *src = NULL, *srce = NULL;
210 int w, h, sstrd, dstrd;
214 w = blitter.size & 0x7ff;
215 h = (blitter.size >> 16) & 0x7ff;
216 sstrd = blitter.srcstride;
217 dstrd = blitter.dststride;
219 // XXX: need to confirm this..
220 addr = (blitter.dstaddr & ~3) | ((blitter.dstctrl & 0x1f) >> 3);
222 // use dst bpp.. How does it do blits with different src bpp?
223 bpp = (blitter.dstctrl & 0x20) ? 16 : 8;
226 if (((w == 320 && h == 240) || // blit whole screen
227 (w * h >= 320*240/2)) && // ..or at least half of the area
228 mmsp2.mlc_stl_adr <= addr && addr < mmsp2.mlc_stl_adr + 320*240*2)
231 dst = uppermem_lookup(addr, &dste);
233 // XXX: assume fill if no SRCENB, but it could be pattern blit..
234 if (blitter.srcctrl & SRCCTRL_SRCENB) {
235 if (!(blitter.srcctrl & SRCCTRL_INVIDEO))
238 addr = (blitter.srcaddr & ~3) | ((blitter.srcctrl & 0x1f) >> 3);
239 src = uppermem_lookup(addr, &srce);
243 if (src + sstrd * h > srce) {
244 err("blit %08x->%08x %dx%d did not fit src\n",
245 blitter.srcaddr, blitter.dstaddr, w, h);
246 h = (srce - src) / sstrd;
253 if (dst + dstrd * h > dste) {
254 err("blit %08x->%08x %dx%d did not fit dst\n",
255 blitter.srcaddr, blitter.dstaddr, w, h);
256 h = (dste - dst) / dstrd;
261 if (bpp == 16 && (blitter.ctrl & CTRL_TRANSPARENCYENB)) {
262 u32 trc = blitter.ctrl >> 16;
263 for (; h > 0; h--, dst += dstrd, src += sstrd)
264 blt_tr(dst, src, trc, w);
267 for (; h > 0; h--, dst += dstrd, src += sstrd)
268 memcpy(dst, src, w * bpp / 8);
272 // fill. Assume the pattern is cleared and bg color is used
273 u32 bgc = blitter.patbackcolor & 0xffff;
275 for (; h > 0; h--, dst += dstrd)
276 memset16(dst, bgc, w);
279 for (; h > 0; h--, dst += dstrd)
280 memset(dst, bgc, w); // bgc?
285 pthread_cond_signal(&fb_cond);
289 err("blit %08x->%08x %dx%d translated to %p->%p\n",
290 blitter.srcaddr, blitter.dstaddr, w, h, src, dst);
294 // FIXME: pass real dimensions to blitters
295 static void mlc_flip(void *src, int bpp, int stride)
299 // only pass pal to host if it's dirty
300 if (bpp <= 8 && mmsp2.v.dirty_pal) {
301 if (mmsp2.v.dirty_pal == DIRTY_PAL_MMSP2)
302 host_video_update_pal32(mmsp2.mlc_stl_pallt_d32);
304 host_video_update_pal16(mmsp2.mlcpalette);
305 mmsp2.v.dirty_pal = 0;
308 if (bpp != old_bpp) {
309 host_video_change_bpp(bpp);
315 host_video_blit4(src, 320, 240, stride);
319 host_video_blit8(src, 320, 240, stride);
323 host_video_blit16(src, 320, 240, stride);
332 #define ts_add_nsec(ts, ns) { \
334 if (ts.tv_nsec >= 1000000000) { \
336 ts.tv_nsec -= 1000000000; \
340 static int fb_sync_thread_paused;
342 static void *fb_sync_thread(void *arg)
344 int invalid_fb_addr = 1;
345 int manual_refresh = 0;
346 int frame_counter = 0;
350 //ret = pthread_setschedprio(pthread_self(), -1);
351 //log("pthread_setschedprio %d\n", ret);
352 //ret = setpriority(PRIO_PROCESS, 0, -1);
353 //log("setpriority %d\n", ret);
355 ret = clock_gettime(CLOCK_REALTIME, &ts);
357 perror(PFX "clock_gettime");
362 u8 *gp2x_fb, *gp2x_fb_end;
364 ret = pthread_mutex_lock(&fb_mutex);
365 wait_ret = pthread_cond_timedwait(&fb_cond, &fb_mutex, &ts);
366 ret |= pthread_mutex_unlock(&fb_mutex);
369 err("fb_thread: mutex error: %d\n", ret);
373 if (wait_ret != 0 && wait_ret != ETIMEDOUT) {
374 err("fb_thread: cond error: %d\n", wait_ret);
378 if (fb_sync_thread_paused) {
379 ts_add_nsec(ts, 100000000);
383 if (wait_ret != ETIMEDOUT) {
384 clock_gettime(CLOCK_REALTIME, &ts);
385 ts_add_nsec(ts, 50000000);
387 if (manual_refresh == 2)
388 dbg("fb_thread: switch to manual refresh\n");
390 ts_add_nsec(ts, 16666667);
391 if (manual_refresh > 1)
392 dbg("fb_thread: switch to auto refresh\n");
396 gp2x_fb = uppermem_lookup(mmsp2.mlc_stl_adr, &gp2x_fb_end);
397 if (gp2x_fb == NULL || gp2x_fb + 320*240 * mmsp2.v.bpp / 8 > gp2x_fb_end) {
398 if (!invalid_fb_addr) {
399 err("fb_thread: %08x is out of range\n", mmsp2.mlc_stl_adr);
406 mlc_flip(gp2x_fb, mmsp2.v.bpp, mmsp2.v.stride);
409 if (frame_counter & 0x0f)
413 // this is to check for kill key, in case main thread hung
414 // or something else went wrong.
415 pollux.btn_state = host_read_btns();
419 static void fb_thread_pause(void)
421 fb_sync_thread_paused = 1;
422 // wait until it finishes last refresh
423 // that it might be doing now
427 static void fb_thread_resume(void)
429 fb_sync_thread_paused = 0;
432 static u32 xread32_io_cmn(u32 a, u32 *handled)
439 case 0x402c: // MLCVSTRIDE0
440 case 0x4060: // MLCVSTRIDE1
443 case 0x4038: // MLCADDRESS0
444 case 0x406c: // MLCADDRESS1
445 d = pollux.mlc_stl_adr;
448 // ???? ???? YXBA DURiLe ???? VdVuMS LR?? ????
449 // | GPIOC[31:16] | GPIOB[31:16] |
450 case 0xa058: // GPIOBPAD
451 d = (pollux.btn_state >> 1) & 0x0100;
452 d |= (pollux.btn_state << 1) & 0x0200;
453 d |= (pollux.btn_state >> 3) & 0x0080;
454 d |= (pollux.btn_state >> 5) & 0x0040;
455 d |= (pollux.btn_state >> 6) & 0x0c00;
459 case 0xa098: // GPIOCPAD
460 pollux.btn_state = host_read_btns();
461 d = (pollux.btn_state >> 8) & 0x00f0;
462 d |= (pollux.btn_state >> 1) & 0x0008;
463 d |= (pollux.btn_state << 2) & 0x0004;
464 d |= (pollux.btn_state >> 5) & 0x0002;
465 d |= (pollux.btn_state >> 2) & 0x0001;
477 static u32 xread8(u32 a)
479 iolog("r8 ", a, 0, 8);
480 iolog_unh("r8 ", a, 0, 8);
484 static u32 xread16(u32 a)
486 static u32 fudge, old_a;
489 if ((a & 0xffff0000) == 0x7f000000) {
497 // 0000 P000 VuVd00 0000 YXBA RLSeSt 0Ri0D 0Le0U
498 // | GPIOD |GPIOC[8:15]|GPIOM[0:7] |
501 case 0x1184: // GPIOC
502 d = ~mmsp2.btn_state & 0xff00;
505 case 0x1186: // GPIOD
506 t = ~mmsp2.btn_state;
507 d = (t >> 9) & 0x0080;
508 d |= (t >> 11) & 0x0040;
509 d |= (t >> 7) & 0x0800;
512 case 0x1198: // GPIOM
513 mmsp2.btn_state = host_read_btns();
514 d = ~mmsp2.btn_state & 0xff;
517 case 0x1836: // reserved
520 case 0x2816: // DPC_X_MAX
523 case 0x2818: // DPC_Y_MAX
527 d = mmsp2.mlc_stl_cntl;
531 d = mmsp2.mlc_stl_adrl;
535 d = mmsp2.mlc_stl_adrh;
538 d = mmsp2.mlc_stl_pallt_a;
542 d = xread32_io_cmn(a_, &t);
558 iolog_unh("r16", a, d & 0xffff, 16);
562 iolog("r16", a, d, 16);
566 static u32 xread32(u32 a)
569 if ((a & 0xfff00000) == 0x7f000000) {
576 case 0x0a00: // TCOUNT, 1/7372800s
577 clock_gettime(CLOCK_REALTIME, &ts);
578 t64 = (u64)ts.tv_sec * 1000000000 + ts.tv_nsec;
579 // t * 7372800.0 / 1000000000 * 0x100000000 ~= t * 31665935
585 d = xread32_io_cmn(a_, &t);
592 if ((a & 0xfff00000) == 0x7f100000) {
593 u32 *bl = &blitter.dstctrl;
604 iolog_unh("r32", a, d, 32);
607 iolog("r32", a, d, 32);
611 static void xwrite8(u32 a, u32 d)
613 iolog("w8 ", a, d, 8);
614 iolog_unh("w8 ", a, d, 8);
617 static void xwrite16(u32 a, u32 d)
619 iolog("w16", a, d, 16);
620 if ((a & 0xfff00000) == 0x7f000000) {
625 mmsp2.mlc_stl_cntl = d | 0xaa;
627 mmsp2.v.bpp = mode ? mode * 8 : 4;
635 // odd addresses don't affect LCD. What about TV?
638 mmsp2.mlc_stl_adrl = d;
641 mmsp2.mlc_stl_adrh = d;
642 if (mmsp2.mlc_stl_adr != mmsp2.old_mlc_stl_adr)
644 pthread_cond_signal(&fb_cond);
645 mmsp2.old_mlc_stl_adr = mmsp2.mlc_stl_adr;
648 mmsp2.mlc_stl_pallt_a = d & 0x1ff;
651 mmsp2.mlc_stl_pallt_d[mmsp2.mlc_stl_pallt_a++] = d;
652 mmsp2.mlc_stl_pallt_a &= 0x1ff;
653 mmsp2.v.dirty_pal = DIRTY_PAL_MMSP2;
657 iolog_unh("w16", a, d, 16);
660 static void xwrite32(u32 a, u32 d)
662 iolog("w32", a, d, 32);
664 if ((a & 0xfff00000) == 0x7f000000) {
668 case 0x4024: // MLCCONTROL0
669 case 0x4058: // MLCCONTROL1
670 pollux.mlccontrol = d;
672 return; // layer not enabled
673 if ((d >> 16) == 0x443A)
678 case 0x402c: // MLCVSTRIDE0
679 case 0x4060: // MLCVSTRIDE1
682 case 0x4038: // MLCADDRESS0
683 case 0x406c: // MLCADDRESS1
684 pollux.mlc_stl_adr = d;
685 if (d != mmsp2.old_mlc_stl_adr)
687 pthread_cond_signal(&fb_cond);
688 mmsp2.old_mlc_stl_adr = d;
690 case 0x403c: // MLCPALETTE0
691 case 0x4070: // MLCPALETTE1
692 pollux.mlcpalette[d >> 24] = d;
693 pollux.v.dirty_pal = DIRTY_PAL_POLLUX;
697 if ((a & 0xfff00000) == 0x7f100000) {
698 u32 *bl = &blitter.dstctrl;
702 if (a_ == 0x34 && (d & 1))
707 iolog_unh("w32", a, d, 32);
710 #define LINKPAGE_SIZE 0x1000
711 #define LINKPAGE_COUNT 4
712 #define LINKPAGE_ALLOC (LINKPAGE_SIZE * LINKPAGE_COUNT)
721 void (*handler)(struct op_context *op_ctx);
725 struct op_stackframe {
730 static struct op_linkpage *g_linkpage;
731 static u32 *g_code_ptr;
732 static int g_linkpage_count;
735 C_EQ, C_NE, C_CS, C_CC, C_MI, C_PL, C_VS, C_VC,
736 C_HI, C_LS, C_GE, C_LT, C_GT, C_LE, C_AL,
745 #define BIT_SET(v, b) (v & (1 << (b)))
747 void emu_handle_op(struct op_context *op_ctx, struct op_stackframe *sframe)
749 u32 *regs = sframe->saved_regs;
750 u32 cpsr = sframe->cpsr;
752 u32 t, shift, ret, addr;
755 cond = (op & 0xf0000000) >> 28;
756 rd = (op & 0x0000f000) >> 12;
757 rn = (op & 0x000f0000) >> 16;
761 case C_EQ: if ( (cpsr & CPSR_Z)) break; return;
762 case C_NE: if (!(cpsr & CPSR_Z)) break; return;
763 case C_CS: if ( (cpsr & CPSR_C)) break; return;
764 case C_CC: if (!(cpsr & CPSR_C)) break; return;
765 case C_MI: if ( (cpsr & CPSR_N)) break; return;
766 case C_PL: if (!(cpsr & CPSR_N)) break; return;
767 case C_VS: if ( (cpsr & CPSR_V)) break; return;
768 case C_VC: if (!(cpsr & CPSR_V)) break; return;
774 if ((op & 0x0f200090) == 0x01000090) { // AM3: LDRH, STRH
775 if (!BIT_SET(op, 5)) // !H
777 if (BIT_SET(op, 6) && !BIT_SET(op, 20)) // S && !L
780 if (BIT_SET(op, 22)) // imm offset
781 t = ((op & 0xf00) >> 4) | (op & 0x0f);
783 t = regs[op & 0x000f];
785 if (!BIT_SET(op, 23))
789 if (BIT_SET(op, 20)) { // Load
791 if (BIT_SET(op, 6)) { // S
793 ret = (signed int)ret >> 16;
798 xwrite16(addr, regs[rd]);
800 else if ((op & 0x0c000000) == 0x04000000) { // load/store word/byte
802 goto unhandled; // unprivileged
803 if (BIT_SET(op, 25)) { // reg offs
805 goto unhandled; // nah it's media
807 t = regs[op & 0x000f];
808 shift = (op & 0x0f80) >> 7;
809 switch ((op & 0x0060) >> 5) {
810 case 0: t = t << shift; break;
811 case 1: t = t >> (shift + 1); break;
812 case 2: t = (signed int)t >> (shift + 1); break;
813 case 3: goto unhandled; // I'm just lazy
819 if (!BIT_SET(op, 23))
823 if (BIT_SET(op, 24)) // pre-indexed
825 if (!BIT_SET(op, 24) || BIT_SET(op, 21))
826 regs[rn] += t; // writeback
828 if (BIT_SET(op, 20)) { // Load
829 if (BIT_SET(op, 22)) // Byte
836 if (BIT_SET(op, 22)) // Byte
837 xwrite8(addr, regs[rd]);
839 xwrite32(addr, regs[rd]);
846 if (addr != addr_check) {
847 fprintf(stderr, "bad calculated addr: %08x vs %08x\n", addr, addr_check);
854 err("unhandled IO op %08x @ %08x\n", op, op_ctx->pc);
855 for (i = 0; i < 8-1; i++)
856 err(" r%d=%08x r%-2d=%08x\n", i, regs[i], i+8, regs[i+8]);
857 err(" r%d=%08x cpsr=%08x\n", i, regs[i], cpsr);
861 static u32 make_offset12(u32 *pc, u32 *target)
865 lp_offs = (char *)target - (char *)pc - 2*4;
870 if (lp_offs >= LINKPAGE_SIZE) {
871 err("linkpage too far: %d\n", lp_offs);
875 return (u << 23) | lp_offs;
878 static u32 make_jmp(u32 *pc, u32 *target, int bl)
882 jmp_val = target - pc - 2;
883 if (jmp_val < (int)0xff000000 || jmp_val > 0x00ffffff) {
884 err("jump out of range (%p -> %p)\n", pc, target);
888 return 0xea000000 | (bl << 24) | (jmp_val & 0x00ffffff);
891 static void emit_op(u32 op)
896 static void emit_op_io(u32 op, u32 *target)
898 op |= make_offset12(g_code_ptr, target);
902 static void init_linkpage(void)
904 g_linkpage->handler = emu_call_handle_op;
905 g_code_ptr = g_linkpage->code;
908 static void segv_sigaction(int num, siginfo_t *info, void *ctx)
910 struct ucontext *context = ctx;
911 u32 *regs = (u32 *)&context->uc_mcontext.arm_r0;
912 u32 *pc = (u32 *)regs[15];
913 struct op_context *op_ctx;
916 if (((regs[15] ^ (u32)&segv_sigaction) & 0xff000000) == 0 || // PC is in our segment or
917 (((regs[15] ^ (u32)g_linkpage) & ~(LINKPAGE_ALLOC - 1)) == 0) || // .. in linkpage
918 ((long)info->si_addr & 0xffe00000) != 0x7f000000) // faulting not where expected
920 // real crash - time to die
921 err("segv %d %p @ %08x\n", info->si_code, info->si_addr, regs[15]);
922 for (i = 0; i < 8; i++)
923 dbg(" r%d=%08x r%-2d=%08x\n", i, regs[i], i+8, regs[i+8]);
924 signal(num, SIG_DFL);
928 segvlog("segv %d %p @ %08x\n", info->si_code, info->si_addr, regs[15]);
931 op_ctx = (void *)g_code_ptr;
932 op_ctx->pc = (u32)pc;
934 g_code_ptr = &op_ctx->code[0];
936 // emit jump to code ptr
937 *pc = make_jmp(pc, g_code_ptr, 0);
940 emit_op (0xe50d0000 + 0xf00 - 4 * 0); // str r0, [sp, #(-0xf00 + r0_offs)]
941 emit_op (0xe50de000 + 0xf00 - 4 * 14); // str lr, [sp, #(-0xf00 + lr_offs)]
942 emit_op (0xe24f0000 + (g_code_ptr - (u32 *)op_ctx + 2) * 4); // sub r0, pc, #op_ctx
943 emit_op (0xe1a0e00f); // mov lr, pc
944 emit_op_io(0xe51ff000, (u32 *)&g_linkpage->handler); // ldr pc, =handle_op
945 emit_op (0xe51de000 + 0xf00 - 4 * 14); // ldr lr, [sp, #(-0xf00 + lr_offs)]
946 emit_op (make_jmp(g_code_ptr, pc + 1, 0)); // jmp <back>
949 sys_cacheflush(pc, pc + 1);
950 sys_cacheflush(g_linkpage, g_code_ptr);
952 lp_size = (char *)g_code_ptr - (char *)g_linkpage;
953 segvlog("code #%d %d/%d\n", g_linkpage_count, lp_size, LINKPAGE_SIZE);
955 if (lp_size + 13*4 > LINKPAGE_SIZE) {
957 if (g_linkpage_count >= LINKPAGE_COUNT) {
958 err("too many linkpages needed\n");
961 g_linkpage = (void *)((char *)g_linkpage + LINKPAGE_SIZE);
964 //handle_op(regs[15], op, regs, (u32)info->si_addr);
968 void emu_init(void *map_bottom)
970 sigaction_t segv_action = {
971 .sa_sigaction = segv_sigaction,
972 .sa_flags = SA_SIGINFO,
979 if (geteuid() == 0) {
980 fprintf(stderr, "don't try to run as root, device registers or memory "
981 "might get trashed crashing the OS or even damaging the device.\n");
986 g_linkpage = (void *)(((u32)map_bottom - LINKPAGE_ALLOC) & ~0xfff);
987 pret = mmap(g_linkpage, LINKPAGE_ALLOC, PROT_READ|PROT_WRITE,
988 MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0);
989 if (pret != g_linkpage) {
990 perror(PFX "mmap linkpage");
993 log("linkpages @ %p\n", g_linkpage);
999 err("can't init host\n");
1003 ret = host_video_init(NULL, 0);
1005 err("can't init host video\n");
1009 // TODO: check if this really fails on Wiz..
1010 mmsp2.umem = mmap(NULL, 0x2000000, PROT_READ|PROT_WRITE|PROT_EXEC,
1011 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
1013 if (mmsp2.umem == MAP_FAILED) {
1014 // we are short on memmory on Wiz, need special handling
1015 extern void *host_mmap_upper(void);
1016 mmsp2.umem = host_mmap_upper();
1019 if (mmsp2.umem == MAP_FAILED) {
1020 perror(PFX "mmap upper mem");
1024 ret = pthread_create(&tid, NULL, fb_sync_thread, NULL);
1026 err("failed to create fb_sync_thread: %d\n", ret);
1029 pthread_detach(tid);
1032 mmsp2.mlc_stl_adr = 0x03101000; // fb2 is at 0x03381000
1033 mmsp2.mlc_stl_cntl = 0x4ab; // 16bpp, region 1 active
1034 mmsp2.v.width = 320;
1035 mmsp2.v.height = 240;
1036 mmsp2.v.stride = 320*2;
1038 mmsp2.v.dirty_pal = 1;
1040 sigemptyset(&segv_action.sa_mask);
1041 sigaction(SIGSEGV, &segv_action, NULL);
1044 int emu_read_gpiodev(void *buf, int count)
1047 err("gpiodev read %d?\n", count);
1053 mmsp2.btn_state = host_read_btns();
1054 memcpy(buf, &mmsp2.btn_state, count);
1058 static void *emu_mmap_dev(unsigned int length, int prot, int flags, unsigned int offset)
1060 u8 *umem, *umem_end;
1063 if ((offset & ~0x1ffff) == 0xc0000000) {
1064 return mmap((void *)0x7f000000, length, PROT_NONE,
1065 MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED|MAP_NORESERVE, -1, 0);
1068 if ((offset & ~0xffff) == 0xe0020000) {
1069 return mmap((void *)0x7f100000, length, PROT_NONE,
1070 MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED|MAP_NORESERVE, -1, 0);
1073 if ((offset & 0xfe000000) != 0x02000000) {
1074 err("unexpected devmem mmap @ %08x\n", offset);
1079 umem = uppermem_lookup(offset, &umem_end);
1080 if (umem + length > umem_end)
1081 err("warning: uppermem @ %08x overflows by %d bytes\n",
1082 offset, umem + length - umem_end);
1084 dbg("upper mem @ %08x %d\n", offset, length);
1088 void *emu_do_mmap(unsigned int length, int prot, int flags, int fd, unsigned int offset)
1090 if (fd == FAKEDEV_MEM)
1091 return emu_mmap_dev(length, prot, flags, offset);
1093 if (fd == FAKEDEV_FB0)
1094 return emu_mmap_dev(length, prot, flags, offset + 0x03101000);
1096 if (fd == FAKEDEV_FB1)
1097 return emu_mmap_dev(length, prot, flags, offset + 0x03381000);
1099 err("bad/ni mmap(?, %d, %x, %x, %d, %08x)\n", length, prot, flags, fd, offset);
1104 static void emu_sound_open(int fd)
1109 // set default buffer size to 16 * 1K
1110 frag = (16<<16) | 10; // 16K
1111 ret = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &frag);
1113 err("snd ioctl SETFRAGMENT %08x: ", frag);
1119 static int emu_sound_ioctl(int fd, int request, void *argp)
1124 dbg("snd ioctl(%d, %08x, %p)", fd, request, argp);
1126 dbg_c(" [%d]", *arg);
1130 /* People set strange frag settings on GP2X, which even manage
1131 * to break audio on pandora (causes writes to fail).
1132 * Catch this and set to something that works. */
1134 case SNDCTL_DSP_SETFRAGMENT: {
1135 int ret, bsize, frag, frag_cnt;
1139 frag = *arg & 0xffff;
1140 frag_cnt = *arg >> 16;
1141 bsize = frag_cnt << frag;
1142 if (frag < 10 || bsize < 4096*4 || bsize > 4096*4*2) {
1144 * ~4ms. gpSP wants small buffers or else it stutters
1145 * because of it's audio thread sync stuff
1146 * XXX: hardcoding, as low samplerates will result in small fragment size,
1147 * which itself causes ALSA stall and hangs the program.
1148 * Also some apps change samplerate without reopening /dev/dsp,
1149 * which causes ALSA to reject SNDCTL_DSP_SETFRAGMENT.
1151 bsize = 44100 / 250 * 4;
1153 for (frag = 0; bsize; bsize >>= 1, frag++)
1159 frag |= frag_cnt << 16;
1160 ret = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &frag);
1162 err("snd ioctl SETFRAGMENT %08x: ", frag);
1165 // indicate success even if we fail (because of ALSA mostly),
1166 // things like MikMod will bail out otherwise.
1169 case SNDCTL_DSP_SYNC:
1170 // Franxis tends to use sync/write loops, bad idea under ALSA
1176 return ioctl(fd, request, argp);
1179 int emu_do_ioctl(int fd, int request, void *argp)
1181 if (fd == emu_interesting_fds[IFD_SOUND].fd)
1182 return emu_sound_ioctl(fd, request, argp);
1185 /* *********************** */
1192 case FBIOGET_FSCREENINFO: {
1193 struct fb_fix_screeninfo *fix = argp;
1195 memset(fix, 0, sizeof(*fix));
1196 strcpy(fix->id, "mmsp2_RGB0");
1197 fix->type = FB_TYPE_PACKED_PIXELS;
1198 fix->accel = FB_ACCEL_NONE;
1199 fix->visual = FB_VISUAL_TRUECOLOR;
1200 fix->line_length = 320*2;
1201 fix->smem_start = (fd == FAKEDEV_FB0) ? 0x03101000 : 0x03381000;
1202 fix->smem_len = 320*240*2;
1205 case FBIOGET_VSCREENINFO: {
1206 struct fb_var_screeninfo *var = argp;
1207 static const struct fb_bitfield fbb_red = { offset: 11, length: 5, };
1208 static const struct fb_bitfield fbb_green = { offset: 5, length: 6, };
1209 static const struct fb_bitfield fbb_blue = { offset: 0, length: 5, };
1211 memset(var, 0, sizeof(*var));
1212 var->activate = FB_ACTIVATE_NOW;
1214 var->xres_virtual = 320;
1216 var->yres_virtual = 240;
1219 var->vmode = FB_VMODE_NONINTERLACED;
1220 var->bits_per_pixel = 16;
1222 var->green = fbb_green;
1223 var->blue = fbb_blue;
1226 case FBIOPUT_VSCREENINFO: {
1227 struct fb_var_screeninfo *var = argp;
1228 dbg(" put vscreen: %dx%d@%d\n", var->xres, var->yres, var->bits_per_pixel);
1229 if (var->xres != 320 || var->yres != 240 || var->bits_per_pixel != 16)
1235 /* *********************** */
1237 // fake tty0 to make GPH SDL happy
1238 if (request == 0x4b46) // KDGKBENT
1244 err("bad/ni ioctl(%d, %08x, %p)\n", fd, request, argp);
1249 struct dev_fd_t emu_interesting_fds[] = {
1250 [IFD_SOUND] = { "/dev/dsp", -1, emu_sound_open },
1254 static const struct {
1258 { "/mnt/tmp", "./tmp" },
1261 static const char *wrap_path(const char *path)
1267 // do only path mapping for now
1268 for (i = 0; i < ARRAY_SIZE(path_map); i++) {
1269 p = strstr(path, path_map[i].from);
1271 size = strlen(path) + strlen(path_map[i].to) + 1;
1272 buff = malloc(size);
1276 strncpy(buff, path, len);
1277 snprintf(buff + len, size - len, "%s%s", path_map[i].to,
1278 path + len + strlen(path_map[i].from));
1279 dbg("mapped path \"%s\" -> \"%s\"\n", path, buff);
1287 static void wrap_path_free(const char *w_path, const char *old_path)
1289 if (w_path != old_path)
1290 free((void *)w_path);
1293 void *emu_do_fopen(const char *path, const char *mode)
1298 w_path = wrap_path(path);
1299 ret = fopen(w_path, mode);
1300 wrap_path_free(w_path, path);
1306 int emu_do_system(const char *command)
1308 static char tmp_path[512];
1314 if (command == NULL)
1317 for (p2 = command; *p2 && isspace(*p2); p2++)
1320 if (*p2 == '.') // relative path?
1322 else if (*p2 == '/' && strncmp(p2, "/bin", 4) && strncmp(p2, "/lib", 4)
1323 && strncmp(p2, "/sbin", 4) && strncmp(p2, "/usr", 4))
1324 // absolute path, but not a system command
1327 p2 = wrap_path(command);
1329 make_local_path(tmp_path, sizeof(tmp_path), "ginge_prep");
1330 p = tmp_path + strlen(tmp_path);
1332 snprintf(p, sizeof(tmp_path) - (p - tmp_path), " --nomenu %s", p2);
1335 snprintf(tmp_path, sizeof(tmp_path), "%s", p2);
1336 wrap_path_free(p2, command);
1338 dbg("system: \"%s\"\n", tmp_path);
1340 // the app might want the screen too..
1342 ret = system(tmp_path);
1347 int emu_do_execve(const char *filename, char *const argv[], char *const envp[])
1349 const char **new_argv;
1353 if (filename == NULL)
1356 if (strstr(filename, "/gp2xmenu") != NULL)
1359 for (i = 0; argv[i] != NULL; i++)
1363 new_argv = calloc(argc + 2, sizeof(new_argv[0]));
1364 if (new_argv == NULL)
1367 prep_path = malloc(512);
1368 if (prep_path == NULL)
1371 make_local_path(prep_path, 512, "ginge_prep");
1372 new_argv[0] = prep_path;
1373 new_argv[1] = "--nomenu";
1374 new_argv[2] = wrap_path(filename);
1376 if (argv[0] != NULL)
1377 for (i = 1; argv[i] != NULL; i++)
1378 new_argv[i + 2] = argv[i];
1380 dbg("execve \"%s\" %s \"%s\"\n", new_argv[0], new_argv[1], new_argv[2]);
1381 ret = execve(new_argv[0], (char **)new_argv, envp);
1386 // vim:shiftwidth=2:expandtab