CHIP-8


CHIP-8 is an interpreted programming language, developed by Joseph Weisbecker. It was initially used on the COSMAC VIP and Telmac 1800 8-bit microcomputers in the mid-1970s. CHIP-8 programs are run on a CHIP-8 virtual machine. It was made to allow video games to be more easily programmed for these computers.
Roughly fifteen years after CHIP-8 was introduced, derived interpreters appeared for some models of graphing calculators.
An active community of users and developers existed in the late 1970s, beginning with ARESCO's "VIPer" newsletter whose first three issues revealed the machine code behind the CHIP-8 interpreter.

CHIP-8 applications

There are a number of classic video games ported to CHIP-8, such as Pong, Space Invaders, Tetris, and Pac-Man. There are also applications like a random maze generator and Conway's Game of Life. These programs are reportedly placed in the public domain, and can be easily found on the Internet.

CHIP-8 extensions and variations

During the 1970s and 1980s, CHIP-8 users shared CHIP-8 programs, but also changes and extensions to the CHIP-8 interpreter, in the COSMAC VIP users' newsletter, VIPER magazine. These extensions included CHIP-10 and Hi-Res CHIP-8, which introduced a higher resolution than the standard 64x32, and CHIP-8C and CHIP-8X, which extended the monochrome display capabilities to support limited color, among other features. These extensions were mostly backwards compatible, as they were based on the original interpreter, although some repurposed rarely used opcodes for new instructions.
In 1979, Electronics Australia ran a series of articles on building a kit computer similar to the COSMAC VIP, based on the Motorola 6800 architecture. This computer, the DREAM 6800, came with its own version of CHIP-8. A newsletter similar to VIPER, called DREAMER, was used to share CHIP-8 games for this interpreter. In 1981, Electronics Today International ran a series of articles on building a computer, the ETI-660, which also was very similar to the VIP. ETI ran regular ETI-660 and general CHIP-8 columns until 1985.
In 1990, a CHIP-8 interpreter called CHIP-48 was made for HP-48 graphing calculators so games could be programmed more easily. Erik Bryntse later created another interpreter based on CHIP-48, called SCHIP, S-CHIP or Super-Chip. SCHIP extended the CHIP-8 language with a larger resolution and several additional opcodes meant to make programming easier. If it were not for the development of the CHIP-48 interpreter, CHIP-8 would not be as well known today.
David Winter's emulator, disassembler, and extended technical documentation popularized CHIP-8/SCHIP on many other platforms. It laid out a complete list of undocumented opcodes and features, and was distributed across many hobbyist forums. Many emulators used these works as a starting point.
However, CHIP-48 subtly changed the semantics of a few of the opcodes, and SCHIP continued to use those new semantics in addition to changing other opcodes. Many online resources about CHIP-8 propagate these new semantics, so many modern CHIP-8 games are not backward compatible with the original CHIP-8 interpreter for the COSMAC VIP, even if they don't specifically use the new SCHIP extensions.

CHIP-8 today

There is a CHIP-8 implementation for almost every platform, as well as some development tools. Despite this, there are only a small number of games for the CHIP-8.
While CHIP-8 and SCHIP have commonly been implemented as emulators, a pure hardware implementation also exists for certain FPGA boards.

Virtual machine description

Memory

CHIP-8 was most commonly implemented on 4K systems, such as the Cosmac VIP and the Telmac 1800. These machines had 4096 memory locations, all of which are 8 bits which is where the term CHIP-8 originated. However, the CHIP-8 interpreter itself occupies the first 512 bytes of the memory space on these machines. For this reason, most programs written for the original system begin at memory location 512 and do not access any of the memory below the location 512. The uppermost 256 bytes are reserved for display refresh, and the 96 bytes below that were reserved for the call stack, internal use, and other variables.
In modern CHIP-8 implementations, where the interpreter is running natively outside the 4K memory space, there is no need to avoid the lower 512 bytes of memory, and it is common to store font data there.

Registers

CHIP-8 has 16 8-bit data registers named V0 to VF. The VF register doubles as a flag for some instructions; thus, it should be avoided. In an addition operation, VF is the carry flag, while in subtraction, it is the "no borrow" flag. In the draw instruction VF is set upon pixel collision.
The address register, which is named I, is 16 bits wide and is used with several opcodes that involve memory operations.

The stack

The stack is only used to store return addresses when subroutines are called. The original RCA 1802 version allocated 48 bytes for up to 12 levels of nesting; modern implementations usually have more.

Timers

CHIP-8 has two timers. They both count down at 60 hertz, until they reach 0.
Input is done with a hex keyboard that has 16 keys ranging 0 to F. The '8', '4', '6', and '2' keys are typically used for directional input. Three opcodes are used to detect input. One skips an instruction if a specific key is pressed, while another does the same if a specific key is not pressed. The third waits for a key press, and then stores it in one of the data registers.

Graphics and sound

Original CHIP-8 Display resolution is 64×32 pixels, and color is monochrome. Graphics are drawn to the screen solely by drawing sprites, which are 8 pixels wide and may be from 1 to 15 pixels in height. Sprite pixels are XOR'd with corresponding screen pixels. In other words, sprite pixels that are set flip the color of the corresponding screen pixel, while unset sprite pixels do nothing. The carry flag is set to 1 if any screen pixels are flipped from set to unset when a sprite is drawn and set to 0 otherwise. This is used for collision detection.
As previously described, a beeping sound is played when the value of the sound timer is nonzero.

Opcode table

CHIP-8 has 35 opcodes, which are all two bytes long and stored big-endian. The opcodes are listed below, in hexadecimal and with the following symbols:
There have been many implementations of the CHIP-8 instruction set since 1978. The following specification is based on the SUPER-CHIP specification from 1991, as that is the most commonly encountered extension set today. Footnotes denote incompatibilities with the original CHIP-8 instruction set from 1978.
OpcodeTypeC PseudoExplanation
0NNNCallCalls machine code routine at address NNN. Not necessary for most ROMs.
00E0Displaydisp_clearClears the screen.
00EEFlowreturn;Returns from a subroutine.
1NNNFlowgoto NNN;Jumps to address NNN.
2NNNFlow*Calls subroutine at NNN.
3XNNCondifSkips the next instruction if VX equals NN.
4XNNCondifSkips the next instruction if VX doesn't equal NN.
5XY0CondifSkips the next instruction if VX equals VY.
6XNNConstVx = NNSets VX to NN.
7XNNConstVx += NNAdds NN to VX.
8XY0AssignVx=VySets VX to the value of VY.
8XY1BitOpVx=Vx|VySets VX to VX or VY.
8XY2BitOpVx=Vx&VySets VX to VX and VY.
8XY3BitOpVx=Vx^VySets VX to VX xor VY.
8XY4MathVx += VyAdds VY to VX. VF is set to 1 when there's a carry, and to 0 when there isn't.
8XY5MathVx -= VyVY is subtracted from VX. VF is set to 0 when there's a borrow, and 1 when there isn't.
8XY6BitOpVx>>=1Stores the least significant bit of VX in VF and then shifts VX to the right by 1.
8XY7MathVx=Vy-VxSets VX to VY minus VX. VF is set to 0 when there's a borrow, and 1 when there isn't.
8XYEBitOpVx<<=1Stores the most significant bit of VX in VF and then shifts VX to the left by 1.
9XY0CondifSkips the next instruction if VX doesn't equal VY.
ANNNMEMI = NNNSets I to the address NNN.
BNNNFlowPC=V0+NNNJumps to the address NNN plus V0.
CXNNRandVx=rand&NNSets VX to the result of a bitwise and operation on a random number and NN.
DXYNDispdrawDraws a sprite at coordinate that has a width of 8 pixels and a height of N pixels. Each row of 8 pixels is read as bit-coded starting from memory location I; I value doesn’t change after the execution of this instruction. As described above, VF is set to 1 if any screen pixels are flipped from set to unset when the sprite is drawn, and to 0 if that doesn’t happen
EX9EKeyOpifSkips the next instruction if the key stored in VX is pressed.
EXA1KeyOpifSkips the next instruction if the key stored in VX isn't pressed.
FX07TimerVx = get_delaySets VX to the value of the delay timer.
FX0AKeyOpVx = get_keyA key press is awaited, and then stored in VX.
FX15Timerdelay_timerSets the delay timer to VX.
FX18Soundsound_timerSets the sound timer to VX.
FX1EMEMI +=VxAdds VX to I. VF is not affected.
FX29MEMI=sprite_addrSets I to the location of the sprite for the character in VX. Characters 0-F are represented by a 4x5 font.
FX33BCDset_BCD;
*=BCD;
*=BCD;
*=BCD;
Stores the binary-coded decimal representation of VX, with the most significant of three digits at the address in I, the middle digit at I plus 1, and the least significant digit at I plus 2.
FX55MEMreg_dumpStores V0 to VX in memory starting at address I. The offset from I is increased by 1 for each value written, but I itself is left unmodified.
FX65MEMreg_loadFills V0 to VX with values from memory starting at address I. The offset from I is increased by 1 for each value written, but I itself is left unmodified.