Logo

Retro computing with a touch of modern and the home for all things, retroCombs (aka Steven Combs).

Disclosure Statement: When you click on links to various merchants on this site and make a purchase, this can result in this site earning a commission. Affiliate programs and affiliations include, but are not limited to Amazon, the eBay Partner Network, and/or others.

7 March 2022

Converting a Commodore 128 Submarine Tracking System BASIC Program to the MEGA65

by Steven B. Combs, Ph.D.

In a past live stream, not currently available to view, I tried to convert a BASIC program from the Commodore 128 Programmer’s Reference Guide to use on the MEGA65. What I thought would be a line-by-line conversion caught me off guard, as I found there were significant differences between Commodore BASIC 7 (CBM 128) and Commodore BASIC 10 (CBM 65 / MEGA65).

Folks on the live stream tried to help, but we quickly found that trying to convert this program while distracted was not a productive environment nor something you want to watch. In order to convert the program, I needed to spend time in the MEGA65 Book, uninterrupted, reviewing the changes between BASIC version 7 (V7) and BASIC version 10 (V10).

What I found were many command changes between these versions of BASIC when addressing graphic screens; a feature of the Submarine Tracking System program from the Commodore 128 Programmer’s Reference Guide. After about an hour, and a lot of trial and error, I had the conversion complete. After another hour, I had optimized the program and created version using the MEGA65 high-resolution screen. This blog post and companion video explain the differences between the two programs.

[UPDATE: 2022-03-17] Before we begin, below is demo of the submarine tracking program from the MEGA65:

MEGA65 Submarine Tracking System


Table of Contents


Support the Blog and Channel

You can now support me via my Buy Me A Coffee page with a one-time activity or become a full member via my fun Commodore inspired membership levels. When you support the channel, you get additional content and fun extras. Check out the membership levels to learn more.

Thank you to these members for supporting me at the C128 level; making them retroCombs producers:

Companion YouTube Video

Title: Convert a Commodore BASIC program from Version 7 to Version 10 | Submarine Tracking System on MEGA65

In the video below, I convert a Submarine Tracking program for the Commodore 128 to the MEGA65.

Video Errata

  1. [UPDATE: 2022-03-13] Developer Bit Shifter shares a way to use the BOX command with V10, to draw the border and text the way I wanted, here. I made updates to the blog post. I considered a mention but this one keystroke made the V10 code operate like the V7 code and was a more accurate conversion. Since the solution involves the use of a CBM PETSCII control code, a reverse B, I represent this keystrokes with the text {CTRL+B}. Below is code you can try:
10 GRAPHIC CLR : SCREEN 640,400,4
20 BOX 0,0,639,389
30 CHAR 14,382,2,2,2,"{CTRL+B} SUBMARINE TRACKING SYSTEM ",$29000
40 GETKEY A$
50 SCREEN CLOSE
  1. [UPDATE: 2022-03-13] ​Developer Bit Shifter was inspired by this post and companion video. After seeing the changes made from V7 to V10, and the way circles were created and their limitations, he has modified the code. Per Bit Shifter:

    “Your sonar program inspired me to write new routines for CIRCLE and ELLIPSE which now draw the outline continuously and not segmented as before. Additionally I added start angle, end angle and dotted outline. Now it should be easier, to program the sonar view.”

  2. [UPDATE: 2022-03-13] Developer Bit Shifter posted an updated ROM along with updated CIRCLE and ELLIPSE parameters. This new ROM is now live and available on the FileHost. [UPDATE: 2022-03-17]The animated demo at the beginning of this blog post demonstrates the new .ROM in action. Now, both the C128 and MEGA65 versions function and appear the same way.
  3. [UPDATE: 2022-03-13] The MEGA65 team is calling the MEGA65 computer version of BASIC, BASIC 65. CBM called the C65 version of BASIC, C65 BASIC. Developer Bit Shifter shared that:

    …we thought, it would be better to name it different. I estimate ca. 30 new commands, many bug fixes and ca. 40% changed/optimised code. E.g. fast variables, 32bit hex-constants, byte variables and arrays, binary read/write and much more.

Below are the links I mention in this blog post and companion video. All Amazon links are affiliate links. I’d like to thank everyone for your support of the blog and the YouTube channel by starting your purchases here!

  1. C128 Programmer’s Reference Manual
  2. MEGA65 Book
  3. Hyperkin Ranger Controller
  4. Hyperkin Trooper Controller

Submarine Tracking Program

After a recent purchase to replace my C128 from the 1980s, I was perusing a PDF version of the C128 Programmer’s Reference Manual for interesting tips and tricks for an upcoming project; however, there was a BASIC program on page 116 that drew my eye, the Submarine Tracking System. There was no image of the program’s output, but the 23 lines of code intimated a cool graphics display through its use of circles and sound.

I fired up the C128 VICE emulator on my Mac and typed in the program. Yes, I could have used the technique from my Transfer BASIC Programs in Print to Physical Commodore Computers post and video to speed up the process, but it was 23 lines long and since I hoped to convert this to a MEGA65 program, typing it would allow me to process each line and how it worked.

I was correct, the program was a fun 23 lines of code.

Commodore 128 Version

Below is the BASIC program from page 116 of the C128 Programmer’s Reference Manual. I’ll cover the lines of code when we compare them to the MEGA65 version. The C128 uses BASIC version 7 (more on version later).

C128 Submarine Tracking System

TIP: Load the C128 VICE emulator, download the .txt file above, copy the contents, and paste into the VICE emulator. Type RUN and start tracking subs!

About Commodore BASICS

Before we analyze the program’s code, and change it to work on the MEGA65, let’s take a brief look at the history of Commodore’s BASIC language. Per Wikipedia;

Commodore BASIC, known as PET BASIC or CBM-BASIC, is the dialect of the BASIC programming language used in Commodore International’s 8-bit home computer line, stretching from the PET of 1977 to the C128 of 1985.

…based on 6502 Microsoft BASIC, it shares many characteristics with other 6502 BASICs of the time, such as Applesoft BASIC.

Commodore licensed BASIC from Microsoft in 1977 on a “pay once, no royalties” basis after Jack Tramiel turned down Bill Gates’ offer of a $3 per unit fee. As stated by Jack Tramiel during the Commodore 64’s 25th anniversary celebration at the Computer History Museum on December 10, 2007, Trammel told Gates, “I’m already married,” and that he would only pay $25,000 for a perpetual license.

Commodore BASIC version numbers have an interesting history. I will not cover the changes to each version of BASIC, but will review the version numbers. Commodore released the following versions of BASIC on the following devices. The list of devices is not comprehensive. My intent is to list the first computer to host a specific version of BASIC. If there are errors, please let me know in the comments below.

Releases

The following version of basic were released from 1977 to 1985.

Unreleased Versions

There were two other BASIC versions that were not released, like the computers that hosted them.

Version 10 of BASIC has been revived by the MEGA65 developer community and is in active development for the soon to be released MEGA65. They continue to stomp out bugs and add new features to commands that follow the original C65 roadmap or add function to the MEGA65. As the owner of a MEGA65 Dev Kit, I’ve been following development, and this caused me to want to port a BASIC program from my previous favorite 8-bit computer, the Commodore 128, to my newest 8-bit computer obsession, the MEGA65. The Submarine Tracking System turned out to be a good first program to convert.

MEGA65 Version

Before I compare the two programs, below is the MEGA65 version. My goal was to create code that was a line-by-line conversion and maintain the same line numbers for both versions. This will make it easier when I show the two programs side-by-side. I came close.

[UPDATE: 2022-03-13]

10 REM SUBMARINE TRACKING SYSTEM (MEGA65)
20 SPEED 1:GRAPHIC CLR:SCREEN 320,200,8
30 BOX 0,0,319,194
40 CHAR 7,190,1,1,2,"{CTRL+B} SUBMARINE TRACKING SYSTEM ",$29000
50 PEN 2
60 LET XR=10
70 DO
80 PEN 2:CIRCLE 160,95,XR
90 LET XR=XR+10
100 LOOP UNTIL XR=90
110 LET XR=10
120 DO
130 LET XR=10
140 DO
150 PEN 1:CIRCLE 160,95-XR,2,1
160 PEN 0:CIRCLE 160,95-XR,2,1
170 SOUND 1,10000,15
180 PEN 0:CIRCLE 160,95,XR
190 PEN 2:CIRCLE 160,95,XR
200 XR=XR+10
210 LOOP UNTIL XR=90
220 LOOP

💾 Download: MEGA65 SUB TRACK

Below is an image of the output, but entering and running on the MEGA65 is the best way to experience the program.

MEGA65 Submarine Tracking System

Comparing Lines

Before we compare the programs, view the two versions of BASCIC (V7 on the left and V10 on the right) side-by-side below.

Compare the C128 and MEGA65 versions

NOTE:* I indent the code to better visualize program loops. You can’t do this in Commodore BASIC, but it’s a good visual tool to help describe, develop, or troubleshoot programs.

The program has three sections:

  1. Configure the graphics screen and colors - lines 10 through 60
  2. Draw the Submarine Tracking System - lines 70 through 100
  3. Track a submarine - lines 110 through 220

Let’s look at each section. I’ll present the line number, an explanation of each line, and a discussion of differences.

Configure the Graphics Screen and Colors

10 - The REM statement provides the title for the program. There is nothing special to see here.

20 - Configures the graphics display and the colors. V10 includes a SPEED command and sets the MEGA65 to run at 1 Mhz. The SPEED command has three settings: 1 = 1 Mhz, 3 or 3.5 = 3.5 Mhz, and any other number = 40 Mhz. I’d recommend using 40 for clarity. The MEGA65 cannot be “throttled” to 2 Mhz to match the speed of the C128. I chose 1 Mhz as the best possible comparison.

30 - Draws a box around the edge of the screen. [See Video Errata] My V10 version uses the LINE command to connect five line segments and centers the line at the bottom vertically with the text drawn in line 40. V10 includes the BOX command; however, when used with the CHAR command (below), there are differences. When characters are placed on top of the box, the entire box is erased in the 8-bit grid where the character is placed. On V10, the line remains after the character is drawn to the screen.

40 - Uses the CHAR command to add the words SUBMARINE TRACKING SYSTEM to the bottom of the display on the graphics screen. The CHAR is used in lieu of the PRINT command. The V7 CHAR command sets the color, the X and Y start coordinates, and then the text to draw. V10 includes many additional options as shown below:

[UPDATE: 2022-03-13] Upon viewing my video, Bit Shifter has made several changes to the way the CIRCLE and ELLIPSE commands work. He posted an example on the Discord #basic channel.

50 - The V7 COLOR command changes the color of the next object drawn to red. V10 uses the PEN command and a value of 2 sets to red.

60 - This line establishes a radius variable(s). V7 creates an XR and XY while V10 uses a single XR variable. We will discuss why when we examine the differences of the CIRCLE command between versions of BASIC.

Draw the Submarine Tracking System

With initial screen established, it is now time draw the sonar screen using a loop to draw concentric circles.

70 - DO, loops a line or lines of code until it encounters the LOOP command and condition. In this case, line 100, which we will discuss later.

80 - The CIRCLE command draws a circle using the radius specified in line 60. There is a significant difference in the CIRCLE command between V7 and V10. V10 includes a streamline version because it includes an ELLIPSE command. The V7 CIRCLE command does double duty. You must specify both vertical and horizontal circular values, and you include a color value. It’s a busy command! V10 asks for a center, radius, and an optional solid parameter.

NOTE: The CIRCLE command is drawn differently between V7 and V10. If you watch the V7 program, you notice the C128 circle is drawn from end-to-end. The V10 draws each quadrant of the circle at the same time. This was inherent in the C65 version of BASIC to maximize the efficiency of the CPU when drawing the circle. I could draw circles end-to-end using replacement code; however, that was not the point of this project.

90 - A value of 10 is added to XR and YR (V7) or XR (V10). Once again, this is due to the V7 version requiring values for the horizontal and vertical radius required by the CIRCLE command.

100 - LOOP ensures lines 80 and 90 continue until eight concentric circles are drawn when the value of XR is equal to 90. It then proceeds to line 110.

With the Submarine Tracking System drawn, we can now track a submarine.

Track the Submarine

This program’s tracking is rudimentary, with the submarine moving from the center to the bottom of the screen and then starts over from the center; however, the 8-bit representation is fun and there’s potential to add more code and more fun.

NOTE: My MEGA65 code makes the sonar blip larger than the original C128 code. It is much easier to spot the submarine!

110 - The DO command is the beginning of an infinite loop, or a loop that continues until it meets the LOOP conditions in line 220 (V7) or 210 (V10). Since there is no condition to this LOOP command, the program will continue until you press the [Run/Stop] key.

120 - Similar to line 60, this line sets the radius variable to either 0 (V7) or 10 (V10). The V10 CIRCLE command will not allow a value of 0 for the radius in lines 170 and 180. In those lines, we can add +10; however, that’s more code and calculations than making the adjustments in line 120.

130 - By now, we know the DO command begins a loop; however, this loops is within a loop and as I mentioned in line 110 becomes a nested loop. That is a loop that runs within a loop. This DO command continues until it meets the conditions of the LOOP command in line 210 (V7) or 200 (V10). That condition is XR = 90. When it meets this condition, the nested loop exits and the infinite loop at line 110 restarts.

Because V7 and V10 use graphics mode and the CIRCLE command differently to animate the sonar, the program lines do not match up in the next loop.

140 - Draws a filled black circle with a radius of 4 which erases the previous filled red circle.

150 - Combined with line 140 above, the two lines are similar to V7 line 160 which used the V7 DRAW command (not available in V10) to quickly draw a pixel sized blip in white and then black (on/off). Instead of the draw command, I use a filled red circle with a radius of 4 which represents the submarine and is much easier to see. Lines 140 and 150 must be in this order or the blip will display constantly on each line.

160 - Sounds (pings) the sonar when the submarine is drawn. Similar to V7 line 180.

170 - Draws a black circle over the red circle to erase the sonar circle.

180 - Draws a red circle over the empty to redraw the sonar circle. Similar to V7 line 190.

200 (V7) & 190 (V10) - Add 10 to XR to jump to the next southern ring in the sonar circles.

210 (V7) & 200 (V10) - As stated in line 130 checks the condition of XR for a value of 90. When this value is reached, it exits the nested loop started at line 110

220 (V7) & 210 (V10) - As stated in line 110, this LOOP command has no conditions to terminate the loop. Program execution will jump back to line 110 until you press the [Run/Stop] key.

All said and done, the V10 code is shorter than the V7 code. Some of that is from my code optimizations; however, even without, the V10 code is [UPDATE: 2022-03-13] 468 457 characters with spaces and 397 402 without. The V7 code is 809 with spaces and 687 without. That’s a ≈40% difference between the code (with no spaces). That’s a lot of saved keystrokes.

High Resolution versions

After the conversion, I became curious. Could I convert the program quickly using the MEGA65 high-resolution screen and the answer, after an exercise in doubling values, was yes! Along the way, I made a few adjustments:

  1. Convert to 640 x 400 resolution.
  2. Use the SPEED command to make things, well, speedier!
  3. Use the SLEEP command between submarine pings for a more consistent ping interval.
  4. Optimize the code further.

Below is the code:

[UPDATE: 2022-03-13]


10 rem submarine tracking system (mega65 high res)
20 speed 3 : graphic clr : screen 640,400,4
30 box 0,0,639,388
40 char 14,380,2,2,2,"{uloff} submarine tracking system ",$29000
50 xr=10 : pen 2
60 do
70 circle 320,190,xr
80 xr=xr+20
90 loop until xr=190 : speed 40 : sleep 2
100 do
110 xr=10
120 do
130 pen 1:circle 320,190+xr,4,1 : sleep 1
140 pen 0:circle 320,190+xr,4,1 : sleep 2
150 sound 1,16000,15
160 pen 0:circle 320,190,xr
170 pen 2:circle 320,190,xr
180 xr=xr+20
190 loop until xr=190
200 loop

💾 On Disk: MEGA65 Submarine Tracking System (High Resolution)

Below is an image of the output, but entering and running on the MEGA65 is the best way to experience the program.

High Resolution MEGA65 Submarine Tracking System

Below are few highlights of the conversion from the MEGA65 low-resolution screen to high resolution along with program enhancements.

20 - I enjoy watching the computer draw the submarine tracking system so I use the SPEED command, set to 3. If I set SPEED to 40, the screen will draw instantaneously. That’s the power of running basic at 40 Mhz! The last part set up the high-resolution graphics screen to 640 x 400 pixels. This is a simple doubling of 320 x 200, or the standard graphics screen. This becomes the beginning of an exercise to double everything else. The 2 at the end sets the biplane level, or number of colors, to 16.

40 - The doubling exercise continues with the CHAR command. This time we double the width and height of the text using 2,2,"submarine tracking system".

90 - I kick up the SPEED to 40 Mhz. You can no longer see the circles being drawn pixel-by-pixel.

110 - I add SLEEP 2 to pause between drawing the screen and ping submarines. That pause gives the program breathing room before tracking begins.

130 & 140 - This SLEEP 1 & 2 commands wait between pings. Because the computer is running at 40 Mhz, we don’t wait for the circles to be drawn each time and without this pause, the blip would move at “light speed!” This version of the code, unlike the V7 or slower V10, ensures each ping is approximately the same interval.

If there are changes to any other code, it was a doubling situation. Could we enhance the program further? Sure!

Enhancing the Program

Below is a list of things that can enhance this program on the MEGA65.

What ideas do you have? Post them in the comments below.

Final Thoughts

  1. Converting the program and preparing this blog post and companion video was a blast and brought back many memories from my early Commodore VIC-20 days hacking around and troubleshooting BASIC programs.
  2. This simple program conversion caused me to learn more about the MEGA65 and to begin to appreciate BASIC V10.
  3. I’m sure there are many other ways to create this program and would love to read your thoughts and ideas. Leave them in the comments below.
  4. Now that I have a background in BASIC program conversions, I plan to do others. This will aid in my understanding of the changes between BASIC versions, but to become more versed with V10 to create original programs. Be sure to follow my blog and YouTube channel.

Join the Fun

Don’t miss the retro-computing fun. SUBSCRIBE to my YouTube channel and check out my other posts.

Help make this content better! Leave your comments, corrections, additions, and thoughts in the comments below. You can email me at retrocombs@icloud.com. Thanks for reading and if you are inclined, please let others know about the blog using the hashtag #retroCombs.

🕹️ retroCombs, OUT!