Back to BASICs
Back to BASICs. Dan’s MEGA65 Digest for December 2022.
One of the most satisfying things you can do with a vintage computer is to turn it on and start writing a program. With nothing but your curiosity, a little persistence, and maybe a book and a pad of scratch paper, you can craft a program that solves a problem, performs a task, produces a work of art, tells a story, or generates an interactive experience.
In this Digest, we’ll look at a few ways to get starting writing programs for the MEGA65 using the built-in BASIC 65 programming language. This won’t be a BASIC tutorial—there are hundreds of vintage books on that subject, and you already have the MEGA65 User’s Guide. There are additional chapters for beginners in the MEGA65 Complete Compendium. Instead, we’ll focus on several ways to create and run BASIC programs with your MEGA65, and with your PC.
But first!
Batch 2 shipments have begun!
Trenz Electronic has announced that batch 2 shipping has begun, and some have already received their MEGAs! The next 400 recipients should receive shipping notices in the next few weeks, followed soon after by receipt of your very own MEGA65.
Batch #3 is expected to deliver in the third quarter (July-September) of 2023. It’s too early to say how many units will ship in the next batch. Here’s hoping that FPGA supply will be flowing more freely next year.
Has your shipping address changed?
For those of us awaiting delivery of a pre-ordered MEGA65: If your shipping address has changed since you placed the pre-order, send an email to: sales@trenz-electronic.de
Changing the shipping address in your Trenz Electronic website account settings will not update shipping addresses on pending orders. You must send them email to update the address on a pre-order.
I know many of us have been waiting a long time for our MEGA65s. If your address needs updating, take care of it soon to ensure smooth delivery.
Digest feeds
You’re probably reading this Digest delivered as email via Substack. If you clicked through to the audio version with the play icon at the top of the email, you’re probably listening to it using a web browser on your desktop computer, tablet, or smartphone. You can read past and current issues, as well as manage your email subscription, on the Substack website: m65digest.substack.com Substack has a phone app, and it includes a built-in audio player.
Alternatively, you can follow the Digest using your favorite feed reader. The Substack website has an embedded RSS feed, and you can usually just copy the website address into your feed reader to subscribe. If your reader needs the exact feed URL, it’s just this: m65digest.substack.com/feed
I’m pleased to announce that the audio version of the Digest is now available in your podcast player! Search for “Dan’s MEGA65 Digest” in Apple Podcasts or the Pocket Casts directory. I’ve also found that some (but not all) podcast players can subscribe directly to the Substack RSS feed as if it’s a podcast.
Finally, just to make sure we’re not too dependent on a single platform, I post a copy of the Digest to my personal website. dansanderson.com/mega65 has links to all of my MEGA65 projects, along with every issue of the Digest. You can follow the RSS feed on that page for just the Digest, or follow the one on my homepage for everything I post. I sometimes write about MEGA65 topics on my main blog.
Regardless of how you follow the Digest, thanks for being here!
Entering commands and programs
Like the Commodores that precede it, the MEGA65 starts up with a blinking cursor at the READY.
prompt. You can enter a command by typing the name of the command followed by arguments, and execute the command immediately by pressing the Return key. For example, here’s a command to turn the color of the screen border to black:
BORDER 0
You can use the same blinking cursor to write a program, a collection of commands on numbered lines. If you type the BORDER
command after a line number then press Return, it does not change the border color, but instead stores the command to be run as part of the program.
10 BORDER 0
Here’s a short program that prompts for a temperature in degrees Fahrenheit, then displays the equivalent temperature in degrees Celsius. Try typing these lines into your MEGA65:
100 PRINT "ENTER A TEMPERATURE IN DEGREES FAHRENHEIT:"
110 INPUT F
120 C=(F-32)*5/9
130 PRINT
140 PRINT F;" DEGREES FAHRENHEIT IS ";C;" DEGREES CELSIUS."
If you’ve written a program on a Commodore before, this should feel familiar. The BASIC screen editor allows you to type whatever you want anywhere on the screen. When you press Return, it checks whether the line with the cursor on it begins with a line number, and if so, it stores the contents of that line in BASIC program memory. If there’s already a line in memory with that line number, BASIC replaces the old line with the new one.
It’s important to remember that what’s on the screen does not necessarily represent the program stored in memory. You can think of the screen as a sort of scratch pad on which you can type whatever you like. Only when you press Return on a line will BASIC try to do something with what you typed.
To see the lines of the actual program stored in memory, use the LIST
command.
LIST
If you notice a mistake or something you’d like to change, you can move the cursor up to the line displayed on the screen, type new text over the old text, then press Return to replace the line in memory.
BASIC keeps the program in line number order, but you can enter the lines in any order you like. This is handy for inserting a line between two other lines: pick a number between the numbers of the existing lines, and enter the new line with that number. This is why BASIC programmers tend to number their lines by tens (10, 20, 30…), to make it easy to insert a line in the middle of the program (15).
To delete a line, enter the line number with no text after it. To delete a range of numbered lines, use the DELETE
command followed by the range:
DELETE 1000-1500
To erase the entire program from memory, use the NEW
command. If you do this accidentally and want to bring the deleted program back, don’t panic, just type: NEW RESTORE
Saving programs to disk
Your program is in the computer’s memory, but it is not saved to permanent storage automatically. (Maybe that’s obvious in the context of vintage computers, but we get accustomed to modern conveniences sometimes.) If you turned off the MEGA65 now, the program would be forgotten.
Hopefully by now you’ve had a chance to mount a D81 disk image and load a program from it. You can also use the Freeze menu to create an empty disk image on your SD card. Try this now: hold Restore for a second then release to open the Freeze menu. Press 0 to mount a disk to device 0 (unit 8). At the top of the list of disk images is the option to create a “NEW D81 DD IMAGE.” Select this, then give the disk a name. I like to call my work disks “WORK” to make it seem like I’m doing something important.
The new disk image is now mounted. Press F3 to resume to BASIC. (Be careful not to press F5 to reset if you have a program in memory!)
With a BASIC program in memory, save it to your disk with the DSAVE
command. Put the filename in double-quotes:
DSAVE "TEMPCONV"
Confirm that your program is on the disk by listing the disk directory:
DIR
As written, the DSAVE
command will fail if there is already a file with that name on the disk. It’s a common practice to save your work periodically with different filenames suffixed with increasing numbers, so you can revert back to an older version if necessary. (Commodores don’t have built-in revision control systems!) Over a few days, you might end up with "TEMPCONV2"
, "TEMPCONV3"
, "TEMPCONV17"
on your disk, until you’ve decided which version is the final version.
Alternatively, you can live dangerously and overwrite the same file each time you save. To do this, prepend the filename with an “at” symbol (@
). (The symbol is not considered part of the filename.)
DSAVE "@TEMPCONV"
You can now turn off your MEGA65. The next time you turn it on, you can use the MOUNT
command with your work disk name followed by .D81
to mount it, then the DLOAD
command to load your program back into memory:
MOUNT "WORK.D81"
DLOAD "TEMPCONV"
Listing long programs
Eventually your program will be many lines long. The LIST
command will try to list your entire program, and only the bottommost 25 rows will be on the screen at the end. How can we navigate a long program listing?
(If you want to experiment with this without writing a long program, MOUNT "MEGA65.D81"
then DLOAD "AUTOBOOT.C65"
to load the demo disk’s menu program, which is written in BASIC. If you disabled opening the menu on start-up, this file has been renamed: use DLOAD "MENU"
instead.)
As with previous Commodore BASICs, the LIST
command accepts a range of line numbers. This is a slightly cumbersome but precise way to look at a few lines at a time, if you know their numbers.
LIST 100-300 : rem List lines 100 through 300
LIST -300 : rem List all lines up to 300
LIST 1000- : rem List all lines starting from 1000
LIST 250 : rem List only line 250
When listing many lines, you can tap the No Scroll key to pause the listing. You can press Run Stop to terminate a paused listing, or No Scroll again to resume. The MEGA65 is so fast in its default 40 MHz mode that this isn’t a practical way to peruse a listing. You could use the SPEED 1
command to set it to 1 MHz and slow it down, but be warned: there’s a bug where the MEGA65 crashes when you Run Stop in slow mode! I don’t recommend this method at all for this reason—but I mention it because it’s interesting.
The MEGA65 LIST
command has an option to display a program listing one screenful at a time:
LIST P
LIST P 100-300
Press a key at each page to advance to the next one, or press Run Stop to abort at the current page.
There’s a third method to browse long listings, and it’s my favorite: press F9 and F11 to scroll down and up by a line. (If you’ve redefined your function keys, you can use Ctrl-P and Ctrl-V instead.) I like this method because the screen editor remains active, so you can start typing inside the listing as soon as you see what you want to change. Notice that the screen does not have a modern-style scrollback buffer, even though it seems like it does: it’s actually reading lines from BASIC memory to fill in the scrolling display.
With lots of text on the screen, remember that you can get new blank lines to type on by running the cursor off the bottom of the screen, or pressing Shift + Clr Home to clear the screen entirely. You can also clear just from the cursor’s line to the bottom of the screen by pressing the ESC key followed by the @ key.
The Eleven programming environment
The BASIC 65 screen editor is thoroughly retro and thoroughly Commodore, and it picked up some nice features along the way from the Commodore 128, Commodore 65, and a few added by the MEGA65 team. But perhaps you want a coding environment that’s a bit more modern—without leaving your MEGA65.
The Eleven programming environment, by Stephen Kleinert (username ubik), reinvents the BASIC 65 editing experience. It provides a modern-style code editor and extensions to the BASIC 65 language to make it easier to use for large projects. It dispenses with line numbers, and instead uses labels for flow control (such as goto
statements). Variable names can be longer than two characters, and preprocessor directives allow for efficient definition of constants without taking up variable space.
The brilliant part is that Eleven compiles your program into plain BASIC 65 code. It generates line numbers, replaces labels, renames variables, strips comments, and minifies syntax. The result is a compact BASIC 65 program that you can save to disk and give to your friends, with no bulky runtime library or other baggage. BASIC 65 is a powerful enough language; Eleven just adds modern documentation and editing conveniences, making BASIC programs easier to write and maintain.
A copy of Eleven was included on your SD card, and you can download the latest version from Filehost. Here’s how to give it a try:
MOUNT "ELEVEN.D81"
BOOT
- Enjoy reading the documentation directly in the editor. Use cursor keys to scroll.
- Press F1 to load a program.
- Type
dizzy.el
and press Return. Scroll through the program listing to see how it works. - Press F5 to compile. The program compiles and starts running. Press any key to exit to the
READY.
prompt. LIST
to see the compiled BASIC 65 program.BOOT
to return to the Eleven editor, with the last-saved project loaded.
Eleven will insist that you save your changes before compiling, so you won’t lose anything if your program crashes the machine.
Stephen has released Eleven to the MEGA65 project with an open source license. There may be on-going improvements in the future.
Writing BASIC 65 programs on your PC
Coding directly on the MEGA65 with the printed User’s Guide at your side is a cozy, comfortable experience, free of Internet-y distractions. But sometimes—just sometimes—I want to use modern tools to develop retro software, even if I’m writing in BASIC.
Full disclosure: I’m a developer tools nerd. I like storing text source files in a Git repository, running things through multiple levels of processing, and automating it all together with Makefiles. One of my favorite ways to write MEGA65 BASIC is on my PC.
I use a command-line tool called petcat that converts a text file containing BASIC code into a PRG file that I can run on my MEGA65 or in the Xemu MEGA65 emulator. petcat comes with the VICE Commodore emulator suite, which I have installed for all of my non-MEGA65 Commodore projects. VICE doesn’t provide a MEGA65 emulator (that’s what Xemu is for), but petcat does know about BASIC 65 commands.
That temperature conversion program looks like this in a petcat text file, literally the same text but in lowercase:
100 print "enter a temperature in degrees fahrenheit:"
110 input f
120 c=(f-32)*5/9
130 print
140 print f;" degrees fahrenheit is ";c;" degrees celsius."
To convert this to a MEGA65 PRG file, use this shell command:
petcat -w65 -o tempconv.prg -- tempconv.bas
You can test the PRG file in Xemu by dragging it into a running Xemu window, or by starting Xemu with appropriate command-line options. If you happen to have a JTAG USB adapter, you can beam this PRG file directly to your MEGA65 with the m65 command-line tool or with the M65Connect app (search for “M65Connect” on Filehost).
petcat labels
You might notice that a text file on a PC doesn’t seem to be capable of representing everything that might go in a BASIC program. For example, there are characters on the MEGA65 keyboard that are not present on a PC keyboard, like the Up Arrow symbol. How would a petcat program print the graphical glyphs on the front of the keys, like the heart shape on the S? What about the PETSCII control codes, like Ctrl-3 that changes the print color to red?
petcat represents as many characters as it can with ASCII equivalents. All of the right-hand glyphs that you access by holding Shift and pressing a letter are simply the capital letters. The PETSCII symbols for pound, Up Arrow, Back Arrow, and pi are represented by ASCII characters with the same encoded value: \
, ^
, _
, and ~
, respectively.
Nearly everything else appears in a petcat listing as a label inside curly brackets, such as {red}
for Ctrl-3. For example:
10 print "{clr}{red}SSS {wht}mega65 {red}SSS{wht}"
The left-hand glyphs that you access by holding Ctrl and pressing a letter are labels like {CTRL-A}
. A few codes that are typed with the Mega key look like {CBM-@}
. Other PETSCII control sequences have named labels like {clr}
or {stop}
.
I won’t clutter this Digest with a complete list. I’ll just say that petcat also knows how to convert a PRG file back into text. If you’re unsure of what label to use for a character or control code, you can create a small program with the character in it using the MEGA65 or Xemu, then run petcat in reverse to see what it looks like.
petcat -65 -o myprog.bas -- myprog.prg
CBM PRG Studio 4
If your PC runs Windows, there’s a compelling new option for MEGA65 cross development. CBM PRG Studio is a much loved integrated development environment for writing programs for Commodore computers. Starting with version 4.0, it includes support for the MEGA65.
I don’t normally use windows, so I’m not deeply familiar with CBM PRG Studio’s features. But it took me no time at all to figure out how to use it to write a MEGA65 BASIC program and build the PRG file. It looks like a compelling IDE for Kick Assembler, as well. Give it a try!
Managing D81 disk images on a PC
Maybe you’ve written a program on your MEGA65 and saved it to a D81 disk image, and now you want to extract the PRG file on your PC. Or perhaps you’ve written a PRG on your PC, and want to put it in a D81 disk image for the MEGA65. We’ve got tools for that!
Windows users should go straight to DirMaster, a powerful disk image management tool that supports many formats, including D81. You can create new images, open existing ones, copy files to and from images, and much more.
Mac and Linux users will prefer droiD64, a desktop app similar to DirMaster. It has fewer features, but I like it because it’s fast and works on all operating systems. It requires a Java 11 runtime, such as OpenJDK or the Oracle Java runtime. I wrote a bit more detail about droiD64 in a recent blog post. Check that out for a technical recommendation on how to improve the start-up script that comes with droiD64.
Of course, I did say I was a developer tools nerd, so naturally my build scripts use command-line tools to create D81 image files. VICE comes with a tool called c1541, and cbmconvert (download) is another option. Personally, I write my own D81 build logic in Python with the d64 Python library. Most people will get by just fine with DirMaster or droiD64.
It’s the end of the year, and I hope everyone is able to take some time off, disconnect from the Internet a bit, and spend some quality time with your MEGA65, and maybe family and friends too if you get around to it. When you get back online at the end of your break, don’t forget to share your BASIC programs by uploading them to Filehost!
I hope you’re all doing well this holiday, and I hope to see you again in 2023.
— Dan