This is brief description of the steps to port Snes9x 2010 (a fork of Snes9x) to the new platform. It describes what code you have to write and what functions exist that you can make use of. It also gives some insights as to how Snes9x actually works, although that will be subject of another document yet to be written.
A C++ compiler. For the most part Snes9x really isn't written in C++, it just uses the C++ compiler as a “better C” compiler. GCC is good for compiling Snes9x (http://gcc.gnu.org/).
A fast CPU. SNES emulation is very compute intensive; two, or sometimes three CPUs to emulate, an 8-channel 16-bit stereo sound digital signal processor with real-time sample decompression, filter and echo effects, two custom graphics processor chips that can produce transparency, scaling, rotation and window effects in 32768 colors, and finally hardware DMA all take their toll on the host CPU.
Enough RAM. Snes9x uses 8MB to load SNES ROM images and several MB for emulating sound, graphics, custom chips, and so on.
A 16-bit color (two bytes per pixel) or deeper display, at least 512*478 pixels in resolution. Pixel format conversion may be required before you place the rendered SNES screen on to the display.
Sound output requires spooling 16-bit stereo digital sound data to the host sound system. Some ports can use interrupts or callbacks from the sound system to know when more sound data is required, most other ports have to periodically poll the host sound system to see if more data is required; if it is then the sound mixing code is called to fill the sound buffer with SNES sound data, which then can be passed on to the host sound system. Sound data is generated as an array of shorts (int16
). Stereo sound data generates twice as many samples, with each channel's samples interleaved, first left's then right's.
For the user to be able to control and play SNES games, some form of input device is required, a joypad or keyboard, for example. The real SNES can have 2 eight-button digital joypads connected to it or 5 joypads when an optional multi-player adaptor is connected, although most games only require a single joypad. Access to all eight buttons and the direction pad, of course, are usually required by most games. Snes9x does emulate the multi-player adaptor hardware, if you were wondering, but its still up to you to provide the emulation of the individual joypads.
The real SNES also has a SNES mouse, Super Scope and Justifier (light-gun) available as optional extras. Snes9x can emulate all of these using some form of pointing device, usually the host system's mouse.
Some SNES game cartridges contains a small amount of extra RAM and a battery, so ROMs could save a player's progress through a game for games that takes many hours to play from start to finish. Snes9x simulates this S-RAM by saving the contents of the area of memory occupied by the S-RAM into a file then automatically restoring it again the next time the user plays the same game. If the hardware you're porting to doesn't have a storage media available then you could be in trouble.
Snes9x also implements freeze-game files which can record the state of the SNES hardware and RAM at a particular point in time and can restore it to that exact state at a later date - the result is that users can save a game at any point, not just at save-game or password points provided by the original game coders. Each freeze file is over 400k in size. To help save disk space, Snes9x can be compiled with zlib (http://www.zlib.net/), which is used to GZIP compress the freeze files, reducing the size to typically below 100k. zlib is also used to load GZIP or ZIP compressed ROM images.
RIGHTSHIFT_IS_SAR
Define this if your compiler uses shift right arithmetic for signed values. For example, GCC and Visual C++ use shift right arithmetic.
CPU_SHUTDOWN
This is a speed-up hack. When defined and if Settings.ShutdownMaster
is true
, Snes9x starts watching for when CPU is in a simply loop waiting for a known event to happen - like the end of the current scanline. If Snes9x spots CPU in such a loop, it simply skips the emulation of its instructions until the event happens. It can be a big win with lots of SNES games, but will break a small number of games. In this case, set Settings.ShutdownMaster
to false
before you load a ROM image. Note that this hack is forcibly disabled in some games. See Memory.ApplyROMFixes
function for the list of such games.
CORRECT_VRAM_READS
You must define this. It allows correct VRAM reads.
ZLIB / UNZIP_SUPPORT
Define these if you want to support GZIP/ZIP compressed ROM images and GZIP compressed freeze-game files.
ZSNES_FX / ZSNES_C4
Define these if your CPU is x86 compatible and you're going to use the ZSNES Super FX and C4 assembler codes.
ZLIB
UNZIP_SUPPORT
CPU_SHUTDOWN
RIGHTSHIFT_IS_SAR
CORRECT_VRAM_READS
You may need to edit port.h
to fit Snes9x to your system.
If the byte ordering of your system is least significant byte first, make sure LSB_FIRST
is defined, otherwise make sure it's not defined.
You'll need to make sure what pixel format your system uses for 16-bit colors (RGB565
, RGB555
, BGR565
or BGR555
), and if it's not RGB565
, define PIXEL_FORMAT
to it so that Snes9x will use it to render the SNES screen. For example, Windows uses RGB565
, Mac OS X uses RGB555
. If your system is 24 or 32-bit only, then don't define anything; instead write a conversion routine that will take a complete rendered 16-bit SNES screen in RGB565
format and convert to the format required to be displayed on your system.
port.h
also typedefs some types; uint8
for an unsigned 8-bit quantity, uint16
for an unsigned 16-bit quantity, uint32
for a 32-bit unsigned quantity and bool8
for a true
/false
type. Signed versions are also typedef'ed.
Read controls.h
, crosshair.h
, controls.txt
and control-inputs.txt
for details. This section is the minimal explanation to get the SNES controls workable.
The real SNES allows several different types of devices to be plugged into the game controller ports. The devices Snes9x emulates are a joypad, multi-player adaptor known as the Multi Player 5 or Multi Tap (allowing a further 4 joypads to be plugged in), a 2-button mouse, a light gun known as the Super Scope, and a light gun known as the Justifier.
In your initialization code, call S9xUnmapAllControl
function.
Map any IDs to each SNES controller's buttons and pointers. (ID 249-255 are reserved).
Typically, use S9xMapPointer
function for the pointer of the SNES mouse, Super Scope and Justifier, S9xMapButton
function for other buttons. Set poll
to false
for the joypad buttons, true
for the other buttons and pointers.
S9xMapButton(k1P_A_Button, s9xcommand_t cmd = S9xGetCommandT("Joypad1 A"), false);
In your main emulation loop, after S9xMainLoop
function is called, check your system's keyboard/joypad, and call S9xReportButton
function to report the states of the SNES joypad buttons to Snes9x.
void MyMainLoop (void)
{
S9xMainLoop();
MyReportButttons();
}
void MyReportButtons (void)
{
S9xReportButton(k1P_A_Button, (key_is_pressed ? true : false));
}
Call S9xSetController
function. It connects each input device to each SNES input port.
Here's typical controller settings that is used by the real SNES games:
Joypad
S9xSetController(0, CTL_JOYPAD, 0, 0, 0, 0);
S9xSetController(1, CTL_JOYPAD, 1, 0, 0, 0);
Mouse (port 1)
S9xSetController(0, CTL_MOUSE, 0, 0, 0, 0);
S9xSetController(1, CTL_JOYPAD, 1, 0, 0, 0);
Mouse (port 2)
S9xSetController(0, CTL_JOYPAD, 0, 0, 0, 0);
S9xSetController(1, CTL_MOUSE, 1, 0, 0, 0);
Super Scope
S9xSetController(0, CTL_JOYPAD, 0, 0, 0, 0);
S9xSetController(1, CTL_SUPERSCOPE, 0, 0, 0, 0);
Multi Player 5
S9xSetController(0, CTL_JOYPAD, 0, 0, 0, 0);
S9xSetController(1, CTL_MP5, 1, 2, 3, 4);
Justifier
S9xSetController(0, CTL_JOYPAD, 0, 0, 0, 0);
S9xSetController(1, CTL_JUSTIFIER, 0, 0, 0, 0);
Justifier (2 players)
S9xSetController(0, CTL_JOYPAD, 0, 0, 0, 0);
S9xSetController(1, CTL_JUSTIFIER, 1, 0, 0, 0);
bool8 Memory.Init (void)
Allocates and initializes several major lumps of memory, for example the SNES ROM and RAM arrays, tile cache arrays, etc. Returns false
if memory allocation fails.
void Memory.Deinit (void)
Deallocates the memory allocations made by Memory.Init
function.
bool8 S9xGraphicsInit (void)
Allocates and initializes several lookup tables used to speed up SNES graphics rendering. Call after you have initialized the GFX.Screen
and GFX.Pitch
values. Returns false
if memory allocation fails.
void S9xGraphicsDeinit (void)
Deallocates the memory allocations made by S9xGraphicsInit
function.
bool8 S9xInitAPU (void)
Allocates and initializes several arrays used by the sound CPU and sound generation code. Returns false
if memory allocation fails.
void S9xDeinitAPU (void)
Deallocates the allocations made by S9xInitAPU
function.
bool8 S9xInitSound (int buffer_ms, int lag_ms)
Allocates memory for mixing and queueing SNES sound data and does more sound code initialization. Before calling this function you must set up Settings.SoundPlaybackRate
, and Settings.SoundInputRate
(see section below).
buffer_ms
, given in milliseconds, is the memory buffer size for queueing sound data. lag_ms
is allowable latency between when a sample is queued and when it is pulled in S9xMixSamples
. Set lag_ms
to zero if you have your own latency handling code in your port.
void S9xReset (void)
Resets the SNES emulated hardware back to the state it was in at “switch-on” except the S-RAM area is preserved (“hardware reset”). The effect is it resets the current game back to the start. This function is automatically called by Memory.LoadROM
function.
void S9xSoftReset (void)
Similar to S9xReset
function, but “software reset” as you press the SNES reset button.
bool8 Memory.LoadROM (const char *filepath)
Attempts to load the specified ROM image filename into the emulated ROM area. There are many different SNES ROM image formats and the code attempts to auto-detect as many different types as it can and in a vast majority of the cases gets it right.
There are several ROM image options in the Settings
structure; allow the user to set them before calling Memory.LoadROM
function, or make sure they are all reset to default values before each call to Memory.LoadROM
function. See Settings.ForceXXX
in snes9x.h
.
bool8 Memory.LoadMultiCart (const char *cartApath, const char *cartBpath)
Attempts to load multiple ROM images into the emulated ROM area, for the multiple cartridge systems such as Sufami Turbo, Same Game, etc.
bool8 Memory.LoadSRAM (const char *filepath)
Call this function to load the associated S-RAM save file (if any). The filename should be based on the ROM image name to allow easy linkage. The current ports change the directory and the filename extension of the ROM filename to derive the S-RAM filename.
bool8 Memory.SaveSRAM (const char *filepath)
Call this function to save the emulated S-RAM area into a file so it can be restored again the next time the user wants to play the game. Remember to call this when just before the emulator exits or when the user has been playing a game and is about to load another one.
void S9xMainLoop (void)
The emulator main loop. Call this from your own main loop that calls this function (if a ROM image is loaded and the game is not paused), processes any pending host system events, then goes back around the loop again until the emulator exits. S9xMainLoop
function normally returns control to your main loop once every emulated frame, when it reaches the start of scan-line zero. However it may take a few frames when a huge memory transfer is being emulated. The function can return more often if the DEBUGGER
compile-time flag is defined and the CPU has hit a break point, or the DEBUG_MODE_FLAG
bit is set in CPU.Flags
or instruction single-stepping is enabled.
void S9xMixSamples (uint8 *buffer, int sample_count)
Call this function from your host sound system handling code to fill buffer
with ready-mixed SNES sound data. If 16-bit sound mode is chosen, then the buffer will be filled with an array of sample_count
int16
, otherwise an array of sample_count
uint8
. If stereo sound generation is selected the buffer is filled with the same number of samples, but in pairs, first a left channel sample followed by the right channel sample.
If there are less queued samples than you request by sample_count
, the function fills buffer
with silent sound data and returns false
. To avoid this shortage of queued samples, request larger buffer size when calling S9xInitSound
, and handle sound latency safely.
int S9xGetSampleCount (void)
Returns the number of sound samples available in the buffer for your configured playback settings.
void S9xSetSamplesAvailableCallback (void (*) samples_available (void *))
Call this function to set up a callback that is run when sound samples are made available. samples_available
is a function you provide that returns void
. If you choose to provide a callback, you must call the provided S9xFinalizeSamples
function inside it to actually buffer the samples. If you are using a callback-oriented sound API, it is recommended to set up a function that locks a common mutex during the calls to S9xFinalizeSamples
and S9xMixSamples
to prevent them from running at the same time and corrupting the sound buffer.
If you wish to disable a callback you have set up or need to temporarily shut down your sound system, you may pass NULL
for both arguments to revert to the built-in version.
bool8 S9xFreezeGame (const char *filepath)
Call this function to record the current SNES hardware state into a file, the file can be loaded back using S9xUnfreezeGame
function at a later date effectively restoring the current game to exact same spot. Call this function while you're processing any pending system events when S9xMainLoop
function has returned control to you in your main loop.
bool8 S9xUnfreezeGame (const char *filepath)
Restore the SNES hardware back to the exactly the state it was in when S9xFreezeGame
function was used to generate the file specified. You have to arrange the correct ROM is already loaded using Memory.LoadROM
function, an easy way to arrange this is to base freeze-game filenames on the ROM image name. The UNIX/Linux ports load freeze-game files when the user presses a function key, with the names romfilename.000 for F1, romfilename.001 for F2, etc. Games are frozen in the first place when the user presses Shift-function key. You could choose some other scheme.
See cheats.h
, cheats.cpp
and cheats2.cpp
to support the cheat feature.
bool8 S9xDeinitUpdate (int width, int height)
Called once a complete SNES screen has been rendered into the GFX.Screen
memory buffer, now is your chance to copy the SNES rendered screen to the host computer's screen memory. The problem is that you have to cope with different sized SNES rendered screens: 256*224, 256*239, 512*224, 512*239, 512*448 and 512*478.
void S9xMessage (int type, int number, const char *message)
When Snes9x wants to display an error, information or warning message, it calls this function. Check in messages.h
for the types and individual message numbers that Snes9x currently passes as parameters.
The idea is display the message string so the user can see it, but you choose not to display anything at all, or change the message based on the message number or message type.
Eventually all debug output will also go via this function, trace information already does.
const char *S9xGetFilename (const char *extension, enum s9x_getdirtype dirtype)
When Snes9x needs to know the name of the cheat/IPS file and so on, this function is called. Check extension
and dirtype
, and return the appropriate filename. The current ports return the ROM file path with the given extension.
const char *S9xGetDirectory (enum s9x_getdirtype dirtype)
Called when Snes9x wants to know the directory dirtype
.
const char *S9xChooseFilename (bool8 read_only)
If your port can match Snes9x's built-in LoadFreezeFile
/SaveFreezeFile
command (see controls.cpp
), you may choose to use this function. Otherwise return NULL
.
uint16 *GFX.Screen
A uint16
array pointer to (at least) 2*512*478 bytes buffer where Snes9x puts the rendered SNES screen. You should allocate the space by yourself. As well you can change the GFX.Screen
value after S9xDeinitUpdate
function is called so that double-buffering will be easy.
uint32 GFX.Pitch
Bytes per line (not pixels per line) of the GFX.Screen
buffer. Typically set it to 1024. When the SNES screen is 256 pixels width, last half 512 bytes per line are unused.
There are various switches in the Settings
structure. See snes9x.h
for details. At least the settings below are required for good emulation.
memset(&Settings, 0, sizeof(Settings));
Settings.FrameTimePAL = 20000;
Settings.FrameTimeNTSC = 16667;
Settings.SoundPlaybackRate = 32000;
Settings.SoundInputRate = 32000;
Settings.HDMATimingHack = 100;
Settings.BlockInvalidVRAMAccessMaster = true;
Settings.SoundInputRate
Adjusts the sound rate through resampling. For every Settings.SoundInputRate
samples generated by the SNES, Settings.SoundPlaybackRate
samples will be produced.
The sound generation rate on a SNES is directly proportional to the video output rate. Displays that synchronize with the vertical refresh but have a slightly lower refresh-rate than the emulated system can experience sound drop-outs. It may be beneficial to provide an option for users to configure Settings.SoundInputRate
to suit their own systems. Setting Settings.SoundInputRate
to a value that matches the actual output rate (i.e. 31977hz for 59.96hz) or lower will allow the users to eliminate crackling. A range of 31000hz to 33000hz should be inclusive enough for all displays. Use of this setting can eliminate sound discontinuity.