X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?p=pcsx_rearmed.git;a=blobdiff_plain;f=libpcsxcore%2Fpsxdma.c;h=03ee563913ad9402a1b62af612b218b0d7689dc6;hp=e6f68fc2a05394237ca0025f755bdaa687eff9f1;hb=HEAD;hpb=4f329f16f58b969df7ec9081549a7bbd70814f55 diff --git a/libpcsxcore/psxdma.c b/libpcsxcore/psxdma.c index e6f68fc2..55d2a0a7 100644 --- a/libpcsxcore/psxdma.c +++ b/libpcsxcore/psxdma.c @@ -24,6 +24,13 @@ #include "psxdma.h" #include "gpu.h" +#ifndef min +#define min(a, b) ((b) < (a) ? (b) : (a)) +#endif +#ifndef PSXDMA_LOG +#define PSXDMA_LOG(...) +#endif + // Dma0/1 in Mdec.c // Dma3 in CdRom.c @@ -36,57 +43,54 @@ void spuInterrupt() { } void psxDma4(u32 madr, u32 bcr, u32 chcr) { // SPU + u32 words, words_max = 0, words_copy; u16 *ptr; - u32 words; + + madr &= ~3; + ptr = getDmaRam(madr, &words_max); + if (ptr == INVALID_PTR) + log_unhandled("bad dma4 madr %x\n", madr); + + words = words_copy = (bcr >> 16) * (bcr & 0xffff); + if (words_copy > words_max) { + log_unhandled("bad dma4 madr %x bcr %x\n", madr, bcr); + words_copy = words_max; + } switch (chcr) { case 0x01000201: //cpu to spu transfer -#ifdef PSXDMA_LOG PSXDMA_LOG("*** DMA4 SPU - mem2spu *** %x addr = %x size = %x\n", chcr, madr, bcr); -#endif - ptr = (u16 *)PSXM(madr); - if (ptr == NULL) { -#ifdef CPU_LOG - CPU_LOG("*** DMA4 SPU - mem2spu *** NULL Pointer!!!\n"); -#endif + if (ptr == INVALID_PTR) break; - } - words = (bcr >> 16) * (bcr & 0xffff); - SPU_writeDMAMem(ptr, words * 2, psxRegs.cycle); - HW_DMA4_MADR = SWAPu32(madr + words * 4); - SPUDMA_INT(words * 4); + SPU_writeDMAMem(ptr, words_copy * 2, psxRegs.cycle); + HW_DMA4_MADR = SWAPu32(madr + words_copy * 2); + // This should be much slower, like 12+ cycles/byte, it's like + // that because the CPU runs too fast and fifo is not emulated. + // See also set_dma_end(). + set_event(PSXINT_SPUDMA, words * 4 * 4); return; case 0x01000200: //spu to cpu transfer -#ifdef PSXDMA_LOG PSXDMA_LOG("*** DMA4 SPU - spu2mem *** %x addr = %x size = %x\n", chcr, madr, bcr); -#endif - ptr = (u16 *)PSXM(madr); - if (ptr == NULL) { -#ifdef CPU_LOG - CPU_LOG("*** DMA4 SPU - spu2mem *** NULL Pointer!!!\n"); -#endif + if (ptr == INVALID_PTR) break; - } - words = (bcr >> 16) * (bcr & 0xffff); - SPU_readDMAMem(ptr, words * 2, psxRegs.cycle); - psxCpu->Clear(madr, words); + SPU_readDMAMem(ptr, words_copy * 2, psxRegs.cycle); + psxCpu->Clear(madr, words_copy); - HW_DMA4_MADR = SWAPu32(madr + words * 4); - SPUDMA_INT(words * 4); + HW_DMA4_MADR = SWAPu32(madr + words_copy * 4); + set_event(PSXINT_SPUDMA, words * 4 * 4); return; -#ifdef PSXDMA_LOG default: - PSXDMA_LOG("*** DMA4 SPU - unknown *** %x addr = %x size = %x\n", chcr, madr, bcr); + log_unhandled("*** DMA4 SPU - unknown *** %x addr = %x size = %x\n", chcr, madr, bcr); break; -#endif } HW_DMA4_CHCR &= SWAP32(~0x01000000); DMA_INTERRUPT(4); } +#if 0 // Taken from PEOPS SOFTGPU static inline boolean CheckForEndlessLoop(u32 laddr, u32 *lUsedAddr) { if (laddr == lUsedAddr[1]) return TRUE; @@ -127,84 +131,90 @@ static u32 gpuDmaChainSize(u32 addr) { return size; } +#endif void psxDma2(u32 madr, u32 bcr, u32 chcr) { // GPU - u32 *ptr; - u32 words; - u32 size; + u32 *ptr, madr_next, *madr_next_p; + u32 words, words_left, words_max, words_copy; + int cycles_sum, cycles_last_cmd = 0, do_walking; + madr &= ~3; switch (chcr) { case 0x01000200: // vram2mem -#ifdef PSXDMA_LOG PSXDMA_LOG("*** DMA2 GPU - vram2mem *** %lx addr = %lx size = %lx\n", chcr, madr, bcr); -#endif - ptr = (u32 *)PSXM(madr); - if (ptr == NULL) { -#ifdef CPU_LOG - CPU_LOG("*** DMA2 GPU - vram2mem *** NULL Pointer!!!\n"); -#endif + ptr = getDmaRam(madr, &words_max); + if (ptr == INVALID_PTR) { + log_unhandled("bad dma2 madr %x\n", madr); break; } // BA blocks * BS words (word = 32-bits) - words = (bcr >> 16) * (bcr & 0xffff); - GPU_readDataMem(ptr, words); - psxCpu->Clear(madr, words); + words = words_copy = (bcr >> 16) * (bcr & 0xffff); + if (words > words_max) { + log_unhandled("bad dma2 madr %x bcr %x\n", madr, bcr); + words_copy = words_max; + } + GPU_readDataMem(ptr, words_copy); + psxCpu->Clear(madr, words_copy); - HW_DMA2_MADR = SWAPu32(madr + words * 4); + HW_DMA2_MADR = SWAPu32(madr + words_copy * 4); + // careful: gpu_state_change() also messes with this + psxRegs.gpuIdleAfter = psxRegs.cycle + words / 4 + 16; // already 32-bit word size ((size * 4) / 4) - GPUDMA_INT(words / 4); + set_event(PSXINT_GPUDMA, words / 4); return; case 0x01000201: // mem2vram -#ifdef PSXDMA_LOG PSXDMA_LOG("*** DMA 2 - GPU mem2vram *** %lx addr = %lx size = %lx\n", chcr, madr, bcr); -#endif - ptr = (u32 *)PSXM(madr); - if (ptr == NULL) { -#ifdef CPU_LOG - CPU_LOG("*** DMA2 GPU - mem2vram *** NULL Pointer!!!\n"); -#endif - break; + words = words_left = (bcr >> 16) * (bcr & 0xffff); + while (words_left > 0) { + ptr = getDmaRam(madr, &words_max); + if (ptr == INVALID_PTR) { + log_unhandled("bad2 dma madr %x\n", madr); + break; + } + words_copy = min(words_left, words_max); + GPU_writeDataMem(ptr, words_copy); + words_left -= words_copy; + madr += words_copy * 4; } - // BA blocks * BS words (word = 32-bits) - words = (bcr >> 16) * (bcr & 0xffff); - GPU_writeDataMem(ptr, words); - HW_DMA2_MADR = SWAPu32(madr + words * 4); + HW_DMA2_MADR = SWAPu32(madr); + // careful: gpu_state_change() also messes with this + psxRegs.gpuIdleAfter = psxRegs.cycle + words / 4 + 16; // already 32-bit word size ((size * 4) / 4) - GPUDMA_INT(words / 4); + set_event(PSXINT_GPUDMA, words / 4); return; case 0x01000401: // dma chain -#ifdef PSXDMA_LOG PSXDMA_LOG("*** DMA 2 - GPU dma chain *** %lx addr = %lx size = %lx\n", chcr, madr, bcr); -#endif + // when not emulating walking progress, end immediately + madr_next = 0xffffff; - size = GPU_dmaChain((u32 *)psxM, madr & 0x1fffff); - if ((int)size <= 0) - size = gpuDmaChainSize(madr); - HW_GPU_STATUS &= SWAP32(~PSXGPU_nBUSY); + do_walking = Config.GpuListWalking; + if (do_walking < 0 || Config.hacks.gpu_timing1024) + do_walking = Config.hacks.gpu_slow_list_walking; + madr_next_p = do_walking ? &madr_next : NULL; - // we don't emulate progress, just busy flag and end irq, - // so pretend we're already at the last block - HW_DMA2_MADR = SWAPu32(0xffffff); + cycles_sum = GPU_dmaChain((u32 *)psxM, madr & 0x1fffff, + madr_next_p, &cycles_last_cmd); - // Tekken 3 = use 1.0 only (not 1.5x) + HW_DMA2_MADR = SWAPu32(madr_next); - // Einhander = parse linked list in pieces (todo) - // Final Fantasy 4 = internal vram time (todo) - // Rebel Assault 2 = parse linked list in pieces (todo) - // Vampire Hunter D = allow edits to linked list (todo) - GPUDMA_INT(size); + // a hack for Judge Dredd which is annoyingly sensitive to timing + if (Config.hacks.gpu_timing1024) + cycles_sum = 1024; + + psxRegs.gpuIdleAfter = psxRegs.cycle + cycles_sum + cycles_last_cmd; + set_event(PSXINT_GPUDMA, cycles_sum); + //printf("%u dma2cf: %6d,%4d %08x %08x %08x %08x\n", psxRegs.cycle, + // cycles_sum, cycles_last_cmd, madr, bcr, chcr, HW_DMA2_MADR); return; -#ifdef PSXDMA_LOG default: - PSXDMA_LOG("*** DMA 2 - GPU unknown *** %lx addr = %lx size = %lx\n", chcr, madr, bcr); + log_unhandled("*** DMA 2 - GPU unknown *** %x addr = %x size = %x\n", chcr, madr, bcr); break; -#endif } HW_DMA2_CHCR &= SWAP32(~0x01000000); @@ -212,28 +222,44 @@ void psxDma2(u32 madr, u32 bcr, u32 chcr) { // GPU } void gpuInterrupt() { + if (HW_DMA2_CHCR == SWAP32(0x01000401) && !(HW_DMA2_MADR & SWAP32(0x800000))) + { + u32 madr_next = 0xffffff, madr = SWAPu32(HW_DMA2_MADR); + int cycles_sum, cycles_last_cmd = 0; + cycles_sum = GPU_dmaChain((u32 *)psxM, madr & 0x1fffff, + &madr_next, &cycles_last_cmd); + HW_DMA2_MADR = SWAPu32(madr_next); + if ((s32)(psxRegs.gpuIdleAfter - psxRegs.cycle) > 0) + cycles_sum += psxRegs.gpuIdleAfter - psxRegs.cycle; + psxRegs.gpuIdleAfter = psxRegs.cycle + cycles_sum + cycles_last_cmd; + set_event(PSXINT_GPUDMA, cycles_sum); + //printf("%u dma2cn: %6d,%4d %08x\n", psxRegs.cycle, cycles_sum, + // cycles_last_cmd, HW_DMA2_MADR); + return; + } if (HW_DMA2_CHCR & SWAP32(0x01000000)) { HW_DMA2_CHCR &= SWAP32(~0x01000000); DMA_INTERRUPT(2); } - HW_GPU_STATUS |= SWAP32(PSXGPU_nBUSY); // GPU no longer busy +} + +void psxAbortDma2() { + psxRegs.gpuIdleAfter = psxRegs.cycle + 32; } void psxDma6(u32 madr, u32 bcr, u32 chcr) { - u32 words; - u32 *mem = (u32 *)PSXM(madr); + u32 words, words_max; + u32 *mem; -#ifdef PSXDMA_LOG PSXDMA_LOG("*** DMA6 OT *** %x addr = %x size = %x\n", chcr, madr, bcr); -#endif if (chcr == 0x11000002) { - if (mem == NULL) { -#ifdef CPU_LOG - CPU_LOG("*** DMA6 OT *** NULL Pointer!!!\n"); -#endif - HW_DMA6_CHCR &= SWAP32(~0x01000000); + madr &= ~3; + mem = getDmaRam(madr, &words_max); + if (mem == INVALID_PTR) { + log_unhandled("bad6 dma madr %x\n", madr); + HW_DMA6_CHCR &= SWAP32(~0x11000000); DMA_INTERRUPT(6); return; } @@ -241,26 +267,23 @@ void psxDma6(u32 madr, u32 bcr, u32 chcr) { // already 32-bit size words = bcr; - while (bcr--) { + while (bcr-- && mem > (u32 *)psxM) { *mem-- = SWAP32((madr - 4) & 0xffffff); madr -= 4; } *++mem = SWAP32(0xffffff); - //GPUOTCDMA_INT(size); // halted psxRegs.cycle += words; - GPUOTCDMA_INT(16); + set_event(PSXINT_GPUOTCDMA, 16); return; } -#ifdef PSXDMA_LOG else { // Unknown option - PSXDMA_LOG("*** DMA6 OT - unknown *** %x addr = %x size = %x\n", chcr, madr, bcr); + log_unhandled("*** DMA6 OT - unknown *** %x addr = %x size = %x\n", chcr, madr, bcr); } -#endif - HW_DMA6_CHCR &= SWAP32(~0x01000000); + HW_DMA6_CHCR &= SWAP32(~0x11000000); DMA_INTERRUPT(6); } @@ -268,7 +291,7 @@ void gpuotcInterrupt() { if (HW_DMA6_CHCR & SWAP32(0x01000000)) { - HW_DMA6_CHCR &= SWAP32(~0x01000000); + HW_DMA6_CHCR &= SWAP32(~0x11000000); DMA_INTERRUPT(6); } }