Skip to content

The desktop ​

The desktop build is your game as an ordinary program for the PC: the same C code, compiled by the PC's C compiler against Rachael, the SNES rebuilt in portable C. The picture is the SNES's, pixel for pixel, and Rachael counts what the console would lose, so most of a game can be written, played and tested here. What it does not give is the console's time: the game's code runs at the PC's speed, however long a tick would take on the SNES.

Building for the desktop ​

Use superdevkit build linux in the game's folder to build it for Linux, here in a copy of examples/hello made with superdevkit init hello:

sh
superdevkit build linux

It writes the program to build/linux/hello, named after the name in the game's project file, and prints built build/linux/hello. Before the C compiler runs, the build runs the commands the project file names under [build] before, then converts the game's pictures and sound effects into C in build/gen (see Graphics and sound). It needs a C compiler (cc, or the one CC names: gcc or clang) and the X11 and OpenGL libraries the program links with. raylib, the library behind the window and the sound, comes ready-built in version 5.5. A release zip of superdevkit already holds it for its own system, in raylib/linux of the superdevkit home. In a home built from the repository, the first build fetches it from raylib's releases on GitHub into that folder, and later builds reuse it.

The build has three steps, and the first two are there to keep the game honest:

  1. The game alone. The game's sources are compiled as on the SNES, without the host's headers (-nostdinc, -ffreestanding). The only include folders are dampf's (<stdint.h>, <string.h>, <dampf.h>), the kit's public headers in lib/include, the game's own include folders and build/gen. A game that includes <stdio.h> fails here. The macros the project file defines under [desktop] are set for these files.
  2. The symbol check. superdevkit reads what the game's object files leave undefined and accepts only the functions and variables declared in lib/include/sdevkit, what the game defines itself, and a few that the C compiler calls on its own (memcpy, memset, memmove and the stack and buffer checks). It also looks in them for the game's interrupt handlers and writes build/linux/obj/game_interrupts.c, which hands them to Rachael and the frontend: game_irq, which a game defines to change the picture from a row on (sdevkit/irq.h) and which Rachael calls just before that row while she draws, and the NMI handler that runs the vertical blank of a game that publishes its data for interrupt code (SDEVKIT_PUBLISH, sdevkit/publish.h). C has no symbol that may be missing and that gcc, MSVC and emcc all accept, so the build looks for them instead.
  3. The link with Rachael (lib/rachael), the desktop frontend (lib/desktop) and raylib.

The symbol check exists because Rachael can only count what goes through the console API. A function of raylib or the C library declared by hand, or a call into Rachael's internals, would work on the PC and have no counterpart on the SNES, so the check rejects it and names the symbol and the object. No header the game can include declares rand, so a game that declares it itself and calls it in src/main.c gets:

error: check: src_main.o: uses rand, which is not part of superdevkit's console API (lib/include/sdevkit, ADR-6F3CK2AR)

Use superdevkit run to build the game and start it in one go. It builds for the target of the PC it runs on (linux, also under WSL; win64 on Windows), starts the program in the game's folder and waits until you close the window. superdevkit build linux --plan lists what the build would run without running it; the command's options are on The superdevkit command.

For Windows, run superdevkit build win64, on Windows or from WSL. It goes through the same three steps with Visual Studio's C compiler (MSVC, found with vswhere; it needs the C++ tools for x64) and raylib's MSVC package, and writes build/win64/hello.exe. The Windows release zip holds that package; from the Linux zip under WSL, the first win64 build fetches it into raylib/windows of the home. Without Visual Studio or those tools, the build stops and says to install Visual Studio with the workload "Desktop development with C++", or to build for the SNES, which needs none of this: superdevkit build snes. Started from Windows, from Explorer for example, the program plays with the keyboard and with the gamepads Windows knows: WSL sees no gamepad, since Windows keeps every input device. It can also be started from WSL, with superdevkit run win64 there. It is built as a Windows program without a console.

Playing ​

Start the program without options to play in a window, or let superdevkit run start it:

sh
build/linux/hello

The window opens at three times the SNES's 256 by 224 pixels; --scale N picks another factor. You can resize it: the picture fills it as far as its shape allows, centred, with black around it. Before the game, the splash screen plays, as on the SNES.

The keyboard and the first gamepad are joypad 1, at the same time:

SNESKeyboardGamepad
Up, down, left, rightarrow keysthe directional pad
BZthe bottom face button
AXthe right face button
YAthe left face button
XSthe top face button
LQthe left shoulder button
RWthe right shoulder button
StartEnterthe right middle button
Selectright Shift or Backspacethe left middle button

The window shows the picture through a retro TV filter that makes it look like a SNES on a CRT television. Press Tab to turn it off and on, or start with --no-crt to start without it.

Press F12 for a screenshot: hello_0.png, hello_1.png and so on, in the folder you started the program from. A screenshot is Rachael's picture without the filter.

The window plays the game's sound. The sound effects go through Rachael's DSP, the SNES's sound chip rebuilt in C, at 32 kHz in stereo. The music the game asks for with sdevkit_music_play plays from the track's file, which the build copies into build/linux/music/ next to the program, over and over until the game stops it or asks for another; it pauses when the game is paused. On the SNES, music is not built yet (see Limits). Start with --mute for a window without sound; the game and its checksums run the same. How a game gets its sounds is in Graphics and sound.

A game with save RAM (sdevkit_sram, sized by sram in its project file) keeps it in the file <game>.srm next to the program: build/linux/catchme.srm for examples/catchme, which keeps its high score there. The program reads the file before the game starts; without one, the save RAM holds zeros. It writes the file when the save RAM has changed, which it looks at every half second, and when the window closes. A check and a replay never touch the file: they start with the save RAM their file names.

When you close the window, the program prints to the error output what the SNES would have lost during the session (hello: N bytes would not have reached the PPU on the SNES, the PPU being the chip that draws the picture), the register writes Rachael does not draw and those made while the picture is drawn, the changes to the register shadow of sdevkit/shadow.h made without their mark, and the DSP writes she does not play.

All options, as the program prints them for an option it does not know or one that lacks its argument:

usage: build/linux/hello [--scale N] [--no-crt] [--mute] [--seed N] [--set name=value] [--replay recording.txt [--jump TICK]]
       build/linux/hello --check check.txt [--seed N] [--set name=value] [--out dir] [--record recording.txt] [--wav sound.wav]
                 [--view-shot view.png [--view-tab 0..3]]

The web build runs this same frontend in the browser, with the same keys for the game, though it has the debug view and its keys (F1, F5 to F8) only in a build made with --debug; see The web.

The check without a window ​

Use --check to play a game without a window for a fixed number of ticks, with the input of a file, and --out for the folder its files go to (by default the current one):

sh
mkdir -p build/check
build/linux/hello --check check.txt --out build/check

The folder must exist. The file has one instruction a line; # starts a comment:

LineMeaning
ticks Nplay N ticks (600 without the line)
press T1 T2 buttons...hold the buttons on joypad 1 from tick T1 to tick T2 - 1
shot Ta screenshot after T ticks
splash Na screenshot of frame N of the splash screen
sram Hthe save RAM at power-on, its bytes in hex, the rest zero (all zero without the line)
seed Nthe seed game_init gets (0 without the line)
set VALUE namea value for what the game registered with the debug view, before the first tick

The buttons are b, y, select, start, up, down, left, right, a, x, l and r. On the desktop every frame is one tick; the splash screen plays before the first tick and gets no input. The check of examples/hello:

examples/hello/check.txt
ticks 400
splash 1
splash 16
splash 31
splash 120
splash 230
shot 30
press 60 160 right
press 160 260 down
shot 300

The check writes the screenshots into the --out folder as example_shot_30.png, example_splash_16.png and so on, unfiltered. These are the names the repository's check in Mesen (dev/example_check.lua) gives its screenshots too, so that the pictures of the two can be compared file by file. It also writes hashes.txt: the checksum of every tick, the tick and the checksum in hex on a line (0 98683870). The checksum covers the bytes the game names with sdevkit_dbg_hash in its tick and its game_vblank, and nothing else: the kit names nothing itself, so a game names what its next picture depends on, such as its sprite table and its positions. A game that names nothing gets the same checksum, 811c9dc5, on every tick, and the comparison tells nothing about it. Two runs that give the same checksums played the same game.

It also writes the sound there, as sound.txt: every write to a register of the DSP, with init (made in game_init) or the tick after which it came, the register and the value in hex; after game_init's writes, the line aram H holds a checksum of what the game uploaded into the sound chip's memory. dampf's emulator writes the same file for the ROM, so the sound of the two builds can be compared. Add --wav file to write the DSP's output as a WAV file as well. A check plays no music. Then it prints one line:

frames 400 ticks 400 lost 0 undrawn 0 unheard 0 sprites 0 drawing 0 unmarked 0 refused 0 repeated 0

The exit status is 1 when a byte was lost (written outside the vertical blank, or more than one vertical blank can copy), when the game wrote a register Rachael does not draw, so that the SNES would show another picture, or when it wrote the DSP in a way Rachael does not play, so that the SNES would sound different. The line counts all three: lost the lost bytes, and each frame that lost some gets a line of its own before it (lost frame N: B bytes); undrawn the register writes Rachael does not draw and unheard the DSP writes she does not play.

Three more counts fail the check as well. drawing counts the register writes made while the picture is drawn, which the SNES would show from the line being drawn on; they belong in game_vblank. unmarked counts the changes to the optional register shadow (sdevkit/shadow.h) made without their mark, which the SNES does not commit. repeated is for a game that publishes its data for interrupt code (sdevkit/publish.h): the SNES runs such a game's vertical blank again while a tick overruns, so after each tick the check runs it a second time, counts every byte of the picture's state that the second run changes, and then puts Rachael back as she was. When any of these counts is not 0, a line before the last names the first write or byte:

hello: N register writes Rachael does not draw, the first $21XX = $YY (the SNES would show something else)
hello: N register writes while the picture is drawn, the first $21XX = $YY (the SNES would change the picture from the line being drawn on: write it in game_vblank)
hello: N changes of sdevkit_ppu without their mark, the first in group $XX (sdevkit/shadow.h: the SNES does not commit them)
hello: N bytes the vertical blank changed when it ran again on the same published data, the first a VRAM word I (sdevkit/publish.h: the SNES runs it again while a tick overruns, and would show something else)
hello: N DSP writes Rachael does not play, the first $XX = $YY (docs/sound.md: what Rachael's DSP plays)

Two counts do not fail the check. sprites counts the lines of the picture with more sprites or sprite tiles than the SNES draws; a game may mean that, to make sprites flicker. refused counts the copies the optional upload queue (sdevkit/queue.h) turned down as over its budget, which the SNES turns down the same way. What each of these rules counts is explained in The SNES hardware. A file the program cannot read, an unknown button, or an sram line longer than the game's save RAM ends it with exit status 2.

Recordings and replays ​

Every session in the window is recorded: the joypad and the checksum of every tick. When you see something worth keeping, a bug or a moment to come back to, press F11. The program puts a mark at that tick and saves the session so far as <game>_rec_<n>.txt in the folder you started it from, with the first free number, and says so:

hello: tick 1234 flagged, the recording is hello_rec_0.txt

Each F11 writes a new file with the next free number, holding the session from its start and every mark so far. A recording is a check.txt: its ticks line, the save RAM the session started with as an sram line (unless it was all zero), seed and set lines if the session started with them, the joypad as a press line for each run of ticks with the same buttons held, and two more kinds of line, mark T for each mark and hash T H for the checksum of every tick. So one file plays in the window, in a check and, without seed and set lines, on the SNES side, and from the same save RAM.

Use --replay to play a recording in the window:

sh
build/linux/hello --replay hello_rec_0.txt

It pauses at each mark and at the first tick whose checksum is not the recorded one, which means that from there the program plays another game than the one that recorded. After the last tick of the recording the keyboard takes over, and the recording goes on, keeping the old marks. Four keys control the play, also without a replay, and the window's title shows the tick and the state:

  • F5 pauses and goes on.
  • F6 steps one tick while paused; Shift+F6 goes one tick back.
  • F7 runs eight ticks a frame, and turns that off again.
  • F8 runs a tick every four frames (slow motion), and turns that off again.

A game's state cannot be rewound, since its variables are its own. So Shift+F6 starts the program again and plays up to the tick before, fast and without the splash screen, then pauses. A session goes into <game>_back.txt first, in the folder you started the program from, and from then on the program is a replay of that file, which leaves the .srm file alone. What the debug view changed is left out. The web build has no step back.

Add --jump to a replay to reach a late tick without watching the way there. It plays fast to that tick and pauses, past any marks on the way, and stops earlier only at a checksum that differs:

sh
build/linux/hello --replay hello_rec_0.txt --jump 1200

A recording also plays without a window: --check with a recording compares the checksums too, prints the first tick that differs and exits with status 1. Add --record file to a check to write the recording of its run.

To play a recording on the SNES side, build the game's ROM with superdevkit build snes and give dev/replay.sh of the superdevkit home the ROM and the recording:

sh
superdevkit build snes
<home>/dev/replay.sh build/snes/hello.sfc hello_rec_0.txt

It plays the ROM in dampf's own emulator with the recording's joypad and compares the checksum of every tick with the recorded one:

400 ticks played, 400 checksums the same as the recording

Where a tick differs, it names the first one and exits with status 1:

tick 200 has checksum b184ea42 on the SNES, the recording deadbeef

That tick is where the SNES and the desktop part ways, the place to look for a difference between the two builds. The ROM must be built from the same source as the program that recorded the file; with any other change the checksums differ for that reason alone. It runs with a release's own Java in jre/, else the one JAVA_HOME names, else the one on the PATH. A recording with a seed or set line does not play there: the SNES starts with seed 0 and has no debug registrations, so the emulator refuses the file.

Name two more files after the recording to keep the ROM's checksums and DSP writes, in the formats of hashes.txt and sound.txt, and compare them with what the desktop's check wrote:

sh
<home>/dev/replay.sh build/snes/hello.sfc hello_rec_0.txt rom_hashes.txt rom_sound.txt

Debugging on the PC ​

Press F1 to open the debug view, a panel beside the picture. At its top are the tick and four buttons for the run, the same as F5 to F8: Pause (Go while paused), Step, Slow and Fast. Below them are four tabs:

  • Game: what the game registered to change or to show while it runs, a switch for the game's boxes over the picture, and at the bottom the lost bytes, undrawn register writes and unheard DSP writes so far.
  • Palettes: the 256 colours of CGRAM; a click on a row picks the palette the Tiles tab draws in.
  • Tiles: VRAM as tiles of 8 by 8 pixels with 4 bits a pixel, 16 a row, in that palette; the mouse wheel scrolls.
  • Sprites: the entries of the sprite table that are on the screen, with position, tile, palette, priority and size; the one under the mouse is outlined on the picture.

A game registers in game_init what it wants to change while it runs: sdevkit_dbg_toggle a flag of 0 or 1, sdevkit_dbg_range a number between two bounds, sdevkit_dbg_color a colour, sdevkit_dbg_action a button, which sets its variable to 1. Two more only show something and change nothing: sdevkit_dbg_text a line of text the game keeps up to date, sdevkit_dbg_value a 16-bit value, in decimal and hex. In each tick it can draw over the picture in one of eight colours: sdevkit_dbg_box a box, a collision box for instance, sdevkit_dbg_fill a filled box the picture shows through, and sdevkit_dbg_arrow an arrow. On the SNES these functions are empty and dampf removes the calls, so the game needs no #ifdef. In the following game the player moves left and right, and going past x = 200 sends it back to the start unless it is invincible. The view changes the player's speed, switches invincibility on and off and puts the player back to the start with a button; the player's box is green while it is invincible and red otherwise:

c
#include <stdint.h>
#include "sdevkit/loop.h"
#include "sdevkit/snes.h"

static int16_t player_x = 120;
static int16_t player_y = 100;
static int16_t player_speed = 2;
static uint8_t invincible;
static uint8_t reset_player;

void game_init(uint16_t seed)
{
    (void)seed;
    sdevkit_dbg_range("player speed", &player_speed, 0, 8);
    sdevkit_dbg_toggle("invincible", &invincible);
    sdevkit_dbg_action("reset player", &reset_player);
}

void game_tick(const uint16_t pad[SDEVKIT_PADS])
{
    if (pad[0] & SDEVKIT_KEY_RIGHT) {
        player_x += player_speed;
    }
    if (pad[0] & SDEVKIT_KEY_LEFT) {
        player_x -= player_speed;
    }
    if (player_x > 200 && !invincible) {
        reset_player = 1;           /* past 200: back to the start */
    }
    if (reset_player) {
        player_x = 120;
        player_y = 100;
        reset_player = 0;
    }
    sdevkit_dbg_box(player_x, player_y, 16, 16, invincible ? 1 : 0);
}

void game_vblank(void)
{
}

A change made in the view is not part of the recording, so from that tick on a replay plays another game. The program says so once on the error output, the window's title adds changed in the debug view, and a recording saved from then on notes it in a comment line:

# the debug view changed player speed before tick 812: from there a replay plays another game

To make such a change part of the recording, set it at the start with --set name=value, as often as you need; a name with a space goes in quotes. Add --seed N for the seed game_init gets, 0 without it. A recording keeps both, as seed and set lines, so its replay starts the same way:

sh
build/linux/hello --seed 7 --set "player speed=4" --set invincible=1

A name the game does not register, or a value outside its bounds, ends the program with exit status 2, and the message for an unknown name lists the ones it has.

With --check, --view-shot file.png draws the view once after the check into a PNG, in a hidden window, and --view-tab 0..3 picks the tab; it is there for tests and documentation.

The desktop build also carries debug information: the Linux build compiles with -g, and the Windows build writes build/win64/<game>.pdb next to the program. So the game's C runs under gdb or the Visual Studio debugger like any program of the PC, with breakpoints, steps and variables in your own source.

The build optimizes (-O2, /O2 on Windows), and at that level gdb shows some variables as <optimized out>. Add --debug to build without optimization (-O0, /Od), so that the debugger shows every variable; the picture and the checksums stay the same. superdevkit run takes the option as well:

sh
superdevkit build linux --debug

A check is a good way in: it opens no window and plays the same input every time, so a bug comes back at the same tick. Under gdb, with a breakpoint on the game's tick:

sh
mkdir -p build/check
gdb --args build/linux/hello --check check.txt --out build/check
(gdb) break game_tick
(gdb) run

A recording works the same way in place of check.txt: it plays the session up to the moment you pressed F11.

What the debugger shows is the game compiled by the PC's C compiler. The 65816 code dampf writes for the SNES is not there; to step through that, see Debugging in Mesen.

What the desktop does not tell you ​

Rachael is no emulator: it runs no 65816 code, and the game's code runs at the PC's speed. A tick that takes two frames on the SNES takes one on the desktop, and code too slow for the console is never too slow here. The window keeps 60 frames a second whatever the game does in a tick; only the check runs as fast as the PC can. The desktop finds what the SNES would lose or draw differently, but not whether a tick fits in a frame.

For the console's time, look at the timing report that dampf build writes next to the ROM (see Exact timing), and run the ROM in Mesen, as Debugging in Mesen shows.