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