Category Archives: Screencast

Catch the WP Guru Video Podcast where Complex Stuff gets made extremely simple. These are screencasts on a variety of topics, such as WordPress, Dreamweaver, iOS Development and a lot of other super complicated stuff.

Check it out on iTunes
or subscribe with your favourite Podcast client

Writing HELLO WORLD in Machine Language on the Commodore 128

The Commodore 128 has a built-in machine language monitor which makes it ideal for ML development. However, most (or pretty much all) documentation on this subject is geared towards the Commodore 64, making it slightly difficult to get a head start in writing ML code for the 128.

Before I forget how to do it, here are a few pointers – courtesy of Jim Butterfield’s book “Machine Language – Expanded Edition”.

Getting Started

Let’s begin by typing MONITOR in C128 mode. It’ll take us to the machine language monitor. We’ll start our programme at $0B00. To begin assembling our code, we’ll type A 0B00 (A for Assemble), followed by these lines:

The MONITOR will turn this text into the output you’ll see in the screenshot above (the lines starting with a . dot). Here’s what this code will do when called:

First we’ll load the X register with a value of zero. We’ll use this register as a counter. In the next line we’ll load the accumulator with whatever is stored in address $0C10 plus whatever is stored in the X register. So if X has a value of zero, then the contents of $0C10 will be loaded. If X was 1, then the value in $0C11 would be loaded, and so forth.

We’re using this as ASCII representation of our text (Hello World in a box in this case). With JSR $FFD2 we’ll call a Kernal routine that prints a single character onto the screen. Now we’re incrementing X by one and ask if it’s 45 yet (CPX #$2D). This would indicate that we’ve printed all the characters we need. If that’s not the case, we’ll return to line 2 and keep printing. Otherwise, we’ll stop the programme.

Storing ASCII characters

You’d think it was possible to simply type in text in the MONITOR. But of course that would be too easy. Instead we need to grab one of those massive tables and hack in each character’s ASCII code in hex. How convenient!

Type M 0C10 (or whichever location in memory you’d like to store your text string at) and overtype the numbers at the start of the line, each one representing a single byte of our ASCII text. At the end of each line you’ll see what those characters look like when converted.

In my case it’s a total of 45 characters, beginning with a return, followed by the top of the box, HELLO WORLD, and the bottom of the box.

Running from the MONITOR

To start the programme from the monitor, we’ll type G F0B00. We’ll end up with a SYNTAX ERROR and back on the BASIC screen though due to the RTS command at the end of the listing. If we replace it with a BRK command instead, we’ll end up back in the MONITOR.

The important thing to remember is the five digit addressing mode on the C128 (i.e. G for GO, followed by F0B00). Our programme starts at $0B00 in memory, but to make it run properly we’ll have to specify which BANK it should be called from. Anything other than BANK 0 or BANK 1 is fine, otherwise we won’t reach the print routine at $FFD2. In my example I’m choosing F, but E would work fine too (as we’ll see in a moment).

Running from BASIC

Type X to exit the monitor and go back into the land of BASIC. First we’ll need to choose a BANK. We’ll have 16 to choose from (0 to 15), so perhaps let’s try BANK 15. Now we’ll need to type the start of our programme in decimal:

or we can use the DEC command to convert hex to decimal on the fly:

Saving the programme

From the MONITOR, we can save the programme using the S command. It needs to be followed by a name (in double quotes), followed by the drive number, memory start and memory end plus one byte – all separated by a comma. It’s probably easier to show than to write:

We’re saving more bytes than strictly necessary here due to the large gap between our code and the beginning of the ASCII string. Our string could go up to $0C3F. The last byte in $0C40 is NOT saved to disk (or tape).

We can do the same from BASIC using the BSAVE command (for Binary SAVE). The syntax is BSAVE “FILE NAME”, P1234 TO P5678. Sadly the DEC command doesn’t work inline with this command, which would make it extremely useful. We’ll have to convert the values into decimal manually instead.

Loading the programme

To bring our masterpiece back into the computer from the MONITOR, the L command works a treat:

From BASIC we can load the programme the usual way, making sure we load it with ,DEVICE,1 at the end. This is to make sure it is loaded into the same memory it was saved from, rather than the start of BASIC:

Happy Assembling!

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

Lottery Statistics in Commodore BASIC

In this episode I’m adding statistics support to my previous lottery generator on the Commodore 64.

I’ll add an array that is updated if my supplied numbers have been matched, and how many times over how many draws this has happened. I’ll also add an option to pause the programme and display the statistics before random draws can continue.

When this app is run continuously it will collect statistical data on how many lottery draws are necessary to match all supplied numbers.

PS: By the time the video had uploaded, my emulator had drawn over one million sets, and none of them have matched my numbers 🙁

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

Matching Lottery Numbers on the Commodore 64

In this episode I’m amending my previous lottery number generator to take six lucky numbers from the user to match against the randomly drawn numbers from the Commodore 64.

This will allow us to compare what the computer has drawn to the user’s input, as well as keep drawing numbers until the user input comes up. It’ll be an interesting experiment to see how many draws that will take…

Enjoy!

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

Working with Keyboard Input in Commodore BASIC

In this episode I’ll show you three ways to take user input from the keyboard in Commodore BASIC.

The INPUT and GET commands work on all systems, while the GETKEY command only works on the Plus/4 and C128. I’ll demonstrate how to use all of them.

This will come in handy for our little lottery programme we’ve been working on, so that we can prompt the user for some numbers to compare against later.

Enjoy!

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

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: