A simple emulator for the CHIP-8 interpreted programming language.

Overview

CHIP-8 Emulator

A simple emulator for the CHIP-8 interpreted programming language written in C (SDL for graphics).

About

This is a really basic emulator written for educational purposes, the code is meant to be readable and clear, with a lot of comments. I am a beginner in this field and this is my first attempt to emulators. I have implemented this project mainly by following the Wikipedia page and this awesome resource; I had no experience with SDL either so I have helped myself during that process with various resources online. The most important part to me was understanding the architecture itself, not the graphics.

Requirements

This project has been developed under a Linux system.

  • SDL library
  • C compiler

Run

make

./bin/emulator.out <game_rom_path>

Or you could use the test_emu script I wrote to automate the process of testing the program.

./test_emu.sh

Quick Walkthrough

By reading here:

Chip-8 is a simple, interpreted, programming language which was first used
on some do-it-yourself computer systems in the late 1970s and early 1980s.
The COSMAC VIP, DREAM 6800, and ETI 660 computers are a few examples.
These computers typically were designed to use a television as a display
had between 1 and 4K of RAM, and used a 16-key hexadecimal keypad for input.
The interpreter took up only 512 bytes of memory, and programs, which were entered into the computer in hexadecimal, were even smaller

So this emulator replicates this behaviour by creating a virtual machine with the following specs:

  • 4096 Bytes of memory
  • 16 general 8-bit purpose registers
  • A special 16-bit register
  • ProgramCounter and StackPointer pseudo registers
  • The stack (array of 16 16-bit values)
  • The keypad (adapted to modern keyboards)
  • 64x32 pixels monochrome screen
  • Delay timer
  • Sound timer

The emulator is then able to run .ch8 games. Please note that I have written only the emulator itself, not the games (freely accessible on the Internet).

The emulator logic consits of parsing and executing the instructions coming from those games.

scr_1

The default mode is debug mode, which outputs a lot of stuff to the screen, if you want to disable it you have to set the int DEBUG = 1; variable to 0 in chip8.c.

Improvements

  • Sound support
    • I was lazy and focused more on the interpreter so there's no sound here
  • Use function pointers instead of big switch statements.

References

Another good article

Improvements

Comments
  • Many bugs at main.c lines 20 and 25

    Many bugs at main.c lines 20 and 25

    At line 20 of main.c, check_rom() is called. Soon after, load_rom() is called at line 25. This causes several problems:

    check_rom() fopen()s a file and doesn't close it before returning, causing the open FILE to leak. Additionally, it unnecessarily makes any further attempt at opening the file implementation-defined behavior, although I don't know of a C implementation where opening the same file multiple times doesn't just work.

    The load_rom() call also fopen()s the same file, without any checking of return values. A non-existing file is not the only cause for an error return of fopen().

    Even if the above issues are fixed, there is an inherent TOCTTOU error in doing this. There is no guarantee that the file will still exist by the time load_rom() is called.

    The fix is to get rid of the check_rom() function and check the return value of fopen() in load_rom().

    bug fixed 
    opened by guijan 2
  • overhaul Makefile

    overhaul Makefile

    Hi again. Someone in the thread at https://www.reddit.com/r/C_Programming/comments/lcgwj3/a_simple_and_beginner_friendly_chip8_emulator/ provided an overhauled Makefile.

    I made a few small changes to their suggestion:

    • The .POSIX special target was added, now the makefile is explicitly POSIX-compliant.
    • CFLAGS, LDFLAGS, and LDLIBS are appended to MYCFLAGS, MYLDFLAGS, and MYLDLIBS respectively, these variables are then used by the C compiler. This allows appending extra compiler flags in pure POSIX make, no GNU extensions.
    • /usr/local/include is added to the list of paths to search for includes. /usr/local/lib is searched for libraries, so I thought this would match the original intent. This change also fixes the build on OpenBSD.

    Now making debug or optimized builds is easy: $ make CFLAGS='-g' Would build this program with debug symbols without touching the source code for instance.

    opened by guijan 1
  • use memcpy to copy memory

    use memcpy to copy memory

    Hi, I'm a beginner C programmer myself and I'm sending a minor patch to test the waters.

    It's a bit silly to use a for loop to copy memory when we have memcpy. This patch also gets rid of the magic number 80, the sizeof operator is the right tool for this job.

    opened by guijan 1
  • Improving load_rom()

    Improving load_rom()

    As suggested by a oh5nxo on Reddit, the function load_rom() could be improved in the following way:

    void load_rom(char* filename) {
        FILE* fp = fopen(filename, "rb");
    
        // read program size into memory
        fread(memory + 0x200, 1, sizeof(memory) - 0x200, fp);
    
        fclose(fp);
    }
    

    From a quick first test, it seems good. Waiting to update to give it a better look.

    enhancement good first issue 
    opened by f0lg0 1
  • Fixed 0xFX55 implementation

    Fixed 0xFX55 implementation

    I found a mistake in your implementation for the OP code 0xFX55

    As it is stated in the Cogwood reference:

    Fx55 - LD [I], Vx Store registers V0 through Vx in memory starting at location I.

    The interpreter copies the values of registers V0 through Vx into memory, starting at the address in I.

    http://devernay.free.fr/hacks/chip8/C8TECH10.HTM#0.1

    opened by khengari77 0
  • A mistake in implementing 0xFX55

    A mistake in implementing 0xFX55

    ` // FX55: Stores V0 through Vx (Vx included) in memory starting // at addr I. case 0x0055: debug_print("[OK] 0x%X: FX55\n", op);

                    for (int i = 0; i <= x; i++) {
                        V[i] = memory[I + i];
                    }
    
                    pc += 2;
                    break;
    

    `

    shouldn't this segment of code be like this:

    `// FX55: Stores V0 through Vx (Vx included) in memory starting // at addr I. case 0x0055: debug_print("[OK] 0x%X: FX55\n", op);

                    for (int i = 0; i <= x; i++) {
                        memory[I + i] = V[i];
                    }
    
                    pc += 2;
                    break;`
    
    opened by khengari77 0
Owner
Leonardo Folgoni
Trying to emulate life in C
Leonardo Folgoni
Dolphin is an emulator for running GameCube and Wii games on Windows, Linux, macOS, and recent Android devices.

Dolphin is a GameCube / Wii emulator, allowing you to play games for these two platforms on PC with improvements.

Dolphin Emulator 9.4k Dec 31, 2022
Dolphin |MMJR| is a Gamecube/Wii Emulator for Android devices; based on Dolphin MMJ source code which is aimed at pure performance.

Dolphin |MMJR| An Android-only performance-focused Dolphin (Official) fork, continued from the Dolphin MMJ source code by Weihuoya. This version is me

null 291 Dec 28, 2022
A PSOBB Server Emulator Suite for PC PSOBB players

Tethealla Tethealla PSOBB server for linux. Installation If you are opting for database usage, MySQL devel libraries are required along with your stan

Kotori 2 Nov 15, 2022
zMonkey is an open-source 200G network impairment emulator tool

zMonkey is an open-source 200G network impairment emulator tool to emulate the real-world WAN/DC conditions for your applications. it can supp

Mie~~~ 17 Nov 11, 2022
Simple server and client using python socket and declarative programming

Socket-programming Simple server and client using python socket and declarative programming How to use? open cmd and navigate to the location of the s

MAINAK CHAUDHURI 24 Dec 17, 2022
The Beginner's Guide to eBPF Programming for Networking

The Beginner's Guide to eBPF Programming for Networking As seen at Cloud Native eBPF Day 2021. Setup Create a container that we can issue curl request

Liz Rice 79 Dec 23, 2022
As a Teaching Assistant, this is a sample project about socket programming for my teaching in a capstone course in NTUST(National Taiwan University of Science and Technology)

socket-programming As a Teaching Assistant, this is a sample project about socket programming for my teaching in a capstone course in NTUST(National T

Chang Wei 2 Oct 26, 2021
A beginner friendly Client-Server program in C. Socket Programming in C

basic-client-server Program on basic client-server connection. This is intended to whomever it may be useful. If you use this somewhere else, proper r

Abdul Samad 1 Oct 21, 2021
簡單的 socket programming 入門筆記。

socket programming socket 本質上是一種 IPC (Inter-Process Communication) 的技術,用於兩個或多個 process 進行資料交換或者通訊。 在網路領域,socket 著重的不是同一台主機間 process 的通訊,而是不同主機執行的 proc

davidleitw 26 Nov 24, 2022
A toy implementation of socket programming for Lean 4.

Lean4-Socket A toy implementation of socket programming for Lean 4. Installation Lake import Lake open System Lake DSL package foo where dependenci

王虛白 18 Jul 21, 2022
The program shows how bluetooth devices are connected, without using socket programming

The program shows how bluetooth devices are connected, without using socket programming, it shows how files are shared using principles of OOP

SAKSHI JAIN 2 Oct 5, 2022
WAFer is a C language-based software platform for scalable server-side and networking applications. Think node.js for C programmers.

WAFer WAFer is a C language-based ultra-light scalable server-side web applications framework. Think node.js for C programmers. Because it's written i

Riolet Corporation 693 Dec 6, 2022
OceanBase Client for C. A driver to connect applications developed in C language to OceanBase Database.

Oceanbase Client for C OceanBase Client for C is a driver used to connect applications developed in C to OceanBase Database Server. Compatibility Serv

OceanBase 22 Nov 8, 2022
A language server protocol implementation

A barebone LSP implementation Starting point for server implementations of the LSP protocol. Provides the infrastructure to easily hook in functionali

Henner Zeller 5 Jan 7, 2023
About Add any Program in any language you like or add a hello world Program ❣️ if you like give us ⭐

Hello-World About Add any Program in any language you like or add a hello world Program ❣️ if you like give us ⭐ Give this Project a Star ⭐ If you lik

Lokesh Jangid 15 Oct 28, 2022
Fast IP validator with C language as a python module

fipv fipv (fast ip validator) is a python package build with C language. Simply it includes basic IP address validator functions as a C function, so w

Erdoğan Yoksul 14 Oct 21, 2022
Cmake-language-server - CMake LSP Implementation

cmake-language-server CMake LSP Implementation. Alpha Stage, work in progress. Features Builtin command completion Documentation for commands and vari

Regen 198 Jan 9, 2023
Apache Thrift is a lightweight, language-independent software stack for point-to-point RPC implementation

Apache Thrift Introduction Thrift is a lightweight, language-independent software stack for point-to-point RPC implementation. Thrift provides clean a

The Apache Software Foundation 9.5k Jan 7, 2023
Unofficial language server for Luau.

Luau language server Disclaimer: this project is unofficial! Extensions powered by lls vscode-lls (in development) Architecture Because Lua and by ext

Alexander McCord 4 May 12, 2022