2017-02-17 17:26:45 +00:00
|
|
|
#include "global.h"
|
2017-03-05 07:35:03 +00:00
|
|
|
#include "dma3.h"
|
2017-02-17 17:26:45 +00:00
|
|
|
|
2018-01-08 05:59:42 +00:00
|
|
|
#define MAX_DMA_REQUESTS 128
|
|
|
|
|
|
|
|
#define DMA_REQUEST_COPY32 1
|
|
|
|
#define DMA_REQUEST_FILL32 2
|
|
|
|
#define DMA_REQUEST_COPY16 3
|
|
|
|
#define DMA_REQUEST_FILL16 4
|
|
|
|
|
2021-02-21 00:20:18 +00:00
|
|
|
struct Dma3Request
|
2018-01-08 05:59:42 +00:00
|
|
|
{
|
2021-02-21 01:12:17 +00:00
|
|
|
const u8 *src;
|
|
|
|
u8 *dest;
|
2018-01-08 05:59:42 +00:00
|
|
|
u16 size;
|
|
|
|
u16 mode;
|
|
|
|
u32 value;
|
2021-02-21 00:20:18 +00:00
|
|
|
};
|
2017-09-30 13:32:46 +00:00
|
|
|
|
2021-02-21 00:20:18 +00:00
|
|
|
static struct Dma3Request sDma3Requests[MAX_DMA_REQUESTS];
|
|
|
|
|
|
|
|
static vbool8 sDma3ManagerLocked;
|
|
|
|
static u8 sDma3RequestCursor;
|
2017-09-30 13:32:46 +00:00
|
|
|
|
2017-02-17 17:26:45 +00:00
|
|
|
void ClearDma3Requests(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2021-02-21 00:20:18 +00:00
|
|
|
sDma3ManagerLocked = TRUE;
|
|
|
|
sDma3RequestCursor = 0;
|
2017-02-17 17:26:45 +00:00
|
|
|
|
2018-01-08 05:59:42 +00:00
|
|
|
for (i = 0; i < MAX_DMA_REQUESTS; i++)
|
2017-02-17 17:26:45 +00:00
|
|
|
{
|
2021-02-21 00:20:18 +00:00
|
|
|
sDma3Requests[i].size = 0;
|
|
|
|
sDma3Requests[i].src = NULL;
|
|
|
|
sDma3Requests[i].dest = NULL;
|
2017-02-17 17:26:45 +00:00
|
|
|
}
|
2017-09-18 16:36:05 +00:00
|
|
|
|
2021-02-21 00:20:18 +00:00
|
|
|
sDma3ManagerLocked = FALSE;
|
2017-02-17 17:26:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProcessDma3Requests(void)
|
|
|
|
{
|
2018-01-08 05:59:42 +00:00
|
|
|
u16 bytesTransferred;
|
2017-09-18 16:36:05 +00:00
|
|
|
|
2021-02-21 00:20:18 +00:00
|
|
|
if (sDma3ManagerLocked)
|
2017-02-17 17:26:45 +00:00
|
|
|
return;
|
|
|
|
|
2018-01-08 05:59:42 +00:00
|
|
|
bytesTransferred = 0;
|
2017-02-17 17:26:45 +00:00
|
|
|
|
|
|
|
// as long as there are DMA requests to process (unless size or vblank is an issue), do not exit
|
2021-02-21 00:20:18 +00:00
|
|
|
while (sDma3Requests[sDma3RequestCursor].size != 0)
|
2017-02-17 17:26:45 +00:00
|
|
|
{
|
2021-02-21 00:20:18 +00:00
|
|
|
bytesTransferred += sDma3Requests[sDma3RequestCursor].size;
|
2017-09-18 16:36:05 +00:00
|
|
|
|
2018-01-08 05:59:42 +00:00
|
|
|
if (bytesTransferred > 40 * 1024)
|
|
|
|
return; // don't transfer more than 40 KiB
|
2018-01-07 21:06:16 +00:00
|
|
|
if (*(u8 *)REG_ADDR_VCOUNT > 224)
|
|
|
|
return; // we're about to leave vblank, stop
|
2017-02-17 17:26:45 +00:00
|
|
|
|
2021-02-21 00:20:18 +00:00
|
|
|
switch (sDma3Requests[sDma3RequestCursor].mode)
|
2017-02-17 17:26:45 +00:00
|
|
|
{
|
2018-01-08 05:59:42 +00:00
|
|
|
case DMA_REQUEST_COPY32: // regular 32-bit copy
|
2021-02-21 00:20:18 +00:00
|
|
|
Dma3CopyLarge32_(sDma3Requests[sDma3RequestCursor].src,
|
|
|
|
sDma3Requests[sDma3RequestCursor].dest,
|
|
|
|
sDma3Requests[sDma3RequestCursor].size);
|
2017-02-17 17:26:45 +00:00
|
|
|
break;
|
2018-01-08 05:59:42 +00:00
|
|
|
case DMA_REQUEST_FILL32: // repeat a single 32-bit value across RAM
|
2021-02-21 00:20:18 +00:00
|
|
|
Dma3FillLarge32_(sDma3Requests[sDma3RequestCursor].value,
|
|
|
|
sDma3Requests[sDma3RequestCursor].dest,
|
|
|
|
sDma3Requests[sDma3RequestCursor].size);
|
2017-02-17 17:26:45 +00:00
|
|
|
break;
|
2018-01-08 05:59:42 +00:00
|
|
|
case DMA_REQUEST_COPY16: // regular 16-bit copy
|
2021-02-21 00:20:18 +00:00
|
|
|
Dma3CopyLarge16_(sDma3Requests[sDma3RequestCursor].src,
|
|
|
|
sDma3Requests[sDma3RequestCursor].dest,
|
|
|
|
sDma3Requests[sDma3RequestCursor].size);
|
2017-02-17 17:26:45 +00:00
|
|
|
break;
|
2018-01-08 05:59:42 +00:00
|
|
|
case DMA_REQUEST_FILL16: // repeat a single 16-bit value across RAM
|
2021-02-21 00:20:18 +00:00
|
|
|
Dma3FillLarge16_(sDma3Requests[sDma3RequestCursor].value,
|
|
|
|
sDma3Requests[sDma3RequestCursor].dest,
|
|
|
|
sDma3Requests[sDma3RequestCursor].size);
|
2017-02-17 17:26:45 +00:00
|
|
|
break;
|
2017-09-18 16:36:05 +00:00
|
|
|
}
|
2018-01-08 05:59:42 +00:00
|
|
|
|
|
|
|
// Free the request
|
2021-02-21 00:20:18 +00:00
|
|
|
sDma3Requests[sDma3RequestCursor].src = NULL;
|
|
|
|
sDma3Requests[sDma3RequestCursor].dest = NULL;
|
|
|
|
sDma3Requests[sDma3RequestCursor].size = 0;
|
|
|
|
sDma3Requests[sDma3RequestCursor].mode = 0;
|
|
|
|
sDma3Requests[sDma3RequestCursor].value = 0;
|
|
|
|
sDma3RequestCursor++;
|
|
|
|
|
|
|
|
if (sDma3RequestCursor >= MAX_DMA_REQUESTS) // loop back to the first DMA request
|
|
|
|
sDma3RequestCursor = 0;
|
2017-02-17 17:26:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 05:59:42 +00:00
|
|
|
s16 RequestDma3Copy(const void *src, void *dest, u16 size, u8 mode)
|
2017-02-17 17:26:45 +00:00
|
|
|
{
|
|
|
|
int cursor;
|
2018-01-08 05:59:42 +00:00
|
|
|
int i = 0;
|
2017-09-18 16:36:05 +00:00
|
|
|
|
2021-02-21 00:20:18 +00:00
|
|
|
sDma3ManagerLocked = TRUE;
|
|
|
|
cursor = sDma3RequestCursor;
|
2018-01-08 05:59:42 +00:00
|
|
|
|
|
|
|
while (i < MAX_DMA_REQUESTS)
|
2017-02-17 17:26:45 +00:00
|
|
|
{
|
2021-02-21 00:20:18 +00:00
|
|
|
if (sDma3Requests[cursor].size == 0) // an empty request was found.
|
2017-02-17 17:26:45 +00:00
|
|
|
{
|
2021-02-21 00:20:18 +00:00
|
|
|
sDma3Requests[cursor].src = src;
|
|
|
|
sDma3Requests[cursor].dest = dest;
|
|
|
|
sDma3Requests[cursor].size = size;
|
2017-09-18 16:36:05 +00:00
|
|
|
|
2018-01-08 05:59:42 +00:00
|
|
|
if (mode == 1)
|
2021-02-21 00:20:18 +00:00
|
|
|
sDma3Requests[cursor].mode = DMA_REQUEST_COPY32;
|
2017-02-17 17:26:45 +00:00
|
|
|
else
|
2021-02-21 00:20:18 +00:00
|
|
|
sDma3Requests[cursor].mode = DMA_REQUEST_COPY16;
|
2017-09-18 16:36:05 +00:00
|
|
|
|
2021-02-21 00:20:18 +00:00
|
|
|
sDma3ManagerLocked = FALSE;
|
2018-01-08 05:59:42 +00:00
|
|
|
return cursor;
|
2017-02-17 17:26:45 +00:00
|
|
|
}
|
2018-01-08 05:59:42 +00:00
|
|
|
if (++cursor >= MAX_DMA_REQUESTS) // loop back to start.
|
2017-02-17 17:26:45 +00:00
|
|
|
cursor = 0;
|
2018-01-08 05:59:42 +00:00
|
|
|
i++;
|
2017-02-17 17:26:45 +00:00
|
|
|
}
|
2021-02-21 00:20:18 +00:00
|
|
|
sDma3ManagerLocked = FALSE;
|
2018-01-08 05:59:42 +00:00
|
|
|
return -1; // no free DMA request was found
|
2017-02-17 17:26:45 +00:00
|
|
|
}
|
|
|
|
|
2018-01-08 05:59:42 +00:00
|
|
|
s16 RequestDma3Fill(s32 value, void *dest, u16 size, u8 mode)
|
2017-02-17 17:26:45 +00:00
|
|
|
{
|
|
|
|
int cursor;
|
2018-01-08 05:59:42 +00:00
|
|
|
int i = 0;
|
2017-09-18 16:36:05 +00:00
|
|
|
|
2021-02-21 00:20:18 +00:00
|
|
|
cursor = sDma3RequestCursor;
|
|
|
|
sDma3ManagerLocked = TRUE;
|
2017-09-18 16:36:05 +00:00
|
|
|
|
2018-01-08 05:59:42 +00:00
|
|
|
while (i < MAX_DMA_REQUESTS)
|
2017-02-17 17:26:45 +00:00
|
|
|
{
|
2021-02-21 00:20:18 +00:00
|
|
|
if (sDma3Requests[cursor].size == 0) // an empty request was found.
|
2017-02-17 17:26:45 +00:00
|
|
|
{
|
2021-02-21 00:20:18 +00:00
|
|
|
sDma3Requests[cursor].dest = dest;
|
|
|
|
sDma3Requests[cursor].size = size;
|
|
|
|
sDma3Requests[cursor].mode = mode;
|
|
|
|
sDma3Requests[cursor].value = value;
|
2017-02-17 17:26:45 +00:00
|
|
|
|
|
|
|
if(mode == 1)
|
2021-02-21 00:20:18 +00:00
|
|
|
sDma3Requests[cursor].mode = DMA_REQUEST_FILL32;
|
2017-02-17 17:26:45 +00:00
|
|
|
else
|
2021-02-21 00:20:18 +00:00
|
|
|
sDma3Requests[cursor].mode = DMA_REQUEST_FILL16;
|
2017-09-18 16:36:05 +00:00
|
|
|
|
2021-02-21 00:20:18 +00:00
|
|
|
sDma3ManagerLocked = FALSE;
|
2018-01-08 05:59:42 +00:00
|
|
|
return cursor;
|
2017-02-17 17:26:45 +00:00
|
|
|
}
|
2018-01-08 05:59:42 +00:00
|
|
|
if (++cursor >= MAX_DMA_REQUESTS) // loop back to start.
|
2017-02-17 17:26:45 +00:00
|
|
|
cursor = 0;
|
2018-01-08 05:59:42 +00:00
|
|
|
i++;
|
2017-02-17 17:26:45 +00:00
|
|
|
}
|
2021-02-21 00:20:18 +00:00
|
|
|
sDma3ManagerLocked = FALSE;
|
2018-01-08 05:59:42 +00:00
|
|
|
return -1; // no free DMA request was found
|
2017-02-17 17:26:45 +00:00
|
|
|
}
|
|
|
|
|
2018-01-29 23:26:36 +00:00
|
|
|
s16 CheckForSpaceForDma3Request(s16 index)
|
2017-02-17 17:26:45 +00:00
|
|
|
{
|
2018-01-08 05:59:42 +00:00
|
|
|
int i = 0;
|
2017-02-17 17:26:45 +00:00
|
|
|
|
2018-01-08 05:59:42 +00:00
|
|
|
if (index == -1) // check if all requests are free
|
2017-02-17 17:26:45 +00:00
|
|
|
{
|
2018-01-08 05:59:42 +00:00
|
|
|
while (i < MAX_DMA_REQUESTS)
|
|
|
|
{
|
2021-02-21 00:20:18 +00:00
|
|
|
if (sDma3Requests[i].size != 0)
|
2017-02-17 17:26:45 +00:00
|
|
|
return -1;
|
2018-01-08 05:59:42 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else // check the specified request
|
|
|
|
{
|
2021-02-21 00:20:18 +00:00
|
|
|
if (sDma3Requests[index].size != 0)
|
2018-01-08 05:59:42 +00:00
|
|
|
return -1;
|
2017-02-17 17:26:45 +00:00
|
|
|
return 0;
|
2017-09-18 16:36:05 +00:00
|
|
|
}
|
2017-02-17 17:26:45 +00:00
|
|
|
}
|