Here you will find a collection of useful programs and routines (mostly written in assembly language) that can be adapted to any 6502-based computer.
-
MicroChess
MicroChess is a very famous game written by Peter Jennings in 1976. It was originally written for the MOS Technology KIM-1 but is easily ported and was used on other systems like the Rockwell AIM-65. Daryl Rictor has updated the program for use on projects with a 6551-based serial port. Both the original and the enhanced version are provided.
-
Floating Point Routines for the 6502
Originally published in the August 1976 issue of Dr. Dobb's Journal, these floating point routines allow 6502 users to perform most of the more popular and desired floating point and transcendental functions, namely: Natural Log, Common Log, Addition, Subtraction, Multiplication, Division, and conversions between floating and fixed point numbers.
-
Errata for Rankin's 6502 Floating Point Routines
In the November/December issue of Dr. Dobb's Journal Roy Rankin published three error corrections to the Floating Point Routines presented above.
-
Floating Point Implementation in the Apple II
An almost identical set of the above routines appeared in the original manual for the Apple II (the Red Book, January 1978). Documentation for these routines appeared in another book, the Wozpak II, in November 1979.
-
IEEE 754 quadruple precision floating point for 65C816
The main goal is build a "virtual numeric co-processor" with some internal pseudo-registers stack-oriented, interfaced by 'cop' instruction. About 16k binary, 10k lines of source. See announcement.
-
Calc65 floating-point arithmetic and transcendental function package
"The math routines use Cordic algorithms and similar strategies involving pseudo-multiplication and pseudo-division. The square root routine employs non-restoring pseudo-division. The BCD number format occupies 8 bytes: note that there are 12 mantissa digits and 3 exponent digits."
-
Calculation of Clock Frequency
This program calculates the clock frequency of the 6502-based system it is running on and reports it back via RS-232 through a 6551 ACIA.
-
Clearing a Section of Memory
Clear up to 256 bytes anywhere in memory using this routine.
-
Day of the Week
Computes the day of the week from (almost) any given date within the years 1900 and 2155.
-
Enhanced Day of the Week
This is an alternative to Paul Guertin's program. It is longer but has the advantage of supporting a wider range of dates.
-
Practical Memory Move Routines
Here are some reasonably fast general-purpose routines for moving blocks of memory. You simply specify the address to move from, the address to move to, and the size of the block.
-
Address Line Test
The typical RAM test consists of reading and writing $AA and $55 to each memory location. This works well for finding bad RAM locations. However, it does not specifically find bad address lines. This simple method does just that.
-
Efficient Nybble Swap
Efficient nybble swap on 6502, taking only 8 bytes and 12 clock cycles, and no variables, no stack usage, no look-up table, no X or Y usage. It uses only the accumulator and status register.
-
Large Multiply & Divide
Two routines for multiplying and dividing larger integers. The first is a 32-bit multiply routine which computes a 64-bit product and the second is a 64-bit divide routine which gives a 32-bit quotient.
-
Fast Multiply by 10
Multiplying a number by ten is fast and easy with this routine.
-
Fast, Table-Driven Multiplication
This routine uses tables of squares to quickly multiply two 8-bit numbers. The algorithm is very useful when you want to multiply multiple bytes by the same factor, and could easily be expanded for 16-bit numbers.
-
Division (32-bit)
Routines for unsigned division of a 32-bit dividend by a 16-bit divisor, resulting with a 16-bit quotient and a 16-bit remainder. Presented for the 65C02 (with changes for use on 6502) and 65C816, with notes of interest to Forth users.
-
Large Look-up Tables for 16-Bit Fixed-Point Math
Uses large look-up tables for very fast 16-bit fixed-point math, including trig and log functions. Also includes an introduction to fixed-point/scaled-integer math.
-
BCD to Binary Conversion (32-bit)
Two routines for converting between larger binary and BCD numbers. The first routine converts a 32-bit packed BCD number to its binary equivalent and the second performs the reverse operation.
-
Hexadecimal to Decimal Conversion
Routines for converting 8- and 16-bit hexadecimal numbers to their decimal (BCD) equivalents.
-
More Hexadecimal to Decimal Conversion
Alternate versions of Garth's conversion routines, slower but eliminating the lookup tables.
-
CRC Calculations
Three routines for computing 32-, 16-, and 8-bit CRC (Cyclic Redundancy Check) error detecting codes.
-
More CRC Calculations
Alternate versions of Paul's CRC calculation routines that execute in constant cycle time and eliminate the lookup tables.
-
Permutation Generator
Generating permutations is usually done with a recursive procedure, but here is a cute iterative routine that does it simply and efficiently.
-
Square Calculator
Calculates the 16-bit unsigned integer square of a signed 16-bit integer.
-
Root Calculator
Calculates the 8-bit root and 9-bit remainder of a 16-bit unsigned integer.
-
Linear Congruential Pseudo-Random Number Generator Routines
A set of routines for generating pseudo-random numbers, using a linear congruential type generator.
-
Counting Push-Button Switch Closures, With Debouncing
Counts closures on a push-button switch until a second push-button is pushed. Uses 6522 VIA Timer #1 to generate a 10-millisecond delay for the debounce routine.
-
One-Shot Timer Examples for the 6522
Two small programs demonstrate how to use Timer 1 and 2 in one-shot mode and also a short explanation of how to generate a pulse.
-
Identifying 6522 Internal Interrupt Sources
A simple program to identify which of the seven possible sources caused a 6522 interrupt.
-
Interrupt-Serviced 256-Byte Data Buffer
Demonstrates how to set up a data buffer suitable for many operations such as sending data to a printer or serial device.
-
Print Immediate (PRIMM)
By cleverly manipulating the stack this useful routine allows you to inline data to be printed with your assembly code, almost like a BASIC PRINT statement.
-
Intel HEX File Downloader
This program allows any 6502 system with a 6551-based serial port to painlessly download programs from a host computer by sending them in the Intel HEX file format.
-
X-Modem File Transfer Protocol
This implementation of the X-Modem file transfer protocol will work on any 6502-based computer with a 6551 serial port. Using X-Modem allows PC's to easily exchange files with your project using a terminal program.
Monitors, Assemblers, and Interpreters
-
2KSA [2KSA.PDF: 4.34MB]
The 2K Symbolic Assembler is a two-pass assembler for 6502 systems which assembles to less than two kilobytes. This PDF includes all 65 pages of the original documentation which includes commented source code and hex dumps for the program. It is a really nice assembler considering its size and it so well documented that making changes to it are easy.
-
FIG FORTH 6502 [FORTH65.ZIP: 32KB]
FORTH Interest Group (FIG) FORTH kernel for the 6502. It is written for the Rockwell System-65 but is easily adaptable to your own systems. Note that this has the "UM/MOD" division bug. An explanation of the bug and a correction can be found in Garth Wilson's 32-bit Division entry, which can also be found here in the Source Code Library.
-
32-bit DO LOOP & related words in 6502 ITC Forth [text source-code file]
All in 32-bit (including the increment value for 2+LOOP): 32-bit equivalents for DO, ?DO, LOOP, +LOOP, I, BOUNDS, LEAVE, ?LEAVE, UNLOOP, and the internals, plus explanation. These augment, not replace, the normal 16-bit ones, putting "2" in front of the names to tell the double-precision ones apart from the normal single-precision ones.
-
Porting Sweet 16
Sweet 16 is a metaprocessor or "pseudo microprocessor" implemented in 6502 assembly language. Originally written by Steve Wozniak and used in the Apple II, Sweet 16 can also be ported to other 6502-based systems to provide useful 16-bit functionality. This article includes the source code for Sweet 16, along with a brief history, programming instructions, and notes to help port it.
-
VTL02 (VTL-2 for the 6502)
VTL-2 is a very tiny language (the interpreter is under 1k.) Originally written in 1976 by Frank McCoy and Gary Shannon for the 8800 and 6800, a port is now available for the 6502. There's a discussion on the forum with a series of updates.
-
Supermon 816
Supermon 816 is a full-featured machine-language monitor that is adaptable to most computers that are powered by the Western Design Center's W65C816S 16-bit microprocessor if operating in native mode.
-
Mini Multitasking Kernel
This mini-kernel uses IRQ interrupts from a free running 6502 timer for task switching. It supports four concurrent tasks, each receiving fixed time slices in a round-robin method.
-
Bubble Sort (for 8-bit Elements)
A short description of the bubble sort technique followed by a subroutine that arranges the 8-bit elements of a list in ascending order.
-
Bubble Sort (for 16-bit Elements)
The sorting routine above expanded to sort a list containing 16-bit elements.
-
Combination Sort
The Combination Sort is a modified bubble sort (iterative) that takes much less time to complete.
-
Optimal Sort (for 8-bit elements)
The Optimal Sort is another improvement on the bubble sort but uses a different approach than the Combination Sort.
-
Optimal Sort (for 16-bit elements)
An expansion on the 8-bit Optimal Sort above, this version allows for sorting of large numbers (over 255) of 16-bit elements.
-
Radix 256 Counting Sort
A radix 256 counting sort to demonstrate that other sorts can be significantly faster than QuickSort. This implementation does a signed 16-bit integer sort of 1K of data.
-
Shell Sort (for 16-bit elements)
The Shell Sort is a highly-efficient sorting algorithm. This implementation can sort a large number (>32,000) of 16-bit elements. Sorting a completely unsorted array of 10,000 elements takes just over thirty seconds on a 1 Mhz machine.
-
Pattern Matcher
Matches a string against a pattern and returns with the carry bit set if they match, or clear if they don't. Supports wildcard characters "?" and "*".
-
Simple Comparisons
Compare two strings in memory stored in Pascal string format (length byte followed by string). Case-sensitive and -insensitive versions are provided.
-
Convert ASCII Number String to 32-bit Binary
This 6502 assembly language program converts a null-terminated ASCII number string into a 32-bit unsigned binary value in little-endian format. It can accept a number in binary, octal, decimal or hexadecimal format.
-
Convert 32-bit Binary to ASCII Number String
This program converts 32-bit binary back to an ASCII string. It is a companion to "Convert ASCII Number String to 32-bit Binary" above.
-
W65C816 String Manipulation Library [816STRINGLIB.ZIP: 648KB]
This W65C816S string manipulation library is a collection of assembly language subroutines that perform common character string operations, such as copying and concatenating.