Museum of Electronic Games & Art MEGA65-specific information for cc65

Stefan A. Haubenthal


An overview over the MEGA65 run-time system as it's implemented for the cc65 C compiler.

1. Overview

2. Binary format

3. Memory layout

4. Linker configurations

5. Platform-specific header files

6. Loadable drivers

7. Limitations

8. Other hints

9. License


1. Overview

The MEGA65 is a modern 8-bit computer that is based on the never-released Commodore 65. It uses the 45GS02 CPU and has a VIC-IV graphics controller with 256 colors and an 80-column text mode. It has two CIA (6526) I/O chips and SID sound chips. It supports file storage on Secure Digital cards, and allows two joysticks and a mouse to be connected.

This file contains an overview of the MEGA65 run-time system as it comes with the cc65 C compiler. It describes the memory layout, MEGA65-specific header files, available drivers, and any pitfalls specific to that platform.

Please note that the functions mentioned here are described in detail in the separate function reference. Even functions marked as "platform dependent" may be available on more than one platform. Please see the function reference for more information.

2. Binary format

The standard binary output format generated by the linker for the MEGA65 target is a machine language program that's prepended with a 16-bit load address and a one-line BASIC stub which selects bank 0 and calls the machine language part via SYS. That means that a program can be loaded as a BASIC program, and started with RUN. It is, of course, possible to change that behaviour by using a modified program-header file and linker config.

3. Memory layout

cc65-generated programs with the default setup use the memory configuration that is active when the program is started. The usable memory range is $2001 - $7FFF. The area above $7FFF is not used by the default configuration, because it may contain ROM, I/O, or memory that is reserved by the MEGA65 firmware.

Special locations:

Stack

The C run-time stack is located at $8000, and grows downward.

Heap

The C heap is located at the end of the program, and grows toward the C run-time stack.

Zero page

The default configuration uses the zero page locations $18 - $31. The rest of page zero is reserved for BASIC and the Kernal.

4. Linker configurations

The ld65 linker comes with a default config. file for the MEGA65, which is used via -t mega65. The MEGA65 package comes with additional secondary linker config. files which are used via -t mega65 -C <configfile>.

The default configuration uses 26 bytes in the zero page, at $18 - $31. (The rest of page zero is reserved for BASIC and the Kernal.) The assembly configuration makes the zero page from $0002 to $00FF available to the program.

4.1 Default config. file (mega65.cfg)

The default configuration is tailored to C programs. It supplies the load address and a small BASIC stub that starts the compiled program using a SYS command.

4.2 mega65-asm.cfg

This configuration is made for Assembly programmers who don't need a special setup. The default start address is $2001. It can be changed with the linker command-line option --start-addr or -S. All standard segments, with the exception of ZEROPAGE, are written to the output file; and, a two-byte load address is prepended.

To use that config. file, assemble with -t mega65, and link with -C mega65-asm.cfg. The former will make sure that the correct character translations are in effect, while the latter supplies the actual config. When using cl65, use both command-line options.

Sample command line for cl65:

cl65 -o file.prg -t mega65 -C mega65-asm.cfg source.s

To generate code that loads to $4000:

cl65 -o file.prg -Wl -S,$4000 -t mega65 -C mega65-asm.cfg source.s

It also is possible to add a small BASIC header to the program, that uses SYS to jump to the program entry point (which is the start of the code segment). The advantage is that the program can be started using RUN.

To generate a program with a BASIC SYS header, use

cl65 -o file.prg -u __EXEHDR__ -t mega65 -C mega65-asm.cfg source.s

Please note that, in this case, a changed start address doesn't make sense, because the program must be loaded to BASIC's start address.

5. Platform-specific header files

Programs containing MEGA65-specific code may use the mega65.h or cbm.h header files. Using the latter may be an option when writing code for more than one CBM-like platform, because it includes mega65.h, and declares several functions common to all CBM-like platforms.

5.1 MEGA65-specific functions

There are no functions that are specific to the MEGA65. See the function reference for the functions that are available on all CBM-like platforms.

cpeekcolor() works differently on the MEGA65 than it does on other platforms. Each character has two colors: background and text (foreground). cpeekcolor() returns both colors. The high nybble describes the background color, the low nybble describes the text color. For example, if the function is used on the default screen, then it returns $61, which means white-on-blue.

5.2 CBM-specific functions

Some functions are available for all (or, at least most) of the Commodore-like machines. See the function reference for declarations and usage.

5.3 Hardware access

The following pseudo variables declared in the mega65.h header file do allow access to hardware located in the address space. Some variables are structures, accessing the struct fields will access the chip registers.

VIC

The VIC structure allows access to the VIC-IV (the graphics controller). The structure is currently identical to the VIC-II register layout; see the _vic2.h header file located in the include directory for the declaration of the structure.

SID1, SID2

Access to the two SID (sound interface device) chips is available via the SID1 and SID2 variables. The structure behind these variables is explained in _sid.h.

CIA1, CIA2

Access to the two CIA (complex interface adapter) chips is available via the CIA1 and CIA2 variables. The structure behind these variables is explained in _6526.h.

COLOR_RAM

A pointer to the color RAM at $D800.

6. Loadable drivers

The names in the parentheses denote the symbols to be used for static linking of the drivers.

6.1 Graphics drivers

No graphics drivers are currently available for the MEGA65.

6.2 Extended memory drivers

No extended memory drivers are currently available for the MEGA65.

6.3 Joystick drivers

The default drivers, joy_stddrv (joy_static_stddrv), point to m65-stdjoy.joy (mega65_stdjoy_joy).

m65-stdjoy.joy (mega65_stdjoy_joy)

Supports up to two standard joysticks connected to the joystick ports of the MEGA65.

6.4 Mouse drivers

No mouse drivers are currently available for the MEGA65.

6.5 RS232 device drivers

No serial drivers are currently available for the MEGA65.

7. Limitations

The MEGA65's firmware and memory configuration can change at any time. Some changes could make old programs fail to work.

8. Other hints

8.1 RUN/STOP and ESC keys

The RUN/STOP key stops a running program. Holding Shift while pressing it runs the first program on disk.

The ESC key is used to start escape sequences. Press and release it, and then press the key that selects the desired function. See the MEGA65 user guide for the available escape sequences.

8.2 Passing arguments to the program

Command-line arguments can be passed to main(). Because that is not supported directly by BASIC, the following syntax was chosen:

    RUN:REM ARG1 " ARG2 IS QUOTED" ARG3 "" ARG5

  1. Arguments are separated by spaces.
  2. Arguments may be quoted.
  3. Leading and trailing spaces around an argument are ignored. Spaces within a quoted argument are allowed.
  4. The first argument passed to main() is the program name.
  5. A maximum number of 10 arguments (including the program name) are supported.

8.3 Program return code

The program return code (low byte) is passed back to BASIC by the use of its ST variable.

8.4 Interrupts

The run-time for the MEGA65 uses routines marked as .INTERRUPTOR for interrupt handlers. Such routines must be written as simple machine language subroutines, and will be called automatically by the interrupt handler code if they are linked into a program. See the discussion of the .CONDES feature in the assembler manual.

9. License

This software is provided "as-is", without any expressed or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.

Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated, but is not required.
  2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.