input: kill "merge with defaults" behavior
[picodrive.git] / platform / gp2x / code940 / 940.c
... / ...
CommitLineData
1// The top-level functions for the ARM940\r
2// (c) Copyright 2006-2007, Grazvydas "notaz" Ignotas\r
3\r
4#include "940shared.h"\r
5#include "../../common/mp3.h"\r
6\r
7static _940_data_t *shared_data = (_940_data_t *) 0x00100000;\r
8static _940_ctl_t *shared_ctl = (_940_ctl_t *) 0x00200000;\r
9static unsigned char *mp3_data = (unsigned char *) 0x00400000;\r
10YM2612 *ym2612_940;\r
11\r
12// from init.s\r
13int wait_get_job(int oldjob);\r
14void spend_cycles(int c);\r
15void dcache_clean(void);\r
16void dcache_clean_flush(void);\r
17void drain_wb(void);\r
18// this should help to resolve race confition where shared var\r
19// is changed by other core just before we update it\r
20void set_if_not_changed(int *val, int oldval, int newval);\r
21\r
22void _memcpy(void *dst, const void *src, int count);\r
23\r
24// asm volatile ("mov r0, #0" ::: "r0");\r
25// asm volatile ("mcr p15, 0, r0, c7, c6, 0" ::: "r0"); /* flush dcache */\r
26// asm volatile ("mcr p15, 0, r0, c7, c10, 4" ::: "r0"); /* drain write buffer */\r
27\r
28\r
29static void mp3_decode(void)\r
30{\r
31 int mp3_offs = shared_ctl->mp3_offs;\r
32 unsigned char *readPtr = mp3_data + mp3_offs;\r
33 int bytesLeft = shared_ctl->mp3_len - mp3_offs;\r
34 int offset; // frame offset from readPtr\r
35 int retries = 0, err;\r
36\r
37 if (bytesLeft <= 0) return; // EOF, nothing to do\r
38\r
39 for (retries = 0; retries < 2; retries++)\r
40 {\r
41 offset = mp3_find_sync_word(readPtr, bytesLeft);\r
42 if (offset < 0)\r
43 goto set_eof;\r
44\r
45 readPtr += offset;\r
46 bytesLeft -= offset;\r
47\r
48 err = MP3Decode(shared_data->mp3dec, &readPtr, &bytesLeft,\r
49 shared_data->mp3_buffer[shared_ctl->mp3_buffsel], 0);\r
50 if (err) {\r
51 if (err == ERR_MP3_MAINDATA_UNDERFLOW)\r
52 // just need another frame\r
53 continue;\r
54\r
55 if (err == ERR_MP3_INDATA_UNDERFLOW)\r
56 goto set_eof;\r
57\r
58 if (err <= -6 && err >= -12) {\r
59 // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_*\r
60 // just try to skip the offending frame..\r
61 readPtr++;\r
62 bytesLeft--;\r
63 continue;\r
64 }\r
65 shared_ctl->mp3_errors++;\r
66 shared_ctl->mp3_lasterr = err;\r
67 }\r
68 break;\r
69 }\r
70\r
71 set_if_not_changed(&shared_ctl->mp3_offs, mp3_offs, readPtr - mp3_data);\r
72 return;\r
73\r
74set_eof:\r
75 set_if_not_changed(&shared_ctl->mp3_offs, mp3_offs, shared_ctl->mp3_len);\r
76}\r
77\r
78static void ym_flush_writes(void)\r
79{\r
80 UINT16 *wbuff;\r
81 int i;\r
82\r
83 if (shared_ctl->writebuffsel == 1) {\r
84 wbuff = shared_ctl->writebuff1;\r
85 } else {\r
86 wbuff = shared_ctl->writebuff0;\r
87 }\r
88\r
89 /* playback all writes */\r
90 for (i = 2048; i > 0; i--) {\r
91 UINT16 d = *wbuff++;\r
92 if (d == 0xffff) break;\r
93 if (d == 0xfffe) continue;\r
94 YM2612Write_(d >> 8, d);\r
95 }\r
96}\r
97\r
98static void ym_update(int *ym_buffer)\r
99{\r
100 int i, dw;\r
101 int two_upds = 0;\r
102 UINT16 *wbuff;\r
103\r
104 if (shared_ctl->writebuffsel == 1) {\r
105 wbuff = shared_ctl->writebuff1;\r
106 } else {\r
107 wbuff = shared_ctl->writebuff0;\r
108 }\r
109\r
110 /* playback all writes */\r
111 for (i = 2048/2; i > 0; i--) {\r
112 UINT16 d;\r
113 dw = *(int *)wbuff;\r
114 d = dw;\r
115 wbuff++;\r
116 if (d == 0xffff) break;\r
117 if (d == 0xfffe) { two_upds=1; break; }\r
118 YM2612Write_(d >> 8, d);\r
119 d = (dw>>16);\r
120 wbuff++;\r
121 if (d == 0xffff) break;\r
122 if (d == 0xfffe) { two_upds=1; break; }\r
123 YM2612Write_(d >> 8, d);\r
124 }\r
125\r
126 if (two_upds)\r
127 {\r
128 int len1 = shared_ctl->length / 2;\r
129 shared_ctl->ym_active_chs =\r
130 YM2612UpdateOne_(ym_buffer, len1, shared_ctl->stereo, 1);\r
131\r
132 for (i *= 2; i > 0; i--) {\r
133 UINT16 d = *wbuff++;\r
134 if (d == 0xffff) break;\r
135 YM2612Write_(d >> 8, d);\r
136 }\r
137\r
138 ym_buffer += shared_ctl->stereo ? len1*2 : len1;\r
139 len1 = shared_ctl->length - len1;\r
140\r
141 shared_ctl->ym_active_chs =\r
142 YM2612UpdateOne_(ym_buffer, len1, shared_ctl->stereo, 1);\r
143 } else {\r
144 shared_ctl->ym_active_chs =\r
145 YM2612UpdateOne_(ym_buffer, shared_ctl->length, shared_ctl->stereo, 1);\r
146 }\r
147}\r
148\r
149\r
150void Main940(void)\r
151{\r
152 int *ym_buffer = shared_data->ym_buffer;\r
153 int job = 0;\r
154 ym2612_940 = &shared_data->ym2612;\r
155\r
156\r
157 for (;;)\r
158 {\r
159 job = wait_get_job(job);\r
160\r
161 shared_ctl->lastjob = job;\r
162\r
163 switch (job)\r
164 {\r
165 case JOB940_INITALL:\r
166 /* ym2612 */\r
167 shared_ctl->writebuff0[0] = shared_ctl->writebuff1[0] = 0xffff;\r
168 YM2612Init_(shared_ctl->baseclock, shared_ctl->rate);\r
169 /* Helix mp3 decoder */\r
170 shared_data->mp3dec = MP3InitDecoder();\r
171 break;\r
172\r
173 case JOB940_INVALIDATE_DCACHE:\r
174 drain_wb();\r
175 dcache_clean_flush();\r
176 break;\r
177\r
178 case JOB940_YM2612RESETCHIP:\r
179 YM2612ResetChip_();\r
180 break;\r
181\r
182 case JOB940_PICOSTATELOAD:\r
183 YM2612PicoStateLoad_();\r
184 break;\r
185\r
186 case JOB940_PICOSTATESAVE2:\r
187 YM2612PicoStateSave2(0, 0);\r
188 _memcpy(shared_ctl->writebuff0, ym2612_940->REGS, 0x200);\r
189 break;\r
190\r
191 case JOB940_PICOSTATELOAD2_PREP:\r
192 ym_flush_writes();\r
193 break;\r
194\r
195 case JOB940_PICOSTATELOAD2:\r
196 _memcpy(ym2612_940->REGS, shared_ctl->writebuff0, 0x200);\r
197 YM2612PicoStateLoad2(0, 0);\r
198 break;\r
199\r
200 case JOB940_YM2612UPDATEONE:\r
201 ym_update(ym_buffer);\r
202 break;\r
203\r
204 case JOB940_MP3DECODE:\r
205 mp3_decode();\r
206 break;\r
207\r
208 case JOB940_MP3RESET:\r
209 if (shared_data->mp3dec) MP3FreeDecoder(shared_data->mp3dec);\r
210 shared_data->mp3dec = MP3InitDecoder();\r
211 break;\r
212 }\r
213\r
214 shared_ctl->loopc++;\r
215 dcache_clean();\r
216 }\r
217}\r
218\r