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