BBC BASIC for the Commodore 64 ============================== J.G.Harston, 70 Camm Street, Walkley, Sheffield, S6 3TR http://mdfs.net/Software/BBCBasic/C64 Date: 16-Feb-2013 This is the 6502 implementation of the BBC BASIC programming language with an OS interface veneer to the Commodore 64 Kernal. It uses a slightly patched version of the standard HiBASIC version of BBC BASIC II with an interface veneer to the Commodore 64 Kernal. Kernal Interface ~~~~~~~~~~~~~~~~ The Commodore 64 hardware and OS interface imposes some differences to I/O calls. Colours ------- As a system with no colour palette, COLOUR and GCOL select physical colours with the standard colour mapping for selecting physical colours: %00xxxbgr - &00+num - foreground colour %01xxxbgr - &40+num - reserved %10xxxbgr - &80+num - background colour %11xxxbgr - &C0+num - border colour The Commodore 64 has 16 physical colours, so this gives the following colours: Fore Back ground ground Border Colour 0 &00 128 &80 192 &C0 Black 1 &01 129 &81 193 &C1 Dark Red/Brown 2 &02 130 &82 194 &C2 Dark Green 3 &03 131 &83 195 &C3 Dark Yellow/Orange 4 &04 132 &84 196 &C4 Dark Blue 5 &05 133 &85 197 &C5 Dark Magenta/Purple 6 &06 134 &86 198 &C6 Dark Cyan/Light Red 7 &07 135 &87 199 &C7 Dark White/Grey 8 &08 135 &88 200 &C8 Light Black/Dark Grey 9 &09 136 &89 201 &C9 Light Red 10 &0A 137 &8A 202 &CA Light Green 11 &0B 138 &8B 203 &CB Light Yellow 12 &0C 139 &8C 204 &CC Light Blue 13 &0D 140 &8D 205 &CD Light Magenta/Cyan 14 &0E 141 &8E 206 &CE Light Cyan/Grey 15 &0F 142 &8F 207 &CF White The background colour is the same for the whole screen, so COLOUR &80+num changes the whole screen background. In this it is closer to changing the palette with VDU 19,0,colour,0,0,0. This is a limitation of the Commodore 64 hardware. Screen modes ------------ Currently MODE n just clears the screen and sets white on black text. All Commodore screen modes are 40 characters by 25 lines, just like the BBC's MODE 6, but with 16 colours. It can be thought of as a cross between the BBC's MODE 7 and MODE 1. Character set ------------- Input from the keyboard and output to the screen translate characters to and from the appropriate ASCII character. There are currently no soft (definable) characters. Characters &80 to &8F currently mimic teletext colour characters and select text colour 0 to 15 with a padding space. Control codes ------------- 00 NULL 08 LEFT 10 18 01 09 RIGHT 11 COLOUR n 19 02 0A DOWN 12 1A 03 0B UP 13 1B 04 0C CLS 14 1C 05 0D CR 15 1D 06 0E 16 MODE n 1E HOME 07 0F 17 1F TAB x,y 7F DELETE Unimplemented control codes are passed to UKVDU (Unknown VDU). Keyboard layout --------------- The Commodore uses the following keyboard layout when running BBC BASIC. +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----++--------+ | | ! | " | # | $ | % | & | ' | ( | ) | | \ | | | _ | f0 | BS || f2 | | TAB | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | + | - | £ | COPY| DEL || f1 | +-----+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-----++--------+ | | q | w | e | r | t | y | u | i | o | p | { | } | ~ | || f4 | | CTRL | Q | W | E | R | T | Y | U | I | O | P | @ | * | ^ |RESTORE || f3 | +-+--+----++----++----++----++----++----++----++----++----++----++----++----++----++-----+----+---++--------+ | f10| | a | s | d | f | g | h | j | k | l | [ | ] | = | f9 | | f6 | | Esc|LOCK| A | S | D | F | G | H | J | K | L | : | ; | = | RETURN | | f5 | +----+----+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-----+-----------+ +--------+ | | | z | x | c | v | b | n | m | < | > | ? | | Up | <== | | f8 | | C= | SHIFT | Z | X | C | V | B | N | M | , | . | / | SHIFT | Dn | ==> | | f7 | +----+-------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+--------+-----+-----+ +--------+ +-----------------------------------------------------+ | | | SPACE | +-----------------------------------------------------+ Notes: Top line: Shift+key Bottom line: unshifted key The Commodore with a PC keyboard, or an emulator using a PC keyboard, uses the following keyboard layout when running BBC BASIC. +-----+ +-----+-----+-----+-----+ +-----+-----+-----+-----+ +-----+-----+-----+-----+ +-----+-----+-----+ | f10 | | f2 | f1 | f4 | f3 | | f6 | f5 | f8 | f7 | | | | | | | | | | | ESC | | f1 | f2 | f3 | f4 | | f5 | f6 | f7 | f8 | | | | | | | | | | +-----+ +-----+-----+-----+-----+ +-----+-----+-----+-----+ +-----+-----+-----+-----+ +-----+-----+-----+ +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+---------+ +-----+-----+-----+ | | ! | " | # | $ | % | & | ' | ( | ) | | \ | | | BS | | _ | f0 | | | TAB | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | + | - | DEL | | £ | COPY|RESTR| +-----+-+---+-+---+-+---+-+---+-+---+-+---+-+---+-+---+-+---+-+---+-+---+-+---+-+-------+ +-----+-----+-----+ | | q | w | e | r | t | y | u | i | o | p | { | } | f9 | | ~ | | | | CTRL | Q | W | E | R | T | Y | U | I | O | P | @ | * |RETURN | | ^ | | | +-------+-+---+-+---+-+---+-+---+-+---+-+---+-+---+-+---+-+---+-+---+-+---+-+---+-+ | +-----+-----+-----+ | f10 | a | s | d | f | g | h | j | k | l | [ | ] | = | | | ESCAPE | A | S | D | F | G | H | J | K | L | : | ; | = | | +-----+---+-+---+-+---+-+---+-+---+-+---+-+---+-+---+-+---+-+---+-+---+-+---+-----+-----+ +-----+ | | | z | x | c | v | b | n | m | < | > | ? | | | Dwn | |SHIFT| | Z | X | C | V | B | N | M | , | . | / | SHIFT | | Up | +-----+-+---+-+---+-+---+-----+-----+-----+-----+-----+-----+---+-+---+-+---+-----+-----+ +-----+-----+-----+ | | | | | | | | | | ==> | Up | <== | | C= | | | SPACE | | | | | | <== | Dwn | ==> | +-------+-----+-----+-------------------------------------------+-----+-----+-----+-----+ +-----+-----+-----+ Notes: Top line: Shift+key Bottom line: unshifted key File interface -------------- Filenames are d:filename, for instance: LOAD "8:ClockSpeed" is the equivalent of Commodore Basic's LOAD "ClockSpeed",8 Device numbers are in hex, ie 8: 9: A: B: etc. On startup, the last accessed device is used as the current default drive. The default drive can be changed with *D: or *DRIVE D, eg: LOAD "1:ClockSpeed":REM Loads from tape REM Now select device 8: *DRIVE 8 LOAD "Environ" :REM Loads from device 8 LOAD "9:TestFS" :REM Loads from device 9 SAVE "TestFS" :REM Saves to device 8 (*D: and *DRIVE currently unimplemented, may also implement decimal device numbers) If you have a suitable replacement DOS installed, then full paths can also be used, for example: LOAD "8:/directory/filename" is the equivalent of Commodore BASIC's LOAD "/directory/filename",8 Program environment ------------------- OSBYTE 0,<>0 returns %xx0x1xxx, a bitmap that shows: * (os% AND -24)=8 - Directory seperators are '/' * (os% AND -24)<>0 - Extension seperators are '.' * (os% AND -24)<>0 - Drive specifiers are d: This allows the following standard code to be used: d$=".":s$="/":IF(os%AND-24):d$="/":s$=".":IF(os%AND-32):d$="\" filename$=dir$+d$+name$+s$+ext$ PAGE is <&FFFF, indicating that calls to &FFxx are allowed, allowing the following standard code to be used: IF PAGE<&FFFF THEN CALL &FFF4 HIMEM is <&FFFF, indicating a small/64K memory map. OSARGS 0,0 returns a number (to be decided) indicating CommodoreFS. (Currently returns &00) CALL/USR -------- BBC BASIC runs with the full 64K RAM paged in, the OS interface veneer pages in the Kernal and I/O for the duration of Kernal calls. If the called address for CALL or USR is less than &400000 then a BBC API environment is available at &FFxx. If the called address is &400000 or greater then the Commodore 64 Kernal and I/O is mapped in. This gives the usual &FFFFxxxx for I/O devices. &64xxxx is also a useful range to use as a visual reminder of calling the C64 Kernal. For example: A%=ASC"A":CALL &FFEE :REM Call BBC OSWRCH A%=ASC"A":CALL &64FFD2 :REM Call C64 CHROUT This also applies to code in main memory: DIM mcode% 50:P%=mcode%:[ machine code ... CALL mcode% :REM Calls code with BBC API visible CALL &640000+mcode% :REM Calls code with C64 API visible CALL &FFFF0000+mcode% :REM Calls code with I/O devices visible Memory layout ------------- &0000 Zero page &0400-&07FF BASIC workspace &0800 Default PAGE, set from C64 Kernal MemBottom BASIC programs occupy memory going up from PAGE &8000 MemTop with bitmapped screen. &AC00 Text screen and default top of memory. On startup, the C64 Kernal MemTop is set to this. HIMEM is initially set to this, and BASIC's stack occupies memory going down from here. &B000-&B7FF - Startup and OS Interface &B800-&F77F - BBC BASIC &F800-&FFFF - OS Interface Zero page locations ~~~~~~~~~~~~~~~~~~~ &00-&8F - BASIC, as with C64 &50-&6F - Currently being used for Host workspace &70-&8F - Reserved for use by programs running in BASIC &90-&FA - Kernal, as with C64 &FB-&FF - Interface between BASIC and Kernal &FB/FC - reserved &FD/FE - Error pointer &FF - Escape state OSWORD 5/6 access BASIC memory with address<&00400000 and Kernal/IO with address>&003FFFFF. For testing ADVAL(addr) also accesses Kernal/IO, so eg ADVAL(&D020) reads the border colour. misc ---- BBC BASIC can be put in a Commodore 64 cartridge if: * The cartridge can hold 20K of code * The cartridge can switch itself out of memory The cartridge should copy the BASIC code to &B000-&FFFF, page itself out of memory, then jump to &FF98 (OSCOLD). org &8000 EQUW cold:EQUW warm:EQUS "CBM80" .cold SEI:LDA #&30:STA &01 :\ Page in all RAM copy from cartridge to &B000 page out the cartidge JMP &FF98 The cartridge has to page itself and and then jump via &B000. It is probably easier to do this by copying a bit of code to the stack: \ needs RAM paged in at &E0xx-&FFxx 0100 xx xx xx STA xxxx :\ Do whatever pages out the cartridge 0103 4C xx FF JMP FFxx :\ Initialise and start There is an OSQUIT entry in the Host API (&FFA7). Unsure as yet what that should do. Conventionally, it should return to whatever called the current program. Vectors ------- Not yet implmented, still checking C64 extension ROMs for what they hook into. &200-&258 is C64 BASIC text input buffer, but some extensions also use it, so potentially dangerous to use it for vectors. &300-&313 BASIC vectors NB! JiffyDOS uses &313 for default drive &314-&333 Kernal vectors Will be testing with JiffyDOS for file access.