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