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
114 if (PsndRate != 11025 && PsndRate != 22050 && PsndRate != 44100) PsndRate = 22050;
\r
115 PicoOpt |= 8; // 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 ((PicoMCD & 1) && 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 ((PicoMCD & 1) && 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
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 & 8) ? 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&1) && *ym2612_dacen;
\r
167 // int do_pcm = PsndOut && (PicoMCD&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
180 short *d = PsndOut + pos*2;
\r
181 int dout = *ym2612_dacout;
\r
183 // some manual loop unrolling here :)
\r
191 short *d = PsndOut + pos;
\r
203 int *d = PsndBuffer;
\r
204 d += (PicoOpt&8) ? pos*2 : pos;
\r
205 pcm_update(d, len, 1);
\r
211 PICO_INTERNAL void PsndClear(void)
\r
214 if (PsndLen_exc_add) len++;
\r
216 memset32((int *) PsndOut, 0, len); // assume PsndOut to be aligned
\r
218 short *out = PsndOut;
\r
219 if ((int)out & 2) { *out++ = 0; len--; }
\r
220 memset32((int *) out, 0, len/2);
\r
221 if (len & 1) out[len-1] = 0;
\r
226 PICO_INTERNAL int PsndRender(int offset, int length)
\r
228 int buf32_updated = 0;
\r
229 int *buf32 = PsndBuffer+offset;
\r
230 int stereo = (PicoOpt & 8) >> 3;
\r
231 // emulating CD && PCM option enabled && PCM chip on && have enabled channels
\r
232 int do_pcm = (PicoMCD&1) && (PicoOpt&0x400) && (Pico_mcd->pcm.control & 0x80) && Pico_mcd->pcm.enabled;
\r
235 #if !SIMPLE_WRITE_SOUND
\r
236 if (offset == 0) { // should happen once per frame
\r
237 // compensate for float part of PsndLen
\r
238 PsndLen_exc_cnt += PsndLen_exc_add;
\r
239 if (PsndLen_exc_cnt >= 0x10000) {
\r
240 PsndLen_exc_cnt -= 0x10000;
\r
248 SN76496Update(PsndOut+offset, length, stereo);
\r
250 // Add in the stereo FM buffer
\r
252 buf32_updated = YM2612UpdateOne(buf32, length, stereo, 1);
\r
254 memset32(buf32, 0, length<<stereo);
\r
256 //printf("active_chs: %02x\n", buf32_updated);
\r
260 pcm_update(buf32, length, stereo);
\r
261 //buf32_updated = 1;
\r
265 // CD mode, cdda enabled, not data track, CDC is reading
\r
266 if ((PicoMCD & 1) && (PicoOpt & 0x800) && !(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1))
\r
267 mp3_update(buf32, length, stereo);
\r
269 // convert + limit to normal 16bit output
\r
270 PsndMix_32_to_16l(PsndOut+offset, buf32, length);
\r
277 #if defined(_USE_MZ80)
\r
279 // memhandlers for mz80 core
\r
280 unsigned char mz80_read(UINT32 a, struct MemoryReadByte *w) { return z80_read(a); }
\r
281 void mz80_write(UINT32 a, UINT8 d, struct MemoryWriteByte *w) { z80_write(d, a); }
\r
283 // structures for mz80 core
\r
284 static struct MemoryReadByte mz80_mem_read[]=
\r
286 {0x0000,0xffff,mz80_read},
\r
287 {(UINT32) -1,(UINT32) -1,NULL}
\r
289 static struct MemoryWriteByte mz80_mem_write[]=
\r
291 {0x0000,0xffff,mz80_write},
\r
292 {(UINT32) -1,(UINT32) -1,NULL}
\r
294 static struct z80PortRead mz80_io_read[] ={
\r
295 {(UINT16) -1,(UINT16) -1,NULL}
\r
297 static struct z80PortWrite mz80_io_write[]={
\r
298 {(UINT16) -1,(UINT16) -1,NULL}
\r
301 int mz80_run(int cycles)
\r
303 int ticks_pre = mz80GetElapsedTicks(0);
\r
305 return mz80GetElapsedTicks(0) - ticks_pre;
\r
308 #elif defined(_USE_DRZ80)
\r
310 struct DrZ80 drZ80;
\r
312 static unsigned int DrZ80_rebasePC(unsigned short a)
\r
314 drZ80.Z80PC_BASE = (unsigned int) Pico.zram;
\r
315 return drZ80.Z80PC_BASE + a;
\r
318 static unsigned int DrZ80_rebaseSP(unsigned short a)
\r
320 drZ80.Z80SP_BASE = (unsigned int) Pico.zram;
\r
321 return drZ80.Z80SP_BASE + a;
\r
324 static void DrZ80_irq_callback()
\r
326 drZ80.Z80_IRQ = 0; // lower irq when accepted
\r
330 #if defined(_USE_DRZ80) || defined(_USE_CZ80)
\r
331 static unsigned char z80_in(unsigned short p)
\r
333 elprintf(EL_ANOMALY, "Z80 port %04x read", p);
\r
337 static void z80_out(unsigned short p,unsigned char d)
\r
339 elprintf(EL_ANOMALY, "Z80 port %04x write %02x", p, d);
\r
344 // z80 functionality wrappers
\r
345 PICO_INTERNAL void z80_init(void)
\r
347 #if defined(_USE_MZ80)
\r
348 struct mz80context z80;
\r
352 // Modify the default context
\r
353 mz80GetContext(&z80);
\r
355 // point mz80 stuff
\r
356 z80.z80Base=Pico.zram;
\r
357 z80.z80MemRead=mz80_mem_read;
\r
358 z80.z80MemWrite=mz80_mem_write;
\r
359 z80.z80IoRead=mz80_io_read;
\r
360 z80.z80IoWrite=mz80_io_write;
\r
362 mz80SetContext(&z80);
\r
364 #elif defined(_USE_DRZ80)
\r
365 memset(&drZ80, 0, sizeof(struct DrZ80));
\r
366 drZ80.z80_rebasePC=DrZ80_rebasePC;
\r
367 drZ80.z80_rebaseSP=DrZ80_rebaseSP;
\r
368 drZ80.z80_read8 =z80_read;
\r
369 drZ80.z80_read16 =z80_read16;
\r
370 drZ80.z80_write8 =z80_write;
\r
371 drZ80.z80_write16 =z80_write16;
\r
372 drZ80.z80_in =z80_in;
\r
373 drZ80.z80_out =z80_out;
\r
374 drZ80.z80_irq_callback=DrZ80_irq_callback;
\r
376 #elif defined(_USE_CZ80)
\r
377 memset(&CZ80, 0, sizeof(CZ80));
\r
379 Cz80_Set_Fetch(&CZ80, 0x0000, 0x1fff, (UINT32)Pico.zram); // main RAM
\r
380 Cz80_Set_Fetch(&CZ80, 0x2000, 0x3fff, (UINT32)Pico.zram); // mirror
\r
381 Cz80_Set_ReadB(&CZ80, (UINT8 (*)(UINT32 address))z80_read); // unused (hacked in)
\r
382 Cz80_Set_WriteB(&CZ80, z80_write);
\r
383 Cz80_Set_INPort(&CZ80, z80_in);
\r
384 Cz80_Set_OUTPort(&CZ80, z80_out);
\r
388 PICO_INTERNAL void z80_reset(void)
\r
390 #if defined(_USE_MZ80)
\r
392 #elif defined(_USE_DRZ80)
\r
393 memset(&drZ80, 0, 0x54);
\r
394 drZ80.Z80F = (1<<2); // set ZFlag
\r
395 drZ80.Z80F2 = (1<<2); // set ZFlag
\r
396 drZ80.Z80IX = 0xFFFF << 16;
\r
397 drZ80.Z80IY = 0xFFFF << 16;
\r
398 drZ80.Z80IM = 0; // 1?
\r
399 drZ80.Z80PC = drZ80.z80_rebasePC(0);
\r
400 drZ80.Z80SP = drZ80.z80_rebaseSP(0x2000); // 0xf000 ?
\r
401 #elif defined(_USE_CZ80)
\r
403 Cz80_Set_Reg(&CZ80, CZ80_IX, 0xffff);
\r
404 Cz80_Set_Reg(&CZ80, CZ80_IY, 0xffff);
\r
405 Cz80_Set_Reg(&CZ80, CZ80_SP, 0x2000);
\r
407 Pico.m.z80_fakeval = 0; // for faking when Z80 is disabled
\r
411 PICO_INTERNAL void z80_pack(unsigned char *data)
\r
413 #if defined(_USE_MZ80)
\r
414 struct mz80context mz80;
\r
415 *(int *)data = 0x00005A6D; // "mZ"
\r
416 mz80GetContext(&mz80);
\r
417 memcpy(data+4, &mz80.z80clockticks, sizeof(mz80)-5*4); // don't save base&memhandlers
\r
418 #elif defined(_USE_DRZ80)
\r
419 *(int *)data = 0x015A7244; // "DrZ" v1
\r
420 drZ80.Z80PC = drZ80.z80_rebasePC(drZ80.Z80PC-drZ80.Z80PC_BASE);
\r
421 drZ80.Z80SP = drZ80.z80_rebaseSP(drZ80.Z80SP-drZ80.Z80SP_BASE);
\r
422 memcpy(data+4, &drZ80, 0x54);
\r
423 #elif defined(_USE_CZ80)
\r
424 *(int *)data = 0x00007a43; // "Cz"
\r
425 *(int *)(data+4) = Cz80_Get_Reg(&CZ80, CZ80_PC);
\r
426 memcpy(data+8, &CZ80, (INT32)&CZ80.BasePC - (INT32)&CZ80);
\r
430 PICO_INTERNAL void z80_unpack(unsigned char *data)
\r
432 #if defined(_USE_MZ80)
\r
433 if (*(int *)data == 0x00005A6D) { // "mZ" save?
\r
434 struct mz80context mz80;
\r
435 mz80GetContext(&mz80);
\r
436 memcpy(&mz80.z80clockticks, data+4, sizeof(mz80)-5*4);
\r
437 mz80SetContext(&mz80);
\r
442 #elif defined(_USE_DRZ80)
\r
443 if (*(int *)data == 0x015A7244) { // "DrZ" v1 save?
\r
444 memcpy(&drZ80, data+4, 0x54);
\r
446 drZ80.Z80PC = drZ80.z80_rebasePC(drZ80.Z80PC-drZ80.Z80PC_BASE);
\r
447 drZ80.Z80SP = drZ80.z80_rebaseSP(drZ80.Z80SP-drZ80.Z80SP_BASE);
\r
451 z80_int(); // try to goto int handler, maybe we won't execute trash there?
\r
453 #elif defined(_USE_CZ80)
\r
454 if (*(int *)data == 0x00007a43) { // "Cz" save?
\r
455 memcpy(&CZ80, data+8, (INT32)&CZ80.BasePC - (INT32)&CZ80);
\r
456 Cz80_Set_Reg(&CZ80, CZ80_PC, *(int *)(data+4));
\r
464 PICO_INTERNAL void z80_exit(void)
\r
466 #if defined(_USE_MZ80)
\r
471 #if 1 // defined(__DEBUG_PRINT) || defined(__GP2X__) || defined(__GIZ__)
\r
472 PICO_INTERNAL void z80_debug(char *dstr)
\r
474 #if defined(_USE_DRZ80)
\r
475 sprintf(dstr, "Z80 state: PC: %04x SP: %04x\n", drZ80.Z80PC-drZ80.Z80PC_BASE, drZ80.Z80SP-drZ80.Z80SP_BASE);
\r
476 #elif defined(_USE_CZ80)
\r
477 sprintf(dstr, "Z80 state: PC: %04x SP: %04x\n", CZ80.PC - CZ80.BasePC, CZ80.SP.W);
\r