2008-08-07 14:26:07 +02:00
|
|
|
/***************************************************************************************
|
2009-05-13 16:26:55 +02:00
|
|
|
* Genesis Plus
|
2008-08-07 14:26:07 +02:00
|
|
|
*
|
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Charles Mac Donald (original code)
|
2009-05-13 16:26:55 +02:00
|
|
|
* Eke-Eke (2007,2008,2009), additional code & fixes for the GCN/Wii port
|
2008-08-07 14:26:07 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
* Sound Hardware
|
|
|
|
****************************************************************************************/
|
2007-08-10 22:34:06 +02:00
|
|
|
|
|
|
|
#include "shared.h"
|
2009-07-22 19:25:41 +02:00
|
|
|
#include "Fir_Resampler.h"
|
2007-08-10 22:34:06 +02:00
|
|
|
|
2008-08-25 21:38:03 +02:00
|
|
|
/* cycle-accurate samples */
|
2009-01-28 17:43:15 +01:00
|
|
|
static int m68cycles_per_sample[2];
|
2008-08-14 17:41:17 +02:00
|
|
|
|
2009-07-22 19:25:41 +02:00
|
|
|
/* return the number of samples that should be retrieved */
|
|
|
|
static inline int fm_sample_cnt(uint8 z80)
|
2007-08-10 22:34:06 +02:00
|
|
|
{
|
2009-07-22 19:25:41 +02:00
|
|
|
if (z80) return ((((count_z80 + current_z80 - z80_ICount) * 15) / (m68cycles_per_sample[0] * 7)) - snd.fm.pos);
|
|
|
|
else return ((count_m68k / m68cycles_per_sample[0]) - snd.fm.pos);
|
2008-08-14 17:41:17 +02:00
|
|
|
}
|
|
|
|
|
2009-07-22 19:25:41 +02:00
|
|
|
static inline int psg_sample_cnt(uint8 z80)
|
2008-08-14 17:41:17 +02:00
|
|
|
{
|
2009-07-22 19:25:41 +02:00
|
|
|
if (z80) return ((((count_z80 + current_z80 - z80_ICount) * 15) / (m68cycles_per_sample[1] * 7)) - snd.psg.pos);
|
|
|
|
else return ((count_m68k / m68cycles_per_sample[1]) - snd.psg.pos);
|
2008-08-07 14:26:07 +02:00
|
|
|
}
|
2007-08-10 22:34:06 +02:00
|
|
|
|
2008-08-07 14:26:07 +02:00
|
|
|
/* update FM samples */
|
2009-07-22 19:25:41 +02:00
|
|
|
static inline void fm_update(int cnt)
|
2008-08-07 14:26:07 +02:00
|
|
|
{
|
2009-07-22 19:25:41 +02:00
|
|
|
if (cnt > 0)
|
2008-12-11 18:38:29 +01:00
|
|
|
{
|
2009-07-22 19:25:41 +02:00
|
|
|
YM2612Update(cnt);
|
|
|
|
snd.fm.pos += cnt;
|
2008-12-11 18:38:29 +01:00
|
|
|
}
|
2008-08-07 14:26:07 +02:00
|
|
|
}
|
2007-08-10 22:34:06 +02:00
|
|
|
|
2008-08-07 14:26:07 +02:00
|
|
|
/* update PSG samples */
|
2009-07-22 19:25:41 +02:00
|
|
|
static inline void psg_update(int cnt)
|
2008-08-07 14:26:07 +02:00
|
|
|
{
|
2009-07-22 19:25:41 +02:00
|
|
|
if (cnt > 0)
|
2008-12-11 18:38:29 +01:00
|
|
|
{
|
2009-07-22 19:25:41 +02:00
|
|
|
SN76489_Update(snd.psg.buffer + snd.psg.pos, cnt);
|
|
|
|
snd.psg.pos += cnt;
|
2008-12-11 18:38:29 +01:00
|
|
|
}
|
2007-08-10 22:34:06 +02:00
|
|
|
}
|
|
|
|
|
2008-08-07 14:26:07 +02:00
|
|
|
void sound_init(int rate)
|
2007-08-10 22:34:06 +02:00
|
|
|
{
|
2008-12-11 18:38:29 +01:00
|
|
|
double vclk = (vdp_pal ? (double)CLOCK_PAL : (double)CLOCK_NTSC) / 7.0; /* 68000 and YM2612 clock */
|
|
|
|
double zclk = (vdp_pal ? (double)CLOCK_PAL : (double)CLOCK_NTSC) / 15.0; /* Z80 and SN76489 clock */
|
2007-08-10 22:34:06 +02:00
|
|
|
|
2009-02-22 20:57:41 +01:00
|
|
|
/* cycle-accurate FM & PSG samples */
|
|
|
|
m68cycles_per_sample[0] = (int)(((double)m68cycles_per_line * (double)lines_per_frame * (double)vdp_rate / (double)rate) + 0.5);
|
|
|
|
m68cycles_per_sample[1] = m68cycles_per_sample[0];
|
2009-01-28 17:43:15 +01:00
|
|
|
|
2009-07-22 19:25:41 +02:00
|
|
|
/* YM2612 is emulated at its original frequency (VLCK/144) */
|
2009-02-25 17:53:28 +01:00
|
|
|
if (config.hq_fm)
|
2009-07-22 19:25:41 +02:00
|
|
|
{
|
2009-01-28 17:43:15 +01:00
|
|
|
m68cycles_per_sample[0] = 144;
|
2009-08-14 15:54:15 +02:00
|
|
|
|
|
|
|
/* "real" ratio is (vclk/144.0)/(rate) but this causes scratchy sound in Wii/GCN 50Hz video mode */
|
|
|
|
/* since "real" framerate is lower than 50 fps whereas PAL Wii/GCN framerate is higher than 50 fps */
|
|
|
|
/* it's better to directly use the ratio between generated & expected numbers of samples per frame */
|
|
|
|
Fir_Resampler_time_ratio((double)m68cycles_per_line * (double)lines_per_frame * (double)vdp_rate / 144.0 / (double)rate);
|
2009-07-22 19:25:41 +02:00
|
|
|
}
|
2008-08-07 14:26:07 +02:00
|
|
|
|
2008-12-11 18:38:29 +01:00
|
|
|
/* initialize sound chips */
|
2009-05-27 16:07:14 +02:00
|
|
|
SN76489_Init((int)zclk,rate);
|
|
|
|
YM2612Init((int)vclk,rate);
|
2007-08-10 22:34:06 +02:00
|
|
|
}
|
|
|
|
|
2009-02-22 20:57:41 +01:00
|
|
|
void sound_update(int fm_len, int psg_len)
|
2007-08-10 22:34:06 +02:00
|
|
|
{
|
2008-12-11 18:38:29 +01:00
|
|
|
/* update last samples (if needed) */
|
2009-07-22 19:25:41 +02:00
|
|
|
fm_update(fm_len - snd.fm.pos);
|
|
|
|
psg_update(psg_len - snd.psg.pos);
|
2008-08-07 14:26:07 +02:00
|
|
|
|
2008-08-14 17:41:17 +02:00
|
|
|
/* reset samples count */
|
2009-07-22 19:25:41 +02:00
|
|
|
snd.fm.pos = 0;
|
|
|
|
snd.psg.pos = 0;
|
2007-08-10 22:34:06 +02:00
|
|
|
}
|
|
|
|
|
2008-08-07 14:26:07 +02:00
|
|
|
/* write FM chip */
|
|
|
|
void fm_write(unsigned int cpu, unsigned int address, unsigned int data)
|
2007-08-10 22:34:06 +02:00
|
|
|
{
|
2009-07-22 19:25:41 +02:00
|
|
|
if (address & 1) fm_update(fm_sample_cnt(cpu));
|
2009-02-25 17:53:28 +01:00
|
|
|
YM2612Write(address, data);
|
2007-08-10 22:34:06 +02:00
|
|
|
}
|
|
|
|
|
2008-08-07 14:26:07 +02:00
|
|
|
/* read FM status */
|
|
|
|
unsigned int fm_read(unsigned int cpu, unsigned int address)
|
2007-08-10 22:34:06 +02:00
|
|
|
{
|
2009-07-22 19:25:41 +02:00
|
|
|
fm_update(fm_sample_cnt(cpu));
|
2009-02-25 17:53:28 +01:00
|
|
|
return YM2612Read();
|
2007-08-10 22:34:06 +02:00
|
|
|
}
|
|
|
|
|
2008-08-07 14:26:07 +02:00
|
|
|
|
|
|
|
/* PSG write */
|
|
|
|
void psg_write(unsigned int cpu, unsigned int data)
|
2007-08-10 22:34:06 +02:00
|
|
|
{
|
2009-07-22 19:25:41 +02:00
|
|
|
psg_update(psg_sample_cnt(cpu));
|
2009-05-27 16:07:14 +02:00
|
|
|
SN76489_Write(data);
|
2007-08-10 22:34:06 +02:00
|
|
|
}
|