Skip to content
keithadlerPublic

Latest commit

 

History

19 Commits

Folders and files

Repository files navigation

Cyloid

License: MIT

A tank shooter for the Atari 2600, written in 6502 assembly by Keith Adler. Open source under the MIT license.

🎮 Play in Browser

Play Cyloid Now

Arrow keys to move, Space to fire.

Build

brew install dasm        # macOS (or download from https://github.com/dasm-assembler/dasm)
make

Produces game.bin and cyloid.rom — a 4KB NTSC Atari 2600 ROM.

Play Locally

Download Stella (the Homebrew cask was disabled in September 2026 because it fails Gatekeeper) and open cyloid.rom.

open -a Stella cyloid.rom

Test

tests/frametime.py runs the ROM headlessly on a cycle-counted 6502 and checks that every screen is exactly 262 scanlines with 192 visible, that no visible line uses more than 76 CPU cycles, and reports the VBLANK budget per screen. It scripts joystick input, injected deaths and hits so it reaches every game mode.

pip install py65
make test                 # or: python3 tests/frametime.py cyloid.rom --worst

--worst pins the tank, a target and the missile onto the same obstacle-band lines to hit the kernel's worst case. --trace out.txt dumps every TIA register write with its scanline and cycle, which is how the v1.1 renderer rewrite was checked against v1.0.

Controls (Stella)

Key Action
Arrow keys Move tank
Space Fire missile (on the title: skip the intro)
F2 Reset (restarts from the title)

Game Overview

Navigate your tank through obstacle-filled arenas, destroy all targets to advance, and survive the homing boss attacks.

Objective

  • Destroy all targets on each level to advance
  • Survive 8 levels with increasing difficulty
  • Score 99 points to win
  • Targets = 1-5 points (streak bonus), Boss = 0 points (obstacle only)

HUD

The 8-line top border is drawn with mid-scanline playfield rewrites so its two halves differ:

  • Left: remaining lives as cyan pips
  • Center-left: score, two 3x5 playfield-pixel digits
  • Right: level bar, one pip per level, in the level's target color
  • The whole border blinks red for 24 frames whenever the boss spawns

Lives

  • 3 lives per game
  • Death triggers a 45-frame explosion animation at the death location
  • Tank respawns at center-bottom after explosion
  • 45 frames of invincibility after respawn

Enemies

  • Targets (colored diamonds): 2-5 per level, bounce off walls
    • Speed increases with level
    • Last 1-2 targets move at double speed
  • Boss (bright yellow): homing kamikaze attacker
    • Appears every ~4 seconds from a random edge (left or right)
    • Homes directly toward player position
    • Speed scales with level (2px/update → 4px/update)
    • Resets when it kills the player
    • Shooting it triggers an explosion but awards no points

Scoring

  • Each target hit scores 1 + streak bonus (consecutive hits without missing)
  • Streak: 1st hit = 1pt, 2nd = 2pt, 3rd = 3pt, 4th = 4pt, 5th+ = 5pt
  • Streak resets when missile hits a wall or goes off-screen
  • Score is capped at 99 (reaching it wins the game)
  • Screen flashes white when a level is completed

Obstacles

  • Playfield walls appear in horizontal bands across the field
  • Density increases with level (sparse on level 0, dense by level 5+)
  • Touching walls kills the player
  • Missiles stop when hitting walls (with a sound effect)
  • Pattern randomized each level using LFSR

Levels

8 levels (0-7) that cycle, each with unique:

  • Field/wall/target color palette
  • Obstacle density (controlled by per-level bitmask)
  • Target movement directions and speed
  • Boss homing speed

Title Sequence

Screen Technique Duration
KEITH Reflected PF text 3 sec
ADLER Reflected PF text 2.5 sec
PRESENTS Asymmetric PF (no mirror) 2.5 sec
CYLOID Asymmetric PF + P0 tank flyby 3 sec
Black Blank 1.5 sec

Game Flow

Title Sequence → Gameplay (wait for first move)
  ↓ (all targets destroyed)
Level Score Screen (victory jingle) → Next Level
  ↓ (last life lost)
Final Score Screen (sad melody) → GAME OVER (somber melody) → Title
  ↓ (score reaches 99)
Final Score Screen → GAME OVER → Title

Technical Architecture

This section is a reference for anyone learning Atari 2600 programming. The 2600 has no frame buffer — the CPU must feed graphics data to the TIA chip in real-time as the electron beam scans each line ("racing the beam").

The Hardware

Component Details
CPU 6507 (6502 variant), 1.19 MHz
Graphics TIA (Television Interface Adapter)
RAM 128 bytes ($80-$FF), shared with stack
ROM 4KB at $F000-$FFFF (bankswitching for larger)
Input 2 joystick ports, console switches
Audio 2 channels, 32 frequencies, 16 waveforms

TIA Objects

The TIA provides 5 movable objects. That's ALL you get — no tiles, no sprites, no layers:

Object Width Cyloid Usage
Player 0 (P0) 8 pixels Tank sprite (4 directional graphics, per-row colors)
Player 1 (P1) 8 pixels Targets (flickered) + Boss (yellow)
Missile 0 (M0) 1-8 clocks Player bullet
Missile 1 (M1) 1-8 clocks Unused
Ball (BL) 1-8 clocks Unused
Playfield (PF) 40 pixels Obstacles, walls, title text, HUD

Playfield Registers

The playfield is 40 pixels wide, defined by 3 registers (20 pixels, mirrored or repeated):

PF0: 4 pixels  (bits 4,5,6,7 → pixels 0-3)
PF1: 8 pixels  (bits 7,6,5,4,3,2,1,0 → pixels 4-11)  ← reversed!
PF2: 8 pixels  (bits 0,1,2,3,4,5,6,7 → pixels 12-19)

In reflect mode (CTRLPF bit 0 = 1), the right half mirrors the left. In repeat mode, it duplicates.

Frame Timing

NTSC requires exactly 262 scanlines per frame at 60 Hz:

VSYNC:     3 lines  — Tell TV to start new frame
VBLANK:   37 lines  — CPU processing time (game logic runs here)
Visible:  192 lines  — The picture (kernel runs here)
Overscan:  30 lines  — More CPU time (cleanup)
Total:    262 lines

Each scanline = 76 CPU cycles. The kernel must complete ALL graphics updates within 76 cycles per line or the display glitches.

VBLANK Timer

Instead of counting scanlines manually during VBLANK, use the RIOT timer:

lda #43         ; 43 × 64 = 2752 cycles ≈ 37 scanlines
sta TIM64T      ; Start timer
; ... do game logic ...
lda INTIM       ; Check timer
bne .-2         ; Wait until expired
sta WSYNC       ; Sync to scanline boundary

Everything that costs scanlines (sprite positioning takes one line per object plus an HMOVE line) has to happen before that wait, or the frame grows. Cyloid v1.0 positioned sprites after the wait and its game screen was 266 lines while the title screens were 262-267; v1.1 does all setup first, then EndVBlank (wait, WSYNC, picture on), then exactly 192 WSYNCs on every screen. tests/frametime.py enforces this.

Kernel Design

Cyloid uses a 2-line kernel — each iteration draws 2 scanlines:

Line 1: Obstacle PF + Tank sprite (P0) + tank row color   — max 61 cycles
Line 2: Target sprite (P1) + Missile (M0)                 — max 71 cycles

(Both figures include the jmp that carries over from the previous line; the harness measures them.)

The obstacle check is a table lookup indexed by the scanline:

ldy BandTbl,x   ; band number 0-3 on band lines, $FF elsewhere
bmi .noObstacle
lda ObsPF1,y
sta PF1
lda ObsPF2,y
sta PF2

BandTbl is 184 bytes, page-aligned so the indexed load never pays a page-crossing cycle. It encodes 8-scanline bands every 32 lines ((line & $1F) >= $18), and replaced a shift-and-mask computation that cost 16 cycles per line. The savings bought the per-row COLUP0 write that gives the tank its white barrel, cyan body and brown treads.

An inactive missile is parked at Y=200, below the field, so line 2 never needs a MissileOn test.

Horizontal Positioning

The 2600 has no X-position registers. To position an object horizontally, you use the "divide by 15" trick:

lda ObjectX     ; desired X position (0-159)
sec
sta WSYNC       ; wait for start of line
.loop:
sbc #15         ; subtract 15 (one TIA "section")
bcs .loop       ; keep going until negative
eor #7          ; convert remainder to fine motion
asl
asl
asl
asl
sta HMxx        ; set fine motion (-8 to +7 pixels)
sta RESxx       ; set coarse position (where the loop ended)
sta WSYNC
sta HMOVE       ; apply fine motion

Asymmetric Playfield

For non-mirrored text (PRESENTS, CYLOID, GAME OVER) and the HUD, Cyloid updates PF registers mid-scanline:

Cycle  0-14: Write left-half PF1, PF2 (PF0 stays blank)
Cycle 14-45: Delay
Cycle 45-55: Write right-half PF2 (lands on cycle 48), PF1 (cycle 55)

In reflect mode the right half draws PF2 first (from color clock 148), then PF1 (180), then PF0 (212). By updating registers between the left and right draw windows, each half shows different data. All four texts share one routine, DrawAsym, and one 128-byte data block (AsymText), 32 bytes per word.

The HUD line does the same trick with a color change thrown in: lives and score on the left in cyan, then PF2 blanked at cycle 48, COLUPF switched to the level color, and the level bar written to PF1 before its half starts drawing.

Flicker Multiplexing

With only 2 player sprites, Cyloid shows up to 5 targets + 1 boss by cycling which one P1 draws each frame:

Frame 1: P1 = Target 0
Frame 2: P1 = Boss
Frame 3: P1 = Target 1
Frame 4: P1 = Boss
Frame 5: P1 = Target 2
...

At 60fps, each object appears at 12-30fps — visible flicker but playable. This is the same technique used by Space Invaders on the 2600.

Collision Detection

The TIA provides hardware collision registers — latches that set when objects overlap on screen:

Register Bits Cyloid Usage
CXP0FB bit 7: P0-PF Tank vs walls (death)
CXPPMM bit 7: P0-P1 Tank vs target/boss (death)
CXM0FB bit 7: M0-PF Missile vs walls (stop bullet)
CXCLR write-any Clear all collision latches

Missile vs target uses software collision (distance check) because the missile and targets are on different frames due to the frame-split architecture.

Frame-Split Architecture

Game logic is too heavy for one VBLANK period. Cyloid splits work across two frames:

EVEN frames: Missile movement, hit detection (every live target + boss)
ODD frames:  Wall collision, death timers, target movement, flicker, boss AI, player collision
EVERY frame: Joystick, fire button, flash/respawn, tank graphics, sound

The harness measures the busiest play frame at about 1820 of the 2752 available VBLANK cycles, including sprite positioning and the HUD score build.

Sound

The TIA has 2 audio channels, each with:

  • AUDCx — waveform (0-15): pure tone, noise, buzz, etc.
  • AUDFx — frequency divider (0-31): lower = higher pitch
  • AUDVx — volume (0-15)

Critical: TIA registers are write-only. You cannot read back AUDV0 to decay the volume — you must track it in RAM (SndVol variable).

LFSR Random Number Generator

Random:
    lda RandSeed
    lsr             ; shift right, bit 0 → carry
    bcc .noTap
    eor #$B4        ; XOR with tap polynomial
.noTap:
    sta RandSeed    ; store new seed
    rts             ; random value in A

Produces 255 unique values before repeating. Seed must be non-zero.

Memory Map

$0000-$002C  TIA registers (write)
$0030-$003D  TIA registers (read — collision, input)
$0080-$00FF  RAM (128 bytes)
  $80-$CD    Game variables (78 bytes)
  $CE-$FF    Stack (50 bytes, grows down from $FF)
$0280-$0297  RIOT registers (timer, I/O)
$F000-$FFF9  ROM (4090 bytes: code + data, BandTbl page-aligned at $FF00)
$FFFA-$FFFF  Vectors (NMI, RESET, IRQ — 6 bytes)

Repository

File Description
game.asm Main game source (~2400 lines, heavily commented)
tests/frametime.py Headless frame-timing audit (needs pip install py65)
cyloid.rom Pre-built ROM (4KB, playable in any 2600 emulator)
vcs.h TIA/RIOT register definitions (standard include)
macro.h Standard macros: CLEAN_START, VERTICAL_SYNC, SLEEP
Makefile Build automation (make to build, make clean to reset)
CHANGELOG.md Complete version history (v0.1 through v1.1)
LICENSE MIT License

Contributing

Contributions welcome! Fork the repo, make changes, and submit a pull request. The game is written in 6502 assembly for the DASM assembler.

Resources for Learning 2600 Programming

Acknowledgments

  • Created by Keith Adler
  • Javatari.js by Paulo Augusto Peccin — browser-based Atari 2600 emulator that makes it possible to play Cyloid online
  • Stella — the reference Atari 2600 emulator used during development
  • DASM — the macro assembler for 6502
  • The Atari 2600 homebrew community at AtariAge for decades of knowledge sharing

License

This project is open source under the MIT License.

Releases

Packages

Contributors

Languages