Introduction
Some weeks ago I got together with a coworker and we got interested in modding my poor old Commodore 64 computer. We are currently building an interface between the C64 and PC so that the computers can communicate via the cassette port. In fact, C64 does think that it is loading a program from a tape instead of another computer. This way there is no need to make modifications to the Commodore 64.
This has also brought up the interest to learn the Commodore 64 assembly language, a skill I never learned as a kid. All those LDAs and STAs were too much for me at the time and I played games and coded in Basic instead.
So few weeks ago I started to look at programming the good old Commodore 64 and found out that my Basic skills had pretty much dried away. I remember being rather good with Basic, but after reading the manuals I started to wonder how much of my Basic coding was just dumb copy-paste instead of really understanding the inner workings of the language and system.
After fiddling for a while I decided to start learning the assembly language. I decided to use Dasm cross-assembler so that I can write the code in some adequate editor and then cross-compile the code to be run either in emulator or inĀ real C64. I decided to start with a lousy “star-field” program that just fills the screen with some characters. In this post I will do an autopsy to the Basic version of the program and then move on to the assembler version in the future posts.

"Fancy" star effect aka. screen full of ...
The Basic version
10 print chr$(147)
20 x = (rnd(0)*1023)+1024
30 x = (rnd(x)*1023)+1024
40 y = 32
50 ch = peek(x)
60 if ch=42 then y=81
70 if ch=81 then y=87
80 if ch=32 then y=42
90 poke x,y
100 get i$
110 if i$<>”" then stop
120 goto 30
Line 10 clears the screen.
Line 20 gets a random number between 1024 and 2047 using the internal clock (or some such) as the seed.
Line 30 does the same, but uses the previous random number as the seed. If I would comment out this line, the characters would come up in steady columns like in Matrix (not really, but you get the idea). Such a good random number generator.
Why 1024-2047? As it happens the screen resides in memory locations 1024-2047 and we use this random number to display a character in certain location. For example “POKE 1024,81″ would display a “ball” in the upper left corner of the screen.
Line 40 puts value 32 (space) to variable “y” which we use later on to display stuff on screen.
Line 50 looks at the current character in given location (where our random number points to).
lines 60 to 80 just compare the read value so that if it is
- asterisk (42), then put 81 (ball) to variable “y”
- a ball (81), then put “a donut” (87) to variable “y”
- a space (32), then put an asterisk (42) to variable “y”
- otherwise retain the old value of 32 in variable “y”
Line 90 does all the work and puts the character in variable “y” to the random screen location “x”.
Line 100 reads user input (one keypress).
Line 110 checks it any key was pressed, if so then stop the program execution.
Otherwise in line 120 jump back to the line 30 and start over again (without clearing the screen, of course).
Looks pretty easy, right?
Filed under: Commodore64 | Leave a Comment »