Skip to content

dllen/c-impl-nes-emulator

Repository files navigation

NES Emulator in C

A Nintendo Entertainment System (NES) emulator written in C using SDL2.

Features

  • 6502 CPU: Full instruction set implementation with all addressing modes
  • PPU (2C02): Basic PPU structure with register handling
  • Cartridge Loading: iNES format support with Mapper 0 (NROM)
  • Controller Input: NES controller emulation with keyboard mapping
  • SDL2 Graphics: Window and rendering system

Building

Prerequisites

  • GCC compiler
  • SDL2 development libraries
  • Make

On macOS:

brew install sdl2

Compile

make

On Apple Silicon Macs, the build uses x86_64 architecture:

arch -x86_64 make

Running

./bin/nes_emu <path_to_rom.nes>

Example:

./bin/nes_emu roms/game.nes

Tools

NES ROM Analyzer

Analyze NES ROM files and display detailed information:

./bin/nes_info <rom.nes>

Features:

  • iNES header parsing
  • Mapper identification
  • ROM size information
  • Interrupt vectors
  • CHR-ROM analysis
  • Visual pattern display

Example output:

╔════════════════════════════════════════╗
║       NES ROM Analysis Tool            ║
╚════════════════════════════════════════╝

File: Tetris.nes

=== iNES Header Information ===
Magic: NES ✓ Valid

--- ROM Sizes ---
PRG-ROM: 8 x 16KB = 128KB
CHR-ROM: 16 x 8KB = 128KB

--- Mapper ---
Mapper Number: 65
Mapper Name: Irem H3001

Testing

Run unit tests:

make test

Tests verify:

  • CPU operations
  • Mapper 65 bank switching
  • PPU palette/nametable
  • Pattern extraction

Project Structure

.
├── Makefile
├── include/
│   ├── common.h      # Common type definitions
│   ├── bus.h         # Memory bus interface
│   ├── cpu.h         # 6502 CPU interface
│   ├── ppu.h         # PPU interface
│   └── cartridge.h   # ROM cartridge interface
└── src/
    ├── main.c        # Entry point and SDL loop
    ├── bus.c         # Memory mapping
    ├── cpu.c         # 6502 CPU implementation
    ├── ppu.c         # PPU implementation
    └── cartridge.c   # ROM loading

Implementation Details

CPU (MOS 6502)

  • All official opcodes implemented
  • 12 addressing modes
  • Interrupt handling (NMI, IRQ, RESET)
  • Cycle-accurate timing

PPU (Picture Processing Unit)

  • Basic register implementation ($2000-$2007)
  • Scanline and cycle tracking
  • VBlank NMI generation
  • Frame buffer output

Cartridge

  • iNES format parsing
  • Mapper 0 (NROM) support
  • 16KB/32KB PRG-ROM
  • 8KB CHR-ROM/RAM

Controls

The emulator maps keyboard keys to NES controller buttons:

NES Button Keyboard Key
A X
B Z
Start Enter
Select Space
Up
Down
Left
Right

Current Limitations

  • Only Mapper 0 (NROM) is supported
  • No APU (Audio Processing Unit)
  • PPU rendering is not fully implemented
  • No sprite or background rendering

Next Steps

To make this a fully functional emulator:

  1. PPU Rendering: Implement background and sprite rendering
  2. More Mappers: Implement additional mappers (MMC1, MMC3, etc.)
  3. APU: Add audio support
  4. Save States: Implement save/load functionality

License

This is an educational project for learning NES emulation.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors