Programming in C64 BASIC
The GAMES CAROUSEL gives you access to the BASIC programming language.
If you want to follow through the BASIC examples, we strongly recommend connecting a USB keyboard to THEC64 Mini, rather than trying to use the Virtual Keyboard. Please read CHAPTER 6: KEYBOARDS In the User Guide PDF before starting this introduction to BASIC.
If a compatible USB memory stick (formatted to FAT32) is attached to THEC64 Mini before BASIC is launched, then you can save your BASIC code to the memory stick. See Saving and Loading below for further information.
WHAT IS BASIC?
When you power-on a C64 computer, the first thing you see is BASIC. We provide access to BASIC on THEC64 Mini for those who wish to experience programming using version 2 of the C64 variant of the Beginners All-purpose Symbolic Instruction Code.
BASIC is a high-level programming language, designed to be easy to use. It is an interpreted language, meaning any BASIC instructions you type have to be translated by the computer before it can run them. This actually happens quite quickly and is done invisibly and automatically by the computer.
Computers like the original C64 and THEC64 Mini natively ‘talk’ in machine code, which is a series of zeros (0) and ones (1). When they use machine code, they operate very fast. However, people don’t directly program in machine code, so we use a different way to send instructions to the computer, using programming languages that are closer to what we are used to.
Some programming languages are a few steps closer to machine code, and so they are interpreted (or translated if you prefer) quicker than those that aren’t.
BASIC has always been an excellent introduction to programming for complete beginners even though it is quite a few steps away from machine code.
Learning to program using C64 BASIC helps you think programatically, and it also teaches you how to type on a keyboard. Both of these skills are important to learn.
C64 BASIC has a series of keywords that tell the computer what to do next. Learn those and you will be writing BASIC code in no time at all. There are 71 BASIC keywords to discover.
If you think 71 sounds a lot, don’t worry. You don’t have to learn them all at once. Many of them are related to doing mathematical calculations, and only come into play when you start doing more complex coding. More on keywords later!
LAUNCHING C64 BASIC
When you first launch BASIC from the GAMES CAROUSEL on the HOME screen, you see a blue screen. It begins by revealing how much computer memory is available for programming. Underneath that information is a READY prompt and underneath that is a steadily blinking square cursor.
Staring at the cursor won’t get you very far. It’s ready and waiting for you to enter your first BASIC commands.
There are two ways of entering BASIC code. You can type it in and press RETURN. The computer will immediately try and run your code. However, to repeat the same code later on, you have to type it in again. That’s not so good. The second method is a lot more efficient. You place numbers at the start of each line of your code that tells the computer in which order to run them. The great thing is that using line numbers also means it won’t run your code until you type the RUN keyword *and* it means you don’t have to type it all in again (as long as you don’t turn off the computer).
YOUR FIRST BASIC PROGRAM
Below is an example BASIC program. We will look at each line of code in turn before doing this on THEC64 Mini.
There are three separate lines of BASIC code to explain.
10 PRINT “RETRO GAMES LIMITED”
20 PRINT “HELLO WORLD!”
30 GOTO 10
Line 10 uses the keyword PRINT which sends whatever appears between the quotation marks “” to the computer screen (and not to a printer as you might expect!).
Line 20 does the same as line 10 but will send different words to the screen.
Line 30 instructs the computer to go to line 10, which places the computer in a loop of running line 10, then line 20, then line 30 telling it to go back to line 10 again and so on.
The line numbers are in units of ten just in case we want to insert extra lines of code, e.g. a line 15 that comes between line 10 and line 20. If you type a line of code that begins with the same line number as an existing line, the new line will replace the old one as soon as you press RETURN, without any warnings. If you type just the line number and nothing else then press RETURN, you delete that line number.
Now type each line exactly as you see it (above). Press RETURN at the end of each numbered line to commit that line to the computer’s memory and to move down a line, before starting to type the next one.
Press RETURN at the end of line 30.
If you make a mistake, use the Backspace key on your USB keyboard (or press the C button on the joystick when using the Virtual keyboard). Each press will erase the character immediately to the left of the cursor’s position on the screen.
If you’re happy that you’ve typed everything in correctly, type the RUN keyword (without a line number). Your code will then run!
If you’re wondering how to stop your new BASIC code from running forever, try pressing ESC (on a USB keyboard) or RUN/STOP (if using the virtual keyboard). You have instructed THEC64 Mini to BREAK into your code. Don’t worry – it’s not as bad as it sounds!
To see your code is still intact, you can type the following keyword at the square cursor:
Your three lines of code are displayed, safe and sound just as they were the last time you saw them.
Believe it or not, you’ve already used four BASIC keywords. To prove it, here is a table showing all 71 of them.
We’re now going to slightly change your code so that it only prints the two lines of text five times before stopping, rather than going on forever.
5 FOR A=1 TO 5
10 PRINT “RETRO GAMES LIMITED”
20 PRINT “HELLO WORLD!”
30 NEXT A
You should immediately notice that we have added a line number 5 and we have changed what was on line 30.
Look at these two new lines of code for a moment and ignore lines 10 and 20 in-between. These lines introduce two new concepts to your BASIC knowledge as well as two new keywords.
One concept is defining and using variables in your code. In this example, we are using ‘A’ as a container to temporarily store a number inside. The letter A was chosen but it could easily have been the word ‘WORLD’, so it could have read…
5 FOR WORLD=1 TO 5
(lines 10 and 20 remain the same as before)
30 NEXT WORLD
… and your program would still have worked. You could also have changed the number at the end of your line of code from 5 to 6. Variable names are chosen by you and can (almost) be anything you like. However, there are rules to naming these handy variables for storing things in.
You can’t use reserved keywords used by BASIC or by the C64 for its own system variables. How do you know which ones can’t be used? Well, if you had used LIST as your variable name, running the program would have produced:
?SYNTAX ERROR IN 5
It’s not the most helpful of errors, but if THEC64 Mini returns that error, you know something is wrong. Remember that list of C64 BASIC keywords? Don’t use any of those as names for your variables and you can’t go far wrong.
Try to keep your variable names short, but relevant where you can. If your code isn’t overly complex, then you can just use A, B, C and so on, just as long as you remember what they are used for!
Types of Variable
The only other thing to know is that there are three types of variable, and they are defined by what type of information is stored in them.
The two most common that you will use are integer and string variables.
An integer is simply a whole number, so no fractions or decimal points. A string is letters or letters and numbers.
How do you say which type of variable you want to use? For integers, you just use a name for your variable, without anything else, e.g. WORLD. For strings, you add a $ to the end of the variable name, so RECIPE$ might be used to store the name of your favourite cake or biscuit recipe, e.g. “Rocky Road”.
So, looking at your amended BASIC example, we know that ‘A’ is used to store a whole number (which can actually be between -32768 up to 32767). In our example, it is only going to reach a maximum value of 5.
Now let’s discuss the FOR and NEXT keywords. These are paired together and create a FOR… NEXT loop. What this does is from the FOR keyword, the computer stores a number inside ‘A’ that starts at 1 and will end at 5 (in this particular example).
Each time the running code encounters the NEXT ‘variable’ (NEXT A in our example), it returns to the line with the FOR keyword and increments (adds one to) the current number stored in the variable called ‘A’. The code then runs each line of code it comes across afterwards (i.e. line 10 and line 20) until it hits NEXT A again, then it returns to the FOR keyword and repeats the process until the value of ‘A’ equals 5, then it stops.
So, add in line 5 exactly as shown previously, change line 30, and then RUN the amended code and see what happens.
Now, it would be useful to show what the current value of ‘A’ is. Add a new line 25:
25 Print A
Now RUN your code again.
You can now see the two lines of printed text are followed by a number that goes up by one each time, e.g. 1, 2, 3, 4, 5.
It’s useful to see the value stored in variable ‘A’ as you can now see how it changes within that FOR… NEXT loop, adding one automatically whenever it reaches NEXT.
Let’s now make this look a little better by adding something to line 25.
25 PRINT “VARIABLE ‘A’ IS NOW“;A
The semi-colon at the end tells BASIC to keep the cursor on the same line as the text it is printing to the screen, and then display the value of the variable A.
RUN this latest version of your code.
That’s a little better. It’s clear now what those numbers mean and we’ve learned how to place the value of a variable into a PRINT command at the same time!
That almost concludes the introduction to C64 BASIC. However, before we finish, let’s learn how you can keep (save) your code so it can be run and amended in future.
Saving and Loading
On THEC64 Mini, you have two available methods for saving your BASIC programs:
1. Press the MENU button and select ‘Save/Load game’ and then save to an available slot on THEC64 Mini as you would do for a game. See CHAPTER 5: PLAYING GAMES in the User Guide PDF for more details on saving and loading to slots.
2. If a compatible USB memory stick (formatted to FAT32) is attached to THEC64 Mini before BASIC is launched, then you can save your BASIC code to the memory stick. When launching BASIC, THEC64 Mini looks for a specific disk image file on the memory stick. If the file isn’t found, then a THEC64-drive8.d64 file is automatically created on the USB memory stick for you. You will then be able to save and load to and from this file from BASIC.
Please note that to accommodate THEC64 Joystick, a USB Keyboard and a USB memory stick at the same time, you need to connect a separate USB hub (not supplied) which gives access to additional USB ports. Retro Games Ltd cannot guarantee that all USB hubs will work with THEC64 Mini and some hubs require a separate power source.
With a compatible USB memory stick connected, you can save to the disk image using the standard SAVE to disk command, e.g.
The name of the file goes between the speech marks and it can be whatever you like, as long as it isn’t longer than 15 characters. In this example, it’s nice and short. The number 8 after the filename is a device ID number that is allocated to the disk image file on your USB memory stick.
Press RETURN afterwards to begin the save.
BASIC will report ‘SAVING’ followed by your chosen filename, and when it is completed, it returns to the READY prompt and the square cursor.
To save over an existing file with the same filename, add @0: to the front, like this:
Please be aware that C64 BASIC won’t give any warnings that you are over-writing the file when you do this.
You can check that the save worked by using the VERIFY keyword.
If everything is okay, you will see VERIFYING followed by OK.
Another time, ensure that the same USB storage device is connected to THEC64 Mini before you run BASIC, and then type the following command in BASIC to get your program back.
What’s on my disk?
From BASIC, you can look at a disk and see what files are on there. To do that requires use of the LOAD command, but in a slightly different way from before. Instead of typing a filename we are using the reserved variable $.
BASIC will report SEARCHING FOR $ followed by LOADING and then it returns to the READY prompt once again.
To see what’s on the disk is simple. Just type the following command:
Instead of listing BASIC code, this time the command shows what is on the disk.
In the above example, we have just one program on the disk and it’s the program we just saved called RGL. The program uses up 1 block on the disk and there are 663 blocks still free to save programs to in the future.
Be aware that if you load the directory of a disk and have some BASIC code on the C64 at the same time, the $ listing will replace the BASIC listing in the computer’s memory and you will lose your program (if you haven’t already saved it).
If you want to use multiple disks, then you will need to use a different USB memory stick for each disk, or temporarily rename the files because the .D64 file is always saved with the same filename – THEC64-drive8.d64 – each time.
If you don’t have a USB storage device attached, but decide to try saving to device 8 anyway, BASIC will look like it has successfully saved but when you load the directory of the disk it will be empty. This is because without USB storage, BASIC uses a disk image that can only be read, not saved to. The tell-tale sign is the name of the disk, which is READONLY instead of THEC64. The disk is completely empty and cannot have anything saved to it.
To overcome this, you can always save in one of the four save slots, switch off THEC64 Mini, insert a USB memory stick into a spare USB port, switch THEC64 Mini back on, load BASIC, restore the save slot and then save to the D64 disk image using the SAVE command detailed earlier. Device 8 will now be the disk image on your USB memory stick.
Before we finish, how many C64 BASIC commands have you used now?
You’re up to 11/71 commands already! We hope this small introductions has given you an interest to learn more. There are plenty of online resources available. Also, have a look at our Links page.
A video of this quick introductory tutorial is also available to watch below.