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