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
14 #include "../PicoInt.h"
\r
15 #include "../cd/pcm.h"
\r
18 void (*PsndMix_32_to_16l)(short *dest, int *src, int count) = mix_32_to_16l_stereo;
\r
20 // master int buffer to mix to
\r
21 static int PsndBuffer[2*44100/50];
\r
24 static unsigned short dac_info[312]; // pppppppp ppppllll, p - pos in buff, l - length to write for this sample
\r
26 // cdda output buffer
\r
27 short cdda_out_buffer[2*1152];
\r
31 int PsndLen=0; // number of mono samples, multiply by 2 for stereo
\r
32 int PsndLen_exc_add=0; // this is for non-integer sample counts per line, eg. 22050/60
\r
33 int PsndLen_exc_cnt=0;
\r
34 short *PsndOut=NULL; // PCM data buffer
\r
37 extern int *sn76496_regs;
\r
40 static void dac_recalculate(void)
\r
42 int i, dac_cnt, pos, len, lines = Pico.m.pal ? 312 : 262, mid = Pico.m.pal ? 68 : 93;
\r
44 if(PsndLen <= lines) {
\r
50 for(i=226; i != 225; i++) {
\r
51 if (i >= lines) i = 0;
\r
59 dac_info[i] = (pos<<4)|len;
\r
65 for(i = 225; i != 224; i++) {
\r
66 if (i >= lines) i = 0;
\r
68 while(dac_cnt >= 0) {
\r
72 if (i == mid) // midpoint
\r
73 while(pos+len < PsndLen/2) {
\r
78 dac_info[i] = (pos<<4)|len;
\r
82 for(len = 0, i = pos; i < PsndLen; i++) len++;
\r
83 if (PsndLen_exc_add) len++;
\r
84 dac_info[224] = (pos<<4)|len;
\r
86 //for(i=len=0; i < lines; i++) {
\r
87 // printf("%03i : %03i : %i\n", i, dac_info[i]>>4, dac_info[i]&0xf);
\r
88 // len+=dac_info[i]&0xf;
\r
90 //printf("rate is %i, len %f\n", PsndRate, (double)PsndRate/(Pico.m.pal ? 50.0 : 60.0));
\r
91 //printf("len total: %i, last pos: %i\n", len, pos);
\r
96 PICO_INTERNAL void PsndReset(void)
\r
100 // also clear the internal registers+addr line
\r
101 ym2612_regs = YM2612GetRegs();
\r
102 memset(ym2612_regs, 0, 0x200+4);
\r
103 z80startCycle = z80stopCycle = 0;
\r
109 // to be called after changing sound rate or chips
\r
110 void PsndRerate(int preserve_state)
\r
112 void *state = NULL;
\r
113 int target_fps = Pico.m.pal ? 50 : 60;
\r
115 // not all rates are supported in MCD mode due to mp3 decoder limitations
\r
116 if (PicoAHW & PAHW_MCD) {
\r
117 if (PsndRate != 11025 && PsndRate != 22050 && PsndRate != 44100) PsndRate = 22050;
\r
118 PicoOpt |= POPT_EN_STEREO; // force stereo
\r
121 if (preserve_state) {
\r
122 state = malloc(0x200);
\r
123 if (state == NULL) return;
\r
124 memcpy(state, YM2612GetRegs(), 0x200);
\r
126 YM2612Init(Pico.m.pal ? OSC_PAL/7 : OSC_NTSC/7, PsndRate);
\r
127 if (preserve_state) {
\r
128 // feed it back it's own registers, just like after loading state
\r
129 memcpy(YM2612GetRegs(), state, 0x200);
\r
130 YM2612PicoStateLoad();
\r
131 if ((PicoAHW & PAHW_MCD) && !(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1))
\r
135 if (preserve_state) memcpy(state, sn76496_regs, 28*4); // remember old state
\r
136 SN76496_init(Pico.m.pal ? OSC_PAL/15 : OSC_NTSC/15, PsndRate);
\r
137 if (preserve_state) memcpy(sn76496_regs, state, 28*4); // restore old state
\r
142 // calculate PsndLen
\r
143 PsndLen=PsndRate / target_fps;
\r
144 PsndLen_exc_add=((PsndRate - PsndLen*target_fps)<<16) / target_fps;
\r
147 // recalculate dac info
\r
150 if (PicoAHW & PAHW_MCD)
\r
151 pcm_set_rate(PsndRate);
\r
153 // clear all buffers
\r
154 memset32(PsndBuffer, 0, sizeof(PsndBuffer)/4);
\r
155 memset(cdda_out_buffer, 0, sizeof(cdda_out_buffer));
\r
160 PsndMix_32_to_16l = (PicoOpt & POPT_EN_STEREO) ? mix_32_to_16l_stereo : mix_32_to_16_mono;
\r
162 if (PicoAHW & PAHW_PICO)
\r
167 // This is called once per raster (aka line), but not necessarily for every line
\r
168 PICO_INTERNAL void Psnd_timers_and_dac(int raster)
\r
171 int do_dac = PsndOut && (PicoOpt&POPT_EN_FM) && *ym2612_dacen;
\r
172 // int do_pcm = PsndOut && (PicoAHW&1) && (PicoOpt&0x400);
\r
174 // Our raster lasts 63.61323/64.102564 microseconds (NTSC/PAL)
\r
177 if (!do_dac /*&& !do_pcm*/) return;
\r
179 pos=dac_info[raster], len=pos&0xf;
\r
186 short *d = PsndOut + pos*2;
\r
187 int dout = *ym2612_dacout;
\r
188 if(PicoOpt&POPT_EN_STEREO) {
\r
189 // some manual loop unrolling here :)
\r
197 short *d = PsndOut + pos;
\r
209 int *d = PsndBuffer;
\r
210 d += (PicoOpt&8) ? pos*2 : pos;
\r
211 pcm_update(d, len, 1);
\r
217 static pm_file *cdda_stream = NULL;
\r
219 static void cdda_raw_update(int *buffer, int length)
\r
221 int ret, cdda_bytes;
\r
222 if (cdda_stream == NULL) return;
\r
224 cdda_bytes = length*4;
\r
225 if (PsndRate <= 22050) cdda_bytes *= 2;
\r
226 if (PsndRate < 22050) cdda_bytes *= 2;
\r
228 ret = pm_read(cdda_out_buffer, cdda_bytes, cdda_stream);
\r
229 if (ret < cdda_bytes) {
\r
230 memset((char *)cdda_out_buffer + ret, 0, cdda_bytes - ret);
\r
231 cdda_stream = NULL;
\r
236 switch (PsndRate) {
\r
237 case 44100: mix_16h_to_32(buffer, cdda_out_buffer, length*2); break;
\r
238 case 22050: mix_16h_to_32_s1(buffer, cdda_out_buffer, length*2); break;
\r
239 case 11025: mix_16h_to_32_s2(buffer, cdda_out_buffer, length*2); break;
\r
243 PICO_INTERNAL void cdda_start_play(void)
\r
245 int lba_offset, index, lba_length, i;
\r
247 elprintf(EL_STATUS, "cdda play track #%i", Pico_mcd->scd.Cur_Track);
\r
249 index = Pico_mcd->scd.Cur_Track - 1;
\r
251 lba_offset = Pico_mcd->scd.Cur_LBA - Track_to_LBA(index + 1);
\r
252 if (lba_offset < 0) lba_offset = 0;
\r
253 lba_offset += Pico_mcd->TOC.Tracks[index].Offset;
\r
255 // find the actual file for this track
\r
256 for (i = index; i >= 0; i--)
\r
257 if (Pico_mcd->TOC.Tracks[i].F != NULL) break;
\r
259 if (Pico_mcd->TOC.Tracks[i].F == NULL) {
\r
260 elprintf(EL_STATUS|EL_ANOMALY, "no track?!");
\r
264 if (Pico_mcd->TOC.Tracks[i].ftype == TYPE_MP3)
\r
268 lba_length = Pico_mcd->TOC.Tracks[i].Length;
\r
269 for (i++; i < Pico_mcd->TOC.Last_Track; i++) {
\r
270 if (Pico_mcd->TOC.Tracks[i].F != NULL) break;
\r
271 lba_length += Pico_mcd->TOC.Tracks[i].Length;
\r
275 pos1024 = lba_offset * 1024 / lba_length;
\r
277 mp3_start_play(Pico_mcd->TOC.Tracks[index].F, pos1024);
\r
281 cdda_stream = Pico_mcd->TOC.Tracks[i].F;
\r
282 PicoCDBufferFlush(); // buffering relies on fp not being touched
\r
283 pm_seek(cdda_stream, lba_offset * 2352, SEEK_SET);
\r
284 if (Pico_mcd->TOC.Tracks[i].ftype == TYPE_WAV)
\r
286 // skip headers, assume it's 44kHz stereo uncompressed
\r
287 pm_seek(cdda_stream, 44, SEEK_CUR);
\r
292 PICO_INTERNAL void PsndClear(void)
\r
295 if (PsndLen_exc_add) len++;
\r
296 if (PicoOpt & POPT_EN_STEREO)
\r
297 memset32((int *) PsndOut, 0, len); // assume PsndOut to be aligned
\r
299 short *out = PsndOut;
\r
300 if ((int)out & 2) { *out++ = 0; len--; }
\r
301 memset32((int *) out, 0, len/2);
\r
302 if (len & 1) out[len-1] = 0;
\r
307 PICO_INTERNAL int PsndRender(int offset, int length)
\r
309 int buf32_updated = 0;
\r
310 int *buf32 = PsndBuffer+offset;
\r
311 int stereo = (PicoOpt & 8) >> 3;
\r
312 // emulating CD && PCM option enabled && PCM chip on && have enabled channels
\r
313 int do_pcm = (PicoAHW & PAHW_MCD) && (PicoOpt&POPT_EN_MCD_PCM) &&
\r
314 (Pico_mcd->pcm.control & 0x80) && Pico_mcd->pcm.enabled;
\r
317 #if !SIMPLE_WRITE_SOUND
\r
318 if (offset == 0) { // should happen once per frame
\r
319 // compensate for float part of PsndLen
\r
320 PsndLen_exc_cnt += PsndLen_exc_add;
\r
321 if (PsndLen_exc_cnt >= 0x10000) {
\r
322 PsndLen_exc_cnt -= 0x10000;
\r
329 if (PicoOpt & POPT_EN_PSG)
\r
330 SN76496Update(PsndOut+offset, length, stereo);
\r
332 if (PicoAHW & PAHW_PICO) {
\r
333 PicoPicoPCMUpdate(PsndOut+offset, length, stereo);
\r
337 // Add in the stereo FM buffer
\r
338 if (PicoOpt & POPT_EN_FM) {
\r
339 buf32_updated = YM2612UpdateOne(buf32, length, stereo, 1);
\r
341 memset32(buf32, 0, length<<stereo);
\r
343 //printf("active_chs: %02x\n", buf32_updated);
\r
347 pcm_update(buf32, length, stereo);
\r
348 //buf32_updated = 1;
\r
352 // CD mode, cdda enabled, not data track, CDC is reading
\r
353 if ((PicoAHW & PAHW_MCD) && (PicoOpt & POPT_EN_MCD_CDDA) &&
\r
354 !(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1))
\r
356 // note: only 44, 22 and 11 kHz supported, with forced stereo
\r
357 int index = Pico_mcd->scd.Cur_Track - 1;
\r
359 if (Pico_mcd->TOC.Tracks[index].ftype == TYPE_MP3)
\r
360 mp3_update(buf32, length, stereo);
\r
362 cdda_raw_update(buf32, length);
\r
365 // convert + limit to normal 16bit output
\r
366 PsndMix_32_to_16l(PsndOut+offset, buf32, length);
\r
372 // -----------------------------------------------------------------
\r
376 #if defined(_USE_MZ80)
\r
378 // memhandlers for mz80 core
\r
379 unsigned char mz80_read(UINT32 a, struct MemoryReadByte *w) { return z80_read(a); }
\r
380 void mz80_write(UINT32 a, UINT8 d, struct MemoryWriteByte *w) { z80_write(d, a); }
\r
382 // structures for mz80 core
\r
383 static struct MemoryReadByte mz80_mem_read[]=
\r
385 {0x0000,0xffff,mz80_read},
\r
386 {(UINT32) -1,(UINT32) -1,NULL}
\r
388 static struct MemoryWriteByte mz80_mem_write[]=
\r
390 {0x0000,0xffff,mz80_write},
\r
391 {(UINT32) -1,(UINT32) -1,NULL}
\r
393 static struct z80PortRead mz80_io_read[] ={
\r
394 {(UINT16) -1,(UINT16) -1,NULL}
\r
396 static struct z80PortWrite mz80_io_write[]={
\r
397 {(UINT16) -1,(UINT16) -1,NULL}
\r
400 int mz80_run(int cycles)
\r
402 int ticks_pre = mz80GetElapsedTicks(0);
\r
404 return mz80GetElapsedTicks(0) - ticks_pre;
\r
407 #elif defined(_USE_DRZ80)
\r
409 struct DrZ80 drZ80;
\r
411 static unsigned int DrZ80_rebasePC(unsigned short a)
\r
413 drZ80.Z80PC_BASE = (unsigned int) Pico.zram;
\r
414 return drZ80.Z80PC_BASE + a;
\r
417 static unsigned int DrZ80_rebaseSP(unsigned short a)
\r
419 drZ80.Z80SP_BASE = (unsigned int) Pico.zram;
\r
420 return drZ80.Z80SP_BASE + a;
\r
423 static void DrZ80_irq_callback()
\r
425 drZ80.Z80_IRQ = 0; // lower irq when accepted
\r
429 #if defined(_USE_DRZ80) || defined(_USE_CZ80)
\r
430 static unsigned char z80_in(unsigned short p)
\r
432 elprintf(EL_ANOMALY, "Z80 port %04x read", p);
\r
436 static void z80_out(unsigned short p,unsigned char d)
\r
438 elprintf(EL_ANOMALY, "Z80 port %04x write %02x", p, d);
\r
443 // z80 functionality wrappers
\r
444 PICO_INTERNAL void z80_init(void)
\r
446 #if defined(_USE_MZ80)
\r
447 struct mz80context z80;
\r
451 // Modify the default context
\r
452 mz80GetContext(&z80);
\r
454 // point mz80 stuff
\r
455 z80.z80Base=Pico.zram;
\r
456 z80.z80MemRead=mz80_mem_read;
\r
457 z80.z80MemWrite=mz80_mem_write;
\r
458 z80.z80IoRead=mz80_io_read;
\r
459 z80.z80IoWrite=mz80_io_write;
\r
461 mz80SetContext(&z80);
\r
463 #elif defined(_USE_DRZ80)
\r
464 memset(&drZ80, 0, sizeof(struct DrZ80));
\r
465 drZ80.z80_rebasePC=DrZ80_rebasePC;
\r
466 drZ80.z80_rebaseSP=DrZ80_rebaseSP;
\r
467 drZ80.z80_read8 =z80_read;
\r
468 drZ80.z80_read16 =z80_read16;
\r
469 drZ80.z80_write8 =z80_write;
\r
470 drZ80.z80_write16 =z80_write16;
\r
471 drZ80.z80_in =z80_in;
\r
472 drZ80.z80_out =z80_out;
\r
473 drZ80.z80_irq_callback=DrZ80_irq_callback;
\r
475 #elif defined(_USE_CZ80)
\r
476 memset(&CZ80, 0, sizeof(CZ80));
\r
478 Cz80_Set_Fetch(&CZ80, 0x0000, 0x1fff, (UINT32)Pico.zram); // main RAM
\r
479 Cz80_Set_Fetch(&CZ80, 0x2000, 0x3fff, (UINT32)Pico.zram); // mirror
\r
480 Cz80_Set_ReadB(&CZ80, (UINT8 (*)(UINT32 address))z80_read); // unused (hacked in)
\r
481 Cz80_Set_WriteB(&CZ80, z80_write);
\r
482 Cz80_Set_INPort(&CZ80, z80_in);
\r
483 Cz80_Set_OUTPort(&CZ80, z80_out);
\r
487 PICO_INTERNAL void z80_reset(void)
\r
489 #if defined(_USE_MZ80)
\r
491 #elif defined(_USE_DRZ80)
\r
492 memset(&drZ80, 0, 0x54);
\r
493 drZ80.Z80F = (1<<2); // set ZFlag
\r
494 drZ80.Z80F2 = (1<<2); // set ZFlag
\r
495 drZ80.Z80IX = 0xFFFF << 16;
\r
496 drZ80.Z80IY = 0xFFFF << 16;
\r
497 drZ80.Z80IM = 0; // 1?
\r
498 drZ80.Z80PC = drZ80.z80_rebasePC(0);
\r
499 drZ80.Z80SP = drZ80.z80_rebaseSP(0x2000); // 0xf000 ?
\r
500 #elif defined(_USE_CZ80)
\r
502 Cz80_Set_Reg(&CZ80, CZ80_IX, 0xffff);
\r
503 Cz80_Set_Reg(&CZ80, CZ80_IY, 0xffff);
\r
504 Cz80_Set_Reg(&CZ80, CZ80_SP, 0x2000);
\r
506 Pico.m.z80_fakeval = 0; // for faking when Z80 is disabled
\r
510 PICO_INTERNAL void z80_pack(unsigned char *data)
\r
512 #if defined(_USE_MZ80)
\r
513 struct mz80context mz80;
\r
514 *(int *)data = 0x00005A6D; // "mZ"
\r
515 mz80GetContext(&mz80);
\r
516 memcpy(data+4, &mz80.z80clockticks, sizeof(mz80)-5*4); // don't save base&memhandlers
\r
517 #elif defined(_USE_DRZ80)
\r
518 *(int *)data = 0x015A7244; // "DrZ" v1
\r
519 drZ80.Z80PC = drZ80.z80_rebasePC(drZ80.Z80PC-drZ80.Z80PC_BASE);
\r
520 drZ80.Z80SP = drZ80.z80_rebaseSP(drZ80.Z80SP-drZ80.Z80SP_BASE);
\r
521 memcpy(data+4, &drZ80, 0x54);
\r
522 #elif defined(_USE_CZ80)
\r
523 *(int *)data = 0x00007a43; // "Cz"
\r
524 *(int *)(data+4) = Cz80_Get_Reg(&CZ80, CZ80_PC);
\r
525 memcpy(data+8, &CZ80, (INT32)&CZ80.BasePC - (INT32)&CZ80);
\r
529 PICO_INTERNAL void z80_unpack(unsigned char *data)
\r
531 #if defined(_USE_MZ80)
\r
532 if (*(int *)data == 0x00005A6D) { // "mZ" save?
\r
533 struct mz80context mz80;
\r
534 mz80GetContext(&mz80);
\r
535 memcpy(&mz80.z80clockticks, data+4, sizeof(mz80)-5*4);
\r
536 mz80SetContext(&mz80);
\r
541 #elif defined(_USE_DRZ80)
\r
542 if (*(int *)data == 0x015A7244) { // "DrZ" v1 save?
\r
543 memcpy(&drZ80, data+4, 0x54);
\r
545 drZ80.Z80PC = drZ80.z80_rebasePC(drZ80.Z80PC-drZ80.Z80PC_BASE);
\r
546 drZ80.Z80SP = drZ80.z80_rebaseSP(drZ80.Z80SP-drZ80.Z80SP_BASE);
\r
550 z80_int(); // try to goto int handler, maybe we won't execute trash there?
\r
552 #elif defined(_USE_CZ80)
\r
553 if (*(int *)data == 0x00007a43) { // "Cz" save?
\r
554 memcpy(&CZ80, data+8, (INT32)&CZ80.BasePC - (INT32)&CZ80);
\r
555 Cz80_Set_Reg(&CZ80, CZ80_PC, *(int *)(data+4));
\r
563 PICO_INTERNAL void z80_exit(void)
\r
565 #if defined(_USE_MZ80)
\r
570 #if 1 // defined(__DEBUG_PRINT) || defined(__GP2X__) || defined(__GIZ__)
\r
571 PICO_INTERNAL void z80_debug(char *dstr)
\r
573 #if defined(_USE_DRZ80)
\r
574 sprintf(dstr, "Z80 state: PC: %04x SP: %04x\n", drZ80.Z80PC-drZ80.Z80PC_BASE, drZ80.Z80SP-drZ80.Z80SP_BASE);
\r
575 #elif defined(_USE_CZ80)
\r
576 sprintf(dstr, "Z80 state: PC: %04x SP: %04x\n", CZ80.PC - CZ80.BasePC, CZ80.SP.W);
\r