1 // This is part of Pico Library
\r
3 // (c) Copyright 2004 Dave, All rights reserved.
\r
4 // (c) Copyright 2006,2007 notaz, All rights reserved.
\r
5 // Free for non-commercial use.
\r
7 // For commercial use, separate licencing terms must be obtained.
\r
12 #include "sn76496.h"
\r
13 #include "../pico_int.h"
\r
14 #include "../cd/pcm.h"
\r
17 void (*PsndMix_32_to_16l)(short *dest, int *src, int count) = mix_32_to_16l_stereo;
\r
19 // master int buffer to mix to
\r
20 static int PsndBuffer[2*(44100+100)/50];
\r
23 static unsigned short dac_info[312+4]; // pppppppp ppppllll, p - pos in buff, l - length to write for this sample
\r
25 // cdda output buffer
\r
26 short cdda_out_buffer[2*1152];
\r
30 int PsndLen=0; // number of mono samples, multiply by 2 for stereo
\r
31 int PsndLen_exc_add=0; // this is for non-integer sample counts per line, eg. 22050/60
\r
32 int PsndLen_exc_cnt=0;
\r
34 short *PsndOut=NULL; // PCM data buffer
\r
37 int timer_a_next_oflow, timer_a_step; // in z80 cycles
\r
38 int timer_b_next_oflow, timer_b_step;
\r
41 extern int *sn76496_regs;
\r
44 static void dac_recalculate(void)
\r
46 int i, dac_cnt, pos, len, lines = Pico.m.pal ? 312 : 262, mid = Pico.m.pal ? 68 : 93;
\r
48 if (PsndLen <= lines)
\r
55 for(i=226; i != 225; i++)
\r
57 if (i >= lines) i = 0;
\r
65 dac_info[i] = (pos<<4)|len;
\r
73 for(i = 225; i != 224; i++)
\r
75 if (i >= lines) i = 0;
\r
77 while(dac_cnt >= 0) {
\r
81 if (i == mid) // midpoint
\r
82 while(pos+len < PsndLen/2) {
\r
87 dac_info[i] = (pos<<4)|len;
\r
91 for(len = 0, i = pos; i < PsndLen; i++) len++;
\r
92 if (PsndLen_exc_add) len++;
\r
93 dac_info[224] = (pos<<4)|len;
\r
95 mid = (dac_info[lines-1] & 0xfff0) + ((dac_info[lines-1] & 0xf) << 4);
\r
96 for (i = lines; i < sizeof(dac_info) / sizeof(dac_info[0]); i++)
\r
98 //for(i=len=0; i < lines; i++) {
\r
99 // printf("%03i : %03i : %i\n", i, dac_info[i]>>4, dac_info[i]&0xf);
\r
100 // len+=dac_info[i]&0xf;
\r
102 //printf("rate is %i, len %f\n", PsndRate, (double)PsndRate/(Pico.m.pal ? 50.0 : 60.0));
\r
103 //printf("len total: %i, last pos: %i\n", len, pos);
\r
108 PICO_INTERNAL void PsndReset(void)
\r
110 // PsndRerate calls YM2612Init, which also resets
\r
116 // to be called after changing sound rate or chips
\r
117 void PsndRerate(int preserve_state)
\r
119 void *state = NULL;
\r
120 int target_fps = Pico.m.pal ? 50 : 60;
\r
122 // not all rates are supported in MCD mode due to mp3 decoder limitations
\r
123 if (PicoAHW & PAHW_MCD) {
\r
124 if (!(11025-100 <= PsndRate && PsndRate <= 11025+100) &&
\r
125 !(22050-100 <= PsndRate && PsndRate <= 22050+100) &&
\r
126 !(44100-100 <= PsndRate && PsndRate <= 44100+100))
\r
128 PicoOpt |= POPT_EN_STEREO; // force stereo
\r
131 if (preserve_state) {
\r
132 state = malloc(0x204);
\r
133 if (state == NULL) return;
\r
134 ym2612_pack_state();
\r
135 memcpy(state, YM2612GetRegs(), 0x204);
\r
137 YM2612Init(Pico.m.pal ? OSC_PAL/7 : OSC_NTSC/7, PsndRate);
\r
138 if (preserve_state) {
\r
139 // feed it back it's own registers, just like after loading state
\r
140 memcpy(YM2612GetRegs(), state, 0x204);
\r
141 ym2612_unpack_state();
\r
142 if ((PicoAHW & PAHW_MCD) && !(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1))
\r
146 if (preserve_state) memcpy(state, sn76496_regs, 28*4); // remember old state
\r
147 SN76496_init(Pico.m.pal ? OSC_PAL/15 : OSC_NTSC/15, PsndRate);
\r
148 if (preserve_state) memcpy(sn76496_regs, state, 28*4); // restore old state
\r
153 // calculate PsndLen
\r
154 PsndLen=PsndRate / target_fps;
\r
155 PsndLen_exc_add=((PsndRate - PsndLen*target_fps)<<16) / target_fps;
\r
158 // recalculate dac info
\r
161 if (PicoAHW & PAHW_MCD)
\r
162 pcm_set_rate(PsndRate);
\r
164 // clear all buffers
\r
165 memset32(PsndBuffer, 0, sizeof(PsndBuffer)/4);
\r
166 memset(cdda_out_buffer, 0, sizeof(cdda_out_buffer));
\r
171 PsndMix_32_to_16l = (PicoOpt & POPT_EN_STEREO) ? mix_32_to_16l_stereo : mix_32_to_16_mono;
\r
173 if (PicoAHW & PAHW_PICO)
\r
178 PICO_INTERNAL void PsndDoDAC(int line_to)
\r
180 int pos, pos1, len;
\r
181 int dout = ym2612.dacout;
\r
182 int line_from = PsndDacLine;
\r
184 PsndDacLine = line_to + 1;
\r
186 pos =dac_info[line_from]>>4;
\r
187 pos1=dac_info[line_to];
\r
188 len = ((pos1>>4)-pos) + (pos1&0xf);
\r
191 if (PicoOpt & POPT_EN_STEREO) {
\r
192 short *d = PsndOut + pos*2;
\r
193 for (; len > 0; len--, d+=2) *d = dout;
\r
195 short *d = PsndOut + pos;
\r
196 for (; len > 0; len--, d++) *d = dout;
\r
201 int *d = PsndBuffer;
\r
202 d += (PicoOpt&8) ? pos*2 : pos;
\r
203 pcm_update(d, len, 1);
\r
209 static pm_file *cdda_stream = NULL;
\r
211 static void cdda_raw_update(int *buffer, int length)
\r
213 int ret, cdda_bytes, mult = 1;
\r
214 if (cdda_stream == NULL)
\r
217 cdda_bytes = length*4;
\r
218 if (PsndRate <= 22050 + 100) mult = 2;
\r
219 if (PsndRate < 22050 - 100) mult = 4;
\r
220 cdda_bytes *= mult;
\r
222 ret = pm_read(cdda_out_buffer, cdda_bytes, cdda_stream);
\r
223 if (ret < cdda_bytes) {
\r
224 memset((char *)cdda_out_buffer + ret, 0, cdda_bytes - ret);
\r
225 cdda_stream = NULL;
\r
231 case 1: mix_16h_to_32(buffer, cdda_out_buffer, length*2); break;
\r
232 case 2: mix_16h_to_32_s1(buffer, cdda_out_buffer, length*2); break;
\r
233 case 4: mix_16h_to_32_s2(buffer, cdda_out_buffer, length*2); break;
\r
237 PICO_INTERNAL void cdda_start_play(void)
\r
239 int lba_offset, index, lba_length, i;
\r
241 elprintf(EL_STATUS, "cdda play track #%i", Pico_mcd->scd.Cur_Track);
\r
243 index = Pico_mcd->scd.Cur_Track - 1;
\r
245 lba_offset = Pico_mcd->scd.Cur_LBA - Track_to_LBA(index + 1);
\r
246 if (lba_offset < 0) lba_offset = 0;
\r
247 lba_offset += Pico_mcd->TOC.Tracks[index].Offset;
\r
249 // find the actual file for this track
\r
250 for (i = index; i > 0; i--)
\r
251 if (Pico_mcd->TOC.Tracks[i].F != NULL) break;
\r
253 if (Pico_mcd->TOC.Tracks[i].F == NULL) {
\r
254 elprintf(EL_STATUS|EL_ANOMALY, "no track?!");
\r
258 if (Pico_mcd->TOC.Tracks[i].ftype == TYPE_MP3)
\r
262 lba_length = Pico_mcd->TOC.Tracks[i].Length;
\r
263 for (i++; i < Pico_mcd->TOC.Last_Track; i++) {
\r
264 if (Pico_mcd->TOC.Tracks[i].F != NULL) break;
\r
265 lba_length += Pico_mcd->TOC.Tracks[i].Length;
\r
269 pos1024 = lba_offset * 1024 / lba_length;
\r
271 mp3_start_play(Pico_mcd->TOC.Tracks[index].F, pos1024);
\r
275 cdda_stream = Pico_mcd->TOC.Tracks[i].F;
\r
276 PicoCDBufferFlush(); // buffering relies on fp not being touched
\r
277 pm_seek(cdda_stream, lba_offset * 2352, SEEK_SET);
\r
278 if (Pico_mcd->TOC.Tracks[i].ftype == TYPE_WAV)
\r
280 // skip headers, assume it's 44kHz stereo uncompressed
\r
281 pm_seek(cdda_stream, 44, SEEK_CUR);
\r
286 PICO_INTERNAL void PsndClear(void)
\r
289 if (PsndLen_exc_add) len++;
\r
290 if (PicoOpt & POPT_EN_STEREO)
\r
291 memset32((int *) PsndOut, 0, len); // assume PsndOut to be aligned
\r
293 short *out = PsndOut;
\r
294 if ((long)out & 2) { *out++ = 0; len--; }
\r
295 memset32((int *) out, 0, len/2);
\r
296 if (len & 1) out[len-1] = 0;
\r
301 static int PsndRender(int offset, int length)
\r
303 int buf32_updated = 0;
\r
304 int *buf32 = PsndBuffer+offset;
\r
305 int stereo = (PicoOpt & 8) >> 3;
\r
306 // emulating CD && PCM option enabled && PCM chip on && have enabled channels
\r
307 int do_pcm = (PicoAHW & PAHW_MCD) && (PicoOpt&POPT_EN_MCD_PCM) &&
\r
308 (Pico_mcd->pcm.control & 0x80) && Pico_mcd->pcm.enabled;
\r
311 pprof_start(sound);
\r
313 #if !SIMPLE_WRITE_SOUND
\r
314 if (offset == 0) { // should happen once per frame
\r
315 // compensate for float part of PsndLen
\r
316 PsndLen_exc_cnt += PsndLen_exc_add;
\r
317 if (PsndLen_exc_cnt >= 0x10000) {
\r
318 PsndLen_exc_cnt -= 0x10000;
\r
325 if (PicoOpt & POPT_EN_PSG)
\r
326 SN76496Update(PsndOut+offset, length, stereo);
\r
328 if (PicoAHW & PAHW_PICO) {
\r
329 PicoPicoPCMUpdate(PsndOut+offset, length, stereo);
\r
333 // Add in the stereo FM buffer
\r
334 if (PicoOpt & POPT_EN_FM) {
\r
335 buf32_updated = YM2612UpdateOne(buf32, length, stereo, 1);
\r
337 memset32(buf32, 0, length<<stereo);
\r
339 //printf("active_chs: %02x\n", buf32_updated);
\r
343 pcm_update(buf32, length, stereo);
\r
344 //buf32_updated = 1;
\r
348 // CD mode, cdda enabled, not data track, CDC is reading
\r
349 if ((PicoAHW & PAHW_MCD) && (PicoOpt & POPT_EN_MCD_CDDA) &&
\r
350 !(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1))
\r
352 // note: only 44, 22 and 11 kHz supported, with forced stereo
\r
353 int index = Pico_mcd->scd.Cur_Track - 1;
\r
355 if (Pico_mcd->TOC.Tracks[index].ftype == TYPE_MP3)
\r
356 mp3_update(buf32, length, stereo);
\r
358 cdda_raw_update(buf32, length);
\r
361 if ((PicoAHW & PAHW_32X) && (PicoOpt & POPT_EN_PWM))
\r
362 p32x_pwm_update(buf32, length, stereo);
\r
364 // convert + limit to normal 16bit output
\r
365 PsndMix_32_to_16l(PsndOut+offset, buf32, length);
\r
372 // to be called on 224 or line_sample scanlines only
\r
373 PICO_INTERNAL void PsndGetSamples(int y)
\r
375 #if SIMPLE_WRITE_SOUND
\r
376 if (y != 224) return;
\r
377 PsndRender(0, PsndLen);
\r
378 if (PicoWriteSound) PicoWriteSound(PsndLen);
\r
381 static int curr_pos = 0;
\r
386 curr_pos += PsndRender(curr_pos, PsndLen-PsndLen/2);
\r
387 else curr_pos = PsndRender(0, PsndLen);
\r
388 if (emustatus&1) emustatus|=2; else emustatus&=~2;
\r
389 if (PicoWriteSound) PicoWriteSound(curr_pos);
\r
390 // clear sound buffer
\r
393 else if (emustatus & 3) {
\r
396 curr_pos = PsndRender(0, PsndLen/2);
\r
401 PICO_INTERNAL void PsndGetSamplesMS(void)
\r
403 int stereo = (PicoOpt & 8) >> 3;
\r
404 int length = PsndLen;
\r
406 #if !SIMPLE_WRITE_SOUND
\r
407 // compensate for float part of PsndLen
\r
408 PsndLen_exc_cnt += PsndLen_exc_add;
\r
409 if (PsndLen_exc_cnt >= 0x10000) {
\r
410 PsndLen_exc_cnt -= 0x10000;
\r
416 if (PicoOpt & POPT_EN_PSG)
\r
417 SN76496Update(PsndOut, length, stereo);
\r
419 // upmix to "stereo" if needed
\r
422 for (i = length, p = (void *)PsndOut; i > 0; i--, p++)
\r
426 if (PicoWriteSound != NULL)
\r
427 PicoWriteSound(length);
\r