CHIP-8 implementation in Rust targeting AVR microcontrollers

Overview

CHIP-8 implementation for a very simple breadboard toy

This is a CHIP-8 virtual machine implementation running on AVR.

Its big party trick is that it is written in Rust: it is the first non-trivial Rust application running on AVR. You can read about its development process in my blog post. The CHIP-8 VM itself is implemented in a separate, portable crate written in idiomatic Rust fashion, heavily using algebraic data types and pattern matching; this crate can then be used both to build an SDL-based desktop app and also this crate running on AVR microcontrollers.

The intended hardware is a simple circuit with very few components:

  • AVR ATMega328P microcontroller
  • PCD8522 84x48 monochrome LCD
  • Microchip 23K640 serial RAM
  • 4x4 keypad
  • 10K resistors (4 pcs)
  • 10K trimpot

All of these components come in throughhole versions so it is very easy to build it on a breadboard. NOTE THE RAM CHIP DOESN'T SUPPORT 5 Volts. The board is meant to be powered at 3.3 Volts. An Arduino Uno is going to fry the RAM chip.

Schematics Photo of breadboard version

Building

AVR support in Rust is not yet available its mainline version, and there are some questionable hacks that I had to add to work around some compiler bugs and missing features. All this means you'll need to build very specific versions of LLVM and Rust to be able to compile this crate. Also, because of some remaining bugs in the LLVM AVR backend, Rust's stock libcore cannot be compiled yet; we need to use Xargo to link to a slightly stripped down version.

1. Build branch of LLVM with AVR support + kludges

$ git clone -b avr-rust-demo https://github.com/avr-rust/llvm.git llvm-avr
$ mkdir -p build/llvm
$ cd build/llvm
$ cmake ../../llvm-avr -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=AVR -DLLVM_TARGETS_TO_BUILD=X86
$ make
$ cd ../...

2. Build branch of rustc with AVR support

$ git clone -b avr-support https://github.com/avr-rust/rust.git rust-avr
$ mkdir -p build/rust
$ cd build/rust
$ ../../rust-avr/configure --llvm-root=$(realpath ../llvm)
$ make
$ cd ../..

3. Register freshly-built Rust AVR toolchain with rustup (needed to work around a Xargo bug)

$ rustup toolchain link avr-toolchain $(realpath build/rust/build/x86_64-unknown-linux-gnu/stage1
$ rustup default avr-toolchain

4. Build chip8-avr and all its dependencies using Xargo

$ git clone https://github.com/gergoerdi/rust-avr-chip8-avr chip8-avr
$ cd chip8-avr
$ sh build.sh

Running

The above process will result in the AVR ELF executable target/avr-atmega328p/release/chip8-avr.elf. This executable can be uploaded to the ATMega328P via an AVR programmer; or if you use something like an Arduino Pro 3.3V or an Adafruit Trinket Pro 3.3V, you can upload it directly via USB.

Another way of trying it out is simulation: I've implemented a SimAVR-based simulator for the above schematics that almost runs in real time, as an interactive SDL app.

What's next?

There is no shortage of Rust and LLVM bugs and missing features in the AVR backends, if you want to help out.

You might also like...
CHIP-8 emulator/interpreter in Rust

rCHIP-8 A CHIP-8 interpreter/emulator written in Rust. Currently the interpreter only runs on UNIX terminals because of the method used to control the

A CHIP-8 emulator/interpreter in Rust
A CHIP-8 emulator/interpreter in Rust

rchip8 A CHIP-8 emulator/interpreter in Rust To run the emulator just type "cargo run -- -d". It will run a simple inline program in main. Commandlin

My attempt at making a chip-8 emulator

ate-chip My attempt at making a chip-8 emulator Credits Here are some of the things that I used for reference while building this http://devernay.free

Emulator of CHIP-8

CHIP-8-Emulator This emulator supports CHIP-8 and SUPERCHIP games. Installation Since Sdl2 is used, you'll need the Sdl2 runtime library for you syste

A CHIP-8 emulator

Ruschip - A CHIP-8 Emulator written in Rust Features Supports multiple CHIP-8 variants (planned not added) Supports most--if not all--of the quirks, a

🍊 WIP: Yet another implementation of MikanOS for aarch64 CPUs, written in Rust.

🍊 mikan Yet another implementation of MikanOS for aarch64 CPUs, written in Rust. MikanOS (uchan-nos/mikanos) was originally created by @uchan-nos, wh

NES emulator written in Rust to learn Rust

OxideNES A NES emulator in Rust. CPU should be accurate, PPU is mostly accurate, timing between the 2 is off for some corner cases and hardware qui

Chip8 emulator written in pure rust, using rust-sdl2 for graphics

Rust-8 chip8 emulator written in rust, using rust-sdl2 for graphics. Features Fully implemented all 35 original chip8 opcodes. This emulator does NOT

Commodore 64 emulator written in Rust
Commodore 64 emulator written in Rust

Rust64 - a C64 emulator written in Rust This is my attempt to study the Rust programming language and have fun at the same time. The goal is to presen

Comments
  • Update to upstreamed avr rust

    Update to upstreamed avr rust

    I get this error locally

    
    error: linking with `avr-gcc` failed: exit code: 1
      |
      = note: "avr-gcc" "-Os" "-mmcu=atmega328p" "-T" "lookup-text.ld" "-L" "/home/dylan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/avr-atmega328p/lib" "/home/dylan/repos/rust-avr-chip8-avr/target/avr-atmega328p/release/deps/chip8_avr-3101ca07c6937603.chip8_avr.122g828f-cgu.0.rcgu.o" "/home/dylan/repos/rust-avr-chip8-avr/target/avr-atmega328p/release/deps/chip8_avr-3101ca07c6937603.chip8_avr.122g828f-cgu.1.rcgu.o" "/home/dylan/repos/rust-avr-chip8-avr/target/avr-atmega328p/release/deps/chip8_avr-3101ca07c6937603.chip8_avr.122g828f-cgu.10.rcgu.o" "/home/dylan/repos/rust-avr-chip8-avr/target/avr-atmega328p/release/deps/chip8_avr-3101ca07c6937603.chip8_avr.122g828f-cgu.11.rcgu.o" "/home/dylan/repos/rust-avr-chip8-avr/target/avr-atmega328p/release/deps/chip8_avr-3101ca07c6937603.chip8_avr.122g828f-cgu.2.rcgu.o" "/home/dylan/repos/rust-avr-chip8-avr/target/avr-atmega328p/release/deps/chip8_avr-3101ca07c6937603.chip8_avr.122g828f-cgu.3.rcgu.o" "/home/dylan/repos/rust-avr-chip8-avr/target/avr-atmega328p/release/deps/chip8_avr-3101ca07c6937603.chip8_avr.122g828f-cgu.4.rcgu.o" "/home/dylan/repos/rust-avr-chip8-avr/target/avr-atmega328p/release/deps/chip8_avr-3101ca07c6937603.chip8_avr.122g828f-cgu.5.rcgu.o" "/home/dylan/repos/rust-avr-chip8-avr/target/avr-atmega328p/release/deps/chip8_avr-3101ca07c6937603.chip8_avr.122g828f-cgu.6.rcgu.o" "/home/dylan/repos/rust-avr-chip8-avr/target/avr-atmega328p/release/deps/chip8_avr-3101ca07c6937603.chip8_avr.122g828f-cgu.7.rcgu.o" "/home/dylan/repos/rust-avr-chip8-avr/target/avr-atmega328p/release/deps/chip8_avr-3101ca07c6937603.chip8_avr.122g828f-cgu.8.rcgu.o" "/home/dylan/repos/rust-avr-chip8-avr/target/avr-atmega328p/release/deps/chip8_avr-3101ca07c6937603.chip8_avr.122g828f-cgu.9.rcgu.o" "-o" "/home/dylan/repos/rust-avr-chip8-avr/target/avr-atmega328p/release/deps/chip8_avr-3101ca07c6937603.elf" "-Wl,--gc-sections" "-L" "/home/dylan/repos/rust-avr-chip8-avr/target/avr-atmega328p/release/deps" "-L" "/home/dylan/repos/rust-avr-chip8-avr/target/release/deps" "-L" "/home/dylan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/avr-atmega328p/lib" "-Wl,-Bstatic" "/home/dylan/repos/rust-avr-chip8-avr/target/avr-atmega328p/release/deps/libchip8_engine-e22d920b1e810129.rlib" "/home/dylan/repos/rust-avr-chip8-avr/target/avr-atmega328p/release/deps/librustc_std_workspace_core-8bb013930499e942.rlib" "/home/dylan/repos/rust-avr-chip8-avr/target/avr-atmega328p/release/deps/libcore-f7688596ec6ef2a0.rlib" "/home/dylan/repos/rust-avr-chip8-avr/target/avr-atmega328p/release/deps/libcompiler_builtins-fd91d00a6345f8c0.rlib" "-Wl,-Bdynamic" "-Wl,--gc-sections" "target/avr-atmega328p/release/deps/rom.o"
      = note: /usr/bin/avr-ld: /home/dylan/repos/rust-avr-chip8-avr/target/avr-atmega328p/release/deps/chip8_avr-3101ca07c6937603.elf section `.text' will not fit in region `text'
              /usr/bin/avr-ld: region `text' overflowed by 1538 bytes
              collect2: error: ld returned 1 exit status
    
    opened by dylanmckay 2
A CHIP-8 emulator for Rust in ~350 LOC

chip8 An implementation of the CHIP-8 for Rust in ~350 lines of code. What is CHIP-8? CHIP-8 is a virtual machine (along with a supporting programming

Daniel Gatis 8 Apr 23, 2022
Rust library for handling CHIP-8 configuration options

octopt octopt is a library for handling CHIP-8 configuration settings. It contains Rust data structures that aim to represent all possible CHIP-8 opti

Tobias V. Langhoff 3 Aug 7, 2022
A CHIP-8 emulator written in Rust

damn8 A CHIP-8 emulator written in Rust To run, pass a path to a rom in the command line. TODO: Use function pointers to handle opcodes instead of a l

null 1 Jan 11, 2022
A CHIP-8 emulator in Rust with iced-powered GUI

CHIP-8 Emulator in Rust A CHIP-8 emulator written in Rust and iced-powered GUI. All 34 CHIP-8 instructions (SUPER CHIP instructions are not supported)

Yuto Takahashi 6 Dec 27, 2022
CHIP-8 emulator written in Rust as a side project.

marisa-rs a CHIP-8 emulator, with learning purposes in mind, written in Rust. this is mainly a learning project of mine for deeper knowledge in emulat

Lumine 0 Jul 26, 2022
A CHIP-8 emulator written in Rust

chip8 A CHIP-8 emulator written in Rust Goal The goal of this project is to build a fully functional emulator (but I should name it an interpreter) of

Fabian Vilers 2 Jun 30, 2022
A CHIP-8 emulator written in Rust 🦀

A Rusty CHIP-8 Emulator CHIP-8 is an interpreted programming language, developed by Joseph Weisbecker. It was initially used on the COSMAC VIP and Tel

Varun Shenoy 2 Aug 15, 2022
A simple CHIP-8 emulator written in Rust.

CHIP-8 emulator This is a small learning project to get familiar with emulator development, the Rust programming language and the SDL2 library. Discla

null 3 Aug 31, 2022
CHIP-8 emulator written in Rust 🦀!

CHIP-Ahoyto ?? CHIP-8 emulator written in Rust ?? . The goal of this project is purely experimental and a learning tool for rust. The work of this emu

João Magalhães 3 Dec 15, 2022
A Chip-8 Emulator written in Rust & SDL2

Crab-8 A fully featured Chip-8 Emulator written in Rust. This project was written as a learning & development project in rust, and as such the code ma

Harry Smith 3 Dec 1, 2022