Pagetable.Com
Total Page:16
File Type:pdf, Size:1020Kb
12/11/2019 Microsoft BASIC for 6502 Original Source Code [1978] – pagetable.com pagetable.com About Some Assembly Required Twitter: Microsoft BASIC for 6502 Original @pagetable Source Code [1978] Mastodon: 2015-01-13 by Michael Steil @pagetable@mastodo n.social This is the original 1978 source code of Microsoft BASIC for 6502 with all original comments, documentation and easter eggs: Categories 6502 Amiga Apple Archeology BASIC Bildschirmtext C64 C128 https://www.pagetable.com/?p=774 1/31 12/11/2019 Microsoft BASIC for 6502 Original Source Code [1978] – pagetable.com TITLE BASIC M6502 8K VER 1.1 BY MICRO-SOFT SEARCH M6502 Commodore SALL RADIX 10 ;THROUGHOUT ALL BUT MATH-PAK. Commodore Peripheral Digital Video DOS $Z:: ;STARTING POINT FOR M6502 SIMULATOR Bus Final ORG 0 ;START OFF AT LOCATION ZERO. SUBTTL SWITCHES,MACROS. Cartridge III Floppy REALIO=4 ;5=STM Disks Game Boy GEOS ;4=APPLE. ;3=COMMODORE. ;2=OSI GitHub Hacks ;1=MOS TECH,KIM Hardware KERNAL ;0=PDP-10 SIMULATING 6502 INTPRC==1 ;INTEGER ARRAYS. ADDPRC==1 ;FOR ADDITIONAL PRECISION. Literature LNGERR==0 ;LONG ERROR MESSAGES. TIME== 0 ;CAPABILITY TO SET AND READ A CLK. Operating Systems EXTIO== 0 ;EXTERNAL I/O PET Presentation M6502.MAC (1978-07-27, 6955 lines, 161,685 bytes) Puzzle SCUMM Security Tapes This is currently the oldest publicly available piece of source written by Bill Gates. Teardown TED Tricks Trivia Uncategorized Language VIC-20 Virtualization Whines X16 x86 Xbox Like the 8080 version, the 6502 version was developed on a PDP-10, using the MACRO-10 assembler. A set of macros developed by Paul Allen allowed MACRO-10 to understand and translate 6502 assembly, albeit in a modified format to fit the syntax of macros, for example: github MOS 6502 MACRO-10 LDA #0 LDAI 0 https://www.pagetable.com/?p=774 2/31 12/11/2019 Microsoft BASIC for 6502 Original Source Code [1978] – pagetable.com LDA (ADDR),Y LDADY ADDR MACRO-10 did not support hex numbers, which is why most numbers are in decimal mist64 format. In the floating point code, all numbers are octal. The RADIX statement switches between the two. Octal can also be forced with a ^O prefix. Worked on x16 rom Conditional translation is done using the IFE and IFN statements, which test whether the argument is zero. The following only adds the string to the binary if REALIO is equal to 4: IFE REALIO-4,<DT"APPLE BASIC V1.1"> Archives Macros Select Month The source defines many macros that make development easier. There are some examples: Macro Definition Comment Meta SYNCHK LDAI <Q> Get the next character and make sure it’s Q, otherwise SYNTAX (Q) JSR SYNCHR ERROR. This pattern is used a lot. Log in LDWD LDA WD Most 16 bit constants are loaded into A/Y with this macro, but Entries RSS (WD) LDY <WD>+1 macros for A/X and X/Y also exist. Comments RSS LDAI WordPress.org LDWDI <<WD>&^O377> This loads an immediate constant into A/Y. (WD) LDYI <<WD>/^O400> https://www.pagetable.com/?p=774 3/31 12/11/2019 Microsoft BASIC for 6502 Original Source Code [1978] – pagetable.com PSHWD LDA <WD>+1 This pushes a 16 bit value from memory (absolute or zero page) (WD) PHA onto the stack. LDA WD PHA JEQ BNE .+5 A compact way to express out-of-bounds branches. Macros (WD) JMP WD exist for all branches. This emits a byte value of 0x2C (BIT absolute), which skips the XWD SKIP2 next instruction. (The ^O1000 part wraps the byte in a PDP-10 ^O1000,^O054 instruction – see below.) Configurations The BASIC source supports several compile-time configuration options: Name Comment Description INTPRC INTEGER ARRAYS FOR ADDITIONAL ADDPRC 40 bit (9 digit) vs 32 bit (7 digit) float PRECISION LONG ERROR LNGERR Error message strings instead of two-character codes MESSAGES TIME CAPABILITY TO TI and TI$ support SET AND READ A CLK https://www.pagetable.com/?p=774 4/31 12/11/2019 Microsoft BASIC for 6502 Original Source Code [1978] – pagetable.com EXTIO EXTERNAL I/O PRINT#, INPUT#, CMD, SYS (!), OPEN and CLOSE support SAVE AND LOAD DISKO LOAD, SAVE (and on Commodore: VERIFY) support COMMANDS FOR THE "NULL" NULL support, a command to configure the number of NUL NULCMD COMMAND characters to print to the terminal after each line break GETCMD GET support RORSW If 1, the ROR instruction is not used TELLS IF THIS IS The RAM version can optionally jetison the SIN, COS, TAN and ROMSW ON ROM ATN commands at startup CLMWID Column width for TAB()“ LONG LONGI INITIALIZATION SWITCH STKEND The top of stack at startup Page of the input buffer; if 0, the buffer uses parts of the BUFPAG zero page INPUT BUFFER BUFLEN SIZE TERMINAL LINE LINLEN LENGTH https://www.pagetable.com/?p=774 5/31 12/11/2019 Microsoft BASIC for 6502 Original Source Code [1978] – pagetable.com ROMLOC ADDRESS OF START OF PURE SEGMENT KIMROM KIM-specific smaller config Targets The constant REALIO is used to configure what computer system to generate the binary for. It has one of the following values: Value Comment Banner Machine PDP-10 SIMULATING SIMULATED BASIC FOR THE Paul Allen’s Simulator on 0 6502 6502 V1.1 PDP-10 1 MOS TECH,KIM KIM BASIC V1.1 MOS KIM-1 2 OSI OSI 6502 BASIC VERSION 1.1 OSI Model 500 3 COMMODORE ### COMMODORE BASIC ### Commodore PET 2001 4 APPLE APPLE BASIC V1.1 Apple II 5 STM STM BASIC V1.1 (unreleased) All versions except Commodore also print “COPYRIGHT 1978 MICROSOFT” in a new line. The target defines the setting of the configuration constants, but some code is also conditionally compiled depending on a specific target. https://www.pagetable.com/?p=774 6/31 12/11/2019 Microsoft BASIC for 6502 Original Source Code [1978] – pagetable.com What is interesting is that initially it was Microsoft adapting their source for the different computers, instead of giving source to the different vendors and having them adapt it. Features like file I/O and time support seem to have been specifically developed for Commodore, for example. Later, the computer companies would get the source from Microsoft and develop themselves – source code of the Apple and Commodore derivatives is available; they both contain Microsoft comments. By the way, the numbering of these targets probably indicated in which order Microsoft signed contracts with computer manufacturers. MOS was first (for the KIM), then OSI, then Commodore/MOS again (this time for the PET), then Apple. The PDP-10 Target Paul Allen’s additional macros for 6502 development made the MACRO-10 assembler output one 36 bit PDP-10 instruction word for every 6502 byte. When targeting a real 6502 machine, the 6502 binary could be created by simply extracting one byte from every PDP- 10 word. In the case of targeting the simulator, the code created by the assembler could just be run without modification, since every emitted PDP-10 instruction was constructed so that it would trap – the linked-in simulator would then extract the 6502 opcode from the instruction and emulate the 6502 behavior. While this trick was mostly abstracted by the (unreleased) macro package, its workings can be seen in a few cases in the BASIC source. Here, it defines SKIP1 and SKIP2. Instead of just emitting 0x24 or 0x2C, respectively, it combines it with the octal value of 01000 to make it a PDP-10 instruction that traps: https://www.pagetable.com/?p=774 7/31 12/11/2019 Microsoft BASIC for 6502 Original Source Code [1978] – pagetable.com DEFINE SKIP1, <XWD ^O1000,^O044> ;BIT ZERO PAGE TRICK. DEFINE SKIP2, <XWD ^O1000,^O054> ;BIT ABS TRICK. In the initialization code, it writes a JMP instruction into RAM. On the simulator, it has to patch up the opcode of JMP (0x4C, decimal 76) to be the correct PDP-10 instruction: LDAI 76 ;JMP INSTRUCTION. IFE REALIO,<HRLI 1,^O1000> ;MAKE AN INST. With this information, we can reconstruct what the set of 6502 macros, which is not part of this source, probably looked like. Here is LDAI (LDA immediate): DEFINE LDAI (Q),< XWD ^O1000,^O251 ;EMIT OPCODE XWD ^O1000,<Q> ;EMIT OPERAND > You can also see native TJSR PDP-10 assembly instructions for character I/O: IFE REALIO,< TJSR INSIM##> ;GET A CHARACTER FROM SIMULATOR IFE REALIO,< TJSR OUTSIM##> ;CALL SIMULATOR OUTPUT ROUTINE https://www.pagetable.com/?p=774 8/31 12/11/2019 Microsoft BASIC for 6502 Original Source Code [1978] – pagetable.com The DDT command, which breaks into the PDP-10’s DDT debugger, only exists in this config: IFE REALIO,< DDT: PLA ;GET RID OF NEWSTT RETURN. PLA HRRZ 14,.JBDDT## JRST 0(14)> The KIM and OSI Targets The KIM target is meant for the MOS KIM-1 and Ohio Scientific OSI Model 500 single- board computers. These are the first ports to specific computers, and also the cleanest, i.e. except for the character I/O interface and the very simple LOAD/SAVE implementation for the KIM, there is nothing specific about these targets. The Commodore Target The Commodore target is meant for the Commodore PET 2001. It includes LOAD/SAVE/VERIFY (the commands jump directly to outside “KERNAL” ROM code), the I/O commands (SYS, PRINT#, OPEN etc.), the GET command and the π, ST, TI and TI$ symbols. CLEAR is renamed to CLR, "OK" is renamed to "READY.", the BEL character is not printed, and character I/O code behaves differently to account for the more featureful screen editor of the PET. Oh, and the Commodore version of course includes the Bill Gates WAIT 6502,1 easter egg! This is the WAIT instruction: ; THE WAIT LOCATION,MASK1,MASK2 STATEMENT WAITS UNTIL THE CONTENTS ; OF LOCATION IS NONZERO WHEN XORED WITH MASK2 https://www.pagetable.com/?p=774 9/31 12/11/2019 Microsoft BASIC for 6502 Original Source Code [1978] – pagetable.com ; AND THEN ANDED WITH MASK1.