One of the fun things about playing with these microcontrollers and 8-bit computers has been stepping back to simpler programming environments. At my day job, I mostly code in C++ and have to deal with large, complex libraries like .Net, where it’s hard to keep the whole project in your head at one time. But for these C64 projects I’ve been using assembly language, and for the Arduino, while the toolchain supports most of C++, I’ve been writing code in an old-fashioned ANSI C style. Plus, I’ve had fun browsing into the source code of the libraries to see how they work. So today, let me take another step back and talk about BASIC.
Now, BASIC gets a bad rap these days – and not without reason. It’s crude, it’s interpreted (usually), and it’s slow (because it’s interpreted (usually)). It does very little to encourage structured programming, and makes it really easy to write spaghetti code. It doesn’t have the syntactic sugar of modern languages – things like classes and functions and iterators, for example. I learned back in junior high that trying to write a complex program in BASIC is an exercise in frustration.
Of course, that’s not really the point. The important thing about BASIC is that anyone can learn it – and they can learn it quickly. You can argue about the bad habits it teaches, but the reality is that for millions of people, BASIC was their first (and often last) experience with programming. If you bought a microcomputer in the late 70s or early 80s, it was just there. In the Commodore 64, BASIC was so tightly integrated into the system that you couldn’t tell where the BASIC interpreter stopped and the KERNAL “operating system” started.
Well, okay, the BASIC interpreter stopped at $BFFF and the KERNAL started at $E000 in the C64 memory map, but you know what I mean.
Anyway, one of the fun things I’ve been playing with this week is a BASIC interpreter for the Arduino. It’s been kind of fun, and it might even be useful someday.
This particular BASIC – TinyBasic – has an interesting history. Apparently, it was first published as Palo Alto Tiny BASIC in Dr. Dobb’s Journal in 1976. In 1985, Gordon Brandly wrote a version of it in 68000 assembly. Recently, Mike Field ported it to C and the Arduino, which is the version I’m using. Even more recently, Scott Lawrence added some Arduino-specific features and made his version available at github.
TinyBasic is a pretty simple version of the language, and the version I started with didn’t have any support for Arduino-specific stuff like the I/O pins. But the interpreter is only a few thousand lines of code, and adding new commands is surprisingly easy. I very quickly added commands for PINMODE, DIGITALREAD, DIGITALWRITE, ANALOGWRITE, DELAY, and even SHIFTOUT They’re all really simple, and just parse the arguments and then call the equivalent function in the Arduino libraries.
When you run TinyBasic on the Arduino, you can talk to it using the Arduino serial monitor or a terminal program like Putty. The neat thing about an interpreted language is that you can use it interactively, and that’s why I think it might be useful for investigating circuits – with a couple commands you can set the pins to whatever you like and see what happens. Or, you can quickly put together a short program and debug it interactively.
As an example, I tossed together the pictured circuit – it’s just using a shift register to control some LEDs, and a single button for input. Then I wrote some TinyBasic programs to control it. Nothing complicated, but simple stuff like:
10 PINMODE 10 OUTPUT 15 PINMODE 9 OUTPUT 20 PINMODE 8 OUTPUT 25 PINMODE 7 INPUT 26 DIGITALWRITE 7 HIGH 40 FOR I = 0 TO 255 45 IF DIGITALREAD(7)=0 I=0 50 DIGITALWRITE 9 LOW 60 SHIFTOUT 10 8 MSBFIRST I 65 DIGITALWRITE 9 HIGH 70 DELAY 100 80 NEXT I
to set the lights blinking in a pattern. It’s just delightfully easy to use. I ordered some new components recently, and I think this’ll be a great way to test them out when they arrive.
Getting inside the TinyBasic interpreter and adding to it has also got me thinking about future projects. All the individual pieces of an 8-bit computer are readily available, and there are people out there building machines from scratch. The hardware side of that doesn’t mystify me as much as it used to. I always used to think that the software for a completely custom machine would be frustratingly complicated, but maybe it wouldn’t be so bad – and I did take a few operating system design classes back in my college days. It just might be a fun thing to try out…