forked from pret/pokeemerald
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdma3_manager.c
183 lines (154 loc) · 5.09 KB
/
dma3_manager.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "global.h"
#include "dma3.h"
#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
struct Dma3Request
{
const u8 *src;
u8 *dest;
u16 size;
u16 mode;
u32 value;
};
static struct Dma3Request sDma3Requests[MAX_DMA_REQUESTS];
static vbool8 sDma3ManagerLocked;
static u8 sDma3RequestCursor;
void ClearDma3Requests(void)
{
int i;
sDma3ManagerLocked = TRUE;
sDma3RequestCursor = 0;
for (i = 0; i < MAX_DMA_REQUESTS; i++)
{
sDma3Requests[i].size = 0;
sDma3Requests[i].src = NULL;
sDma3Requests[i].dest = NULL;
}
sDma3ManagerLocked = FALSE;
}
void ProcessDma3Requests(void)
{
u16 bytesTransferred;
if (sDma3ManagerLocked)
return;
bytesTransferred = 0;
// as long as there are DMA requests to process (unless size or vblank is an issue), do not exit
while (sDma3Requests[sDma3RequestCursor].size != 0)
{
bytesTransferred += sDma3Requests[sDma3RequestCursor].size;
if (bytesTransferred > 40 * 1024)
return; // don't transfer more than 40 KiB
if (*(u8 *)REG_ADDR_VCOUNT > 224)
return; // we're about to leave vblank, stop
switch (sDma3Requests[sDma3RequestCursor].mode)
{
case DMA_REQUEST_COPY32: // regular 32-bit copy
Dma3CopyLarge32_(sDma3Requests[sDma3RequestCursor].src,
sDma3Requests[sDma3RequestCursor].dest,
sDma3Requests[sDma3RequestCursor].size);
break;
case DMA_REQUEST_FILL32: // repeat a single 32-bit value across RAM
Dma3FillLarge32_(sDma3Requests[sDma3RequestCursor].value,
sDma3Requests[sDma3RequestCursor].dest,
sDma3Requests[sDma3RequestCursor].size);
break;
case DMA_REQUEST_COPY16: // regular 16-bit copy
Dma3CopyLarge16_(sDma3Requests[sDma3RequestCursor].src,
sDma3Requests[sDma3RequestCursor].dest,
sDma3Requests[sDma3RequestCursor].size);
break;
case DMA_REQUEST_FILL16: // repeat a single 16-bit value across RAM
Dma3FillLarge16_(sDma3Requests[sDma3RequestCursor].value,
sDma3Requests[sDma3RequestCursor].dest,
sDma3Requests[sDma3RequestCursor].size);
break;
}
// Free the request
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;
}
}
s16 RequestDma3Copy(const void *src, void *dest, u16 size, u8 mode)
{
int cursor;
int i = 0;
sDma3ManagerLocked = TRUE;
cursor = sDma3RequestCursor;
while (i < MAX_DMA_REQUESTS)
{
if (sDma3Requests[cursor].size == 0) // an empty request was found.
{
sDma3Requests[cursor].src = src;
sDma3Requests[cursor].dest = dest;
sDma3Requests[cursor].size = size;
if (mode == 1)
sDma3Requests[cursor].mode = DMA_REQUEST_COPY32;
else
sDma3Requests[cursor].mode = DMA_REQUEST_COPY16;
sDma3ManagerLocked = FALSE;
return cursor;
}
if (++cursor >= MAX_DMA_REQUESTS) // loop back to start.
cursor = 0;
i++;
}
sDma3ManagerLocked = FALSE;
return -1; // no free DMA request was found
}
s16 RequestDma3Fill(s32 value, void *dest, u16 size, u8 mode)
{
int cursor;
int i = 0;
cursor = sDma3RequestCursor;
sDma3ManagerLocked = TRUE;
while (i < MAX_DMA_REQUESTS)
{
if (sDma3Requests[cursor].size == 0) // an empty request was found.
{
sDma3Requests[cursor].dest = dest;
sDma3Requests[cursor].size = size;
sDma3Requests[cursor].mode = mode;
sDma3Requests[cursor].value = value;
if(mode == 1)
sDma3Requests[cursor].mode = DMA_REQUEST_FILL32;
else
sDma3Requests[cursor].mode = DMA_REQUEST_FILL16;
sDma3ManagerLocked = FALSE;
return cursor;
}
if (++cursor >= MAX_DMA_REQUESTS) // loop back to start.
cursor = 0;
i++;
}
sDma3ManagerLocked = FALSE;
return -1; // no free DMA request was found
}
s16 CheckForSpaceForDma3Request(s16 index)
{
int i = 0;
if (index == -1) // check if all requests are free
{
while (i < MAX_DMA_REQUESTS)
{
if (sDma3Requests[i].size != 0)
return -1;
i++;
}
return 0;
}
else // check the specified request
{
if (sDma3Requests[index].size != 0)
return -1;
return 0;
}
}