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

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

  1. Evan Donovan says:

    When I tried copying this to my /usr/local/bin & running it I got the following error message:

    -bash: /usr/local/bin/apple1basic: cannot execute binary file

    I am running Mac OS X 10.4.11. Does this program only work on 10.5?

  2. Michael Steil says:

    @Evan: The binaries posted are Intel-only for now, sorry. If you ask nicely, I’ll target PowerPC, too. :-)

  3. devnull says:

    Nice one! Will try out the binary.

  4. Nico says:

    Thanks for the link to this thesis, very interesting.

  5. [...] on pagetable.com, they managed to pull the original Apple I Basic off of its uber-old school cassette tape, and re-compile a working version of the code. And… [...]

  6. Claudio Carlquist says:

    Very cool! I’d love to have it under Linux. Is it opensourced or do you have any plan on porting it to Linux? Indeed, very well done!

  7. Geoff says:

    Asking nicely for PPC binaries… please?

  8. James says:

    hah what a brilliant idea, just makes me wish I could actually understand basic to be able to make something cool with it

  9. Eric3 says:

    With a MacBook with 10.4, both BASIC binaries give me “Bus error” when I try them. Any ideas?

  10. Rhyre says:

    Nice thesis – please add me to the list of people asking nicely for source code.

    Remember, this whole thing (Apple) started by taking advantage of Woz’ willingness to share.
    It’s only fair to make your ‘Apple I BASIC interpreter’ available in source, so those of us who
    run Linux or PowerPC processors can benefit.

    Other folks wrote “open letters” to people who shared their BASIC interpreters.

    - Ralph

  11. Mark Hughes says:

    The random number generator isn’t seeded differently per run, so you always get the same values. How did it work on the original hardware?

    ] cat random.bas
    #!/usr/bin/env apple1basic
    10 FOR I=1 TO 100
    20 PRINT RND(100);” “;
    30 NEXT I
    40 PRINT
    50 END
    ]
    ] ./random.bas
    56 72 81 92 2 80 60 31 56 80 68 7 43 20 48 4 49 94 60 54 20 2 79 4 35 22 26 16 36 32 41 82 1 36 30 11 8 59 85 24 34 29 28 24 21 1 39 30 56 28 48 42 97 65 75 15 73 76 41 12 52 12 69 43 91 99 40 67 59 94 69 57 9 34 92 56 61 84 34 77 35 6 14 7 68 83 44 26 2 28 81 13 72 75 7 36 30 59 47 50
    ]
    ] ./random.bas
    56 72 81 92 2 80 60 31 56 80 68 7 43 20 48 4 49 94 60 54 20 2 79 4 35 22 26 16 36 32 41 82 1 36 30 11 8 59 85 24 34 29 28 24 21 1 39 30 56 28 48 42 97 65 75 15 73 76 41 12 52 12 69 43 91 99 40 67 59 94 69 57 9 34 92 56 61 84 34 77 35 6 14 7 68 83 44 26 2 28 81 13 72 75 7 36 30 59 47 50

  12. Awesome idea. Too bad I’m also getting the “Bus error” mentioned above on my 10.4.11 Mac Mini.

  13. Muscai Cristi says:

    Hello ! I love Basic, I used ZX Spectrum, Commodore 64, PC XT, 286,386,486,P1,PII,PIII and P4 lots of Basics, GWBasic,QBasic, Visual Basic 1,4,6 and .NET and now Real Basic on My Power Mac G4@1.5 Ghz.
    I need to use this script basic for PPC (becouse I love my Mac)and I want to make some script for it !! please ! if you will make some ppc versions please send me an E-mail.

  14. Jon Biddell says:

    This brings back some old and SCARY memories of programming PDP 11/45′s back in the 1970′s.

  15. Navnita says:

    i want a gw basic application (downloadable, free) for my mac os x

  16. SJS says:

    +1 for a PPC port … please!

    BTW, my Dad has a very old gwbasic script that he wants me to update. Will this apple1basic run a gwbasic script? Or am I comparing apples to oranges?

    :-)
    sjs

  17. [...] all of Microsoft BASIC only depends on the CPU, makes no other assumptions on the hardware it runs on (be it Commodore, Apple, Atari, …), [...]

  18. tim says:

    I love it. Are you generating assembly or using some intermediate representation like C? How portable would the resulting code be? Since you’re using POSIX IO, could you easily emit code for other IA32 (or amd64) POSIX systems or even portably for all POSIX systems? I’d love to run these, but am not a Mac user.

  19. The ‘apple1basic’ program works fine, everything works as it should, as a script, and non-interactively. But, for some reason, the cbmbasic program only works interactively. When I try to run it as part of a script, it simply does nothing.

    I’d love to have the source for this.

    Then again, cbm basic for the C=64 is well documented. It seems like it should not be too hard for a good ‘c’ programmer to just write an implementation that would be portable.

  20. MiaM says:

    I’m also asking nicely for PPC binaries. Please?

  21. [...] A while back, I released Apple I BASIC and Commodore BASIC as a scripting language for Mac OS X 10.5 on Intel. It did not work on any other OS or on a different CPU type. [...]

  22. Quang says:

    Wow! This is an awesome idea. Commore 64 was my first computer and I loved it. I used to write quite a few simple programs on it. Let’s PEEK, POKE, and SYS

  23. cory says:

    gettin’ bus error when I try to run as well. on an intel (macbook).

  24. CyberSkull says:

    Nifty, but I’m running PPC, not Intel at home. :(

  25. [...] Apple I BASIC as a native script interpreter on Mac OS X — http://www.pagetable.com/?p=35 [...]

  26. B Yates says:

    Ok, how about a version of TI Extended Basic?
    Please?
    *THAT* was a powerful Basic!

  27. mdrobot says:

    Is there any chance of the Apple BASIC being ported to windows, like cbmbasic was? It rocks!

  28. zoodg says:

    Another person for a port to PPC, please?

  29. jack says:

    >>But? why??

    Why? Because it’s completely awesome! That’s why!

    As a guy who cut his teeth on the Apple ][, spending thousands of hours learning every inch of that wonderful machine, typing in programs from Nibble magazine (and those Beagle Bros. 2-line obfuscated gems), I can’t thank you enough for this.

    This makes me smile.

  30. Jason P Sage says:

    Like Jack… I’m just glad someone did this… for both Apple and c64 no less! Double Awesome!

    I wonder how the c64 POKE and PEEK commands work out now? Integrated into the OS hardware? Can I “emulate” the SID chip? LOL…

  31. BillBoard says:

    To Mark Hughes: (Random number generator question…)

    I used to own an Apple II — random numbers were always in the same pattern everytime the computer was booted. Turn on the computer on a given day, select a series of random numbers. Turn off the computer, then restart the computer — select a series of random numbers again… same series of numbers as before. So, I’m pretty sure random numbers were generated based on the internal clock from the moment the computer was swicthed on.

  32. Just a guy says:

    How do I create a new file called hello.bas for me to cat. Please excuse my naivette.
    I installed the binary in the correct /usr/bin and was able to run the hello world operations from the prompt. When I try to do the example command “cat hello.bas” I get an error that the file doesn’t exist.

  33. Michael Steil says:

    @Just a guy:
    TextEdit? :-)

  34. Just a guy says:

    @Michael Steil:
    Your smiley face leads me to believe you are trying to be helpful, but I ask in all sincerity “Did you try creating a .bas file with TextEdit before you recommended it?”
    -No, probably not. Because as far as I can tell you can’t create a .bas file with text edit.

    On a positive note your comment inspired me to try gedit which worked perfectly.

    Thanks for the bump over that hurdle.

  35. Just a guy says:

    After further investigation into the preferences of TextEdit I found that by changing the New Document / Format setting to Plain Text I am able to save a ‘.bas’ file.

    Sorry, if my reply came across as snarky.

    I am still thankful for your reply which sent me down this road.

  36. MiaM says:

    @Mark Hughes and @BillBoard:

    I don’t know how apple I basic did, but the Microsoft basic used in C64, VIC-20 e.t.c. always gave the same random series every time you powered on / reset’ed the computer.

    I remember that the RND function took a negative parameter to initiate the random generator to a specific random series (i.e. you could specify a specific gamle run for games using random numbers by initiating the random number generator with a specific negative numer – compare to freecell.exe – “choose game#” in most windows versions), and most programs started with something like
    10 X = RND (-TI)
    where X were discarded and TI were the real time clock with 60Hz resolution. Because it’s highly unlikely that you start a program on exactly the same clock tick twice it gives a good enough variation.

    AFAIK the random number was however not that good if it were initialized with a rather high negative numer, so if the computer had been running for a few hours or you had set the real time clock yourself (with for example TI$=”101112″ for 12 seconds past 10:11 AM) you could get really bad random series which repeated very fast.

    Try a program like
    10 A=RND(-65530)
    20 A=RND(1)
    30 I=I+1
    40 B=RND(1)
    50 PRINT B;
    60 IF BA THEN 30
    70 PRINT “R:”;I;
    80 I=0
    90 GET A$
    100 IF A$=”X” THEN END
    110 IF A$=” ” THEN 30
    120 GOTO 90

    that program will initialize the random number generator, generate a number and then keep on generating numbers and print them until it encounders the same number again, then wait for “X” for exit or space to continue, that way you can look at the sceen and check for repeating patterns yourself.

    (I haven’t tried the program myself, it may contain errors. Also I don’t remember if the maximum initialization number were around -32768 or -65535, AFAIK higher numbers were truncated)

  37. John says:

    Hi, i’ve got the binary for windows (xp). Is giving me all kinds of syntax error messages. I’ve got the vcredistrib installed so i’m clueless why this is not working for me. Frustrating because in many ways it works and i start to get excited. I have other versions of MBasic that work flawlessly so i’m not sure where to start. Checksums?

  38. Michael Steil says:

    @John: Are you using uppercase commands?

  39. John says:

    Michael, just checking back and i see that a message that i thought i posted before is not here. So here goes again…

    Upper case doesn’t solve the problem. I can do something simple like:

    10 Print “Hello”
    Run
    But beyond this, not much. Even in caps, i can’t LIST or CLS and other basic commands without “syntax errors”. I am reminded of a time when i got the binary for the MESS project and it didn’t work. So finally, in frustration, i downloaded the SVN file, which was huge, and compiled it. Finally I was able to get a working version. I wonder if this is the same situation. Meanwhile, i have a question for anyone. Is this version of the BASIC interpreter for the Commodore true to it’s original function? Is it a robust rendition of the original? I am interested in programming somthing that will run on top of it and need it to be reliable. Anyone?

  40. John says:

    Ok, partly, the problem is i was unfamiliar with the version of BASIC that came with the Commodore 64. I had been recently using IBM PC BASIC from era 1982 and assumed there were more similarities than there were. For one thing, 10 Print “Hello” won’t work in C64 BASIC because PRINT needs to be capitalized, even in a line. Once i googled and downloaded some original documentation, i was having much better luck with CBM BASIC. It’s complicated though, because i’m still getting two situations where it does not seem to function right. When calling on a time function such as TI or TI$, my DOS window shuts down immediately. Also, i get extra characters on the screen when printing numbers and spaces. I’m running XP on a Pentium 3. Dunno.

  41. Queru says:

    Great job! Please, open source it, be free as in freedom.

  42. IAN says:

    Thank you. I’ve been looking for a basic interpreter for the Mac command line for a while now, and this is my solution (and a darn good one too!)

  43. jin man says:

    IT looks great. for basic learner. there are alternative basic program in mac. ‘ object-basic’ tool which does not look comfortable [ as normal users in window programming pattern ] only syntax except dialog I supposed..

  44. limited edition t shirts, online boutique, 100% organic…

    [...]Apple I BASIC as a Mac OS X Scripting Language « pagetable.com[...]…

  45. Vlasveldaad says:

    Werkelijk grappig dat dit kan. Misschien dat Microsoft er een hoop geld voorover heeft om dit stuk te maken.

  46. Nicholas says:

    Sounds good… wait what? Replace Perl? Are you insane?

  47. Padraic says:

    hello, like john i’m receiving syntax error messages consistently. the .exec file opens in terminal, and i have no previous familiarity with basic at all. i am using a macbook OS X ver. 10.6.8. I have no previous experience with coding or writing programs, and thought going all the way back to the “beginning” would help me out as my main goal is to program arcade games in the original approach. in any event, i downloaded the user manual, and i’ve even tried following some online instructionals but everything returns a syntax error except for “hello world!”. i am even unable to copy the apple1basic to /usr/bin. any help would be greatly appreciated. thank you.

  48. [...] The research into static recompilation done by Michael Steil (referenced above) culminated in this paper and you can find source and such here. [...]

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