# Commodore 610-specific information for cc65

## Ullrich von Bassewitz,
Greg King

An overview over the Commodore 610 runtime system as it is implemented for the cc65 C compiler.

## 1. Overview

## 2. Binary format

## 3. Memory layout

## 4. Platform-specific header files

- 4.1 CBM 610-specific functions
- 4.2 CBM-specific functions
- 4.3 Hardware access

## 5. Loadable drivers

- 5.1 Graphics drivers
- 5.2 Extended memory drivers
- 5.3 Joystick drivers
- 5.4 Mouse drivers
- 5.5 RS232 device drivers

## 6. Limitations

- 6.1 Realtime clock
- 6.2 Kernal and hardware access
- 6.3 Interrupts

## 7. Other hints

- 7.1 Passing arguments to the program
- 7.2 Program return code
- 7.3 Interrupt handlers

## 8. License

## 1. Overview

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

Please note that CBM 610-specific functions are just mentioned here, they 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.

In addition to the Commodore 610 (named B40 in the U.S.), several other machines are supported by this cc65 target, since they have identical hardware: The Commodore 620 and 630 (more memory, additional coprocessor card), and the Commodore 710, 720, and 730 (same hardware in another case with a built-in monitor).

## 2. Binary format

The standard binary output format generated by the linker for the Commodore 610 target is a machine language program with a one-line BASIC stub, which transfers control to the machine language running in bank 1. 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 startup file and linker config.

## 3. Memory layout

cc65 generated programs for the Commodore 610 run in bank 1, the memory bank reserved for BASIC programs. Since there are no ROMs in this memory bank, kernal subroutines are either emulated or called by bank switching, which has the disadvantage of being slow compared to a direct call.

The default memory configuration for the CBM 610 allocates all memory between $0002 and $FFF0 in bank 1 for the compiled program. Some space in low memory is lost, because a separate hardware stack is set up in page 1, and the kernal replacement functions need some more memory locations. A few more bytes are lost in high memory, because the runtime sets up a CBM-compatible jump table at $FF81. The main startup code is located at $0400, so about 63K of the complete bank are actually usable for applications.

Special locations:

Stack

  The C runtime stack is located at $FF81, and grows downwards.

Heap

  The C heap is located at the end of the program, and grows towards the C runtime stack.

## 4. Platform-specific header files

Programs containing CBM 610-specific code may use the `cbm610.h` or `cbm.h` header files. Using the later may be an option when writing code for more than one CBM platform, since it includes `cbm610.h`, and declares several functions common to all CBM platforms.

## 4.1 CBM 610-specific functions

The functions listed below are special for the CBM 610. See the function reference for declaration and usage.

- peekbsys
- peekwsys
- pokebsys
- pokewsys

## 4.2 CBM-specific functions

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

- cbm_close
- cbm_closedir
- cbm_k_setlfs
- cbm_k_setnam
- cbm_k_load
- cbm_k_save
- cbm_k_open
- cbm_k_close
- cbm_k_readst
- cbm_k_chkin
- cbm_k_ckout
- cbm_k_basin
- cbm_k_bsout
- cbm_k_clrch
- cbm_k_tksa
- cbm_k_second
- cbm_load
- cbm_open
- cbm_opendir
- cbm_read
- cbm_readdir
- cbm_save
- cbm_write
- get_tv

## 4.3 Hardware access

The following pseudo variables declared in the `cbm610.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.

Note: All I/O chips are located in the system bank (bank 15); and can therefore not be accessed like on other platforms. Please use one of the `peekbsys`, `peekwsys`, `pokebsys`, and `pokewsys` functions to access the I/O chips. Direct reads and writes to the structures named below will not work!

`CRTC`

  The `CRTC` structure allows access to the CRTC (the video controller). See the `_6545.h` header file located in the include directory for the declaration of the structure.

`SID`

  The `SID` structure allows access to the SID (the sound interface device). See the `_sid.h` header file located in the include directory for the declaration of the structure.

`ACIA`

  Access to the ACIA (the RS232 chip) is available via the `ACIA` variable. See the `_6551.h` header file located in the include directory for the declaration of the structure.

`CIA`

  Access to the CIA chip is available via the `CIA` variable. See the `_6526.h` header file located in the include directory for the declaration of the structure.

`TPI1, TPI2`

  The two 6525 triport chips may be accessed by using these variables. See the `_6525.h` header file located in the include directory for the declaration of the structure.

## 5. Loadable drivers

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

## 5.1 Graphics drivers

No graphics drivers are currently available for the Commodore 610 (and since the machine has no graphics capabilities, chances for a graphics driver aren't really good :-).

## 5.2 Extended memory drivers

`cbm610-ram.emd (cbm610_ram_emd)`

  A driver for the RAM in bank 2. Supports up to 255 pages with 256 bytes each.

## 5.3 Joystick drivers

The Commodore 610 is a business machine, and doesn't have joystick ports. There are no drivers for the non-existing ports available.

## 5.4 Mouse drivers

No mouse drivers are currently available for the Commodore 610.

## 5.5 RS232 device drivers

`cbm610-std.ser (cbm610_std_ser)`

  Driver for the 6551 ACIA chip built into the Commodore 610. Supports up to 19200 BPS, hardware flow control (RTS/CTS), and interrupt-driven receives. Note that, because of the peculiarities of the 6551 chip, transmits are not interrupt driven; and, the transceiver blocks if the receiver asserts flow control because of a full buffer.

## 6. Limitations

## 6.1 Realtime clock

The realtime clock functions use the CIA1 TOD clock. As that clock only stores the time but not the date, the date set by `clock_settime()` is simply stored inside the C library for retrieval in the same program via `clock_gettime()`.

## 6.2 Kernal and hardware access

Since the program runs in bank 1, and the kernal and all I/O chips are located in bank 15, calling ROM routines or accessing hardware needs special code. The cc65 runtime implements wrappers for all functions in the kernal jump table. While this simplifies things, it should be noted that the wrappers do have quite an impact on performance: A cross-bank call has an extra 300µs penalty added by the wrapper.

## 6.3 Interrupts

Compiled programs contain an interrupt handler that runs in the program bank. This has several advantages, one of them being performance (see cross-bank call overhead mentioned above). However, this introduces one problem: Interrupts are lost while the CPU executes code in the kernal bank. As a result, the clock may go wrong; and (worse), serial interrupts may get lost.

Since the cc65 runtime does only call the kernal for disk I/O, this means that a program should not do file I/O while it depends on interrupts.

## 7. Other hints

## 7.1 Passing arguments to the program

Command-line arguments can be passed to `main()`. Since 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.

## 7.2 Program return code

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

## 7.3 Interrupt handlers

The runtime for the Commodore 610 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 when they are linked into a program. See the discussion of the `.CONDES` feature in the assembler manual.

## 8. 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.