RetroArch/ssnes.c

333 lines
8.0 KiB
C
Raw Normal View History

2010-05-28 16:21:33 +00:00
/* SSNES - A Super Ninteno Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES 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 Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES 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 SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
2010-05-27 14:46:22 +00:00
#include <stdbool.h>
2010-05-26 19:27:37 +00:00
#include <GL/glfw.h>
2010-05-26 20:42:58 +00:00
#include <samplerate.h>
2010-05-28 16:07:04 +00:00
#include <libsnes.hpp>
2010-05-26 19:27:37 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <rsound.h>
#include <string.h>
#include "config.h"
2010-05-28 00:45:18 +00:00
#include "driver.h"
2010-05-26 19:27:37 +00:00
2010-05-28 00:45:18 +00:00
static bool video_active = true;
static bool audio_active = true;
2010-05-26 20:42:58 +00:00
static SRC_STATE* source = NULL;
2010-05-26 19:27:37 +00:00
2010-05-28 00:45:18 +00:00
//////////////////////////////////////////////// Backends
extern const audio_driver_t audio_rsound;
2010-05-28 12:33:18 +00:00
extern const audio_driver_t audio_oss;
2010-05-28 13:41:38 +00:00
extern const audio_driver_t audio_alsa;
2010-05-28 00:45:18 +00:00
extern const video_driver_t video_gl;
////////////////////////////////////////////////
2010-05-26 22:39:56 +00:00
2010-05-28 00:45:18 +00:00
static driver_t driver = {
2010-05-28 11:53:54 +00:00
#if VIDEO_DRIVER == VIDEO_GL
.video = &video_gl,
2010-05-28 16:21:33 +00:00
#else
#error "Define a video driver in config.h"
2010-05-28 11:53:54 +00:00
#endif
2010-05-28 16:21:33 +00:00
2010-05-28 11:53:54 +00:00
#if AUDIO_DRIVER == AUDIO_RSOUND
2010-05-28 12:33:18 +00:00
.audio = &audio_rsound,
#elif AUDIO_DRIVER == AUDIO_OSS
.audio = &audio_oss,
2010-05-28 13:41:38 +00:00
#elif AUDIO_DRIVER == AUDIO_ALSA
.audio = &audio_alsa,
2010-05-28 16:21:33 +00:00
#else
#error "Define an audio driver in config.h"
2010-05-28 11:53:54 +00:00
#endif
2010-05-28 00:45:18 +00:00
};
static void init_drivers(void);
static void uninit_drivers(void);
static void init_video_input(void);
static void uninit_video_input(void);
static void init_audio(void);
static void uninit_audio(void);
2010-05-26 22:39:56 +00:00
static void load_state(const char* path, uint8_t* data, size_t size);
static void write_state(const char* path, uint8_t* data, size_t size);
2010-05-26 19:27:37 +00:00
2010-05-28 00:45:18 +00:00
static void init_drivers(void)
2010-05-26 19:27:37 +00:00
{
2010-05-28 00:45:18 +00:00
init_video_input();
init_audio();
2010-05-26 19:27:37 +00:00
}
2010-05-28 00:45:18 +00:00
static void uninit_drivers(void)
2010-05-26 19:27:37 +00:00
{
2010-05-28 00:45:18 +00:00
uninit_video_input();
uninit_audio();
2010-05-26 19:27:37 +00:00
}
2010-05-28 00:45:18 +00:00
static void init_audio(void)
2010-05-26 19:27:37 +00:00
{
2010-05-28 00:45:18 +00:00
driver.audio_data = driver.audio->init(audio_device, out_rate, out_latency);
if ( driver.audio_data == NULL )
audio_active = false;
2010-05-26 19:27:37 +00:00
2010-05-28 00:45:18 +00:00
int err;
source = src_new(SRC_SINC_MEDIUM_QUALITY, 2, &err);
}
2010-05-26 19:27:37 +00:00
2010-05-28 00:45:18 +00:00
static void uninit_audio(void)
{
if ( driver.audio_data && driver.audio )
driver.audio->free(driver.audio_data);
2010-05-26 19:27:37 +00:00
2010-05-28 00:45:18 +00:00
if ( source )
src_delete(source);
2010-05-26 19:27:37 +00:00
}
2010-05-28 00:45:18 +00:00
static void init_video_input(void)
2010-05-26 19:27:37 +00:00
{
2010-05-29 12:45:40 +00:00
video_info_t video = {
.width = (fullscreen) ? fullscreen_x : (256 * xscale),
.height = (fullscreen) ? fullscreen_y : (224 * yscale),
.fullscreen = fullscreen,
.vsync = vsync,
.force_aspect = force_aspect,
.smooth = video_smooth
};
driver.video_data = driver.video->init(&video, (input_driver_t**)&(driver.input));
2010-05-26 20:42:58 +00:00
2010-05-28 00:45:18 +00:00
if ( driver.video_data == NULL )
2010-05-26 20:42:58 +00:00
{
exit(1);
}
2010-05-28 00:45:18 +00:00
if ( driver.input != NULL )
2010-05-26 20:42:58 +00:00
{
2010-05-28 00:45:18 +00:00
driver.input_data = driver.video_data;
2010-05-26 20:42:58 +00:00
}
2010-05-28 00:45:18 +00:00
else
2010-05-26 20:42:58 +00:00
{
2010-05-28 00:45:18 +00:00
driver.input_data = driver.input->init();
if ( driver.input_data == NULL )
exit(1);
2010-05-26 20:42:58 +00:00
}
}
2010-05-28 00:45:18 +00:00
static void uninit_video_input(void)
2010-05-26 19:27:37 +00:00
{
2010-05-28 00:45:18 +00:00
if ( driver.video_data && driver.video )
driver.video->free(driver.video_data);
2010-05-26 19:27:37 +00:00
2010-05-28 00:45:18 +00:00
if ( driver.input_data != driver.video_data && driver.input )
driver.input->free(driver.input_data);
}
2010-05-26 19:27:37 +00:00
2010-05-28 00:45:18 +00:00
static void video_frame(const uint16_t *data, unsigned width, unsigned height)
{
if ( !video_active )
return;
2010-05-26 19:27:37 +00:00
2010-05-28 00:45:18 +00:00
uint16_t output[width * height];
2010-05-26 19:27:37 +00:00
int y;
for ( y = 0; y < height; y++ )
{
const uint16_t *src = data + y * 1024;
2010-05-27 15:34:10 +00:00
uint16_t *dst = output + y * width;
2010-05-26 19:27:37 +00:00
2010-05-28 00:45:18 +00:00
memcpy(dst, src, width * sizeof(uint16_t));
2010-05-26 19:27:37 +00:00
}
2010-05-28 00:45:18 +00:00
if ( !driver.video->frame(driver.video_data, output, width, height) )
video_active = false;
2010-05-26 19:27:37 +00:00
}
2010-05-28 00:45:18 +00:00
static void audio_sample(uint16_t left, uint16_t right)
2010-05-26 19:27:37 +00:00
{
2010-05-28 00:45:18 +00:00
if ( !audio_active )
return;
2010-05-26 22:26:11 +00:00
static float data[256];
static int data_ptr = 0;
2010-05-26 20:42:58 +00:00
2010-05-26 22:26:11 +00:00
data[data_ptr++] = (float)(*(int16_t*)&left)/0x7FFF;
data[data_ptr++] = (float)(*(int16_t*)&right)/0x7FFF;
2010-05-26 19:27:37 +00:00
2010-05-26 22:26:11 +00:00
if ( data_ptr == 256 )
2010-05-26 20:42:58 +00:00
{
2010-05-26 22:26:11 +00:00
float outsamples[2048];
int16_t temp_outsamples[4096];
2010-05-26 20:42:58 +00:00
2010-05-26 22:26:11 +00:00
SRC_DATA src_data;
2010-05-26 20:42:58 +00:00
2010-05-26 22:26:11 +00:00
src_data.data_in = data;
src_data.data_out = outsamples;
src_data.input_frames = 128;
src_data.output_frames = 1024;
src_data.end_of_input = 0;
src_data.src_ratio = (double)out_rate / (double)in_rate;
2010-05-26 20:42:58 +00:00
2010-05-26 22:26:11 +00:00
src_process(source, &src_data);
2010-05-26 20:42:58 +00:00
2010-05-26 22:26:11 +00:00
src_float_to_short_array(outsamples, temp_outsamples, src_data.output_frames_gen * 4);
2010-05-26 20:42:58 +00:00
2010-05-28 00:45:18 +00:00
if ( driver.audio->write(driver.audio_data, temp_outsamples, src_data.output_frames_gen * 4) < 0 )
audio_active = false;
2010-05-26 20:42:58 +00:00
2010-05-28 00:45:18 +00:00
data_ptr = 0;
2010-05-26 19:27:37 +00:00
}
}
2010-05-28 00:45:18 +00:00
static void input_poll(void)
2010-05-26 19:27:37 +00:00
{
2010-05-28 00:45:18 +00:00
driver.input->poll(driver.input_data);
2010-05-26 19:27:37 +00:00
}
static int16_t input_state(bool port, unsigned device, unsigned index, unsigned id)
{
2010-05-28 00:54:20 +00:00
return driver.input->input_state(driver.input_data, snes_keybinds, port, device, index, id);
2010-05-26 19:27:37 +00:00
}
int main(int argc, char *argv[])
{
if ( argc != 2 )
{
fprintf(stderr, "Usage: %s file\n", argv[0]);
exit(1);
}
2010-05-29 13:21:30 +00:00
FILE *file = fopen(argv[1], "rb");
if ( file == NULL )
{
fprintf(stderr, "Could not open file: \"%s\"\n", argv[1]);
exit(1);
}
2010-05-26 19:27:37 +00:00
char savefile_name[strlen(argv[1]+5)];
strcpy(savefile_name, argv[1]);
strcat(savefile_name, ".sav");
2010-05-28 00:45:18 +00:00
init_drivers();
2010-05-26 19:27:37 +00:00
snes_init();
2010-05-28 00:45:18 +00:00
snes_set_video_refresh(video_frame);
snes_set_audio_sample(audio_sample);
snes_set_input_poll(input_poll);
2010-05-26 19:27:37 +00:00
snes_set_input_state(input_state);
fseek(file, 0, SEEK_END);
long length = ftell(file);
rewind(file);
if ((length & 0x7fff) == 512)
{
length -= 512;
fseek(file, 512, SEEK_SET);
}
void *rom_buf = malloc(length);
if ( rom_buf == NULL )
{
2010-05-28 12:33:18 +00:00
fprintf(stderr, "SSNES [ERROR] :: Couldn't allocate memory!\n");
2010-05-26 19:27:37 +00:00
exit(1);
}
if ( fread(rom_buf, 1, length, file) < length )
{
2010-05-28 12:33:18 +00:00
fprintf(stderr, "SSNES [ERROR] :: Didn't read whole file.\n");
2010-05-26 19:27:37 +00:00
exit(1);
}
fclose(file);
snes_load_cartridge_normal(NULL, rom_buf, length);
free(rom_buf);
unsigned serial_size = snes_serialize_size();
uint8_t *serial_data = malloc(serial_size);
snes_serialize(serial_data, serial_size);
2010-05-26 22:39:56 +00:00
load_state(savefile_name, serial_data, serial_size);
snes_reset();
2010-05-26 19:27:37 +00:00
2010-05-28 00:45:18 +00:00
///// TODO: Modular friendly!!!
2010-05-26 19:27:37 +00:00
for(;;)
{
int quitting = glfwGetKey(GLFW_KEY_ESC) || !glfwGetWindowParam(GLFW_OPENED);
if ( quitting )
break;
if ( glfwGetKey( SAVE_STATE_KEY ))
2010-05-26 22:39:56 +00:00
{
write_state(savefile_name, serial_data, serial_size);
}
else if ( glfwGetKey( LOAD_STATE_KEY ) )
2010-05-26 22:39:56 +00:00
load_state(savefile_name, serial_data, serial_size);
else if ( glfwGetKey( TOGGLE_FULLSCREEN ) )
{
fullscreen = !fullscreen;
2010-05-28 00:45:18 +00:00
uninit_drivers();
init_drivers();
}
2010-05-26 19:27:37 +00:00
snes_run();
}
2010-05-26 22:39:56 +00:00
write_state(savefile_name, serial_data, serial_size);
2010-05-26 19:27:37 +00:00
snes_unload();
snes_term();
2010-05-28 00:45:18 +00:00
uninit_drivers();
2010-05-26 19:27:37 +00:00
return 0;
}
2010-05-26 22:39:56 +00:00
static void write_state(const char* path, uint8_t* data, size_t size)
{
FILE *file = fopen(path, "wb");
if ( file != NULL )
{
2010-05-28 12:33:18 +00:00
//fprintf(stderr, "SSNES: Saving state. Size: %d bytes.\n", (int)size);
2010-05-26 22:39:56 +00:00
snes_serialize(data, size);
2010-05-27 14:46:22 +00:00
if ( fwrite(data, 1, size, file) != size )
fprintf(stderr, "SSNES [WARN]: Did not save state properly.");
2010-05-26 22:39:56 +00:00
fclose(file);
}
}
static void load_state(const char* path, uint8_t* data, size_t size)
{
FILE *file = fopen(path, "rb");
if ( file != NULL )
{
2010-05-28 12:33:18 +00:00
//fprintf(stderr, "SSNES: Loading state. Size: %d bytes.\n", (int)size);
2010-05-27 14:46:22 +00:00
if ( fread(data, 1, size, file) != size )
fprintf(stderr, "SSNES [WARN]: Did not load state properly.");
2010-05-26 22:39:56 +00:00
fclose(file);
snes_unserialize(data, size);
}
}