# cc65 Library Overview

## Ullrich von Bassewitz

An overview over the runtime and C libraries that come with the cc65 compiler, including a discussion of the differences to the ISO standard.

## 1. Overview

## 2. ISO C compatible library

## 3. CPU-specific stuff - 6502.h

## 4. Target-specific stuff

## 5. Direct console I/O - `conio.h`

## 6. Using the joystick - `joystick.h`

## 7. Using a mouse - `mouse.h`

## 8. Copyright

## 1. Overview

This file contains a short overview of the libraries available for the cc65 C compiler. Please have a look at the function reference for a function-by-function list. Because the function reference is not complete (we're working on that), it may happen that you don't find a specific function. In that case, have a look into the header files. All functions, that are not defined by the ISO C standard, have a short comment in the headers, explaining their use.

## 2. ISO C compatible library

The C library contains a large subset of the ISO C library. Functions usually are missing in areas where there are no support on typical 6502 systems. Wide-character sets are an example for that.

I will not go into detail about the ISO functions. If a function is not mentioned here explicitly, expect it to be available, and to behave as defined in the C standard.

Functions that are not available:

- `tmpfile/tmpnam`
- `system` (cc65 alternative `exec`)
- All functions that handle floating point numbers in some manner.
- The `ldiv` function (cc65 currently is not able to return structs, by value, with a size not equal to 1, 2, or 4 bytes).
- All functions handling wide-character strings.
- Signals and all related functions (having `SIGSEGV` would be cool. :-)
- `setbuf/setvbuf`

Functions not available on all supported systems:

- `fopen/fread/fwrite/fclose/fputs/fgets/fscanf`: The functions are built on open/read/write/close. Those latter functions are not available on all systems.
- `ftell/fseek/fgetpos/fsetpos`: Support depends on the capabilities of the target machine.
- `rename/remove/rewind`: Support depends on the capabilities of the target machine.
- `time`: Many of the supported systems don't have a real-time clock, which means that the `time` function is not available. Please note that the other functions from `time.h` are available.

Functions that are limited in any way:

- `strcspn/strpbrk/strspn`: Those functions have a length limitation of 256 for the second string argument. Since that string gives a character set, and there are only 256 distinct characters, that shouldn't be a problem.
- `getenv`: Since there is no such thing as an environment on all supported systems, the `getenv` function always will return a `NULL` pointer.
- `locale`: There is no locale other than the "C" locale. The native locale is identical to the "C" locale.

In addition to those limitations, some more functions are limited if inlined versions are requested by using the `-Os` command-line option:

- The `strlen` function works for only strings with a maximum length of 255 characters.
- The `isXXX` character classification functions from `<ctype.h>` will give unpredictable results if the argument is not in character range (0..255). That limitation may be removed by #undef'ing the function name (when using `-Os`, the functions are actually macros that expand to inline assembly code, but the real functions still are available if the macro definition is removed).

## 3. CPU-specific stuff - 6502.h

The header file 6502.h contains some functions that make sense only with the 6502 CPU. Examples are macros to insert more or less useful instructions into your C code, or a function to call arbitrary machine language subroutines, passing registers in and out.

## 4. Target-specific stuff

For each supported system, there's a header file that contains calls or defines specific for that system. So, when programming for the C64, include `<c64.h>`, for the C128, include `<c128.h>`, and so on. To make the task for the Commodore systems easier, there is also a header file named `<cbm.h>` that will define stuff common for all CBM systems, and include the header file for the specific target system.

The header files contain

- Defines for special keys (such as function keys)
- Defines for special characters (such as the graphics characters)
- Variables with a fixed address in memory that may be used to access special hardware. For the C64 and C128, there is a variable struct named `SID`. Writing to the fields of that struct will write to the SID device instead. Using those variables will make your program more readable and more portable. Don't fear ineffective code when using those variables, the compiler will translate reads and writes to those structs into direct memory accesses.
- Other routines that make sense for only a specific system. One example is routines to write memory locations in the system bank for the CBM-II family.

## 5. Direct console I/O - `conio.h`

The `conio.h` header file contains a large set of functions that do screen and keyboard I/O. The functions will write directly to the screen or poll the keyboard directly with no more help from the operating system than needed. This has some disadvantages, but on the other side it's fast and reasonably portable. Conio implementations exist for the following targets:

- apple2
- apple2enh
- atari
- atari5200
- atarixl
- atmos
- c128
- c16 (works also for the c116 with up to 32K memory)
- c64
- cbm510 (40-column video)
- cbm610 (all CBM series-II computers with 80-column video)
- creativision
- cx16
- gamate
- geos-apple
- geos-cbm
- nes
- osic1p
- pce
- pet (all CBM PET systems except the 2001)
- plus4 (or expanded c16/c116)
- telestrat
- vic20

The `conio.h` header file does include the system-specific header files also, which define constants for special characters and keys.

## 6. Using the joystick - `joystick.h`

For systems that have a joystick, `joystick.h` will define a subroutine to read the current value, including constants to evaluate the result of that function.

## 7. Using a mouse - `mouse.h`

Some target machines support a mouse. Mouse support currently is available for the following targets:

- apple2
- apple2enh
- atari
- atarixl
- c128
- c64
- cbm510
- cx16

The available functions are declared in `mouse.h`.

## 8. Copyright

This C runtime library implementation for the cc65 compiler is (C) Copyright 1998-2002 Ullrich von Bassewitz. For usage of the binaries and/or sources, the following conditions do apply:

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 marked plainly 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.