Reading — step 1 of 5
Read
~2 min readDisplay & Input
Input & Timers
CHIP-8 keypad: 16 keys laid out:
1 2 3 C
4 5 6 D
7 8 9 E
A 0 B F
Modern emulators map to keyboard, e.g.:
1 2 3 4 -> 1 2 3 C
Q W E R -> 4 5 6 D
A S D F -> 7 8 9 E
Z X C V -> A 0 B F
Input instructions:
EX9E: skip next if key VX is pressed.EXA1: skip next if key VX is NOT pressed.FX0A: wait for key press, store in VX. Blocks until any key pressed.
Implementation:
python
Game loop calls update_keys() from real input, then runs CPU cycle.
Timers:
- Delay timer (DT): decremented at 60 Hz down to 0. No effect at 0.
- Sound timer (ST): decremented at 60 Hz. While > 0, output a beep (one tone).
Used for:
- DT: pacing — game waits N ticks before advancing.
- ST: sound effects (one note, fixed pitch).
LD V0, 60 ; 1 second
LD DT, V0 ; FX15
loop:
LD V1, DT ; FX07
SE V1, 0
JP loop
This loops until DT hits 0 = 1 second wait.
Decrement happens regardless of CPU state. In emulator: real-time clock or per-cycle counter.
python
Called 60 times per second, separate from CPU cycles.
Sound:
- Original CHIP-8: single-tone buzzer.
- Modern emulators: play 440 Hz square wave or generic beep when ST > 0.
Random:
CXKK: VX = (random byte) AND KK.- Used for non-deterministic game elements.
- Use real RNG; deterministic seed for testing.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…