cc68a136 |
1 | /* faked 940 code just uses local copy of ym2612 */ |
2 | #include <stdio.h> |
3 | #include <stdlib.h> |
4 | #include <string.h> |
5 | #include <unistd.h> |
6 | #include <sys/mman.h> |
7 | #include <sys/ioctl.h> |
8 | #include <fcntl.h> |
9 | #include <errno.h> |
10 | |
11 | #include "../../Pico/sound/ym2612.h" |
12 | #include "../gp2x/gp2x.h" |
13 | #include "../gp2x/emu.h" |
14 | #include "../gp2x/menu.h" |
cb0316e4 |
15 | #include "../gp2x/code940/940shared.h" |
16 | #include "../gp2x/helix/pub/mp3dec.h" |
17 | #include "../../Pico/PicoInt.h" |
cc68a136 |
18 | |
19 | |
20 | static YM2612 ym2612; |
21 | |
22 | YM2612 *ym2612_940 = &ym2612; |
cc68a136 |
23 | |
51a902ae |
24 | // static _940_data_t shared_data_; |
cb0316e4 |
25 | static _940_ctl_t shared_ctl_; |
51a902ae |
26 | // static _940_data_t *shared_data = &shared_data_; |
e11c5548 |
27 | _940_ctl_t *shared_ctl = &shared_ctl_; |
cb0316e4 |
28 | |
29 | unsigned char *mp3_mem = 0; |
30 | |
31 | #define MP3_SIZE_MAX (0x1000000 - 4*640*480) |
cc68a136 |
32 | |
33 | /***********************************************************/ |
34 | |
cc68a136 |
35 | |
36 | int YM2612Write_940(unsigned int a, unsigned int v) |
37 | { |
38 | YM2612Write_(a, v); |
39 | |
40 | return 0; // cause the engine to do updates once per frame only |
41 | } |
42 | |
43 | UINT8 YM2612Read_940(void) |
44 | { |
45 | return YM2612Read_(); |
46 | } |
47 | |
48 | |
49 | int YM2612PicoTick_940(int n) |
50 | { |
51 | YM2612PicoTick_(n); |
52 | |
53 | return 0; |
54 | } |
55 | |
56 | |
57 | void YM2612PicoStateLoad_940(void) |
58 | { |
59 | int i; |
60 | |
61 | YM2612PicoStateLoad_(); |
62 | |
63 | for(i = 0; i < 0x100; i++) { |
64 | YM2612Write_(0, i); |
65 | YM2612Write_(1, ym2612.REGS[i]); |
66 | } |
67 | for(i = 0; i < 0x100; i++) { |
68 | YM2612Write_(2, i); |
69 | YM2612Write_(3, ym2612.REGS[i|0x100]); |
70 | } |
71 | } |
72 | |
73 | |
e11c5548 |
74 | void sharedmem_init(void) |
cc68a136 |
75 | { |
cb0316e4 |
76 | mp3_mem = malloc(MP3_SIZE_MAX); |
e11c5548 |
77 | } |
78 | |
79 | void sharedmem_deinit(void) |
80 | { |
81 | free(mp3_mem); |
82 | } |
cb0316e4 |
83 | |
e11c5548 |
84 | void YM2612Init_940(int baseclock, int rate) |
85 | { |
cc68a136 |
86 | YM2612Init_(baseclock, rate); |
87 | } |
88 | |
89 | |
90 | void YM2612ResetChip_940(void) |
91 | { |
92 | YM2612ResetChip_(); |
93 | } |
94 | |
95 | |
cb0316e4 |
96 | #if 0 |
97 | static void local_decode(void) |
98 | { |
99 | int mp3_offs = shared_ctl->mp3_offs; |
100 | unsigned char *readPtr = mp3_mem + mp3_offs; |
101 | int bytesLeft = shared_ctl->mp3_len - mp3_offs; |
102 | int offset; // frame offset from readPtr |
103 | int err = 0; |
104 | |
105 | if (bytesLeft <= 0) return; // EOF, nothing to do |
106 | |
107 | offset = MP3FindSyncWord(readPtr, bytesLeft); |
108 | if (offset < 0) { |
109 | shared_ctl->mp3_offs = shared_ctl->mp3_len; |
110 | return; // EOF |
111 | } |
112 | readPtr += offset; |
113 | bytesLeft -= offset; |
114 | |
115 | err = MP3Decode(shared_data->mp3dec, &readPtr, &bytesLeft, |
116 | shared_data->mp3_buffer[shared_ctl->mp3_buffsel], 0); |
117 | if (err) { |
118 | if (err == ERR_MP3_INDATA_UNDERFLOW) { |
119 | shared_ctl->mp3_offs = shared_ctl->mp3_len; // EOF |
120 | return; |
121 | } else if (err <= -6 && err >= -12) { |
122 | // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_* |
123 | // just try to skip the offending frame.. |
124 | readPtr++; |
125 | } |
126 | shared_ctl->mp3_errors++; |
127 | shared_ctl->mp3_lasterr = err; |
128 | } |
129 | shared_ctl->mp3_offs = readPtr - mp3_mem; |
130 | } |
131 | #endif |
132 | |
133 | |
134 | |
135 | |
136 | static FILE *loaded_mp3 = 0; |
137 | |
4f265db7 |
138 | int YM2612UpdateOne_940(int *buffer, int length, int stereo, int is_buf_empty) |
cc68a136 |
139 | { |
cb0316e4 |
140 | #if 0 |
141 | int cdda_on, *ym_buffer = mix_buffer; |
142 | static int mp3_samples_ready = 0, mp3_buffer_offs = 0; |
143 | static int mp3_play_bufsel = 1; |
144 | |
145 | |
146 | YM2612UpdateOne_(buffer, length, stereo); // really writes to mix_buffer |
cc68a136 |
147 | |
cb0316e4 |
148 | // emulatind MCD, not data track, CDC is reading, playback was started, track not ended |
149 | cdda_on = (PicoMCD & 1) && !(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1) |
150 | && loaded_mp3 && shared_ctl->mp3_offs < shared_ctl->mp3_len; |
151 | |
152 | /* mix data from previous go */ |
153 | if (cdda_on && mp3_samples_ready >= length) |
154 | { |
155 | if (1152 - mp3_buffer_offs >= length) { |
156 | mix_samples(buffer, ym_buffer, shared_data->mp3_buffer[mp3_play_bufsel] + mp3_buffer_offs*2, length, stereo); |
157 | |
158 | mp3_buffer_offs += length; |
159 | } else { |
160 | // collect from both buffers.. |
161 | int left = 1152 - mp3_buffer_offs; |
162 | mix_samples(buffer, ym_buffer, shared_data->mp3_buffer[mp3_play_bufsel] + mp3_buffer_offs*2, left, stereo); |
163 | mp3_play_bufsel ^= 1; |
164 | mp3_buffer_offs = length - left; |
165 | mix_samples(buffer + left * 2, ym_buffer + left * 2, |
166 | shared_data->mp3_buffer[mp3_play_bufsel], mp3_buffer_offs, stereo); |
cc68a136 |
167 | } |
cb0316e4 |
168 | mp3_samples_ready -= length; |
cc68a136 |
169 | } else { |
cb0316e4 |
170 | mix_samples(buffer, ym_buffer, 0, length, stereo); |
171 | } |
172 | |
173 | // make sure we will have enough mp3 samples next frame |
174 | if (cdda_on && mp3_samples_ready < length) |
175 | { |
176 | shared_ctl->mp3_buffsel ^= 1; |
177 | local_decode(); |
178 | mp3_samples_ready += 1152; |
179 | } |
180 | #else |
4f265db7 |
181 | return YM2612UpdateOne_(buffer, length, stereo, is_buf_empty); |
cb0316e4 |
182 | #endif |
183 | } |
184 | |
185 | |
e11c5548 |
186 | void mp3_update(int *buffer, int length, int stereo) |
187 | { |
188 | // nothing.. |
189 | } |
190 | |
191 | |
cb0316e4 |
192 | /***********************************************************/ |
193 | |
194 | void mp3_start_play(FILE *f, int pos) // pos is 0-1023 |
195 | { |
196 | int byte_offs = 0; |
197 | |
198 | if (loaded_mp3 != f) |
199 | { |
200 | printf("loading mp3... "); fflush(stdout); |
201 | fseek(f, 0, SEEK_SET); |
202 | fread(mp3_mem, 1, MP3_SIZE_MAX, f); |
203 | if (feof(f)) printf("done.\n"); |
204 | else printf("done. mp3 too large, not all data loaded.\n"); |
205 | shared_ctl->mp3_len = ftell(f); |
206 | loaded_mp3 = f; |
cc68a136 |
207 | } |
cb0316e4 |
208 | |
209 | // seek.. |
210 | if (pos) { |
211 | byte_offs = (shared_ctl->mp3_len << 6) >> 10; |
212 | byte_offs *= pos; |
213 | byte_offs >>= 6; |
214 | } |
215 | printf("mp3 pos1024: %i, byte_offs %i/%i\n", pos, byte_offs, shared_ctl->mp3_len); |
216 | |
217 | shared_ctl->mp3_offs = byte_offs; |
cc68a136 |
218 | } |
219 | |
cb0316e4 |
220 | |
75736070 |
221 | int mp3_get_offset(void) |
222 | { |
223 | return 0; |
224 | } |
cb0316e4 |
225 | |
e11c5548 |
226 | |