Tag Archives: C64

Everything about the Commodore 64, or C64 or CBM 64. Some even called him the VIC-64. I got mine in the mid eighties, used from a neighbour – complete with monitor and 1541 disk drive (vintage 1982/1983), and even a needle printer. I later got hold of a C16 Datasette (the dark grey one) which connected to my C64 via an adaptor. I used it until 1992/1993.

How to print numbers as columns in Commodore BASIC

In this episode I’m demonstrating how to print numbers in evenly spaced columns in Commodore BASIC.

On the C128 and the Plus/4 we can use a nifty little function called PRINT USING for this, with which we can format the output of any printed text or variable.

On the C64 and VIC-20 that function doesn’t exist, so we’ll have to convert a numeric value into a string (using the STR$ function), and then determine how long our string is. Following that we’ll have to manually pad our string value with as many spaces as are required. Continue reading How to print numbers as columns in Commodore BASIC

Watch the full course in one convenient playlist:
Catch this episode on my Commodore Podcast:

Sorting an Array on the Commodore 64

In this episode I’ll demonstrate how to sort a numeric array on the Commodore 64. The same principle works for string arrays, and of course on all other Commodore BASIC computers.

The technique I’m using here is called Bubble Sort: in effect we’re comparing the first two items in the array, and if the left one is larger than the right one, the values are swapped around. This loop continues until all items in the array have been compared and sorted (hence the smallest items “bubble” to the front of the array, much like the smallest bubbles in a soda float to the top first).

Here’s the full code I’m building, including the lottery portion. The Bubble Sort code starts in line 200.

I’ve explained how to build the lottery generator in this code here: https://wpguru.co.uk/2018/03/how-to-generate-lottery-numbers-on-the-commodore-64/

Happy retro hacking!

Watch the full course in one convenient playlist:
Catch this episode on my Commodore Podcast:

How to generate Lottery Numbers on the Commodore 64

In this episode I’ll demonstrate how to draw random lottery numbers on a Commodore 64. The secret sauce here is not only the RND function to generate random numbers, but also two loops inside each other that prevent the same number from coming up more than once.

Here’s the lottery generator code:

To adapt this listing to match your local lottery, change line 20 to the amount of numbers to be drawn from your pool (6 in my example), and change the value in line 30 to match the size of your pool (49 in my example).

Any questions, please let me know below.

Happy retro hacking!

Watch the full course in one convenient playlist:
Catch this episode on my Commodore Podcast:

How to build a Word Splitter on the C64 in Commodore BASIC

In this episode I’m demonstrating how to build a word splitter on the Commodore 64. We’ll use string functions to parse a sentence and split each word off into an array of words so that they can be analysed later (for example, as part of an adventure game).

Here’s the code I’m building:

The interesting part starts in line 100 and onwards, where I’m building a subroutine that deals with the string functions. In line 110 I’m resetting/initialising two of the three important variables: LT$ holds a single letter from the phrase we’re getting in A$, while WD is counting each word we’re splitting out.

The FOR loop in line 120 parses each letter of the phrase, and if it finds a space character (line 140), the word count is increased. If the letter is not a space, then it’s added to the current word held in WD$(WD). The current word is assembled character by character.

Apologies for the audio quality, I did this on my laptop while sitting on the balcony, hence sea planes flying overhead can be heard (as well as the neighbours dog and kids).

Happy hacking 🙂

Watch the full course in one convenient playlist:
Catch this episode on my Commodore Podcast:

How to build a time of day clock on the Commodore 64

In this video I’ll demonstrate how to build a simple clock on the C64. We’ll go through this process step by step, including the built-in TI and TI$ variables, string formatting with LEFT$, RIGHT$ and MID$, as well as screen formatting.

Here’s the code I’m writing – works in Commodore BASIC v2 and above:

Many of the characters that appear in this listing are cursor control characters and appear in reverse in the video. They either position the cursor or print PETSCII graphics.

Inspired by David’s video, in which he connects an LCD screen to his C64’s User Port: https://www.youtube.com/watch?v=vV8FbwobrKY

Watch the full course in one convenient playlist:
Catch this episode on my Commodore Podcast:

How to create random YouTube URLs in Commodore BASIC v2

In this episode I’ll demonstrate how to create those seemingly random YouTube Video IDs using a Commodore 64.

Here’s the code I’m writing – works in BASIC v2 and above:

The first line switches to lower case letters (I forgot to show that in the video).

NOTE: In addition to the upper case and lower case alphabet, and the numbers 0-9, YouTube also use two special characters that I forgot to mention in the video. One is the standard minus sign (-), and the other one is the underscore (_). The Commodore machines cannot produce the latter. For simplicity’s sake, I’ve left both of those out (just though I’d mention it here).

Inspired by Tom Scott’s video “Will YouTube ever run out of Video IDs” – watch it here: https://www.youtube.com/watch?v=gocwRvLhDf8

Watch the full course in one convenient playlist:
Catch this episode on my Commodore Podcast:

String Operations on Commodore Computers

Commodore BASIC has some interesting and simple string functions built in. Three of them are self explanatory: LEN, LEFT$ and RIGHT$. But others, like the mysterious MID$ and INSTR functions, are a little tricker, and I can never remember how they works.

So here’s a quick recap on how they all work.

LEN (A$)

Returns the length of any given string. For example,

a$=”the cake is a lie”

print len (a$)
17

returns 17, which is the number of characters in our string.

LEFT$ (A$,X)

The LEFT$ function takes the x left characters from a given string. Here’s an example:

We get “one”, because those are the 3 leftmost characters in our string a$.

RIGHT$ (A$,X)

Likewise, RIGHT$ takes the x right characters from any given string:

Here we get “three”, because those are the 5 right characters of a$.

MID$ (A$,X,Y)

MID$ is a little more complex. It takes x characters from a given string, starting at position y. Let’s look at our earlier example again:

We get “two”, because those are the 3 characters, starting at position 5. The first position in all these string operations counts as one rather than zero.

But did you know that MID$ can also be used to assign and replace different characters in a string? Consider this:

Now we’ve replaced the 3 characters in our string with another string, starting at position 5.

I had no idea it cold do that! All these string operations work in all variations of the Commodore BASIC, except for the MID$ assignment which only works on the Plus/4 and the C128.

 

INSTR (A$, B$)

On the Plus/4 and C128, we can even check if one string is contained in another and at which position this occurs. Consider this:

In our example, INSTR returns 5 because “cake” has been found at position 5 of “the cake is a lie”.

We can also specify a position from which the search shall be started like this:

Now INSTR returns 0 because “cake” has not been found beyond position 6 of our input string.

How to use direct block access commands in Commodore DOS

Commodore-Logo-PaddedWe can access each sector’s raw data on Commodore disk drives with direct block access commands. Supported drives include the 1541, 1571, the VICE emulator as well as the SD2IEC card reader (for the most part).

Each single sided floppy contains 35 sectors, while a double sided 1571 formatted disk contains 70 sectors. Each track contains between 17 and 21 sectors depending on how far inside or outside they are. Each sector contains 255 bytes we can read or write.

Sectors are the same as blocks: only the directory refers to them as “blocks” and shows us how many we have available.

We’ll need to open two channels to our disk drives: a command channel and a data channel. Here’s how to do it:
Continue reading How to use direct block access commands in Commodore DOS

How to split a long string into separate words in Commodore BASIC

Here’s a quick word splitter routine for CBM BASIC. It takes phrase and “explodes” all words into an array, removing spaces. Feel free to adopt it for your own needs.

Line 50 clears an array of 10 words called WD$(n). We also setup a word counter called WD. So in this example we can only detect a maximum of ten words. If you need more, you must DIM the array first.

Lines 70 loops through all characters in our phrase. We make use of the first element in our array WD$(0) to store each single character. If it’s a space, we’ll increase the word counter and move on to the next character. If it’s not (line 80), then we’ll add this character to the current word which is stored in WD$(WD).

The rest of the code simply prints each word on a new line to see our handy work.

The evolution of Flow Control in Commodore BASIC

Commodore-Logo-PaddedWhen I had a C64 back in the days, the only IF/THEN statement we could create in BASIC v2 was just that: IF and THEN.

There was no ELSE, and everything had to be on a single line. It was the only flow control we had back in those days, and it was a severe limitation compared to what we can do with higher level languages today.

Here’s an example:

Such constructs weren’t very convenient when you wanted to react to anything other than the tested value. Instead you had to write an IF statement for every possible other outcome which could make the code very cluttered with several GOTO statements.

When the Plus/4 came out Commodore added the ELSE statement. It still had to be on the same line as the IF and THEN, but at least we had ELSE as an option:

ELSE needs to be on the same line as the IF/THEN statement and separated by a colon.

ELSE was a very handy addition to the BASIC arsenal, but it still didn’t solve the problem that your evaluation had to be cramped into a single line. This was improved on the C128 with the introduction of BEGIN/BEND.

BEGIN and BEND allowed you to execute as many lines of code as you wanted between those statements when a condition was true (or false). Used together with IF, THEN and ELSE it was a very useful thing to have:

BEGIN means that everything starting from now until BEND is met will be executed as part of the IF/THEN statement – as many lines of code you like.

When BEND is met the code continues as normal – but we can also use ELSE after a colon on the same line, indicating that this is still part of the first IF/THEN statement. Here we can use BEGIN again for as many lines as we like, until we finish with BEND again.

It was very very cool!

 

Herdware

sys 32800,123,45,6 to show these credits (thanks, team!)