6cadc2da |
1 | // The top-level functions for the ARM940\r |
2 | // (c) Copyright 2006-2007, Grazvydas "notaz" Ignotas\r |
3 | \r |
cc68a136 |
4 | #include "940shared.h"\r |
5 | \r |
42c7b147 |
6 | static _940_data_t *shared_data = (_940_data_t *) 0x00100000;\r |
7 | static _940_ctl_t *shared_ctl = (_940_ctl_t *) 0x00200000;\r |
e362c573 |
8 | static unsigned char *mp3_data = (unsigned char *) 0x00400000;\r |
cc68a136 |
9 | YM2612 *ym2612_940;\r |
cc68a136 |
10 | \r |
11 | // from init.s\r |
abe0ea43 |
12 | int wait_get_job(int oldjob);\r |
cc68a136 |
13 | void spend_cycles(int c);\r |
abe0ea43 |
14 | void dcache_clean(void);\r |
15 | void dcache_clean_flush(void);\r |
16 | void drain_wb(void);\r |
51a902ae |
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 |
cc68a136 |
20 | \r |
e4fb433c |
21 | void _memcpy(void *dst, const void *src, int count);\r |
22 | \r |
cc68a136 |
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 |
26 | \r |
51a902ae |
27 | \r |
091facb8 |
28 | static int find_sync_word(unsigned char *buf, int nBytes)\r |
29 | {\r |
30 | unsigned char *p, *pe;\r |
31 | \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 |
34 | {\r |
35 | int pn;\r |
36 | if (p[0] != 0xff) continue;\r |
37 | pn = p[1];\r |
38 | if ((pn & 0xf8) != 0xf8 || // currently must be MPEG1\r |
39 | (pn & 6) == 0) { // invalid layer\r |
40 | p++; continue;\r |
41 | }\r |
42 | pn = p[2];\r |
43 | if ((pn & 0xf0) < 0x20 || (pn & 0xf0) == 0xf0 || // bitrates\r |
44 | (pn & 0x0c) != 0) { // not 44kHz\r |
45 | continue;\r |
46 | }\r |
47 | \r |
48 | return p - buf;\r |
49 | }\r |
50 | \r |
51 | return -1;\r |
52 | }\r |
53 | \r |
51a902ae |
54 | static void mp3_decode(void)\r |
55 | {\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 |
091facb8 |
60 | int retries = 0, err;\r |
51a902ae |
61 | \r |
62 | if (bytesLeft <= 0) return; // EOF, nothing to do\r |
63 | \r |
7c34867a |
64 | for (retries = 0; retries < 2; retries++)\r |
65 | {\r |
66 | offset = find_sync_word(readPtr, bytesLeft);\r |
67 | if (offset < 0)\r |
68 | goto set_eof;\r |
69 | \r |
70 | readPtr += offset;\r |
71 | bytesLeft -= offset;\r |
72 | \r |
73 | err = MP3Decode(shared_data->mp3dec, &readPtr, &bytesLeft,\r |
74 | shared_data->mp3_buffer[shared_ctl->mp3_buffsel], 0);\r |
75 | if (err) {\r |
76 | if (err == ERR_MP3_MAINDATA_UNDERFLOW)\r |
77 | // just need another frame\r |
78 | continue;\r |
79 | \r |
80 | if (err == ERR_MP3_INDATA_UNDERFLOW)\r |
81 | goto set_eof;\r |
82 | \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 |
86 | readPtr++;\r |
87 | bytesLeft--;\r |
88 | continue;\r |
89 | }\r |
90 | shared_ctl->mp3_errors++;\r |
91 | shared_ctl->mp3_lasterr = err;\r |
51a902ae |
92 | }\r |
7c34867a |
93 | break;\r |
51a902ae |
94 | }\r |
7c34867a |
95 | \r |
51a902ae |
96 | set_if_not_changed(&shared_ctl->mp3_offs, mp3_offs, readPtr - mp3_data);\r |
7c34867a |
97 | return;\r |
98 | \r |
99 | set_eof:\r |
100 | set_if_not_changed(&shared_ctl->mp3_offs, mp3_offs, shared_ctl->mp3_len);\r |
51a902ae |
101 | }\r |
102 | \r |
e4fb433c |
103 | static void ym_flush_writes(void)\r |
104 | {\r |
105 | UINT16 *wbuff;\r |
106 | int i;\r |
107 | \r |
108 | if (shared_ctl->writebuffsel == 1) {\r |
109 | wbuff = shared_ctl->writebuff1;\r |
110 | } else {\r |
111 | wbuff = shared_ctl->writebuff0;\r |
112 | }\r |
113 | \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 |
120 | }\r |
121 | }\r |
122 | \r |
2433f409 |
123 | static void ym_update(int *ym_buffer)\r |
124 | {\r |
125 | int i, dw;\r |
126 | int two_upds = 0;\r |
127 | UINT16 *wbuff;\r |
128 | \r |
129 | if (shared_ctl->writebuffsel == 1) {\r |
130 | wbuff = shared_ctl->writebuff1;\r |
131 | } else {\r |
132 | wbuff = shared_ctl->writebuff0;\r |
133 | }\r |
134 | \r |
135 | /* playback all writes */\r |
136 | for (i = 2048/2; i > 0; i--) {\r |
137 | UINT16 d;\r |
138 | dw = *(int *)wbuff;\r |
139 | d = dw;\r |
140 | wbuff++;\r |
141 | if (d == 0xffff) break;\r |
142 | if (d == 0xfffe) { two_upds=1; break; }\r |
143 | YM2612Write_(d >> 8, d);\r |
144 | d = (dw>>16);\r |
145 | wbuff++;\r |
146 | if (d == 0xffff) break;\r |
147 | if (d == 0xfffe) { two_upds=1; break; }\r |
148 | YM2612Write_(d >> 8, d);\r |
149 | }\r |
150 | \r |
e4fb433c |
151 | if (two_upds)\r |
152 | {\r |
2433f409 |
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 |
156 | \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 |
161 | }\r |
162 | \r |
163 | ym_buffer += shared_ctl->stereo ? len1*2 : len1;\r |
164 | len1 = shared_ctl->length - len1;\r |
165 | \r |
166 | shared_ctl->ym_active_chs =\r |
167 | YM2612UpdateOne_(ym_buffer, len1, shared_ctl->stereo, 1);\r |
168 | } else {\r |
169 | shared_ctl->ym_active_chs =\r |
170 | YM2612UpdateOne_(ym_buffer, shared_ctl->length, shared_ctl->stereo, 1);\r |
171 | }\r |
172 | }\r |
173 | \r |
51a902ae |
174 | \r |
abe0ea43 |
175 | void Main940(void)\r |
cc68a136 |
176 | {\r |
abe0ea43 |
177 | int *ym_buffer = shared_data->ym_buffer;\r |
178 | int job = 0;\r |
cc68a136 |
179 | ym2612_940 = &shared_data->ym2612;\r |
cc68a136 |
180 | \r |
cc68a136 |
181 | \r |
abe0ea43 |
182 | for (;;)\r |
cc68a136 |
183 | {\r |
abe0ea43 |
184 | job = wait_get_job(job);\r |
185 | \r |
186 | shared_ctl->lastjob = job;\r |
cc68a136 |
187 | \r |
abe0ea43 |
188 | switch (job)\r |
cc68a136 |
189 | {\r |
abe0ea43 |
190 | case JOB940_INITALL:\r |
191 | /* ym2612 */\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 |
196 | break;\r |
197 | \r |
198 | case JOB940_INVALIDATE_DCACHE:\r |
199 | drain_wb();\r |
200 | dcache_clean_flush();\r |
201 | break;\r |
202 | \r |
203 | case JOB940_YM2612RESETCHIP:\r |
204 | YM2612ResetChip_();\r |
205 | break;\r |
206 | \r |
207 | case JOB940_PICOSTATELOAD:\r |
208 | YM2612PicoStateLoad_();\r |
209 | break;\r |
210 | \r |
e4fb433c |
211 | case JOB940_PICOSTATESAVE2:\r |
212 | YM2612PicoStateSave2(0, 0);\r |
213 | _memcpy(shared_ctl->writebuff0, ym2612_940->REGS, 0x200);\r |
214 | break;\r |
215 | \r |
216 | case JOB940_PICOSTATELOAD2_PREP:\r |
217 | ym_flush_writes();\r |
218 | break;\r |
219 | \r |
220 | case JOB940_PICOSTATELOAD2:\r |
221 | _memcpy(ym2612_940->REGS, shared_ctl->writebuff0, 0x200);\r |
222 | YM2612PicoStateLoad2(0, 0);\r |
223 | break;\r |
224 | \r |
2433f409 |
225 | case JOB940_YM2612UPDATEONE:\r |
226 | ym_update(ym_buffer);\r |
abe0ea43 |
227 | break;\r |
cc68a136 |
228 | \r |
abe0ea43 |
229 | case JOB940_MP3DECODE:\r |
230 | mp3_decode();\r |
231 | break;\r |
091facb8 |
232 | \r |
233 | case JOB940_MP3RESET:\r |
234 | if (shared_data->mp3dec) MP3FreeDecoder(shared_data->mp3dec);\r |
235 | shared_data->mp3dec = MP3InitDecoder();\r |
236 | break;\r |
abe0ea43 |
237 | }\r |
42c7b147 |
238 | \r |
239 | shared_ctl->loopc++;\r |
abe0ea43 |
240 | dcache_clean();\r |
cc68a136 |
241 | }\r |
242 | }\r |
b837b69b |
243 | \r |