How bubio/swanium-crystal works
Purpose and Entry Points
Swanium Crystal is a Crystal-language, cross-platform WonderSwan emulator targeting WonderSwan Crystal hardware while running older WonderSwan/Color ROMs through compatibility behavior. The executable entry point is src/swanium.cr; production output is bin/swanium-crystal. Startup accepts --rom or opens native ROM selection, validates .ws/.wsc cartridges, and starts directly from the cartridge reset vector without a BIOS.
Runtime Composition
The application separates deterministic emulation core, SDL-backed platform services, and native frontend controls:
• Core machine orchestration: src/swanium/core/machine.cr, src/swanium/core/hardware.cr
• SDL display/input/audio integration: src/swanium/platform/sdl.cr, src/swanium/platform/audio_resampler.cr
• Platform-specific menu/control adapters: src/swanium/frontend/native_controls.cr, src/swanium/frontend/native_controls_linux.cr, src/swanium/frontend/native_controls_windows.cr
Crystal is the principal runtime. SDL2 provides video, input, and audio. Linux additionally uses GTK3/GDK X11 for window/menu integration and runs through X11/XWayland rather than native Wayland.
Emulation Machine and CPU/Memory Flow
Machine coordinates emulation time, resets, frame execution, interrupts, DMA, video, audio, and persistence-facing state. CPU memory accesses pass through mapped bus/address-decoding layers:
• V30 CPU and architectural state: src/swanium/core/cpu.cr, src/swanium/core/registers.cr, src/swanium/core/flags.cr
• Address and bus routing: src/swanium/core/bus.cr, src/swanium/core/memory_bus.cr, src/swanium/core/v30_address_decoder.cr
• Interrupt/DMA hardware: src/swanium/core/interrupt_controller.cr, src/swanium/core/dma_controller.cr
This is the main boundary between CPU instruction execution and memory-mapped hardware devices.
Cartridge and Durable Data
ROM ingestion and cartridge hardware are distinct from runtime machine state:
• ROM validation/loading: src/swanium/platform/rom_loader.cr
• Cartridge header, mapper, SRAM/EEPROM behavior: src/swanium/core/cartridge.cr
• RTC and save-state serialization: src/swanium/core/rtc.cr, src/swanium/core/save_state.cr
• Host persistence management: src/swanium/platform/state_store.cr
The loader admits supported ROM extensions and validates cartridge metadata/entry data. Cartridge-backed writable media, RTC, recent-ROM metadata, and ten save-state slots cross the core-to-host storage boundary through the state store.
Video Pipeline
The PPU produces WonderSwan framebuffer state, including palette and sprite behavior; the frontend/platform layer presents it through SDL with orientation, scaling, fullscreen, and nearest/linear filtering controls.
• PPU/render state: src/swanium/core/ppu.cr, src/swanium/core/palette.cr, src/swanium/core/sprite_latch.cr
• Display-facing test/pattern support: src/swanium/frontend/video_test_pattern.cr
• SDL presentation boundary: src/swanium/platform/sdl.cr
Audio Pipeline
The APU synthesizes hardware channels and buffered PCM; platform code resamples/output-matches it for SDL audio:
• APU coordination and PCM buffering: src/swanium/core/apu.cr, src/swanium/core/pcm_sample_buffer.cr
• Sound generators/channels: src/swanium/core/voice_channel.cr, src/swanium/core/wave_generator.cr, src/swanium/core/noise_generator.cr
• Host resampling: src/swanium/platform/audio_resampler.cr
User Interaction, Debugging, and Delivery
Input bindings and debug controls feed emulator actions such as pause, overlay, save/load, and display controls. Native menus supplement SDL’s game surface.
• Input and debugger: src/swanium/frontend/input_bindings.cr, src/swanium/frontend/debugger.cr
• macOS/Linux/Windows menu implementations: src/swanium/frontend/macos_menu.cr, src/swanium/frontend/linux_menu.cr, src/swanium/frontend/windows_menu.cr
• Release automation: .github/workflows/publish-release-assets.yml, tools/macos/package_macos_app.sh, tools/windows/package_windows.ps1
CI is split by OS under .github/workflows/; packaging produces macOS app bundles, Windows ZIP artifacts, and Linux Debian/RPM/AppImage distributions.
Main entry points: machine.cr Machine · memory_bus.cr Memory bus · cartridge.cr Cartridge · ppu.cr PPU render state
Components 21
Emulation Core
Deterministic WonderSwan hardware emulation.
- Machine · Runs frames, reset, timing, and hardware coordination.
src/swanium/core/machine.cr - Hardware map
src/swanium/core/hardware.cr - V30 CPU state · Instruction execution, registers, and flags.
src/swanium/core/cpu.cr - Memory bus · Decodes CPU memory accesses to mapped hardware.
src/swanium/core/memory_bus.cr - Interrupts and DMA
src/swanium/core/interrupt_controller.cr - Cartridge · Models headers, mapping, SRAM, and EEPROM.
src/swanium/core/cartridge.cr - RTC and save state
src/swanium/core/save_state.cr - PPU render state · Produces framebuffer, palette, and sprite state.
src/swanium/core/ppu.cr - APU PCM buffer · Synthesizes channels into buffered PCM.
src/swanium/core/apu.cr
Host Platform
SDL and native OS integration.
- ROM loader · Accepts supported cartridges and validates metadata.
src/swanium/platform/rom_loader.cr - SDL platform · Presents video and supplies SDL input/audio services.
src/swanium/platform/sdl.cr - Audio resampler
src/swanium/platform/audio_resampler.cr - State store · Persists writable media, RTC, recent ROMs, and save slots.
src/swanium/platform/state_store.cr
Frontend Controls
User actions, menus, and debugging.
- Input bindings
src/swanium/frontend/input_bindings.cr - Debugger
src/swanium/frontend/debugger.cr - Native controls · Dispatches Linux and Windows native control integration.
src/swanium/frontend/native_controls.cr - Platform menus
src/swanium/frontend/linux_menu.cr
Release Delivery
Cross-platform packaging and publishing.
- Release workflow · Builds and publishes OS-specific release artifacts.
.github/workflows/publish-release-assets.yml - macOS packaging
tools/macos/package_macos_app.sh - Windows packaging
tools/windows/package_windows.ps1
Other components
- Application entry
src/swanium.cr
Connections 25
- Application entry → ROM loader: loads ROM
- Application entry → Machine: starts emulation
- ROM loader → Cartridge: creates
- Machine → Hardware map: coordinates
- Hardware map → V30 CPU state: contains
- V30 CPU state → Memory bus: memory accesses
- Memory bus → Cartridge: mapped ROM/media
- Memory bus → PPU render state: mapped video registers
- Memory bus → APU PCM buffer: mapped audio registers
- Machine → Interrupts and DMA: services
- Machine → PPU render state: advances
- Machine → APU PCM buffer: advances
- PPU render state → SDL platform: framebuffer presentation
- APU PCM buffer → Audio resampler: PCM
- Audio resampler → SDL platform: SDL audio
- Cartridge → State store: persistent media
- RTC and save state → State store: save slots and RTC
- Machine → RTC and save state: serializes state
- SDL platform → Input bindings: input events
- Input bindings → Machine: emulator actions
- Debugger → Machine: inspection and control
- Native controls → Platform menus: platform implementation
- Platform menus → Machine: menu actions
- Release workflow → macOS packaging: builds bundle
- Release workflow → Windows packaging: builds ZIP