A Chip-8 emulator written in Kotlin

Related tags

Kotlin chip-8
Overview

Chipsk-8

A Chip-8 emulator written in Kotlin

What is Chip-8?

Chip-8 is a processor developed in the 70s. Because of its simplicity, it's a great starter project for anyone interested in learning how to implement an emulator. The specification is pretty small (about ten pages) and there are several roms available that make it easy to test how well your emulator runs.

How to run

$ ./gradlew run

The emulator will load with Space Invaders by default (press 5 to start the game, then 4/5/6 to move around and shoot). Open a new rom by clicking on the "Open rom..." button.

You can pause the emulator at any time (key 'p'), which will update the disassembly window to show the next instructions about to be executed. You can also adjust the clock speed to make the emulator go slower or faster.

Architecture

The game creates a Computer object which is made of a Display, Keyboard, FrameBuffer and Cpu.

Cpu

The CPU reads a new instruction (the next two bytes extracted at the program counter location) at a fixed rate which defines the clock speed. Two timers are needed: one for the CPU and one for the device timer register, called DT, which needs to tick at 60 Hz according to the spec. Since there is no specific definition for the CPU clock, I used the timing diagram from the document to set it at around 500Hz:

// CPU clock: around 500 Hz by default
cpuFuture = executor.scheduleAtFixedRate(cpuTick, 0, 1_000_000L / cpuClockHz, TimeUnit.MICROSECONDS)

// Delay Timer: 60 Hz by spec
timerFuture = executor.scheduleAtFixedRate(timerTick, 0, 16L, TimeUnit.MILLISECONDS)

The next two bytes are then masked and turned into instructions. All the op codes can be found in the Ops.kt file. Here is an example:

/**
 * 7xkk
 * Set Vx = Vx + kk
 */
class Add(c: Computer, n: Nibbles): Op(c, n) {
    override fun run() { cpu.V[x] = unsigned(cpu.V[x] + kk) }
    override fun toString() = "ADD V$x, $kk"
}

Display

The Display is a simple interface which allows multiple strategies to render the frame buffer:

interface Display {
    val pane: Pane
    fun draw(frameBuffer: IntArray)
    fun clear(frameBuffer: IntArray)
}

For example, here is a text based renderer:

The emulator window will resize gracefully:

You can also easily alter other aspects of the renderer:

You might also like...
A fast, lightweight, entity component system library written in Kotlin.

Fleks A fast, lightweight, entity component system library written in Kotlin. Motivation When developing my hobby games using LibGDX, I always used As

Sample Code for fake Kotlin library written in Java

Jatlin このリポジトリは ブログ記事 のためのサンプルコードです。詳細は記事をご覧ください。 プロジェクト構成 :java-lib にKotlinに偽装したJavaファイルが含まれます。 :kotlin-lib は :java-lib をビルドしたJARファイルをKotlinから読み込んで実行

📚  Sample Android Components Architecture on a modular word focused on the scalability, testability and maintainability written in Kotlin, following best practices using Jetpack.
📚 Sample Android Components Architecture on a modular word focused on the scalability, testability and maintainability written in Kotlin, following best practices using Jetpack.

Android Components Architecture in a Modular Word Android Components Architecture in a Modular Word is a sample project that presents modern, 2020 app

Slime World Format implementation written in Kotlin with Zstd for bukkit.

Slime Korld Easily create many slime worlds with the Slime Korld. What is Slime Korld? Slime Korld is a bukkit library written in Kotlin to make minec

Stresscraft - State-of-art Minecraft stressing software written in Kotlin

StressCraft (W.I.P) State-of-art Minecraft stressing software written in Kotlin.

GBooks - A simple android app written in Kotlin to read books from the Google Book Api
GBooks - A simple android app written in Kotlin to read books from the Google Book Api

G-Books A simple android app written in Kotlin to read books from the Google Boo

LanServers - A small plugin written in Kotlin that runs on all major Minecraft Servers

LanServers This is a small plugin written in Kotlin that runs on all major Minec

A Java Virtual Machine written in Kotlin
A Java Virtual Machine written in Kotlin

jvm.kotlin A Java Virtual Machine written in Kotlin. Introduction jvm.kotlin is a toy JVM programmed in Kotlin. The main purpose of this project is le

A property/method accessor library for the JVM, written in Kotlin

unlok - unlock your JVM a property/method accessor library for the JVM, written in Kotlin. how to import you can import unlok from maven central just

Owner
Cedric Beust
Creator of Android Gmail.
Cedric Beust
Android To-Do MVVM Architecture App written in Kotlin.(ViewModel, ROOM, Livedata, Coroutines)

MVVM-To-Do-App A To-Do application written in kotlin using Android Architectural components What's new? Room + Coroutines - Upgraded Room to v2.1. Roo

Naveen T P 77 Dec 8, 2022
An extension of EditText with pin style written in Kotlin

pin-edittext An extension of EditText with pin style Usage Include PinCodeEditText in your layout XML <com.oakkub.android.PinEditText android:layo

Metas Kerdwat 15 May 28, 2021
🍼Debug Bottle is an Android runtime debug / develop tools written using kotlin language.

???? 中文 / ???? 日本語 / ???? English ?? Debug Bottle An Android debug / develop tools written using Kotlin language. All the features in Debug bottle are

Yuriel Arlencloyn 846 Nov 14, 2022
A lightweight cache library written in Kotlin

[NEW] Released to Maven Central: 'com.github.yundom:kache:1.x.x' Kache A runtime in-memory cache. Installation Put this in your build.gradle implemen

Dennis 22 Nov 19, 2022
Android AsyncTask wrapper library, written in Kotlin

KillerTask This is a Kotlin Android library to create async background tasks. Inspired by TinyTask, but more beautiful and easy to use for Kotlin Andr

Inaka 26 Oct 3, 2022
Advancement Utils for Android Developer written in Kotlin

RichUtils About Version: 2.0.0 :: Dreaming Bird (夢魅鳥, Yumemidori) :: Release Note (Eng) : https://blog.uzuki.live/richutils-2-0-0-dreaming-bird-releas

WindSekirun (wind.seo) 169 Nov 13, 2022
A simple 'Slide to Unlock' Material widget for Android, written in Kotlin 📱🎨🦄

Slide To Act A simple Slide to Unlock Material widget for Android, written in Kotlin ??. Getting Started Example Features Attributes area_margin inner

Nicola Corti 984 Jan 2, 2023
Mobile client for official Nextcloud News App written as Kotlin Multiplatform Project

Newsout Android and iOS mobile client for Nextcloud news App. The Android client is already available to download in the Play Store. F-Droid and Apple

Simon Schubert 118 Oct 3, 2022
Spigot-Plugin message providing system written in Kotlin

teller Spigot-Plugin message providing system written in Kotlin Usage Create an instance of PropertiesMessageProvider using the Constructor with an in

Luca Zieserl 2 Jan 16, 2022
A furry-themed assembly language and interpreter written in Kotlin.

A furry-themed assembly language and interpreter written in Kotlin. Inspired by Furcode, the Synacor challenge, and JVM bytecode. I spent multiple hou

Maow 9 Nov 22, 2021