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
28 int PsndLen=0; // number of mono samples, multiply by 2 for stereo
\r
29 int PsndLen_exc_add=0; // this is for non-integer sample counts per line, eg. 22050/60
\r
30 int PsndLen_exc_cnt=0;
\r
31 short *PsndOut=NULL; // PCM data buffer
\r
34 extern int *sn76496_regs;
\r
37 static void dac_recalculate(void)
\r
39 int i, dac_cnt, pos, len, lines = Pico.m.pal ? 312 : 262, mid = Pico.m.pal ? 68 : 93;
\r
41 if(PsndLen <= lines) {
\r
47 for(i=226; i != 225; i++) {
\r
48 if (i >= lines) i = 0;
\r
56 dac_info[i] = (pos<<4)|len;
\r
62 for(i = 225; i != 224; i++) {
\r
63 if (i >= lines) i = 0;
\r
65 while(dac_cnt >= 0) {
\r
69 if (i == mid) // midpoint
\r
70 while(pos+len < PsndLen/2) {
\r
75 dac_info[i] = (pos<<4)|len;
\r
79 for(len = 0, i = pos; i < PsndLen; i++) len++;
\r
80 if (PsndLen_exc_add) len++;
\r
81 dac_info[224] = (pos<<4)|len;
\r
83 //for(i=len=0; i < lines; i++) {
\r
84 // printf("%03i : %03i : %i\n", i, dac_info[i]>>4, dac_info[i]&0xf);
\r
85 // len+=dac_info[i]&0xf;
\r
87 //printf("rate is %i, len %f\n", PsndRate, (double)PsndRate/(Pico.m.pal ? 50.0 : 60.0));
\r
88 //printf("len total: %i, last pos: %i\n", len, pos);
\r
93 PICO_INTERNAL void PsndReset(void)
\r
97 // also clear the internal registers+addr line
\r
98 ym2612_regs = YM2612GetRegs();
\r
99 memset(ym2612_regs, 0, 0x200+4);
\r
100 z80startCycle = z80stopCycle = 0;
\r
106 // to be called after changing sound rate or chips
\r
107 void PsndRerate(int preserve_state)
\r
109 void *state = NULL;
\r
110 int target_fps = Pico.m.pal ? 50 : 60;
\r
112 // not all rates are supported in MCD mode due to mp3 decoder limitations
\r
113 if (PicoAHW & PAHW_MCD) {
\r
114 if (PsndRate != 11025 && PsndRate != 22050 && PsndRate != 44100) PsndRate = 22050;
\r
115 PicoOpt |= POPT_EN_STEREO; // force stereo
\r
118 if (preserve_state) {
\r
119 state = malloc(0x200);
\r
120 if (state == NULL) return;
\r
121 memcpy(state, YM2612GetRegs(), 0x200);
\r
122 if ((PicoAHW & PAHW_MCD) && Pico_mcd->m.audio_track)
\r
123 Pico_mcd->m.audio_offset = mp3_get_offset();
\r
125 YM2612Init(Pico.m.pal ? OSC_PAL/7 : OSC_NTSC/7, PsndRate);
\r
126 if (preserve_state) {
\r
127 // feed it back it's own registers, just like after loading state
\r
128 memcpy(YM2612GetRegs(), state, 0x200);
\r
129 YM2612PicoStateLoad();
\r
130 if ((PicoAHW & PAHW_MCD) && Pico_mcd->m.audio_track)
\r
131 mp3_start_play(Pico_mcd->TOC.Tracks[Pico_mcd->m.audio_track].F, Pico_mcd->m.audio_offset);
\r
134 if (preserve_state) memcpy(state, sn76496_regs, 28*4); // remember old state
\r
135 SN76496_init(Pico.m.pal ? OSC_PAL/15 : OSC_NTSC/15, PsndRate);
\r
136 if (preserve_state) memcpy(sn76496_regs, state, 28*4); // restore old state
\r
141 // calculate PsndLen
\r
142 PsndLen=PsndRate / target_fps;
\r
143 PsndLen_exc_add=((PsndRate - PsndLen*target_fps)<<16) / target_fps;
\r
146 // recalculate dac info
\r
149 if (PicoAHW & PAHW_MCD)
\r
150 pcm_set_rate(PsndRate);
\r
152 // clear all buffers
\r
153 memset32(PsndBuffer, 0, sizeof(PsndBuffer)/4);
\r
158 PsndMix_32_to_16l = (PicoOpt & POPT_EN_STEREO) ? mix_32_to_16l_stereo : mix_32_to_16_mono;
\r
162 // This is called once per raster (aka line), but not necessarily for every line
\r
163 PICO_INTERNAL void Psnd_timers_and_dac(int raster)
\r
166 int do_dac = PsndOut && (PicoOpt&POPT_EN_FM) && *ym2612_dacen;
\r
167 // int do_pcm = PsndOut && (PicoAHW&1) && (PicoOpt&0x400);
\r
169 // Our raster lasts 63.61323/64.102564 microseconds (NTSC/PAL)
\r
172 if (!do_dac /*&& !do_pcm*/) return;
\r
174 pos=dac_info[raster], len=pos&0xf;
\r
181 short *d = PsndOut + pos*2;
\r
182 int dout = *ym2612_dacout;
\r
183 if(PicoOpt&POPT_EN_STEREO) {
\r
184 // some manual loop unrolling here :)
\r
192 short *d = PsndOut + pos;
\r
204 int *d = PsndBuffer;
\r
205 d += (PicoOpt&8) ? pos*2 : pos;
\r
206 pcm_update(d, len, 1);
\r
212 PICO_INTERNAL void PsndClear(void)
\r
215 if (PsndLen_exc_add) len++;
\r
216 if (PicoOpt & POPT_EN_STEREO)
\r
217 memset32((int *) PsndOut, 0, len); // assume PsndOut to be aligned
\r
219 short *out = PsndOut;
\r
220 if ((int)out & 2) { *out++ = 0; len--; }
\r
221 memset32((int *) out, 0, len/2);
\r
222 if (len & 1) out[len-1] = 0;
\r
227 PICO_INTERNAL int PsndRender(int offset, int length)
\r
229 int buf32_updated = 0;
\r
230 int *buf32 = PsndBuffer+offset;
\r
231 int stereo = (PicoOpt & 8) >> 3;
\r
232 // emulating CD && PCM option enabled && PCM chip on && have enabled channels
\r
233 int do_pcm = (PicoAHW & PAHW_MCD) && (PicoOpt&POPT_EN_MCD_PCM) &&
\r
234 (Pico_mcd->pcm.control & 0x80) && Pico_mcd->pcm.enabled;
\r
237 #if !SIMPLE_WRITE_SOUND
\r
238 if (offset == 0) { // should happen once per frame
\r
239 // compensate for float part of PsndLen
\r
240 PsndLen_exc_cnt += PsndLen_exc_add;
\r
241 if (PsndLen_exc_cnt >= 0x10000) {
\r
242 PsndLen_exc_cnt -= 0x10000;
\r
249 if (PicoOpt & POPT_EN_PSG)
\r
250 SN76496Update(PsndOut+offset, length, stereo);
\r
252 // Add in the stereo FM buffer
\r
253 if (PicoOpt & POPT_EN_FM) {
\r
254 buf32_updated = YM2612UpdateOne(buf32, length, stereo, 1);
\r
256 memset32(buf32, 0, length<<stereo);
\r
258 //printf("active_chs: %02x\n", buf32_updated);
\r
262 pcm_update(buf32, length, stereo);
\r
263 //buf32_updated = 1;
\r
267 // CD mode, cdda enabled, not data track, CDC is reading
\r
268 if ((PicoAHW & PAHW_MCD) && (PicoOpt & POPT_EN_MCD_CDDA) &&
\r
269 !(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1))
\r
270 mp3_update(buf32, length, stereo);
\r
272 // convert + limit to normal 16bit output
\r
273 PsndMix_32_to_16l(PsndOut+offset, buf32, length);
\r
280 #if defined(_USE_MZ80)
\r
282 // memhandlers for mz80 core
\r
283 unsigned char mz80_read(UINT32 a, struct MemoryReadByte *w) { return z80_read(a); }
\r
284 void mz80_write(UINT32 a, UINT8 d, struct MemoryWriteByte *w) { z80_write(d, a); }
\r
286 // structures for mz80 core
\r
287 static struct MemoryReadByte mz80_mem_read[]=
\r
289 {0x0000,0xffff,mz80_read},
\r
290 {(UINT32) -1,(UINT32) -1,NULL}
\r
292 static struct MemoryWriteByte mz80_mem_write[]=
\r
294 {0x0000,0xffff,mz80_write},
\r
295 {(UINT32) -1,(UINT32) -1,NULL}
\r
297 static struct z80PortRead mz80_io_read[] ={
\r
298 {(UINT16) -1,(UINT16) -1,NULL}
\r
300 static struct z80PortWrite mz80_io_write[]={
\r
301 {(UINT16) -1,(UINT16) -1,NULL}
\r
304 int mz80_run(int cycles)
\r
306 int ticks_pre = mz80GetElapsedTicks(0);
\r
308 return mz80GetElapsedTicks(0) - ticks_pre;
\r
311 #elif defined(_USE_DRZ80)
\r
313 struct DrZ80 drZ80;
\r
315 static unsigned int DrZ80_rebasePC(unsigned short a)
\r
317 drZ80.Z80PC_BASE = (unsigned int) Pico.zram;
\r
318 return drZ80.Z80PC_BASE + a;
\r
321 static unsigned int DrZ80_rebaseSP(unsigned short a)
\r
323 drZ80.Z80SP_BASE = (unsigned int) Pico.zram;
\r
324 return drZ80.Z80SP_BASE + a;
\r
327 static void DrZ80_irq_callback()
\r
329 drZ80.Z80_IRQ = 0; // lower irq when accepted
\r
333 #if defined(_USE_DRZ80) || defined(_USE_CZ80)
\r
334 static unsigned char z80_in(unsigned short p)
\r
336 elprintf(EL_ANOMALY, "Z80 port %04x read", p);
\r
340 static void z80_out(unsigned short p,unsigned char d)
\r
342 elprintf(EL_ANOMALY, "Z80 port %04x write %02x", p, d);
\r
347 // z80 functionality wrappers
\r
348 PICO_INTERNAL void z80_init(void)
\r
350 #if defined(_USE_MZ80)
\r
351 struct mz80context z80;
\r
355 // Modify the default context
\r
356 mz80GetContext(&z80);
\r
358 // point mz80 stuff
\r
359 z80.z80Base=Pico.zram;
\r
360 z80.z80MemRead=mz80_mem_read;
\r
361 z80.z80MemWrite=mz80_mem_write;
\r
362 z80.z80IoRead=mz80_io_read;
\r
363 z80.z80IoWrite=mz80_io_write;
\r
365 mz80SetContext(&z80);
\r
367 #elif defined(_USE_DRZ80)
\r
368 memset(&drZ80, 0, sizeof(struct DrZ80));
\r
369 drZ80.z80_rebasePC=DrZ80_rebasePC;
\r
370 drZ80.z80_rebaseSP=DrZ80_rebaseSP;
\r
371 drZ80.z80_read8 =z80_read;
\r
372 drZ80.z80_read16 =z80_read16;
\r
373 drZ80.z80_write8 =z80_write;
\r
374 drZ80.z80_write16 =z80_write16;
\r
375 drZ80.z80_in =z80_in;
\r
376 drZ80.z80_out =z80_out;
\r
377 drZ80.z80_irq_callback=DrZ80_irq_callback;
\r
379 #elif defined(_USE_CZ80)
\r
380 memset(&CZ80, 0, sizeof(CZ80));
\r
382 Cz80_Set_Fetch(&CZ80, 0x0000, 0x1fff, (UINT32)Pico.zram); // main RAM
\r
383 Cz80_Set_Fetch(&CZ80, 0x2000, 0x3fff, (UINT32)Pico.zram); // mirror
\r
384 Cz80_Set_ReadB(&CZ80, (UINT8 (*)(UINT32 address))z80_read); // unused (hacked in)
\r
385 Cz80_Set_WriteB(&CZ80, z80_write);
\r
386 Cz80_Set_INPort(&CZ80, z80_in);
\r
387 Cz80_Set_OUTPort(&CZ80, z80_out);
\r
391 PICO_INTERNAL void z80_reset(void)
\r
393 #if defined(_USE_MZ80)
\r
395 #elif defined(_USE_DRZ80)
\r
396 memset(&drZ80, 0, 0x54);
\r
397 drZ80.Z80F = (1<<2); // set ZFlag
\r
398 drZ80.Z80F2 = (1<<2); // set ZFlag
\r
399 drZ80.Z80IX = 0xFFFF << 16;
\r
400 drZ80.Z80IY = 0xFFFF << 16;
\r
401 drZ80.Z80IM = 0; // 1?
\r
402 drZ80.Z80PC = drZ80.z80_rebasePC(0);
\r
403 drZ80.Z80SP = drZ80.z80_rebaseSP(0x2000); // 0xf000 ?
\r
404 #elif defined(_USE_CZ80)
\r
406 Cz80_Set_Reg(&CZ80, CZ80_IX, 0xffff);
\r
407 Cz80_Set_Reg(&CZ80, CZ80_IY, 0xffff);
\r
408 Cz80_Set_Reg(&CZ80, CZ80_SP, 0x2000);
\r
410 Pico.m.z80_fakeval = 0; // for faking when Z80 is disabled
\r
414 PICO_INTERNAL void z80_pack(unsigned char *data)
\r
416 #if defined(_USE_MZ80)
\r
417 struct mz80context mz80;
\r
418 *(int *)data = 0x00005A6D; // "mZ"
\r
419 mz80GetContext(&mz80);
\r
420 memcpy(data+4, &mz80.z80clockticks, sizeof(mz80)-5*4); // don't save base&memhandlers
\r
421 #elif defined(_USE_DRZ80)
\r
422 *(int *)data = 0x015A7244; // "DrZ" v1
\r
423 drZ80.Z80PC = drZ80.z80_rebasePC(drZ80.Z80PC-drZ80.Z80PC_BASE);
\r
424 drZ80.Z80SP = drZ80.z80_rebaseSP(drZ80.Z80SP-drZ80.Z80SP_BASE);
\r
425 memcpy(data+4, &drZ80, 0x54);
\r
426 #elif defined(_USE_CZ80)
\r
427 *(int *)data = 0x00007a43; // "Cz"
\r
428 *(int *)(data+4) = Cz80_Get_Reg(&CZ80, CZ80_PC);
\r
429 memcpy(data+8, &CZ80, (INT32)&CZ80.BasePC - (INT32)&CZ80);
\r
433 PICO_INTERNAL void z80_unpack(unsigned char *data)
\r
435 #if defined(_USE_MZ80)
\r
436 if (*(int *)data == 0x00005A6D) { // "mZ" save?
\r
437 struct mz80context mz80;
\r
438 mz80GetContext(&mz80);
\r
439 memcpy(&mz80.z80clockticks, data+4, sizeof(mz80)-5*4);
\r
440 mz80SetContext(&mz80);
\r
445 #elif defined(_USE_DRZ80)
\r
446 if (*(int *)data == 0x015A7244) { // "DrZ" v1 save?
\r
447 memcpy(&drZ80, data+4, 0x54);
\r
449 drZ80.Z80PC = drZ80.z80_rebasePC(drZ80.Z80PC-drZ80.Z80PC_BASE);
\r
450 drZ80.Z80SP = drZ80.z80_rebaseSP(drZ80.Z80SP-drZ80.Z80SP_BASE);
\r
454 z80_int(); // try to goto int handler, maybe we won't execute trash there?
\r
456 #elif defined(_USE_CZ80)
\r
457 if (*(int *)data == 0x00007a43) { // "Cz" save?
\r
458 memcpy(&CZ80, data+8, (INT32)&CZ80.BasePC - (INT32)&CZ80);
\r
459 Cz80_Set_Reg(&CZ80, CZ80_PC, *(int *)(data+4));
\r
467 PICO_INTERNAL void z80_exit(void)
\r
469 #if defined(_USE_MZ80)
\r
474 #if 1 // defined(__DEBUG_PRINT) || defined(__GP2X__) || defined(__GIZ__)
\r
475 PICO_INTERNAL void z80_debug(char *dstr)
\r
477 #if defined(_USE_DRZ80)
\r
478 sprintf(dstr, "Z80 state: PC: %04x SP: %04x\n", drZ80.Z80PC-drZ80.Z80PC_BASE, drZ80.Z80SP-drZ80.Z80SP_BASE);
\r
479 #elif defined(_USE_CZ80)
\r
480 sprintf(dstr, "Z80 state: PC: %04x SP: %04x\n", CZ80.PC - CZ80.BasePC, CZ80.SP.W);
\r