Programming & reference

Write your own programs: BASIC basics, color and screen graphics, sprites, SID sound, data handling, and every reference table from the User's Guide.

Print the full guide

Nothing on this page matches. Clear the filter or try another page from the menu.

BASIC basics

A line without a number runs immediately. A line with a number (0–63999) is stored as part of a program and runs when you type RUN. Number lines in steps of 10 so you can squeeze lines in later.

NEW
10 PRINT "COMMODORE 64"
20 GOTO 10
RUN

RUN/STOP stops it (shows BREAK IN 10). LIST shows it again; ? is turned into PRINT for you.

Editing a program

To Do
Replace a line Type it again with the same number
Delete a line Type just its number and RETURN
Fix a line LIST, move the cursor onto it, type over, press RETURN
Make room SHIFT + INST/DEL inserts a space
Add lines in any order They are sorted by number automatically

Quote mode

After you type an opening ", cursor and color keys don't act; they type a symbol into the text instead. When the PRINT runs, those symbols act. Example: PRINT " + SHIFT-CLR/HOME + HELLO" clears the screen, then prints HELLO. Typed one by mistake? INST/DEL removes it. Listings write these as {CLR/HOME}, {CRSR DOWN} and so on.

Variables

Type Example names Holds
Real (decimal) A, X1, BZ Numbers like 23.5
Integer A%, X1%, BZ% Whole numbers
String A$, X1$, BZ$ Text

First character a letter, second a letter or digit. Only the first two characters count: PA and PARTNO are the same variable. Names can't contain a keyword (TOTAL contains TO). ST, TI and TI$ are reserved. X = X + 1 means "add 1 to X".

In a PRINT Effect
; between items Print right next to each other
, between items Jump to the next 10-character column
; or , at the end Stay on the same line for the next PRINT
Positive numbers Print with a leading space (room for a minus sign)

Operators work in this order: ↑ (power), negative sign, * /, + -, comparisons (= < > <= >= <>), NOT, AND, OR. Brackets change it: (2+3)*4 = 20.

Decisions and loops

10 FOR CT = 1 TO 5
20 PRINT "HELLO"
30 NEXT CT

STEP .5 counts in halves; FOR N = 10 TO 1 STEP -1 counts down. Loops can sit inside loops but must not cross (that causes ?NEXT WITHOUT FOR). FOR T=1 TO 500:NEXT is a simple delay. IF CT < 5 THEN 20 jumps back while a condition holds. ON X GOTO 100,200,300 picks a line by number.

Asking the user, random numbers

Command Behavior
INPUT "YOUR NAME";N$ Shows a prompt (max 38 chars) and a ?, waits for RETURN
GET A$: IF A$="" THEN 10 Reads one key without RETURN; loop until a key is pressed
RND(1) Number between 0 and 1
INT(6*RND(1))+1 Whole number 1–6 (a dice roll)

Random maze in one line: 10 PRINT CHR$(205.5+RND(1)); : GOTO 10

Colors and screen graphics

The 16 colors, every way to get them

# (POKE value) Color Keys CHR$ code
0 Black CTRL 1 144
1 White CTRL 2 5
2 Red CTRL 3 28
3 Cyan CTRL 4 159
4 Purple CTRL 5 156
5 Green CTRL 6 30
6 Blue CTRL 7 31
7 Yellow CTRL 8 158
8 Orange C= 1 129
9 Brown C= 2 149
10 Light red C= 3 150
11 Gray 1 (dark) C= 4 151
12 Gray 2 (medium) C= 5 152
13 Light green C= 6 153
14 Light blue C= 7 154
15 Gray 3 (light) C= 8 155

Other codes: CHR$(18) reverse on, CHR$(146) reverse off, CHR$(147) clear screen, CHR$(19) home, CHR$(14) lowercase, CHR$(142) uppercase/graphics.

Address Controls
53280 Border color
53281 Background color
53282 / 53283 Extra background colors (special text modes)
646 Current text (cursor) color

When reading back, use PEEK(53280) AND 15: the top bits can hold junk.

Putting characters anywhere on screen

The screen is 40 columns × 25 rows = 1000 spots. Each spot has two memory locations: one for the character and one for its color.

Map Starts (top-left) Ends (bottom-right) Formula for column X (0–39), row Y (0–24)
Screen (character) 1024 2023 1024 + X + 40*Y
Color 55296 56295 55296 + X + 40*Y

A white ball in the middle: POKE 1524,81 : POKE 55796,1. Screen memory takes screen codes, not CHR$ codes: 81 = ball, 32 = space, 160 = solid block, 1 = A.

10 PRINT "{CLR/HOME}"
20 POKE 53280,7 : POKE 53281,13
30 X = 1 : Y = 1 : DX = 1 : DY = 1
50 POKE 1024 + X + 40*Y, 81
60 FOR T = 1 TO 10 : NEXT
70 POKE 1024 + X + 40*Y, 32
80 X = X + DX : IF X <= 0 OR X >= 39 THEN DX = -DX
100 Y = Y + DY : IF Y <= 0 OR Y >= 24 THEN DY = -DY
120 GOTO 50

Sprites

Sprites are movable pictures, 24 × 21 pixels, up to 8 on screen (numbered 0–7). The video chip handles them, including collision detection. All sprite registers start at V = 53248.

  1. Design it on a 24 × 21 grid. Each row is 3 bytes, so a sprite is 63 bytes.
  2. Store the shape: POKE the 63 numbers into memory, e.g. starting at 832.
  3. Point to it: POKE 2040,13 tells sprite 0 its shape is at 13 × 64 = 832. Pointers for sprites 0–7 are at 2040–2047.
  4. Switch it on: POKE V+21,1.
  5. Color and place it: POKE V+39,1 (white), POKE V,100 : POKE V+1,100.
10 V=53248 : POKE V+21,1 : POKE 2040,13 : POKE V+39,7
20 FOR N=0 TO 62 : READ D : POKE 832+N,D : NEXT
30 FOR X=24 TO 255 : POKE V,X : POKE V+1,120 : NEXT
40 GOTO 30
100 DATA 255,255,255, ... (63 numbers in total)

Working out a row's numbers

Pixel (left → right) 1st 2nd 3rd 4th 5th 6th 7th 8th
Value 128 64 32 16 8 4 2 1

Add the values of filled pixels: all 8 = 255, left half = 240, none = 0. The same values pick sprites in the on/off, expand, priority and collision registers: sprite 0 = 1, sprite 1 = 2 … sprite 7 = 128 (POKE V+21,255 = all on).

Sprite registers (V = 53248)

Register Address What it does
V+0, V+1 53248, 53249 Sprite 0 X, Y
V+2 … V+15 53250–53263 Sprites 1–7 X, Y (in pairs)
V+16 53264 Extra X bit: add the sprite's value to go past X = 255
V+21 53269 Sprites on/off
V+23 53271 Double height
V+27 53275 Priority: 1 = behind background characters
V+28 53276 Multicolor mode on
V+29 53277 Double width
V+30 53278 Sprite–sprite collision (reading clears it)
V+31 53279 Sprite–background collision
V+37, V+38 53285, 53286 Shared multicolor 0 and 1
V+39 … V+46 53287–53294 Colors of sprites 0–7

Other video registers

Address Name Use
V+17 (53265) Control 1 Vertical scroll, 24/25 rows, screen blank, bitmap mode, extended color, raster bit 8
V+18 (53266) Raster Current screen line; raster interrupts
V+19, V+20 Light pen X, Y Light pen position
V+22 (53270) Control 2 Horizontal scroll, 38/40 columns, multicolor text
V+24 (53272) Memory setup Where screen and character set live (POKE 53272,23 = lowercase set)
V+25, V+26 Interrupts Interrupt flags and mask
V+32 (53280) Border color
V+33–V+36 Background colors 0–3

Visible X runs about 24–343 and Y about 50–249. Check a hit with IF PEEK(V+30) AND 1 THEN .... Lower-numbered sprites appear in front.

Sound and music

The SID chip has 3 voices, each with its own pitch, waveform and volume envelope, plus a shared filter and master volume. The C64U can run up to 8 emulated SIDs and has sockets for 2 real SID chips. The guide's programs use the first SID at 54272.

  1. Clear the chip: FOR L=54272 TO 54296 : POKE L,0 : NEXT
  2. Volume: POKE 54296,15. Nothing plays without this.
  3. Envelope first: POKE 54277,9 (attack/decay), POKE 54278,0 (sustain/release)
  4. Pitch: POKE 54273,34 : POKE 54272,75
  5. Start the note: POKE 54276,17 (triangle + 1)
  6. Wait: FOR T=1 TO 250 : NEXT
  7. Release it: POKE 54276,16
Waveform Start note Release note Sounds like
Triangle 17 16 Soft, flute-like
Sawtooth 33 32 Bright, brassy
Pulse 65 64 Hollow to buzzy (set pulse width too)
Noise 129 128 Drums, explosions, wind

SID registers

What Voice 1 Voice 2 Voice 3 Values
Frequency low 54272 54279 54286 0–255
Frequency high 54273 54280 54287 0–255
Pulse width low 54274 54281 54288 0–255
Pulse width high 54275 54282 54289 0–15
Control / waveform 54276 54283 54290 See waveforms
Attack / Decay 54277 54284 54291 Attack 16–240 + decay 0–15
Sustain / Release 54278 54285 54292 Sustain 16–240 + release 0–15
Shared Address Values
Filter cutoff low / high 54293 / 54294 0–7 / 0–255
Resonance + filtered voices 54295 Resonance ×16 + voice 1 = 1, voice 2 = 2, voice 3 = 4
Volume + filter type 54296 Volume 0–15 + low-pass 16, band-pass 32, high-pass 64, voice 3 off 128
Paddle X / Y (read) 54297 / 54298 0–255
Voice 3 output (read) 54299 Random numbers when voice 3 plays noise
Voice 3 envelope (read) 54300 0–255

ADSR and instruments

Stage Meaning Values
Attack How fast the note rises to full volume 16, 32, 64, 128 (add for in-between)
Decay How fast it drops to the sustain level 1, 2, 4, 8
Sustain Volume held while the note is on 16, 32, 64, 128
Release How fast it fades after you stop the note 1, 2, 4, 8
Instrument Waveform Attack/Decay Sustain/Release
Piano Pulse 9 0
Flute Triangle 96 0
Harpsichord Sawtooth 9 0
Xylophone Triangle 9 0
Organ Triangle 0 240
Calliope Triangle 0 240
Accordion Triangle 102 0
Trumpet Sawtooth 96 0

Note values

Quick scale for voice 1 (the guide's "low C" octave):

Note C C# D D# E F F# G G# A A# B C
High (54273) 34 36 38 40 43 45 48 51 54 57 61 64 68
Low (54272) 75 85 126 200 52 198 127 97 111 172 126 188 149

Exact values for octave 4 (NTSC and PAL differ; each octave up doubles the number):

Note NTSC high NTSC low PAL high PAL low
C-4 16 195 17 103
C#-4 17 195 18 111
D-4 18 209 19 136
D#-4 19 239 20 178
E-4 21 31 21 237
F-4 22 96 23 58
F#-4 23 181 24 156
G-4 25 30 26 19
G#-4 26 156 27 159
A-4 28 49 29 68
A#-4 29 223 31 2
B-4 31 165 32 218

The full 8-octave table is Appendix J (page 221) of the User's Guide.

Data handling

DATA lines hold values inside the program; each READ takes the next one in order.

10 T = 0 : N = 0
20 READ X : IF X = -1 THEN 50
30 T = T + X : N = N + 1
40 GOTO 20
50 PRINT "AVERAGE ="; T/N
60 DATA 75, 80, 92, 64, -1
Rule Detail
Mixed types Numbers and text can share DATA lines; READ into the matching type (A or A$)
Commas or edge spaces in text Put the text in quotes
Ran out of values ?OUT OF DATA; end the list with a marker like -1
Start again RESTORE goes back to the first DATA value

Arrays

Command Meaning
A(5) = 12 Use without DIM for up to 11 items (0–10)
DIM A(100) Room for 101 items (0–100)
DIM N$(20) / DIM C%(50) Arrays of text / whole numbers
DIM B(3,4) Two-dimensional: 4 rows × 5 columns
DIM twice ?REDIM'D ARRAY: DIM each array once, near the start
Index too big ?BAD SUBSCRIPT
10 DIM F(6)
20 FOR I = 1 TO 1000
30 R = INT(6*RND(1))+1 : F(R) = F(R)+1
40 NEXT
50 FOR R = 1 TO 6 : PRINT R; F(R) : NEXT

Screen codes and CHR$ codes

The C64 has two numbering systems for characters. Screen codes go into screen memory with POKE. CHR$ (PETSCII) codes are what PRINT, ASC(), CHR$() and GET use. The letter A is screen code 1 but CHR$ code 65.

Screen codes

Code Uppercase/graphics set Upper/lowercase set
0 @ @
1–26 A–Z a–z
27–31 [ £ ] ↑ ← same
32 space space
33–47 ! " # $ % & ' ( ) * + , - . / same
48–57 0–9 0–9
58–63 : ; < = > ? same
64–95 Graphics (81 = ball, 86 = cross) 65–90 = A–Z
96–127 More graphics Graphics
128–255 Reverse-video versions of 0–127 same

CHR$ control codes

Code Does Code Does
5 White text 144 Black text
8 Lock case switching 9 Unlock case switching
13 RETURN 141 SHIFT + RETURN
14 Lowercase mode 142 Uppercase/graphics mode
17 Cursor down 145 Cursor up
29 Cursor right 157 Cursor left
18 Reverse on 146 Reverse off
19 Cursor home 147 Clear screen
20 Delete 148 Insert
28 Red 156 Purple
30 Green 158 Yellow
31 Blue 159 Cyan
129 Orange 149 Brown
150 Light red 151 Gray 1
152 Gray 2 153 Light green
154 Light blue 155 Gray 3
Function key f1 f3 f5 f7 f2 f4 f6 f8
CHR$ code 133 134 135 136 137 138 139 140
Code Character
32–63 Space, punctuation and digits (same as ASCII)
64 @
65–90 A–Z (a–z in lowercase mode)
91–95 [ £ ] ↑ ←
96–127 Graphics (SHIFT + key)
160 SHIFT + space
161–191 Graphics (C= + key)
193–218 A–Z in lowercase mode
192–223 Same as 96–127
224–254 Same as 160–190
255 π

Find any key's code: 10 GET A$ : IF A$="" THEN 10 then 20 PRINT ASC(A$) : GOTO 10.

Math functions BASIC doesn't have

Build them with DEF FN, e.g. DEF FN SC(X)=1/COS(X), then use FN SC(1.2). Angles are in radians; π is SHIFT + ↑.

Function BASIC formula
Secant 1/COS(X)
Cosecant 1/SIN(X)
Cotangent 1/TAN(X)
Inverse sine ATN(X/SQR(-X*X+1))
Inverse cosine -ATN(X/SQR(-X*X+1))+π/2
Inverse secant ATN(SQR(X*X-1))+(SGN(X)-1)*π/2
Inverse cosecant ATN(1/SQR(X*X-1))+(SGN(X)-1)*π/2
Inverse cotangent ATN(-X)+π/2
Hyperbolic sine (EXP(X)-EXP(-X))/2
Hyperbolic cosine (EXP(X)+EXP(-X))/2
Hyperbolic tangent (EXP(X)-EXP(-X))/(EXP(X)+EXP(-X))
Hyperbolic secant 2/(EXP(X)+EXP(-X))
Hyperbolic cosecant 2/(EXP(X)-EXP(-X))
Hyperbolic cotangent EXP(-X)/(EXP(X)-EXP(-X))*2+1
Inverse hyperbolic sine LOG(X+SQR(X*X+1))
Inverse hyperbolic cosine LOG(X+SQR(X*X-1))
Inverse hyperbolic tangent LOG((1+X)/(1-X))/2
Inverse hyperbolic secant LOG((1+SQR(1-X*X))/X)
Inverse hyperbolic cosecant LOG((SGN(X)+SQR(X*X+1))/X)
Inverse hyperbolic cotangent LOG((X+1)/(X-1))/2

The printed Appendix F mislabels the inverse cosecant and gives a wrong inverse hyperbolic cotangent; the versions above are the standard ones. Also handy: round to 2 decimals INT(X*100+.5)/100, remainder X-INT(X/Y)*Y, log base 10 LOG(X)/LOG(10).

Connections and pinouts

Pins are numbered as you look at the port from outside the computer.

Game ports

Pin Port 1 (front) Port 2 (rear) Note
1 JOY A0 (up) JOY B0 (up)
2 JOY A1 (down) JOY B1 (down)
3 JOY A2 (left) JOY B2 (left)
4 JOY A3 (right) JOY B3 (right)
5 POT AY POT BY Paddle
6 Button A / light pen Button B Fire
7 +5V +5V Max 50 mA
8 GND GND
9 POT AX POT BX Paddle

A/V port (8-pin 262° DIN)

The C64U puts out stereo on pins 3 and 5 (line level) and has no audio input. Pick the mode at Advanced Settings → U64 Specific Settings → Analog Video Mode. MIDI cables (270° DIN) look similar but are not the same.

Pin CVBS + S-Video mode (default) RGB mode (e.g. SCART)
1 Luminance (Y) Green
2 Ground Ground
3 Audio out left Audio out left
4 Composite video Red
5 Audio out right Audio out right
6 Chrominance (C) Blue
7 – Csync
8 – Fast switching

IEC serial and cassette

IEC pin 1 2 3 4 5 6
Signal SRQ IN GND ATN CLK DATA RESET
Cassette pin A-1 B-2 C-3 D-4 E-5 F-6
Signal GND +5V Motor Read Write Sense

Cartridge slot (44 pins)

Top pin Signal Bottom pin Signal
1, 22 GND A, Z GND
2, 3 +5V B ROMH
4 IRQ C RESET
5 R/W D NMI
6 Dot clock E φ2 clock
7 I/O 1 F–Y Address A15 → A0
8 GAME
9 EXROM
10 I/O 2
11 ROML
12 BA
13 DMA
14–21 Data D7 → D0

User port (with the optional adapter)

Pin Signal Pin Signal
1 GND A GND
2 +5V (max 100 mA) B FLAG2
3 RESET C–L PB0–PB7
4 CNT1 M PA2
5 SP1 N GND
6 CNT2
7 SP2
8 PC2
9 SER ATN IN
10, 11 9V AC (max 100 mA)
12 GND

Inside the C64U

Part What to know
Power socket 12V DC, center-positive; 2.5 A (30 W) with LED lighting, 1.25 A (15 W) without
SD card slot microSD for internal storage, shows in the File Browser
Internal USB-A Upper port only is active; extra internal storage
SID sockets (2, ZIF) Real or modern SID chips alongside the UltiSIDs; notch toward the back, lock lever down
User port connector For the optional user-port adapter
C64U keyboard connector USB-C shaped, but only for the C64U keyboard
C64 keyboard connector For an original keyboard; black wire to the leftmost pin (seen from the front)
Utility buttons header Reset, menu and freeze buttons
Power LED header 2-pin, 3-pin (power + disk) or 4-pin RGB LED; use the rearmost pins

Don't connect anything to unlabeled ports: they're for factory use and can damage the computer.

Programs to try

The User's Guide prints three complete programs to type in (pages 211–216).

Program What it is Teaches
Jotto (Jim Butterfield) Guess the computer's secret 5-letter word; it tells you how many letters match Strings, READ/DATA word lists, INPUT checks
Sequence A letter-sequence puzzle from the PET User Group Random strings, MID$, game loops
Piano Keyboard (Gene Deals) Turns the keyboard into a piano; function keys change sound and octave SID registers, GET for live keys

Words in braces mean keys, not text: {CLR/HOME} = SHIFT + CLR/HOME. Type them inside the quotes. A ?SYNTAX ERROR IN line number points to the line to check.