bobbimanners / Applecorn

Allows Acorn BBC Microcomputer language ROMs to run on Apple //e enhanced, //c and IIGS.
GNU General Public License v3.0
48 stars 8 forks source link
6502-assembly acorn apple2 apple2gs bbc-basic bbc-micro

Applecorn

Applecorn Logo

Applecorn is a ProDOS application for the Apple //e Enhanced which provides an environment for Acorn BBC Microcomputer language ROMs to run. This allows BBC BASIC and other Acorn languages to run on the Apple //e and compatible systems. Applecorn implements the Acorn MOS (Machine Operating System) API on top of Apple ProDOS.

The language ROMs run as-is, without any modification required.

Any BBC Micro software that follows Acorn's coding guidelines and uses approved MOS system calls should work on Applecorn, subject to the limitations imposed by the different capabilities of the Apple II hardware.

Hardware Requirements

The minimum requirement for Applecorn is an Apple II system with 128KB of memory and a 65C02 processor. This includes the following:

Note, for the Apple IIgs, you should NOT enable the "Alternate Text Mode" CDA!

How to Run the Software

Boot the diskette applecorn.po which is an 800KB bootable ProDOS diskette. Applecorn is a .SYSTEM program and should start automatically when this disk is booted.

You can optionally boot your system from other ProDOS media, and then simply select to start APLCORN.SYSTEM from your favorite ProDOS selector, such as Bitsy-Bye. I use version 2.4.2 of ProDOS for my testing, but the software should run on other versions of ProDOS.

When first started, Applecorn will display a ROM selection menu. Choose the language ROM you wish to load by pressing the associated number key. Applecorn will then load the requested ROM file from the diskette. Each of these files is a dump of a 16KB BBC Micro language ROM.

Once the ROM has loaded, it will automatically be started and you will see the prompt. For BBC BASIC, the prompt character is >.

Most of the BBC Micro languages (including BBC BASIC) prefer upper case input. You may want to keep Caps Lock enabled most of the time!

32 Kilobytes of space is available for your programs and variables. PAGE is set to &0E00.

'Applecorn MOS' Features

Compatible Language ROMs

All of the language manuals are provided in a separate GitHub repo: https://github.com/bobbimanners/Applecorn-Manuals

The Applecorn diskette includes all of the example programs which were provided on diskette with COMAL, Forth, Lisp and ISO Pascal. The main BCPL diskette, which contains tools that are essential for using the BCPL ROM, is also included on the Applecorn diskette.

Applecorn Collage

Video Modes

The following video modes are supported:

On Apple //e and //c, MODE 0 will redirect to MODE 3 and MODE 1 will redirect to MODE 6.

Graphics are supported in the bitmapped modes. MODE 0 and MODE 1 emulate the respective modes of the BBC Micro reasonably faithfully; MODE 2 is quite different, due to the quirks of the Apple II HGR mode.

Following BBC Micro conventions, all graphics are drawn using a 1280x1024 coordinate system, with the origin at the bottom left of the screen.

NOTE: You can set the startup video mode by holding down the appropriate number key while Applecorn starting (while it is loading the ROM file.)

Andy McFadden's FDraw library is used for efficient high resolution line and point plotting in MODE 2.

Escape Key

The BBC Micro uses interrupts extensively and the Escape key is handled asynchronously. The language ROM code simply checks an 'escape flag' in zero page ($FF) from time to time to detect if Escape has been pressed.

The Apple //e does not use interrupts in its keyboard handling and the basic machine include no sources of interrupts at all (there is no system timer.) This prevents Escape from being handled in the same manner.

As a partial workaround, Applecorn checks whether the Escape key is pressed from time to time when it has control, but there are cases where a program can run forever without ever making a MOS call. In these cases the only way to interrupt the program is to press Ctrl-Reset.

Ctrl-Reset

The Ctrl-Reset key combination is the only asynchronously handled keyboard event on the Apple //e. Applecorn sets up a reset handler which will restart the ROM after Ctrl-Reset. Any user program in aux memory will be untouched.

For ROMs such as BASIC or COMAL, the OLD command can be used to recover the program in memory.

Special VDU Features

'Sideways ROM' Support

The BBC Micro allows multiple ROMs to be banked into the 16KB space between $8000 to $BFFF. Acorn referred to such ROMs as 'sideways ROMs'. The BBC Micro architecture allows up to 16 sideways ROMs to be connected to the system and paged into this address space (although the original BBC Micro only featured four physical sockets.)

A sideways ROM may be either a 'language ROM' or a 'service ROM'. Language ROMs include a user interface, and are typically programming languages. However, note that ROM-based applications such as Acorn's View word processor are also considered to be language ROMs. Service ROMs do not contain a user interface but instead provide additional star commands to the operating system. Utility and filing system ROMs fall into this category.

When a star command is entered, the operating system first offers it to the currently active language ROM. If the current language does not support the command, the system pages in the ROMs one at a time, starting from the highest slot, offering the command to each of them. If none of the ROMs services the command an error is displayed.

Sideways ROMs are used in a number of ways:

Applecorn emulates sideways ROMs by simply loading the ROM images from disk into the $8000 to $BFFF space. Unlike switching physical ROMs in the BBC Micro, this is not instantaneous. While it is possible to configure up to 16 ROMs, fewer are recommended in order to keep response times down. This mechanism allows multi-ROM languages such as ISO-Pascal to be supported.

The *HELP command displays information about each of the available sideways ROMs. (Note that the BCPL ROM does not print any *HELP info. This is a bug in the ROM, not Applecorn!)

HostFS

Applecorn's HostFS uses the ProDOS MLI to service all Acorn MOS filesystem calls. This means that Applecorn works directly with ProDOS volumes, such as floppy disks, mass storage devices and even network connected drives using ADT's VEDrive.

HostFS Pathnames

Pathnames used within Applecorn are regular ProDOS paths. The directory separator is forward slash / and every ProDOS filesystem has a volume name which is used to access the top level ('volume') directory. For example, a volume with the name 'TEST' would be mounted under ProDOS as /TEST.

Applecorn adds a few extra features to ProDOS paths, as follows:

HostFS Wildcards

Applecorn HostFS provides support for wildcards. The following wildcard characters are used:

The HostFS tries to follow the same conventions as Acorn's ADFS, which allows wildcards to be used in some cases to abbreviate long pathnames and in others to specify a list of files to operate on at once.

Like ADFS, HostFS commands accept several different types of file argument. Following Acorn's convention, these may be described as follows:

Wildcards are expanded wherever they appear in the path with one exception. For non-leaf nodes, the first match will be always be substituted. In the case of <*objspec*> the first match for the leaf node will always be used. However for <listspec> all matches of the leaf node will be operated upon.

Examples: :71/T*DIR/TEST??.TXT, *A*/FILES/C*/TEST*.TXT

Wildcards may also be used for OSFILE and OSFIND system calls, so BASIC command like LOAD"", CHAIN"", OPENIN"" and OPENUP"" can use wildcards to specify the file to open.

The attentive reader will have noticed that I mention an exception to wildcard matching. Volume directory names are not currently subject to wildcard search. Either type them in full, or use the colon notation to specify physical drive as an abbreviation.

Star Commands

Applecorn implements the command line interface for MOS built-in functions and also for accessing the filingsystem. Following Acorn conventions, these commands are all invoked with a leading asterix *.

Applecorn Commands

These commands are specific to Applecorn.

*QUIT - Terminate Applecorn and quit to ProDOS. Because the 'BBC Micro' lives in auxiliary memory, you can usually restart Applecorn by running it again and (assuming you are using BBC BASIC) recover your program with OLD.

*FAST - turn Apple II accelerator on (supports GS, Ultrawarp and ZipChip).

*SLOW - turn Apple II accelerator off (supports GS, Ultrawarp and ZipChip).

MOS Commands

The following commands correspond to those in the original Acorn MOS.

*HELP [topic] - Prints out information in a manner similar to the same command on the BBC micro.

*BASIC - Start the BASIC language (assuming it is present as one of the virtual Sideways ROMs.) BASIC is the only language ROM which does not provide its own startup command, so MOS provides it.

*ECHO This is some text - echo a line of text to the screen.

*FX a[,x,y] - Invokes OSBYTE MOS calls.

*OPT - Sets file system options. *OPT 255,x may be used to enable or disable debugging output.

*KEY n <text> - Programs a user-defined function key. n can take values from 0 to 15. Values of 0 through 9 refer to the function keys (Open Apple plus number key 0 through 9), while higher numbers may be used to program the cursor keys and Copy key. For example *KEY 1 LIST|M (note the special syntax to insert a control character.)

*CODE x,y - Invokes user machine code through the user vector USERV. 6502 registers X and Y are initialized according to the value of the two numeric arguments provided.

*LINE <text> - Invokes user machine code through the user vector USERV. 6502 registers X and Y are initialized to point at the command-line text provided (X is least significant byte, Y most significant byte.)

*TV n - This command does nothing. It is provided for compatibility because it is common for BBC Micro programs to use the *TV command to adjust the vertical position of the picture on the monitor.

*ROM - On the real BBC Micro, this selects the ROM Filing System. This command is accepted by Applecorn but does nothing.

*TAPE - On the real BBC Micro, this selects the Cassette Filing System. This command is accepted by Applecorn but does nothing.

Filing System & HostFS Commands

This set of commands relates to filesystem operations, which would correspond to the Disk Filing System (DFS) or Advanced Disk Filing System (ADFS) of the BBC Micro.

*CAT [<*objspec*>] (or *. [*objspec*]) - Simple listing of the files in the specified directory, or the current working directory ('current prefix') if no directory argument is given.

*EX [<*objspec*>] - Detailed listing of files in the current directory showing the load address, length and permissions. *EX expects a directory as an argument, for example *EX :71/MYDIR.

*INFO [<listspec>] - Displays the same detailed file listing as *EX, but accepting a <listspec>. *INFO expects a list of objects as an argument, so *INFO :71/MYDIR/* would display the same info as the *EX example above.

*DIR <*objspec*> - Allows the current directory to be changed to any ProDOS path. *CD and *CHDIR are synonyms for *DIR. The argument is expected to be a directory.

*LOAD <*objspec*> SSSS - Load file <*objspec*> into memory at hex address SSSS. If the address SSSS is omitted then the file is loaded to the address stored in its aux filetype.

*SAVE <objspec> SSSS EEEE - Save memory from hex address SSSS to hex address EEEE into file <objspec>. The start address SSSS is recorded in the aux filetype. (Note, no wildcards when saving.)

*RUN <*objspec*> - Load file filepath into memory at the address stored in its aux filetype and jump to to it. This is used for loading and starting machine code programs. You can also simply do *<*objspec*> or */<*objspec*> (the latter form is useful if the name is the same as that of a ROM routine.)

*DELETE <objspec> - Delete file <objspec> from disk. This command can also delete directories, provided they are empty. No wildcards are allowed.

*REMOVE <objspec> - Delete file <objspec> from disk. This command is identical to *DELETE except that no error message is shown if the file to be deleted does not exist.

*DESTROY <listspec> - Deletes multiple files as specified by <listspec>. For example, *DESTROY PROJECT/*.ASM.

*RENAME <objspec1> <objspec2> - Rename file or directory <objspec1> to <objspec2>. No wildcards are allowed.

*TITLE [<drv>] <title> - Change the disk title (or volumename in ProDOS terms.) If no drive is specified, the name of the volume in the drive corresponding to the current ProDOS prefix is changed.

*DRIVE <drv> - Switch to the specified physical drive. This is equivalent to using *DIR but does not allow subdirectories to be specified. The working directory will be set to the volume directory corresponding to the physical device specified.

*FREE [<drv>] - Shows blocks used and blocks free on the specified physical device. If no drive is specified, the free space on the drive corresponding to the current ProDOS prefix is returned.

*CDIR <objspec> - create directory dirname. *MKDIR is a synonym.

*ACCESS <listspec> attribs - change file permisions. The string attribs can contain any of the following:

*COPY <listspec> <*objspec*> - Copy file(s). There are two forms of the *COPY command:

*TYPE <*objspec*> - type a text file to the screen.

*DUMP <*objspec*> - print a hexdump of a file to the screen.

*BUILD <objspec> - provides a quick and dirty way to create small text files, one line at a time. This is quite useful for making small files for use with *EXEC. Press Escape to finish editing and close the file.

*SPOOL <objspec> - copies all screen output to the filename given. Issuing the *SPOOL command with no filename stops spooling and closes any open spool file.

*EXEC <*objspec*> - reads input from the filename given, and executes it, as though it were being typed on the keyboard.

*CLOSE - close any open files (including spool file, if open.)

Audio Support

Applecorn includes an audio engine which emulates that of the Acorn MOS. The audio facilities may be used from BBC BASIC using the SOUND and ENVELOPE commands. There are equivalents in other Acornsoft languaes, should you wish to conduct your sonatas in Forth or Pascal.

Applecorn supports two different audio systems:

If Applecorn is run on a IIgs, Ensoniq audio will be enabled, otherwise a Mockingboard will be assumed to be present in slot 4 (or you will have no audio.)

Applecorn implements to core features of the BBC Micro audio engine:

The BBC Micro utilizes the Hitachi SN76489 sound generator chip. The Ensoniq DOC is a more powerful sound chip, based on a wavetable, which allows it to emulate teh SN76489 quite closely. The Mockingboard uses the popular General Instruments AY-3-8910 chip, which has slightly different capabilities, especially with regard to the noise channel. As a result, the Ensoniq emulation is closer to the BBC Micro original than the Mockingboard.

Up to 16 envelopes are supported without having to worry about any memory conflicts with other functions.

The audio code relies on an interrupt service routine being invoked 100 times a second. This ISR also updates the pseudo-realtime clock which may be read (and set) in BASIC using the TIME variable. For a //e with no Mockingboard there are is no source of interrupts, so TIME remains at zero.

How to Build

Applecorn is built natively on the Apple //e using the Merlin 16 assembler v3.53 (requires a 65816 though.) It may also be built using Merlin-32 on Windows, Linux or Mac if preferred. The code should also assemble on Merlin-8 2.58 provided some of the longer comments are trimmed (Merlin-16 allows longer lines.)

To build in Merlin-32, use the m32build provided.

In Merlin-16 (Merlin-8 in parenthesis, where different):

Theory of Operation

BBC Micro

On the BBC Micro, language ROMs have a very clean interface to the Machine Operating System (MOS). Syscalls are used for all accesses to the hardware, rather than poking at memory mapped addresses directly, as is common in other 6502 systems. This was done partly to enable programs to run on a second processor connected to the main BBC Micro over an interprocessor interface called The Tube.

On the BBC Micro, the 64K address space looks like this:

                 +----------------------+ $ffff
                 |                      |
                 | MOS ROM (16KB)       |
                 |                      |
                 +----------------------+ $c000
                 |                      |
                 | Language ROM (16KB)  |
                 |                      |
                 +----------------------+ $8000
                 |                      |
                 | User RAM (32K)       |
                 |                      |
                 |                      |
                 |                      |
                 |                      |
                 |//////////////////////|
                 +----------------------+ $0000

The hatched area at the bottom represents reserved space such as zero page, the stack and pages two through seven which are used by the language ROM for various purposes.

Display memory on the BBC Micro is allocated at the top of user RAM, from $8000 down. Higher resolution modes use more memory, reducing the user RAM available for BASIC programs.

The BBC Micro uses a unique paging mechanism referred to as 'Sideways ROM', which allows up to 16 language and filing system ROMs to be banked into the 16KB space from $8000 to $bfff.

Apple //e

The Apple //e, with 128KB has two 64KB banks of memory. The main memory bank is as follows:

   +----------------------+ $ffff +----------------------+
   | BASIC/Monitor ROM    |       | Language Card        |
   |                      |       |                      | +-4K Bank Two----+
   |###I/O Space (4KB)####|       +----------------------+ +----------------+
   +----------------------+ $c000
   |                      |
   |                      |
   |                      |
   |                      |
   |                      |
   | User RAM (48K)       |
   |                      |
   |                      |
   |                      |
   |                      |
   |//////////////////////|
   +----------------------+ $0000

Here there is 48KB of RAM rather than 32KB, and the total ROM is just 12KB, starting at $d000. The 4KB from $c000 to $cfff is all memory mapped I/O.

An additional 16KB of memory is available, which is referred to as the Language Card (LC henceforth.) This memory can be banked into the space where the ROM usually resides from $d000 up. Note that this address space is only 12KB so the 16KB LC memory is itself banked with the bottom 4KB of LC space having two banks.

When an Extended 80 Column card is installed in the aux slot of the Apple //e, an additional 64KB of RAM is added, for a total of 128KB. The entire arrangement described above is duplicated, so there is 64KB of main memory (divided between the 'lower 48K' and 16KB of LC memory) and 64KB of auxiliary memory (divided in exactly the same manner.)

The Apple //e has softswitches to select whether to address the main or aux bank for the main portion of RAM (ie: the 48KB up to $bfff). Reading and writing may be switched separately so it is possible to execute code and read data from one bank while writing to the other. A separate softswitch controls whether zero page, the stack, and LC memory addresses will be passed to main or aux banks.

The ProDOS operating system resides primarily in the main bank Language Card memory, so this memory is not available to Applecorn if we wish to retain the facilties provided by ProDOS.

The Apple //e screen normally resides from $400 to $7ff in main memory (for 40 column mode) or at $400 to $7ff in both main and aux memory (for 80 column mode.) There is a softswitch to switch to text page two from $800 to $bff.

Applecorn Architecture

MAIN BANK:

   +----------------------+ $ffff +----------------------+
   | BASIC/Monitor ROM    |       | Language Card        |
   |                      |       | ProDOS               | +-4K Bank Two----+
   |###I/O Space (4KB)####|       +----------------------+ +-Unused---------+
   +----------------------+ $c000
   |                      |
   |                      |
   |                      |
   |                      |
   | Applecorn loader &   |
   | Applecorn code to    |
   | interface with       |
   | ProDOS               |
   |                      |
   |                      |
   |//////////////////////|
   +----------------------+ $0000

AUX BANK:

   +----------------------+ $ffff 
   | Language Card        |
   | Applecorn MOS        |       +-4K Bank Two----+
   |###I/O Space (4KB)####|       +-Unused---------+
   +----------------------+ $c000
   |                      |
   | Acorn Language ROM   |
   |                      |
   +----------------------+
   |                      |
   | Acorn language       |
   | user code/data       |
   | space                |
   |                      |
   |                      |
   |//////////////////////|
   +----------------------+ $0000

Limitations

Applecorn MOS is relatively complete. Most limitations reflect the different hardware capabilities of the BBC Micro and the Apple II.