Sega Mega Drive Game Development Kit (SGDK)

repository·master·Indexed 24 days ago

https://github.com/stephane-d/sgdk

A 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.

Tokens
15.6K
Snippets
33
Records
93
Agent score
80%

What's inside SGDK

  1. Overview of MiniMusic sound driver

    master
    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.
  2. Overview of SGDK

    master

    SGDK (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-elf target) and libgcc to 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).
  3. Overview of MegaWiFi API modules

    master

    The 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 the megawifi module and send raw commands.
    • util: General purpose utilities, including ip_validate() for IP strings, str_to_uint8() for string-to-integer conversion, and a json module (based on jsmn) for parsing JSON.
    • gamejolt: Implementation of the Gamejolt Game API for online trophies, scoreboards, and friend management.
  4. Use ConvSym to extract and convert symbols

    master

    ConvSym 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.deb2
  5. XGM Driver Overview

    master

    The 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.
  6. XGM2 Driver Overview

    master

    The 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.
  7. Requirements for using flash saving

    master

    To use this module on physical hardware, you need a flash cartridge with the #WE signal 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.

  8. XGM2 Packed Data Stream Compression

    master

    Streams 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.
  9. How to use resources with rescomp

    master

    SGDK relies heavily on resources (such as tilesets, sprites, and music) which must be compiled using the rescomp tool.

    To understand which kinds of resources you can use and how to declare them in your code, refer to the rescomp.txt file located in the bin/ directory of the SGDK installation.

    For a practical implementation guide, examine the sample/game/sonic folder in the repository, which demonstrates how to use both SGDK functions and resources in a real project.

  10. Understand flash chip operations and restrictions

    master

    When 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 saveman module, which abstracts these complexities. If you choose to bypass it, you must manually manage sector erasures and interrupt handling.

  11. Conditional Assembly with IF/ELSE

    master

    You can conditionally assemble code blocks using IF, IFDEF, and IFNDEF.

    • 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 every IF block.
    IFDEF MSX_LEAN_AND_MEAN
      CALL InitOwnMM
    ELSE
      CALL InitDos2MemMan
    ENDIF
  12. Available Z80 RAM for MiniMusic on Teradrive

    master

    On 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.