1 // The top-level functions for the ARM940
\r
2 // (c) Copyright 2006-2007, Grazvydas "notaz" Ignotas
\r
4 #include "940shared.h"
\r
6 static _940_data_t *shared_data = (_940_data_t *) 0x00100000;
\r
7 static _940_ctl_t *shared_ctl = (_940_ctl_t *) 0x00200000;
\r
8 static unsigned char *mp3_data = (unsigned char *) 0x00400000;
\r
12 int wait_get_job(int oldjob);
\r
13 void spend_cycles(int c);
\r
14 void dcache_clean(void);
\r
15 void dcache_clean_flush(void);
\r
16 void drain_wb(void);
\r
17 // this should help to resolve race confition where shared var
\r
18 // is changed by other core just before we update it
\r
19 void set_if_not_changed(int *val, int oldval, int newval);
\r
21 void _memcpy(void *dst, const void *src, int count);
\r
23 // asm volatile ("mov r0, #0" ::: "r0");
\r
24 // asm volatile ("mcr p15, 0, r0, c7, c6, 0" ::: "r0"); /* flush dcache */
\r
25 // asm volatile ("mcr p15, 0, r0, c7, c10, 4" ::: "r0"); /* drain write buffer */
\r
28 static int find_sync_word(unsigned char *buf, int nBytes)
\r
30 unsigned char *p, *pe;
\r
32 /* find byte-aligned syncword - need 12 (MPEG 1,2) or 11 (MPEG 2.5) matching bits */
\r
33 for (p = buf, pe = buf + nBytes - 4; p < pe; p++)
\r
36 if (p[0] != 0xff) continue;
\r
38 if ((pn & 0xf8) != 0xf8 || // currently must be MPEG1
\r
39 (pn & 6) == 0) { // invalid layer
\r
43 if ((pn & 0xf0) < 0x20 || (pn & 0xf0) == 0xf0 || // bitrates
\r
44 (pn & 0x0c) != 0) { // not 44kHz
\r
54 static void mp3_decode(void)
\r
56 int mp3_offs = shared_ctl->mp3_offs;
\r
57 unsigned char *readPtr = mp3_data + mp3_offs;
\r
58 int bytesLeft = shared_ctl->mp3_len - mp3_offs;
\r
59 int offset; // frame offset from readPtr
\r
60 int retries = 0, err;
\r
62 if (bytesLeft <= 0) return; // EOF, nothing to do
\r
64 for (retries = 0; retries < 2; retries++)
\r
66 offset = find_sync_word(readPtr, bytesLeft);
\r
71 bytesLeft -= offset;
\r
73 err = MP3Decode(shared_data->mp3dec, &readPtr, &bytesLeft,
\r
74 shared_data->mp3_buffer[shared_ctl->mp3_buffsel], 0);
\r
76 if (err == ERR_MP3_MAINDATA_UNDERFLOW)
\r
77 // just need another frame
\r
80 if (err == ERR_MP3_INDATA_UNDERFLOW)
\r
83 if (err <= -6 && err >= -12) {
\r
84 // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_*
\r
85 // just try to skip the offending frame..
\r
90 shared_ctl->mp3_errors++;
\r
91 shared_ctl->mp3_lasterr = err;
\r
96 set_if_not_changed(&shared_ctl->mp3_offs, mp3_offs, readPtr - mp3_data);
\r
100 set_if_not_changed(&shared_ctl->mp3_offs, mp3_offs, shared_ctl->mp3_len);
\r
103 static void ym_flush_writes(void)
\r
108 if (shared_ctl->writebuffsel == 1) {
\r
109 wbuff = shared_ctl->writebuff1;
\r
111 wbuff = shared_ctl->writebuff0;
\r
114 /* playback all writes */
\r
115 for (i = 2048; i > 0; i--) {
\r
116 UINT16 d = *wbuff++;
\r
117 if (d == 0xffff) break;
\r
118 if (d == 0xfffe) continue;
\r
119 YM2612Write_(d >> 8, d);
\r
123 static void ym_update(int *ym_buffer)
\r
129 if (shared_ctl->writebuffsel == 1) {
\r
130 wbuff = shared_ctl->writebuff1;
\r
132 wbuff = shared_ctl->writebuff0;
\r
135 /* playback all writes */
\r
136 for (i = 2048/2; i > 0; i--) {
\r
138 dw = *(int *)wbuff;
\r
141 if (d == 0xffff) break;
\r
142 if (d == 0xfffe) { two_upds=1; break; }
\r
143 YM2612Write_(d >> 8, d);
\r
146 if (d == 0xffff) break;
\r
147 if (d == 0xfffe) { two_upds=1; break; }
\r
148 YM2612Write_(d >> 8, d);
\r
153 int len1 = shared_ctl->length / 2;
\r
154 shared_ctl->ym_active_chs =
\r
155 YM2612UpdateOne_(ym_buffer, len1, shared_ctl->stereo, 1);
\r
157 for (i *= 2; i > 0; i--) {
\r
158 UINT16 d = *wbuff++;
\r
159 if (d == 0xffff) break;
\r
160 YM2612Write_(d >> 8, d);
\r
163 ym_buffer += shared_ctl->stereo ? len1*2 : len1;
\r
164 len1 = shared_ctl->length - len1;
\r
166 shared_ctl->ym_active_chs =
\r
167 YM2612UpdateOne_(ym_buffer, len1, shared_ctl->stereo, 1);
\r
169 shared_ctl->ym_active_chs =
\r
170 YM2612UpdateOne_(ym_buffer, shared_ctl->length, shared_ctl->stereo, 1);
\r
177 int *ym_buffer = shared_data->ym_buffer;
\r
179 ym2612_940 = &shared_data->ym2612;
\r
184 job = wait_get_job(job);
\r
186 shared_ctl->lastjob = job;
\r
190 case JOB940_INITALL:
\r
192 shared_ctl->writebuff0[0] = shared_ctl->writebuff1[0] = 0xffff;
\r
193 YM2612Init_(shared_ctl->baseclock, shared_ctl->rate);
\r
194 /* Helix mp3 decoder */
\r
195 shared_data->mp3dec = MP3InitDecoder();
\r
198 case JOB940_INVALIDATE_DCACHE:
\r
200 dcache_clean_flush();
\r
203 case JOB940_YM2612RESETCHIP:
\r
204 YM2612ResetChip_();
\r
207 case JOB940_PICOSTATELOAD:
\r
208 YM2612PicoStateLoad_();
\r
211 case JOB940_PICOSTATESAVE2:
\r
212 YM2612PicoStateSave2(0, 0);
\r
213 _memcpy(shared_ctl->writebuff0, ym2612_940->REGS, 0x200);
\r
216 case JOB940_PICOSTATELOAD2_PREP:
\r
220 case JOB940_PICOSTATELOAD2:
\r
221 _memcpy(ym2612_940->REGS, shared_ctl->writebuff0, 0x200);
\r
222 YM2612PicoStateLoad2(0, 0);
\r
225 case JOB940_YM2612UPDATEONE:
\r
226 ym_update(ym_buffer);
\r
229 case JOB940_MP3DECODE:
\r
233 case JOB940_MP3RESET:
\r
234 if (shared_data->mp3dec) MP3FreeDecoder(shared_data->mp3dec);
\r
235 shared_data->mp3dec = MP3InitDecoder();
\r
239 shared_ctl->loopc++;
\r