Spiraling in Color BASIC and 6809 Assembly – part 1

See also: part 1 and part 2.

It seems each 80s computer system had certain styles to programs that ran on them. There was a certain “look” to the loading screens of many Commodore 64 games, for example.

On the Radio Shack Color Computer, programs often made use of the low-resolution 64×32 8-color semigraphics to create title screens. Graphical games would often drop back to text mode between levels, presenting information on the 32×16 “nuclear green” text screen.

Some programmers would create transitions between screens, such as wiping left to right with a solid color. One of my favorite transitions was a spiral pattern, where the outside drew towards the center of the screen.

Here is an example of that type of effect, albeit done quite slowing in Color BASIC by a program I wrote for this article:

spiralbas.bas

The above video shows the spiral routine being used to spiral in the full 32×16 screen (in orange), then three more spirals done at different sizes, locations and colors, just to test the routine.

The program looks like this:

0 REM SPIRAL.BAS

10 CLS
15 ' X=START MEM LOC
20 X=1024
25 ' XS=XSTEPS (WIDTH)
30 XS=32
35 ' YS=YSTEPS (HEIGHT)
40 YS=16
45 ' B=CHAR TO POKE
50 B=255
60 GOSUB 100

70 X=1024
71 XS=18
72 YS=8
73 B=175 '143+32
74 GOSUB 100

75 X=1294 '1024+14+32*8
76 XS=18
77 YS=8
78 B=207 '143+64
79 GOSUB 100

80 X=1157 '1024+5+32*4
81 XS=22
82 YS=8
83 B=239 '143+96
84 GOSUB 100

99 GOTO 99

100 ' RIGHT
110 A=XS
120 POKE X,B
130 A=A-1
140 IF A=0 THEN 170
150 X=X+1
160 GOTO 120
170 X=X+32
180 YS=YS-1
190 IF YS=0 THEN 600

200 ' DOWN
210 A=YS
220 POKE X,B
230 A=A-1
240 IF A=0 THEN 270
250 X=X+32
260 GOTO 220
270 X=X-1
280 XS=XS-1
290 IF XS=0 THEN 600

300 ' LEFT
310 A=XS
320 POKE X,B
330 A=A-1
340 IF A=0 THEN 370
350 X=X-1
360 GOTO 320
370 X=X-32
380 YS=YS-1
390 IF YS=0 THEN 600

400 ' UP
410 A=YS
420 POKE X,B
430 A=A-1
440 IF A=0 THEN 470
450 X=X-32
460 GOTO 420
470 X=X+1
480 XS=XS-1
490 IF XS=0 THEN 600

500 GOTO 100

600 RETURN

If you wanted to try this yourself, without using a real Color Computer or even having an emulator installed on your computer, you could:

  1. Save the above BASIC code to a text file.
  2. Go to the online XRoar emulator: http://www.6809.org.uk/xroar/online/
  3. Select “Machine:” of Tandy CoCo (NTSC) (or PAL if you prefer). It will even run on a Dragon, so the default machine is fine.
  4. Click its “Load…” button then browse/select the text file you just saved.
  5. From the emulator, type “CLOAD” and the program will load as if it was loading from a cassette tape.
  6. Type “RUN” and see it in all it’s 32×16 text mode glory.

The worst code is bad code.

This program is small, and it’s written in a rather odd way. While there were some BASICs that only allowed one command per line, Microsoft Color BASIC was not one of those. You could pack lines together (which reduced code size and improved speed). You will also notice it is missing using commands like FOR/NEXT. This was intentional, since this program was written like this to match a 6809 assembly implementation that I will be sharing later in this article series.

I suppose if BASIC did not have FOR/NEXT, this would be okay:

0 REM DO THIS 10 TIMES, SLOWLY
10 A=10
20 PRINT "HELLO"
30 A=A-1
40 IF A>1 THEN 20

But this is slow because it is doing variable math (A=A-1) and a comparison (A>1) each time through. Using FOR/NEXT would be much faster:

0 ROM DO THIS 10 TIMES, FASTER
10 FOR A=1 TO 10
20 PRINT "HELLO"
30 NEXT

The RIGHT/DOWN/LEFT/UP routines could be made about three times faster by changing them to FOR/NEXT loops:

100 ' RIGHT
    110 FOR A=X TO X+XS-1
    120 POKE A,B
    160 NEXT
    170 X=A+31
    180 YS=YS-1
    190 IF YS=0 THEN 600

    200 ' DOWN
    210 FOR A=X TO X+32*(YS-1) STEP 32
    220 POKE A,B
    260 NEXT
    270 X=A-33
    280 XS=XS-1
    290 IF XS=0 THEN 600

    300 ' LEFT
    310 FOR A=X TO X-XS+1 STEP -1
    320 POKE A,B
    360 NEXT
    370 X=A-31
    380 YS=YS-1
    390 IF YS=0 THEN 600

    400 ' UP
    410 FOR A=X TO X-32*(YS-1) STEP -32
    420 POKE A,B
    460 NEXT
    470 X=A+33
    480 XS=XS-1
    490 IF XS=0 THEN 600

If I set TIMER=0 at the start of the first version, and print TIMER at the end, it prints around 973 (just over 16 seconds).

The FOR/NEXT version shows 360 (about 6 seconds). “Almost” three times faster.

spiralbas2.bas

And, by packing lines together and doing some other tricks, it could be made even faster.

So, as you can see, doing it the slow way wouldn’t make sense if this article was just about doing the spiral in BASIC.

In the next installment, I will share the 6809 assembly version, unless there are enough “here is a faster way” comments to this section that I need to share them, first.

Until next time…

12 thoughts on “Spiraling in Color BASIC and 6809 Assembly – part 1

  1. Sebastian Tepper

    I did some benchmarks to see how much speed you can gain by applying the speed-up techniques already discussed in previous articles:

    1) SPIRAL1 (your non FOR-NEXT version)
    16.2 seconds

    2) SPIRAL2 (a very readable BASIC code using comments, spaces and individual instructions per line)
    6.0 seconds

    3) SPIRAL3 (removing comments and packing lines from SPIRAL2):
    5.8 seconds

    4) SPIRAL4 (declaring variables with a DIM statement at the beginning, using HEX numbers and a dot instead of zero, plus removing all non-mandatory spaces–not very readable but fast):
    5.5 seconds

    So, if you compare SPIRAL2 and SPIRAL4, the overall speed gain is in the order of 8%. Looking at the program running it is not really noticeable (at least to me).

    Then comes the final version:

    5) SPIRAL5 (compiling SPIRAL2 with BASIC Compiler 2.5 found on the Color Computer Archive site):
    0.15 seconds

    This is basically 40x faster with essentially no extra effort. Only addition I had to do was adding a DIM statement declaring all variables at the beginning of the program (line 0).

    Beauty of this is that you can write properly commented code with spaces for clarity and little to no line packing, then TEST it in the comfortable BASIC environment, and finally compiling for speed when you are ready.

    More in a comment to follow.

    Reply
    1. Allen Huffman Post author

      WOW! I am unfamiliar with this compiler. I always wanted one when I was a kid, but never purchased (or even pirated) one to see what they could do. That is a very interesting approach.

      Reply
  2. Sebastian Tepper

    I did some benchmarks to see how much speed you can gain by applying the speed-up techniques already discussed in previous articles:

    SPIRAL1 (your non FOR-NEXT version)
    16.2 seconds

    SPIRAL2 (a very readable BASIC code using comments, spaces and individual instructions per line)
    6.0 seconds

    SPIRAL3 (removing comments and packing lines from SPIRAL2):
    5.8 seconds

    SPIRAL4 (declaring variables with a DIM statement at the beginning, using HEX numbers and a dot instead of zero, plus removing all non-mandatory spaces–not very readable but fast):
    5.5 seconds

    So, if you compare SPIRAL2 and SPIRAL4, the overall speed gain is in the order of 8%. Looking at the program running it is not really noticeable (at least to me).

    Then comes the final version:

    SPIRAL5 (compiling SPIRAL2 with BASIC Compiler 2.5 found on the Color Computer Archive site):
    0.15 seconds

    This is basically 40x faster with essentially no extra effort. Only addition I had to do was adding a DIM statement declaring all variables at the beginning of the program (line 0).

    Beauty of this is that you can write properly commented code with spaces for clarity and little to no line packing, then TEST it in the comfortable BASIC environment, and finally compiling for speed when you are ready.

    More in a comment to follow.

    Reply
    1. Allen Huffman Post author

      There were three before this, all pending. The first one was complete, the second two were partial. Not sure what is going on, but the pending ones would have been new email addresses or something that wasn’t seen before.

      Reply
    1. Allen Huffman Post author

      I see 9 comments. Anything that is from a new commenter is Pending on my end, but not blocked. I just have to approve it. It should only do that the first time, based on email address.

      Reply
  3. Sebastian Tepper

    SPIRAL3 (declaring variables with DIM at the beginning, no comments, packing code like crazy, using hexadecimal numbers and a dot instead of zero)
    5.5 seconds

    Overall mere 8.3 percent speed reduction

    Reply
  4. Sebastian Tepper

    SPIRAL3 (declaring variables with DIM at the beginning, no comments, packing code like crazy, using hexadecimal numbers and a dot instead of zero)
    5.5 seconds

    Overall mere 8.3 percent speed reduction

    Reply
  5. Sebastian Tepper

    SPIRAL4 (taking SPIRAL2 and using the Basic Compiler version 2.5 found at the color computer archive site)
    0.15 seconds

    Essentially 40x faster without writing assembly code or sacrificing comments and code readability!

    Reply

Leave a Reply to Sebastian TepperCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.