Commodore Banner Exchange

Drawing the United Kingdom flag with BASIC (Commodore 64, Sinclair Spectrum)

Drawing the United Kingdom flag is an interesting exercise, especially if we want to draw it by using CBM BASIC V2 on a Commodore 64. The idea was born because I’ve found a nice Sinclair Spectrum program that draws this flag, on the Spectrum 16K/48K manual.

The Spectrum program makes use of the graphics commands offered by Sinclair BASIC and seems to be quite fast. Since I have bothered to type it in, here is the .tap image if you are interested on this piece of coding.


DOWNLOAD: UK flag (Sinclair Spectrum, from the Sinclair manual)


I am quite impressed by the program, as it performs filling tasks very quickly for BASIC. Here is the image created by the Spectrum:



The flag is drawn so that colour clash is avoided. So, you can notice some differences from the original flag, but that’s of little importance and hardly noticeable after all.

A Commodore 64 BASIC V2 version using the bitmap screen would be quite slow. But, we can use the hardware text mode.

The drawing seems way too complex to be drawn fast without a line command. But, the coloured stripes only have one slope: 30 degrees. So, we can just create a couple of programmable characters, mirror them in both X and Y directions, then plot those special characters according to the 30 degrees slope.


As you can see from the above drawing, we can draw a line with a 30 degrees slope by just plotting a couple of characters, repeated and repeated.

To draw the UK flag, once we have those two characters, we only need a few more ones that can be obtained by mirroring. For instance, we can mirror characters 1 and 2 in the X direction, obtaining two new mirrored characters. Then, we can mirror all those four characters about the Y direction, obtaining another four mirrored characters.

Overall, eight characters are enough. But, the following program also uses reverse characters: that allows for a better geometry of the flag.

The Commodore 64 BASIC V2 version follows:


DOWNLOAD: United Kingdom flag (Commodore 64 BASIC)


This program creates a set of re-programmed characters and uses them to plot the UK flag on the text screen. Characters are plotted using the KERNAL routine PLOT (with a direct call to ROM).

Reverse spaces are used to fill in areas. The result is as follows:



Like on the Spectrum version, the drawing has been made so that on every 8×8 pixels character cell there is only one color. This is useful on the C64 version as well, since we can easily plot each stripe one at a time (we are only using a restricted character set and we don’t have access to individual bits). If we had two stripes sharing the same character cells, things would have been a little more difficult in BASIC V2. Multicolor mode and a more complex character set would have been needed.

I have not disabled the SHIFT + C= key combination so that you can better see how characters are plotted:



Each diagonal stripe is drawn by incrementing the X plot coordinate by two and the Y plot coordinate by one. This way, we have characters-made lines with a 30 degrees slope.

To create the character set, a special bit reversal routine has been used. You can find it from line 5100. This routine is used to mirror each byte of a character shape in the X direction. Basically, if you have an 8 bit number, you can mirror it by using the following process:

  • split the 8 bit number in two 4 bit ones;
  • swap their positions;
  • now, split each 4 bit number into two 2 bit numbers;
  • swap their positions, in couples;
  • at this point, split each 2 bit number in a bit digit;
  • swap the positions of all those digits, again in couples.

For example, if we have to find the reversal of the binary number 10000000 (128 decimal):

Split the original 8 bit number in 4 bit numbers:

1000 0000

Swap places:

0000 1000

Split again:

00 00 10 00

Swap places between couples:

00 00 00 10

Split again:

0 0 0 0 0 0 1 0

Finally, swap again between couples:

0 0 0 0 0 0 0 1

So, we get:


Which is the mirrored original number in the X direction.

In order to efficiently split numbers, the 8 bit reversal routine in the code makes use of divisions and ANDs. One might be tempted to use strings, but that would be either slow and unsafe (again, due to slow garbage collection possible concerns).

The 8 bit reversal routine alone is also available.


DOWNLOAD: 8 bit reversal routine


This program is actually two programs in one. The program starting from line 100 is a classic bit reversal routine that works bit by bit. It is much easier to understand, but it is quite slower.


Leave a Reply

Your email address will not be published.

Commodore Banner Exchange
Insert math as
Additional settings
Formula color
Text color
Type math using LaTeX
Nothing to preview