cc68a136 |
1 | #include "940shared.h"\r |
2 | \r |
42c7b147 |
3 | static _940_data_t *shared_data = (_940_data_t *) 0x00100000;\r |
4 | static _940_ctl_t *shared_ctl = (_940_ctl_t *) 0x00200000;\r |
5 | static unsigned char *mp3_data = (unsigned char *) 0x01000000;\r |
cc68a136 |
6 | YM2612 *ym2612_940;\r |
cc68a136 |
7 | \r |
8 | // from init.s\r |
abe0ea43 |
9 | int wait_get_job(int oldjob);\r |
cc68a136 |
10 | void spend_cycles(int c);\r |
abe0ea43 |
11 | void dcache_clean(void);\r |
12 | void dcache_clean_flush(void);\r |
13 | void drain_wb(void);\r |
51a902ae |
14 | // this should help to resolve race confition where shared var\r |
15 | // is changed by other core just before we update it\r |
16 | void set_if_not_changed(int *val, int oldval, int newval);\r |
cc68a136 |
17 | \r |
18 | // asm volatile ("mov r0, #0" ::: "r0");\r |
19 | // asm volatile ("mcr p15, 0, r0, c7, c6, 0" ::: "r0"); /* flush dcache */\r |
20 | // asm volatile ("mcr p15, 0, r0, c7, c10, 4" ::: "r0"); /* drain write buffer */\r |
21 | \r |
51a902ae |
22 | \r |
23 | static void mp3_decode(void)\r |
24 | {\r |
25 | int mp3_offs = shared_ctl->mp3_offs;\r |
26 | unsigned char *readPtr = mp3_data + mp3_offs;\r |
27 | int bytesLeft = shared_ctl->mp3_len - mp3_offs;\r |
28 | int offset; // frame offset from readPtr\r |
29 | int err;\r |
30 | \r |
31 | if (bytesLeft <= 0) return; // EOF, nothing to do\r |
32 | \r |
33 | offset = MP3FindSyncWord(readPtr, bytesLeft);\r |
34 | if (offset < 0) {\r |
35 | set_if_not_changed(&shared_ctl->mp3_offs, mp3_offs, shared_ctl->mp3_len);\r |
36 | return; // EOF\r |
37 | }\r |
38 | readPtr += offset;\r |
39 | bytesLeft -= offset;\r |
40 | \r |
41 | err = MP3Decode(shared_data->mp3dec, &readPtr, &bytesLeft,\r |
42 | shared_data->mp3_buffer[shared_ctl->mp3_buffsel], 0);\r |
43 | if (err) {\r |
44 | if (err == ERR_MP3_INDATA_UNDERFLOW) {\r |
51a902ae |
45 | set_if_not_changed(&shared_ctl->mp3_offs, mp3_offs, shared_ctl->mp3_len);\r |
46 | return;\r |
47 | } else if (err <= -6 && err >= -12) {\r |
48 | // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_*\r |
49 | // just try to skip the offending frame..\r |
50 | readPtr++;\r |
51 | }\r |
52 | shared_ctl->mp3_errors++;\r |
53 | shared_ctl->mp3_lasterr = err;\r |
54 | }\r |
55 | set_if_not_changed(&shared_ctl->mp3_offs, mp3_offs, readPtr - mp3_data);\r |
56 | }\r |
57 | \r |
58 | \r |
abe0ea43 |
59 | void Main940(void)\r |
cc68a136 |
60 | {\r |
abe0ea43 |
61 | int *ym_buffer = shared_data->ym_buffer;\r |
62 | int job = 0;\r |
cc68a136 |
63 | ym2612_940 = &shared_data->ym2612;\r |
cc68a136 |
64 | \r |
cc68a136 |
65 | \r |
abe0ea43 |
66 | for (;;)\r |
cc68a136 |
67 | {\r |
abe0ea43 |
68 | job = wait_get_job(job);\r |
69 | \r |
70 | shared_ctl->lastjob = job;\r |
cc68a136 |
71 | \r |
abe0ea43 |
72 | switch (job)\r |
cc68a136 |
73 | {\r |
abe0ea43 |
74 | case JOB940_INITALL:\r |
75 | /* ym2612 */\r |
76 | shared_ctl->writebuff0[0] = shared_ctl->writebuff1[0] = 0xffff;\r |
77 | YM2612Init_(shared_ctl->baseclock, shared_ctl->rate);\r |
78 | /* Helix mp3 decoder */\r |
79 | shared_data->mp3dec = MP3InitDecoder();\r |
80 | break;\r |
81 | \r |
82 | case JOB940_INVALIDATE_DCACHE:\r |
83 | drain_wb();\r |
84 | dcache_clean_flush();\r |
85 | break;\r |
86 | \r |
87 | case JOB940_YM2612RESETCHIP:\r |
88 | YM2612ResetChip_();\r |
89 | break;\r |
90 | \r |
91 | case JOB940_PICOSTATELOAD:\r |
92 | YM2612PicoStateLoad_();\r |
93 | break;\r |
94 | \r |
95 | case JOB940_YM2612UPDATEONE: {\r |
96 | int i, dw, *wbuff;\r |
97 | if (shared_ctl->writebuffsel == 1) {\r |
98 | wbuff = (int *) shared_ctl->writebuff1;\r |
99 | } else {\r |
100 | wbuff = (int *) shared_ctl->writebuff0;\r |
b837b69b |
101 | }\r |
42c7b147 |
102 | \r |
abe0ea43 |
103 | /* playback all writes */\r |
104 | for (i = 2048/2; i > 0; i--) {\r |
105 | UINT16 d;\r |
106 | dw = *wbuff++;\r |
107 | d = dw;\r |
108 | if (d == 0xffff) break;\r |
109 | YM2612Write_(d >> 8, d);\r |
110 | d = (dw>>16);\r |
111 | if (d == 0xffff) break;\r |
112 | YM2612Write_(d >> 8, d);\r |
113 | }\r |
114 | \r |
85f8e929 |
115 | shared_ctl->ym_active_chs =\r |
116 | YM2612UpdateOne_(ym_buffer, shared_ctl->length, shared_ctl->stereo, 1);\r |
abe0ea43 |
117 | break;\r |
cc68a136 |
118 | }\r |
cc68a136 |
119 | \r |
abe0ea43 |
120 | case JOB940_MP3DECODE:\r |
121 | mp3_decode();\r |
122 | break;\r |
123 | }\r |
42c7b147 |
124 | \r |
125 | shared_ctl->loopc++;\r |
abe0ea43 |
126 | dcache_clean();\r |
cc68a136 |
127 | }\r |
128 | }\r |
b837b69b |
129 | \r |