{"id":46,"date":"2008-10-20T22:27:15","date_gmt":"2008-10-21T06:27:15","guid":{"rendered":"http:\/\/www.pagetable.com\/?p=46"},"modified":"2008-10-20T22:27:15","modified_gmt":"2008-10-21T06:27:15","slug":"create-your-own-version-of-microsoft-basic-for-6502","status":"publish","type":"post","link":"https:\/\/www.pagetable.com\/?p=46","title":{"rendered":"Create your own Version of Microsoft BASIC for 6502"},"content":{"rendered":"<p><i>Update<\/i>: The source is available at <a href=\"https:\/\/github.com\/mist64\/msbasic\">github.com\/mist64\/msbasic<\/a><\/p>\n<p>If you disassemble a single binary, you can never tell why something was done in a certain way. If you have eight different versions, you can tell a lot. This episode of &#8220;<a href=\"http:\/\/www.pagetable.com\/?cat=8\">Computer Archeology<\/a>&#8221; is about reverse engineering eight different versions of Microsoft BASIC 6502 (Commodore, AppleSoft etc.), reconstructing the family tree, and understanding when bugs were fixed and when new bugs, features and easter eggs were introduced.<\/p>\n<p>This article also presents a set of assembly source files that can be made to compile into a byte exact copy of seven different versions of Microsoft BASIC, and lets you even create your own version.<\/a><\/p>\n<h3>Microsoft BASIC for MOS 6502<\/h3>\n<p>First written in 1976, Microsoft BASIC for the 8 bit MOS 6502 has been available for virtually every 6502-based computer including the Commodore series (PET, C64), the Apple II series, Atari 8 bit machines, and many more.<\/p>\n<p><p>These are the first eight versions of Microsoft BASIC:<\/p>\n<table border=\"1\">\n<tr>\n<td>Name<\/td>\n<td>Release<\/td>\n<td>VER<\/td>\n<td>ROM<\/td>\n<td>FP<\/td>\n<td>ROR<\/td>\n<td>Buffer<\/td>\n<td>Extensions<\/td>\n<td>Version<\/td>\n<\/tr>\n<tr>\n<td>Commodore BASIC 1<\/td>\n<td>1977<\/td>\n<td><\/td>\n<td>Y<\/td>\n<td>9<\/td>\n<td>Y<\/td>\n<td>ZP<\/td>\n<td>CBM<\/td>\n<td>1.0<\/td>\n<\/tr>\n<tr>\n<td>OSI BASIC<\/td>\n<td>1977<\/td>\n<td>1.0rev3.2<\/td>\n<td>Y<\/td>\n<td>6<\/td>\n<td>Y<\/td>\n<td><\/td>\n<td>&#8211;<\/td>\n<td>1.0a<\/td>\n<\/tr>\n<tr>\n<td>AppleSoft I<\/td>\n<td>1977<\/td>\n<td>1.1<\/td>\n<td>N<\/td>\n<td>9<\/td>\n<td>Y<\/td>\n<td>0200<\/td>\n<td>Apple<\/td>\n<td>1.1<\/td>\n<\/tr>\n<tr>\n<td>KIM BASIC<\/td>\n<td>1977<\/td>\n<td>1.1<\/td>\n<td>N<\/td>\n<td>9<\/td>\n<td>N<\/td>\n<td>ZP<\/td>\n<td>&#8211;<\/td>\n<td>1.1a<\/td>\n<\/tr>\n<tr>\n<td>AppleSoft II<\/td>\n<td>1978<\/td>\n<td><\/td>\n<td>Y<\/td>\n<td>9<\/td>\n<td>Y<\/td>\n<td>0200<\/td>\n<td>Apple<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>Commodore BASIC 2<\/td>\n<td>1979<\/td>\n<td><\/td>\n<td>Y<\/td>\n<td>9<\/td>\n<td>Y<\/td>\n<td>0200<\/td>\n<td>CBM<\/td>\n<td>2a<\/td>\n<\/tr>\n<td>KBD BASIC<\/td>\n<td>1982<\/td>\n<td><\/td>\n<td>Y<\/td>\n<td>6<\/td>\n<td>Y<\/td>\n<td>0700<\/td>\n<td>KBD<\/td>\n<td>2b<\/td>\n<\/tr>\n<tr>\n<td>MicroTAN<\/td>\n<td>1980<\/td>\n<td><\/td>\n<td>Y<\/td>\n<td>9<\/td>\n<td>N<\/td>\n<td>ZP<\/td>\n<td>&#8211;<\/td>\n<td>2c<\/td>\n<\/tr>\n<\/table>\n<p><b>Name<\/b>: Name of the computer system or BASIC interpreter<\/p>\n<p><b>Release<\/b>: Release date of this version &#8211; not necessarily the date when the source code was forked from Microsoft&#8217;s<\/p>\n<p><b>VER<\/b>: Version string inside the interpreter itself<\/p>\n<p><b>ROM<\/b>: Whether the software shipped in ROM, or was a program supposed to be loaded into RAM<\/p>\n<p><b>FP<\/b>: Whether the 6 digit or 9 digit floating point library was included. 9 digit als means that long error messages were included instead of two character codes, and the GET statement was supported.<\/p>\n<p><b>ROR<\/b>: Whether the ROR assembly instruction was used or whether <a href=\"http:\/\/www.pagetable.com\/?p=45\">the code worked around it<\/a><\/p>\n<p><b>Buffer<\/b>: Location of the direct mode input buffer; either zero page or above<\/p>\n<p><b>Extensions<\/b>: What BASIC extensions were added by the OEM, of any.<\/p>\n<p><b>Version<\/b>: My private version number used in this article and in my combined source<\/p>\n<h3>The Microsoft BASIC 6502 Combined Source Code<\/h3>\n<p>Download the assembly source code here: <a href=\"http:\/\/www.weihenstephan.org\/~michaste\/pagetable\/msbasic\/msbasic.zip\">msbasic.zip<\/a><\/p>\n<p>In order to assemble if, you will need the <a href=\"http:\/\/www.cc65.org\/\">CC65<\/a> compiler\/assembler\/linker package.<\/p>\n<p>The source can be assembled into byte-exact versions of the following seven BASICs:<\/p>\n<ul>\n<li>Commodore BASIC 1<\/li>\n<li>OSI BASIC<\/li>\n<li>AppleSoft I<\/li>\n<li>KIM-1 BASIC<\/li>\n<li>Commodore BASIC 2 (PET)<\/li>\n<li>Intellivision Keyboard Component BASIC<\/li>\n<li>MicroTAN BASIC<\/li>\n<\/ul>\n<p>You can build the source by running the shell script make.sh. This will create the seven files cbmbasic1.bin, osi.bin, applesoft.bin, kb9.bin, cbmbasic2.bin, kbd.bin and microtan.bin in the &#8220;tmp&#8221; directory, which are identical to the original ROMs.<\/p>\n<p>You are welcome to help clean up the source more, to make it more readable and to break features out into CONFIG_* defines, so that the source base can be made more customizable.<\/p>\n<p>Make sure to read on to the end of the article, as it explains more about the source and what you can do with it.<\/p>\n<h3>Microsoft BASIC 1<\/h3>\n<p><a href=\"http:\/\/www.pagetable.com\/?p=43\">Ric Weiland, Bill Gates and Monte Davidoff<\/a> at Microsoft wrote MOS 6502 BASIC in the summer of 1976 by converting the Intel 8080 version. While the former could fit well into 8 KB, so that a computer manufacturer could add some machine-specific I\/O code and ship a single 8 KB ROM, code density was less on the 6502, and they could not fit it significantly below 8 KB &#8211; it was around 7900 bytes &#8211; so that computers with BASIC in ROM would require more than a single 8 KB ROM chip.<\/p>\n<p>Spilling over 8 KB anyway, they decided to also offer an improved version with extra features in a little under 9 KB: This version had a 40 bit floating point library (&#8220;9 digits&#8221;) instead of the 32 bit one (&#8220;6 digits&#8221;), and the two-character error codes were replaced with actual error messages:<\/p>\n<table border=\"1\">\n<tr>\n<td align=\"center\"><b>6 digit BASIC<\/b><\/td>\n<td align=\"center\"><b>9 digit BASIC<\/b><\/td>\n<\/tr>\n<tr>\n<td><tt>?NF ERROR<br \/>OK<\/tt><\/td>\n<td><tt>?NEXT WITHOUT FOR ERROR<br \/>OK<\/tt><\/td>\n<\/tr>\n<\/table>\n<p>9 digit BASIC also added support for the GET statement to read single keystrokes from the keyboard.<\/p>\n<p>On startup, Microsoft BASIC 6502 asks for the size of memory:<\/p>\n<pre>MEMORY SIZE?<\/pre>\n<p>If the user just presses return, BASIC detects the size of memory itself. If, on the other hand, the user enters &#8220;A&#8221;, it prints:<\/p>\n<pre>WRITTEN BY RICHARD W. WEILAND.<\/pre>\n<p>Versions since 1.1 print:<\/p>\n<pre>WRITTEN BY WEILAND & GATES<\/pre>\n<p>Then it asks:<\/p>\n<pre>TERMINAL WIDTH?<\/pre>\n<p>Microsoft&#8217;s codebase could also be assembled either for use in ROM or in RAM: The RAM version additionally asks:<\/p>\n<pre>WANT SIN-COS-TAN-ATN?<\/pre>\n<p>These four statements are located at the very end of the interpreter image (actually, the init code is at the very end, but that gets overwritten anyway), so that up to 250 more bytes are available for the BASIC program if the start of BASIC RAM was set to the beginning of the SIN\/COS\/TAN\/ATN code (&#8220;N&#8221;), or to overwrite ATN only (&#8220;A&#8221;) &#8211; in this case, the user would gain about 100 bytes extra.<\/p>\n<p>All these questions were very similar to the ones presented on an Intel 8080 BASIC system &#8211; after all, BASIC 6502 was a direct port.<\/p>\n<p>The start message looks something like this:<\/p>\n<pre>\nMOS TECH 6502 BASIC V1.0\nCOPYRIGHT 1977 BY MICROSOFT CO.\nn BYTES FREE\nOK\n<\/pre>\n<p>Microsoft&#8217;s codebase was very generic and didn&#8217;t make any assumptions on the machine it was running on. A single binary image could run on any 6502 system, if the start of RAM was set correctly, the calls to &#8220;MONRDKEY&#8221;, &#8220;MONCOUT&#8221;, &#8220;LOAD&#8221; and &#8220;SAVE&#8221; were filled with pointers to the machine-specific I\/O code, and the &#8220;ISCNTC&#8221; function was filled with code to test for Ctrl+C.<\/p>\n<p>Microsoft maintained this source tree internally and, at different points in time, handed their current version of the source to OEMs, which adapted and\/or extended it for their machines. While most OEM versions were heavily modified in its user interaction (startup screen, line editing&#8230;), most of the code was very similar; some functions were even never changed for any version of BASIC. No OEM ever came back to Microsoft for updates, except for Apple and Commodore, which both synced once each, up to the bugfixed version 2.<\/p>\n<h3>Commodore BASIC 1 (1.0)<\/h3>\n<p>The BASIC that shipped with the <a href=\"http:\/\/www.zimmers.net\/anonftp\/pub\/cbm\/firmware\/computers\/pet\/index.html\">first Commodore PET<\/a> in 1977 is the oldest known version of Microsoft BASIC for 6502. It does not say &#8220;Microsoft&#8221; anywhere, and memory size detection and screen width were hardcoded, so on startup, it just prints <tt>*** COMMODORE BASIC ***<\/tt>, followed by the number of bytes available for BASIC.<\/p>\n<p>Commodore added the &#8220;OPEN&#8221;, &#8220;CLOSE&#8221;, &#8220;PRINT#&#8221;, &#8220;INPUT#&#8221; and &#8220;CMD&#8221; statements for file I\/O and added VERIFY to compare a program in memory to a file on a storage device. They also added &#8220;SYS&#8221; to call into assembly code &#8211; Microsoft&#8217;s code had only provided the &#8220;USR&#8221; function with a similar purpose. It seems Commodore didn&#8217;t like the &#8220;OK&#8221; prompt, so they renamed it to &#8220;READY.&#8221;.<\/p>\n<p>All machine-specifics were properly abstracted by calls into the KERNAL jump table, the upper 7 KB of the 16 KB ROM &#8211; except for one call out into the screen editor part of the PET ROM:<\/p>\n<pre>\n        iny\n        lda     (INDEX),y\n.ifdef CONFIG_CBM1_PATCHES\n        jsr     LE7F3\t; patch\n.else\n        ldy     #$00\n        asl     a\n        adc     #$05\n.endif\n        adc     INDEX\n        sta     INDEX\n        bcc     L33A7\n        inc     INDEX+1\n<\/pre>\n<p>This code fixes the garbage collector by doing the missing ldy\/asl\/adc in the patch code.<\/p>\n<p>Speaking of patches: Commodore BASIC 1 has been binary patched a lot: There are six patch functions appended to the very end of the interpreter image that work around miscellaneous fixes. This is what one of these calls into a patch function looks like:<\/p>\n<pre>\n  .ifdef CONFIG_CBM1_PATCHES\n        jmp     PATCH1\n  .else\n        clc\n        jmp     CONTROL_C_TYPED\n  .endif\n<\/pre>\n<p>Here is the patch function &#8211; someone indeed forget to clear the carry flag:<\/p>\n<pre>\nPATCH1:\n        clc\n        jmp     CONTROL_C_TYPED\n<\/pre>\n<p>Some of these patches are in generic code, and some in Microsoft-specific code. Later fixes in generic code are not necessarily identical to these patches. So this indicates that Commodore wrote the fixes. But it is unknown why these additions were done in the binary as opposed to the source: Commodore had the source and made lots of additions to it. Maybe it was just more convenient to patch the binary for debugging at some point.<\/p>\n<h3>Ohio Scientific (1.0a)<\/h3>\n<p>Ohio Scientific sold a wide series of 6502-based machines for several years, but they all shipped with the same version of <a href=\"http:\/\/osiweb.org\/osiweb\/software.html\">6 digit BASIC<\/a> bought from Microsoft in 1977.<\/p>\n<p>6 digit vs. 9 digit was probably a compile time option, because the differences are pretty straightforward, as can be seen in this example:<\/p>\n<pre>\n; ----------------------------------------------------------------------------\n; ADD MANTISSAS OF FAC AND ARG INTO FAC\n; ----------------------------------------------------------------------------\nFADD4:\n        adc     ARGEXTENSION\n        sta     FACEXTENSION\n.ifndef CONFIG_SMALL\n        lda     FAC+4\n        adc     ARG+4\n        sta     FAC+4\n.endif\n        lda     FAC+3\n        adc     ARG+3\n        sta     FAC+3\n        lda     FAC+2\n        adc     ARG+2\n        sta     FAC+2\n        lda     FAC+1\n        adc     ARG+1\n        sta     FAC+1\n        jmp     NORMALIZE_FAC5\n<\/pre>\n<p>Ohio Scientific only made minimal adaptions for their computers, and added no extensions. It asks for memory size and terminal width, and then prints <tt>OSI 6502 BASIC VERSION 1.0 REV 3.2\"<\/tt>.<\/p>\n<p>One quirk on the Ohio Scientific is the inclusion of the <tt>WANT SIN-COS-TAN-ATN<\/tt> string, although BASIC ran in ROM. The code to print this string and adjust memory layout accordingly is not included. OSI BASIC is 7906 bytes in size. Without the extra string, they could have saved 21 bytes.<\/p>\n<p>The string <a href=\"http:\/\/osiweb.org\/osiweb\/software.html\">Garbage Collector was horribly broken in OSI BASIC<\/a>, effectively destroying all string data &#8211; in Commodore BASIC 1, it had been binary patched for fix the problem.<\/p>\n<h3>AppleSoft I (1.1)<\/h3>\n<p>Apple shipped the first Apple II systems with Integer BASIC in ROM, Microsoft BASIC was only available as an option loaded from disk or tape. <a href=\"http:\/\/cowgod.org\/apple2\/tapes\/\">AppleSoft BASIC<\/a>, as it was named, had only minor adaptions and extensions. On startup, it printed:<\/p>\n<pre>\nAPPLE BASIC V1.1\nCOPYRIGHT 1977 BY MICROSOFT CO.\n<\/pre>\n<p>In order to make AppleSoft feel more like Integer BASIC, it showed a &#8216;]&#8217; character instead of &#8220;OK&#8221; and said &#8220;ERR&#8221; instead of ERROR.<\/p>\n<p>The memory size easter egg was modified in this version, it printed <tt>COPYRIGHT 1977 BY MICROSOFT CO<\/tt> instead of Weiland&#8217;s and Gates&#8217; names. Since the Apple II character output code ignored the uppermost bit, this text could be hidden in ROM by setting the MSBs of every character:<\/tt><\/p>\n<pre>\n.;287F C3 CF D0 D9 D2 C9 C7 C8 \"COPYRIGH\"\n.;2887 D4 A0 B1 B9 B7 B7 A0 C2 \"T 1977 B\"\n.;288F D9 A0 CD C9 C3 D2 CF D3 \"Y MICROS\"\n.;2897 CF C6 D4 A0 C3 CF 0D 00 \"OFT CO.\"\n<\/pre>\n<p>This version introduced another easter egg present in all later versions: BASIC 1.1 was the first version to include the &#8220;MICROSOFT!&#8221; easter egg text, as <a href=\"http:\/\/www.pagetable.com\/?p=43\">described in a previous article<\/a>. The encoded (XOR 0x87) text was hidden in some floating point constants and never addressed.<\/p>\n<p>AppleSoft I is the oldest known BASIC 1.1. Compared to 1.0, version 1.1 included minor bugfixes in GET\/INPUT\/READ, TAB() and LIST, as well as the fix in the <a href=\"http:\/\/osiweb.org\/osiweb\/software.html\">Garbage Collector<\/a> present in the Ohio Scientific machines and binary patched in Commodore BASIC 1.<\/p>\n<p>BASIC 1.0 also had a bug where lines in direct mode that started with a colon were ignored:<\/p>\n<pre>\n        jsr     CHRGET\n.ifdef CONFIG_11\n        tax\n.endif\n        beq     L2351\n<\/pre>\n<p>CHRGET is supposed to set the zero flag on the end of an instruction, which can be end of line (0 character) or a colon. The original code wanted to check for an empty line and got the first character, and went on reading another line of it was empty &#8211; but a colon as the first character had the same effect. 1.1 fixed this by setting the flags on the value again.<\/p>\n<p>Version 1.1 also contained various tiny speed optimizations: BEQs and BNEs were changed so that a cycle could be saved on the more likely case.<\/p>\n<p>Here is another optimization in LEFT$\/RIGHT$\/MID$:<\/p>\n<pre>\n.ifndef CONFIG_11\n        sta     JMPADRS+1\n        pla\n        sta     JMPADRS+2\n.else\n        tay\n        pla\n        sta     Z52\n.endif\n[...]\n.ifdef CONFIG_11\n        lda     Z52\n        pha\n        tya\n        pha\n.endif\n        ldy     #$00\n        txa\n.ifndef CONFIG_11\n        inc     JMPADRS+1\n        jmp     (JMPADRS+1)\n.else\n        rts\n.endif\n<\/pre>\n<p>The original code isn&#8217;t only suboptimal, it&#8217;s even dangerous, because it only increments the low byte of the address it wants to jump to and assumes it doesn&#8217;t roll over to $00.<\/p>\n<p>For some reason, the random number seed was changed slightly:<\/p>\n<pre>\n    .ifdef CONFIG_11\n        .byte   $80,$4F,$C7,$52,$58\n    .else\n        .byte   $80,$4F,$C7,$52,$59\n    .endif\n<\/pre>\n<p>But this doesn&#8217;t make a difference, due to a bug present in all 9 digit versions of BASIC: The value is copied into the zero page together with the CHRGET routine:<\/p>\n<pre>\n.ifdef CONFIG_SMALL\n        ldx     #GENERIC_CHRGET_END-GENERIC_CHRGET\n.else\n        ldx     #GENERIC_CHRGET_END-GENERIC_CHRGET-1\n.endif\nL4098:\n        lda     GENERIC_CHRGET-1,x\n        sta     CHRGET-1,x\n        dex\n        bne     L4098\n<\/pre>\n<p>On 9 digit BASIC, one extra byte had to be copied, but the start index was not changed, so the last digit was omitted. This bug exists in every known version of Microsoft BASIC.<\/p>\n<p>Another bug was introduced on the Apple II: All previous versions of BASIC had the input buffer for instructions in direct mode in the zero page. On the Apple II, it was at $0200 in RAM, which broke some code that made assumptions on the address:<\/p>\n<pre>\nNEWSTT:\n        jsr     ISCNTC   ; check for Ctrl + C\n        lda     TXTPTR\n        ldy     TXTPTR+1 ; high-byte of instruction pointer\n        beq     L2683    ; 0 -> direct mode\n        sta     OLDTEXT\n        sty     OLDTEXT+1\n<\/pre>\n<p>Subsequent versions of BASIC compared the high-address of the text pointer:<\/p>\n<pre>\n        cpy     #>INPUTBUFFER\n<\/pre>\n<h3>KIM-1 (1.1a)<\/h3>\n<p>The KIM-1 is a computer kit based around the MOS 6502, which was sold by the makers of the 6502 to show off the capabilities of this CPU. A 6 digit and a 9 digit version of Microsoft BASIC <a href=\"http:\/\/www.hansotten.com\/indexkim.html\">was available on tape<\/a>, but the 6 digit version seems to be very rare. BASIC for the KIM-1 is the most authentic version of Microsoft BASIC, because it has only been minimally modified, it contains all questions about memory size, screen width, and the trigonometric functions, as well as the memory width easter egg. The encoded &#8220;MICROSOFT!&#8221; string can be found at the same spot as on the Apple II.<\/p>\n<p>Although this is based on BASIC 1.1, just like AppleSoft I, there are a few fixes in array handling and the PRINT statement.<\/p>\n<p>But they also introduced another bug: In input handling, again concerning the location of the input buffer, there is the following code:<\/p>\n<pre>\n        ldx     #<(INPUTBUFFER-1)\n        ldy     #>(INPUTBUFFER-1)\n        bne     L2AF8\t; always\n<\/pre>\n<p>This code has been in place since 1.0 and assumes that INPUTBUFFER is above $0100. On the CBM1, which had the input buffer in the zero page, this had been hotfixed by Commodore by swapping the ldx and the ldy. On the OSI, this code didn&#8217;t exist, as it is only included in versions that have the GET statement, i.e. 9 digit versions. AppleSoft I was not affected either, because it had the input buffer at $0200. And versions after the KIM fixed this by replacing the BNE with a BEQ in case the input buffer is in the zero page. It is obviously hard to maintain a single codebase with many compile time options that still does optimizations like these.<\/p>\n<p>Since the first KIM-1 systems shipped in late 1975, their CPUs had the <a href=\"http:\/\/www.pagetable.com\/?p=45\">6502 ROR bug<\/a>, so KIM-1 BASIC had to work around this: Every ROR instruction is replaced by a corresponding sequence using LSR instead.<\/p>\n<h3>AppleSoft II (2.0)<\/h3>\n<p>AppleSoft II is the oldest version of Microsoft BASIC 2. It was available on tape or disk, and also in ROM in later Apple II models. It is the first BASIC from an OEM that had extended BASIC which was re-sync&#8217;ed with Microsoft&#8217;s codebase. In other words: Apple licensed an improved and bugfixed version of BASIC, and merged their old changes into it.<\/p>\n<p>BASIC 2 contains mostly bugfixes (all input buffer location bugs have finally been eliminated), small optimizations (reuse two adjacent zeros inside the floating point constant of 1\/2 as the 16 bit constant of zero instead of laying it down separately), better error handling for DEF FN, and support for &#8220;GO TO&#8221; with a space in between as a synonym for GOTO. Also, the memory test pattern has been changed from $92\/$24 to the more standard $55\/$AA.<\/p>\n<p>In AppleSoft II, Apple also eliminated the &#8220;memory size&#8221; and &#8220;terminal width&#8221; questions.<\/a><\/p>\n<h3>Commodore BASIC 2 (2.0a)<\/h3>\n<p>Just like Apple, Commodore went back to Microsoft for an updated version of BASIC, and integrated its changes into the new version. The version they got was slightly newer than Apple&#8217;s, but the major difference was that Microsoft added the <a href=\"http:\/\/www.pagetable.com\/?p=43\">&#8220;WAIT 6502&#8221; easter egg<\/a>. For this, they changed the encoding of the string &#8220;MICROSOFT!&#8221; that was hidden in every BASIC since 1.1 from XORed ASCII into PETSCII with the upper two bits randomly set &#8211; this way, the text would be just as obfuscated, but it the decoder would be shorter on PET systems. So Commodore BASIC 2 is the only version of Microsoft BASIC that ever accesses this hidden text.<\/p>\n<p>Every version since 2.0a had the PETSCII version of the &#8220;MICROSOFT!&#8221; text in it &#8211; and <a href=\"http:\/\/www.pagetable.com\/?p=43\">so did every version of BASIC for 6809<\/a>.<\/p>\n<h3>Intellivision Keyboard Component BASIC (2.0b)<\/h3>\n<p>The <a href=\"http:\/\/www.spatula-city.org\/~im14u2c\/intv\/\">Mattel Intellivision<\/a> is a game console released in 1980 that contained a very nonstandard 16 bit &#8220;CP1610&#8221; CPU. After a series of delays, the &#8220;Keyboard Component&#8221;, an extension with its own 6502 CPU and Microsoft BASIC, was released in 1982, but canceled very soon. They are very rare today.<\/p>\n<p>The BASIC in the Keyboard Component is the most custom of all known versions. It is based on a 6 digit version of BASIC 2 and younger than Commodore BASIC 2: It contains two bugfixes: One piece of code that pulled its caller&#8217;s address from the stack and normalized it by adding one, had forgotten to respect the carry, so this could fail if the caller sits just on a page boundary. The other fix changed the number of steps needed for normalizing a floating point number.<\/p>\n<p>Intellivision BASIC replaced LOAD and SAVE by PLOD, PSAV, VLOD, VSAV and SLOD, PRT, GETC and VER were added, and PEEK, POKE and WAIT were removed. But the customizations were even more extensive: Instead of keeping the interface to library code, a lot of code was replaced inline, and the whole init code was rewritten. While most of the generic code, for example memory handling was unchanged across Commodore, Ohio, AppleSoft and KIM, making it easier to later integrate Microsoft&#8217;s fixes, some of even this code was altered on the Keyboard Component.<\/p>\n<p>What is interesting about the strings in Intellivision BASIC is that they use both upper- and lower case. The start message is this:<\/p>\n<pre>\nINTELLIVISION BASIC\nCopyright Microsoft, Mattel  1980\n<\/pre>\n<p>But upper-\/lowercase support doesn&#8217;t stop here: The complete code has been extended to be case insensitive, but case preserving. The CHRGET code, a super-optimized function living in the zeropage has been patched with a call to this function:<\/p>\n<pre>\nLF430:\n        cmp     #'a'\n        bcc     LF43A\n        cmp     #'z'+1\n        bcs     LF43A\n        sbc     #$1F\nLF43A:\n        rts\n<\/pre>\n<p>This very unoptimized piece of code adds at least 17 cycles to every CHRGET, and will slow down execution measurably.<\/p>\n<h3>Microtan BASIC (2.0c)<\/h3>\n<p>The version of BASIC that shipped on the Tangerine MICROTAN 65 is, like the Ohio and KIM versions, again a very authentic version with few changes. The updated BASIC 2 contained a single bug fix, which is the floating point constant of -32768 which hadn&#8217;t been updated from 6 to 9 digits correctly and was missing a byte. The startup message looks like this:<\/p>\n<pre>\nMICROTAN BASIC\n(C) 1980 MICROSOFT\n<\/pre>\n<p>Microtan BASIC contains the complete &#8220;memory size&#8221; and terminal width procedures and the &#8220;Weiland\/Gates&#8221; easter egg.<\/p>\n<p>Although the Microtan was introduced in 1980, its version of BASIC was, like the KIM version, assembled with code that <a href=\"http:\/\/www.pagetable.com\/?p=45\">worked around the ROR bug<\/a> in 6502 chips until mid-1976. The I\/O library on the other hand made use of ROR, suggesting that this compile time option was set in error.<\/p>\n<h3>Bugs never fixed<\/h3>\n<p>As you can see, the first versions had many bugs that were quickly fixed, but fixed became less and less &#8211; simply because there were only very few bugs left. But still there are some bugs that never got fixed. The short copy of the random number seed for example, exists on all versions.<\/p>\n<p>Similarly, the two extra constants used for generating random numbers (CONRND1, CONRND2) are 4 bytes in all versions, which is one byte short for 9 digit BASIC. But this is another bug that doesn&#8217;t really matter, since the numbers will still be random enough.<\/p>\n<p>The buggy check on large line numbers has also never been fixed. Typing <a href=\"http:\/\/www.ffd2.com\/fridge\/chacking\/c=hacking10.txt\">35072121<\/a> into any version of Microsoft BASIC will have the interpreter jump to a pseudo random memory address. The buggy code resides in &#8220;LINGET&#8221;.<\/p>\n<p>Something similar happens in the case of <tt>PRINT 5+\"A\"+-5<\/tt>: The interpreter will build up the formula on the CPU stack, but miss the string\/float type mismatch because of the &#8220;+-&#8220;, and messes up its stack when removing items. This bug is in &#8220;FRMEVL&#8221;.<\/p>\n<p>But the fact that Microsoft never fixed these bugs in their codebase doesn&#8217;t mean none of the OEMs fixed them. While the LINGET and FRMEVL seem to have been unnoticed everywhere, at least the CONRND1\/CONRND2 bug has been fixed by Commodore, at least as early as for the VIC-20 in 1980.<\/p>\n<h3>How to build your own BASIC<\/h3>\n<p>Now that you have the source that can build seven different OEM versions of Microsoft BASIC, and that you know about the differences between those, you might be interested in building your own version of BASIC 6502 for some 6502-based machine or customizing BASIC to build a bugfixed or extended version for some platform.<\/p>\n<p>First duplicate one of the cfg files, and add it to make.sh. cbmbasic2 is a good start, as you can quite easily test the resulting images in the <a href=\"http:\/\/www.viceteam.org\/\">VICE emulator<\/a> &#8211; CC65 can even provide symbol information for the VICE debugger. Add a case in defines.s to define one of CBM1, CBM2, APPLE etc., because you need one flavour of platform specific code, and include your own defines_*.s. For Commodore BASIC, you also need to define CONFIG_CBM_ALL.<\/p>\n<p>If you are targeting a new type of computer, make sure to adjust the zero page locations in your defines_*.s file (ZP_STARTn) so that they don&#8217;t clash with your I\/O library. Also make sure that, in case you are compiling for RAM, the init code does not try to detect the memory size and overwrite itself.<\/p>\n<p>The CONFIG_n defines specify what Microsoft-version the OEM version is based on. If CONFIG_2B is defined, for example, CONFIG_2A, CONFIG_2, CONFIG_11A, CONFIG_11 and CONFIG_10A will be defined as well, and all bugfixes up to version 2B will be enabled. The following symbols can be defined in addition:<\/p>\n<table border=\"1\">\n<tr>\n<td>CONFIG_CBM1_PATCHES<\/p>\n<td>jump out into CBM1&#8217;s binary patches instead of doing the right thing inline<br \/>\n<\/tr>\n<tr>\n<td>CONFIG_CBM_ALL<\/p>\n<td>add all Commodore-specific additions except file I\/O<br \/>\n<\/tr>\n<tr>\n<td>CONFIG_DATAFLG<\/p>\n<td>?<br \/>\n<\/tr>\n<tr>\n<td>CONFIG_EASTER_EGG<\/p>\n<td>include the CBM2 &#8220;WAIT 6502&#8221; easter egg<br \/>\n<\/tr>\n<tr>\n<td>CONFIG_FILE<\/p>\n<td>support Commodore PRINT#, INPUT#, GET#, CMD<br \/>\n<\/tr>\n<tr>\n<td>CONFIG_IO_MSB<\/p>\n<td>all I\/O has bit #7 set<br \/>\n<\/tr>\n<tr>\n<td>CONFIG_MONCOUT_DESTROYS_Y<\/p>\n<td>Y needs to be preserved when calling MONCOUT<br \/>\n<\/tr>\n<tr>\n<td>CONFIG_NO_CR<\/p>\n<td>terminal doesn&#8217;t need explicit CRs on line ends<br \/>\n<\/tr>\n<tr>\n<td>CONFIG_NO_LINE_EDITING<\/p>\n<td>disable support for Microsoft-style &#8220;@&#8221;, &#8220;_&#8221;, BEL etc.<br \/>\n<\/tr>\n<tr>\n<td>CONFIG_NO_POKE<\/p>\n<td>don&#8217;t support PEEK, POKE and WAIT<br \/>\n<\/tr>\n<tr>\n<td>CONFIG_NO_READ_Y_IS_ZERO_HACK<\/p>\n<td>don&#8217;t do a very volatile trick that saves one byte<br \/>\n<\/tr>\n<tr>\n<td>CONFIG_NULL<\/p>\n<td>support for the NULL statement (send sync 0s for serial terminals)<br \/>\n<\/tr>\n<tr>\n<td>CONFIG_PEEK_SAVE_LINNUM<\/p>\n<td>preserve LINNUM on a PEEK<br \/>\n<\/tr>\n<tr>\n<td>CONFIG_PRINTNULLS<\/p>\n<td>whether PRINTNULLS does anything<br \/>\n<\/tr>\n<tr>\n<td>CONFIG_PRINT_CR<\/p>\n<td>print CR when line end reached<br \/>\n<\/tr>\n<tr>\n<td>CONFIG_RAM<\/p>\n<td>optimizations for RAM version of BASIC, only use on 1.x<br \/>\n<\/tr>\n<tr>\n<td>CONFIG_ROR_WORKAROUND<\/p>\n<td>use workaround for buggy 6502s from 1975\/1976; not safe for CONFIG_SMALL!<br \/>\n<\/tr>\n<tr>\n<td>CONFIG_SAFE_NAMENOTFOUND<\/p>\n<td>check both bytes of the caller&#8217;s address in NAMENOTFOUND<br \/>\n<\/tr>\n<tr>\n<td>CONFIG_SCRTCH_ORDER<\/p>\n<td>where in the init code to call SCRTCH<br \/>\n<\/tr>\n<tr>\n<td>CONFIG_SMALL<\/p>\n<td>use 6 digit FP instead of 9 digit, use 2 character error messages, don&#8217;t have GET<br \/>\n<\/table>\n<p>Changing symbol definitions can alter an existing base configuration, but it is not guaranteed to assemble or work correctly.<\/p>\n<p>I am very interested in your creations. Please add a comment to this article if you have made something new out of this source base!<\/p>\n<h3>Using the Floating Point Library Standalone<\/h3>\n<p>The complete project has been split into many components, each in their own assembly source file. The core floating point library is in float.s, extra trigonometric functions are in trig.s. It should not be too hard to use this broken-out part (in a 6 digit or 9 digit version) standalone in your own creations. The 9 digit version is a little over 2 KB in size, the 6 digit version is a little smaller.<\/p>\n<h3>Adding More Versions<\/h3>\n<p>If you want to add another version of BASIC into the source base, you can do it like this: Use &#8220;da65&#8221; from the CC65 package to dissemble your version of BASIC  and all existing .bin files (with the correct base addresses), and run a &#8220;diff&#8221; command on the new disassembly and each of the disassemblies of the existing versions. The diff that contained the fewest changes (just look at the file size) is probably a good candidate to base your new version on. Or look at the release date or the family tree to find a version which is similar.<\/p>\n<p>Now create a new version in the source base, as described earlier. Make sure the new version assembles; then compare the disassembly of your version with the disassembly of the original binary in a diff program, like the excellent Mac OS X FileMerge, to find the differences. In most cases, you will only have to adjust a few defines (CONFIG_* and zero page locations) in your defines_*.s file to get matching output. Otherwise, add ifdefs to the respective source files. Run regress.sh to verify that you didn&#8217;t break the other versions.<\/p>\n<p>Repeat the last step until the assembly process outputs the same file. Send your changes to me. \ud83d\ude42<\/p>\n<p>Note that the idea of all versions of BASIC in the current source code is that they are all direct forks from Microsoft&#8217;s codebase. I chose not to include versions like Commodore BASIC 4, Commodore BASIC 2 for the VIC-20\/C64 etc., and I wouldn&#8217;t add very late AppleSoft versions, because these are only extended versions of earlier forks and contain no extra code from the original Microsoft source base. Versions that would be very interesting to integrate would be AppleSoft II and Atari Microsoft BASIC, preferably the very first revisions of these.<\/p>\n<h3>Credits<\/h3>\n<ul>\n<li>Function names and all uppercase comments taken from Bob Sander-Cederlof&#8217;s excellent <a href=\"http:\/\/www.txbobsc.com\/scsc\/scdocumentor\/\">AppleSoft II disassembly<\/a><\/li>\n<li><a href=\"http:\/\/cowgod.org\/replica1\/applesoft\/\">AppleSoft lite by Tom Greene<\/a> helped a lot, too.<\/li>\n<li>Thanks to Joe Zbicak for his help with Intellivision Keyboard BASIC<\/li>\n<li>This work is dedicated to the memory of my dear hacking pal Michael &#8220;acidity&#8221; Kollmann.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Update: The source is available at github.com\/mist64\/msbasic If you disassemble a single binary, you can never tell why something was done in a certain way. If you have eight different versions, you can tell a lot. This episode of &#8220;Computer Archeology&#8221; is about reverse engineering eight different versions of Microsoft BASIC 6502 (Commodore, AppleSoft etc.), &#8230; <a title=\"Create your own Version of Microsoft BASIC for 6502\" class=\"read-more\" href=\"https:\/\/www.pagetable.com\/?p=46\" aria-label=\"Read more about Create your own Version of Microsoft BASIC for 6502\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,5,6,16,17],"tags":[],"class_list":["post-46","post","type-post","status-publish","format-standard","hentry","category-2","category-archeology","category-basic","category-github","category-hacks"],"_links":{"self":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/posts\/46","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=46"}],"version-history":[{"count":0,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/posts\/46\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=46"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=46"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=46"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}