View Source Chip8.Interpreter (chip8 v1.0.0)
An interpreter for the Chip-8 language.
The interpreter uses the following components to run a program:
Display | A monochrome display used to render graphics. See Chip8.Interpreter.Display for more information. |
Memory | A memory space of 4Kb used to store programs and data. See Chip8.Interpreter.Memory for more information. |
Delay Timer | A timer used to coordinate events within a program, usually referred to as dt. See Chip8.Interpreter.Timer for more information. |
Sound Timer | A timer used to emit a monotone sound while it's active, usually referred to as st. See Chip8.Interpreter.Timer for more information. |
Stack | A stack to store and retrieve memory addresses when calling and returning from subroutines. |
Program Counter | A 16-bit integer that points to the memory address of the current instruction, and is usually referred to as pc. |
I | A 16-bit register used to point to memory locations, usually to render sprites. |
Variable Registers | 16 8-bit registers to store and retrieve variable data. See Chip8.Interpreter.VRegisters for more information. |
Note that some of the components above are only accessible to programs through specific instructions (e.g. display and memory components) and some are not accessible at all (i.e. program counter).
cycle
Cycle
At each cycle, the interpreter performs the same sequence of steps until the the program reaches its end:
- Fetch
2 bytes
of memory from the address that pc is pointing at; - Decode the fetched bytes into an opcode with its operands;
- Execute the opcode by updating the interpreter state based on the opcode specification.
All programs are executed through this simple routine, so to prevent the interpreter to stop running they create an infinite loop (either a direct or indirect one) and keep executing until the user quits the application.
Link to this section Summary
Link to this section Types
@type t() :: %Chip8.Interpreter{ cycle_rate: pos_integer(), display: Chip8.Interpreter.Display.t(), dt: Chip8.Interpreter.Timer.t(), i: non_neg_integer(), keyboard: Chip8.Interpreter.Keyboard.t(), memory: Chip8.Interpreter.Memory.t(), pc: non_neg_integer(), st: Chip8.Interpreter.Timer.t(), stack: Chip8.Stack.t(), v: Chip8.Interpreter.VRegisters.t() }
Link to this section Functions
@spec change_cycle_rate(t(), pos_integer()) :: t()
@spec get_font_character_address(0..15) :: non_neg_integer()
@spec load_font(t(), Chip8.Interpreter.Memory.data()) :: t()
@spec pixelmap(t()) :: Chip8.Interpreter.Display.pixelmap()
@spec press_key(t(), Chip8.Interpreter.Keyboard.key()) :: t()
@spec release_key(t(), Chip8.Interpreter.Keyboard.key()) :: t()