1 // This is part of Pico Library
\r
3 // (c) Copyright 2004 Dave, All rights reserved.
\r
4 // (c) Copyright 2006 notaz, All rights reserved.
\r
5 // Free for non-commercial use.
\r
7 // For commercial use, separate licencing terms must be obtained.
\r
13 #include "sn76496.h"
\r
16 #pragma warning (disable:4244)
\r
19 #if defined(_USE_MZ80)
\r
20 #include "../../cpu/mz80/mz80.h"
\r
21 #elif defined(_USE_DRZ80)
\r
22 #include "../../cpu/DrZ80/drz80.h"
\r
25 #include "../PicoInt.h"
\r
26 #include "../cd/pcm.h"
\r
29 // master int buffer to mix to
\r
30 static int PsndBuffer[2*44100/50];
\r
32 //int z80CycleAim = 0;
\r
36 unsigned short dac_info[312]; // pppppppp ppppllll, p - pos in buff, l - length to write for this sample
\r
40 int PsndLen=0; // number of mono samples, multiply by 2 for stereo
\r
41 int PsndLen_exc_add=0; // this is for non-integer sample counts per line, eg. 22050/60
\r
42 int PsndLen_exc_cnt=0;
\r
43 short *PsndOut=NULL; // PCM data buffer
\r
46 extern int *ym2612_dacen;
\r
47 extern INT32 *ym2612_dacout;
\r
48 void YM2612TimerHandler(int c,int cnt);
\r
51 extern int *sn76496_regs;
\r
54 static void dac_recalculate()
\r
56 int i, dac_cnt, pos, len, lines = Pico.m.pal ? 312 : 262, mid = Pico.m.pal ? 68 : 93;
\r
58 if(PsndLen <= lines) {
\r
64 for(i=226; i != 225; i++) {
\r
65 if (i >= lines) i = 0;
\r
73 dac_info[i] = (pos<<4)|len;
\r
79 for(i = 225; i != 224; i++) {
\r
80 if (i >= lines) i = 0;
\r
82 while(dac_cnt >= 0) {
\r
86 if (i == mid) // midpoint
\r
87 while(pos+len < PsndLen/2) {
\r
92 dac_info[i] = (pos<<4)|len;
\r
96 for(len = 0, i = pos; i < PsndLen; i++) len++;
\r
97 if (PsndLen_exc_add) len++;
\r
98 dac_info[224] = (pos<<4)|len;
\r
100 //for(i=len=0; i < lines; i++) {
\r
101 // printf("%03i : %03i : %i\n", i, dac_info[i]>>4, dac_info[i]&0xf);
\r
102 // len+=dac_info[i]&0xf;
\r
104 //printf("rate is %i, len %f\n", PsndRate, (double)PsndRate/(Pico.m.pal ? 50.0 : 60.0));
\r
105 //printf("len total: %i, last pos: %i\n", len, pos);
\r
112 extern int z80stopCycle;
\r
115 // also clear the internal registers+addr line
\r
116 ym2612_regs = YM2612GetRegs();
\r
117 memset(ym2612_regs, 0, 0x200+4);
\r
124 // to be called after changing sound rate or chips
\r
125 void sound_rerate(int preserve_state)
\r
127 unsigned int state[28];
\r
128 int target_fps = Pico.m.pal ? 50 : 60;
\r
130 // not all rates are supported in MCD mode due to mp3 decoder limitations
\r
132 if (PsndRate != 11025 && PsndRate != 22050 && PsndRate != 44100) PsndRate = 22050;
\r
133 PicoOpt |= 8; // force stereo
\r
136 if (preserve_state) {
\r
137 if ((PicoMCD & 1) && Pico_mcd->m.audio_track)
\r
138 Pico_mcd->m.audio_offset = mp3_get_offset();
\r
140 YM2612Init(Pico.m.pal ? OSC_PAL/7 : OSC_NTSC/7, PsndRate);
\r
141 if (preserve_state) {
\r
142 // feed it back it's own registers, just like after loading state
\r
143 YM2612PicoStateLoad();
\r
144 if ((PicoMCD & 1) && Pico_mcd->m.audio_track)
\r
145 mp3_start_play(Pico_mcd->TOC.Tracks[Pico_mcd->m.audio_track].F, Pico_mcd->m.audio_offset);
\r
148 if (preserve_state) memcpy(state, sn76496_regs, 28*4); // remember old state
\r
149 SN76496_init(Pico.m.pal ? OSC_PAL/15 : OSC_NTSC/15, PsndRate);
\r
150 if (preserve_state) memcpy(sn76496_regs, state, 28*4); // restore old state
\r
152 // calculate PsndLen
\r
153 PsndLen=PsndRate / target_fps;
\r
154 PsndLen_exc_add=((PsndRate - PsndLen*target_fps)<<16) / target_fps;
\r
157 // recalculate dac info
\r
161 pcm_set_rate(PsndRate);
\r
163 // clear all buffers
\r
164 memset32(PsndBuffer, 0, sizeof(PsndBuffer)/4);
\r
170 // This is called once per raster (aka line), but not necessarily for every line
\r
171 void sound_timers_and_dac(int raster)
\r
174 int do_dac = PsndOut && (PicoOpt&1) && *ym2612_dacen;
\r
175 // int do_pcm = PsndOut && (PicoMCD&1) && (PicoOpt&0x400);
\r
177 // Our raster lasts 63.61323/64.102564 microseconds (NTSC/PAL)
\r
180 if (!do_dac /*&& !do_pcm*/) return;
\r
182 pos=dac_info[raster], len=pos&0xf;
\r
188 short *d = PsndOut + pos*2;
\r
189 int dout = *ym2612_dacout;
\r
191 // some manual loop unrolling here :)
\r
199 short *d = PsndOut + pos;
\r
211 int *d = PsndBuffer;
\r
212 d += (PicoOpt&8) ? pos*2 : pos;
\r
213 pcm_update(d, len, 1);
\r
219 void sound_clear(void)
\r
222 if (PsndLen_exc_add) len++;
\r
223 if (PicoOpt & 8) memset32((int *) PsndOut, 0, len); // clear both channels at once
\r
224 else memset(PsndOut, 0, len<<1);
\r
228 int sound_render(int offset, int length)
\r
230 int buf32_updated = 0;
\r
231 int *buf32 = PsndBuffer+offset;
\r
232 int stereo = (PicoOpt & 8) >> 3;
\r
233 // emulating CD && PCM option enabled && PCM chip on && have enabled channels
\r
234 int do_pcm = (PicoMCD&1) && (PicoOpt&0x400) && (Pico_mcd->pcm.control & 0x80) && Pico_mcd->pcm.enabled;
\r
237 if (offset == 0) { // should happen once per frame
\r
238 // compensate for float part of PsndLen
\r
239 PsndLen_exc_cnt += PsndLen_exc_add;
\r
240 if (PsndLen_exc_cnt >= 0x10000) {
\r
241 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 if ((PicoMCD & 1) && (PicoOpt & 0x800))
\r
266 mp3_update(buf32, length, stereo);
\r
268 // convert + limit to normal 16bit output
\r
270 mix_32_to_16l_stereo(PsndOut+offset, buf32, length);
\r
271 else mix_32_to_16_mono (PsndOut+offset, buf32, length);
\r
278 #if defined(_USE_MZ80)
\r
280 // memhandlers for mz80 core
\r
281 unsigned char mz80_read(UINT32 a, struct MemoryReadByte *w) { return z80_read(a); }
\r
282 void mz80_write(UINT32 a, UINT8 d, struct MemoryWriteByte *w) { z80_write(d, a); }
\r
284 // structures for mz80 core
\r
285 static struct MemoryReadByte mz80_mem_read[]=
\r
287 {0x0000,0xffff,mz80_read},
\r
288 {(UINT32) -1,(UINT32) -1,NULL}
\r
290 static struct MemoryWriteByte mz80_mem_write[]=
\r
292 {0x0000,0xffff,mz80_write},
\r
293 {(UINT32) -1,(UINT32) -1,NULL}
\r
295 static struct z80PortRead mz80_io_read[] ={
\r
296 {(UINT16) -1,(UINT16) -1,NULL}
\r
298 static struct z80PortWrite mz80_io_write[]={
\r
299 {(UINT16) -1,(UINT16) -1,NULL}
\r
302 #elif defined(_USE_DRZ80)
\r
304 static struct DrZ80 drZ80;
\r
306 static unsigned int DrZ80_rebasePC(unsigned short a)
\r
308 drZ80.Z80PC_BASE = (unsigned int) Pico.zram;
\r
309 return drZ80.Z80PC_BASE + a;
\r
312 static unsigned int DrZ80_rebaseSP(unsigned short a)
\r
314 drZ80.Z80SP_BASE = (unsigned int) Pico.zram;
\r
315 return drZ80.Z80SP_BASE + a;
\r
318 static unsigned char DrZ80_in(unsigned short p)
\r
323 static void DrZ80_out(unsigned short p,unsigned char d)
\r
327 static void DrZ80_irq_callback()
\r
329 drZ80.Z80_IRQ = 0; // lower irq when accepted
\r
334 // z80 functionality wrappers
\r
337 #if defined(_USE_MZ80)
\r
338 struct mz80context z80;
\r
342 // Modify the default context
\r
343 mz80GetContext(&z80);
\r
345 // point mz80 stuff
\r
346 z80.z80Base=Pico.zram;
\r
347 z80.z80MemRead=mz80_mem_read;
\r
348 z80.z80MemWrite=mz80_mem_write;
\r
349 z80.z80IoRead=mz80_io_read;
\r
350 z80.z80IoWrite=mz80_io_write;
\r
352 mz80SetContext(&z80);
\r
354 #elif defined(_USE_DRZ80)
\r
356 memset(&drZ80, 0, sizeof(struct DrZ80));
\r
357 drZ80.z80_rebasePC=DrZ80_rebasePC;
\r
358 drZ80.z80_rebaseSP=DrZ80_rebaseSP;
\r
359 drZ80.z80_read8 =z80_read;
\r
360 drZ80.z80_read16 =z80_read16;
\r
361 drZ80.z80_write8 =z80_write;
\r
362 drZ80.z80_write16 =z80_write16;
\r
363 drZ80.z80_in =DrZ80_in;
\r
364 drZ80.z80_out =DrZ80_out;
\r
365 drZ80.z80_irq_callback=DrZ80_irq_callback;
\r
371 #if defined(_USE_MZ80)
\r
373 #elif defined(_USE_DRZ80)
\r
374 memset(&drZ80, 0, 0x54);
\r
375 drZ80.Z80F = (1<<2); // set ZFlag
\r
376 drZ80.Z80F2 = (1<<2); // set ZFlag
\r
377 drZ80.Z80IX = 0xFFFF << 16;
\r
378 drZ80.Z80IY = 0xFFFF << 16;
\r
379 drZ80.Z80IM = 0; // 1?
\r
380 drZ80.Z80PC = drZ80.z80_rebasePC(0);
\r
381 drZ80.Z80SP = drZ80.z80_rebaseSP(0x2000); // 0xf000 ?
\r
383 Pico.m.z80_fakeval = 0; // for faking when Z80 is disabled
\r
386 void z80_resetCycles()
\r
388 #if defined(_USE_MZ80)
\r
389 mz80GetElapsedTicks(1);
\r
395 #if defined(_USE_MZ80)
\r
397 #elif defined(_USE_DRZ80)
\r
398 drZ80.z80irqvector = 0xFF; // default IRQ vector RST opcode
\r
403 // returns number of cycles actually executed
\r
404 int z80_run(int cycles)
\r
406 #if defined(_USE_MZ80)
\r
407 int ticks_pre = mz80GetElapsedTicks(0);
\r
409 return mz80GetElapsedTicks(0) - ticks_pre;
\r
410 #elif defined(_USE_DRZ80)
\r
411 return cycles - DrZ80Run(&drZ80, cycles);
\r
417 void z80_pack(unsigned char *data)
\r
419 #if defined(_USE_MZ80)
\r
420 struct mz80context mz80;
\r
421 *(int *)data = 0x00005A6D; // "mZ"
\r
422 mz80GetContext(&mz80);
\r
423 memcpy(data+4, &mz80.z80clockticks, sizeof(mz80)-5*4); // don't save base&memhandlers
\r
424 #elif defined(_USE_DRZ80)
\r
425 *(int *)data = 0x015A7244; // "DrZ" v1
\r
426 drZ80.Z80PC = drZ80.z80_rebasePC(drZ80.Z80PC-drZ80.Z80PC_BASE);
\r
427 drZ80.Z80SP = drZ80.z80_rebaseSP(drZ80.Z80SP-drZ80.Z80SP_BASE);
\r
428 memcpy(data+4, &drZ80, 0x54);
\r
432 void z80_unpack(unsigned char *data)
\r
434 #if defined(_USE_MZ80)
\r
435 if(*(int *)data == 0x00005A6D) { // "mZ" save?
\r
436 struct mz80context mz80;
\r
437 mz80GetContext(&mz80);
\r
438 memcpy(&mz80.z80clockticks, data+4, sizeof(mz80)-5*4);
\r
439 mz80SetContext(&mz80);
\r
444 #elif defined(_USE_DRZ80)
\r
445 if(*(int *)data == 0x015A7244) { // "DrZ" v1 save?
\r
446 memcpy(&drZ80, data+4, 0x54);
\r
448 drZ80.Z80PC = drZ80.z80_rebasePC(drZ80.Z80PC-drZ80.Z80PC_BASE);
\r
449 drZ80.Z80SP = drZ80.z80_rebaseSP(drZ80.Z80SP-drZ80.Z80SP_BASE);
\r
453 z80_int(); // try to goto int handler, maybe we won't execute trash there?
\r
460 #if defined(_USE_MZ80)
\r
465 #if defined(__DEBUG_PRINT) || defined(WIN32)
\r
466 void z80_debug(char *dstr)
\r
468 #if defined(_USE_DRZ80)
\r
469 sprintf(dstr, "%sZ80 state: PC: %04x SP: %04x\n", dstr, drZ80.Z80PC-drZ80.Z80PC_BASE, drZ80.Z80SP-drZ80.Z80SP_BASE);
\r