INTRODUCTION
============
Commodore 64 BASIC (Beginner's All-purpose Symbolic Instruction Code) is a dialect
of the BASIC programming language.
It is based on Microsoft BASIC and includes a set of instructions for writing
programs on the C64.
Version: BASIC V2
Memory available: ~38911 bytes
COMMANDS
========
- LOAD "filename",8 ; Load from disk drive
- SAVE "filename",8 ; Save to disk
- LIST ; Show current program
- RUN ; Execute the loaded BASIC program
- NEW ; Clear current program from memory
- CONT ; Continue a program after STOP
- CLR ; Clear variables
- SYS address ; Jump to machine code at specified address
- POKE addr, value ; Write value to memory address
- PEEK(addr) ; Read value from memory address
CONTROL STRUCTURES
==================
- IF condition THEN statement
Example: IF X > 10 THEN PRINT "Too big!"
- FOR var = start TO end [STEP increment]
Example: FOR I = 1 TO 10 : PRINT I : NEXT I
- GOTO line_number
- GOSUB line_number / RETURN
- ON expr GOTO line1, line2, ...
Example: ON X GOTO 100, 200, 300
FUNCTIONS
=========
- RND(x) ; Return pseudo-random number
- ABS(x) ; Absolute value
- INT(x) ; Truncate to integer
- SGN(x) ; Sign of number
- LEN(string) ; Length of string
- LEFT$(str,n) ; Left n chars of string
- MID$(str,start,n) ; Middle part of string
- RIGHT$(str,n) ; Right n chars
- CHR$(code) ; Return character
- ASC(char) ; Return ASCII code
INPUT/OUTPUT
============
- INPUT var
- PRINT expr
Example: PRINT "HELLO WORLD"
- GET var
Example: GET A$ ; Read a single character into A$
EXAMPLES
========
10 PRINT "HELLO WORLD"
20 INPUT "WHAT IS YOUR NAME";N$
30 PRINT "HELLO ";N$
40 FOR I=1 TO 5: PRINT I: NEXT I
50 POKE 53280,0 : REM SET BORDER COLOR TO BLACK
ERROR CODES
===========
?SYNTAX ERROR ; Typing mistake or illegal syntax
?OUT OF MEMORY ERROR ; Not enough RAM
?TYPE MISMATCH ERROR ; Wrong variable type used
?UNDEF'D STATEMENT ; GOTO to a line that doesn't exist
MEMORY MAP
==========
Commodore 64 Memory Map (partial overview):
0000-00FF Zero Page
0100-01FF Stack
0200-03FF Screen Editor Buffer
0400-07FF Screen RAM (default: 1024)
0800-9FFF BASIC Program/Text
A000-BFFF BASIC ROM
C000-CFFF Free for machine code
D000-DFFF I/O Area (VIC-II, SID, CIA)
E000-FFFF Kernal ROM
GRAPHICS & SPRITES
==================
VIC-II Graphics Chip:
Screen Resolution: 320x200 (hi-res), 160x200 (multicolor)
Colors: 16 fixed
Screen RAM starts at $0400 (default)
Color RAM: $D800 - $DBE7
SPRITES:
- 8 hardware sprites
- Each sprite is 24x21 pixels
- Enable: POKE 53269,1 for sprite 0
- X/Y position: POKE 53248 + N*2, X : POKE 53249 + N*2, Y
- Multicolor: POKE 53276,1
SOUND & SID CHIP
================
SID (Sound Interface Device) at $D400
Voice 1:
- Frequency Low: 54272 ($D400)
- Frequency High: 54273 ($D401)
- Waveform Control: 54276 ($D404)
- Attack/Decay: 54277 ($D405)
- Sustain/Release: 54278 ($D406)
Example:
POKE 54296,15 ; Volume = max
POKE 54272,100 ; Set frequency low byte
POKE 54273,0 ; Set frequency high byte
POKE 54276,17 ; Triangle waveform + Gate on
To silence: POKE 54276,0
DISK/TAPE I/O
=============
Disk Commands (with device 8):
- LOAD "PROGRAM",8
- SAVE "PROGRAM",8
- OPEN15,8,15,"I" ; Initialize drive
- PRINT#15,"S0:FILE" ; Scratch file
- CLOSE15
Tape:
- LOAD and SAVE work without specifying device
- Tape counter and motor control not programmable
USR FUNCTION AND MACHINE CODE
=============================
USR Function:
You can define USR(x) to call a machine code routine.
Example:
POKE 785,0 : POKE 786,2 : POKE 787,0 ; Set USR pointer to 512
POKE 512,96 ; RTS instruction at $0200
PRINT USR(0) ; Call the routine
You can embed your own machine code starting at 512 (or other locations).
KEYBOARD SCAN CODES
===================
Each row of the keyboard matrix is mapped to a specific bit in a CIA port.
Example:
- Row 0 (Ctrl, 1, Q, etc.): POKE 56320,254 : PRINT PEEK(56321)
- You can detect keypresses by scanning each row with POKE/PEEK.
COLOR CODES
===========
0 - Black 8 - Orange
1 - White 9 - Brown
2 - Red 10 - Light red
3 - Cyan 11 - Dark grey
4 - Purple 12 - Grey
5 - Green 13 - Light green
6 - Blue 14 - Light blue
7 - Yellow 15 - Light grey
To set border color: POKE 53280, color
To set background: POKE 53281, color
TOKENS AND ABBREVIATIONS
========================
BASIC keywords can be shortened:
- PR. = PRINT
- G. = GOTO
- L. = LIST
Each keyword is stored as a single byte token internally:
PRINT = $99, IF = $89, FOR = $81, etc.