Sunday, August 5, 2012
6502 Floating Point Routines
In the August 1976 issue of Dr. Dobb's Journal Steve Wozniak and Roy Rankin published a listing of floating point math routines for the 6502. The code is portable and provides routines for floating point add, subtract, multiply and divide as well as natural and common log and exponential functions and conversion between fixed and floating point. A version of this code was later included in the firmware for the Apple II.
The article in PDF and text format is available from 6502.org so I thought it would be fun to get it running on the Replica 1. I took the code and made a few small changes to get it to assemble under the CC65 assembler. I also included some later fixes that were published in Dr. Dobb's.
I then wrote an interactive program that allows the different functions to be executed. I did some minimal testing and all the routines seemed to be working quite well.
Here is a sample run of the program (input by the user is in bold):
FLOATING POINT DEMONSTRATION PROGRAM
F - FIXED TO FLOATING POINT
P - FLOATING TO FIXED POINT
L - NATURAL LOG
N - COMMON LOG
E - EXPONENTIAL
A - FLOATING POINT ADD
S - FLOATING POINT SUBTRACT
M - FLOATING POINT MULTIPLY
D - FLOATING POINT DIVIDE
? - THIS HELP SCREEN
X - EXIT
SELECT A FUNCTION: F
FIXED TO FLOATING POINT
ENTER 16-BIT HEX NUMBER: 0123
FLOATING POINT IS: 88 48C000
SELECT A FUNCTION: P
FLOATING TO FIXED POINT
ENTER EXPONENT AND MANTISSA: 88 48C000
FIXED POINT IS: 0123
SELECT A FUNCTION: F
FIXED TO FLOATING POINT
ENTER 16-BIT HEX NUMBER: 0456
FLOATING POINT IS: 8A 456000
SELECT A FUNCTION: M
FLOATING POINT MULTIPLY
ENTER EXPONENT AND MANTISSA: 88 48C000
ENTER EXPONENT AND MANTISSA: 8A 456000
RESULT IS: 92 4EDC20
SELECT A FUNCTION: P
FLOATING TO FIXED POINT
ENTER EXPONENT AND MANTISSA: 92 4EDC20
ERROR OCCURRED AT ADDRESS $1FE6
SELECT A FUNCTION: A
FLOATING POINT ADD
ENTER EXPONENT AND MANTISSA: 88 48C000
ENTER EXPONENT AND MANTISSA: 8A 456000
RESULT IS: 8A 579000
SELECT A FUNCTION: P
FLOATING TO FIXED POINT
ENTER EXPONENT AND MANTISSA: 8A 579000
FIXED POINT IS: 0579
SELECT A FUNCTION: X
This code could be used to write assembly language programs that need to do floating point math. One piece is missing though -- conversion between the floating point representation and one that can be input by a user or printed. This is usually done using Binary Coded Decimal (BCD).
A little research found a couple of articles by Marvin L De Jong in the February and April 1981 issues of COMPUTE! magazine that described BCD to floating point and floating point to BCD conversion routines. While the floating point format he used was slightly different from what was used in Woz's code, I'm hoping that I can adapt it. So that is my next little project.
All of the code can be found here on github.
Subscribe to:
Post Comments (Atom)
5 comments:
Are you sure this is BCD? It looks like it's just the usual decimal (string) representation. BCD is normally used to mean the encoding of two digits per byte.
I'm just now getting to the floating point / BCD code. He refers to it as BCD but it does look like it is string representation.
you may have an important weblog here! would you wish to make some invite posts on my blog? casino bonus
Hi Jeff,
I came across this while looking for floating point routines to help with learning 6502 assembler. (I have been writing little programs for the Commander X16 - a modern 6502 hobby computer.) Anyway, I hope you don't mind me borrowing chunks of your code here. I'm not using it for profit or any commercial enterprise, just for fun. Many thanks for putting it here - it's very useful!
All the best.
Thanks for this information, I wil used it for my fig-forth emulation to include floating point. Please can you explane how to convert from a float to the 4 bytes format, and back. Thats is my missing link.
Cheers,
Jan
Post a Comment