2008-08-07 14:26:07 +02:00
/****************************************************************************
2009-04-15 17:33:51 +02:00
* main . c
2008-08-07 14:26:07 +02:00
*
2009-05-01 14:56:48 +02:00
* Genesis Plus GX
2008-12-11 18:38:29 +01:00
*
2009-05-13 16:26:55 +02:00
* Softdev ( 2006 )
2011-04-01 00:11:05 +02:00
* Eke - Eke ( 2007 - 2010 )
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
2008-12-11 18:38:29 +01:00
*
2008-08-07 14:26:07 +02:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-12-11 18:38:29 +01:00
2008-08-07 14:26:07 +02:00
# include "shared.h"
# include "font.h"
2009-05-19 18:14:43 +02:00
# include "gui.h"
2010-07-06 16:03:50 +02:00
# include "menu.h"
2009-04-15 17:33:51 +02:00
# include "aram.h"
2010-05-07 20:25:27 +02:00
# include "history.h"
2010-05-06 14:59:43 +02:00
# include "file_slot.h"
2010-11-01 19:13:17 +01:00
# include "file_load.h"
2010-05-07 20:25:27 +02:00
# include "filesel.h"
2010-11-01 19:13:17 +01:00
# include "cheats.h"
2008-08-07 14:26:07 +02:00
2008-12-10 19:16:30 +01:00
# include <fat.h>
2008-11-13 08:24:30 +01:00
# ifdef HW_RVL
2009-04-15 17:33:51 +02:00
# include <wiiuse/wpad.h>
2009-04-21 03:05:56 +02:00
# endif
2009-04-15 17:33:51 +02:00
2010-12-04 18:16:32 +01:00
/* audio "exact" samplerate, measured on real hardware */
# ifdef HW_RVL
# define SAMPLERATE_48KHZ 48000
# else
# define SAMPLERATE_48KHZ 48044
# endif
2010-01-27 14:12:06 +01:00
u32 Shutdown = 0 ;
2010-05-07 20:25:27 +02:00
u32 ConfigRequested = 1 ;
2009-05-20 17:11:06 +02:00
2009-04-21 03:05:56 +02:00
# ifdef HW_RVL
2010-04-23 14:31:07 +02:00
/****************************************************************************
* Power Button callback
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2011-04-01 00:11:05 +02:00
static void PowerOff_cb ( void )
2008-11-13 08:24:30 +01:00
{
Shutdown = 1 ;
ConfigRequested = 1 ;
}
2009-04-24 01:24:40 +02:00
# endif
2008-12-07 20:31:50 +01:00
2011-04-01 00:11:05 +02:00
/****************************************************************************
* Reset Button callback
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static void Reset_cb ( void )
{
gen_reset ( 0 ) ;
}
2008-08-07 14:26:07 +02:00
/***************************************************************************
2008-12-12 16:38:04 +01:00
* Genesis Plus Virtual Machine
2008-08-07 14:26:07 +02:00
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-04-20 00:31:08 +02:00
static void load_bios ( void )
2008-08-07 14:26:07 +02:00
{
2010-08-08 20:09:37 +02:00
/* clear BIOS detection flag */
2010-06-14 10:05:45 +02:00
config . tmss & = ~ 2 ;
2008-08-07 14:26:07 +02:00
2008-12-12 16:38:04 +01:00
/* open BIOS file */
2009-07-30 09:15:54 +02:00
FILE * fp = fopen ( OS_ROM , " rb " ) ;
2008-08-07 14:26:07 +02:00
if ( fp = = NULL ) return ;
/* read file */
fread ( bios_rom , 1 , 0x800 , fp ) ;
fclose ( fp ) ;
2010-08-08 20:09:37 +02:00
/* check ROM file */
if ( ! strncmp ( ( char * ) ( bios_rom + 0x120 ) , " GENESIS OS " , 10 ) )
{
/* valid BIOS detected */
config . tmss | = 2 ;
}
2008-08-07 14:26:07 +02:00
}
2009-04-20 00:31:08 +02:00
static void init_machine ( void )
2008-08-07 14:26:07 +02:00
{
2010-11-01 19:13:17 +01:00
/* allocate cart.rom here (10 MBytes) */
2009-08-06 20:31:05 +02:00
cart . rom = memalign ( 32 , MAXROMSIZE ) ;
if ( ! cart . rom )
2009-04-15 17:33:51 +02:00
{
2009-05-19 18:14:43 +02:00
FONT_writeCenter ( " Failed to allocate ROM buffer... Rebooting " , 18 , 0 , 640 , 200 , ( GXColor ) WHITE ) ;
gxSetScreen ( ) ;
sleep ( 2 ) ;
2009-04-24 01:24:40 +02:00
gx_audio_Shutdown ( ) ;
gx_video_Shutdown ( ) ;
2009-04-15 17:33:51 +02:00
# ifdef HW_RVL
DI_Close ( ) ;
SYS_ResetSystem ( SYS_RESTART , 0 , 0 ) ;
# else
SYS_ResetSystem ( SYS_HOTRESET , 0 , 0 ) ;
# endif
}
2008-08-07 14:26:07 +02:00
2008-12-12 16:38:04 +01:00
/* BIOS support */
load_bios ( ) ;
2008-08-07 14:26:07 +02:00
/* allocate global work bitmap */
memset ( & bitmap , 0 , sizeof ( bitmap ) ) ;
2008-12-12 16:38:04 +01:00
bitmap . width = 720 ;
2008-08-07 14:26:07 +02:00
bitmap . height = 576 ;
bitmap . depth = 16 ;
bitmap . granularity = 2 ;
bitmap . pitch = bitmap . width * bitmap . granularity ;
bitmap . viewport . w = 256 ;
bitmap . viewport . h = 224 ;
bitmap . viewport . x = 0 ;
bitmap . viewport . y = 0 ;
2008-12-04 20:32:22 +01:00
bitmap . data = texturemem ;
2008-08-07 14:26:07 +02:00
}
2010-05-28 14:08:00 +02:00
static void run_emulation ( void )
{
/* main emulation loop */
while ( 1 )
{
/* Main Menu request */
if ( ConfigRequested )
{
/* stop video & audio */
gx_audio_Stop ( ) ;
gx_video_Stop ( ) ;
/* show menu */
2010-07-06 16:03:50 +02:00
menu_execute ( ) ;
2010-05-28 14:08:00 +02:00
ConfigRequested = 0 ;
/* start video & audio */
gx_video_Start ( ) ;
2010-08-08 20:09:37 +02:00
gx_audio_Start ( ) ;
2010-05-28 14:08:00 +02:00
frameticker = 1 ;
}
/* automatic frame skipping */
if ( frameticker > 1 )
{
/* skip frame */
system_frame ( 1 ) ;
2010-08-08 20:09:37 +02:00
frameticker = 1 ;
2010-05-28 14:08:00 +02:00
}
else
{
/* render frame */
frameticker = 0 ;
system_frame ( 0 ) ;
/* update video */
gx_video_Update ( ) ;
}
/* update audio */
gx_audio_Update ( ) ;
2010-08-08 20:09:37 +02:00
/* check interlaced mode change */
if ( bitmap . viewport . changed & 4 )
{
2011-04-28 02:09:40 +02:00
/* VSYNC "original" mode */
if ( ! config . render & & ( gc_pal = = vdp_pal ) )
2010-08-08 20:09:37 +02:00
{
u8 * temp = memalign ( 32 , YM2612GetContextSize ( ) ) ;
if ( temp )
{
/* save YM2612 context */
memcpy ( temp , YM2612GetContextPtr ( ) , YM2612GetContextSize ( ) ) ;
/* framerate has changed, reinitialize audio timings */
2011-04-28 02:09:40 +02:00
if ( vdp_pal )
{
audio_init ( SAMPLERATE_48KHZ , interlaced ? 50.00 : ( 1000000.0 / 19967.5 ) ) ;
}
else
{
audio_init ( SAMPLERATE_48KHZ , interlaced ? 59.94 : ( 1000000.0 / 16715.0 ) ) ;
}
/* reinitialize sound chip emulation */
2010-08-08 20:09:37 +02:00
sound_init ( ) ;
/* restore YM2612 context */
YM2612Restore ( temp ) ;
free ( temp ) ;
}
}
/* clear flag */
bitmap . viewport . changed & = ~ 4 ;
}
2010-05-28 14:08:00 +02:00
/* wait for next frame */
2010-11-01 19:13:17 +01:00
while ( frameticker < 1 ) usleep ( 1 ) ;
2010-05-28 14:08:00 +02:00
}
}
2008-08-07 14:26:07 +02:00
/**************************************************
Load a new rom and performs some initialization
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-04-20 00:31:08 +02:00
void reloadrom ( int size , char * name )
2008-08-07 14:26:07 +02:00
{
2010-11-01 19:13:17 +01:00
/* hot-swap previous & current cartridge */
bool hotswap = config . hot_swap & & cart . romsize ;
2009-10-16 13:32:55 +02:00
2011-04-01 00:11:05 +02:00
/* ROM size */
2009-08-06 20:31:05 +02:00
cart . romsize = size ;
2011-04-01 00:11:05 +02:00
/* load ROM file */
2009-10-16 13:32:55 +02:00
load_rom ( name ) ;
2011-04-01 00:11:05 +02:00
/* ROM filename without extension*/
sprintf ( rom_filename , " %s " , name ) ;
2011-04-08 14:07:20 +02:00
int i = strlen ( rom_filename ) - 1 ;
2011-04-28 02:09:40 +02:00
while ( ( i > 0 ) & & ( rom_filename [ i ] ! = ' . ' ) ) i - - ;
if ( i > 0 ) rom_filename [ i ] = 0 ;
2011-04-01 00:11:05 +02:00
2009-10-16 13:32:55 +02:00
if ( hotswap )
{
2011-04-01 00:11:05 +02:00
if ( system_hw = = SYSTEM_PBC )
{
sms_cart_init ( ) ;
sms_cart_reset ( ) ;
}
else
{
md_cart_init ( ) ;
md_cart_reset ( 1 ) ;
}
2009-10-16 13:32:55 +02:00
}
else
{
2011-04-28 02:09:40 +02:00
/* Initialize audio emulation */
2011-04-01 00:11:05 +02:00
/* To prevent any sound skipping, sound chips must run at the exact same speed as the rest of emulation (see sound.c) */
2011-04-28 02:09:40 +02:00
/* When TV output mode matches emulated video mode, we use video hardware interrupt (VSYNC) and exact framerates for perfect synchronization */
/* In 60Hz TV modes, Wii & GC framerates have been measured to be 59.94 (interlaced or progressive) and ~59.825 fps (non-interlaced) */
/* In 50Hz TV modes, Wii & GC framerates have been measured to be 50.00 (interlaced) and ~50.845 fps (non-interlaced) */
/* When modes does not match, emulation is synchronized with audio hardware interrupt (DMA) and we use default framerates (50Hz for PAL, 60Hz for NTSC). */
if ( vdp_pal )
{
audio_init ( SAMPLERATE_48KHZ , ( config . tv_mode = = 0 ) ? 50.0 : ( config . render ? 50.00 : ( 1000000.0 / 19967.5 ) ) ) ;
}
else
{
audio_init ( SAMPLERATE_48KHZ , ( config . tv_mode = = 1 ) ? 60.0 : ( config . render ? 59.94 : ( 1000000.0 / 16715.0 ) ) ) ;
}
2010-11-01 19:13:17 +01:00
2011-04-01 00:11:05 +02:00
/* system power ON */
2010-01-24 12:41:53 +01:00
system_init ( ) ;
system_reset ( ) ;
2009-10-16 13:32:55 +02:00
}
2010-11-01 19:13:17 +01:00
/* load Cheats */
CheatLoad ( ) ;
/* load SRAM */
if ( config . s_auto & 1 )
{
slot_autoload ( 0 , config . s_device ) ;
}
/* load State */
if ( config . s_auto & 2 )
{
slot_autoload ( config . s_default , config . s_device ) ;
}
2008-08-07 14:26:07 +02:00
}
2009-04-20 00:31:08 +02:00
/**************************************************
Shutdown everything properly
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void shutdown ( void )
{
2010-11-01 19:13:17 +01:00
/* save current config */
2010-05-07 20:25:27 +02:00
config_save ( ) ;
2010-11-01 19:13:17 +01:00
/* save current game state */
2010-05-06 14:59:43 +02:00
if ( config . s_auto & 2 )
2010-08-19 15:25:07 +02:00
{
2010-05-06 14:59:43 +02:00
slot_autosave ( config . s_default , config . s_device ) ;
2010-08-19 15:25:07 +02:00
}
2010-11-01 19:13:17 +01:00
/* shutdown emulation */
2009-04-20 00:31:08 +02:00
system_shutdown ( ) ;
audio_shutdown ( ) ;
2009-08-06 20:31:05 +02:00
free ( cart . rom ) ;
2009-04-24 01:24:40 +02:00
gx_audio_Shutdown ( ) ;
gx_video_Shutdown ( ) ;
2009-04-20 00:31:08 +02:00
# ifdef HW_RVL
DI_Close ( ) ;
# endif
}
2008-08-07 14:26:07 +02:00
/***************************************************************************
* M A I N
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-01-14 17:32:35 +01:00
u32 frameticker = 0 ;
2008-08-07 14:26:07 +02:00
int main ( int argc , char * argv [ ] )
{
2011-04-01 00:11:05 +02:00
char pathname [ MAXPATHLEN ] ;
2010-12-04 18:16:32 +01:00
# ifdef HW_RVL
/* initialize DI interface */
DI_UseCache ( 0 ) ;
DI_Init ( ) ;
# endif
2010-04-23 14:31:07 +02:00
/* initialize video engine */
2009-04-24 01:24:40 +02:00
gx_video_Init ( ) ;
2010-04-23 14:31:07 +02:00
2008-11-13 08:24:30 +01:00
# ifdef HW_DOL
2010-12-04 18:16:32 +01:00
/* initialize DVD interface */
2008-08-07 14:26:07 +02:00
DVD_Init ( ) ;
# endif
2010-12-04 18:16:32 +01:00
/* initialize input engine */
gx_input_Init ( ) ;
2009-04-24 01:24:40 +02:00
/* initialize FAT devices */
2010-12-04 18:16:32 +01:00
int retry = 0 ;
int fatMounted = 0 ;
/* try to mount FAT devices during 3 seconds */
while ( ! fatMounted & & ( retry < 12 ) )
{
fatMounted = fatInitDefault ( ) ;
usleep ( 250000 ) ;
retry + + ;
}
if ( fatMounted )
2008-10-26 19:32:32 +01:00
{
2009-05-01 14:56:48 +02:00
/* base directory */
sprintf ( pathname , DEFAULT_PATH ) ;
2010-05-07 20:25:27 +02:00
DIR_ITER * dir = diropen ( pathname ) ;
2010-08-12 21:21:25 +02:00
if ( dir ) dirclose ( dir ) ;
else mkdir ( pathname , S_IRWXU ) ;
2009-05-01 14:56:48 +02:00
2010-04-23 14:31:07 +02:00
/* default SRAM & Savestate files directory */
2009-05-01 14:56:48 +02:00
sprintf ( pathname , " %s/saves " , DEFAULT_PATH ) ;
dir = diropen ( pathname ) ;
2010-08-12 21:21:25 +02:00
if ( dir ) dirclose ( dir ) ;
else mkdir ( pathname , S_IRWXU ) ;
2009-05-01 14:56:48 +02:00
2010-04-23 14:31:07 +02:00
/* default Snapshot files directory */
2009-05-01 14:56:48 +02:00
sprintf ( pathname , " %s/snaps " , DEFAULT_PATH ) ;
dir = diropen ( pathname ) ;
2010-08-12 21:21:25 +02:00
if ( dir ) dirclose ( dir ) ;
else mkdir ( pathname , S_IRWXU ) ;
2010-11-01 19:13:17 +01:00
/* default Cheat files directory */
sprintf ( pathname , " %s/cheats " , DEFAULT_PATH ) ;
dir = diropen ( pathname ) ;
if ( dir ) dirclose ( dir ) ;
else mkdir ( pathname , S_IRWXU ) ;
2008-10-26 19:32:32 +01:00
}
2008-10-17 20:35:28 +02:00
2010-11-01 19:13:17 +01:00
/* initialize sound engine */
2009-04-24 01:24:40 +02:00
gx_audio_Init ( ) ;
2009-04-21 03:05:56 +02:00
2010-04-23 14:31:07 +02:00
/* initialize genesis plus core */
2009-05-05 14:58:25 +02:00
legal ( ) ;
2009-05-01 14:56:48 +02:00
config_default ( ) ;
history_default ( ) ;
init_machine ( ) ;
2009-01-09 18:11:42 +01:00
2009-04-05 20:20:43 +02:00
/* run any injected rom */
2009-08-06 20:31:05 +02:00
if ( cart . romsize )
2008-08-07 14:26:07 +02:00
{
2010-05-07 20:25:27 +02:00
int size = cart . romsize ;
cart . romsize = 0 ;
ARAMFetch ( ( char * ) cart . rom , ( void * ) 0x8000 , size ) ;
reloadrom ( size , " INJECT.bin " ) ;
ConfigRequested = 0 ;
2009-04-24 01:24:40 +02:00
gx_video_Start ( ) ;
gx_audio_Start ( ) ;
2009-04-05 20:20:43 +02:00
frameticker = 1 ;
}
2010-05-07 20:25:27 +02:00
else if ( config . autoload )
2009-04-05 20:20:43 +02:00
{
2010-05-07 20:25:27 +02:00
SILENT = 1 ;
2010-11-01 19:13:17 +01:00
if ( OpenDirectory ( TYPE_RECENT ) )
2010-05-07 20:25:27 +02:00
{
2011-04-01 00:11:05 +02:00
int size = LoadFile ( cart . rom , 0 , pathname ) ;
2010-05-07 20:25:27 +02:00
if ( size )
{
2011-04-01 00:11:05 +02:00
reloadrom ( size , pathname ) ;
2010-05-07 20:25:27 +02:00
gx_video_Start ( ) ;
gx_audio_Start ( ) ;
frameticker = 1 ;
ConfigRequested = 0 ;
}
}
SILENT = 0 ;
2008-08-07 14:26:07 +02:00
}
2009-01-09 18:11:42 +01:00
2009-04-24 01:24:40 +02:00
# ifdef HW_RVL
2010-11-01 19:13:17 +01:00
/* power button callback */
2011-04-01 00:11:05 +02:00
SYS_SetPowerCallback ( PowerOff_cb ) ;
2009-04-24 01:24:40 +02:00
# endif
2011-04-01 00:11:05 +02:00
/* reset button callback */
SYS_SetResetCallback ( Reset_cb ) ;
2009-01-14 17:32:35 +01:00
/* main emulation loop */
2010-05-28 14:08:00 +02:00
run_emulation ( ) ;
2009-01-06 18:15:28 +01:00
2010-11-01 19:13:17 +01:00
/* we should never return anyway */
2008-08-07 14:26:07 +02:00
return 0 ;
}