Chip8Python
An XO Chip, Super Chip, and Chip 8 emulator written in Python
Install / Use
/learn @craigthomas/Chip8PythonREADME
Yet Another (Super) Chip 8 Emulator
An Octo compatible XO Chip, Super Chip, and Chip 8 emulator.
Table of Contents
What is it?
This project is a Chip 8 emulator written in Python 3. The original purpose of the project was to create a simple learning emulator that was well documented and coded in terms that were easy to understand. It was also an exercise to learn more about Python. The result is a simple command-line based Chip 8 emulator.
In addition to supporting Chip 8 ROMs, the emulator also supports the XO Chip and Super Chip specifications. Note that while there are no special flags that are needed to run an XO Chip, Super Chip, or normal Chip 8 ROM, there are other compatibility flags that may need to be set for the ROM to run properly. See the Quirks Modes documentation below for more information.
There are two other versions of the emulator written in different languages:
License
This project makes use of an MIT style license. Please see the file called LICENSE.
Installing
Copy the source files to a directory of your choice. In addition to the source, you will need the following required software packages:
I strongly recommend creating a virtual environment using the virtualenv builder as well as the virtualenvwrapper tools. With these tools, you can easily create a virtual sandbox to install pygame and run the emulator in, without touching your master Python environment.
Ubuntu Installation
The installation under Ubuntu 20.04 requires several different steps:
-
Install SDL libraries. The SDL (Simple DirectMedia Layer) libraries are used by PyGame to draw images on the screen. Several other dependencies are needed by SDL in order to install PyGame. To install the required SDL libraries (plus dependencies) from the command-line:
sudo apt install python3 python3-dev libsdl-dev libfreetype6-dev \ libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev libsdl-sound1.2-dev \ libportmidi-dev -
Install PIP. The
pippackage manager is used for managing Python packages. To installpipfrom the command-line:sudo apt install python3-pip -
(Optional) Install virtual environment support for Python:
-
Install virtual environment support:
pip3 install virtualenv pip3 install virtualenvwrapper -
First you must update your
.bashrcfile in the home directory and add a few lines to the bottom of that file:cat >> ~/.bashrc << EOF export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 export WORKON_HOME=$HOME/.virtualenvs export PATH=$PATH:$HOME/.local/bin source $HOME/.local/bin/virtualenvwrapper.sh EOF -
Next you must source the
.bashrcfile:source ~/.bashrc -
Finally, you can create the environment:
mkvirtualenv chip8
-
-
Clone (or download) the Chip 8 emulator project:
sudo apt install git git clone https://github.com/craigthomas/Chip8Python.git -
Install the requirements from the project:
pip install -r requirements.txt
Windows Installation
-
Download and install Python 3.6.8 for Windows. Make sure that
pipandAdd python.exe to Pathoptions are checked when performing the installation. Later versions of Python 3 are also likely to work correctly with the emulator. -
(Optional) Install virtual environment support for Python. Run the following commands from a command prompt:
- Install the virtual environment wrapper:
pip install virtualenv pip install virtualenvwrapper-win-
Create a new environment for the Chip 8 emulator:
mkvirtualenv chip8
-
Install Git for Windows.
-
Clone (or download) the source files from GitHub. Run the following commands in a command prompt window:
git clone https://github.com/craigthomas/Chip8Python.git -
Install the requirements for the project. Run the following commands in a command prompt window in the directory where you cloned or downloaded the source files:
pip install -r requirements.txt
Running
Running a ROM
Note that if you created a virtual environment as detailed above,
you will need to workon that environment before starting the emulator:
workon chip8
The command-line interface requires a single argument, which is the full path to a Chip 8 ROM. Run the following command in the directory where you cloned or downloaded the source files:
python yac8e.py /path/to/rom/filename
This will start the emulator with the specified ROM.
Screen Scale
The --scale switch will scale the size of the window (the original size at 1x
scale is 64 x 32):
python yac8e.py /path/to/rom/filename --scale 10
The command above will scale the window so that it is 10 times the normal size.
Instructions Per Second
The --ticks switch will limit the number of instructions per second that the
emulator is allowed to run. By default, the value is set to 1,000. Minimum values
are 200. Use this switch to adjust the running time of ROMs that execute too quickly.
For simplicity, each instruction is assumed to take the same amount of time.
python yac8e.py /path/to/rom/filename --ticks 2000
Quirks Modes
Over time, various extensions to the Chip8 mnemonics were developed, which resulted in an interesting fragmentation of the Chip8 language specification. As discussed in Octo's Mastering SuperChip documentation, one version of the SuperChip instruction set subtly changed the meaning of a few instructions from their original Chip8 definitions. This change went mostly unnoticed for many implementations of the Chip8 langauge. Problems arose when people started writing programs using the updated language model - programs written for "pure" Chip8 ceased to function correctly on emulators making use of the altered specification.
To address this issue, Octo implements
a number of quirks modes so that all Chip8 software can run correctly,
regardless of which specification was used when developing the Chip8 program.
This same approach is used here, such that there are several quirks flags
that can be passed to the emulator at startup to force it to run with
adjustments to the language specification.
Additional quirks and their impacts on the running Chip8 interpreter are examined in great depth at Chromatophore's HP48-Superchip repository. Many thanks for this detailed explanation of various quirks found in the wild!
Shift Quirks
The --shift_quirks flag will change the way that register shift operations work.
In the original language specification two registers were required: the
destination register x, and the source register y. The source register y
value was shifted one bit left or right, and stored in x. For example,
shift left was defined as:
Vx = Vy << 1
However, with the updated language specification, the source and destination
register are assumed to always be the same, thus the y register is ignored and
instead the value is sourced from x as such:
Vx = Vx << 1
Index Quirks
The --index_quirks flag controls whether post-increments are made to the index register
following various register based operaitons. For load (Fn65) and store (Fn55) register
operations, the original specification for the Chip8 language results in the index
register being post-incremented by the number of registers stored. With the Super
Chip8 specification, this behavior is not always adhered to. Setting --index_quirks
will prevent the post-in
