moon-gba

    Pure-MoonBit 32-bit ARM7TDMI Game Boy Advance emulator, ARM/Thumb disassembler, scanline PPU (Modes 0-5 + OBJ), HLE BIOS, headless CLI, and 60 FPS browser debugger

    gba
    arm7tdmi
    emulator
    ppu
    disassembler
    gameboy-advance
    Download zip
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    1 hour ago
    Downloads
    2

    #moon-gba

    Pure-MoonBit 32-bit ARM7TDMI Game Boy Advance emulator, ARM/Thumb disassembler, scanline PPU (Modes 0–5 + 128 OAM sprites), HLE BIOS, headless CLI, and zero-upload 60 FPS browser console & real-time debugger.

    #Features

    1. Zero-Dependency Root Library (wasm / wasm-gc / js / native)
      • ARM7TDMI CPU (cpu.mbt, arm.mbt, thumb.mbt): All 6 banked processor modes (USR/SYS, FIQ, IRQ, SVC, ABT, UND), CPSR/SPSR switching, exact 3-stage pipeline PC offsets, full ARMv4T 32-bit ARM instruction set (ALU + barrel shifter carry edge cases, MUL/MLA/UMULL/UMLAL/SMULL/SMLAL, unaligned rotating LDR/LDRH/LDRSH, LDM/STM with S-bit and writeback quirks, SWP/SWPB, MRS/MSR, B/BL/BX, SWI), and all 19 Thumb 16-bit instruction formats.
      • ARM/Thumb Disassembler (disasm.mbt): Real-time disassembly for CLI and browser debugger.
      • 32-bit Memory Bus & Peripherals (bus.mbt, dma.mbt, timer.mbt, bios.mbt): BIOS, EWRAM (256 KB), IWRAM (32 KB), I/O registers, Palette RAM (1 KB), VRAM (96 KB with 8-bit write duplication & mirror semantics), OAM (1 KB), ROM WaitStates 0–2, SRAM (64 KB with 8-bit lane duplication), 4-channel DMA (Immediate/VBlank/HBlank), 4 hardware timers with prescalers & cascade, interrupt controller (IME/IE/IF), keypad (KEYINPUT), and clean-room HLE BIOS (SoftReset, RegisterRamReset, Halt, IntrWait, VBlankIntrWait, Div/DivArm, Sqrt, CpuSet, CpuFastSet, LZ77UnCompWram/LZ77UnCompVram).
      • 240×160 Scanline PPU (ppu.mbt): Exact 1,232-cycle scanlines (960 HDraw + 272 HBlank × 228 lines = 280,896 cycles/frame), Modes 0–5 (regular tiled 4bpp/8bpp, affine rotation/scaling BG2/BG3, direct 15-bit & paletted 8-bit bitmaps), 128 OAM sprites (1D/2D, regular & affine), per-layer debug visibility toggles (BG0–BG3, OBJ), RGBA8888 framebuffer, binary PPM export, and native SVG Palette RAM inspector.
    2. Headless CLI (cmd/main)
      • Execute any .gba ROM for N frames, inspect registers/CPSR/framebuffer stats as JSON, export PPM screenshots & SVG palettes, and disassemble ARM/Thumb code.
    3. Zero-Upload 60 FPS Browser Console (web/, docs/index.html)
      • Ships with two public-domain built-in interactive 32-bit GBA demo ROMs (MOON PLASMA Mode 3 + OBJ sprite, MOON TILES Mode 0 scrolling tilemap) and supports dragging local .gba homebrew ROMs directly in the browser.

    #Official Hardware Verification (jsmolka/gba-tests)

    Verified automatically in CI via python3 demo/verify_gba.py:

    Test ROMCoverageStatusVerified Output
    arm/arm.gbaFull 32-bit ARMv4T instruction suitePASSr12=0, pc=0x08001EC4
    thumb/thumb.gbaFull 16-bit Thumb instruction suitePASSr12=0, pc=0x08000AAC
    memory/memory.gbaUnaligned loads, VRAM/PRAM/OAM 8-bit quirks, mirrors, SRAMPASSr12=0, pc=0x080004C8
    ppu/hello.gbaMode 4 bitmap text renderingPASSdispcnt=0x0404, non_black=209
    ppu/shades.gbaMode 0 tiled 32-shade blue gradientPASSdispcnt=0x0100, avg_rgb=[0,0,115]
    ppu/stripes.gbaMode 0 tiled RGB stripe patternPASSdispcnt=0x0100, non_black=38400

    #Quick Start

    #Library Usage

    let gba = @gba.Gba::new(rom_bytes)
    gba.set_key(@gba.KEY_RIGHT, true)
    gba.run_frames(60)
    let ppm = gba.framebuffer_ppm()
    let lines = gba.disassemble_around_pc(8)

    #Build & Verify Locally

    # Run unit tests across all 4 targets for target in wasm wasm-gc js native; do moon test --target "$target" --deny-warn . done # Build native CLI and run jsmolka/gba-tests oracle moon build --target native --release --deny-warn cmd/main python3 demo/verify_gba.py --skip-build # Build browser console bundle and run V8 smoke test python3 demo/build_web.py

    MemoryBus

    pub(open) trait MemoryBus {
    fn read8(Self, UInt) -> UInt
    fn read16(Self, UInt) -> UInt
    fn read32(Self, UInt) -> UInt
    fn write8(Self, UInt, UInt) -> Unit
    fn write16(Self, UInt, UInt) -> Unit
    fn write32(Self, UInt, UInt) -> Unit
    fn handle_swi(Self, Cpu, UInt) -> Bool
    }

    Abstract 32-bit memory bus interface used by the ARM7TDMI CPU core.

    Cpu

    pub struct Cpu {
    r : FixedArray[UInt]
    cpsr : UInt
    spsr : UInt
    bank_usr : FixedArray[UInt]
    bank_fiq : FixedArray[UInt]
    bank_irq : FixedArray[UInt]
    bank_svc : FixedArray[UInt]
    bank_abt : FixedArray[UInt]
    bank_und : FixedArray[UInt]
    spsr_fiq : UInt
    spsr_irq : UInt
    spsr_svc : UInt
    spsr_abt : UInt
    spsr_und : UInt
    halted : Bool
    branched : Bool
    cycles : UInt64
    }

    ARM7TDMI CPU state including 16 general-purpose registers, CPSR/SPSR, and all 6 banked register sets (User/System, FIQ, IRQ, Supervisor, Abort, Undefined).

    Cpu::check_cond

    fn Cpu::check_cond(self : Cpu, cond : UInt) -> Bool

    Evaluate a 4-bit ARM condition code against current CPSR flags.

    Cpu::exec_arm

    fn[B : MemoryBus] Cpu::exec_arm(self : Cpu, bus : B, op : UInt, pc : UInt) -> Int

    Execute one 32-bit ARM instruction at pc and return approximate cycle count.

    Cpu::exec_thumb

    fn[B : MemoryBus] Cpu::exec_thumb(self : Cpu, bus : B, op : UInt, pc : UInt) -> Int

    Execute one 16-bit Thumb instruction at pc and return approximate cycle count.

    Cpu::flag_c

    fn Cpu::flag_c(self : Cpu) -> Bool

    Cpu::flag_n

    fn Cpu::flag_n(self : Cpu) -> Bool

    Cpu::flag_v

    fn Cpu::flag_v(self : Cpu) -> Bool

    Cpu::flag_z

    fn Cpu::flag_z(self : Cpu) -> Bool

    Cpu::is_thumb

    fn Cpu::is_thumb(self : Cpu) -> Bool

    Cpu::mode

    fn Cpu::mode(self : Cpu) -> UInt

    Cpu::new

    fn Cpu::new() -> Cpu

    Create a new ARM7TDMI CPU initialized to GBA direct-boot post-BIOS state: PC = 0x08000000, System mode (0x1F), SP_usr = 0x03007F00, SP_irq = 0x03007FA0, SP_svc = 0x03007FE0.

    Cpu::pc

    fn Cpu::pc(self : Cpu) -> UInt

    Cpu::read_user_reg

    fn Cpu::read_user_reg(self : Cpu, idx : Int) -> UInt

    Read a user-bank register (used by LDM/STM with S bit when R15 is not in list).

    Cpu::reset

    fn Cpu::reset(self : Cpu) -> Unit

    Reset CPU to GBA post-BIOS entry state at 0x08000000.

    Cpu::restore_spsr

    fn Cpu::restore_spsr(self : Cpu) -> Unit

    Restore CPSR from current mode's SPSR (no-op in User/System modes).

    Cpu::set_c

    fn Cpu::set_c(self : Cpu, carry : Bool) -> Unit

    Cpu::set_cpsr

    fn Cpu::set_cpsr(self : Cpu, val : UInt) -> Unit

    Set CPSR with proper mode banking.

    Cpu::set_nz

    fn Cpu::set_nz(self : Cpu, val : UInt) -> Unit

    Cpu::set_v

    fn Cpu::set_v(self : Cpu, overflow : Bool) -> Unit

    Cpu::step

    fn[B : MemoryBus] Cpu::step(self : Cpu, bus : B) -> Int

    Execute one instruction (ARM or Thumb) on the given memory bus and return cycles consumed.

    Cpu::switch_mode

    fn Cpu::switch_mode(self : Cpu, new_mode : UInt) -> Unit

    Switch processor mode, saving current banked registers and loading target mode's bank.

    Cpu::trigger_irq

    fn Cpu::trigger_irq(self : Cpu) -> Bool

    Trigger a hardware IRQ exception if CPSR.I == 0.

    Cpu::trigger_swi_exception

    fn Cpu::trigger_swi_exception(self : Cpu, ret_addr : UInt) -> Unit

    Trigger a hardware SWI exception (vector 0x00000008).

    Cpu::write_user_reg

    fn Cpu::write_user_reg(self : Cpu, idx : Int, val : UInt) -> Unit

    Write a user-bank register (used by LDM/STM with S bit when R15 is not in list).

    DmaChannel

    pub struct DmaChannel {
    sad : UInt
    dad : UInt
    cnt_l : UInt
    cnt_h : UInt
    int_src : UInt
    int_dst : UInt
    int_cnt : UInt
    enabled : Bool
    }

    State of a single GBA DMA channel (DMA0–DMA3).

    DmaChannel::new

    fn DmaChannel::new() -> DmaChannel

    DmaChannel::reset

    fn DmaChannel::reset(self : DmaChannel) -> Unit

    Gba

    pub struct Gba {
    cpu : Cpu
    bus : GbaBus
    }

    Top-level Game Boy Advance console combining ARM7TDMI CPU, 32-bit Bus, DMA, Timers, HLE BIOS, and PPU.

    Gba::disassemble_around_pc

    fn Gba::disassemble_around_pc(self : Gba, count : Int) -> Array[String]

    Disassemble count instructions starting from current PC (respecting ARM/Thumb mode).

    Gba::framebuffer

    fn Gba::framebuffer(self : Gba) -> FixedArray[Byte]

    Return the 240x160 RGBA8888 framebuffer (240 * 160 * 4 bytes).

    Gba::framebuffer_ppm

    fn Gba::framebuffer_ppm(self : Gba) -> Bytes

    Export the current 240x160 framebuffer as a binary PPM (P6) image.

    Gba::framebuffer_stats

    fn Gba::framebuffer_stats(self : Gba) -> (Int, Int, Int, Int)

    Summary of RGB channel averages and exact color counts in the current framebuffer.

    Gba::load_rom

    fn Gba::load_rom(self : Gba, rom : Bytes) -> Unit

    Load a new ROM image and reset the console.

    Gba::new

    fn Gba::new(rom : Bytes) -> Gba

    Construct a new GBA instance loaded with the given ROM image bytes.

    Gba::render_palette_svg

    fn Gba::render_palette_svg(self : Gba) -> String

    Render the current BG/OBJ Palette RAM (512 colors) as a standalone SVG palette swatch sheet.

    Gba::reset

    fn Gba::reset(self : Gba) -> Unit

    Reset CPU, RAM, I/O registers, DMA, timers, and PPU while keeping the loaded ROM.

    Gba::rom_title

    fn Gba::rom_title(self : Gba) -> String

    Read the 12-byte ASCII game title from the GBA ROM header (0x080000A0..0x080000AB).

    Gba::run_frames

    fn Gba::run_frames(self : Gba, frames : Int) -> Unit

    Run the GBA for frames complete video frames.

    Gba::set_key

    fn Gba::set_key(self : Gba, key : Int, pressed : Bool) -> Unit

    Set the pressed state of a GBA keypad button (KEY_A..KEY_L).

    Gba::set_layer_mask

    fn Gba::set_layer_mask(self : Gba, mask : UInt) -> Unit

    Set debug PPU layer visibility mask (bit0=BG0..bit3=BG3, bit4=OBJ).

    Gba::step

    fn Gba::step(self : Gba) -> Int

    Execute one CPU instruction, tick timers and PPU, and dispatch pending IRQs. Returns cycles consumed.

    Gba::step_frame

    fn Gba::step_frame(self : Gba) -> Unit

    Run the GBA until one complete video frame (280,896 cycles) has been rendered.

    GbaBus

    pub struct GbaBus {
    bios : FixedArray[Byte]
    ewram : FixedArray[Byte]
    iwram : FixedArray[Byte]
    pram : FixedArray[Byte]
    vram : FixedArray[Byte]
    oam : FixedArray[Byte]
    sram : FixedArray[Byte]
    rom : Bytes
    bios_latch : UInt
    irq_ime : UInt
    irq_ie : UInt
    irq_if : UInt
    keyinput : UInt
    keycnt : UInt
    waitcnt : UInt
    postflg : UInt
    dma : FixedArray[DmaChannel]
    timers : FixedArray[TimerChannel]
    ppu : Ppu
    }

    Complete GBA 32-bit memory bus, I/O register bank, DMA, timers, IRQ, and keypad state.
    impl MemoryBus for GbaBus

    GbaBus::handle_swi

    fn GbaBus::handle_swi(self : GbaBus, cpu : Cpu, comment : UInt) -> Bool

    GbaBus::new

    fn GbaBus::new(rom : Bytes) -> GbaBus

    GbaBus::read16

    fn GbaBus::read16(self : GbaBus, addr : UInt) -> UInt

    GbaBus::read32

    fn GbaBus::read32(self : GbaBus, addr : UInt) -> UInt

    GbaBus::read8

    fn GbaBus::read8(self : GbaBus, addr : UInt) -> UInt

    GbaBus::reset

    fn GbaBus::reset(self : GbaBus) -> Unit

    GbaBus::write16

    fn GbaBus::write16(self : GbaBus, addr : UInt, val : UInt) -> Unit

    GbaBus::write32

    fn GbaBus::write32(self : GbaBus, addr : UInt, val : UInt) -> Unit

    GbaBus::write8

    fn GbaBus::write8(self : GbaBus, addr : UInt, val : UInt) -> Unit

    Ppu

    pub struct Ppu {
    dispcnt : UInt
    dispstat : UInt
    vcount : UInt
    bgcnt : FixedArray[UInt]
    bghofs : FixedArray[UInt]
    bgvofs : FixedArray[UInt]
    bg2pa : UInt
    bg2pb : UInt
    bg2pc : UInt
    bg2pd : UInt
    bg2x : UInt
    bg2y : UInt
    bg2x_int : Int
    bg2y_int : Int
    bg3pa : UInt
    bg3pb : UInt
    bg3pc : UInt
    bg3pd : UInt
    bg3x : UInt
    bg3y : UInt
    bg3x_int : Int
    bg3y_int : Int
    line_cycles : Int
    in_hblank : Bool
    frame_count : UInt64
    layer_mask : UInt
    framebuf : FixedArray[Byte]
    line_color : FixedArray[UInt]
    line_prio : FixedArray[Int]
    }

    240x160 GBA Scanline Picture Processing Unit supporting Modes 0–5 and 128 OAM sprites.

    Ppu::new

    fn Ppu::new() -> Ppu

    Ppu::render_scanline

    fn Ppu::render_scanline(self : Ppu, vram : FixedArray[Byte], pram : FixedArray[Byte], oam : FixedArray[Byte], y : Int) -> Unit

    Render scanline y (0..159) into self.framebuf.

    Ppu::reset

    fn Ppu::reset(self : Ppu) -> Unit

    SimpleBus

    pub struct SimpleBus {
    mem : FixedArray[Byte]
    base : UInt
    }

    Minimal flat RAM bus for standalone CPU unit tests.

    SimpleBus::handle_swi

    fn SimpleBus::handle_swi(_self : SimpleBus, _cpu : Cpu, _comment : UInt) -> Bool

    SimpleBus::new

    fn SimpleBus::new(size : Int, base? : UInt) -> SimpleBus

    SimpleBus::read16

    fn SimpleBus::read16(self : SimpleBus, addr : UInt) -> UInt

    SimpleBus::read32

    fn SimpleBus::read32(self : SimpleBus, addr : UInt) -> UInt

    SimpleBus::read8

    fn SimpleBus::read8(self : SimpleBus, addr : UInt) -> UInt

    SimpleBus::write16

    fn SimpleBus::write16(self : SimpleBus, addr : UInt, val : UInt) -> Unit

    SimpleBus::write32

    fn SimpleBus::write32(self : SimpleBus, addr : UInt, val : UInt) -> Unit

    SimpleBus::write8

    fn SimpleBus::write8(self : SimpleBus, addr : UInt, val : UInt) -> Unit

    TimerChannel

    pub struct TimerChannel {
    reload : UInt
    counter : UInt
    ctrl : UInt
    sub_cycles : Int
    }

    State of a single GBA hardware timer (TM0–TM3).

    TimerChannel::new

    TimerChannel::reset

    fn TimerChannel::reset(self : TimerChannel) -> Unit

    CYCLES_HBLANK

    let CYCLES_HBLANK : Int

    CYCLES_HDRAW

    let CYCLES_HDRAW : Int

    CYCLES_PER_FRAME

    let CYCLES_PER_FRAME : Int

    CYCLES_SCANLINE

    let CYCLES_SCANLINE : Int

    FLAG_C

    let FLAG_C : UInt

    FLAG_F

    let FLAG_F : UInt

    FLAG_I

    let FLAG_I : UInt

    FLAG_N

    let FLAG_N : UInt

    CPSR flag bit masks.

    FLAG_T

    let FLAG_T : UInt

    FLAG_V

    let FLAG_V : UInt

    FLAG_Z

    let FLAG_Z : UInt

    KEY_A

    let KEY_A : Int

    GBA Keypad button bit indices (active-low in KEYINPUT 0x04000130).

    KEY_B

    let KEY_B : Int

    KEY_DOWN

    let KEY_DOWN : Int

    KEY_L

    let KEY_L : Int

    KEY_LEFT

    let KEY_LEFT : Int

    KEY_R

    let KEY_R : Int

    KEY_RIGHT

    let KEY_RIGHT : Int

    KEY_SELECT

    let KEY_SELECT : Int

    KEY_START

    let KEY_START : Int

    KEY_UP

    let KEY_UP : Int

    MODE_ABT

    let MODE_ABT : UInt

    MODE_FIQ

    let MODE_FIQ : UInt

    MODE_IRQ

    let MODE_IRQ : UInt

    MODE_SVC

    let MODE_SVC : UInt

    MODE_SYS

    let MODE_SYS : UInt

    MODE_UND

    let MODE_UND : UInt

    MODE_USR

    let MODE_USR : UInt

    ARM7TDMI processor operating modes (CPSR bits 4..0).

    SCANLINES_TOTAL

    let SCANLINES_TOTAL : Int

    SCREEN_HEIGHT

    let SCREEN_HEIGHT : Int

    SCREEN_WIDTH

    let SCREEN_WIDTH : Int

    alu_add

    fn alu_add(a : UInt, b : UInt, carry_in : Bool) -> (UInt, Bool, Bool)

    Compute a + b + carry_in and return (result, carry_out, overflow).

    alu_sub

    fn alu_sub(a : UInt, b : UInt, carry_in : Bool) -> (UInt, Bool, Bool)

    Compute a - b - (1 - carry_in) via two's complement addition a + ~b + carry_in.

    asr32

    fn asr32(val : UInt, amt : Int) -> UInt

    Arithmetic shift right 32-bit unsigned value by amt (0..31).

    barrel_shift_imm

    fn barrel_shift_imm(val : UInt, shift_type : UInt, amt : Int, carry_in : Bool) -> (UInt, Bool)

    Barrel shifter for immediate-amount shifts (reg_shift = false). Returns (shifted_value, carry_out).

    barrel_shift_reg

    fn barrel_shift_reg(val : UInt, shift_type : UInt, amt : Int, carry_in : Bool) -> (UInt, Bool)

    Barrel shifter for register-specified shifts (amt = Rs & 0xFF). Returns (shifted_value, carry_out).

    bgr555_to_rgb

    fn bgr555_to_rgb(c : UInt) -> (Byte, Byte, Byte)

    Convert a 15-bit GBA BGR555 color to (r, g, b) 8-bit channels.

    disassemble_arm

    fn disassemble_arm(op : UInt, pc : UInt) -> String

    Disassemble a single 32-bit ARM instruction at address pc.

    disassemble_thumb

    fn disassemble_thumb(op : UInt, pc : UInt) -> String

    Disassemble a single 16-bit Thumb instruction at address pc.

    hex16

    fn hex16(v : UInt) -> String

    Format a 16-bit unsigned integer as 4-digit uppercase hex.

    hex32

    fn hex32(v : UInt) -> String

    Format a 32-bit unsigned integer as 8-digit uppercase hex.

    hle_swi

    fn hle_swi(bus : GbaBus, cpu : Cpu, comment : UInt) -> Bool

    Execute High-Level Emulated (HLE) GBA BIOS SWI calls. Returns true if handled via HLE.

    init_hle_bios

    fn init_hle_bios(bios : FixedArray[Byte]) -> Unit

    Initialize the minimal 16 KB HLE BIOS stub table (IRQ trampoline at 0x00000018). Contains no copyrighted code — only the standard 6-instruction ARM IRQ dispatch sequence calling the user handler pointer at 0x03FFFFFC (mirrored to IWRAM 0x03007FFC).

    ror32

    fn ror32(val : UInt, amt : Int) -> UInt

    Rotate right 32-bit unsigned value by amt (0..31).

    run_dma_channel

    fn run_dma_channel(bus : GbaBus, ch : Int, timing : UInt) -> UInt

    Execute a DMA transfer for channel ch (0..3) if enabled and matching timing (0=Imm, 1=VBlank, 2=HBlank). Returns IRQ flag bit (1 << (8 + ch)) if IRQ should be raised, else 0.

    sign_ext_28

    fn sign_ext_28(v : UInt) -> Int

    Sign-extend a 28-bit affine reference register value to 32-bit signed Int.

    tick_ppu

    fn tick_ppu(bus : GbaBus, cycles : Int) -> Bool

    Advance PPU scanline state by cycles. Triggers HBlank/VBlank/VCount IRQs and DMAs. Returns true when a full frame (VCount wraps to 0) completes.

    tick_timers

    fn tick_timers(bus : GbaBus, cycles : Int) -> Unit

    Advance all 4 hardware timers by cycles CPU cycles, handling cascade and IRQ flags.

    trigger_dma

    fn trigger_dma(bus : GbaBus, timing : UInt) -> Unit

    Trigger all enabled DMA channels matching timing.