Sega Mega Drive Game Development Kit (SGDK)
repository·master·Indexed 24 days ago
https://github.com/stephane-d/sgdkA comprehensive C-based development kit for the Sega Mega Drive. It provides a development library, a resource compiler (rescomp) for graphics and music, and a GCC-based toolchain (m68k-elf) to build ROM images. The kit includes support for flash-save modules with a high-level save manager (saveman) for persistent storage on flash cartridges, as well as integration guides for Visual Studio Code via the Genesis-Code extension.
What's inside SGDK
- MiniMusic is a lightweight Z80 sound driver designed for the Sega Mega Drive. It is optimized for minimal footprint and runs entirely from Z80 RAM without accessing the cartridge slot. This makes it ideal for scenarios where you need to play sound simultaneously while performing cartridge Flash writes, or when strict memory constraints require a tiny driver.
Overview of SGDK
masterSGDK (Sega Mega Drive Game Development Kit) is a free development kit for creating software in the C language for the Sega Mega Drive.
Key components:
- Development Library: Includes C source code for Mega Drive development.
- Custom Tools: Used for compiling resources (graphics, music, etc.).
- Compiler: Uses GCC (
m68k-elftarget) andlibgccto generate ROM images.
Requirements:
- Java: Required for the custom resource tools.
- GCC Compiler: Binaries (GCC 13.2) are provided for Windows. For Linux or macOS, you must install the compiler yourself (see Linux/macOS section).
Overview of MegaWiFi API modules
masterThe MegaWiFi API is composed of several specialized modules:
tsk: A simple multitasking implementation providing one supervisor task and one user task.megawifi: The primary module for WiFi communications, including socket management (client/server), HTTP/HTTPS requests, AP association, and NTP time synchronization.mw-msg: Contains command message definitions. This is intended for advanced users who need to bypass themegawifimodule and send raw commands.util: General purpose utilities, includingip_validate()for IP strings,str_to_uint8()for string-to-integer conversion, and ajsonmodule (based onjsmn) for parsing JSON.gamejolt: Implementation of the Gamejolt Game API for online trophies, scoreboards, and friend management.
Use ConvSym to extract and convert symbols
masterConvSym is a command-line utility used to extract symbol lists from various assembler-specific file formats (like ASM68K, GNU AS, or SGDK) and convert them into DEB1/DEB2 formats for debuggers or human-readable text files.
Basic Usage Pattern:
convsym [input_file|-] [output_file|-] <options>input_file: The source file containing symbols. Use-for STDIN.output_file: The destination file. Use-for STDOUT.options: Configuration for input/output formats, offsets, and filtering.
convsym symbols.sym symbols.deb2XGM Driver Overview
masterThe XGM (eXtended Genesis Music) driver is a music driver designed specifically for the Sega Megadrive/Genesis. It is optimized to run entirely on the Z80 CPU, leaving the 68000 CPU free for other tasks.
Key features include:
- Low Resource Usage: Designed to minimize CPU decoding and maintain data sizes smaller than VGM files.
- Channel Support: Supports both FM and PSG chips. It allows up to 4 PCM channels (8-bit signed at 14 kHz) via software mixing in the FM DAC (replacing the 6th FM channel). Total theoretical capacity is 13 channels (5 FM + 4 PCM + 4 PSG).
- SFX Support: Supports PCM-format SFX with 16 priority levels.
- Compatibility: Developed for SGDK.
XGM2 Driver Overview
masterThe XGM2 (eXtended Genesis Music) driver is a music driver for the Sega Megadrive/Genesis system. It is designed to replace the original XGM driver by reducing the music data footprint (~30% reduction for FM/PSG data), improving PCM latency, and adding envelope support for FM and PSG.
Key technical characteristics:
- CPU Usage: Runs at 100% on the Z80 CPU, leaving the 68000 free for other tasks.
- Channel Support: Supports up to 5 FM channels, 4 PSG channels, and 3 PCM channels (via software mixing in the FM DAC, replacing the 6th FM channel), totaling up to 12 channels.
- PCM Playback: Fixed at ~13.3 kHz (full speed) or ~6.65 kHz (half speed) per channel. Half speed mode reduces ROM usage for low-sample-rate sounds.
- SFX: Supported only through PCM channels. It is recommended to use the second or third PCM channel for SFX, as the first is generally reserved for music.
Requirements for using flash saving
masterTo use this module on physical hardware, you need a flash cartridge with the
#WEsignal properly wired and a supported chip that meets these criteria:- Is compatible with the AMD specification.
- Implements a CFI interface.
Emulator Support: Currently, only Genesis Plus GX (version from January 14, 2026, or newer) implements the features required to emulate flash saving.
XGM2 Packed Data Stream Compression
masterStreams for FM and PSG data are compressed using a classic LZ compression scheme.
Input Stream Block Format
LLLMMMMM [oooooooo] <literal_data>- LLL: Literal size (0 = no literal).
- MMMMM: Match size.
- If LLL > 0: Literal size is 1-7. Literal data follows (after match offset if present).
- If MMMMM == 1: Special function: triggers a 256-byte page cross on the next block (sets Z80 bank).
- If MMMMM >= 2: Match size is 2-31. The next byte represents the match offset (
b7-b0, 0-255). - If LLL 0 and MMMMM 0: End of block.
How to use resources with rescomp
masterSGDK relies heavily on resources (such as tilesets, sprites, and music) which must be compiled using the
rescomptool.To understand which kinds of resources you can use and how to declare them in your code, refer to the
rescomp.txtfile located in thebin/directory of the SGDK installation.For a practical implementation guide, examine the
sample/game/sonicfolder in the repository, which demonstrates how to use both SGDK functions and resources in a real project.Understand flash chip operations and restrictions
masterWhen working with flash memory for save data, you must account for the physical limitations of the chip. Flash chips typically support three operations:
- Reading: Accessing data at a specific address.
- Programming: Writing data to a memory address. This can usually only be done if the address has been previously erased.
- Erasing: Setting all bits in a sector to a predefined value (typically
0xFF).
Critical Restriction: You cannot erase a single word. You must erase an entire sector. Because sectors are often large (e.g., 64 KiB), any data you wish to preserve must be managed carefully. If you attempt to program an address that has not been erased, the operation may fail or behave unexpectedly depending on the chip.
To avoid data loss, it is highly recommended to use the
savemanmodule, which abstracts these complexities. If you choose to bypass it, you must manually manage sector erasures and interrupt handling.Conditional Assembly with IF/ELSE
masterYou can conditionally assemble code blocks using
IF,IFDEF, andIFNDEF.IF <expression>: Assembles the following lines if the expression is non-zero.IFDEF <id>: Assembles if the identifier<id>is defined (identifiers are not labels).IFNDEF <id>: Assembles if the identifier<id>is NOT defined.ELSE: Provides an alternative block if the condition is not met.ENDIF: Required to close everyIFblock.
IFDEF MSX_LEAN_AND_MEAN CALL InitOwnMM ELSE CALL InitDos2MemMan ENDIFAvailable Z80 RAM for MiniMusic on Teradrive
masterOn standard Mega Drive hardware, MiniMusic is limited to 6KB of sound data because the Z80 RAM is 8KB and the first 2KB are reserved for the sound program.
On Teradrive hardware, which features 16KB of Z80 RAM, MiniMusic can utilize up to 14KB of data. Note that this configuration is not officially supported outside of standard Mega Drive compatibility and targets a very specific userbase.