Skip to main content

C/C++ NES emulator with Python bindings

Project description

cynes - C/C++ NES emulator with Python bindings

cynes is a lightweight multiplatform NES emulator providing a simple Python interface. The core of the emulation is based on the very complete documentation provided by the Nesdev Wiki. The current implementation consists of

  • A cycle-accurate CPU emulation
  • A cycle-accurate PPU emulation
  • A cycle-accurate APU emulation (even though it does not produce any sound)
  • Few basic NES mappers (more to come)

The Python bindings allow to interact easily with one or several NES emulators at the same time, ideal for machine learning application.

Installation

cynes can be installed using pip :

pip install cynes

It can also be built from source using (requires cmake) :

python setup.py build

How to use

A cynes NES emulator can be created by instanticiating a new NES object. The following code is the minimal code to run a ROM file.

from cynes.windowed import WindowedNES

# We initialize a new emulator by specifying the ROM file used
with WindowedNES("rom.nes") as nes:
    # While the emulator should not be closed, we can continue the emulation
    while not nes.should_close:
        # The step method run the emulation for a single frame
        # It also returns the content of the frame buffer as a numpy array
        frame = nes.step()

Multiple emulators can be created at once by instantiating several NES objects.

Windowed / Headless modes

The default NES class run in "headless" mode, meaning that no rendering is performed. A simple wrapper around the base emulator providing a basic renderer and input handling using SDL2 is present in the windowed submodule.

from cynes import NES
from cynes.windowed import WindowedNES

# We can create a NES emulator without a rendering window
nes_headless = NES("rom.nes")

while not nes_headless.has_crashed:
    frame = nes_headless.step()

# And with the rendering window
nes_windowed = WindowedNES("rom.nes")

while not nes_windowed.should_close:
    frame = nes_windowed.step()

While the rendering overhead is quite small, running in headless mode can improve the performances when the window is not needed. The content of the frame buffer can always be accessed using the step method.

Controller

The state of the controller can be directly modified using the following syntax :

from cynes import *

# Simple input
nes.controller = NES_INPUT_RIGHT

# Multiple button presses at once
nes.controller = NES_INPUT_RIGHT | NES_INPUT_A

# Chaining multiple button presses at once
nes.controller = NES_INPUT_START
nes.controller |= NES_INPUT_B
nes.controller |= NES_INPUT_SELECT

# Undefined behavior
nes.controller = NES_INPUT_RIGHT | NES_INPUT_LEFT
nes.controller = NES_INPUT_DOWN | NES_INPUT_UP

# Run the emulator with the specified controller state for 5 frames
nes.step(frames=5)

Note that the state of the controller is maintained even after the step method is called. This means that it has to be reset to 0 to release the buttons.

Two controllers can be used at the same time. The state of the second controller can be modified by updating the 8 most significant bits of the same variable.

# P1 will press left and P2 will press the right button
nes.controller = NES_INPUT_LEFT | NES_INPUT_RIGHT << 8

Key handlers

Key handlers are a simple way of associating custom actions to shortcuts. This feature is only present with the windowed mode. The key events (and their associated handlers) are fired when calling the step method.

# Disable the default window controls
nes = WindowedNES("rom.nes", default_handlers=False)

# Custom key handlers can be defined using the register method
import sdl2

def kill():
    nes.close()

nes.register(sdl2.SDL_SCANCODE_O, kill)

By default, the emulator comes with key handlers that map window keys to the controller buttons. The mapping is the following :

  • the arrow keys for the D-pad
  • the keys X and Z for the A and B buttons respectively
  • the keys A and S for the SELECT and START buttons respectively

Save states

The state of the emulator can be saved as a numpy array and later be restored.

# The state of the emulator can be dump using the save method
save_state = nes.save()

# And restored using the load method
nes.load(save_state)

Memory modification should never be performed directly on a save state, as it is prone to memory corruption. Theses two methods can be quite slow, therefore, they should be called sparsely.

Memory access

The memory of the emulator can be read from and written to using the following syntax :

# The memory content can be accessed as if the emulator was an array
player_state = nes[0x000E]

# And can be written in a similar fashion
nes[0x075A] = 0x8

Note that only the CPU RAM $0000 - $1FFFF and the mapper RAM $6000 - $7FFF should be accessed. Trying to read / write a value to other addresses may desynchronize the components of the emulator, resulting in a undefined behavior.

Closing

An emulator is automatically closed when the object is released by Python. In windowed mode, the close method can be used to close the window without having to wait for Python to release the object. As presented previously, the WindowedNES can also be used as a context manager, which will call close automatcially when exiting the context. It can also be closed manualy using the close method.

# In windowed mode, this can be used to close the window
nes.close()

# Deleting the emulator in windowed mode also closes the window
del nes

# The method should_close indicates whether or not the emulator function should be called
nes.close()
nes.should_close # True

When the emulator is closed, but the object is not deleted yet, the should_close property will be set to True, indicating that calling any NES function will not work properly. This method can also return True in two other cases :

  • When the CPU of the emulator is frozen. When the CPU hits a JAM instruction (illegal opcode), it is frozen until the emulator is reset. This should never happen, but memory corruptions can cause them, so be careful when accessing the NES memory.
  • In windowed mode, when the window is closed or when the ESC key is pressed.

License

This project is licensed under GPL-3.0

cynes - C/C++ NES emulator with Python bindings
Copyright (C) 2021 - 2024 Combey Theo

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cynes-0.1.0.tar.gz (20.3 kB view hashes)

Uploaded Source

Built Distributions

cynes-0.1.0-cp312-cp312-win_amd64.whl (100.6 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

cynes-0.1.0-cp312-cp312-win32.whl (90.9 kB view hashes)

Uploaded CPython 3.12 Windows x86

cynes-0.1.0-cp312-cp312-musllinux_1_1_x86_64.whl (659.3 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ x86-64

cynes-0.1.0-cp312-cp312-musllinux_1_1_i686.whl (721.7 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ i686

cynes-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (135.1 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

cynes-0.1.0-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl (145.5 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ i686

cynes-0.1.0-cp312-cp312-macosx_11_0_arm64.whl (96.2 kB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

cynes-0.1.0-cp312-cp312-macosx_10_9_x86_64.whl (100.8 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ x86-64

cynes-0.1.0-cp311-cp311-win_amd64.whl (100.5 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

cynes-0.1.0-cp311-cp311-win32.whl (90.8 kB view hashes)

Uploaded CPython 3.11 Windows x86

cynes-0.1.0-cp311-cp311-musllinux_1_1_x86_64.whl (660.6 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

cynes-0.1.0-cp311-cp311-musllinux_1_1_i686.whl (722.6 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ i686

cynes-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (136.0 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

cynes-0.1.0-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl (146.2 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ i686

cynes-0.1.0-cp311-cp311-macosx_11_0_arm64.whl (96.7 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

cynes-0.1.0-cp311-cp311-macosx_10_9_x86_64.whl (101.2 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

cynes-0.1.0-cp310-cp310-win_amd64.whl (99.5 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

cynes-0.1.0-cp310-cp310-win32.whl (89.9 kB view hashes)

Uploaded CPython 3.10 Windows x86

cynes-0.1.0-cp310-cp310-musllinux_1_1_x86_64.whl (659.5 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

cynes-0.1.0-cp310-cp310-musllinux_1_1_i686.whl (721.6 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ i686

cynes-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (134.3 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

cynes-0.1.0-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl (145.1 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686

cynes-0.1.0-cp310-cp310-macosx_11_0_arm64.whl (95.4 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

cynes-0.1.0-cp310-cp310-macosx_10_9_x86_64.whl (100.0 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

cynes-0.1.0-cp39-cp39-win_amd64.whl (99.0 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

cynes-0.1.0-cp39-cp39-win32.whl (90.1 kB view hashes)

Uploaded CPython 3.9 Windows x86

cynes-0.1.0-cp39-cp39-musllinux_1_1_x86_64.whl (659.9 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

cynes-0.1.0-cp39-cp39-musllinux_1_1_i686.whl (722.0 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ i686

cynes-0.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (134.8 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

cynes-0.1.0-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl (145.5 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686

cynes-0.1.0-cp39-cp39-macosx_11_0_arm64.whl (95.5 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

cynes-0.1.0-cp39-cp39-macosx_10_9_x86_64.whl (100.1 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

cynes-0.1.0-cp38-cp38-win_amd64.whl (99.4 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

cynes-0.1.0-cp38-cp38-win32.whl (89.9 kB view hashes)

Uploaded CPython 3.8 Windows x86

cynes-0.1.0-cp38-cp38-musllinux_1_1_x86_64.whl (659.5 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

cynes-0.1.0-cp38-cp38-musllinux_1_1_i686.whl (721.5 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ i686

cynes-0.1.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (134.3 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

cynes-0.1.0-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl (144.9 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686

cynes-0.1.0-cp38-cp38-macosx_11_0_arm64.whl (95.3 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

cynes-0.1.0-cp38-cp38-macosx_10_9_x86_64.whl (99.9 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

cynes-0.1.0-cp37-cp37m-win_amd64.whl (98.6 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

cynes-0.1.0-cp37-cp37m-win32.whl (89.6 kB view hashes)

Uploaded CPython 3.7m Windows x86

cynes-0.1.0-cp37-cp37m-musllinux_1_1_x86_64.whl (661.2 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

cynes-0.1.0-cp37-cp37m-musllinux_1_1_i686.whl (723.8 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ i686

cynes-0.1.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (135.6 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

cynes-0.1.0-cp37-cp37m-manylinux_2_17_i686.manylinux2014_i686.whl (147.0 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ i686

cynes-0.1.0-cp37-cp37m-macosx_10_9_x86_64.whl (98.3 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

cynes-0.1.0-cp36-cp36m-win_amd64.whl (110.1 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

cynes-0.1.0-cp36-cp36m-win32.whl (101.1 kB view hashes)

Uploaded CPython 3.6m Windows x86

cynes-0.1.0-cp36-cp36m-musllinux_1_1_x86_64.whl (672.6 kB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ x86-64

cynes-0.1.0-cp36-cp36m-musllinux_1_1_i686.whl (735.2 kB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ i686

cynes-0.1.0-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (146.9 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64

cynes-0.1.0-cp36-cp36m-manylinux_2_17_i686.manylinux2014_i686.whl (158.5 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ i686

cynes-0.1.0-cp36-cp36m-macosx_10_9_x86_64.whl (109.4 kB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page