# bubio/swanium-crystal architecture

> Architecture diagram of the GitHub repository bubio/swanium-crystal, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bubio/swanium-crystal
- Repository: https://github.com/bubio/swanium-crystal

## Overview

#### 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: [`src/swanium/core/machine.cr`](https://github.com/bubio/swanium-crystal/blob/main/src/swanium/core/machine.cr) (Machine), [`src/swanium/core/memory_bus.cr`](https://github.com/bubio/swanium-crystal/blob/main/src/swanium/core/memory_bus.cr) (Memory bus), [`src/swanium/core/cartridge.cr`](https://github.com/bubio/swanium-crystal/blob/main/src/swanium/core/cartridge.cr) (Cartridge), [`src/swanium/core/ppu.cr`](https://github.com/bubio/swanium-crystal/blob/main/src/swanium/core/ppu.cr) (PPU render state)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Emulation Core"]
  node_machine{{"Machine<br/>emulation orchestrator<br/>[machine.cr]"}}
  node_hardware["Hardware map<br/>machine composition<br/>[hardware.cr]"]
  node_cpu["V30 CPU state<br/>CPU execution<br/>[cpu.cr]"]
  node_bus["Memory bus<br/>address routing<br/>[memory_bus.cr]"]
  node_interrupt_dma["Interrupts and DMA<br/>hardware controllers"]
  node_cartridge["Cartridge<br/>mapper and writable media<br/>[cartridge.cr]"]
  node_rtc_state["RTC and save state<br/>core serialization<br/>[save_state.cr]"]
  node_ppu["PPU render state<br/>video hardware<br/>[ppu.cr]"]
  node_apu["APU PCM buffer<br/>audio hardware<br/>[apu.cr]"]
end

subgraph group_host["Host Platform"]
  node_rom_loader["ROM loader<br/>ROM validation<br/>[rom_loader.cr]"]
  node_sdl["SDL platform<br/>video input audio<br/>[sdl.cr]"]
  node_resampler["Audio resampler<br/>host audio output<br/>[audio_resampler.cr]"]
  node_state_store[("State store<br/>host persistence<br/>[state_store.cr]")]
end

subgraph group_frontend["Frontend Controls"]
  node_input["Input bindings<br/>emulator actions<br/>[input_bindings.cr]"]
  node_debugger["Debugger<br/>debug controls<br/>[debugger.cr]"]
  node_native_controls["Native controls<br/>OS menu adapters<br/>[native_controls.cr]"]
  node_menus["Platform menus<br/>macOS Linux Windows menus<br/>[linux_menu.cr]"]
end

subgraph group_delivery["Release Delivery"]
  node_release_ci["Release workflow<br/>CI publishing"]
  node_macos_packaging["macOS packaging<br/>app bundle packaging"]
  node_windows_packaging["Windows packaging<br/>ZIP packaging"]
end

node_entry["Application entry<br/>Crystal executable<br/>[swanium.cr]"]

node_entry -->|"loads ROM"| node_rom_loader
node_entry -->|"starts emulation"| node_machine
node_rom_loader -->|"creates"| node_cartridge
node_machine -->|"coordinates"| node_hardware
node_hardware -->|"contains"| node_cpu
node_cpu -->|"memory accesses"| node_bus
node_bus -->|"mapped ROM/media"| node_cartridge
node_bus -->|"mapped video registers"| node_ppu
node_bus -->|"mapped audio registers"| node_apu
node_machine -->|"services"| node_interrupt_dma
node_machine -->|"advances"| node_ppu
node_machine -->|"advances"| node_apu
node_ppu -->|"framebuffer presentation"| node_sdl
node_apu -->|"PCM"| node_resampler
node_resampler -->|"SDL audio"| node_sdl
node_cartridge -->|"persistent media"| node_state_store
node_rtc_state -->|"save slots and RTC"| node_state_store
node_machine -->|"serializes state"| node_rtc_state
node_sdl -->|"input events"| node_input
node_input -->|"emulator actions"| node_machine
node_debugger -->|"inspection and control"| node_machine
node_native_controls -->|"platform implementation"| node_menus
node_menus -->|"menu actions"| node_machine
node_release_ci -->|"builds bundle"| node_macos_packaging
node_release_ci -->|"builds ZIP"| node_windows_packaging

click node_entry "https://github.com/bubio/swanium-crystal/blob/main/src/swanium.cr"
click node_rom_loader "https://github.com/bubio/swanium-crystal/blob/main/src/swanium/platform/rom_loader.cr"
click node_machine "https://github.com/bubio/swanium-crystal/blob/main/src/swanium/core/machine.cr"
click node_hardware "https://github.com/bubio/swanium-crystal/blob/main/src/swanium/core/hardware.cr"
click node_cpu "https://github.com/bubio/swanium-crystal/blob/main/src/swanium/core/cpu.cr"
click node_bus "https://github.com/bubio/swanium-crystal/blob/main/src/swanium/core/memory_bus.cr"
click node_interrupt_dma "https://github.com/bubio/swanium-crystal/blob/main/src/swanium/core/interrupt_controller.cr"
click node_cartridge "https://github.com/bubio/swanium-crystal/blob/main/src/swanium/core/cartridge.cr"
click node_rtc_state "https://github.com/bubio/swanium-crystal/blob/main/src/swanium/core/save_state.cr"
click node_ppu "https://github.com/bubio/swanium-crystal/blob/main/src/swanium/core/ppu.cr"
click node_apu "https://github.com/bubio/swanium-crystal/blob/main/src/swanium/core/apu.cr"
click node_sdl "https://github.com/bubio/swanium-crystal/blob/main/src/swanium/platform/sdl.cr"
click node_resampler "https://github.com/bubio/swanium-crystal/blob/main/src/swanium/platform/audio_resampler.cr"
click node_state_store "https://github.com/bubio/swanium-crystal/blob/main/src/swanium/platform/state_store.cr"
click node_input "https://github.com/bubio/swanium-crystal/blob/main/src/swanium/frontend/input_bindings.cr"
click node_debugger "https://github.com/bubio/swanium-crystal/blob/main/src/swanium/frontend/debugger.cr"
click node_native_controls "https://github.com/bubio/swanium-crystal/blob/main/src/swanium/frontend/native_controls.cr"
click node_menus "https://github.com/bubio/swanium-crystal/blob/main/src/swanium/frontend/linux_menu.cr"
click node_release_ci "https://github.com/bubio/swanium-crystal/blob/main/.github/workflows/publish-release-assets.yml"
click node_macos_packaging "https://github.com/bubio/swanium-crystal/blob/main/tools/macos/package_macos_app.sh"
click node_windows_packaging "https://github.com/bubio/swanium-crystal/blob/main/tools/windows/package_windows.ps1"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_machine,node_hardware,node_cpu,node_bus,node_interrupt_dma,node_cartridge,node_rtc_state,node_ppu,node_apu toneBlue
class node_rom_loader,node_sdl,node_resampler,node_state_store toneAmber
class node_input,node_debugger,node_native_controls,node_menus toneMint
class node_release_ci,node_macos_packaging,node_windows_packaging toneRose
class node_entry toneNeutral
```

## Components

### Emulation Core

Deterministic WonderSwan hardware emulation.

- [Machine](https://github.com/bubio/swanium-crystal/blob/main/src/swanium/core/machine.cr): emulation orchestrator · Runs frames, reset, timing, and hardware coordination. · `src/swanium/core/machine.cr`
- [Hardware map](https://github.com/bubio/swanium-crystal/blob/main/src/swanium/core/hardware.cr): machine composition · `src/swanium/core/hardware.cr`
- [V30 CPU state](https://github.com/bubio/swanium-crystal/blob/main/src/swanium/core/cpu.cr): CPU execution · Instruction execution, registers, and flags. · `src/swanium/core/cpu.cr`
- [Memory bus](https://github.com/bubio/swanium-crystal/blob/main/src/swanium/core/memory_bus.cr): address routing · Decodes CPU memory accesses to mapped hardware. · `src/swanium/core/memory_bus.cr`
- [Interrupts and DMA](https://github.com/bubio/swanium-crystal/blob/main/src/swanium/core/interrupt_controller.cr): hardware controllers · `src/swanium/core/interrupt_controller.cr`
- [Cartridge](https://github.com/bubio/swanium-crystal/blob/main/src/swanium/core/cartridge.cr): mapper and writable media · Models headers, mapping, SRAM, and EEPROM. · `src/swanium/core/cartridge.cr`
- [RTC and save state](https://github.com/bubio/swanium-crystal/blob/main/src/swanium/core/save_state.cr): core serialization · `src/swanium/core/save_state.cr`
- [PPU render state](https://github.com/bubio/swanium-crystal/blob/main/src/swanium/core/ppu.cr): video hardware · Produces framebuffer, palette, and sprite state. · `src/swanium/core/ppu.cr`
- [APU PCM buffer](https://github.com/bubio/swanium-crystal/blob/main/src/swanium/core/apu.cr): audio hardware · Synthesizes channels into buffered PCM. · `src/swanium/core/apu.cr`

### Host Platform

SDL and native OS integration.

- [ROM loader](https://github.com/bubio/swanium-crystal/blob/main/src/swanium/platform/rom_loader.cr): ROM validation · Accepts supported cartridges and validates metadata. · `src/swanium/platform/rom_loader.cr`
- [SDL platform](https://github.com/bubio/swanium-crystal/blob/main/src/swanium/platform/sdl.cr): video input audio · Presents video and supplies SDL input/audio services. · `src/swanium/platform/sdl.cr`
- [Audio resampler](https://github.com/bubio/swanium-crystal/blob/main/src/swanium/platform/audio_resampler.cr): host audio output · `src/swanium/platform/audio_resampler.cr`
- [State store](https://github.com/bubio/swanium-crystal/blob/main/src/swanium/platform/state_store.cr): host persistence · Persists writable media, RTC, recent ROMs, and save slots. · `src/swanium/platform/state_store.cr`

### Frontend Controls

User actions, menus, and debugging.

- [Input bindings](https://github.com/bubio/swanium-crystal/blob/main/src/swanium/frontend/input_bindings.cr): emulator actions · `src/swanium/frontend/input_bindings.cr`
- [Debugger](https://github.com/bubio/swanium-crystal/blob/main/src/swanium/frontend/debugger.cr): debug controls · `src/swanium/frontend/debugger.cr`
- [Native controls](https://github.com/bubio/swanium-crystal/blob/main/src/swanium/frontend/native_controls.cr): OS menu adapters · Dispatches Linux and Windows native control integration. · `src/swanium/frontend/native_controls.cr`
- [Platform menus](https://github.com/bubio/swanium-crystal/blob/main/src/swanium/frontend/linux_menu.cr): macOS Linux Windows menus · `src/swanium/frontend/linux_menu.cr`

### Release Delivery

Cross-platform packaging and publishing.

- [Release workflow](https://github.com/bubio/swanium-crystal/blob/main/.github/workflows/publish-release-assets.yml): CI publishing · Builds and publishes OS-specific release artifacts. · `.github/workflows/publish-release-assets.yml`
- [macOS packaging](https://github.com/bubio/swanium-crystal/blob/main/tools/macos/package_macos_app.sh): app bundle packaging · `tools/macos/package_macos_app.sh`
- [Windows packaging](https://github.com/bubio/swanium-crystal/blob/main/tools/windows/package_windows.ps1): ZIP packaging · `tools/windows/package_windows.ps1`

### Other components

- [Application entry](https://github.com/bubio/swanium-crystal/blob/main/src/swanium.cr): Crystal executable · `src/swanium.cr`

## Connections

- 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
