Skip to content

Latest commit

 

History

History
168 lines (121 loc) · 6.73 KB

ARCHITECTURE.md

File metadata and controls

168 lines (121 loc) · 6.73 KB

Emulator architecture status

INSTRUCTIONS.md (and render_instructions.py) track CPU opcode coverage in isolation. This is the wider picture: everything else a Game Boy emulator needs, and which of it exists today. Status is generated by render_architecture.py — re-run it after making changes to refresh architecture-coverage.html.

Subsystems

CPU opcode decode

What it is: fetch/decode/execute for the Sharp LR35902's 245 real opcodes plus the CB-prefixed table.

Status: tracked separately — see INSTRUCTIONS.md / opcode-coverage.html (render_instructions.py). Currently 33/245 unprefixed opcodes implemented, CB table not broken out yet.

Cartridge → Bus wiring

What it is: after Cartridge::new parses the ROM into cartridge.rom (src/cartridge/cartridge.rs), those bytes need to end up somewhere the CPU can actually read them.

Status: not started. main.rs parses the cartridge and prints its header, but never copies cartridge.rom into Bus/Memory, and never even calls cpu.step(). Right now the CPU would read an all-zero 64KB array, not the game. This is the single biggest blocker to running any real ROM.

Next step: give Bus::new a cartridge (or a load_cartridge method) and route reads in 0x0000..=0x7FFF to cartridge.rom instead of Memory.

Address-mapped bus / memory map

What it is: on real hardware, different address ranges behave completely differently — ROM banks, VRAM, external RAM, work RAM, echo RAM, OAM, I/O registers, high RAM, and the interrupt-enable register are all distinct, and some (VRAM/OAM) are only accessible during certain PPU modes.

Status: not started. src/architecture/mem.rs is one flat [u8; 0xFFFF] array — every address behaves identically. Bus (bus.rs) is just a thin wrapper around that one Memory, with no per-region dispatch at all.

Next step: turn Bus into the thing that owns the cartridge, VRAM, WRAM, OAM, and I/O registers, and dispatches read_byte/store_byte by address range to the right one — this is also where the PPU, timer, and joypad registers below get wired in.

MBC / ROM & RAM bank switching

What it is: RomOnly cartridges map directly, but MBC1/MBC2/MBC3/MBC5 (all already recognized in CartridgeType, src/cartridge/c_type.rs) support writes to specific ROM addresses that switch which 16KB ROM bank (and which 8KB external RAM bank) is currently mapped in — required for any cartridge bigger than 32KB.

Status: not started, though the groundwork (decoding which MBC a cartridge uses from its header) already exists. No bank-switch registers, no bank-select write handling, no logic that changes what a ROM read resolves to.

Next step: start with MBC1 (most common) once the address-mapped bus above exists — bank switching is just special-cased writes inside that dispatch.

Interrupts (IME / IE / IF, dispatch)

What it is: five interrupt sources (VBlank, LCD STAT, Timer, Serial, Joypad) that pause the running program and jump to a fixed handler address when enabled (IE, 0xFFFF) and pending (IF, 0xFF0F) and the master switch (IME, toggled by EI/DI/RETI) is on.

Status: not started. Cpu has no ime field and no IE/IF handling. EI (0xFB), DI (0xF3), and RETI (0xD9) are still pass() placeholders in instructions_missing.rs.

Next step: add an ime: bool (and pending/enabled tracking, likely in Bus once I/O registers exist there) to Cpu, implement ei/di/reti, then add an interrupt-check-and-dispatch step to the main loop before each cpu.step().

Timer (DIV / TIMA / TMA / TAC)

What it is: a free-running divider register plus a configurable timer that increments at one of four rates and raises the Timer interrupt on overflow.

Status: not started. No timer file, no DIV/TIMA/TMA/TAC anywhere in the codebase.

Next step: a small Timer struct stepped by however many cycles each cpu.step() took (see "Main execution loop" below for why that plumbing doesn't exist yet either), wired into the bus at 0xFF04-0xFF07.

PPU (LCD / graphics)

What it is: the piece that turns VRAM tile data + OAM sprite data + scroll and palette registers into an actual 160x144 image, scanline by scanline, raising the VBlank interrupt once per frame.

Status: not started. No PPU/GPU file, no LCDC/STAT/scanline counter, no framebuffer, no way to see anything the emulator does.

Next step: usually the highest-value missing piece once the CPU/bus/timer basics work, since it's the only way to visually confirm anything is running correctly. Can start with background-only rendering before adding window and sprites.

Joypad input

What it is: reading button state through the 0xFF00 register (and raising the Joypad interrupt on a press, if enabled).

Status: not started. No joypad file; LD (C),A/LD A,(C) and the LDH forms that would touch 0xFF00 are still placeholders too.

Next step: low priority until there's a PPU to actually play against.

APU (sound)

What it is: four sound channels (two pulse, one wave, one noise) mixed to an audio buffer.

Status: not started. No APU/sound file at all.

Next step: lowest priority for getting a game visibly running; safe to defer indefinitely.

Boot ROM

What it is: the real hardware's 256-byte internal ROM that runs before a cartridge is mapped in, initializes hardware registers, and displays the scrolling logo.

Status: not started (and optional). main.rs jumps straight to loading the cartridge; there's no boot ROM loading or the memory-mapping trick real hardware uses to unmap it afterward.

Next step: skip for now — plenty of emulators fake the post-boot register state instead of running a real boot ROM.

Main execution loop

What it is: the thing that actually runs the emulator — repeatedly stepping the CPU, feeding the cycles it took to the timer and PPU, checking for pending interrupts, polling input, and pacing/presenting a frame — instead of one-shot parsing a ROM header and exiting.

Status: not started. main.rs currently only opens tetris.gb, parses and prints the header, and exits; Cpu::new() is constructed but never stepped.

Next step: this is what finally ties CPU+bus+timer+PPU+joypad together once enough of them exist — even a minimal "loop calling cpu.step() forever" is worth having early to catch dispatch bugs sooner.

Suggested order

  1. Cartridge → Bus wiring (nothing works without this)
  2. Address-mapped bus (needed by almost everything below)
  3. Finish more CPU opcodes + interrupts (see INSTRUCTIONS.md)
  4. Main execution loop (even a bare one, to start exercising the above)
  5. Timer
  6. PPU (first real payoff — you can finally see something)
  7. MBC bank switching (needed for anything bigger than a 32KB ROM)
  8. Joypad
  9. APU
  10. Boot ROM (optional, anytime)