Genesis-Plus-GX/source/system.c

490 lines
12 KiB
C
Raw Normal View History

2008-08-07 14:26:07 +02:00
/***************************************************************************************
* Genesis Plus
2008-08-07 14:26:07 +02:00
* Main Emulation
*
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Charles Mac Donald (original code)
* 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
*
****************************************************************************************/
2007-08-10 22:34:06 +02:00
#include "shared.h"
#include "Fir_Resampler.h"
2009-05-26 18:40:35 +02:00
#include "eq.h"
2007-08-10 22:34:06 +02:00
2008-08-07 14:26:07 +02:00
/* Global variables */
2007-08-10 22:34:06 +02:00
t_bitmap bitmap;
t_snd snd;
uint32 mcycles_vdp;
uint32 mcycles_z80;
uint32 mcycles_68k;
uint32 hint_68k;
2008-08-07 14:26:07 +02:00
uint8 system_hw;
2009-05-26 18:40:35 +02:00
/****************************************************************
* AUDIO equalizer
****************************************************************/
static EQSTATE eq;
2009-05-26 18:40:35 +02:00
void audio_set_equalizer(void)
{
init_3band_state(&eq,config.low_freq,config.high_freq,snd.sample_rate);
2009-05-26 18:40:35 +02:00
eq.lg = (double)(config.lg);
eq.mg = (double)(config.mg);
eq.hg = (double)(config.hg);
}
/****************************************************************
* AUDIO stream update & mixing
****************************************************************/
static int32 llp,rrp;
2009-08-11 10:18:46 +02:00
int audio_update (void)
{
int32 i, l, r;
int32 ll = llp;
int32 rr = rrp;
2009-08-11 10:18:46 +02:00
int psg_preamp = config.psg_preamp;
int fm_preamp = config.fm_preamp;
int filter = config.filter;
uint32 factora = (config.lp_range << 16) / 100;
uint32 factorb = 0x10000 - factora;
int32 *fm = snd.fm.buffer;
int16 *psg = snd.psg.buffer;
#ifdef NGC
int16 *sb = (int16 *) soundbuffer[mixbuffer];
#endif
/* get number of available samples */
int size = sound_update(mcycles_vdp);
/* return an aligned number of samples */
size &= ~7;
if (config.hq_fm)
{
/* resample into FM output buffer */
Fir_Resampler_read(fm,size);
#ifdef LOGSOUND
error("%d FM samples remaining\n",Fir_Resampler_written() >> 1);
#endif
}
else
{
/* adjust remaining samples in FM output buffer*/
snd.fm.pos -= (size << 1);
#ifdef LOGSOUND
error("%d FM samples remaining\n",(snd.fm.pos - snd.fm.buffer)>>1);
#endif
}
/* adjust remaining samples in PSG output buffer*/
snd.psg.pos -= size;
#ifdef LOGSOUND
error("%d PSG samples remaining\n",snd.psg.pos - snd.psg.buffer);
#endif
/* mix samples */
for (i = 0; i < size; i ++)
{
/* PSG samples (mono) */
l = r = (((*psg++) * psg_preamp) / 100);
/* FM samples (stereo) */
l += ((*fm++ * fm_preamp) / 100);
r += ((*fm++ * fm_preamp) / 100);
2009-05-26 18:40:35 +02:00
/* filtering */
if (filter & 1)
{
2009-05-26 18:40:35 +02:00
/* single-pole low-pass filter (6 dB/octave) */
ll = (ll>>16)*factora + l*factorb;
rr = (rr>>16)*factora + r*factorb;
2009-08-11 10:18:46 +02:00
l = ll >> 16;
r = rr >> 16;
}
2009-05-26 18:40:35 +02:00
else if (filter & 2)
{
/* 3 Band EQ */
l = do_3band(&eq,l);
r = do_3band(&eq,r);
}
/* clipping (16-bit samples) */
if (l > 32767)
l = 32767;
else if (l < -32768)
l = -32768;
if (r > 32767)
r = 32767;
else if (r < -32768)
r = -32768;
/* update sound buffer */
#ifndef NGC
snd.buffer[0][i] = r;
snd.buffer[1][i] = l;
#else
*sb++ = r;
*sb++ = l;
#endif
}
2009-08-11 10:18:46 +02:00
/* save filtered samples for next frame */
2009-08-11 10:18:46 +02:00
llp = ll;
rrp = rr;
/* keep remaining samples for next frame */
memcpy(snd.fm.buffer, fm, (snd.fm.pos - snd.fm.buffer) << 2);
memcpy(snd.psg.buffer, psg, (snd.psg.pos - snd.psg.buffer) << 1);
#ifdef LOGSOUND
error("%d samples returned\n\n",size);
#endif
return size;
}
/****************************************************************
* AUDIO System initialization
****************************************************************/
int audio_init (int samplerate, float framerate)
{
/* Shutdown first */
audio_shutdown();
/* Clear the sound data context */
memset(&snd, 0, sizeof (snd));
/* Default settings */
snd.sample_rate = samplerate;
snd.frame_rate = framerate;
/* Calculate the sound buffer size (for one frame) */
snd.buffer_size = (int)(samplerate / framerate) + 32;
#ifndef NGC
/* Output buffers */
snd.buffer[0] = (int16 *) malloc(snd.buffer_size * sizeof(int16));
snd.buffer[1] = (int16 *) malloc(snd.buffer_size * sizeof(int16));
if (!snd.buffer[0] || !snd.buffer[1])
return (-1);
#endif
/* SN76489 stream buffers */
#ifndef NGC
snd.psg.buffer = (int16 *) malloc(snd.buffer_size * sizeof(int16));
#else
snd.psg.buffer = (int16 *) memalign(32, snd.buffer_size * sizeof(int16));
#endif
if (!snd.psg.buffer)
return (-1);
/* YM2612 stream buffers */
#ifndef NGC
snd.fm.buffer = (int32 *) malloc(snd.buffer_size * sizeof(int32) * 2);
#else
snd.fm.buffer = (int32 *) memalign(32,snd.buffer_size * sizeof(int32) * 2);
#endif
if (!snd.fm.buffer)
return (-1);
/* Resampling buffer */
if (config.hq_fm)
{
if (!Fir_Resampler_initialize(4096))
return (-1);
}
/* Set audio enable flag */
snd.enabled = 1;
/* Reset audio */
audio_reset();
return (0);
}
/****************************************************************
* AUDIO System reset
****************************************************************/
void audio_reset(void)
{
/* Low-Pass filter */
llp = 0;
rrp = 0;
/* 3 band EQ */
audio_set_equalizer();
/* audio buffers */
Fir_Resampler_clear();
snd.psg.pos = snd.psg.buffer;
snd.fm.pos = snd.fm.buffer;
#ifndef NGC
if (snd.buffer[0])
memset (snd.buffer[0], 0, snd.buffer_size * sizeof(int16));
if (snd.buffer[1])
memset (snd.buffer[1], 0, snd.buffer_size * sizeof(int16));
#endif
if (snd.psg.buffer)
memset (snd.psg.buffer, 0, snd.buffer_size * sizeof(int16));
if (snd.fm.buffer)
memset (snd.fm.buffer, 0, snd.buffer_size * sizeof(int32) * 2);
}
/****************************************************************
* AUDIO System shutdown
****************************************************************/
void audio_shutdown(void)
{
/* Sound buffers */
#ifndef NGC
if (snd.buffer[0])
free(snd.buffer[0]);
if (snd.buffer[1])
free(snd.buffer[1]);
#endif
if (snd.fm.buffer)
free(snd.fm.buffer);
if (snd.psg.buffer)
free(snd.psg.buffer);
/* Resampling buffer */
Fir_Resampler_shutdown();
}
2007-08-10 22:34:06 +02:00
2008-08-07 14:26:07 +02:00
/****************************************************************
* Virtual Genesis initialization
****************************************************************/
2007-08-10 22:34:06 +02:00
void system_init (void)
{
/* Cartridge hardware */
cart_hw_init();
/* Genesis hardware */
gen_init();
io_init();
vdp_init();
render_init();
/* Sound Chips hardware */
sound_init();
2007-08-10 22:34:06 +02:00
}
2008-08-07 14:26:07 +02:00
/****************************************************************
* Virtual Genesis Hard Reset
2008-08-07 14:26:07 +02:00
****************************************************************/
2007-08-10 22:34:06 +02:00
void system_reset (void)
{
/* Cartridge Hardware */
2008-12-10 19:16:30 +01:00
cart_hw_reset();
/* Genesis hardware */
gen_reset(1);
io_reset();
vdp_reset();
render_reset();
2008-12-10 19:16:30 +01:00
/* Sound chips */
sound_reset();
/* Audio System */
audio_reset();
2007-08-10 22:34:06 +02:00
}
2008-08-07 14:26:07 +02:00
/****************************************************************
* Virtual Genesis shutdown
****************************************************************/
2007-08-10 22:34:06 +02:00
void system_shutdown (void)
{
2008-12-10 19:16:30 +01:00
gen_shutdown ();
vdp_shutdown ();
render_shutdown ();
2007-08-10 22:34:06 +02:00
}
2008-08-07 14:26:07 +02:00
/****************************************************************
* Virtual Genesis Frame emulation
****************************************************************/
2007-08-10 22:34:06 +02:00
int system_frame (int do_skip)
{
2008-12-10 19:16:30 +01:00
if (!gen_running)
2008-12-04 20:32:22 +01:00
{
osd_input_Update();
2008-12-04 20:32:22 +01:00
return 0;
2008-12-10 19:16:30 +01:00
}
2008-12-04 20:32:22 +01:00
/* update display settings */
int line;
int reset = resetline;
int vdp_height = bitmap.viewport.h;
int end_line = vdp_height + bitmap.viewport.y;
int start_line = lines_per_frame - bitmap.viewport.y;
2008-12-10 19:16:30 +01:00
int old_interlaced = interlaced;
interlaced = (reg[12] & 2) >> 1;
2008-12-10 19:16:30 +01:00
if (old_interlaced != interlaced)
2008-08-07 14:26:07 +02:00
{
bitmap.viewport.changed = 1;
im2_flag = ((reg[12] & 6) == 6);
2008-12-10 19:16:30 +01:00
odd_frame = 1;
}
2008-08-07 14:26:07 +02:00
odd_frame ^= 1;
2008-12-04 20:32:22 +01:00
/* clear VBLANK, DMA, FIFO FULL & field flags */
status &= 0xFEE5;
/* set FIFO EMPTY flag */
status |= 0x0200;
2008-12-10 19:16:30 +01:00
2008-12-05 17:26:57 +01:00
/* even/odd field flag (interlaced modes only) */
if (odd_frame && interlaced)
status |= 0x0010;
2007-08-10 22:34:06 +02:00
2008-12-10 19:16:30 +01:00
/* reload HCounter */
int h_counter = reg[10];
2007-08-10 22:34:06 +02:00
/* reset VDP FIFO */
fifo_write_cnt = 0;
fifo_lastwrite = 0;
/* reset line cycle count */
mcycles_vdp = 0;
2007-08-10 22:34:06 +02:00
/* process scanlines */
2008-12-10 19:16:30 +01:00
for (line = 0; line < lines_per_frame; line ++)
{
2008-12-05 17:26:57 +01:00
/* update VCounter */
2008-12-10 19:16:30 +01:00
v_counter = line;
2007-08-10 22:34:06 +02:00
2008-12-10 19:16:30 +01:00
/* update 6-Buttons or Menacer */
input_update();
2007-08-10 22:34:06 +02:00
/* 68k line cycle count */
hint_68k = mcycles_68k;
2007-08-10 22:34:06 +02:00
/* Soft Reset line */
2008-12-10 19:16:30 +01:00
if (line == reset)
{
/* Pro Action Replay (switch at "Trainer" position) */
if (config.lock_on == TYPE_AR)
datel_reset(0);
gen_reset(0);
}
2008-08-07 14:26:07 +02:00
/* update VDP DMA */
if (dma_length)
vdp_update_dma();
/* vertical blanking */
if (status & 8)
{
/* render overscan */
if (!do_skip && ((line < end_line) || (line >= start_line)))
render_line(line, 1);
/* clear any pending Z80 interrupt */
if (zirq)
{
zirq = 0;
z80_set_irq_line(0, CLEAR_LINE);
}
}
2008-12-05 17:26:57 +01:00
/* active display */
else
2008-12-10 19:16:30 +01:00
{
/* H Interrupt */
2008-12-05 17:26:57 +01:00
if(--h_counter < 0)
2008-12-10 19:16:30 +01:00
{
h_counter = reg[10];
hint_pending = 0x10;
if (reg[0] & 0x10)
irq_status = (irq_status & ~0x40) | 0x14;
2008-09-13 17:42:24 +02:00
}
2008-08-07 14:26:07 +02:00
/* end of active display */
2008-12-04 20:32:22 +01:00
if (line == vdp_height)
2008-12-10 19:16:30 +01:00
{
2008-12-04 20:32:22 +01:00
/* render overscan */
if (!do_skip && (line < end_line))
render_line(line, 1);
2008-12-04 20:32:22 +01:00
2008-12-05 17:26:57 +01:00
/* update inputs (doing this here fix Warriors of Eternal Sun) */
osd_input_Update();
2008-08-07 14:26:07 +02:00
2008-12-05 17:26:57 +01:00
/* set VBLANK flag */
status |= 0x08;
2008-08-07 14:26:07 +02:00
2008-12-05 17:26:57 +01:00
/* Z80 interrupt is 16ms period (one frame) and 64us length (one scanline) */
2008-12-10 19:16:30 +01:00
zirq = 1;
z80_set_irq_line(0, ASSERT_LINE);
2008-12-10 19:16:30 +01:00
/* delay between VINT flag & V Interrupt (Ex-Mutants, Tyrant) */
m68k_run(mcycles_vdp + 588);
2008-12-05 17:26:57 +01:00
status |= 0x80;
/* delay between VBLANK flag & V Interrupt (Dracula, OutRunners, VR Troopers) */
m68k_run(mcycles_vdp + 788);
if (zstate == 1)
z80_run(mcycles_vdp + 788);
else
mcycles_z80 = mcycles_vdp + 788;
/* V Interrupt */
vint_pending = 0x20;
if (reg[1] & 0x20)
irq_status = (irq_status & ~0x40) | 0x36;
2008-12-04 20:32:22 +01:00
}
else if (!do_skip)
{
/* sprites are processed during horizontal blanking */
if (reg[1] & 0x40)
parse_satb(0x80 + line);
/* render scanline */
2008-12-04 20:32:22 +01:00
render_line(line, 0);
2008-12-10 19:16:30 +01:00
}
2008-12-04 20:32:22 +01:00
}
2008-08-07 14:26:07 +02:00
2008-12-10 19:16:30 +01:00
/* process line */
m68k_run(mcycles_vdp + MCYCLES_PER_LINE);
if (zstate == 1)
z80_run(mcycles_vdp + MCYCLES_PER_LINE);
else
mcycles_z80 = mcycles_vdp + MCYCLES_PER_LINE;
2008-12-10 19:16:30 +01:00
/* SVP chip */
if (svp)
ssp1601_run(SVP_cycles);
/* update line cycle count */
mcycles_vdp += MCYCLES_PER_LINE;
2008-12-10 19:16:30 +01:00
}
/* adjust cpu cycle count for next frame */
mcycles_68k -= mcycles_vdp;
mcycles_z80 -= mcycles_vdp;
2008-12-10 19:16:30 +01:00
return gen_running;
2007-08-10 22:34:06 +02:00
}