| 1 | // vim:shiftwidth=2:expandtab |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <alloca.h> |
| 6 | #include <sys/types.h> |
| 7 | #include <sys/stat.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <sys/mman.h> |
| 10 | #include <sys/types.h> |
| 11 | #include <unistd.h> |
| 12 | #include <signal.h> |
| 13 | #include <asm/ucontext.h> |
| 14 | #include <pthread.h> |
| 15 | #include <errno.h> |
| 16 | #include <time.h> |
| 17 | #include <sys/resource.h> |
| 18 | |
| 19 | #include "header.h" |
| 20 | #include "sys_cacheflush.h" |
| 21 | |
| 22 | //#define LOG_IO |
| 23 | //#define LOG_IO_UNK |
| 24 | //#define LOG_SEGV |
| 25 | |
| 26 | #ifdef LOG_IO |
| 27 | #define iolog log_io |
| 28 | #else |
| 29 | #define iolog(...) |
| 30 | #endif |
| 31 | |
| 32 | #ifdef LOG_IO_UNK |
| 33 | #define iolog_unh log_io |
| 34 | #else |
| 35 | #define iolog_unh(...) |
| 36 | #endif |
| 37 | |
| 38 | #ifdef LOG_SEGV |
| 39 | #define segvlog printf |
| 40 | #else |
| 41 | #define segvlog(...) |
| 42 | #endif |
| 43 | |
| 44 | #if defined(LOG_IO) || defined(LOG_IO_UNK) |
| 45 | #include "mmsp2-regs.h" |
| 46 | #endif |
| 47 | |
| 48 | typedef unsigned int u32; |
| 49 | typedef unsigned short u16; |
| 50 | typedef unsigned char u8; |
| 51 | |
| 52 | static pthread_mutex_t fb_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 53 | static pthread_cond_t fb_cond = PTHREAD_COND_INITIALIZER; |
| 54 | |
| 55 | struct uppermem_block { |
| 56 | u32 addr; // physical |
| 57 | u32 size; |
| 58 | void *mem; |
| 59 | struct uppermem_block *next; |
| 60 | }; |
| 61 | |
| 62 | static struct uppermem_block *upper_mem; |
| 63 | |
| 64 | static struct { |
| 65 | u32 dstctrl; |
| 66 | u32 dstaddr; |
| 67 | u32 dststride; |
| 68 | u32 srcctrl; |
| 69 | u32 srcaddr; // |
| 70 | u32 srcstride; |
| 71 | u32 srcforcolor; |
| 72 | u32 srcbackcolor; |
| 73 | u32 patctrl; // |
| 74 | u32 patforcolor; |
| 75 | u32 patbackcolor; |
| 76 | u32 size; |
| 77 | u32 ctrl; // |
| 78 | u32 run; |
| 79 | u32 intc; |
| 80 | u32 srcfifo; |
| 81 | } blitter; |
| 82 | |
| 83 | #define SRCCTRL_INVIDEO (1 << 8) |
| 84 | #define SRCCTRL_SRCENB (1 << 7) |
| 85 | #define CTRL_TRANSPARENCYENB (1 << 11) |
| 86 | |
| 87 | static struct { |
| 88 | u16 mlc_stl_cntl; |
| 89 | union { |
| 90 | u32 mlc_stl_adr; |
| 91 | struct { |
| 92 | u16 mlc_stl_adrl; |
| 93 | u16 mlc_stl_adrh; |
| 94 | }; |
| 95 | }; |
| 96 | u16 mlc_stl_pallt_a; |
| 97 | union { |
| 98 | u16 mlc_stl_pallt_d[256*2]; |
| 99 | u32 mlc_stl_pallt_d32[256]; |
| 100 | }; |
| 101 | |
| 102 | // state |
| 103 | u16 host_pal[256]; |
| 104 | u32 old_mlc_stl_adr; |
| 105 | u32 btn_state; // as seen through /dev/GPIO |
| 106 | u32 dirty_pal:1; |
| 107 | } mmsp2; |
| 108 | |
| 109 | static u16 *host_screen; |
| 110 | static int host_stride; |
| 111 | |
| 112 | |
| 113 | #if defined(LOG_IO) || defined(LOG_IO_UNK) |
| 114 | static void log_io(const char *pfx, u32 a, u32 d, int size) |
| 115 | { |
| 116 | const char *fmt, *reg = ""; |
| 117 | switch (size) { |
| 118 | case 8: fmt = "%s %08x %02x %s\n"; d &= 0xff; break; |
| 119 | case 32: fmt = "%s %08x %08x %s\n"; break; |
| 120 | default: fmt = "%s %08x %04x %s\n"; d &= 0xffff; break; |
| 121 | } |
| 122 | |
| 123 | if ((a & ~0xffff) == 0x7f000000) |
| 124 | reg = regnames[a & 0xffff]; |
| 125 | |
| 126 | printf(fmt, pfx, a, d, reg); |
| 127 | } |
| 128 | #endif |
| 129 | |
| 130 | static void memset16(void *dst, u32 pattern, int count) |
| 131 | { |
| 132 | u32 *dl; |
| 133 | u16 *d; |
| 134 | |
| 135 | d = (u16 *)((long)dst & ~1); |
| 136 | if ((long)d & 2) { |
| 137 | *d++ = pattern; |
| 138 | count--; |
| 139 | } |
| 140 | dl = (void *)d; |
| 141 | pattern |= pattern << 16; |
| 142 | |
| 143 | while (count >= 2) { |
| 144 | *dl++ = pattern; |
| 145 | count -= 2; |
| 146 | } |
| 147 | if (count) |
| 148 | *(u16 *)dl = pattern; |
| 149 | } |
| 150 | |
| 151 | static void blt_tr(void *dst, void *src, u32 trc, int w) |
| 152 | { |
| 153 | u16 *d = (u16 *)((long)dst & ~1); |
| 154 | u16 *s = (u16 *)((long)src & ~1); |
| 155 | |
| 156 | // XXX: optimize |
| 157 | for (; w > 0; d++, s++, w--) |
| 158 | if (*s != trc) |
| 159 | *d = *s; |
| 160 | } |
| 161 | |
| 162 | #define dump_blitter() \ |
| 163 | { \ |
| 164 | u32 *r = &blitter.dstctrl; \ |
| 165 | int i; \ |
| 166 | for (i = 0; i < 4*4; i++, r++) { \ |
| 167 | printf("%08x ", *r); \ |
| 168 | if ((i & 3) == 3) \ |
| 169 | printf("\n"); \ |
| 170 | } \ |
| 171 | } |
| 172 | |
| 173 | static void *uppermem_lookup(u32 addr, u8 **mem_end) |
| 174 | { |
| 175 | struct uppermem_block *ub; |
| 176 | |
| 177 | for (ub = upper_mem; ub != NULL; ub = ub->next) { |
| 178 | if (ub->addr <= addr && addr < ub->addr + ub->size) { |
| 179 | *mem_end = (u8 *)ub->mem + ub->size; |
| 180 | return (u8 *)ub->mem + addr - ub->addr; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | return NULL; |
| 185 | } |
| 186 | |
| 187 | static void blitter_do(void) |
| 188 | { |
| 189 | u8 *dst, *dste, *src = NULL, *srce = NULL; |
| 190 | int w, h, sstrd, dstrd; |
| 191 | int to_screen = 0; |
| 192 | u32 bpp, addr; |
| 193 | |
| 194 | w = blitter.size & 0x7ff; |
| 195 | h = (blitter.size >> 16) & 0x7ff; |
| 196 | sstrd = blitter.srcstride; |
| 197 | dstrd = blitter.dststride; |
| 198 | |
| 199 | // XXX: need to confirm this.. |
| 200 | addr = (blitter.dstaddr & ~3) | ((blitter.dstctrl & 0x1f) >> 3); |
| 201 | |
| 202 | // use dst bpp.. How does it do blits with different src bpp? |
| 203 | bpp = (blitter.dstctrl & 0x20) ? 16 : 8; |
| 204 | |
| 205 | // maybe the screen? |
| 206 | if (((w == 320 && h == 240) || // blit whole screen |
| 207 | (w * h >= 320*240/2)) && // ..or at least half of the area |
| 208 | mmsp2.mlc_stl_adr <= addr && addr < mmsp2.mlc_stl_adr + 320*240*2) |
| 209 | to_screen = 1; |
| 210 | |
| 211 | dst = uppermem_lookup(addr, &dste); |
| 212 | |
| 213 | // XXX: assume fill if no SRCENB, but it could be pattern blit.. |
| 214 | if (blitter.srcctrl & SRCCTRL_SRCENB) { |
| 215 | if (!(blitter.srcctrl & SRCCTRL_INVIDEO)) |
| 216 | goto bad_blit; |
| 217 | |
| 218 | addr = (blitter.srcaddr & ~3) | ((blitter.srcctrl & 0x1f) >> 3); |
| 219 | src = uppermem_lookup(addr, &srce); |
| 220 | if (src == NULL) |
| 221 | goto bad_blit; |
| 222 | |
| 223 | if (src + sstrd * h > srce) { |
| 224 | err("blit %08x->%08x %dx%d did not fit src\n", |
| 225 | blitter.srcaddr, blitter.dstaddr, w, h); |
| 226 | h = (srce - src) / sstrd; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | if (dst == NULL) |
| 231 | goto bad_blit; |
| 232 | |
| 233 | if (dst + dstrd * h > dste) { |
| 234 | err("blit %08x->%08x %dx%d did not fit dst\n", |
| 235 | blitter.srcaddr, blitter.dstaddr, w, h); |
| 236 | h = (dste - dst) / dstrd; |
| 237 | } |
| 238 | |
| 239 | if (src != NULL) { |
| 240 | // copy |
| 241 | if (bpp == 16 && (blitter.ctrl & CTRL_TRANSPARENCYENB)) { |
| 242 | u32 trc = blitter.ctrl >> 16; |
| 243 | for (; h > 0; h--, dst += dstrd, src += sstrd) |
| 244 | blt_tr(dst, src, trc, w); |
| 245 | } |
| 246 | else { |
| 247 | for (; h > 0; h--, dst += dstrd, src += sstrd) |
| 248 | memcpy(dst, src, w * bpp / 8); |
| 249 | } |
| 250 | } |
| 251 | else { |
| 252 | // fill. Assume the pattern is cleared and bg color is used |
| 253 | u32 bgc = blitter.patbackcolor & 0xffff; |
| 254 | if (bpp == 16) { |
| 255 | for (; h > 0; h--, dst += dstrd) |
| 256 | memset16(dst, bgc, w); |
| 257 | } |
| 258 | else { |
| 259 | for (; h > 0; h--, dst += dstrd) |
| 260 | memset(dst, bgc, w); // bgc? |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | if (to_screen) |
| 265 | pthread_cond_signal(&fb_cond); |
| 266 | return; |
| 267 | |
| 268 | bad_blit: |
| 269 | err("blit %08x->%08x %dx%d translated to %p->%p\n", |
| 270 | blitter.srcaddr, blitter.dstaddr, w, h, src, dst); |
| 271 | dump_blitter(); |
| 272 | } |
| 273 | |
| 274 | // TODO: hw scaler stuff |
| 275 | static void mlc_flip(u8 *src, int bpp) |
| 276 | { |
| 277 | u16 *dst = host_screen; |
| 278 | u16 *hpal = mmsp2.host_pal; |
| 279 | int i, u; |
| 280 | |
| 281 | if (bpp <= 8 && mmsp2.dirty_pal) { |
| 282 | u32 *srcp = mmsp2.mlc_stl_pallt_d32; |
| 283 | u16 *dstp = hpal; |
| 284 | |
| 285 | for (i = 0; i < 256; i++, srcp++, dstp++) { |
| 286 | u32 t = *srcp; |
| 287 | *dstp = ((t >> 8) & 0xf800) | ((t >> 5) & 0x07e0) | ((t >> 3) & 0x001f); |
| 288 | } |
| 289 | mmsp2.dirty_pal = 0; |
| 290 | } |
| 291 | |
| 292 | switch (bpp) { |
| 293 | case 4: |
| 294 | for (i = 0; i < 240; i++, dst += host_stride / 2 - 320) { |
| 295 | for (u = 320 / 2; u > 0; u--, src++) { |
| 296 | *dst++ = hpal[*src >> 4]; |
| 297 | *dst++ = hpal[*src & 0x0f]; |
| 298 | } |
| 299 | } |
| 300 | break; |
| 301 | |
| 302 | case 8: |
| 303 | for (i = 0; i < 240; i++, dst += host_stride / 2 - 320) { |
| 304 | for (u = 320 / 4; u > 0; u--) { |
| 305 | *dst++ = hpal[*src++]; |
| 306 | *dst++ = hpal[*src++]; |
| 307 | *dst++ = hpal[*src++]; |
| 308 | *dst++ = hpal[*src++]; |
| 309 | } |
| 310 | } |
| 311 | break; |
| 312 | |
| 313 | case 16: |
| 314 | for (i = 0; i < 240; i++, dst += host_stride / 2, src += 320*2) |
| 315 | memcpy(dst, src, 320*2); |
| 316 | break; |
| 317 | |
| 318 | case 24: |
| 319 | // TODO |
| 320 | break; |
| 321 | } |
| 322 | |
| 323 | host_screen = host_video_flip(); |
| 324 | } |
| 325 | |
| 326 | #define ts_add_nsec(ts, ns) { \ |
| 327 | ts.tv_nsec += ns; \ |
| 328 | if (ts.tv_nsec >= 1000000000) { \ |
| 329 | ts.tv_sec++; \ |
| 330 | ts.tv_nsec -= 1000000000; \ |
| 331 | } \ |
| 332 | } |
| 333 | |
| 334 | static void *fb_sync_thread(void *arg) |
| 335 | { |
| 336 | int invalid_fb_addr = 1; |
| 337 | int manual_refresh = 0; |
| 338 | struct timespec ts; |
| 339 | int ret, wait_ret; |
| 340 | |
| 341 | //ret = pthread_setschedprio(pthread_self(), -1); |
| 342 | //log("pthread_setschedprio %d\n", ret); |
| 343 | //ret = setpriority(PRIO_PROCESS, 0, -1); |
| 344 | //log("setpriority %d\n", ret); |
| 345 | |
| 346 | ret = clock_gettime(CLOCK_REALTIME, &ts); |
| 347 | if (ret != 0) { |
| 348 | perror(PFX "clock_gettime"); |
| 349 | exit(1); |
| 350 | } |
| 351 | |
| 352 | while (1) { |
| 353 | u8 *gp2x_fb, *gp2x_fb_end; |
| 354 | int mode, bpp; |
| 355 | |
| 356 | ret = pthread_mutex_lock(&fb_mutex); |
| 357 | wait_ret = pthread_cond_timedwait(&fb_cond, &fb_mutex, &ts); |
| 358 | ret |= pthread_mutex_unlock(&fb_mutex); |
| 359 | |
| 360 | if (ret != 0) { |
| 361 | err("fb_thread: mutex error: %d\n", ret); |
| 362 | sleep(1); |
| 363 | continue; |
| 364 | } |
| 365 | if (wait_ret != 0 && wait_ret != ETIMEDOUT) { |
| 366 | err("fb_thread: cond error: %d\n", wait_ret); |
| 367 | sleep(1); |
| 368 | continue; |
| 369 | } |
| 370 | |
| 371 | if (wait_ret != ETIMEDOUT) { |
| 372 | clock_gettime(CLOCK_REALTIME, &ts); |
| 373 | ts_add_nsec(ts, 50000000); |
| 374 | manual_refresh++; |
| 375 | if (manual_refresh == 2) |
| 376 | log("fb_thread: switch to manual refresh\n"); |
| 377 | } else { |
| 378 | ts_add_nsec(ts, 16666667); |
| 379 | if (manual_refresh > 1) |
| 380 | log("fb_thread: switch to auto refresh\n"); |
| 381 | manual_refresh = 0; |
| 382 | } |
| 383 | |
| 384 | mode = (mmsp2.mlc_stl_cntl >> 9) & 3; |
| 385 | bpp = mode ? mode * 8 : 4; |
| 386 | |
| 387 | gp2x_fb = uppermem_lookup(mmsp2.mlc_stl_adr, &gp2x_fb_end); |
| 388 | if (gp2x_fb == NULL || gp2x_fb + 320*240 * bpp / 8 > gp2x_fb_end) { |
| 389 | if (!invalid_fb_addr) { |
| 390 | err("fb_thread: %08x is out of range\n", mmsp2.mlc_stl_adr); |
| 391 | invalid_fb_addr = 1; |
| 392 | } |
| 393 | continue; |
| 394 | } |
| 395 | |
| 396 | mlc_flip(gp2x_fb, bpp); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | static u32 xread8(u32 a) |
| 401 | { |
| 402 | iolog("r8 ", a, 0, 8); |
| 403 | iolog_unh("r8 ", a, 0, 8); |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | static u32 xread16(u32 a) |
| 408 | { |
| 409 | static u32 fudge, old_a; |
| 410 | u32 d = 0, t; |
| 411 | |
| 412 | if ((a & 0xffff0000) == 0x7f000000) { |
| 413 | u32 a_ = a & 0xffff; |
| 414 | switch (a_) { |
| 415 | case 0x0910: // FPLL |
| 416 | case 0x0912: |
| 417 | d = 0x9407; |
| 418 | break; |
| 419 | // minilib reads as: |
| 420 | // 0000 P000 VuVd00 0000 YXBA RLSeSt 0R0D 0L0U |
| 421 | // | GPIOD |GPIOC[8:15]|GPIOM[0:7]| |
| 422 | // /dev/GPIO: |
| 423 | // ... 0PVdVu ... |
| 424 | case 0x1184: // GPIOC |
| 425 | d = ~mmsp2.btn_state & 0xff00; |
| 426 | d |= 0x00ff; |
| 427 | break; |
| 428 | case 0x1186: // GPIOD |
| 429 | t = ~mmsp2.btn_state; |
| 430 | d = (t >> 9) & 0x0080; |
| 431 | d |= (t >> 11) & 0x0040; |
| 432 | d |= (t >> 7) & 0x0800; |
| 433 | d |= 0x373b; |
| 434 | break; |
| 435 | case 0x1198: // GPIOM |
| 436 | mmsp2.btn_state = host_read_btns(); |
| 437 | d = ~mmsp2.btn_state & 0xff; |
| 438 | d |= 0x01aa; |
| 439 | break; |
| 440 | case 0x1836: // reserved |
| 441 | d = 0x2330; |
| 442 | break; |
| 443 | case 0x2816: // DPC_X_MAX |
| 444 | d = 319; |
| 445 | break; |
| 446 | case 0x2818: // DPC_Y_MAX |
| 447 | d = 239; |
| 448 | break; |
| 449 | case 0x28da: |
| 450 | d = mmsp2.mlc_stl_cntl; |
| 451 | break; |
| 452 | case 0x290e: |
| 453 | case 0x2912: |
| 454 | d = mmsp2.mlc_stl_adrl; |
| 455 | break; |
| 456 | case 0x2910: |
| 457 | case 0x2914: |
| 458 | d = mmsp2.mlc_stl_adrh; |
| 459 | break; |
| 460 | case 0x2958: |
| 461 | d = mmsp2.mlc_stl_pallt_a; |
| 462 | break; |
| 463 | default: |
| 464 | goto unh; |
| 465 | } |
| 466 | goto out; |
| 467 | } |
| 468 | |
| 469 | unh: |
| 470 | if (a == old_a) { |
| 471 | d = fudge; |
| 472 | fudge = ~fudge; |
| 473 | } |
| 474 | old_a = a; |
| 475 | iolog_unh("r16", a, d & 0xffff, 16); |
| 476 | |
| 477 | out: |
| 478 | d &= 0xffff; |
| 479 | iolog("r16", a, d, 16); |
| 480 | return d; |
| 481 | } |
| 482 | |
| 483 | static u32 xread32(u32 a) |
| 484 | { |
| 485 | u32 d = 0; |
| 486 | if ((a & 0xfff00000) == 0x7f000000) { |
| 487 | u32 a_ = a & 0xffff; |
| 488 | switch (a_) { |
| 489 | case 0x0a00: // TCOUNT, 1/7372800s |
| 490 | // TODO |
| 491 | break; |
| 492 | } |
| 493 | } |
| 494 | if ((a & 0xfff00000) == 0x7f100000) { |
| 495 | u32 *bl = &blitter.dstctrl; |
| 496 | u32 a_ = a & 0xfff; |
| 497 | if (a_ < 0x40) { |
| 498 | d = bl[a_ / 4]; |
| 499 | if (a_ == 0x34) |
| 500 | d = 0; // not busy |
| 501 | goto out; |
| 502 | } |
| 503 | } |
| 504 | iolog_unh("r32", a, d, 32); |
| 505 | |
| 506 | out: |
| 507 | iolog("r32", a, d, 32); |
| 508 | return d; |
| 509 | } |
| 510 | |
| 511 | static void xwrite8(u32 a, u32 d) |
| 512 | { |
| 513 | iolog("w8 ", a, d, 8); |
| 514 | iolog_unh("w8 ", a, d, 8); |
| 515 | } |
| 516 | |
| 517 | static void xwrite16(u32 a, u32 d) |
| 518 | { |
| 519 | iolog("w16", a, d, 16); |
| 520 | if ((a & 0xfff00000) == 0x7f000000) { |
| 521 | u32 a_ = a & 0xffff; |
| 522 | switch (a_) { |
| 523 | case 0x28da: |
| 524 | mmsp2.mlc_stl_cntl = d | 0xaa; |
| 525 | break; |
| 526 | case 0x290e: |
| 527 | case 0x2910: |
| 528 | // odd addresses don't affect LCD. What about TV? |
| 529 | return; |
| 530 | case 0x2912: |
| 531 | mmsp2.mlc_stl_adrl = d; |
| 532 | return; |
| 533 | case 0x2914: |
| 534 | mmsp2.mlc_stl_adrh = d; |
| 535 | if (mmsp2.mlc_stl_adr != mmsp2.old_mlc_stl_adr) |
| 536 | // ask for refresh |
| 537 | pthread_cond_signal(&fb_cond); |
| 538 | mmsp2.old_mlc_stl_adr = mmsp2.mlc_stl_adr; |
| 539 | return; |
| 540 | case 0x2958: |
| 541 | mmsp2.mlc_stl_pallt_a = d & 0x1ff; |
| 542 | return; |
| 543 | case 0x295a: |
| 544 | mmsp2.mlc_stl_pallt_d[mmsp2.mlc_stl_pallt_a++] = d; |
| 545 | mmsp2.mlc_stl_pallt_a &= 0x1ff; |
| 546 | mmsp2.dirty_pal = 1; |
| 547 | return; |
| 548 | } |
| 549 | } |
| 550 | iolog_unh("w16", a, d, 16); |
| 551 | } |
| 552 | |
| 553 | static void xwrite32(u32 a, u32 d) |
| 554 | { |
| 555 | iolog("w32", a, d, 32); |
| 556 | |
| 557 | if ((a & 0xfff00000) == 0x7f100000) { |
| 558 | u32 *bl = &blitter.dstctrl; |
| 559 | u32 a_ = a & 0xfff; |
| 560 | if (a_ < 0x40) { |
| 561 | bl[a_ / 4] = d; |
| 562 | if (a_ == 0x34 && (d & 1)) |
| 563 | blitter_do(); |
| 564 | return; |
| 565 | } |
| 566 | } |
| 567 | iolog_unh("w32", a, d, 32); |
| 568 | } |
| 569 | |
| 570 | #define LINKPAGE_SIZE 0x1000 |
| 571 | #define LINKPAGE_COUNT 4 |
| 572 | #define LINKPAGE_ALLOC (LINKPAGE_SIZE * LINKPAGE_COUNT) |
| 573 | |
| 574 | struct op_context { |
| 575 | u32 pc; |
| 576 | u32 op; |
| 577 | u32 code[0]; |
| 578 | }; |
| 579 | |
| 580 | struct linkpage { |
| 581 | u32 saved_regs[15]; |
| 582 | u32 cpsr; |
| 583 | u32 *handler_stack; |
| 584 | void (*handler)(struct op_context *op_ctx); |
| 585 | u32 code[0]; |
| 586 | }; |
| 587 | |
| 588 | static struct linkpage *g_linkpage; |
| 589 | static u32 *g_code_ptr; |
| 590 | static int g_linkpage_count; |
| 591 | |
| 592 | static void *g_handler_stack_end; |
| 593 | |
| 594 | #define BIT_SET(v, b) (v & (1 << (b))) |
| 595 | |
| 596 | static void handle_op(struct op_context *op_ctx) |
| 597 | { |
| 598 | u32 *regs = g_linkpage->saved_regs; |
| 599 | u32 op = op_ctx->op; |
| 600 | u32 t, shift, ret, addr; |
| 601 | int rn, rd; |
| 602 | |
| 603 | rd = (op & 0x0000f000) >> 12; |
| 604 | rn = (op & 0x000f0000) >> 16; |
| 605 | |
| 606 | if ((op & 0x0f200090) == 0x01000090) { // AM3: LDRH, STRH |
| 607 | if (!BIT_SET(op, 5)) // !H |
| 608 | goto unhandled; |
| 609 | if (BIT_SET(op, 6) && !BIT_SET(op, 20)) // S && !L |
| 610 | goto unhandled; |
| 611 | |
| 612 | if (BIT_SET(op, 22)) // imm offset |
| 613 | t = ((op & 0xf00) >> 4) | (op & 0x0f); |
| 614 | else // reg offset |
| 615 | t = regs[op & 0x000f]; |
| 616 | |
| 617 | if (!BIT_SET(op, 23)) |
| 618 | t = -t; |
| 619 | addr = regs[rn] + t; |
| 620 | |
| 621 | if (BIT_SET(op, 20)) { // Load |
| 622 | ret = xread16(addr); |
| 623 | if (BIT_SET(op, 6)) { // S |
| 624 | ret <<= 16; |
| 625 | ret = (signed int)ret >> 16; |
| 626 | } |
| 627 | regs[rd] = ret; |
| 628 | } |
| 629 | else |
| 630 | xwrite16(addr, regs[rd]); |
| 631 | } |
| 632 | else if ((op & 0x0d200000) == 0x05000000) { // AM2: LDR[B], STR[B] |
| 633 | if (BIT_SET(op, 25)) { // reg offs |
| 634 | if (BIT_SET(op, 4)) |
| 635 | goto unhandled; |
| 636 | |
| 637 | t = regs[op & 0x000f]; |
| 638 | shift = (op & 0x0f80) >> 7; |
| 639 | switch ((op & 0x0060) >> 5) { |
| 640 | case 0: t = t << shift; break; |
| 641 | case 1: t = t >> (shift + 1); break; |
| 642 | case 2: t = (signed int)t >> (shift + 1); break; |
| 643 | case 3: goto unhandled; // I'm just lazy |
| 644 | } |
| 645 | } |
| 646 | else // imm offs |
| 647 | t = op & 0x0fff; |
| 648 | |
| 649 | if (!BIT_SET(op, 23)) |
| 650 | t = -t; |
| 651 | addr = regs[rn] + t; |
| 652 | |
| 653 | if (BIT_SET(op, 20)) { // Load |
| 654 | if (BIT_SET(op, 22)) // Byte |
| 655 | ret = xread8(addr); |
| 656 | else |
| 657 | ret = xread32(addr); |
| 658 | regs[rd] = ret; |
| 659 | } |
| 660 | else { |
| 661 | if (BIT_SET(op, 22)) // Byte |
| 662 | xwrite8(addr, regs[rd]); |
| 663 | else |
| 664 | xwrite32(addr, regs[rd]); |
| 665 | } |
| 666 | } |
| 667 | else |
| 668 | goto unhandled; |
| 669 | |
| 670 | #if 0 |
| 671 | if (addr != addr_check) { |
| 672 | fprintf(stderr, "bad calculated addr: %08x vs %08x\n", addr, addr_check); |
| 673 | abort(); |
| 674 | } |
| 675 | #endif |
| 676 | return; |
| 677 | |
| 678 | unhandled: |
| 679 | err("unhandled IO op %08x @ %08x\n", op, op_ctx->pc); |
| 680 | } |
| 681 | |
| 682 | static u32 make_offset12(u32 *pc, u32 *target) |
| 683 | { |
| 684 | int lp_offs, u = 1; |
| 685 | |
| 686 | lp_offs = (char *)target - (char *)pc - 2*4; |
| 687 | if (lp_offs < 0) { |
| 688 | lp_offs = -lp_offs; |
| 689 | u = 0; |
| 690 | } |
| 691 | if (lp_offs >= LINKPAGE_SIZE) { |
| 692 | err("linkpage too far: %d\n", lp_offs); |
| 693 | abort(); |
| 694 | } |
| 695 | |
| 696 | return (u << 23) | lp_offs; |
| 697 | } |
| 698 | |
| 699 | static u32 make_jmp(u32 *pc, u32 *target, int bl) |
| 700 | { |
| 701 | int jmp_val; |
| 702 | |
| 703 | jmp_val = target - pc - 2; |
| 704 | if (jmp_val < (int)0xff000000 || jmp_val > 0x00ffffff) { |
| 705 | err("jump out of range (%p -> %p)\n", pc, target); |
| 706 | abort(); |
| 707 | } |
| 708 | |
| 709 | return 0xea000000 | (bl << 24) | (jmp_val & 0x00ffffff); |
| 710 | } |
| 711 | |
| 712 | static void emit_op(u32 op) |
| 713 | { |
| 714 | *g_code_ptr++ = op; |
| 715 | } |
| 716 | |
| 717 | static void emit_op_io(u32 op, u32 *target) |
| 718 | { |
| 719 | op |= make_offset12(g_code_ptr, target); |
| 720 | emit_op(op); |
| 721 | } |
| 722 | |
| 723 | static void init_linkpage(void) |
| 724 | { |
| 725 | g_linkpage->handler = handle_op; |
| 726 | g_linkpage->handler_stack = g_handler_stack_end; |
| 727 | g_code_ptr = g_linkpage->code; |
| 728 | |
| 729 | // common_code. |
| 730 | // r0 and r14 must be saved by caller, r0 is arg for handle_op |
| 731 | // on return everything is restored except lr, which is used to return |
| 732 | emit_op_io(0xe50f1000, &g_linkpage->saved_regs[1]); // str r1, [->saved_regs[1]] @ save r1 |
| 733 | emit_op (0xe24f1000 + // sub r1, pc, =offs(saved_regs[2]) |
| 734 | (g_code_ptr - &g_linkpage->saved_regs[2] + 2) * 4); |
| 735 | emit_op (0xe8813ffc); // stmia r1, {r2-r13} |
| 736 | emit_op_io(0xe51fd000, // ldr sp, [->handler_stack] |
| 737 | (u32 *)&g_linkpage->handler_stack); |
| 738 | emit_op (0xe2414008); // sub r4, r1, #4*2 |
| 739 | emit_op (0xe10f1000); // mrs r1, cpsr |
| 740 | emit_op_io(0xe50f1000, &g_linkpage->cpsr); // str r1, [->cpsr] |
| 741 | emit_op (0xe1a0500e); // mov r5, lr |
| 742 | emit_op (0xe1a0e00f); // mov lr, pc |
| 743 | emit_op_io(0xe51ff000, (u32 *)&g_linkpage->handler); // ldr pc, =handle_op |
| 744 | emit_op_io(0xe51f1000, &g_linkpage->cpsr); // ldr r1, [->cpsr] |
| 745 | emit_op (0xe128f001); // msr cpsr_f, r1 |
| 746 | emit_op (0xe1a0e005); // mov lr, r5 |
| 747 | emit_op (0xe8943fff); // ldmia r4, {r0-r13} |
| 748 | emit_op (0xe12fff1e); // bx lr @ return |
| 749 | } |
| 750 | |
| 751 | static void segv_sigaction(int num, siginfo_t *info, void *ctx) |
| 752 | { |
| 753 | struct ucontext *context = ctx; |
| 754 | u32 *regs = (u32 *)&context->uc_mcontext.arm_r0; |
| 755 | u32 *pc = (u32 *)regs[15]; |
| 756 | struct op_context *op_ctx; |
| 757 | int lp_size; |
| 758 | |
| 759 | if (((regs[15] ^ (u32)&segv_sigaction) & 0xff000000) == 0 || // PC is in our segment or |
| 760 | (((regs[15] ^ (u32)g_linkpage) & ~(LINKPAGE_ALLOC - 1)) == 0) || // .. in linkpage |
| 761 | ((long)info->si_addr & 0xffe00000) != 0x7f000000) // faulting not where expected |
| 762 | { |
| 763 | // real crash - time to die |
| 764 | err("segv %d %p @ %08x\n", info->si_code, info->si_addr, regs[15]); |
| 765 | signal(num, SIG_DFL); |
| 766 | raise(num); |
| 767 | return; |
| 768 | } |
| 769 | segvlog("segv %d %p @ %08x\n", info->si_code, info->si_addr, regs[15]); |
| 770 | |
| 771 | // spit PC and op |
| 772 | op_ctx = (void *)g_code_ptr; |
| 773 | op_ctx->pc = (u32)pc; |
| 774 | op_ctx->op = *pc; |
| 775 | g_code_ptr = &op_ctx->code[0]; |
| 776 | |
| 777 | // emit jump to code ptr |
| 778 | *pc = make_jmp(pc, g_code_ptr, 0); |
| 779 | |
| 780 | // generate code: |
| 781 | // TODO: multithreading |
| 782 | emit_op_io(0xe50f0000, &g_linkpage->saved_regs[0]); // str r0, [->saved_regs[0]] @ save r0 |
| 783 | emit_op_io(0xe50fe000, &g_linkpage->saved_regs[14]); // str r14, [->saved_regs[14]] |
| 784 | emit_op (0xe24f0000 + (g_code_ptr - (u32 *)op_ctx + 2) * 4); // sub r0, pc, #op_ctx |
| 785 | emit_op (make_jmp(g_code_ptr, &g_linkpage->code[0], 1)); // bl common_code |
| 786 | emit_op_io(0xe51fe000, &g_linkpage->saved_regs[14]); // ldr r14, [->saved_regs[14]] |
| 787 | emit_op (make_jmp(g_code_ptr, pc + 1, 0)); // jmp <back> |
| 788 | |
| 789 | // sync caches |
| 790 | sys_cacheflush(pc, pc + 1); |
| 791 | sys_cacheflush(g_linkpage, g_code_ptr); |
| 792 | |
| 793 | lp_size = (char *)g_code_ptr - (char *)g_linkpage; |
| 794 | segvlog("code #%d %d/%d\n", g_linkpage_count, lp_size, LINKPAGE_SIZE); |
| 795 | |
| 796 | if (lp_size + 13*4 > LINKPAGE_SIZE) { |
| 797 | g_linkpage_count++; |
| 798 | if (g_linkpage_count >= LINKPAGE_COUNT) { |
| 799 | err("too many linkpages needed\n"); |
| 800 | abort(); |
| 801 | } |
| 802 | g_linkpage = (void *)((char *)g_linkpage + LINKPAGE_SIZE); |
| 803 | init_linkpage(); |
| 804 | } |
| 805 | //handle_op(regs[15], op, regs, (u32)info->si_addr); |
| 806 | //regs[15] += 4; |
| 807 | } |
| 808 | |
| 809 | void emu_init(void *map_bottom) |
| 810 | { |
| 811 | struct sigaction segv_action = { |
| 812 | .sa_sigaction = segv_sigaction, |
| 813 | .sa_flags = SA_SIGINFO, |
| 814 | }; |
| 815 | pthread_t tid; |
| 816 | void *pret; |
| 817 | int ret; |
| 818 | |
| 819 | g_handler_stack_end = (void *)((long)alloca(1536 * 1024) & ~0xffff); |
| 820 | log("handler stack @ %p (current %p)\n", g_handler_stack_end, &ret); |
| 821 | // touch it now. If we crash now we'll know why |
| 822 | *((char *)g_handler_stack_end - 4096) = 1; |
| 823 | |
| 824 | g_linkpage = (void *)(((u32)map_bottom - LINKPAGE_ALLOC) & ~0xfff); |
| 825 | pret = mmap(g_linkpage, LINKPAGE_ALLOC, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 826 | if (pret != g_linkpage) { |
| 827 | perror(PFX "mmap linkpage"); |
| 828 | exit(1); |
| 829 | } |
| 830 | log("linkpages @ %p\n", g_linkpage); |
| 831 | init_linkpage(); |
| 832 | |
| 833 | // host stuff |
| 834 | ret = host_video_init(&host_stride, 0); |
| 835 | if (ret != 0) { |
| 836 | err("can't alloc screen\n"); |
| 837 | exit(1); |
| 838 | } |
| 839 | host_screen = host_video_flip(); |
| 840 | |
| 841 | ret = pthread_create(&tid, NULL, fb_sync_thread, NULL); |
| 842 | if (ret != 0) { |
| 843 | err("failed to create fb_sync_thread: %d\n", ret); |
| 844 | exit(1); |
| 845 | } |
| 846 | pthread_detach(tid); |
| 847 | |
| 848 | // mmsp2 defaults |
| 849 | mmsp2.mlc_stl_adr = 0x03101000; // fb2 is at 0x03381000 |
| 850 | |
| 851 | sigemptyset(&segv_action.sa_mask); |
| 852 | sigaction(SIGSEGV, &segv_action, NULL); |
| 853 | } |
| 854 | |
| 855 | int emu_read_gpiodev(void *buf, int count) |
| 856 | { |
| 857 | unsigned int btns; |
| 858 | |
| 859 | if (count < 4) { |
| 860 | err("gpiodev read %d?\n", count); |
| 861 | return -1; |
| 862 | } |
| 863 | |
| 864 | btns = host_read_btns(); |
| 865 | memcpy(buf, &btns, 4); |
| 866 | return 4; |
| 867 | } |
| 868 | |
| 869 | struct dev_fd_t emu_interesting_fds[] = { |
| 870 | [IFD_SOUND] = { "/dev/dsp", -1 }, |
| 871 | { NULL, 0 }, |
| 872 | }; |
| 873 | |
| 874 | static void *emu_mmap_dev(unsigned int length, int prot, int flags, unsigned int offset) |
| 875 | { |
| 876 | struct uppermem_block *umem; |
| 877 | char name[32]; |
| 878 | int fd; |
| 879 | |
| 880 | // SoC regs |
| 881 | if ((offset & ~0xffff) == 0xc0000000) { |
| 882 | return mmap((void *)0x7f000000, length, PROT_NONE, |
| 883 | MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED|MAP_NORESERVE, -1, 0); |
| 884 | } |
| 885 | // blitter |
| 886 | if ((offset & ~0xffff) == 0xe0020000) { |
| 887 | return mmap((void *)0x7f100000, length, PROT_NONE, |
| 888 | MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED|MAP_NORESERVE, -1, 0); |
| 889 | } |
| 890 | // upper mem |
| 891 | if ((offset & 0xfe000000) != 0x02000000) |
| 892 | err("unexpected devmem mmap @ %08x\n", offset); |
| 893 | |
| 894 | umem = calloc(1, sizeof(*umem)); |
| 895 | if (umem == NULL) { |
| 896 | err("OOM\n"); |
| 897 | return MAP_FAILED; |
| 898 | } |
| 899 | |
| 900 | umem->addr = offset; |
| 901 | umem->size = length; |
| 902 | umem->mem = mmap(NULL, length, prot, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 903 | if (umem->mem != MAP_FAILED) |
| 904 | goto done; |
| 905 | |
| 906 | log("upper mem @ %08x %d mmap fail, trying backing file\n", offset, length); |
| 907 | sprintf(name, "m%08x", offset); |
| 908 | fd = open(name, O_CREAT|O_RDWR, 0644); |
| 909 | lseek(fd, length - 1, SEEK_SET); |
| 910 | name[0] = 0; |
| 911 | write(fd, name, 1); |
| 912 | |
| 913 | umem->mem = mmap(NULL, length, prot, MAP_SHARED, fd, 0); |
| 914 | if (umem->mem == MAP_FAILED) { |
| 915 | err("failed, giving up\n"); |
| 916 | close(fd); |
| 917 | free(umem); |
| 918 | errno = EINVAL; |
| 919 | return MAP_FAILED; |
| 920 | } |
| 921 | |
| 922 | done: |
| 923 | log("upper mem @ %08x %d\n", offset, length); |
| 924 | umem->next = upper_mem; |
| 925 | upper_mem = umem; |
| 926 | return umem->mem; |
| 927 | } |
| 928 | |
| 929 | void *emu_do_mmap(unsigned int length, int prot, int flags, int fd, unsigned int offset) |
| 930 | { |
| 931 | if (fd == FAKEDEV_MEM) |
| 932 | return emu_mmap_dev(length, prot, flags, offset); |
| 933 | |
| 934 | if (fd == FAKEDEV_FB0) |
| 935 | return emu_mmap_dev(length, prot, flags, offset + 0x03101000); |
| 936 | |
| 937 | if (fd == FAKEDEV_FB1) |
| 938 | return emu_mmap_dev(length, prot, flags, offset + 0x03381000); |
| 939 | |
| 940 | err("bad/ni mmap(?, %d, %x, %x, %d, %08x)", length, prot, flags, fd, offset); |
| 941 | errno = EINVAL; |
| 942 | return MAP_FAILED; |
| 943 | } |
| 944 | |
| 945 | #include <sys/ioctl.h> |
| 946 | #include <linux/soundcard.h> |
| 947 | |
| 948 | static int emu_sound_ioctl(int fd, int request, void *argp) |
| 949 | { |
| 950 | int *arg = argp; |
| 951 | |
| 952 | #if 0 |
| 953 | dbg("snd ioctl(%d, %08x, %p)", fd, request, argp); |
| 954 | if (arg != NULL) |
| 955 | dbg_c(" [%d]", *arg); |
| 956 | dbg_c("\n"); |
| 957 | #endif |
| 958 | |
| 959 | /* People set strange frag settings on GP2X, which even manage |
| 960 | * to break audio on pandora (causes writes to fail). |
| 961 | * Catch this and set to something that works. */ |
| 962 | if (request == SNDCTL_DSP_SPEED) { |
| 963 | int ret, bsize, frag; |
| 964 | |
| 965 | // ~4ms. gpSP wants small buffers or else it stutters |
| 966 | // because of it's audio thread sync stuff |
| 967 | bsize = *arg / 250 * 4; |
| 968 | for (frag = 0; bsize; bsize >>= 1, frag++) |
| 969 | ; |
| 970 | |
| 971 | frag |= 16 << 16; // fragment count |
| 972 | ret = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &frag); |
| 973 | if (ret != 0) { |
| 974 | err("snd ioctl SETFRAGMENT %08x: ", frag); |
| 975 | perror(NULL); |
| 976 | } |
| 977 | } |
| 978 | else if (request == SNDCTL_DSP_SETFRAGMENT) |
| 979 | return 0; |
| 980 | |
| 981 | return ioctl(fd, request, argp); |
| 982 | } |
| 983 | |
| 984 | #include <linux/fb.h> |
| 985 | |
| 986 | int emu_do_ioctl(int fd, int request, void *argp) |
| 987 | { |
| 988 | if (fd == emu_interesting_fds[IFD_SOUND].fd) |
| 989 | return emu_sound_ioctl(fd, request, argp); |
| 990 | |
| 991 | if (argp == NULL) |
| 992 | goto fail; |
| 993 | |
| 994 | if (fd == FAKEDEV_FB0 || fd == FAKEDEV_FB1) { |
| 995 | switch (request) { |
| 996 | case FBIOGET_FSCREENINFO: { |
| 997 | struct fb_fix_screeninfo *fix = argp; |
| 998 | |
| 999 | memset(fix, 0, sizeof(*fix)); |
| 1000 | strcpy(fix->id, "mmsp2_RGB0"); |
| 1001 | fix->type = FB_TYPE_PACKED_PIXELS; |
| 1002 | fix->accel = FB_ACCEL_NONE; |
| 1003 | fix->smem_start = (fd == FAKEDEV_FB0) ? 0x03101000 : 0x03381000; |
| 1004 | fix->smem_len = 320*240*2; |
| 1005 | return 0; |
| 1006 | } |
| 1007 | case FBIOGET_VSCREENINFO: { |
| 1008 | struct fb_var_screeninfo *var = argp; |
| 1009 | static const struct fb_bitfield fbb_red = { offset: 0, length: 4, }; |
| 1010 | static const struct fb_bitfield fbb_green = { offset: 0, length: 4, }; |
| 1011 | static const struct fb_bitfield fbb_blue = { offset: 0, length: 4, }; |
| 1012 | |
| 1013 | memset(var, 0, sizeof(*var)); |
| 1014 | var->activate = FB_ACTIVATE_NOW; |
| 1015 | var->xres = |
| 1016 | var->xres_virtual = 320; |
| 1017 | var->yres = |
| 1018 | var->yres_virtual = 240; |
| 1019 | var->width = |
| 1020 | var->height = -1; |
| 1021 | var->vmode = FB_VMODE_NONINTERLACED; |
| 1022 | var->bits_per_pixel = 16; |
| 1023 | var->red = fbb_red; |
| 1024 | var->green = fbb_green; |
| 1025 | var->blue = fbb_blue; |
| 1026 | return 0; |
| 1027 | } |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | fail: |
| 1032 | err("bad/ni ioctl(%d, %08x, %p)", fd, request, argp); |
| 1033 | errno = EINVAL; |
| 1034 | return -1; |
| 1035 | } |
| 1036 | |