Atari 2600 BASIC Programming

"You need a more mentally challenging job"

This was a friend's reaction when I told him about this article idea. Like most people he wasn't intimately familiar with Atari 2600 BASIC so this response followed my explanation of the limitations:

-Your entire program can not exceed 64 symbols - a symbol being a number, variable, or reserved word (i.e. Goto, If, Then)
-There's a maximum of 9 lines, not really an issue considering the first item
-You only get integers from 0-99
-Only five math functions are available (+,-,*,÷,mod)
-In terms of graphics, you have two dots you can move by setting their coordinates
-You can play a range of beeps but I'm not musically inclined so I can't map them to notes
-Instead of a keyboard you have two 12 button pads to work with

If this sounds horribly limited and lame it's because you haven't yet thought about the hardware it's running on...

Atari 2600 BASIC was programmed by Warren Robinett who is 100x smarter than me and anyone reading this. Well, unless you are him in which case "Hi! Adventure is the greatest Atari 2600 game ever made!". Anyway, since you're probably not him it'll hurt your brain to think about how difficult it was to write a BASIC interpreter for the Atari 2600 that consumed just 64 bytes of memory. Not gigabytes, not megabytes, not even kilobytes - 64 bytes. The Atari 2600 had 128 bytes of RAM and half of that was used for the 64 symbol program you entered so the IDE and interpreter had to be extremely lean.

You should now be in awe of this program.

Not quite as challenging is writing a useful program in just 64 symbols but that's exactly what I'm going to attempt. Think of all those times you tried to cram your thoughts into a 140 character tweet. Now take away 76 characters and try writing a functional program instead of a description of the burrito you just ate.

Before I get to that though, here's a little more back-story which you're free to skip...

I own all the pieces required to run this on the original hardware - Atari 2600, BASIC Programming cartridge, and keypad controllers. Setting all this up and using the original controllers is an exercise in frustration. The 24 keys you get on the two keypads aren't enough so there's a toggle system to shuffle between key sets. If that description sounds confusing it's because, well, it's really confusing.

Even using an emulator like Stella is painful, arguably more painful than the old-school approach. What works amazingly well though is the version that was included on Atari's Greatest Hits Volume 2 for the Nintendo DS. Some madman actually took the time to emulate the dual keypads on the DS touchscreen:

Emulated keypad on the Nintendo DS

It had to be a lot of work to accomplish this and it's sad to think that I'm probably the only person on earth who used it for more than 30 seconds. If you ever feel insane enough to try Atari 2600 BASIC Programming this is definitely the way to go.

The downside to doing this on the DS is that the screenshots look terrible. See how they compare to taking a snapshot in Stella:

Screenshot comparison

So you're not going to get a lot of pictures. It's just text and dots anyway, you're not missing much.

Alright, on to the programs. I'm not going to include a tutorial or the sample programs from the manual, those are perfectly findable on the Google.

Let's start with something simple, moving one of the dots around with the keypad:

1 If Key=2 Then Ver1←Ver1-5
2 If Key=4 Then Hor1←Hor1-5
3 If Key=6 Then Hor1←Hor1+5
4 If Key=8 Then Ver1←Ver1+5
5 Goto 1

Although that only uses five lines it exhausts nearly all available symbols which obviously limits the gaming possibilities.

If my previous comment about Twitter and burritos wasn't transparent enough I'll cut directly to the chase - I have a very difficult time imagining a practical use for Twitter. As far as I can tell the main purpose is to feed people's sense of self-importance. It's the modern equivalent of covering your car with bumper stickers.

However, this experiment has led me to an almost worthwhile application - sharing Atari 2600 BASIC programs. In any programming language it's an interesting challenge to fit an entire functional program into under 140 characters. The challenge is increased further by preserving space to support # and @ tags. Since Atari 2600 BASIC programs are limited to 64 symbols they will almost always fit into a tweet. The only tricky part is that many of the Atari 2600 BASIC symbols are comprised of multiple characters, i.e. "Goto" is one symbol but four characters. That's OK, we can abbreviate these without changing the functionality of the program.

For example, here's the dot moving program in tweet form:

#Atari2600BASIC [1]IfKey=2ThenVer1?Ver1-5 [2]If Key=4ThenHor1?Hor1-5 [3]IfKey=6ThenHor1?Hor1+5 [4]IfKey=8ThenVer1?Ver1+5 [5]Goto1

With controller options highly limited I could only come up with one complete game. I call it "The World's Worst Football Simulator". Specifically, it's simulating the experience of Devin Hester trying to get past the punter on a kick return:

1 S←0
2 Hor2←90, Ver2←0
3 Hor1←0, Ver1←50
4 Ver2←Ver2+1
5 If (Key>0) Then Hor1←Hor1+1
6 If Hit Then Goto 3
7 If Hor1<95 Then Goto 4
8 S←S+1, Note←90
9 Goto 3

Maybe "worst" was a stretch, this game is arguably better than Troy Aikman NFL Football on the Atari Jaguar.

Here it is in tweet form, a little crunching was required to make it fit but nothing was lost:

#Atari2600BASIC [1]S=0[2]H2=90,V2=0[3]H1=0,V1=50[4]V2=V2+1[5]If(Key>0)ThenH1=H1+1[6]IfHitThenGoto3[7]IfH1<95ThenGoto4[8]S=S+1,Note=9[9]Goto3

Let's move over to some common math programs that you'd probably have to tackle in your first week of CS 101.

First up is an implementation of Euclid's Algorithm for computing greatest common divisor complete with animations and sound:

1 Ver1←0, Ver2←9
2 Hor1←50, Hor2←30
3 If (Hor2=0) Then Goto 8
4 T←Hor2
5 Hor2←Hor1 Mod Hor2
6 Hor1←T
7 Goto 3
8 Print Hor1, Note←Hor1

This also fits into a tweet with a bit-o-crunching:

#Atari2600BASIC [1]Ver1=0,Ver2=9[2]Hor1=50,Hor2=30[3]If(Hor2=0)ThenGoto8[4]T=Hor2[5]Hor2=Hor1ModHor2[6]Hor1=T[7]Goto3[8]PrintHor1,Note=Hor1

Finding the midpoint and slope of a line is totally doable with room to spare:

1 Hor1←5, Hor2←10
2 Ver1<20, Ver2←60
3 X←(Hor1+Hor2)÷2
4 Y←(Ver1+Ver2)÷2
5 Print X, Print Y
6 S←(Ver2-Ver1)÷(Hor2-Hor1)
7 Print S

And the tweet version goes a little something like this:

#Atari2600BASIC [1]H1=5,H2=10[2]V1<20,V2=60[3]X=(H1+H2)/2[4]Y=(V1+V2)/2[5]PrintX,PrintY[6]S=(V2-V1)/(H2-H1)[7]PrintS

And for all you software architect types, here's the smallest working implementation of the Model-View-Controller (MVC) design pattern ever produced (that I'm aware of):

1 M←Key
2 If (M=0) Then Goto 1
3 Hor1←Hor1+M
4 Goto 1

It's even smaller as a tweet:

#Atari2600BASIC [1]M=Key [2]If(M=0)ThenGoto1 [3]Hor1=Hor1+M [4]Goto1

To wrap things up, here are a few ideas I thought of that aren't possible to implement, or maybe are by someone smarter than me:

-Decimal to hex conversion - I tried about four different approaches and none were close to fitting.
-Computing Pi to any digits - there are several methods and all require operations that aren't available, even the ol' 22/7 solution is no good because that will just return 3
-Determining whether a number is prime - similar issues to computing Pi, probably doable for numbers under 10 though.
-Any of the Beagle Bros "two-liners", Apple II fans will know what I'm talking about.

If anyone else is brave (or bored) enough to try some Atari 2600 BASIC programming please send me your code like this:

@HuguesJohnson #Atari2600BASIC your-program-here

If I get anything other than "1 Print 'You Suck' 2 Goto 1" I'll post it here eventually.