ch8

package
v0.0.0-...-13aacee Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 21, 2024 License: ISC Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultHertzIO is the default speed (in hertz) in which to update
	// the IO timers and audio.
	DefaultHertzIO = 16 * time.Millisecond

	// DefaultHertzVM is the default speed (in hertz) in which to run a
	// CPU cycle of the CHIP-8 virtual machine.
	DefaultHertzVM = 2 * time.Millisecond

	// DefaultMaxTPS is the default max ticks-per-second (TPS) of the
	// renderer.
	DefaultMaxTPS = 60

	// DefaultScale is the default scale factor of the CHIP-8 screen.
	DefaultScale = 10

	// FontSize is the number of bytes in a CHIP-8 built-in font.
	FontSize = 0x5

	// MaxStackDepth is the maximum depth of the CHIP-8 virtual
	// machine's call stack.
	MaxStackDepth = 0x10

	// MemorySize is the total amount of memory available in the CHIP-8
	// virtual machine.
	MemorySize = 0x1000

	// DisplayWidth is the width (in pixels) of the CHIP-8 display.
	DisplayWidth = 0x40

	// DisplayHeight is the height (in pixels) of the CHIP-8 display.
	DisplayHeight = 0x20

	// NumberOfKeys is the number of keys in the CHIP-8 keyboard.
	NumberOfKeys = 0x10

	// NumberOfFonts is the total number of built-in fonts in the
	// CHIP-8 virtual machine.
	NumberOfFonts = 0x10

	// NumberOfPixels is the total number of pixels in the CHIP-8
	// display.
	NumberOfPixels = DisplayWidth * DisplayHeight

	// NumberOfRegisters is the number of general-purpose registers in
	// the CHIP-8 virtual machine.
	NumberOfRegisters = 0x10

	// ProgramStartAddress is the start memory location where programs
	// are loaded in the virtual machine's memory.
	ProgramStartAddress = 0x200

	// ProgramMemorySize is the total amount of memory available for
	// CHIP-8 programs.
	ProgramMemorySize = MemorySize - ProgramStartAddress
)

Variables

View Source
var ErrTerminated = errors.New("Emulator terminated")

Functions

func InvalidJumpError

func InvalidJumpError(fromAddr, toAddr uint16) error

InvalidJumpError is an error caused by a program trying to jump to an invalid memory location.

func InvalidProgramError

func InvalidProgramError(msg string) error

InvalidProgramError is an error that occurs from loading a program.

func InvalidStateError

func InvalidStateError(msg string) error

InvalidStateError is an error that occurs due to bad state in the virtual machine.

Types

type Emulator

type Emulator struct {
	// contains filtered or unexported fields
}

Emulator is the CHIP-8 emulator.

func NewEmulator

func NewEmulator(opts EmulatorOptions) (*Emulator, error)

NewEmulator creates a new CHIP-8 emulator instance.

func (*Emulator) Draw

func (emu *Emulator) Draw(screen *ebiten.Image)

Draw renders the screen of the emulator.

func (*Emulator) Layout

func (emu *Emulator) Layout(outsideWidth, outsideHeight int) (int, int)

Layout returns the resolution of the emulator's screen.

func (*Emulator) LoadBytes

func (emu *Emulator) LoadBytes(bytes []byte) error

LoadBytes reads bytes into the virtual machine's memory.

Note that the bytes will loaded into memory starting at address 0x200, the start address of the virtual machine's program memory.

func (*Emulator) LoadROM

func (emu *Emulator) LoadROM(filename string) error

LoadROM reads a CHIP-8 ROM program file (*.ch8) and loads it into the virtual machine's memory.

func (*Emulator) Start

func (emu *Emulator) Start() (err error)

Start starts the emulator.

func (*Emulator) Update

func (emu *Emulator) Update() error

Update updates the state of the emulator.

type EmulatorEvent

type EmulatorEvent string

EmulatorEvent is an event that occurs that controls the state of the emulator.

type EmulatorOptions

type EmulatorOptions struct {
	// Scale is the scale factor of the CHIP-8 screen.
	Scale int
}

EmulatorOptions is a set of arguments that allows you to set different options in the emulator.

type VirtualMachine

type VirtualMachine struct {
	I       uint16
	SP      uint8
	PC      uint16
	DT      uint8
	ST      uint8
	Opcode  uint16
	V       [NumberOfRegisters]uint8
	Stack   [MaxStackDepth]uint16
	Memory  [MemorySize]uint8
	Keys    [NumberOfKeys]bool
	Display [DisplayHeight][DisplayWidth]bool
}

VirtualMachine is the CHIP-8 virtual machine.

func NewVirtualMachine

func NewVirtualMachine() *VirtualMachine

NewVirtualMachine creates new CHIP-8 virtual machine instance.

func (*VirtualMachine) Clear

func (vm *VirtualMachine) Clear()

Clear clears the entire state of the virtual machine.

func (*VirtualMachine) ClearDisplay

func (vm *VirtualMachine) ClearDisplay()

ClearDisplay clears the state of the display.

func (*VirtualMachine) ClearKeys

func (vm *VirtualMachine) ClearKeys()

ClearKeys clears the state of the keys.

func (*VirtualMachine) ClearProgram

func (vm *VirtualMachine) ClearProgram()

ClearProgram clears the program loaded in the virtual machine.

func (*VirtualMachine) ClearRegisters

func (vm *VirtualMachine) ClearRegisters()

ClearRegisters clears all the registers, including the program counter, timers, stack pointer.

func (*VirtualMachine) Reset

func (vm *VirtualMachine) Reset()

Reset resets the virtual machine.

This preserves the program/opcodes already loaded in memory.

func (*VirtualMachine) RunCycle

func (vm *VirtualMachine) RunCycle() error

RunCycle runs a single CPU cycle of the virtual machine.

func (*VirtualMachine) UpdateTimers

func (vm *VirtualMachine) UpdateTimers()

UpdateTimers updates the delay and sound timers.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL