Apple I BASIC as a Mac OS X Scripting Language

Update: Commodore BASIC as a Scripting Language for UNIX and Windows – now Open Source

Recently, we reconstructed a perfect copy of Apple I BASIC, the first piece of software Apple ever sold – in 1976. Now it is time to make something useful out of it. Wouldn’t it be nice if you could use Apple I BASIC to replace, say, Perl? Wouldn’t it be nice if you could do this:

$ cat reverse.bas
#!/usr/bin/apple1basic
10 DIM A$(100)
20 INPUT A$
30 FOR I = LEN(A$) TO 1 STEP -1
40 PRINT A$(I,I);
50 NEXT I
60 PRINT
70 END
$ chmod a+x reverse.bas
$ echo MICHAEL STEIL | ./reverse.bas
LIETS LEAHCIM
$

Here is Apple I BASIC as a scripting language for Mac OS X Intel:

apple1basic_osx.zip

Just yet another Apple I emulator for Mac? No, it is not. There are some very important differences:

  • The “apple1basic” executable is a statically recompiled version of the original binary. All code is running natively.
  • “apple1basic” plugs right into UNIX stdin and stdout.
  • You can pass “apple1basic” the filename of a BASIC program to run.
  • You can run BASIC programs like shell scripts.

Let’s play with it for a bit. First, copy “apple1basic” to /usr/bin:

$ sudo cp apple1basic /usr/bin

Let’s try direct mode first:

$ apple1basic
>PRINT"HELLO WORLD!"
HELLO WORLD!
>

Now let’s write a small program:

$ apple1basic
>10 FOR I = 1 TO 10
>20 TAB I: PRINT "HELLO WORLD!"
>30 NEXT I
>40 END
>RUN
HELLO WORLD!
 HELLO WORLD!
  HELLO WORLD!
   HELLO WORLD!
    HELLO WORLD!
     HELLO WORLD!
      HELLO WORLD!
       HELLO WORLD!
        HELLO WORLD!
         HELLO WORLD!
>

We can tell apple1basic to run a BASIC program from a file, too:

$ cat hello.bas
10 FOR I = 1 TO 10
20 TAB I: PRINT "HELLO WORLD!"
30 NEXT I
40 END
$ apple1basic hello.bas
HELLO WORLD!
 HELLO WORLD!
  HELLO WORLD!
   HELLO WORLD!
    HELLO WORLD!
     HELLO WORLD!
      HELLO WORLD!
       HELLO WORLD!
        HELLO WORLD!
         HELLO WORLD!
$

apple1basic can be interactive:

$ cat name.bas
10 DIM N$(20)
20 PRINT "WHAT IS YOUR NAME";
30 INPUT N$
40 PRINT "HELLO, "; N$; "!"
50 END
$ apple1basic name.bas
WHAT IS YOUR NAME?MICHAEL
HELLO, MICHAEL!
$

apple1basic supports redirection of stdin and stdout. Note that if stdin is a pipe, “INPUT” doesn’t print the “?”:

$ cat reverse.bas
10 DIM A$(100)
20 INPUT A$
30 FOR I = LEN(A$) TO 1 STEP -1
40 PRINT A$(I,I);
50 NEXT I
60 PRINT
70 END
$ echo MICHAEL STEIL | apple1basic reverse.bas
LIETS LEAHCIM
$

Which brings us back to our first example: You can even use apple1basic as a UNIX script interpreter by adding the hashbang as the first line:

$ cat reverse.bas
#!/usr/bin/apple1basic
10 DIM A$(100)
20 INPUT A$
30 FOR I = LEN(A$) TO 1 STEP -1
40 PRINT A$(I,I);
50 NEXT I
60 END
$ chmod a+x reverse.bas
$ echo MICHAEL STEIL | ./reverse.bas
LIETS LEAHCIM
$

Some more programs

These games, written for the Apple I in the 1970s, have been taken from this and other sites and converted from tokenized hexcode into ASCII.

You can also download hanoi-apple1.bas, a fixed version of Amit Singh’s BASIC program from his Hanoimania project.

How it is done

My static recompiler takes the Apple I BASIC binary as an input and produces a native executable. This file does not contain the original 6502 code and runs 100% natively. On a 2 GHz machine, it runs about 2000 times faster than a 1 MHz Apple I, so it can run one 6502 clock cycle per native cycle. (In the best case, an interpreter can only get up to 1/10 of an emulated cycle per native cycle.)

Accesses to the Apple I terminal (keyboard and screen) are handled by functions that implement a few hacks. Depending on the program counter and the stack, I can discard some output (like terminal echo, 80 column forced line wrap) and redirect files and stdin to keyboard input, depending on context. For example, if there is a filename on the command line, keyboard emulation passes these characters into BASIC, and adds the “RUN” command. All following input will be read from stdin, and as soon as the code to print the “>” prompt is detected, the output handler quits the application.

Commodore BASIC / Microsoft BASIC 6502

I have done the same thing to the 9 KB Microsoft BASIC for 6502 taken from the Commodore 64 ROM.

cbmbasic_osx.zip

$ cbmbasic 

    **** COMMODORE 64 BASIC V2 ****

 64K RAM SYSTEM  38911 BASIC BYTES FREE

READY.
_

It has many of the same features as Apple I BASIC: It discards the banner and the “READY.” prompt output when runnig in non-interactive mode, accepts a BASIC file as a parameter, and can be used as a script interpreter.

But… why??

I love the idea of reusing old code. Emulation is nice, but it rarely integrates well into modern systems. Most code out there has either a very lightweight (or well-understood) interface to the hardware (like the Apple I) or the opertaing system (like Commodore BASIC, as well as all programs from the GUI era), so hooking it up with modern operting system services can work out very nicely.

Also, emulation of vintage systems rarely cares too much about performance any more, since it is usually fast enough, i.e. as fast as or faster than the original system. But computers have always been slow, and running very old code can have the advantage of working with really fast software.

And yes, I think there is a lot of useful vintage code out there.

Links

pixelstats trackingpixel

51 Responses to “Apple I BASIC as a Mac OS X Scripting Language”

  1. Dave S says:

    Hi,

    How Wonderful!

    But, I cannot get many of the Numeric Commands to work, like INT(), EXP(), or LOG().

    Is there more to the installation than I have done?

    Please advise.

    Thanks,

    Dave

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word