2019-01-05 15:09:05 +00:00
|
|
|
; Prog8 definitions for floating point handling on the Commodore-64
|
|
|
|
|
2023-12-26 22:37:59 +00:00
|
|
|
%option enable_floats, no_symbol_prefixing, ignore_unused
|
2024-02-07 21:36:43 +00:00
|
|
|
%import shared_floats_functions
|
2019-01-05 15:09:05 +00:00
|
|
|
|
2020-09-20 21:49:36 +00:00
|
|
|
floats {
|
2024-04-14 22:52:09 +00:00
|
|
|
; ---- this block contains C-64 floating point related functions ----
|
2019-04-07 22:36:19 +00:00
|
|
|
|
2019-01-05 15:09:05 +00:00
|
|
|
; ---- C64 basic and kernal ROM float constants and functions ----
|
|
|
|
|
|
|
|
; note: the fac1 and fac2 are working registers and take 6 bytes each,
|
|
|
|
; floats in memory (and rom) are stored in 5-byte MFLPT packed format.
|
|
|
|
|
|
|
|
|
|
|
|
; note: fac1/2 might get clobbered even if not mentioned in the function's name.
|
|
|
|
; note: for subtraction and division, the left operand is in fac2, the right operand in fac1.
|
|
|
|
|
2020-03-10 22:09:31 +00:00
|
|
|
romsub $bba2 = MOVFM(uword mflpt @ AY) clobbers(A,Y) ; load mflpt value from memory in A/Y into fac1
|
|
|
|
romsub $bba6 = FREADMEM() clobbers(A,Y) ; load mflpt value from memory in $22/$23 into fac1
|
|
|
|
romsub $ba8c = CONUPK(uword mflpt @ AY) clobbers(A,Y) ; load mflpt value from memory in A/Y into fac2
|
|
|
|
romsub $ba90 = FAREADMEM() clobbers(A,Y) ; load mflpt value from memory in $22/$23 into fac2
|
|
|
|
romsub $bbfc = MOVFA() clobbers(A,X) ; copy fac2 to fac1
|
2024-04-14 22:52:09 +00:00
|
|
|
romsub $bc0c = MOVAF() clobbers(A,X) ; copy fac1 to fac2 (rounded the least significant bit)
|
2020-03-10 22:09:31 +00:00
|
|
|
romsub $bc0f = MOVEF() clobbers(A,X) ; copy fac1 to fac2
|
|
|
|
romsub $bbd4 = MOVMF(uword mflpt @ XY) clobbers(A,Y) ; store fac1 to memory X/Y as 5-byte mflpt
|
2019-01-05 15:09:05 +00:00
|
|
|
|
|
|
|
; fac1 -> unsigned word in Y/A (might throw ILLEGAL QUANTITY) (result also in $14/15)
|
2020-09-20 21:49:36 +00:00
|
|
|
; (tip: use floats.GETADRAY to get A/Y output; lo/hi switched to normal little endian order)
|
2020-03-10 22:09:31 +00:00
|
|
|
romsub $b7f7 = GETADR() clobbers(X) -> ubyte @ Y, ubyte @ A
|
2019-01-05 15:09:05 +00:00
|
|
|
|
2020-03-10 22:09:31 +00:00
|
|
|
romsub $bc9b = QINT() clobbers(A,X,Y) ; fac1 -> 4-byte signed integer in 98-101 ($62-$65), with the MSB FIRST.
|
|
|
|
romsub $b1bf = AYINT() clobbers(A,X,Y) ; fac1-> signed word in 100-101 ($64-$65) MSB FIRST. (might throw ILLEGAL QUANTITY)
|
2019-01-05 15:09:05 +00:00
|
|
|
|
|
|
|
; GIVAYF: signed word in Y/A (note different lsb/msb order) -> float in fac1
|
2020-09-20 21:49:36 +00:00
|
|
|
; (tip: use floats.GIVAYFAY to use A/Y input; lo/hi switched to normal order)
|
|
|
|
; there is also floats.GIVUAYFAY - unsigned word in A/Y (lo/hi) to fac1
|
|
|
|
; there is also floats.FREADS32 that reads from 98-101 ($62-$65) MSB FIRST
|
|
|
|
; there is also floats.FREADUS32 that reads from 98-101 ($62-$65) MSB FIRST
|
2024-09-20 15:28:37 +00:00
|
|
|
; there is also floats.FREAD{S,U}24AXY that read (un)signed int24 into fac1 from A/X/Y (lo/mid/hi bytes)
|
2020-03-10 22:09:31 +00:00
|
|
|
romsub $b391 = GIVAYF(ubyte lo @ Y, ubyte hi @ A) clobbers(A,X,Y)
|
|
|
|
|
|
|
|
romsub $b3a2 = FREADUY(ubyte value @ Y) clobbers(A,X,Y) ; 8 bit unsigned Y -> float in fac1
|
|
|
|
romsub $bc3c = FREADSA(byte value @ A) clobbers(A,X,Y) ; 8 bit signed A -> float in fac1
|
2023-12-29 02:12:44 +00:00
|
|
|
romsub $b7b5 = FREADSTR(ubyte length @ A) clobbers(A,X,Y) ; str -> fac1, $22/23 must point to string, A=string length. Also see parse()
|
2020-03-10 22:09:31 +00:00
|
|
|
romsub $aabc = FPRINTLN() clobbers(A,X,Y) ; print string of fac1, on one line (= with newline) destroys fac1. (consider FOUT + STROUT as well)
|
|
|
|
romsub $bddd = FOUT() clobbers(X) -> uword @ AY ; fac1 -> string, address returned in AY ($0100)
|
|
|
|
|
2024-04-14 22:52:09 +00:00
|
|
|
romsub $b849 = FADDH() clobbers(A,X,Y) ; fac1 += 0.5, for integer rounding- call this before INT
|
2020-03-10 22:09:31 +00:00
|
|
|
romsub $bae2 = MUL10() clobbers(A,X,Y) ; fac1 *= 10
|
2023-12-06 00:15:29 +00:00
|
|
|
romsub $bafe = DIV10() clobbers(A,X,Y) ; fac1 /= 10 , CAUTION: result is always positive! You have to fix sign manually!
|
2020-03-10 22:09:31 +00:00
|
|
|
romsub $bc5b = FCOMP(uword mflpt @ AY) clobbers(X,Y) -> ubyte @ A ; A = compare fac1 to mflpt in A/Y, 0=equal 1=fac1 is greater, 255=fac1 is less than
|
|
|
|
|
|
|
|
romsub $b86a = FADDT() clobbers(A,X,Y) ; fac1 += fac2
|
|
|
|
romsub $b867 = FADD(uword mflpt @ AY) clobbers(A,X,Y) ; fac1 += mflpt value from A/Y
|
|
|
|
romsub $b853 = FSUBT() clobbers(A,X,Y) ; fac1 = fac2-fac1 mind the order of the operands
|
|
|
|
romsub $b850 = FSUB(uword mflpt @ AY) clobbers(A,X,Y) ; fac1 = mflpt from A/Y - fac1
|
|
|
|
romsub $ba2b = FMULTT() clobbers(A,X,Y) ; fac1 *= fac2
|
|
|
|
romsub $ba28 = FMULT(uword mflpt @ AY) clobbers(A,X,Y) ; fac1 *= mflpt value from A/Y
|
2023-03-29 21:46:44 +00:00
|
|
|
romsub $bb12 = FDIVT() clobbers(A,X,Y) ; fac1 = fac2/fac1 mind the order of the operands
|
|
|
|
romsub $bb0f = FDIV(uword mflpt @ AY) clobbers(A,X,Y) ; fac1 = mflpt in A/Y / fac1
|
2020-03-10 22:09:31 +00:00
|
|
|
romsub $bf7b = FPWRT() clobbers(A,X,Y) ; fac1 = fac2 ** fac1
|
|
|
|
romsub $bf78 = FPWR(uword mflpt @ AY) clobbers(A,X,Y) ; fac1 = fac2 ** mflpt from A/Y
|
2020-08-29 21:55:26 +00:00
|
|
|
romsub $bd7e = FINLOG(byte value @A) clobbers (A, X, Y) ; fac1 += signed byte in A
|
2020-03-10 22:09:31 +00:00
|
|
|
|
|
|
|
romsub $aed4 = NOTOP() clobbers(A,X,Y) ; fac1 = NOT(fac1)
|
2024-04-14 22:52:09 +00:00
|
|
|
romsub $bccc = INT() clobbers(A,X,Y) ; INT() truncates, use FADDH first to integer round instead of trunc
|
2020-03-10 22:09:31 +00:00
|
|
|
romsub $b9ea = LOG() clobbers(A,X,Y) ; fac1 = LN(fac1) (natural log)
|
|
|
|
romsub $bc39 = SGN() clobbers(A,X,Y) ; fac1 = SGN(fac1), result of SIGN (-1, 0 or 1)
|
|
|
|
romsub $bc2b = SIGN() -> ubyte @ A ; SIGN(fac1) to A, $ff, $0, $1 for negative, zero, positive
|
|
|
|
romsub $bc58 = ABS() ; fac1 = ABS(fac1)
|
|
|
|
romsub $bf71 = SQR() clobbers(A,X,Y) ; fac1 = SQRT(fac1)
|
|
|
|
romsub $bf74 = SQRA() clobbers(A,X,Y) ; fac1 = SQRT(fac2)
|
|
|
|
romsub $bfed = EXP() clobbers(A,X,Y) ; fac1 = EXP(fac1) (e ** fac1)
|
2021-04-05 22:15:43 +00:00
|
|
|
romsub $bfb4 = NEGOP() clobbers(A) ; switch the sign of fac1 (fac1 = -fac1)
|
2023-12-05 21:54:35 +00:00
|
|
|
romsub $b8d7 = NORMAL() clobbers(A) ; normalize FAC1
|
2020-03-10 22:09:31 +00:00
|
|
|
romsub $e097 = RND() clobbers(A,X,Y) ; fac1 = RND(fac1) float random number generator
|
|
|
|
romsub $e264 = COS() clobbers(A,X,Y) ; fac1 = COS(fac1)
|
|
|
|
romsub $e26b = SIN() clobbers(A,X,Y) ; fac1 = SIN(fac1)
|
|
|
|
romsub $e2b4 = TAN() clobbers(A,X,Y) ; fac1 = TAN(fac1)
|
|
|
|
romsub $e30e = ATN() clobbers(A,X,Y) ; fac1 = ATN(fac1)
|
|
|
|
|
|
|
|
|
|
|
|
asmsub FREADS32() clobbers(A,X,Y) {
|
2019-01-05 15:09:05 +00:00
|
|
|
; ---- fac1 = signed int32 from $62-$65 big endian (MSB FIRST)
|
|
|
|
%asm {{
|
|
|
|
lda $62
|
|
|
|
eor #$ff
|
|
|
|
asl a
|
|
|
|
lda #0
|
|
|
|
ldx #$a0
|
|
|
|
jmp $bc4f ; internal BASIC routine
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2019-07-08 21:00:18 +00:00
|
|
|
asmsub FREADUS32 () clobbers(A,X,Y) {
|
2019-01-05 15:09:05 +00:00
|
|
|
; ---- fac1 = uint32 from $62-$65 big endian (MSB FIRST)
|
|
|
|
%asm {{
|
|
|
|
sec
|
|
|
|
lda #0
|
|
|
|
ldx #$a0
|
|
|
|
jmp $bc4f ; internal BASIC routine
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2019-07-08 21:00:18 +00:00
|
|
|
asmsub FREADS24AXY (ubyte lo @ A, ubyte mid @ X, ubyte hi @ Y) clobbers(A,X,Y) {
|
2019-01-05 15:09:05 +00:00
|
|
|
; ---- fac1 = signed int24 (A/X/Y contain lo/mid/hi bytes)
|
|
|
|
%asm {{
|
|
|
|
sty $62
|
|
|
|
stx $63
|
|
|
|
sta $64
|
|
|
|
lda $62
|
|
|
|
eor #$FF
|
|
|
|
asl a
|
|
|
|
lda #0
|
|
|
|
sta $65
|
|
|
|
ldx #$98
|
|
|
|
jmp $bc4f ; internal BASIC routine
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2024-09-20 15:28:37 +00:00
|
|
|
asmsub FREADU24AXY(ubyte lo @ A, ubyte mid @ X, ubyte hi @ Y) clobbers(A, X, Y) -> float @FAC1 {
|
2024-09-20 15:33:00 +00:00
|
|
|
; ---- fac1 = unsigned int24 (A/X/Y contain lo/mid/hi bytes)
|
2024-09-20 15:28:37 +00:00
|
|
|
%asm{{
|
|
|
|
FAC = $61
|
|
|
|
sty FAC+1
|
|
|
|
stx FAC+2
|
|
|
|
sta FAC+3
|
|
|
|
|
|
|
|
cpy #$00
|
|
|
|
bne +
|
|
|
|
cpx #$00
|
|
|
|
bne +
|
|
|
|
cmp #$00
|
|
|
|
beq ++
|
|
|
|
|
|
|
|
+ ldx #$98
|
|
|
|
bit FAC+1
|
|
|
|
bmi +
|
|
|
|
|
|
|
|
- dex
|
|
|
|
asl FAC+3
|
|
|
|
rol FAC+2
|
|
|
|
rol FAC+1
|
|
|
|
bpl -
|
|
|
|
|
|
|
|
+ stx FAC
|
|
|
|
lda #$00
|
|
|
|
sta FAC+4
|
|
|
|
sta FAC+5
|
|
|
|
rts
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2019-07-08 21:00:18 +00:00
|
|
|
asmsub GIVUAYFAY (uword value @ AY) clobbers(A,X,Y) {
|
2019-01-05 15:09:05 +00:00
|
|
|
; ---- unsigned 16 bit word in A/Y (lo/hi) to fac1
|
|
|
|
%asm {{
|
|
|
|
sty $62
|
|
|
|
sta $63
|
|
|
|
ldx #$90
|
|
|
|
sec
|
|
|
|
jmp $bc49 ; internal BASIC routine
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2019-07-08 21:00:18 +00:00
|
|
|
asmsub GIVAYFAY (uword value @ AY) clobbers(A,X,Y) {
|
2019-01-05 15:09:05 +00:00
|
|
|
; ---- signed 16 bit word in A/Y (lo/hi) to float in fac1
|
|
|
|
%asm {{
|
2020-08-25 17:44:08 +00:00
|
|
|
sta P8ZP_SCRATCH_REG
|
2019-01-05 15:09:05 +00:00
|
|
|
tya
|
2020-08-25 17:44:08 +00:00
|
|
|
ldy P8ZP_SCRATCH_REG
|
2019-04-07 22:36:19 +00:00
|
|
|
jmp GIVAYF ; this uses the inverse order, Y/A
|
2019-01-05 15:09:05 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2019-07-09 05:24:21 +00:00
|
|
|
asmsub GETADRAY () clobbers(X) -> uword @ AY {
|
2019-01-05 15:09:05 +00:00
|
|
|
; ---- fac1 to unsigned word in A/Y
|
|
|
|
%asm {{
|
2019-04-07 22:36:19 +00:00
|
|
|
jsr GETADR ; this uses the inverse order, Y/A
|
2020-08-25 17:44:08 +00:00
|
|
|
sta P8ZP_SCRATCH_B1
|
2019-01-05 15:09:05 +00:00
|
|
|
tya
|
2020-08-25 17:44:08 +00:00
|
|
|
ldy P8ZP_SCRATCH_B1
|
2019-01-05 15:09:05 +00:00
|
|
|
rts
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2022-05-03 21:43:38 +00:00
|
|
|
&uword AYINT_facmo = $64 ; $64/$65 contain result of AYINT
|
2022-04-13 22:38:31 +00:00
|
|
|
|
2023-12-29 02:12:44 +00:00
|
|
|
sub rnd() -> float {
|
2022-10-22 11:33:35 +00:00
|
|
|
%asm {{
|
|
|
|
lda #1
|
|
|
|
jsr FREADSA
|
2023-07-15 13:18:26 +00:00
|
|
|
jmp RND ; rng into fac1
|
2022-10-22 11:33:35 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2023-12-29 02:12:44 +00:00
|
|
|
asmsub parse(str value @AY) -> float @FAC1 {
|
2023-08-16 12:47:20 +00:00
|
|
|
%asm {{
|
|
|
|
sta $22
|
|
|
|
sty $23
|
|
|
|
jsr prog8_lib.strlen
|
|
|
|
tya
|
|
|
|
jmp FREADSTR
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2023-12-05 21:54:35 +00:00
|
|
|
asmsub normalize(float value @FAC1) -> float @ FAC1 {
|
|
|
|
%asm {{
|
|
|
|
jmp floats.NORMAL
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2024-09-20 15:28:37 +00:00
|
|
|
; get the jiffy clock as a float
|
|
|
|
asmsub time() -> float @ FAC1 {
|
|
|
|
%asm {{
|
|
|
|
jsr cbm.RDTIM
|
|
|
|
jmp floats.FREADU24AXY
|
|
|
|
}}
|
|
|
|
}
|
2022-10-22 11:33:35 +00:00
|
|
|
|
2021-05-15 22:14:57 +00:00
|
|
|
%asminclude "library:c64/floats.asm"
|
|
|
|
%asminclude "library:c64/floats_funcs.asm"
|
2019-01-05 15:09:05 +00:00
|
|
|
|
2020-08-29 21:55:26 +00:00
|
|
|
}
|