How dwedit/pocketnes works
Project Type and Purpose
• PocketNES is an embedded emulator: it implements the NES’s 6502-based CPU, PPU (graphics), APU (sound), memory mappers, and UI layers to run NES ROMs on Game Boy Advance hardware.
File Structure
• Top level:
• nespack/ – Windows executables for packing ROMs (tooling, not on‐GBA runtime)
• Makefile / Makefile_mb – build scripts targeting GBA toolchain
• README.md – brief project description
• src/ – emulator source:
• CPU core (6502.s, 6502mac.h)
• Memory subsystem (memory.s, mem.s, cache.c/.h)
• Cartridge loader (loadcart.c/.h) & mappers/ – one .s file per mapper
• PPU (ppu.s), APU (sound.s), interrupt dispatcher (interruptdispatcher.s)
• UI & menus (ui.c/.h, rommenu.c/.h, cheat.c/.h)
• Save/load state (savestate*.c/.h), SRAM (sram.c/.h)
• Speed hacks & deferred calls (new_speed_hack.c/.h, deferred_calls.s)
• Compression lib (minilzo.107/ & minilzo.c/.h)
• GBA startup (gba_crt0_my.s, gba_my.specs, gba_cart_my.ld, etc.)
README
• Confirms: “NES emulator for GBA.” No further architecture notes; all detail resides in the file tree.
Main Components & Interactions
A. GBA Hardware Layer (external)
• Video I/O, Sound hardware, Input buttons, Cartridge interface, SRAM
B. GBA Runtime/Startup
• crt0, linker script, specs → initializes hardware, sets up stack, calls main()
C. Cartridge Loader & NES ROM Storage
• loadcart module reads ROM from flash into emulator memory
D. Memory System & Mapper Layer
• memory.s + mem.c + cache.c + mappers/*
• Handles CPU memory reads/writes, bank switching, mirroring
E. CPU Core (6502)
• 6502.s – fetch/decode/execute, uses asmcalls.h for C integration
F. PPU (Picture Processing Unit)
• ppu.s – renders scanlines, accesses VRAM, writes to GBA frame buffer
G. APU (Audio Processing Unit)
• sound.s – generates audio channels, outputs via GBA sound registers
H. Interrupt & Timing Dispatcher
• interruptdispatcher.s, interrupt.c – synchronizes PPU, APU, DMA, timers
I. UI & Menus
• ui.c, rommenu.c, cheat.c – on-screen menus, cheat toggles, text rendering (font.lz77)
J. Persistence & State
• savestate2.c, sram.c – save/load snapshots, battery-backed SRAM
K. Utilities & Libraries
• Compression (minilzo) for assets, font decompression
• Speed hacks to accelerate hot loops
Architectural Patterns & Principles
• Layered design: low-level CPU/PPU/APU in assembly for performance, high-level UI in C
• Modular mappers allow adding support for new cartridge hardware by dropping in .s files
• Clear separation: hardware abstraction (GBA) vs. emulator core (NES)