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