Building a CHIP-8 Emulator in C
Fourteen bytes set up both paddles in Pong. Follow them from a ROM file into CHIP-8 memory and through my C disassembler.
The first fourteen bytes of the Pong ROM in my CHIP-8 project describe both paddles. Four instructions give their positions, one picks a sprite, and two draw it. Those bytes make a good tour of the code because you can follow them from a file on disk to an address in the emulated machine, then read what each instruction asks the machine to do. My C implementation gets as far as loading the ROM and producing that listing. Pong ROM.
Giving the ROM an address
I represented the machine in one C structure. It has 4,096 bytes of memory, sixteen byte-sized registers, a program counter, an index register called I, and room for sixteen return addresses. The screen is a 64 by 32 array laid out in one dimension. Initialization clears the arrays and sets the program counter to hexadecimal 0x200. It also copies eighty bytes of font data into low memory at 0x50. There are five rows for each hexadecimal digit, so the glyph for A begins at 0x82. Machine layout and font.
The ROM file starts at byte zero on disk, but its instructions use addresses relative to the machine. My loader writes file byte zero at 0x200, file byte one at 0x201, and so on. That leaves 3,584 bytes for a program. The loader checks that length, copies the bytes, and writes the whole memory image to memory.dump. You can inspect both the built-in font and the program in a single file. ROM loading and memory dump.
Reading Pong's opening instructions
From the checkout, make builds the command-line tool. Run ./emu -d roms/Pong.rom to see the disassembly. Each line includes the machine address, the two instruction bytes, and a mnemonic. Here are the first seven instructions with their operands written out in words. Instruction decoder.
0200 6A 02 Load VA with 2
0202 6B 0C Load VB with 12
0204 6C 3F Load VC with 63
0206 6D 0C Load VD with 12
0208 A2 EA Set I to 0x2EA
020A DA B6 Draw 6 rows at (VA, VB)
020C DC D6 Draw 6 rows at (VC, VD)
The pieces have different meanings depending on the first four bits. In 6A 02, the 6 selects a load, A identifies the destination register, and the entire second byte is the value. In A2 EA, three digits combine into one address. In DA B6, A and B name two registers, and 6 gives the sprite height. My decoder uses a shift to select the instruction family, then masks or shifts the remaining bits according to that family's format. Address and sprite decoding.
You can check the paddle shape independently. Run ./emu -f roms/Pong.rom, then od -An -tx1 -j 746 -N 6 memory.dump. Decimal 746 is the address the ROM placed in I. The six bytes there are all 80 in hexadecimal. Each row has only its leftmost bit set, so the same six-byte sprite describes a one-pixel-wide paddle on each side of the screen. The file offset for those bytes is just 0xEA. The extra 0x200 comes from the loader.
Following a call into the score display
A few bytes after the paddles, Pong gives the reader a different exercise. At 0x20E, it loads zero into register VE. At 0x210, the bytes 22 D4 call the subroutine at 0x2D4. To find the subroutine in the ROM, subtract the program base again. File offset 0xD4 is 212 in decimal. From the checkout, od -An -tx1 -j 212 -N 22 roms/Pong.rom shows the complete routine, including its return. Pong program bytes.
02D4 A2 F2 Point I at the three-byte score buffer
02D6 FE 33 Write the decimal digits of VE to that buffer
02D8 F2 65 Load those three bytes into V0, V1, V2
02DA F1 29 Point I at the font glyph for the digit in V1
Take a hypothetical score of 27. The decimal conversion at 0x2D6 would write three ordinary bytes, 00 02 07, at addresses 0x2F2 through 0x2F4. Loading those bytes puts the tens digit in V1 and the units digit in V2. The font lookup then selects the glyph for 2. With this project's font base and five bytes per glyph, that makes I equal to 0x5A. These are the same font bytes copied during initialization, now used for something a player can recognize. Font, decimal, and register-transfer opcodes.
The rest of the routine supplies the coordinates. It sets V4 to hexadecimal 14, or 20, and V5 to zero. A five-row draw would put the tens digit at (20, 0). Adding hexadecimal 15 to V4 moves the next draw to (41, 0). The routine looks up V2's glyph, draws the units digit, and returns. Its caller would continue at 0x212. With the actual initial VE value of zero, both lookups would select the zero glyph.
This also gives a concrete design question for the unfinished execution loop. My stack helper pushes the current program counter, and the pop helper restores it. To return to 0x212, the call implementation and instruction fetch would have to agree on when to advance by two. If the saved address were still 0x210 when the return completed, the next fetch would call the score routine again. The same two bytes make a perfectly readable listing either way. Only executing the control flow would expose the difference. Stack helpers.
Where the listing ends
The disassembler walks forward two bytes at a time. It will eventually reach those six sprite bytes and try to print instructions for them as well. A reader who follows the pointer in I can recognize the drawing data. The linear scan has no record of how the program uses it. Likewise, seeing a draw mnemonic in the listing tells you what the ROM requests. In this revision, the command-line paths end after disassembly or a memory dump. Rendering those paddles, moving them in response to keys, and advancing the game would require an execution loop and frontend. Command-line entry points.