aud_dma.c
9.68 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*************************************************************
aud_dma.c : Nintendo 64 Music Tools Programmers Library
(c) Copyright 1997/1998, Software Creations (Holdings) Ltd.
Version 3.14
Music library DMA buffer manager.
**************************************************************/
/* include configuartion */
#include "libmus_config.h"
/* include system headers */
#include <ultra64.h>
#ifndef SUPPORT_NAUDIO
#include <libaudio.h>
#else
#include <n_libaudio_sc.h>
#include <n_libaudio_sn_sc.h>
#endif
/* include other header files */
#include "libmus.h"
#include "lib_memory.h"
/* include current header file */
#include "aud_dma.h"
/* internal macros */
#define SAMPLE_ALIGNMENT 2 /* must be power of two */
#define FRAME_LAG 1 /* number of frames before buffer is purged */
#define DDROM_WAVEDATA_START 0x00140000 /* address of N64DD ROM samples - from <leo.h> */
extern unsigned long __muscontrol_flag;
/* internal data structures */
typedef struct dma_list_s
{
struct dma_list_s *prev;
struct dma_list_s *next;
int keep_count;
unsigned long sample_addr;
unsigned char *ram_addr;
} dma_list_t;
/* internal workspace */
static dma_list_t *dma_buffer_head; /* first used DMA buffer descriptor */
static dma_list_t *dma_buffer_free; /* first unused DMA buffer descriptor */
static dma_list_t *dma_buffer_list; /* DMA buffer descriptor list */
/* dma message buffers */
static OSIoMesg *audio_IO_mess_buf;
static OSMesg *audio_mess_buf;
static int audio_dma_size;
static int audio_dma_count;
/* internal function prototypes */
static ALDMAproc __CallBackDmaNew(void *ignored);
static s32 __CallBackDmaProcess(s32 addr, s32 len, void *ignored);
static dma_list_t *__MusIntDmaSample(unsigned long sample_addr, int sample_length);
/** Queues and storage for use with audio DMA's ****/
static OSMesgQueue audDMAMessageQ;
/* EPI handles for ROM access */
static OSPiHandle *cartrom_handle;
/*static*/ OSPiHandle *diskrom_handle;
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[EXTERNAL FUNCTION]
__MusIntDmaInit(dma_buffer_count, dma_buffer_size)
[Parameters]
dma_buffer_count number of DMA buffers
dma_buffer_size size of each DMA buffer
[Explanation]
Initialise DMA buffer manager.
[Return value]
Address of audio library DMA initialisation function
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
ALDMANew __MusIntDmaInit(int dma_buffer_count, int dma_buffer_size)
{
int i;
/* get ROM access handles */
cartrom_handle = osCartRomInit();
// Go to MusInitailize()
// diskrom_handle = osDriveRomInit();
/* allocate memory for DMA messages */
audio_IO_mess_buf = __MusIntMemMalloc(dma_buffer_count*2*sizeof(OSIoMesg));
audio_mess_buf = __MusIntMemMalloc(dma_buffer_count*2*sizeof(OSMesg));
/* allocate memory for DMA buffer descriptor list */
dma_buffer_list = __MusIntMemMalloc(dma_buffer_count*sizeof(dma_list_t));
__MusIntMemSet(dma_buffer_list, 0, dma_buffer_count*sizeof(dma_list_t));
/* initialise descriptor list and allocate DMA buffers */
for (i=0; i<dma_buffer_count-1; i++)
{
dma_buffer_list[i].next = dma_buffer_list+i+1;
dma_buffer_list[i+1].prev = dma_buffer_list+i;
dma_buffer_list[i].ram_addr = __MusIntMemMalloc(dma_buffer_size);
dma_buffer_list[i].sample_addr = 0xffffffff;
}
/* allocate last DMA buffer */
dma_buffer_list[i].ram_addr = __MusIntMemMalloc(dma_buffer_size);
dma_buffer_list[i].sample_addr = 0xffffffff;
/* set working vars */
audio_dma_size = dma_buffer_size;
audio_dma_count = 0;
dma_buffer_head = NULL;
dma_buffer_free = dma_buffer_list;
/* create DMA message queue */
osCreateMesgQueue(&audDMAMessageQ, audio_mess_buf, dma_buffer_count*2);
/* return address of audio library DMA new function */
return (__CallBackDmaNew);
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[EXTERNAL FUNCTION]
__MusIntDmaProcess()
[Explanation]
Process the DMA buffers. The DMA message queue is emptied, DMA buffers are aged
and if necessary freed.
[Return value]
NONE
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
void __MusIntDmaProcess(void)
{
OSIoMesg *iomsg;
dma_list_t *current_dma_buffer;
/* empty message queue */
while (audio_dma_count)
{
#ifndef _AUDIODEBUG
osRecvMesg(&audDMAMessageQ,(OSMesg *)&iomsg,OS_MESG_NOBLOCK);
#else
if (osRecvMesg(&audDMAMessageQ,(OSMesg *)&iomsg,OS_MESG_NOBLOCK) == -1)
osSyncPrintf("AUD_DMA.C: Audio DMA not completed.\n");
#endif
audio_dma_count--;
}
/* free any DMA buffers not in use */
current_dma_buffer = dma_buffer_head;
while(current_dma_buffer)
{
current_dma_buffer->keep_count--;
if (!current_dma_buffer->keep_count)
{
dma_list_t *next_buffer;
next_buffer = current_dma_buffer->next;
if (current_dma_buffer->next)
current_dma_buffer->next->prev = current_dma_buffer->prev;
if (current_dma_buffer->prev)
current_dma_buffer->prev->next = current_dma_buffer->next;
else
dma_buffer_head = current_dma_buffer->next;
current_dma_buffer->prev = NULL;
current_dma_buffer->next = dma_buffer_free;
dma_buffer_free = current_dma_buffer;
current_dma_buffer=next_buffer;
}
else
{
current_dma_buffer=current_dma_buffer->next;
}
}
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[CALLBACK FUNCTION]
__CallBackDmaNew(ignored)
[Parameters]
ingored ignored
[Explanation]
Audio library DMA processing initialisation function which simply returns the
address of the normal DMA callback function.
[Return value]
Address of audio library DMA processing function
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
static ALDMAproc __CallBackDmaNew(void *ignored)
{
return (__CallBackDmaProcess);
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[CALLBACK FUNCTION]
__CallBackDmaProcess(addr, len, ignored)
[Parameters]
addr address of sample required
len length of sample required
ingored ignored
[Explanation]
Audio library DMA processing function. Find the requested sample in RAM and return
its address.
[Return value]
RAM address of sample
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
static s32 __CallBackDmaProcess(s32 addr, s32 len, void *ignored)
{
dma_list_t *buffer;
unsigned char *ram_addr;;
buffer = __MusIntDmaSample(addr, len);
if (!buffer)
return (osVirtualToPhysical((void *)addr));
if ((addr&0xff000000)==0xff000000)
{
addr &= 0xffffff;
addr += DDROM_WAVEDATA_START;
}
ram_addr = buffer->ram_addr+(u32)addr-buffer->sample_addr;
return (osVirtualToPhysical(ram_addr));
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[INTERNAL FUNCTION]
__MusIntDmaSample(sample_addr, sample_length)
[Parameters]
sample_addr address of sample required
sample_length length of sample required
[Explanation]
Find requested sample in current DMA buffer or download requested sample into a
new DMA buffer.
[Return value]
Address of dma buffer containing sample
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
static dma_list_t *__MusIntDmaSample(unsigned long sample_addr, int sample_length)
{
dma_list_t *current_dma_buffer, *free_buffer, *last_buffer;
unsigned long sample_addr_end;
OSPiHandle *pi_handle;
OSIoMesg *io_msg;
/* test if sample is coming from the N64DD ROM */
if ((sample_addr&0xff000000)==0xff000000)
{
pi_handle = diskrom_handle;
sample_addr &= 0xffffff;
sample_addr += DDROM_WAVEDATA_START;
}
else
{
if (__muscontrol_flag&MUSCONTROL_RAM)
return (NULL);
pi_handle = cartrom_handle;
}
/* get address of sample end */
sample_addr_end = sample_addr+sample_length;
/* find sample if already downloaded */
last_buffer = NULL;
for (current_dma_buffer=dma_buffer_head; current_dma_buffer; current_dma_buffer=current_dma_buffer->next)
{
/* have we gone past it? */
if (sample_addr<current_dma_buffer->sample_addr)
break;
/* is it contained in this buffer? */
if(sample_addr_end<=current_dma_buffer->sample_addr+audio_dma_size)
{
/* modification 99.01.12 from Yutaka Murakami v3.13 */
current_dma_buffer->keep_count = 1+FRAME_LAG;
return (current_dma_buffer);
}
last_buffer = current_dma_buffer;
}
/* get next free buffer */
free_buffer = dma_buffer_free;
if (!free_buffer)
{
#ifdef _AUDIODEBUG
osSyncPrintf("AUD_DMA.C: Cannot find free DMA buffer.\n");
#endif
return (dma_buffer_head);
}
/* free buffer is to be used so move it along */
dma_buffer_free = free_buffer->next;
/* insert free buffer into list */
if (last_buffer)
{
/* link free with next and fix next */
free_buffer->next = last_buffer->next;
if (free_buffer->next)
free_buffer->next->prev = free_buffer;
/* line free with last and fix last */
free_buffer->prev = last_buffer;
last_buffer->next = free_buffer;
}
else
{
free_buffer->next = dma_buffer_head;
free_buffer->prev = NULL;
if (dma_buffer_head)
dma_buffer_head->prev = free_buffer;
dma_buffer_head = free_buffer;
}
/* DMA sample to buffer */
free_buffer->sample_addr = sample_addr&(~(SAMPLE_ALIGNMENT-1));
free_buffer->keep_count = 1+FRAME_LAG;
io_msg = &audio_IO_mess_buf[audio_dma_count++];
io_msg->hdr.pri = OS_MESG_PRI_NORMAL;
io_msg->hdr.retQueue = &audDMAMessageQ;
io_msg->dramAddr = free_buffer->ram_addr;
io_msg->devAddr = free_buffer->sample_addr;
io_msg->size = audio_dma_size;
osEPiStartDma(pi_handle, io_msg, OS_READ);
/* return dma buffer address */
return (free_buffer);
}
/* end of file */