Skip to content
FeralthedoggPublic

About

No description, website, or topics provided.

Resources

Stars

8 stars

Watchers

0 watching

Forks

Latest commit

 

History

6 Commits

Folders and files

Repository files navigation

CHIP-8 VM — x86-64 / GAS (AT&T) / Windows

License: MIT

A CHIP-8 virtual machine written in x86-64 assembly with AT&T syntax, targeting Windows through MinGW-w64. It includes a Win32 window, a scaled 64×32 framebuffer, keyboard input, and streaming audio generated by a minimal CDP1802 core and a COSMAC VIP–style 555 oscillator model.

The interpreter uses several classic CHIP-8 behaviors, including shifts sourced from Vy and incrementing I after Fx55/Fx65. Its complete behavior differs from the original COSMAC VIP; see compatibility.

Build and run

Install GNU Make and an x86-64 MinGW-w64 toolchain providing x86_64-w64-mingw32-gcc, then run:

make

This assembles chip8.asm and cdp1802.asm and links chip8.exe against gdi32, user32, kernel32, and winmm. Linux and macOS can cross-compile the executable; the GUI application runs on Windows.

Direct compilation from a POSIX shell:

x86_64-w64-mingw32-gcc -x assembler -c chip8.asm -o chip8.o
x86_64-w64-mingw32-gcc -x assembler -c cdp1802.asm -o cdp1802.o
x86_64-w64-mingw32-gcc chip8.o cdp1802.o -o chip8.exe -lgdi32 -luser32 -lkernel32 -lwinmm

Run from PowerShell on Windows:

.\chip8.exe ROMS/IBMLogo.ch8

ROMs are loaded at 0x200 into the 3584-byte program area. Startup prints the ROM path, its first four bytes, and window initialization diagnostics. Running without a ROM argument prints usage and returns exit code 1.

Display and controls

The framebuffer contains 2048 bytes, one monochrome pixel per byte. A top-down 8-bit DIB with a black/white palette is scaled to 640×320 using StretchDIBits. 00E0 and Dxyn mark the display dirty; timer events request a repaint only when needed, using InvalidateRect with background erasure disabled. Painting copies both on and off pixels in a single operation.

The CHIP-8 keypad and matching PC keys are:

CHIP-8          PC
1 2 3 C         1 2 3 4
4 5 6 D         Q W E R
7 8 9 E         A S D F
A 0 B F         Z X C V

Press Esc to exit.

Fx0A waits for a press and then the release of that same key. If a key is already held, it waits for that key's release. The interpreter records its wait state and returns to the message loop, so timers, painting, and audio continue. Releasing a different key does not finish the wait.

Timing

A Win32 timer requests updates every 16 ms. Each update measures elapsed time with QueryPerformanceCounter, then advances separate fractional accumulators for CPU instructions and 60 Hz delay/sound timers. The requested timer interval does not determine the emulated clock directly.

Setting Default
chip8_target_ips 700 instructions per second
Delay/sound timers 60 ticks per second
Maximum elapsed time processed per update 200 ms
chip8_max_steps_per_tick 4000 instructions

Change the data constants in chip8.asm to tune CPU speed. The main thread uses GetMessageA to dispatch timer, painting, keyboard, and audio completion messages.

Interpreter behavior

The core implements the standard CHIP-8 instruction groups: control flow, comparisons, arithmetic/logic, random numbers, drawing, keyboard checks, timers, fonts, BCD, and register transfers. It does not implement SUPER-CHIP or XO-CHIP.

Operation Behavior
8xy6, 8xyE Shift Vy into Vx; write the outgoing bit to VF last.
8xy1, 8xy2, 8xy3 Write the logical result to Vx; no separate reset of VF.
Bnnn Jump to (nnn + V0) & 0xFFF.
Fx1E Set I = (I + Vx) & 0xFFF; leave VF unchanged.
Fx55, Fx65 Transfer V0..Vx, then set I = (I + x + 1) & 0xFFF. Accesses beyond 0xFFF are skipped.
Dxyn Wrap starting coordinates and individual pixels at 64×32; XOR pixels and report collisions in VF; no vblank wait.
Fx33 Store BCD digits at (I + 0/1/2) & 0xFFF, preserving I and all V registers.
Cxkk Use a deterministic LCG byte masked with kk; initialization seeds the generator with 1.

PC and instruction fetch addresses wrap to 12 bits. The BCD wrapping rule keeps writes within CHIP-8 RAM even when I is 0xFFE or 0xFFF.

Audio

Audio uses waveOut, with three 160-sample buffers of unsigned 8-bit mono PCM at 8 kHz: 20 ms per buffer, nominally 60 ms queued. chip8_audio_init opens the device, prepares headers, and queues the initial buffers. Each MM_WOM_DONE message refills and requeues its completed buffer. Closing the application resets the device, unprepares headers, and closes the audio handle.

The minimal CDP1802 interpreter implements IDL, REQ, SEQ, BR, BQ, BNQ, LBR, and LDI; other opcodes advance as one-byte NOPs. It is a small audio helper, not a complete COSMAC VIP CPU emulation. The four-byte beep ROM holds the Q output high:

0000: 7B          SEQ
0001: C0 00 00    LBR 0000

The effective audio gate combines Q with cdp1802_gate, which is updated by the CHIP-8 sound timer. A phase accumulator generates an approximately 1.02 kHz square wave with a two-thirds duty cycle, using the modeled 555 values R1 = R2 = 100 kΩ and C = 0.0047 µF. The audio path applies a DC tracker and one low-pass stage:

dc += (raw - dc) >> 10;
centered = raw - dc + 128;
lpf += (centered - lpf) >> 5;

Samples are clamped to 0..255, then scaled around the silence midpoint 128 by chip8_audio_volume (0..255, default 255). There is no pre-rendered WAV file or PlaySoundA loop.

State and source layout

chip8_state is a single 6219-byte block:

Field Byte offset Size
RAM 0 4096
V0..VF 4096 16
I 4112 2
PC 4114 2
SP 4116 1
Call stack 4117 32
Framebuffer 4149 2048
Delay / sound 6197 / 6198 1 each
Keys 6199 16
Dirty flag 6215 1
Wait flag / target register / key 6216 / 6217 / 6218 1 each

chip8.asm contains the core, Win32 integration, ROM loader, and audio streaming helpers. cdp1802.asm contains the minimal 1802 interpreter and sample generator. Functions use the Microsoft x64 calling convention.

Compatibility and validation

Install the Python test dependencies and run:

python3 -m pip install -r tests/requirements.txt
make test

The tests execute the built executable's x86-64 code with Unicorn. They include regressions for random-register indexing, BCD at the RAM boundary, preservation of nonvolatile registers by the audio callback, and key-release waits. ROM tests use a pinned revision of the independent Timendus test suite.

Timendus check Result
CHIP-8 splash and IBM logo Match reference framebuffers
Corax+ opcodes and arithmetic flags Match reference framebuffers, excluding version labels
Keypad Fx0A scenario Matches the successful reference framebuffer after press/release
Beep SOS timer/gate pattern and nonconstant generated PCM verified
Quirks: memory increment, shifting, jumping Pass in CHIP-8 mode
Quirks: VF reset, display wait, clipping Existing differences: no VF reset, no vblank wait, wrapped sprites

All 13 automated test methods pass. The quirks test asserts the documented profile, including those three differences; this is not a full VIP compatibility pass. Native Windows GUI rendering, audio-device playback, and the scrolling extension test require separate validation.

GitHub Actions cross-compiles and runs the tests on Ubuntu, then uploads chip8.exe, framebuffer PNGs, and a JSON results report. See tests/README.md for reproducibility, fixture licensing, and test limitations.

License

The VM and its test harness are MIT licensed. Downloaded Timendus fixtures retain their upstream licenses and are cached separately from the VM.

About

No description, website, or topics provided.

Resources

Stars

8 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages