From 0300a353a18f9af68e4c2a66496a96e680bffa1b Mon Sep 17 00:00:00 2001 From: notaz Date: Sun, 13 Aug 2023 19:15:31 +0300 Subject: [PATCH] dma: clean up libretro/pcsx_rearmed#740 --- libpcsxcore/psxdma.c | 105 ++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 57 deletions(-) diff --git a/libpcsxcore/psxdma.c b/libpcsxcore/psxdma.c index e15f0185..31424b3d 100644 --- a/libpcsxcore/psxdma.c +++ b/libpcsxcore/psxdma.c @@ -27,6 +27,9 @@ #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 @@ -40,45 +43,38 @@ void spuInterrupt() { } void psxDma4(u32 madr, u32 bcr, u32 chcr) { // SPU - u32 words, words_max, size; + u32 words, words_max = 0, words_copy; u16 *ptr; + 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 = getDmaRam(madr, &words_max); - if (ptr == INVALID_PTR) { -#ifdef CPU_LOG - CPU_LOG("*** DMA4 SPU - mem2spu *** NULL Pointer!!!\n"); -#endif + if (ptr == INVALID_PTR) break; - } - words = (bcr >> 16) * (bcr & 0xffff); - size = min(words, words_max) * 2; - SPU_writeDMAMem(ptr, size, psxRegs.cycle); - HW_DMA4_MADR = SWAPu32((madr & ~3) + words * 4); + SPU_writeDMAMem(ptr, words_copy * 2, psxRegs.cycle); + HW_DMA4_MADR = SWAPu32(madr + words_copy * 2); SPUDMA_INT(words * 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 = getDmaRam(madr, &words_max); - if (ptr == INVALID_PTR) { -#ifdef CPU_LOG - CPU_LOG("*** DMA4 SPU - spu2mem *** NULL Pointer!!!\n"); -#endif + if (ptr == INVALID_PTR) break; - } - words = (bcr >> 16) * (bcr & 0xffff); - size = min(words, words_max) * 2; - SPU_readDMAMem(ptr, size, 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); + HW_DMA4_MADR = SWAPu32(madr + words_copy * 4); SPUDMA_INT(words * 4); return; @@ -134,58 +130,56 @@ static u32 gpuDmaChainSize(u32 addr) { void psxDma2(u32 madr, u32 bcr, u32 chcr) { // GPU u32 *ptr, madr_next, *madr_next_p, size; - u32 words, words_max, words_copy; + u32 words, words_left, words_max, words_copy; int 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 = getDmaRam(madr, &words_max); if (ptr == INVALID_PTR) { -#ifdef CPU_LOG - CPU_LOG("*** DMA2 GPU - vram2mem *** NULL Pointer!!!\n"); -#endif + log_unhandled("bad dma2 madr %x\n", madr); break; } // BA blocks * BS words (word = 32-bits) - words = (bcr >> 16) * (bcr & 0xffff); - words_copy = min(words, words_max); + 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 & ~3) + words * 4); + HW_DMA2_MADR = SWAPu32(madr + words_copy * 4); // already 32-bit word size ((size * 4) / 4) GPUDMA_INT(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 = getDmaRam(madr, &words_max); - if (ptr == INVALID_PTR) { -#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, min(words, words_max)); - HW_DMA2_MADR = SWAPu32((madr & ~3) + words * 4); + HW_DMA2_MADR = SWAPu32(madr); // already 32-bit word size ((size * 4) / 4) GPUDMA_INT(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; @@ -235,18 +229,15 @@ void gpuInterrupt() { } 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) { + mem = getDmaRam(madr, &words_max); if (mem == INVALID_PTR) { -#ifdef CPU_LOG - CPU_LOG("*** DMA6 OT *** NULL Pointer!!!\n"); -#endif + log_unhandled("bad6 dma madr %x\n", madr); HW_DMA6_CHCR &= SWAP32(~0x01000000); DMA_INTERRUPT(6); return; @@ -255,7 +246,7 @@ 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; } -- 2.39.5