# sim65 Users Guide

## Stefan A. Haubenthal,
Brad Smith

sim65 is a simulator for 6502 and 65C02 CPUs. It allows to test target independent code.

## 1. Overview

## 2. Usage

- 2.1 Command line options in detail

## 3. Input and output

## 4. Creating a Test in C

## 5. Creating a Test in Assembly

## 6. Copyright

## 1. Overview

sim65 is used as part of the toolchain to test 6502 or 65C02 code. The binary to test should be compiled with `--target sim6502` or `--target sim65c02`.

## 2. Usage

The simulator is called as follows:

> `

>             Usage: sim65 [options] file [arguments]
>             Short options:
>               -h                    Help (this text)
>               -c                    Print amount of executed CPU cycles
>               -v                    Increase verbosity
>               -V                    Print the simulator version number
>               -x <num>              Exit simulator after <num> cycles

>             Long options:
>               --help                Help (this text)
>               --cycles              Print amount of executed CPU cycles
>               --verbose             Increase verbosity
>               --version             Print the simulator version number

> `

## 2.1 Command line options in detail

Here is a description of all the command line options:

`-h, --help`

  Print the short option summary shown above.

`-c, --cycles`

  Print the number of executed CPU cycles when the program terminates. The cycles for the final "`jmp exit`" are not included in this count.

`-v, --verbose`

  Increase the simulator verbosity.

`-V, --version`

  Print the version number of the utility. When submitting a bug report, please include the operating system you're using, and the compiler version.

`-x num`

  Exit simulator after num cycles.

## 3. Input and output

The simulator will read one binary file per invocation and can log the program loading and paravirtualization calls to stderr.

Example output for the command

> `

>     sim65 --verbose --verbose samples/gunzip65

> `

> `

>     Loaded 'samples/gunzip65' at $0200-$151F
>     PVWrite ($0001, $13C9, $000F)
>     GZIP file name:PVWrite ($0001, $151F, $0001)

>     PVRead ($0000, $FFD7, $0001)
>     PVOpen ("", $0001)
>     PVRead ($0003, $1520, $6590)
>     PVClose ($0003)
>     PVWrite ($0001, $13D9, $000F)
>     Not GZIP formatPVWrite ($0001, $151F, $0001)

>     PVExit ($01)

> `

## 4. Creating a Test in C

For a C test compiled and linked with `--target sim6502` the command line arguments to `sim65` will be passed to `main`, and the return value from `main` will become sim65's exit code. The `exit` function may also be used to terminate with an exit code.

Exit codes are limited to 8 bits.

The standard C library high level file input and output is functional. A sim65 application can be written like a command line application, providing arguments to `main` and using the `stdio.h` interfaces.

Internally, file input and output is provided at a lower level by a set of built-in paravirtualization functions ( see below).

## 5. Creating a Test in Assembly

Assembly tests may similarly be assembled and linked with `--target sim6502` or `--target sim65c02`, and the sim65 library provides an `exit` symbol that the program may `JMP` to terminate with the current A register value as an exit code.

The binary file has a 12 byte header:

- 5 byte signature: `$73, $69, $6D, $36, $35` or `'sim65'`
- 1 byte version: `2`
- 1 byte CPU type: `0` = 6502, `1` = 65C02
- 1 byte sp address: the zero page address of the C parameter stack pointer `sp` used by the paravirtualization functions
- 1 word load address: where to load the data from the file into memory (default: `$0200`)
- 1 word reset address: specifies where to begin execution after loading (default: `$0200`)

Other internal details:

- The entire 64 kilobyte address space is writeable RAM. Aside from the loaded binary, the reset vector at `$FFFC` will be pre-loaded with the given reset address.
- The `exit` address is `$FFF9`. Jumping to this address will terminate execution with the A register value as an exit code.
- Several bytes immediately below the vector table are reserved for paravirtualization functions. Except for `exit`, a `JSR` to one of these addresses will return immediately after performing a special function. These use cc65 calling conventions, and are intended for use with the sim65 target C library.
- `IRQ` and `NMI` events will not be generated, though `BRK` can be used if the IRQ vector at `$FFFE` is manually prepared by the test code.

## 6. Copyright

sim65 (and all cc65 binutils) are (C) Copyright 1998-2000 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 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.