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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
#include <dolphin/dolphin.h>
#include <dolphin/am.h>
#include "__am.h"
static u32 __AMStackPointer[AM_STACK_ENTRIES];
static AMReadInfo __AMReadInfo[AM_STACK_ENTRIES];
static u32 __AMStackLocation;
static u32 __AMFreeBytes;
static u32 __AMPendingReads;
static void __AM_dvd_callback(s32 result, DVDFileInfo* handle) {
u32 i;
AMReadInfo* ptr;
for (i = 0; i < AM_STACK_ENTRIES; i++) {
if (handle == &(__AMReadInfo[i].file_handle)) {
ptr = &__AMReadInfo[i];
break;
}
}
ASSERTLINE(136, i != AM_STACK_ENTRIES);
if (DVDGetCommandBlockStatus(&ptr->file_handle.cb) == 0) {
ARQPostRequest(&ptr->arq_handle, i, 0, 1, (u32)ptr->buffer, ptr->curr_aram_offset, result, __AM_arq_callback);
ptr->curr_aram_offset += result;
ptr->curr_read_offset += result;
}
}
static void __AM_arq_callback(u32 task) {
u32 i;
AMReadInfo* ptr;
u32 remainder;
u32 read_request_length;
for (i = 0; i < AM_STACK_ENTRIES; i++) {
if ((ARQRequest*)task == &__AMReadInfo[i].arq_handle) {
ptr = &__AMReadInfo[i];
break;
}
}
ASSERTLINE(198, i != AM_STACK_ENTRIES);
if (ptr->curr_read_offset < ptr->file_length) {
remainder = ptr->file_length - ptr->curr_read_offset;
read_request_length = OSRoundUp32B(ptr->read_length > remainder ? remainder : ptr->read_length);
DVDReadAsync(&ptr->file_handle, ptr->buffer, read_request_length, ptr->curr_read_offset, __AM_dvd_callback);
} else {
ASSERTMSGLINE(220, __AMPendingReads, "AMPushBuffered(): Dangling read-complete event!\n");
__AMPendingReads--;
if (ptr->callback) {
(*ptr->callback)(ptr->path);
}
}
}
static void __AM_arq_poll_callback(u32 task) {
u32 i;
AMReadInfo* ptr;
for (i = 0; i < AM_STACK_ENTRIES; i++) {
if ((ARQRequest*)task == &__AMReadInfo[i].arq_handle) {
ptr = &__AMReadInfo[i];
break;
}
}
ASSERTLINE(262, i != AM_STACK_ENTRIES);
ptr->poll_flag = 1;
}
void* AMLoadFile(char* path, u32* length) {
DVDFileInfo dvdFileInfo;
u32 roundLength;
s32 readLength;
void* buffer;
int old;
if (!DVDOpen(path, &dvdFileInfo)) {
char ch[1024];
sprintf(ch, "Cannot open %s", path);
ASSERTMSGLINE(290, 0, ch);
}
ASSERTMSGLINE(294, dvdFileInfo.length, "File length is 0\n");
roundLength = OSRoundUp32B(dvdFileInfo.length);
old = OSDisableInterrupts();
buffer = OSAlloc(roundLength);
OSRestoreInterrupts(old);
ASSERTMSGLINE(303, buffer, "Unable to allocate buffer\n");
readLength = DVDReadPrio(&dvdFileInfo, buffer, roundLength, 0, 2);
ASSERTMSGLINE(307, readLength > 0, "Read length <= 0\n");
if (length != 0) {
*length = roundLength;
}
return buffer;
}
u32 AMPush(char* path) {
DVDFileInfo handle;
void* buffer;
u32 buffer_length;
u32 ret_val;
BOOL old;
if (!DVDOpen(path, &handle)) {
OSReport("AMPushBuffered(): Unable to open file '%s'\n", path);
OSPanic(__FILE__, 343, "AM: FATAL ERROR\n");
}
buffer_length = OSRoundUp32B(handle.length);
if (buffer_length) {
old = OSDisableInterrupts();
buffer = OSAlloc(buffer_length);
OSRestoreInterrupts(old);
ASSERTMSGLINE(356, buffer, "AMPush(): Memory allocation failure.\n");
ret_val = __AMPushBuffered(path, buffer, buffer_length, NULL, FALSE);
OSFree(buffer);
return ret_val;
} else {
#if DEBUG
OSReport("AMPush(): WARNING: File has zero length.\n");
#endif
return 0;
}
}
u32 AMPushData(void* buffer, u32 length) {
BOOL old;
u32 round_length;
AMReadInfo* ptr;
ASSERTMSGLINE(401, ((u32)buffer & 0x1F) == 0, "AMPushData(): buffer is not 32-byte aligned!\n");
ASSERTLINE(404, length);
round_length = OSRoundUp32B(length);
if (__AMFreeBytes >= round_length && __AMStackLocation < AM_STACK_ENTRIES - 1) {
ptr = &__AMReadInfo[__AMStackLocation];
old = OSDisableInterrupts();
ptr->aram_start_addr = __AMStackPointer[__AMStackLocation];
__AMStackLocation++;
__AMStackPointer[__AMStackLocation] = ptr->aram_start_addr + round_length;
__AMFreeBytes -= round_length;
ptr->poll_flag = 0;
ARQPostRequest(&ptr->arq_handle, __AMStackLocation - 1, 0, 1, (u32)buffer, ptr->aram_start_addr, round_length, &__AM_arq_poll_callback);
OSRestoreInterrupts(old);
do {} while (!ptr->poll_flag);
return ptr->aram_start_addr;
}
return 0;
}
u32 __AMPushBuffered(char* path, void* buffer, u32 buffer_size, AMCallback callback, int async_flag) {
BOOL old;
u32 round_file_length;
u32 stack_index;
AMReadInfo* ptr;
u32 remainder;
u32 read_request_length;
s32 actual_read_length;
ASSERTMSGLINE(477, ((u32)buffer & 0x1F) == 0, "AMPushBuffered(): buffer is not 32-byte aligned!\n");
ASSERTMSGLINE(480, buffer_size > 31, "AMPushBuffered(): buffer_size is less than 32 bytes!\n");
if (__AMStackLocation < AM_STACK_ENTRIES - 1) {
ptr = &__AMReadInfo[__AMStackLocation];
stack_index = __AMStackLocation;
if (!DVDOpen(path, &ptr->file_handle)) {
OSReport("AMPushBuffered(): Unable to open file '%s'\n", path);
OSPanic(__FILE__, 494, "AM: FATAL ERROR\n");
}
ptr->file_length = ptr->file_handle.length;
round_file_length = OSRoundUp32B(ptr->file_length);
if (__AMFreeBytes >= round_file_length) {
ptr->aram_start_addr = __AMStackPointer[__AMStackLocation];
old = OSDisableInterrupts();
__AMStackLocation++;
__AMStackPointer[__AMStackLocation] = ptr->aram_start_addr + round_file_length;
__AMFreeBytes -= round_file_length;
ptr->curr_read_offset = 0;
ptr->curr_aram_offset = ptr->aram_start_addr;
ptr->read_length = buffer_size & ~0x1F;
ptr->callback = callback;
ptr->path = path;
ptr->buffer = buffer;
OSRestoreInterrupts(old);
if (async_flag == 1) {
DVDReadAsync(&ptr->file_handle, ptr->buffer, ptr->read_length, 0, __AM_dvd_callback);
__AMPendingReads++;
} else {
while (ptr->curr_read_offset < ptr->file_length) {
remainder = ptr->file_length - ptr->curr_read_offset;
read_request_length = OSRoundUp32B(ptr->read_length > remainder ? remainder : ptr->read_length);
actual_read_length = DVDReadPrio(&ptr->file_handle, ptr->buffer, read_request_length, ptr->curr_read_offset, 2);
ASSERTMSGLINE(558, actual_read_length > 0, "AMPushBuffered(): Fatal Error - synchronuous DVD read\n");
ptr->curr_read_offset += actual_read_length;
ptr->poll_flag = FALSE;
ARQPostRequest(&(ptr->arq_handle), stack_index, 0, 1, (u32)ptr->buffer, ptr->curr_aram_offset, actual_read_length, __AM_arq_poll_callback);
ptr->curr_aram_offset += (u32)(actual_read_length);
do {} while (!ptr->poll_flag);
}
}
} else {
#if DEBUG
OSReport("AMPushBuffered(): WARNING: Not enough space in ARAM.\n");
#endif
return 0;
}
} else {
#if DEBUG
OSReport("AMPushBuffered(): WARNING: Stack table is full.\n");
#endif
return 0;
}
return ptr->aram_start_addr;
}
void AMPop(void) {
BOOL old;
old = OSDisableInterrupts();
if (__AMPendingReads == 0) {
if (__AMStackLocation > 1) {
__AMFreeBytes += __AMStackPointer[__AMStackLocation] - __AMStackPointer[__AMStackLocation - 1];
__AMStackLocation--;
}
}
OSRestoreInterrupts(old);
}
u32 AMGetZeroBuffer(void) {
return __AMStackPointer[0];
}
u32 AMGetReadStatus(void) {
BOOL old;
u32 tmp;
old = OSDisableInterrupts();
tmp = __AMPendingReads;
OSRestoreInterrupts(old);
return tmp;
}
u32 AMGetFreeSize(void) {
BOOL old;
u32 tmp;
old = OSDisableInterrupts();
tmp = __AMFreeBytes;
OSRestoreInterrupts(old);
return tmp;
}
u32 AMGetStackPointer(void) {
BOOL old;
u32 tmp;
old = OSDisableInterrupts();
tmp = __AMStackPointer[__AMStackLocation];
OSRestoreInterrupts(old);
return tmp;
}
void AMInit(u32 aramBase, u32 aramBytes) {
u32 i;
u8 __AMZeroBuffer[256 + 31];
u8* ptr;
ASSERTLINE(760, aramBytes);
ptr = (u8*)OSRoundUp32B(__AMZeroBuffer);
__AMStackLocation = 0;
__AMStackPointer[0] = aramBase;
__AMFreeBytes = aramBytes;
__AMPendingReads = 0;
for (i = 0; i < 256; i++) {
ptr[i] = 0;
}
AMPushData(ptr, 256);
}
|