ROM load progress bar
[picodrive.git] / platform / gp2x / code940 / 940.c
CommitLineData
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 6static _940_data_t *shared_data = (_940_data_t *) 0x00100000;\r
7static _940_ctl_t *shared_ctl = (_940_ctl_t *) 0x00200000;\r
e362c573 8static unsigned char *mp3_data = (unsigned char *) 0x00400000;\r
cc68a136 9YM2612 *ym2612_940;\r
cc68a136 10\r
11// from init.s\r
abe0ea43 12int wait_get_job(int oldjob);\r
cc68a136 13void spend_cycles(int c);\r
abe0ea43 14void dcache_clean(void);\r
15void dcache_clean_flush(void);\r
16void 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
19void set_if_not_changed(int *val, int oldval, int newval);\r
cc68a136 20\r
21// asm volatile ("mov r0, #0" ::: "r0");\r
22// asm volatile ("mcr p15, 0, r0, c7, c6, 0" ::: "r0"); /* flush dcache */\r
23// asm volatile ("mcr p15, 0, r0, c7, c10, 4" ::: "r0"); /* drain write buffer */\r
24\r
51a902ae 25\r
26static void mp3_decode(void)\r
27{\r
28 int mp3_offs = shared_ctl->mp3_offs;\r
29 unsigned char *readPtr = mp3_data + mp3_offs;\r
30 int bytesLeft = shared_ctl->mp3_len - mp3_offs;\r
31 int offset; // frame offset from readPtr\r
32 int err;\r
33\r
34 if (bytesLeft <= 0) return; // EOF, nothing to do\r
35\r
36 offset = MP3FindSyncWord(readPtr, bytesLeft);\r
37 if (offset < 0) {\r
38 set_if_not_changed(&shared_ctl->mp3_offs, mp3_offs, shared_ctl->mp3_len);\r
39 return; // EOF\r
40 }\r
41 readPtr += offset;\r
42 bytesLeft -= offset;\r
43\r
44 err = MP3Decode(shared_data->mp3dec, &readPtr, &bytesLeft,\r
45 shared_data->mp3_buffer[shared_ctl->mp3_buffsel], 0);\r
46 if (err) {\r
47 if (err == ERR_MP3_INDATA_UNDERFLOW) {\r
51a902ae 48 set_if_not_changed(&shared_ctl->mp3_offs, mp3_offs, shared_ctl->mp3_len);\r
49 return;\r
50 } else if (err <= -6 && err >= -12) {\r
51 // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_*\r
52 // just try to skip the offending frame..\r
53 readPtr++;\r
54 }\r
55 shared_ctl->mp3_errors++;\r
56 shared_ctl->mp3_lasterr = err;\r
57 }\r
58 set_if_not_changed(&shared_ctl->mp3_offs, mp3_offs, readPtr - mp3_data);\r
59}\r
60\r
2433f409 61static void ym_update(int *ym_buffer)\r
62{\r
63 int i, dw;\r
64 int two_upds = 0;\r
65 UINT16 *wbuff;\r
66\r
67 if (shared_ctl->writebuffsel == 1) {\r
68 wbuff = shared_ctl->writebuff1;\r
69 } else {\r
70 wbuff = shared_ctl->writebuff0;\r
71 }\r
72\r
73 /* playback all writes */\r
74 for (i = 2048/2; i > 0; i--) {\r
75 UINT16 d;\r
76 dw = *(int *)wbuff;\r
77 d = dw;\r
78 wbuff++;\r
79 if (d == 0xffff) break;\r
80 if (d == 0xfffe) { two_upds=1; break; }\r
81 YM2612Write_(d >> 8, d);\r
82 d = (dw>>16);\r
83 wbuff++;\r
84 if (d == 0xffff) break;\r
85 if (d == 0xfffe) { two_upds=1; break; }\r
86 YM2612Write_(d >> 8, d);\r
87 }\r
88\r
89 if (two_upds) {\r
90 int len1 = shared_ctl->length / 2;\r
91 shared_ctl->ym_active_chs =\r
92 YM2612UpdateOne_(ym_buffer, len1, shared_ctl->stereo, 1);\r
93\r
94 for (i *= 2; i > 0; i--) {\r
95 UINT16 d = *wbuff++;\r
96 if (d == 0xffff) break;\r
97 YM2612Write_(d >> 8, d);\r
98 }\r
99\r
100 ym_buffer += shared_ctl->stereo ? len1*2 : len1;\r
101 len1 = shared_ctl->length - len1;\r
102\r
103 shared_ctl->ym_active_chs =\r
104 YM2612UpdateOne_(ym_buffer, len1, shared_ctl->stereo, 1);\r
105 } else {\r
106 shared_ctl->ym_active_chs =\r
107 YM2612UpdateOne_(ym_buffer, shared_ctl->length, shared_ctl->stereo, 1);\r
108 }\r
109}\r
110\r
51a902ae 111\r
abe0ea43 112void Main940(void)\r
cc68a136 113{\r
abe0ea43 114 int *ym_buffer = shared_data->ym_buffer;\r
115 int job = 0;\r
cc68a136 116 ym2612_940 = &shared_data->ym2612;\r
cc68a136 117\r
cc68a136 118\r
abe0ea43 119 for (;;)\r
cc68a136 120 {\r
abe0ea43 121 job = wait_get_job(job);\r
122\r
123 shared_ctl->lastjob = job;\r
cc68a136 124\r
abe0ea43 125 switch (job)\r
cc68a136 126 {\r
abe0ea43 127 case JOB940_INITALL:\r
128 /* ym2612 */\r
129 shared_ctl->writebuff0[0] = shared_ctl->writebuff1[0] = 0xffff;\r
130 YM2612Init_(shared_ctl->baseclock, shared_ctl->rate);\r
131 /* Helix mp3 decoder */\r
132 shared_data->mp3dec = MP3InitDecoder();\r
133 break;\r
134\r
135 case JOB940_INVALIDATE_DCACHE:\r
136 drain_wb();\r
137 dcache_clean_flush();\r
138 break;\r
139\r
140 case JOB940_YM2612RESETCHIP:\r
141 YM2612ResetChip_();\r
142 break;\r
143\r
144 case JOB940_PICOSTATELOAD:\r
145 YM2612PicoStateLoad_();\r
146 break;\r
147\r
2433f409 148 case JOB940_YM2612UPDATEONE:\r
149 ym_update(ym_buffer);\r
abe0ea43 150 break;\r
cc68a136 151\r
abe0ea43 152 case JOB940_MP3DECODE:\r
153 mp3_decode();\r
154 break;\r
155 }\r
42c7b147 156\r
157 shared_ctl->loopc++;\r
abe0ea43 158 dcache_clean();\r
cc68a136 159 }\r
160}\r
b837b69b 161\r