Skip to content
Lucien Le Roux edited this page Jul 21, 2022 · 10 revisions

class Chip8

Interface to the underlying WebAssembly emulator.

static new(rom: Uint8Array): Promise<Chip8>

This static method returns an emulator instance with the provided ROM loaded into memory, ready to be used. It also handles the necessary WebAssembly initialization and setup, which is why this method is asynchronous and should be used instead of the regular constructor.

static VIDEO_WIDTH: number

The display width in pixels (64).

static VIDEO_HEIGHT: number

The display height in pixels (32).

start(): void

Start or resume emulator execution.

stop(error?: Error): void

Stops emulator execution.

cycleUntil(duration: string): void

Runs a certain amount of clock cycles depending on the duration that is passed:

  • tick: runs 1 clock cycle (~1/500s)
  • cpu: runs 1 CPU cycles (~1/500s)
  • timer: runs 1 timer cycle (~1/60s)

input(key: Button, state: boolean): void

Updates the input state with the provided key status.

The canvas the emulator paints to.

error: Error

The current error, if any. It is usually caused by a panic during execution.

logs: Logs

Emulator logs, produced through Rust's log facade.

Current WebAssembly instance memory.

debug: Debug

Debug info containing the emulator state, to inspect its inner workings.

audio: Audio

Module responsible for producing and analyzing audio through the Web Audio API.

status: Status

Current emulator status.

performance: Statistics

Measures of browser frame performance.

class Audio

Interface to the emulator audio.

start(): void

Enables sound output.

stop(): void

Disables sound output.

play(): void

Plays oscillator.

pause(): void

Stops oscillator.

analyze(): void

Manually update analyzer data.

data: object

Contains analyzer data:

get sampleRate: number

The audio context's sample rate.

get type: OscillatorType

The oscillator's type.

set type: OscillatorType

Sets the oscillator's type.

set volume: number

Sets the output volume, between 0 and 1.

set frequency: number

Sets the oscillator frequency in hertz.

class Logs

Interface to the emulator logs.

history: object[]

All log records so far. They represent a Rust log record (https://docs.rs/log/latest/log/struct.Record.html):

  • text: string Log message
  • level: string Log verbosity level. One of TRACE, DEBUG, INFO, WARN or ERROR.
  • location: string place in the original code where this message was logged.

enable(): void

Enable logging (induces performance overhead).

disable(): void

Disables logging.

class Debug

This class represents the state of the emulator for in-depth debugging purposes. After each cycle, the emulator's debug object is replaced with a new instance to stay up-to-date. For performance reasons, all fields are memoized getters: this way, instead of fetching everything at every frame (which would be very expensive), we only pay for what we actually use.

If we try to access a property while the emulator is in an erroneous state, the getter will warn us about it and return null.

get cpu: DebugCpu

CPU state.

get disassembly: DebugDisassembly

Disassembly utilities.

get memory: Uint8Array

Memory RAM.

get input: boolean[]

Array of 16 booleans representing each key state.

get clock: object

Master clock.

  • rate: rate at which the clock is ticked
  • time: time elapsed so far

class DebugCpu

clock: object

CPU clock.

  • rate: rate at which the clock is ticked
  • cycles: number of cycles run so far

clockTimer: object

CPU timers clock.

  • rate: rate at which the clock is ticked
  • cycles: number of cycles run so far

pc: number

Program counter.

sp: number

Stack pointer.

All 16 V registers.

i: number

I register.

stack: Uint16Array

Stack.

dt: number

Delay timer.

st: number

Sound timer.

class DebugDisassembly

total: number

Total number of instructions in the memory segment.

at(address: number): object

Returns the instruction at a given address.

  • address: number address at which the instruction was fetched
  • opcode: number raw opcode
  • disassembly: string textual representation of the instruction

addressToIndex(address: number): number

Converts an address to the index of its instruction.

indexToAddress(index: number): number

Converts an index to the address of its instruction.

type Button

A keypad button. Can be any integer between 0 and 16.

enum Status

Represents execution status:

  • IDLE: emulator is paused
  • RUNNING: emulator is running
  • ERROR: emulator encountered an error