Z80 Snake (Assembly)

Hand-Crafted Z80 Assembly Snake Game

Dial 555-9807 to Launch
            ╔═══════════════════════════════════════════════════╗
            ║                                                   ║
            ║     +--------------------------------------+      ║
            ║     |              ooooooo                  |      ║
            ║     |              o     o                  |      ║
            ║     |    *         o  O<-+                  |      ║
            ║     |              o                       |      ║
            ║     |              o                       |      ║
            ║     +--------------------------------------+      ║
            ║                                                   ║
            ║      Z 8 0   S N A K E   ( A S S E M B L Y )     ║
            ╚═══════════════════════════════════════════════════╝

Introduction

Welcome to Z80 Snake (Assembly)---the same classic snake game as its C-compiled sibling at 555-7635, but this time written entirely by hand in Z80 assembly language.

Where the C version at 555-7635 demonstrates how a high-level language can target the Z80 through compilation, this version takes the opposite approach. Every instruction, every memory address, every byte of data was placed deliberately by a human programmer. The assembly source code---over a thousand lines of it---is assembled in real time by the SLP-Z80 assembler when you connect.

For the player, the experience is identical: steer a snake, eat food, avoid walls and your own tail. But for the technically curious, this version offers a window into what Z80 programming actually looks like at the metal. The source code is embedded directly in the backend and assembled fresh each session, meaning you are witnessing the full journey from assembly mnemonics to running machine code every time you dial in.

Both versions play the same game. If you just want to play Snake, dial either number. If you are interested in how Z80 programmes are structured, the assembly version at 555-9807 is the more instructive one---and the source is documented in this manual.

For complete gameplay instructions, controls, strategy, and troubleshooting, please refer to the Z80 Snake manual (555-7635). This manual covers only the differences specific to the assembly variant.

Quick Start

  1. Dial 555-9807 from the EC-TTY main menu
  2. Wait for the Z80 emulator and assembler to initialise
  3. Press any key at the title screen to begin
  4. Steer with W A S D or arrow keys
  5. Eat * to grow, avoid walls and yourself
  6. Press Q to quit

Getting Connected

ATDT555-9807
CONNECT 14400

Initializing Z80 CPU emulator...
Loading Z80 Snake...

The assembly variant uses the same V.32bis modem profile (14,400 baud) as the C version. Upon connection, both the Z80 CPU emulator and the Z80 assembler WASM modules are loaded. The snake programme is assembled from source and loaded into memory at address $0100.

Differences from the C Version

Feature C Version (555-7635) ASM Version (555-9807)
Source language C Z80 Assembly
BIOS version v1.2 (timer) v2.0
Load address $2000 $0100
Movement timing Interrupt-driven timer Software delay loop
Assembly Pre-compiled binary Assembled at connect time
Title screen None (starts directly) Shows title, waits for keypress

Movement Timing

The most noticeable gameplay difference is in movement timing. The C version uses the BIOS v1.2 hardware timer to fire interrupts at 60 Hz, producing perfectly consistent movement. The assembly version uses a software delay loop (LD BC, $2000; DEC BC; ...), which means the snake's speed is tied to CPU emulation speed rather than a hardware clock. In practice, both feel responsive, but the assembly version's timing may vary slightly under heavy system load.

Title Screen

The assembly version presents a title screen upon connection:

=== Z80 SNAKE ===
    v1.0

Controls: WASD or Arrow Keys

Press any key to start

Press any key to begin. The C version starts gameplay immediately after loading.

Programme Architecture

The assembly source is organised into clearly separated sections, following good practice for Z80 development:

Memory Layout

Address Contents
$0000-$00FF BIOS vectors and services
$0100-$07FF Programme code and string data
$0800-$09FF Variables and snake body array
$FEFF Stack top

Programme Sections

Section Purpose
Entry Point Stack init, screen clear, title display
Main Game Loop Input handling, movement, collision, drawing
Game State Init Reset direction, score, snake position
Input Handler Non-blocking keyboard read with WASD and arrow support
Snake Update Direction application, body shifting, collision detection
Food System Pseudo-random placement using R register
Drawing Routines ANSI cursor positioning, colour output
Utility Functions Decimal printing (8-bit and 16-bit), cursor goto
Data Section ANSI escape strings, game messages
Variables Game state, snake body coordinates

BIOS Services Used

The assembly version uses BIOS v2.0 services via RST 08h:

Service Code Usage
GETC $01 Read keypress (blocking, for title and replay)
PUTC $02 Output single characters
PRINT $03 Output null-terminated strings
KBHIT $05 Non-blocking input check (game loop)
CLEAR $30 Clear terminal screen

Snake Body Storage

The snake body is stored as an array of (x, y) coordinate pairs at the snake_body label. The head is always at index 0. When the snake moves, all segments are shifted down by one position (from tail to head), and the new head position is written at index 0. This shifting approach is simple but costs O(n) per frame---a tradeoff the programmer accepted in favour of code clarity.

Command Reference

Controls are identical to the C version:

Command Context Description
W / Up In-game Move snake up
A / Left In-game Move snake left
S / Down In-game Move snake down
D / Right In-game Move snake right
Q In-game Quit the game
Y Game over Play again
N Game over Disconnect
Any key Title screen Start the game

Quick Reference Card

╔══════════════════════════════════════════════════════════════════════╗
║  Z80 Snake (ASM) - QUICK REFERENCE              Emulator.ca Systems ║
╠══════════════════════════════════════════════════════════════════════╣
║                                                                      ║
║  DIAL: 555-9807 (ASM version)       ALT: 555-7635 (C version)      ║
║                                                                      ║
║  CONTROLS:        WASD or Arrow Keys                                ║
║    W / Up       = Move up            Q = Quit game                  ║
║    A / Left     = Move left          Y = Play again (at game over)  ║
║    S / Down     = Move down          N = Disconnect (at game over)  ║
║    D / Right    = Move right                                        ║
║                                                                      ║
║  SOURCE: Hand-written Z80 assembly, assembled at connect time       ║
║  CPU: Z80 @ 1 MHz | BIOS v2.0 | Delay loop timing | Field: 40x20  ║
║                                                                      ║
╚══════════════════════════════════════════════════════════════════════╝

Troubleshooting

For general gameplay troubleshooting (display issues, controls, colours), refer to the Troubleshooting section of the Z80 Snake manual (555-7635).

Problem: "Assembly errors" message on connect

The assembler encountered an error while processing the embedded source code. This should not occur under normal operation. Disconnect and redial. If the problem persists, report it to your system operator.

Problem: Snake speed feels inconsistent

The assembly version uses a software delay loop rather than a hardware timer. CPU emulation speed can vary depending on your system's current load. If the snake seems to speed up or slow down, this is a consequence of the delay loop approach. The C version at 555-7635 uses timer-driven movement and may feel more consistent.

See Also


Z80 Snake (Assembly) -- Hand-crafted Z80 assembly variant

Emulator.ca Systems

Document Revision 1.0