Add config option for transcendental BASIC functions

Also move all transcendental functions to a separate source directory
Turn off these functions for all configurations for now
This commit is contained in:
Rikard Nordgren committed 2025-04-20 13:08:09 +02:00
1 parent 98b91aef0d
commit 9dde73a14b
35 files changed
+134

No files matched your search

-5
View File
@@ -1,5 +0,0 @@
This is an important floating point math routine, documented in many places.
Example code utilizing this routine:
- https://github.com/irmen/prog8/blob/master/compiler/res/prog8lib/c64flt.p8
-64
View File
@@ -1,64 +0,0 @@
;; #LAYOUT# STD * #TAKE
;; #LAYOUT# X16 BASIC_0 #TAKE-OFFSET 2000
;; #LAYOUT# * BASIC_0 #TAKE
;; #LAYOUT# * * #IGNORE
;
; Math package - natural logarigthm of FAC1
;
;
; See also:
; - [CM64] Computes Mapping the Commodore 64 - page 113
; - https://www.c64-wiki.com/wiki/Floating_point_arithmetic
; - https://codebase64.org/doku.php?id=base:kernal_floating_point_mathematics
; - https://www.c64-wiki.com/wiki/LOG
;
; Calculate log of FAC1
; Algorithm:
; 1. Separate exponent and significand of FAC1. N = exponent, XF = significand
; 2. T = 1.0 - sqrt(2.0) / (XF + sqrt(0.5))
; 3. Apply polynomial 0.43425594189 * T7 + 0.57658454124 * T5 + 0.96180075919 * T3 + 2.8853900731 * T - 0.5 giving log2(XF)
; 4. N + log2(XF) giving log2(X)
; 5. Multiply by LOG(2) giving LOG(X)
log_FAC1:
lda FAC1_exponent ; Store exponent for later
pha
ldx #$80 ; Set exponent to 0 so that FAC1 becomes XF
stx FAC1_exponent
ldy #>const_INV_SQR_2 ; FAC1 <- FAC1 + sqrt(0.5)
lda #<const_INV_SQR_2
jsr add_MEM_FAC1
ldy #>const_SQR_2 ; FAC1 <- sqrt(2.0) / FAC1
lda #<const_SQR_2
jsr div_MEM_FAC1
ldy #>const_ONE ; FAC1 <- 1.0 - FAC1
lda #<const_ONE
jsr sub_MEM_FAC1
ldy #>poly_log ; FAC1 <- log_poly(FAC1)
lda #<poly_log
jsr poly1_FAC1
ldy #>const_NEG_HALF ; FAC1 <- FAC1 - 0.5
lda #<const_NEG_HALF
jsr add_MEM_FAC1
jsr mov_FAC1_FAC2 ; FAC2 <- FAC1
pla ; FAC1 <- N
sec
sbc #$80
jsr convert_A_to_FAC1
jsr add_FAC2_FAC1 ; FAC1 <- FAC1 + FAC2
ldy #>const_LOG_2 ; FAC1 <- FAC1 * log(2.0)
lda #<const_LOG_2
jmp mul_MEM_FAC1
-4
View File
@@ -1,4 +0,0 @@
This is an important floating point math routine, documented in many places.
Example code utilizing this routine:
- https://github.com/irmen/prog8/blob/master/compiler/res/prog8lib/c64flt.p8
-22
View File
@@ -1,22 +0,0 @@
;; #LAYOUT# STD * #TAKE
;; #LAYOUT# X16 BASIC_0 #TAKE-OFFSET 2000
;; #LAYOUT# * BASIC_0 #TAKE
;; #LAYOUT# * * #IGNORE
;
; Math package - square root of FAC1
;
; Note:
; - transfers FAC1 to FAC2 and falls through to SQR2
;
; See also:
; - [CM64] Computes Mapping the Commodore 64 - page 117
; - https://www.c64-wiki.com/wiki/Floating_point_arithmetic
; - https://codebase64.org/doku.php?id=base:kernal_floating_point_mathematics
;
sqr_FAC1:
jsr mov_FAC1_FAC2
; FALLLTROUGH to sqr_FAC2
-4
View File
@@ -1,4 +0,0 @@
This is an important floating point math routine, documented in many places.
Example code utilizing this routine:
- https://github.com/irmen/prog8/blob/master/compiler/res/prog8lib/c64flt.p8
-24
View File
@@ -1,24 +0,0 @@
;; #LAYOUT# STD * #TAKE
;; #LAYOUT# X16 BASIC_0 #TAKE-OFFSET 2000
;; #LAYOUT# * BASIC_0 #TAKE
;; #LAYOUT# * * #IGNORE
;
; Math package - set FAC1 as RAM raised to the power of FAC2
;
; Input:
; - .A - address low byte
; - .Y - address high byte
;
; See also:
; - [CM64] Computes Mapping the Commodore 64 - page 117 (FPWRT)
; - https://www.c64-wiki.com/wiki/BASIC-ROM
; - https://www.c64-wiki.com/wiki/Floating_point_arithmetic
; - https://codebase64.org/doku.php?id=base:kernal_floating_point_mathematics
;
pwr_FAC2_MEM:
jsr mov_MEM_FAC1
; FALLTROUGH to pwr_FAC2_FAC1
-3
View File
@@ -1,3 +0,0 @@
This is an important floating point math routine, documented in many places.
XXX - try to find real-life example
-32
View File
@@ -1,32 +0,0 @@
;; #LAYOUT# STD * #TAKE
;; #LAYOUT# * BASIC_0 #TAKE
;; #LAYOUT# * * #IGNORE
;
; Math package - evaluate polynomial with odd powers only for FAC1 value
;
; Input:
; - .A - table address low byte
; - .Y - table address low byte
; - table format:
; - polynomial degree (8 bit integer)
; - oefficients (floats), starting from highest degree, times 1+degree
;
; Notes:
; - calls POLY2(table, X*X)*X
;
; See also:
; - [CM64] Computes Mapping the Commodore 64 - page 206
; - https://www.c64-wiki.com/wiki/Floating_point_arithmetic
; - https://www.c64-wiki.com/wiki/POLY1
poly1_FAC1:
jsr store_YA_in_ZP ; Store YA in second INDEX pointer
jsr mov_r_FAC1_TMP1 ; TMPF1 <- FAC1 (rounds)
jsr mov_FAC1_FAC2 ; FAC2 <- FAC1
jsr mul_FAC2_FAC1 ; FAC1 <- FAC1 * FAC2 (x^2)
jsr poly2_table_in_index2 ; poly2: skip storing YA to ZP
ldy #$00 ; FAC1 <- FAC1 * TEMPF1
lda #TEMPF1
jmp mul_MEM_FAC1
-3
View File
@@ -1,3 +0,0 @@
This is an important floating point math routine, documented in many places.
XXX - try to find real-life example
-69
View File
@@ -1,69 +0,0 @@
;; #LAYOUT# STD * #TAKE
;; #LAYOUT# * BASIC_0 #TAKE
;; #LAYOUT# * * #IGNORE
;
; Math package - evaluate polynomial with odd and even powers for FAC1 value
;
; Input:
; - .A - table address low byte
; - .Y - table address low byte
; - FAC1 - value of the independent variable
; Outputs: FAC1 holds the calculated value
; Clobbers: X, Y, A, FAC2, TEMPF2
;
; The polynomial table is formatted as:
; First one byte represents the degree of the polynomial
; Then comes an array of floating point number coefficients starting with the highest degree
; Horner's method is used for the calculation.
; See also:
; - [CM64] Computes Mapping the Commodore 64 - page 207
; - https://www.c64-wiki.com/wiki/Floating_point_arithmetic
; - https://www.c64-wiki.com/wiki/POLY1
;
poly2_FAC1:
jsr store_YA_in_ZP
poly2_table_in_index2: ; Called from poly1
ldy #$00 ; FAC1 -> TMPF2
ldx #TEMPF2
jsr mov_FAC1_MEM
ldx #$80 ; 0 -> FAC1
stx FAC1_exponent
ldx #0
lda (INDEX+2, x) ; degree (degree+1 = num coeffs)
sta SGNFLG ; number of terms register
inc INDEX+2
bcc @1
inc INDEX+3
@1:
lda INDEX+2
ldy INDEX+3
jsr add_MEM_FAC1 ; Add with coefficient
ldy #$00
lda #TEMPF2
jsr mul_MEM_FAC1 ; Multiply with x
clc ; Add 5 to table pointer
lda #$05
adc INDEX+2
sta INDEX+2
bcc @2
inc INDEX+3
@2:
dec SGNFLG
bpl @1
rts
store_YA_in_ZP: ; Used also by poly1
sty INDEX+3 ; Store YA in second INDEX pointer
sta INDEX+2
rts
-21
View File
@@ -1,21 +0,0 @@
;; #LAYOUT# STD * #TAKE
;; #LAYOUT# * BASIC_0 #TAKE
;; #LAYOUT# * * #IGNORE
;
; Math package - cosine of FAC1 in radians
;
; Adds pi/2 to FAC1 and falls through to the sine function
;
; See also:
; - [CM64] Computes Mapping the Commodore 64 - page 210
; - https://www.c64-wiki.com/wiki/Floating_point_arithmetic
; - https://codebase64.org/doku.php?id=base:kernal_floating_point_mathematics
;
COS:
ldy #>const_HALF_PI
lda #<const_HALF_PI
jsr add_MEM_FAC1
; FALLTHROUGH to SIN
-4
View File
@@ -1,4 +0,0 @@
This is an important floating point math routine, documented in many places.
Usage example:
- https://www.lemon64.com/forum/viewtopic.php?t=10492
-4
View File
@@ -1,4 +0,0 @@
This is an important floating point math routine, documented in many places.
Usage example:
- https://www.lemon64.com/forum/viewtopic.php?t=10492
-55
View File
@@ -1,55 +0,0 @@
;; #LAYOUT# STD * #TAKE
;; #LAYOUT# * BASIC_0 #TAKE
;; #LAYOUT# * * #IGNORE
;
; Math package - sine of FAC1 in radians
;
; See also:
; - [CM64] Computes Mapping the Commodore 64 - page 210 - XXX address does not match
; - https://www.c64-wiki.com/wiki/Floating_point_arithmetic
; - https://codebase64.org/doku.php?id=base:kernal_floating_point_mathematics
; - https://www.c64-wiki.com/wiki/SIN
sin_FAC1:
jsr mov_FAC1_FAC2 ; FAC1 <- X / (2*pi) (X2)
ldy #>const_DOUBLE_PI
lda #<const_DOUBLE_PI
jsr mov_MEM_FAC1
jsr div_FAC2_FAC1
jsr mov_FAC1_FAC2 ; FAC2 <- X2
jsr int_FAC1 ; FAC1 <- INT(X2)
jsr sub_FAC2_FAC1 ; FAC1 <- X2 - INT(X2) (F)
ldy #>const_QUARTER
lda #<const_QUARTER
jsr FCOMP
bmi sin_next ; FAC1 < 0.25
ldy #>sin_three_quarters
lda #<sin_three_quarters
jsr FCOMP
bmi sin_q23 ; FAC1 < 0.75
jsr mov_FAC1_FAC2 ; FAC1 <- FAC1 - 1.0
ldy #>const_ONE
lda #<const_ONE
jsr mov_MEM_FAC1
jsr sub_FAC2_FAC1
+bra sin_next
sin_q23:
ldy #>const_HALF
lda #<const_HALF
jsr sub_MEM_FAC1 ; FAC1 <- 0.5 - FAC1
; FALLTHROUGH
sin_next:
ldy #>poly_sin
lda #<poly_sin
jmp poly1_FAC1
-4
View File
@@ -1,4 +0,0 @@
This is an important floating point math routine, documented in many places.
Usage example:
- https://www.lemon64.com/forum/viewtopic.php?t=10492
-27
View File
@@ -1,27 +0,0 @@
;; #LAYOUT# STD * #TAKE
;; #LAYOUT# * BASIC_0 #TAKE
;; #LAYOUT# * * #IGNORE
;
; Math package - tangent of FAC1 in radians
;
; See also:
; - [CM64] Computes Mapping the Commodore 64 - page 210
; - https://www.c64-wiki.com/wiki/Floating_point_arithmetic
; - https://codebase64.org/doku.php?id=base:kernal_floating_point_mathematics
; - https://www.c64-wiki.com/wiki/TAN
; tan(x) = sin(x) / cos(x)
tan_FAC1:
jsr mov_r_FAC1_TMP1 ; TMP1 = x
jsr sin_FAC1 ; FAC1 = sin(x)
jsr mov_r_FAC1_TMP2 ; TMP2 = sin(x)
lda #TEMPF1
ldy #0
jsr mov_MEM_FAC1 ; FAC1 = x
jsr COS ; FAC1 = cos(x)
lda #TEMPF2
ldy #0
jsr mov_MEM_FAC2 ; FAC2 = sin(x)
jmp div_FAC2_FAC1 ; FAC1 = tan(x)
-57
View File
@@ -1,57 +0,0 @@
;; #LAYOUT# STD * #TAKE
;; #LAYOUT# * BASIC_0 #TAKE
;; #LAYOUT# * * #IGNORE
;
; Math package - arc tangent of FAC1
;
; See also:
; - [CM64] Computes Mapping the Commodore 64 - page 211
; - https://www.c64-wiki.com/wiki/Floating_point_arithmetic
; - https://codebase64.org/doku.php?id=base:kernal_floating_point_mathematics
; - https://www.c64-wiki.com/wiki/ATN
; Algorithm:
; 1. Save sign of x
; 2. Calculate AX = |x| (we can use that arctan(-x) = -arctan(x)
; 3. If AX > 1 set AX = 1/AX (we can use that arctan(x) = pi/2 - arctan(1/x) )
; 4. Use polynomial to approximate arctan(AX) now since 0 <= AX <= 1
; 5. Adjust sign of result and offset with pi/2 if needed as per above
atn_FAC1:
lda FAC1_sign ; Save sign of FAC1 on the stack
pha
lda #$00 ; FAC1 = abs(FAC1)
sta FAC1_sign
sta TEMPF1 ; AX not scaled
lda #<const_ONE
ldy #>const_ONE
jsr FCOMP
bmi atn_skipscale
lda #<const_ONE
ldy #>const_ONE
jsr div_MEM_FAC1
inc TEMPF1 ; AX scaled
atn_skipscale:
lda #<poly_atn
ldy #>poly_atn
jsr poly1_FAC1
lda TEMPF1
beq atn_skipoffset
lda #<const_HALF_PI
ldy #>const_HALF_PI
jsr sub_MEM_FAC1
atn_skipoffset:
pla
bpl atn_out
jmp toggle_sign_FAC1
atn_out:
rts
-3
View File
@@ -1,3 +0,0 @@
This is an important floating point math routine, documented in many places.
XXX - try to find real-life example
-23
View File
@@ -1,23 +0,0 @@
;; #LAYOUT# STD * #TAKE
;; #LAYOUT# X16 BASIC_0 #TAKE-OFFSET 2000
;; #LAYOUT# * BASIC_0 #TAKE
;; #LAYOUT# * * #IGNORE
; Polynomial for the atn function
poly_atn:
!byte $0B ; series length - 1
+PUT_CONST_POLY_ATN_1
+PUT_CONST_POLY_ATN_2
+PUT_CONST_POLY_ATN_3
+PUT_CONST_POLY_ATN_4
+PUT_CONST_POLY_ATN_5
+PUT_CONST_POLY_ATN_6
+PUT_CONST_POLY_ATN_7
+PUT_CONST_POLY_ATN_8
+PUT_CONST_POLY_ATN_9
+PUT_CONST_POLY_ATN_10
+PUT_CONST_POLY_ATN_11
+PUT_CONST_POLY_ATN_12
-19
View File
@@ -1,19 +0,0 @@
;; #LAYOUT# STD * #TAKE
;; #LAYOUT# X16 BASIC_0 #TAKE-OFFSET 2000
;; #LAYOUT# * BASIC_0 #TAKE
;; #LAYOUT# * * #IGNORE
; Polynomial for the exponential function
poly_exp:
!byte $07 ; series length - 1
+PUT_CONST_POLY_EXP_1
+PUT_CONST_POLY_EXP_2
+PUT_CONST_POLY_EXP_3
+PUT_CONST_POLY_EXP_4
+PUT_CONST_POLY_EXP_5
+PUT_CONST_POLY_EXP_6
+PUT_CONST_POLY_EXP_7
+PUT_CONST_POLY_EXP_8
-15
View File
@@ -1,15 +0,0 @@
;; #LAYOUT# STD * #TAKE
;; #LAYOUT# X16 BASIC_0 #TAKE-OFFSET 2000
;; #LAYOUT# * BASIC_0 #TAKE
;; #LAYOUT# * * #IGNORE
; Polynomial for the logarithm function
poly_log:
!byte $03 ; series length - 1
+PUT_CONST_POLY_LOG_1
+PUT_CONST_POLY_LOG_2
+PUT_CONST_POLY_LOG_3
+PUT_CONST_POLY_LOG_4
-23
View File
@@ -1,23 +0,0 @@
;; #LAYOUT# STD * #TAKE
;; #LAYOUT# X16 BASIC_0 #TAKE-OFFSET 2000
;; #LAYOUT# * BASIC_0 #TAKE
;; #LAYOUT# * * #IGNORE
; Polynomial for the sine function
poly_sin:
!byte $05 ; series length - 1
+PUT_CONST_POLY_SIN_1
+PUT_CONST_POLY_SIN_2
+PUT_CONST_POLY_SIN_3
+PUT_CONST_POLY_SIN_4
+PUT_CONST_POLY_SIN_5
+PUT_CONST_POLY_SIN_6
; Constant 0.75 needed for sine calculation
sin_three_quarters:
!byte $80, $40, $00, $00, $00