MEGA-PI is a token-optimized, high-performance Sega Mega Drive / Genesis emulator designed to run bare-metal on the Raspberry Pi. By bypassing heavy operating systems like Linux, it achieves near-instant boot times and ultra-low input latency.
It leverages the Picodrive emulation core and runs on top of the Circle bare-metal C++ framework, distributing workload across the Raspberry Pi's four ARM cores.
- Multi-core Architecture:
- Core 0 (Orchestrator): Emulates the Sega Mega Drive/Mega CD hardware, clocks the Sub-CPU (S68K) synchronously, and handles emulation events.
- Core 1 (Video): Handles 2x integer nearest-neighbor upscaling (yielding clean 640x480 outputs) and OSD menu rendering.
- Core 2 (Audio): Outputs 44.1 kHz stereo audio via the Pi's PWM sound engine.
- Core 3 (Input): Dedicated USB plug-and-play thread for gamepad and keyboard inputs.
- Sega CD / Mega CD Support:
- High-Quality Formats: Supports CD images in both standard
.cue(with separate.bintracks) and compressed.chdformats (compressed viachdman). - Accurate Subsystem Emulation: Emulates the Sub-CPU, RF5C164 PCM audio chip, CDDA (Redbook) digital audio streaming, and the hardware rotation/scaling graphics coprocessor.
- Region-Free BIOS Handling: Automatically detects the CD region from the image and loads the corresponding BIOS file (
bios_CD_E.bin,bios_CD_U.bin,bios_CD_J.bin).
- High-Quality Formats: Supports CD images in both standard
- Smart Tabbed OSD Navigation: Partitioned across 6 distinct tabs (ALL, FAV, alphabetical splits, and Mega CD), easily browsed using Left/Right controls. Includes clean alignment prefixes (
*for favorites,for standard), viewport-scrolling window, ROM counter, and file size indicator. - Persistent Save & Load States: Supports standard emulator save states mapped directly to the SD card. Save operations physically check the SD card status (
CMD13polling) and verify that sector programming is completed before returning, ensuring 100% data retention across physical power cycles. - HDD LED Simulation: The green activity LED is configured to remain OFF by default and flash dynamically only during physical SD card read/write operations (simulating a classic hard drive activity indicator).
- Favorite Games System: Toggle favorites persistently via a single button press. Favorites are stored inside
SD:/roms/favorites.txtand synced safely to disk. - Input Masking: In-game actions are ignored while holding control hotkey combinations to prevent accidental character movements.
- ROM Support: Scans and loads
.bin,.md,.genfiles for Genesis/Mega Drive, and.cue,.chdfiles for Sega CD / Mega CD.
The emulator supports both USB gamepads and USB keyboards out-of-the-box.
In the menu or during gameplay, use the following shortcuts:
| Action | Control Shortcut |
|---|---|
| Move Up / Down (Menu) | D-pad Up / Down |
| Change Active Tab (Menu) | D-pad Left / Right |
| Favorite Selected ROM (Menu) | Button B |
| Disfavorite Selected ROM (Menu) | Button C |
| Boot Selected ROM (Menu) | Button A / Start |
| Save State (Slot 0) (In-game) | SELECT + D-pad Left |
| Load State (Slot 0) (In-game) | SELECT + D-pad Right |
| Exit to Menu (In-game) | START + SELECT |
In the menu or during gameplay, use the following keys:
| Sega Mega Drive / Action | Keyboard Key | Description |
|---|---|---|
| D-pad (Up/Down/Left/Right) | Arrow Keys | Directional navigation |
| Button A / Select | Z |
Boot selected game |
| Button B / Favorite | X |
Add selected game to Favorites |
| Button C / Unfavorite | C |
Remove selected game from Favorites |
| Button X | A |
Mega Drive Button X |
| Button Y | S |
Mega Drive Button Y |
| Button Z | D |
Mega Drive Button Z |
| Start Button | Enter |
Start game |
| Mode Button | Space |
Mode selection |
| Save State (Slot 0) | F5 |
Save state in-game |
| Load State (Slot 0) | F8 |
Load state in-game |
| Exit to Menu | Escape |
Return to OSD menu |
- emulator/: Bare-metal integration code.
- kernel.cpp / kernel.h: Entrypoint, multicore scheduling, rendering, and input handling.
- emu_orchestrator.cpp / emu_orchestrator.h: Picodrive core setup, ROM loading, and save state actions.
- clib_stubs.cpp: Standard C library stubs and custom file system wrapper mapping standard file functions to Circle's FAT FS.
- osd.cpp / osd.h: SD card ROM scanning and OSD menu state.
- audio_ring_buffer.h: Interlocked circular buffer for audio samples between Core 0 and Core 2.
- shared_state.h: Memory shared across the 4 ARM cores.
- picodrive/: Picodrive emulator core.
- circle/: Circle bare-metal C++ framework.
To compile the project, you need the GNU Arm Embedded Toolchain (arm-none-eabi-gcc).
Circle must be configured for the correct Raspberry Pi hardware model and compiled first.
-
Navigate to the circle/ directory:
cd circle -
Run the configure script with multi-core support enabled.
- For Raspberry Pi 3 (AArch32, outputs
kernel8-32.img):./configure -r 3 --multicore
- For Raspberry Pi 4 (AArch32, outputs
kernel7l.img):./configure -r 4 --multicore
(Note: Add the
-fflag to overwrite an existing configuration if needed) - For Raspberry Pi 3 (AArch32, outputs
-
Compile the Circle framework libraries:
./makeall
Once Circle is compiled, you can build the emulator executable.
-
Navigate to the emulator/ directory:
cd ../emulator -
Compile the project:
make
Depending on the configured target, this will output a bootable image (e.g.,
kernel8-32.imgfor Raspberry Pi 3, orkernel7l.imgfor Raspberry Pi 4).
To package all files needed for the SD card into a single zip file:
-
Version Tracking:
- A single source-of-truth VERSION file in the root of the repository tracks the emulator's current version (e.g.,
1.0). - The emulator/Makefile automatically reads this file during compilation and passes it to the preprocessor via the
-DMEGAPI_VERSIONmacro. This displays the version dynamically in the OSD menu title:--- MEGA-PI BAREMETAL EMULATOR v1.0 ---.
- A single source-of-truth VERSION file in the root of the repository tracks the emulator's current version (e.g.,
-
Run the Packaging Script: Return to the root directory and run the packaging script:
./create_release.sh [version]
- Automatic Versioning (Default): Running
./create_release.shwithout arguments reads the version from the VERSION file, normalizes it with avprefix, and creates a versioned archive, e.g.,mega-pi-release-v1.0.zip. If theVERSIONfile is missing, it falls back to the latest Git tag/hash. - Explicit Versioning: You can pass a specific version string as an argument, e.g.,
./create_release.sh 1.1or./create_release.sh v1.1. The script will normalize the string to ensure a singlevprefix and package it asmega-pi-release-v1.1.zip.
- Automatic Versioning (Default): Running
- Format an SD card as FAT32.
- Copy all files and directories (including the
overlaysdirectory) from the emulator/boot/ directory (including firmware files likebootcode.bin,start.elf,start4.elf,fixup.dat, Device Tree.dtbfiles,config.txt, andcmdline.txt) to the root of the SD card. - Copy your compiled kernel image (
kernel8-32.imgfor RPi 3 orkernel7l.imgfor RPi 4) from the emulator/ directory to the root of the SD card. - Create a folder named
romson the root of the SD card, and place your Sega Genesis ROMs (.bin,.md,.gen) and Sega CD games (.cue+.bintracks, or compressed.chdfiles) inside it. - Sega CD BIOS Setup: Create a folder named
bioson the root of the SD card and copy the official Sega CD BIOS files. They must be named exactly as follows depending on the region:- US Region:
bios_CD_U.bin - EU Region:
bios_CD_E.bin - JP Region:
bios_CD_J.bin
- US Region:
- Plug in a USB Gamepad and/or Keyboard, insert the SD card, and power on the Pi.
- Circle Bare-Metal Framework: Developed by R. Stange (rsta2). Circle provides the outstanding bare-metal C++ environment, USB controllers, scheduling, audio support, and Raspberry Pi interface drivers.
- Picodrive Emulator: Developed by notaz and community contributors. Picodrive provides the optimized Sega Mega Drive / Genesis / Mega CD emulation engine, Cyclone M68K CPU core, and Z80 assembly cores.
