Applesoft Internals: Floating Point Math Package (1 of 2)

Applesoft's floating point package provides a 9 digit scientific package for evaluating numeric equations. It supports trigonometric functions with arguments in radians, exponents and logarithms to base e.
ABBREVIATIONS

msb: most significant bit or byte
lsb: least significant bit or byte
eol: end of line token ($00)

A: the 6502 accumulator
X: the 6502 X register
Y: the 6502 Y register
Z: the zero flag of the 6502 status register
C: the carry flag of the 6502 status register

A,X is a 16 bit number where A has the msb and X the lsb.

(Y,A) is the number or string whose address is in Y and A, with the msb in Y and the lsb in A.

FAC: the floating point accumulator
ARG: the ARGument register

REAL NUMBER FORMAT
The real number format used throughout Applesoft is: the exponent is a single byte signed number (EXP) in excess of $80 form (the signed value has $80 added to it); the mantissa is 4 bytes (HO,MOH,MO,LO); the binary point is to the right of the most significant bit. Since in binary floating point notation the msb is always 1, the number's sign replaces the msb when the number is stored in memory in packed form. The sign, though, is kept in a separate byte (SGN) when only bit 7 is significant. If the exponent is zero, the number is zero, although the mantissa isn't necessarily zero.

Examples:

EXP HO MOH MO LO SGN

Packed format
-10 84 A0 00 00 00
10 84 20 00 00 00

FAC format
-10 84 A0 00 00 00 FF
10 84 A0 00 00 00 00

Arithmetic routine calling conventions for single argument functions (e.g., SGN, ABS or INT):

On entry the argument is in the FAC.
On exit the result is in the FAC.

Arithmetic routine calling conventions for two argument functions (e.g., FADD and FSUB):

On entry the first argument is in the ARG.
On entry the second argument is in the FAC.
On exit the result is in the FAC.


FLOATING POINT REGISTERS
NOTE: The TEMP locations may be used for other things when not used by the floating point math package.
.
FAC
ARG
TEMP1
TEMP2
TEMP3
RND
EXP
9D
A5
93
98
8A
C9
HO
9E
A6
94
99
8B
CA
MOH
9F
A7
95
9A
8C
CB
MO
A0
A8
96
9B
8D
CC
LO
A1
A9
97
9C
8E
CD
SGN
A2
AA
(packed format)

FLOATING POINT CONSTANTS
The following addresses point to useful numbers; they're packed and suitable for use by most of the arithimetic routines including CONUPK and MOVMF.
RND
-32768
1
SQR (1/2)
SQR (2)
-1/2
LN (2)
10
1000000000
1/2
LOG (2) base e
pi/2
pi*2
1/4 
00C9
E0FE
E913
E92D
E932
E937
E93C
EA50
ED14
EE64
EEDB
F063
F06B
F070

FLOATING POINT REGISTER MOVE ROUTINES

MOVFM EAF9
Moves the number in memory Y,A points to into FAC. On exit A and Z reflect FACEXP.

MOV2F EB1E
Pack FAC and temporarily moves it into register 2. On exit A and Z reflect FACEXP.

MOV1F EB21
Pack FAC and moves it temporarily into register 1. On exit A and Z reflect FACEXP.

MOVML EB23
Pack FAC and move it into zero page area starting at X. On exit A and Z reflect FACEXP.

MOVMF EB2B
Pack FAC and move it into memory pointed to by Y,X. On exit A and Z reflect FACEXP.

MOVFA EB53
Move ARG into FAC. On exit A and Z reflect FACEXP.

MOVAF EB63
Move FAC into ARG. On exit A and Z reflect FACEXP.

CONUPK E9E3
Load ARG from memory pointed to by Y,A. On exit A and Z reflect FACEXP.

SUMMARY OF MOVES:

FAC => (Y,A) EB2B MOVMF
FAC => (0,X) EB23 MOVML
FAC => TEMP1 EB21 MOV1F
FAC => TEMP2 EB1E MOV2F
FAC => ARG EB63 MOVAF

ARG => FAC EB53 MOVFA

(Y,A) => FAC EAF9 MOVFM
(Y,A) => ARG E9E3 CONUPK


FLOATING POINT OPERATORS
The following routines require that A and Z reflect FACEXP. Even though FAC move routines set up A and Z, a LDA $9D will insure their proper values.

FMULT E97F
Multiply the FAC by the number in memory pointed to by Y,A.

FMULTT E982
Multipy FAC and ARG. On entry A and Z must reflect FACEXP.

FDIV EA66
Divide the number in memory pointed to by Y,A by FAC.

FDIVT EA69
Divide ARG by FAC. On entry A and Z must reflect FACEXP.

FADD E7BE
Add the number Y,A points to in memory to FAC.

FADDT E7C1
Add FAC and ARG. On entry A and Z must reflect FACEXP.

FSUB E7A7
Subtract FAC from the number Y,A points to in memory.

FSUBT E7AA
Subtract FAC from ARG. On entry A and Z must reflect FACEXP.
Published Date: Feb 18, 2012