project64/Source/Project64-rsp/dma.cpp

138 lines
3.7 KiB
C++
Raw Normal View History

#include <stdint.h>
2016-01-27 09:11:59 +00:00
#include <stdio.h>
2016-02-13 20:01:41 +00:00
#include <string.h>
2016-01-27 09:11:59 +00:00
#include "RSP Registers.h"
#include "Rsp.h"
2016-01-27 09:11:59 +00:00
#include "memory.h"
// #define RSP_SAFE_DMA // Unoptimized DMA transfers
2016-01-27 09:11:59 +00:00
void SP_DMA_READ(void)
2016-01-27 09:11:59 +00:00
{
uint32_t i, j, Length, Skip, Count, End, addr;
uint8_t *Dest, *Source;
2016-01-27 09:11:59 +00:00
addr = (*RSPInfo.SP_DRAM_ADDR_REG) & 0x00FFFFFF;
if (addr > 0x7FFFFF)
{
DisplayError("SP DMA READ\nSP_DRAM_ADDR_REG not in RDRAM space");
return;
}
if ((*RSPInfo.SP_RD_LEN_REG & 0xFFF) + 1 + (*RSPInfo.SP_MEM_ADDR_REG & 0xFFF) > 0x1000)
{
DisplayError("SP DMA READ\nCould not fit copy in memory segment");
return;
}
2016-01-27 09:11:59 +00:00
Length = ((*RSPInfo.SP_RD_LEN_REG & 0xFFF) | 7) + 1;
Skip = (*RSPInfo.SP_RD_LEN_REG >> 20) + Length;
Count = ((*RSPInfo.SP_RD_LEN_REG >> 12) & 0xFF) + 1;
End = ((*RSPInfo.SP_MEM_ADDR_REG & 0x0FFF) & ~7) + (((Count - 1) * Skip) + Length);
Dest = ((*RSPInfo.SP_MEM_ADDR_REG & 0x1000) != 0 ? RSPInfo.IMEM : RSPInfo.DMEM) + ((*RSPInfo.SP_MEM_ADDR_REG & 0x0FFF) & ~7);
Source = RSPInfo.RDRAM + (addr & ~7);
2016-01-27 09:11:59 +00:00
#if defined(RSP_SAFE_DMA)
for (j = 0; j < Count; j++)
{
for (i = 0; i < Length; i++)
{
*(uint8_t *)(((size_t)Dest + j * Length + i) ^ 3) = *(uint8_t *)(((size_t)Source + j * Skip + i) ^ 3);
}
}
2016-01-27 09:11:59 +00:00
#else
if ((Skip & 0x3) == 0)
{
for (j = 0; j < Count; j++)
{
memcpy(Dest, Source, Length);
Source += Skip;
Dest += Length;
}
}
else
{
for (j = 0; j < Count; j++)
{
for (i = 0; i < Length; i++)
{
*(uint8_t *)(((size_t)Dest + i) ^ 3) = *(uint8_t *)(((size_t)Source + i) ^ 3);
}
Source += Skip;
Dest += Length;
}
}
2016-01-27 09:11:59 +00:00
#endif
// TODO: Could this be a problem DMEM to IMEM?
if (CPUCore == RecompilerCPU && (*RSPInfo.SP_MEM_ADDR_REG & 0x1000) != 0)
{
SetJumpTable(End);
}
2016-01-27 09:11:59 +00:00
*RSPInfo.SP_DMA_BUSY_REG = 0;
*RSPInfo.SP_STATUS_REG &= ~SP_STATUS_DMA_BUSY;
2016-01-27 09:11:59 +00:00
}
void SP_DMA_WRITE(void)
2016-01-27 09:11:59 +00:00
{
uint32_t i, j, Length, Skip, Count, addr;
uint8_t *Dest, *Source;
2016-01-27 09:11:59 +00:00
addr = (*RSPInfo.SP_DRAM_ADDR_REG) & 0x00FFFFFF;
if (addr > 0x7FFFFF)
{
DisplayError("SP DMA WRITE\nSP_DRAM_ADDR_REG not in RDRAM space");
return;
}
2016-01-27 09:11:59 +00:00
if ((*RSPInfo.SP_WR_LEN_REG & 0xFFF) + 1 + (*RSPInfo.SP_MEM_ADDR_REG & 0xFFF) > 0x1000)
{
DisplayError("SP DMA WRITE\nCould not fit copy in memory segment");
return;
}
2016-01-27 09:11:59 +00:00
Length = ((*RSPInfo.SP_WR_LEN_REG & 0xFFF) | 7) + 1;
Skip = (*RSPInfo.SP_WR_LEN_REG >> 20) + Length;
Count = ((*RSPInfo.SP_WR_LEN_REG >> 12) & 0xFF) + 1;
Dest = RSPInfo.RDRAM + (addr & ~7);
Source = ((*RSPInfo.SP_MEM_ADDR_REG & 0x1000) != 0 ? RSPInfo.IMEM : RSPInfo.DMEM) + ((*RSPInfo.SP_MEM_ADDR_REG & 0xFFF) & ~7);
2016-01-27 09:11:59 +00:00
#if defined(RSP_SAFE_DMA)
for (j = 0; j < Count; j++)
{
for (i = 0; i < Length; i++)
{
*(uint8_t *)(((size_t)Dest + j * Skip + i) ^ 3) = *(uint8_t *)(((size_t)Source + j * Length + i) ^ 3);
}
}
2016-01-27 09:11:59 +00:00
#else
if ((Skip & 0x3) == 0)
{
for (j = 0; j < Count; j++)
{
memcpy(Dest, Source, Length);
Source += Length;
Dest += Skip;
}
}
else
{
for (j = 0; j < Count; j++)
{
for (i = 0; i < Length; i++)
{
*(uint8_t *)(((size_t)Dest + i) ^ 3) = *(uint8_t *)(((size_t)Source + i) ^ 3);
}
Source += Length;
Dest += Skip;
}
}
2016-01-27 09:11:59 +00:00
#endif
*RSPInfo.SP_DMA_BUSY_REG = 0;
*RSPInfo.SP_STATUS_REG &= ~SP_STATUS_DMA_BUSY;
2016-01-27 09:11:59 +00:00
}