Skip to content

mert-ergun/chip-8

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CHIP-8 Emulator

C++ SDL2 License Platform

A fast, accurate CHIP-8 interpreter/emulator written in modern C++17.


What is CHIP-8?

CHIP-8 is an interpreted programming language developed in the mid-1970s for use on 8-bit microcomputers. It was designed to allow video games to be more easily programmed for these computers. CHIP-8 programs are run on a virtual machine, making it an excellent target for emulation projects.

Specifications

Component Details
Memory 4KB (4096 bytes)
Display 64×32 pixels, monochrome
Registers 16 general-purpose 8-bit registers (V0-VF)
Stack 16 levels
Timers Delay timer, Sound timer (60Hz)
Input 16-key hexadecimal keypad
Instructions 35 opcodes, all 2 bytes long

Features

  • Complete implementation of all 35 CHIP-8 opcodes
  • Configurable display scaling
  • Adjustable emulation speed
  • Clean, well-documented codebase
  • Cross-platform support (macOS, Linux, Windows)

Building

Prerequisites

  • CMake 3.10 or higher
  • C++17 compatible compiler (GCC 7+, Clang 5+, MSVC 2017+)
  • SDL2 development libraries

Installing SDL2

macOS (Homebrew):

brew install sdl2

Ubuntu/Debian:

sudo apt-get install libsdl2-dev

Windows (vcpkg):

vcpkg install sdl2

Build Instructions

# Clone the repository
git clone https://github.com/mert-ergun/chip-8.git
cd chip-8

# Create build directory
mkdir build && cd build

# Configure and build
cmake ..
make

# The executable will be created as 'chip8'

Usage

./chip8 <scale> <delay> <rom_path>

Arguments

Argument Description Recommended
scale Display scale factor (window size = 64×scale by 32×scale) 10-20
delay Cycle delay in milliseconds (controls emulation speed) 1-5
rom_path Path to the CHIP-8 ROM file (.ch8) -

Example

./chip8 15 2 ../roms/IBMLogo.ch8

Keyboard Mapping

CHIP-8 uses a 16-key hexadecimal keypad. This emulator maps it to your keyboard as follows:

CHIP-8 Keypad           Keyboard
┌───┬───┬───┬───┐      ┌───┬───┬───┬───┐
│ 1 │ 2 │ 3 │ C │      │ 1 │ 2 │ 3 │ 4 │
├───┼───┼───┼───┤      ├───┼───┼───┼───┤
│ 4 │ 5 │ 6 │ D │  =>  │ Q │ W │ E │ R │
├───┼───┼───┼───┤      ├───┼───┼───┼───┤
│ 7 │ 8 │ 9 │ E │      │ A │ S │ D │ F │
├───┼───┼───┼───┤      ├───┼───┼───┼───┤
│ A │ 0 │ B │ F │      │ Z │ X │ C │ V │
└───┴───┴───┴───┘      └───┴───┴───┴───┘

Press ESC to exit the emulator.


Project Structure

chip-8/
├── CMakeLists.txt      # Build configuration
├── LICENSE             # MIT License
├── README.md           # This file
├── roms/               # ROM files for testing
│   └── IBM Logo.ch8    # Classic test ROM
└── src/
    ├── chip8.cpp       # CHIP-8 CPU implementation
    ├── chip8.hpp       # CHIP-8 class definition
    ├── main.cpp        # Entry point
    ├── platform.cpp    # SDL2 platform layer
    └── platform.hpp    # Platform class definition

Opcodes Reference

Click to expand full opcode table
Opcode Mnemonic Description
00E0 CLS Clear the display
00EE RET Return from subroutine
1NNN JP addr Jump to address NNN
2NNN CALL addr Call subroutine at NNN
3XKK SE Vx, byte Skip next instruction if Vx == KK
4XKK SNE Vx, byte Skip next instruction if Vx != KK
5XY0 SE Vx, Vy Skip next instruction if Vx == Vy
6XKK LD Vx, byte Set Vx = KK
7XKK ADD Vx, byte Set Vx = Vx + KK
8XY0 LD Vx, Vy Set Vx = Vy
8XY1 OR Vx, Vy Set Vx = Vx OR Vy
8XY2 AND Vx, Vy Set Vx = Vx AND Vy
8XY3 XOR Vx, Vy Set Vx = Vx XOR Vy
8XY4 ADD Vx, Vy Set Vx = Vx + Vy, VF = carry
8XY5 SUB Vx, Vy Set Vx = Vx - Vy, VF = NOT borrow
8XY6 SHR Vx Set Vx = Vx >> 1, VF = LSB
8XY7 SUBN Vx, Vy Set Vx = Vy - Vx, VF = NOT borrow
8XYE SHL Vx Set Vx = Vx << 1, VF = MSB
9XY0 SNE Vx, Vy Skip next instruction if Vx != Vy
ANNN LD I, addr Set I = NNN
BNNN JP V0, addr Jump to NNN + V0
CXKK RND Vx, byte Set Vx = random byte AND KK
DXYN DRW Vx, Vy, n Draw sprite at (Vx, Vy), height N
EX9E SKP Vx Skip if key Vx is pressed
EXA1 SKNP Vx Skip if key Vx is not pressed
FX07 LD Vx, DT Set Vx = delay timer
FX0A LD Vx, K Wait for key press, store in Vx
FX15 LD DT, Vx Set delay timer = Vx
FX18 LD ST, Vx Set sound timer = Vx
FX1E ADD I, Vx Set I = I + Vx
FX29 LD F, Vx Set I = location of sprite for digit Vx
FX33 LD B, Vx Store BCD of Vx at I, I+1, I+2
FX55 LD [I], Vx Store V0-Vx in memory starting at I
FX65 LD Vx, [I] Read V0-Vx from memory starting at I

Resources


License

This project is licensed under the MIT License - see the LICENSE file for details.


Made with coffee and C++

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors