X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=plugins%2Fgpulib%2Fgpu.c;h=17386b403cc17b258a305a841ce15656ea879af0;hb=12367ad02622ba0e6457a7bea7b859bcf85ecb46;hp=b61bff607d32b84add3269a5f7ec716deaae2f22;hpb=9ee0fd5b333039b1140d90f935aa9299825f1e42;p=pcsx_rearmed.git diff --git a/plugins/gpulib/gpu.c b/plugins/gpulib/gpu.c index b61bff60..17386b40 100644 --- a/plugins/gpulib/gpu.c +++ b/plugins/gpulib/gpu.c @@ -10,11 +10,20 @@ #include #include +#include /* for calloc */ + #include "gpu.h" #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) +#ifdef __GNUC__ #define unlikely(x) __builtin_expect((x), 0) +#define preload __builtin_prefetch #define noinline __attribute__((noinline)) +#else +#define unlikely(x) +#define preload(...) +#define noinline +#endif #define gpu_log(fmt, ...) \ printf("%d:%03d: " fmt, *gpu.state.frame_count, *gpu.state.hcnt, ##__VA_ARGS__) @@ -68,10 +77,11 @@ static noinline void update_width(void) static noinline void update_height(void) { + // TODO: emulate this properly.. int sh = gpu.screen.y2 - gpu.screen.y1; if (gpu.status.dheight) sh *= 2; - if (sh <= 0) + if (sh <= 0 || sh > gpu.screen.vres) sh = gpu.screen.vres; gpu.screen.h = sh; @@ -118,11 +128,11 @@ static noinline void get_gpu_info(uint32_t data) case 0x02: case 0x03: case 0x04: - case 0x05: gpu.gp0 = gpu.ex_regs[data & 7] & 0xfffff; break; + case 0x05: case 0x06: - gpu.gp0 = gpu.ex_regs[5] & 0xfffff; + gpu.gp0 = gpu.ex_regs[5] & 0x3fffff; break; case 0x07: gpu.gp0 = 2; @@ -134,13 +144,30 @@ static noinline void get_gpu_info(uint32_t data) } // double, for overdraw guard -#define VRAM_SIZE (1024 * 512 * 2 * 2) +#define VRAM_SIZE ((1024 * 512 * 2 * 2) + 4096) + +// Minimum 16-byte VRAM alignment needed by gpu_unai's pixel-skipping +// renderer/downscaler it uses in high res modes: +#ifdef GCW_ZERO + // On GCW platform (MIPS), align to 8192 bytes (1 TLB entry) to reduce # of + // fills. (Will change this value if it ever gets large page support) + #define VRAM_ALIGN 8192 +#else + #define VRAM_ALIGN 16 +#endif + +// vram ptr received from mmap/malloc/alloc (will deallocate using this) +static uint16_t *vram_ptr_orig = NULL; +#ifdef GPULIB_USE_MMAP static int map_vram(void) { - gpu.vram = gpu.mmap(VRAM_SIZE); + gpu.vram = vram_ptr_orig = gpu.mmap(VRAM_SIZE + (VRAM_ALIGN-1)); if (gpu.vram != NULL) { - gpu.vram += 4096 / 2; + // 4kb guard in front + gpu.vram += (4096 / 2); + // Align + gpu.vram = (uint16_t*)(((uintptr_t)gpu.vram + (VRAM_ALIGN-1)) & ~(VRAM_ALIGN-1)); return 0; } else { @@ -148,9 +175,54 @@ static int map_vram(void) return -1; } } +#else +static int map_vram(void) +{ + gpu.vram = vram_ptr_orig = (uint16_t*)calloc(VRAM_SIZE + (VRAM_ALIGN-1), 1); + if (gpu.vram != NULL) { + // 4kb guard in front + gpu.vram += (4096 / 2); + // Align + gpu.vram = (uint16_t*)(((uintptr_t)gpu.vram + (VRAM_ALIGN-1)) & ~(VRAM_ALIGN-1)); + return 0; + } else { + fprintf(stderr, "could not allocate vram, expect crashes\n"); + return -1; + } +} + +static int allocate_vram(void) +{ + gpu.vram = vram_ptr_orig = (uint16_t*)calloc(VRAM_SIZE + (VRAM_ALIGN-1), 1); + if (gpu.vram != NULL) { + // 4kb guard in front + gpu.vram += (4096 / 2); + // Align + gpu.vram = (uint16_t*)(((uintptr_t)gpu.vram + (VRAM_ALIGN-1)) & ~(VRAM_ALIGN-1)); + return 0; + } else { + fprintf(stderr, "could not allocate vram, expect crashes\n"); + return -1; + } +} +#endif long GPUinit(void) { +#ifndef GPULIB_USE_MMAP + if (gpu.vram == NULL) { + if (allocate_vram() != 0) { + printf("ERROR: could not allocate VRAM, exiting..\n"); + exit(1); + } + } +#endif + + //extern uint32_t hSyncCount; // in psxcounters.cpp + //extern uint32_t frame_counter; // in psxcounters.cpp + //gpu.state.hcnt = &hSyncCount; + //gpu.state.frame_count = &frame_counter; + int ret; ret = vout_init(); ret |= renderer_init(); @@ -161,10 +233,10 @@ long GPUinit(void) gpu.cmd_len = 0; do_reset(); - if (gpu.mmap != NULL) { + /*if (gpu.mmap != NULL) { if (map_vram() != 0) ret = -1; - } + }*/ return ret; } @@ -174,17 +246,24 @@ long GPUshutdown(void) renderer_finish(); ret = vout_finish(); - if (gpu.vram != NULL) { - gpu.vram -= 4096 / 2; - gpu.munmap(gpu.vram, VRAM_SIZE); + + if (vram_ptr_orig != NULL) { +#ifdef GPULIB_USE_MMAP + gpu.munmap(vram_ptr_orig, VRAM_SIZE); +#else + free(vram_ptr_orig); +#endif } - gpu.vram = NULL; + vram_ptr_orig = gpu.vram = NULL; return ret; } void GPUwriteStatus(uint32_t data) { + //senquack TODO: Would it be wise to add cmd buffer flush here, since + // status settings can affect commands already in buffer? + static const short hres[8] = { 256, 368, 320, 384, 512, 512, 640, 640 }; static const short vres[4] = { 240, 480, 256, 480 }; uint32_t cmd = data >> 24; @@ -212,7 +291,7 @@ void GPUwriteStatus(uint32_t data) break; case 0x05: gpu.screen.x = data & 0x3ff; - gpu.screen.y = (data >> 10) & 0x3ff; + gpu.screen.y = (data >> 10) & 0x1ff; if (gpu.frameskip.set) { decide_frameskip_allow(gpu.ex_regs[3]); if (gpu.frameskip.last_flip_frame != *gpu.state.frame_count) { @@ -367,46 +446,62 @@ static void finish_vram_transfer(int is_read) static noinline int do_cmd_list_skip(uint32_t *data, int count, int *last_cmd) { - int cmd = 0, pos = 0, len, dummy; + int cmd = 0, pos = 0, len, dummy, v; int skip = 1; gpu.frameskip.pending_fill[0] = 0; - // XXX: polylines are not properly handled while (pos < count && skip) { uint32_t *list = data + pos; cmd = list[0] >> 24; len = 1 + cmd_lengths[cmd]; - if (cmd == 0x02) { - if ((list[2] & 0x3ff) > gpu.screen.w || ((list[2] >> 16) & 0x1ff) > gpu.screen.h) - // clearing something large, don't skip - do_cmd_list(list, 3, &dummy); - else - memcpy(gpu.frameskip.pending_fill, list, 3 * 4); - } - else if ((cmd & 0xf4) == 0x24) { - // flat textured prim - gpu.ex_regs[1] &= ~0x1ff; - gpu.ex_regs[1] |= list[4] & 0x1ff; - } - else if ((cmd & 0xf4) == 0x34) { - // shaded textured prim - gpu.ex_regs[1] &= ~0x1ff; - gpu.ex_regs[1] |= list[5] & 0x1ff; + switch (cmd) { + case 0x02: + if ((int)(list[2] & 0x3ff) > gpu.screen.w || (int)((list[2] >> 16) & 0x1ff) > gpu.screen.h) + // clearing something large, don't skip + do_cmd_list(list, 3, &dummy); + else + memcpy(gpu.frameskip.pending_fill, list, 3 * 4); + break; + case 0x24 ... 0x27: + case 0x2c ... 0x2f: + case 0x34 ... 0x37: + case 0x3c ... 0x3f: + gpu.ex_regs[1] &= ~0x1ff; + gpu.ex_regs[1] |= list[4 + ((cmd >> 4) & 1)] & 0x1ff; + break; + case 0x48 ... 0x4F: + for (v = 3; pos + v < count; v++) + { + if ((list[v] & 0xf000f000) == 0x50005000) + break; + } + len += v - 3; + break; + case 0x58 ... 0x5F: + for (v = 4; pos + v < count; v += 2) + { + if ((list[v] & 0xf000f000) == 0x50005000) + break; + } + len += v - 4; + break; + default: + if (cmd == 0xe3) + skip = decide_frameskip_allow(list[0]); + if ((cmd & 0xf8) == 0xe0) + gpu.ex_regs[cmd & 7] = list[0]; + break; } - else if (cmd == 0xe3) - skip = decide_frameskip_allow(list[0]); - - if ((cmd & 0xf8) == 0xe0) - gpu.ex_regs[cmd & 7] = list[0]; if (pos + len > count) { cmd = -1; break; // incomplete cmd } - if (cmd == 0xa0 || cmd == 0xc0) + if (0xa0 <= cmd && cmd <= 0xdf) break; // image i/o + pos += len; } @@ -432,9 +527,9 @@ static noinline int do_cmd_buffer(uint32_t *data, int count) } cmd = data[pos] >> 24; - if (cmd == 0xa0 || cmd == 0xc0) { + if (0xa0 <= cmd && cmd <= 0xdf) { // consume vram write/read cmd - start_vram_transfer(data[pos + 1], data[pos + 2], cmd == 0xc0); + start_vram_transfer(data[pos + 1], data[pos + 2], (cmd & 0xe0) == 0xc0); pos += 3; continue; } @@ -496,60 +591,60 @@ void GPUwriteData(uint32_t data) long GPUdmaChain(uint32_t *rambase, uint32_t start_addr) { - uint32_t addr, *list; - uint32_t *llist_entry = NULL; + uint32_t addr, *list, ld_addr = 0; int len, left, count; long cpu_cycles = 0; + preload(rambase + (start_addr & 0x1fffff) / 4); + if (unlikely(gpu.cmd_len > 0)) flush_cmd_buffer(); - // ff7 sends it's main list twice, detect this - if (*gpu.state.frame_count == gpu.state.last_list.frame && - *gpu.state.hcnt - gpu.state.last_list.hcnt <= 1 && - gpu.state.last_list.cycles > 2048) - { - llist_entry = rambase + (gpu.state.last_list.addr & 0x1fffff) / 4; - *llist_entry |= 0x800000; - } - log_io("gpu_dma_chain\n"); addr = start_addr & 0xffffff; - for (count = 0; addr != 0xffffff; count++) + for (count = 0; (addr & 0x800000) == 0; count++) { list = rambase + (addr & 0x1fffff) / 4; len = list[0] >> 24; addr = list[0] & 0xffffff; + preload(rambase + (addr & 0x1fffff) / 4); + cpu_cycles += 10; if (len > 0) cpu_cycles += 5 + len; log_io(".chain %08x #%d\n", (list - rambase) * 4, len); - // loop detection marker - // (bit23 set causes DMA error on real machine, so - // unlikely to be ever set by the game) - list[0] |= 0x800000; - if (len) { left = do_cmd_buffer(list + 1, len); if (left) log_anomaly("GPUdmaChain: discarded %d/%d words\n", left, len); } - if (addr & 0x800000) - break; + #define LD_THRESHOLD (8*1024) + if (count >= LD_THRESHOLD) { + if (count == LD_THRESHOLD) { + ld_addr = addr; + continue; + } + + // loop detection marker + // (bit23 set causes DMA error on real machine, so + // unlikely to be ever set by the game) + list[0] |= 0x800000; + } } - // remove loop detection markers - addr = start_addr & 0x1fffff; - while (count-- > 0) { - list = rambase + addr / 4; - addr = list[0] & 0x1fffff; - list[0] &= ~0x800000; + if (ld_addr != 0) { + // remove loop detection markers + count -= LD_THRESHOLD + 2; + addr = ld_addr & 0x1fffff; + while (count-- > 0) { + list = rambase + addr / 4; + addr = list[0] & 0x1fffff; + list[0] &= ~0x800000; + } } - if (llist_entry) - *llist_entry &= ~0x800000; gpu.state.last_list.frame = *gpu.state.frame_count; gpu.state.last_list.hcnt = *gpu.state.hcnt; @@ -702,6 +797,7 @@ void GPUrearmedCallbacks(const struct rearmed_cbs *cbs) gpu.state.allow_interlace = cbs->gpu_neon.allow_interlace; gpu.state.enhancement_enable = cbs->gpu_neon.enhancement_enable; + gpu.useDithering = cbs->gpu_neon.allow_dithering; gpu.mmap = cbs->mmap; gpu.munmap = cbs->munmap;