Add int to float conversions

This commit is contained in:
Rikard Nordgren committed 2025-01-28 21:07:33 +01:00
1 parent e41558ce33
commit ee736cfcbc
16 files changed
+139 -56

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
;; #LAYOUT# STD * #TAKE
;; #LAYOUT# X16 BASIC_0 #TAKE-OFFSET 2000
;; #LAYOUT# * BASIC_0 #TAKE
;; #LAYOUT# * * #IGNORE
;
; Math package - 16-bit signed integer to FAC1
;
; Output:
; - .A - high byte
; - .Y - low byte
;
; Sets the data type flag at $0D to "numeric", stores the number in the FAC1 mantissa
; then calls convert_i16_to_FAC1
;
; See also:
; - [CM64] Computes Mapping the Commodore 64 - page 107
; - https://www.c64-wiki.com/wiki/GIVAYF
;
GIVAYF:
ldx #$00
stx $0D
sty FAC1_mantissa
sta FAC1_mantissa+1
jmp convert_i16_to_FAC1
+4
View File
@@ -0,0 +1,4 @@
This is an important floating point math routine, documented in many places.
Example codoe utilizing this routine:
- https://github.com/irmen/prog8/blob/master/compiler/res/prog8lib/c64flt.p8
+24
View File
@@ -0,0 +1,24 @@
;; #LAYOUT# STD * #TAKE
;; #LAYOUT# X16 BASIC_0 #TAKE-OFFSET 2000
;; #LAYOUT# * BASIC_0 #TAKE
;; #LAYOUT# * * #IGNORE
;
; Math package - convert uint8 in Y to FAC1
;
; Input: Y - uint8 to convert to float
; Result: FAC1
;
; Stores Y into first FAC1 mantissa byte, clears the second and calls convert_i16_to_FAC1
;
; - https://www.c64-wiki.com/wiki/BASIC-ROM
; - https://sta.c64.org/cbm64basconv.html
; - https://sourceforge.net/p/acme-crossass/code-0/38/tree/trunk/ACME_Lib/cbm/c64/float.a?force=True
; - https://codebase64.org/doku.php?id=base:kernal_floating_point_mathematics
;
convert_Y_to_FAC1:
lda #$00
sta FAC1_mantissa+1
sty FAC1_mantissa
jmp convert_i16_to_FAC1
+5
View File
@@ -0,0 +1,5 @@
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
@@ -0,0 +1,24 @@
;; #LAYOUT# STD * #TAKE
;; #LAYOUT# X16 BASIC_0 #TAKE-OFFSET 2000
;; #LAYOUT# * BASIC_0 #TAKE
;; #LAYOUT# * * #IGNORE
;
; Math package - convert int8 in A to FAC1
;
; Input: A - int8 to convert to float
; Result: FAC1
;
; Sets first byte of FAC1 to A, sign extends A into tge second byte and falls through to convert_i16_to_FAC1
;
; - https://codebase64.org/doku.php?id=base:asm_include_file_for_basic_routines
; - https://codebase64.org/doku.php?id=base:kernal_floating_point_mathematics
;
convert_A_to_FAC1:
sta FAC1_mantissa
jsr sign_extend_A_to_Y
sty FAC1_mantissa+1
+nop ; To not leave a gap to the fallthrough
; Fall through to convert_i16_to_FAC1
+4
View File
@@ -0,0 +1,4 @@
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
+29
View File
@@ -0,0 +1,29 @@
;; #LAYOUT# STD * #TAKE
;; #LAYOUT# X16 BASIC_0 #TAKE-OFFSET 2000
;; #LAYOUT# * BASIC_0 #TAKE
;; #LAYOUT# * * #IGNORE
;
; Math package - Convert signed 16-bit integer to floating point
;
; - https://codebase64.org/doku.php?id=base:kernal_floating_point_mathematics
; - https://www.c64-wiki.com/wiki/GIVAYF (which calls this function)
;
; Convert the signed 16-bit integer in FAC1 mantissa ($62:$63)
; into a floating point number in FAC1
; NOTE:
; According to https://c64os.com/post/floatingpointmath at $BC49
; Assume FAC1_mantissa+0 and +1. X=exponent and C=0 if negative and C=1 if positive
convert_i16_to_FAC1:
ldx #$90
jsr carry
jmp convert_i16_expsign_to_FAC1 ; This should be at $BC49 (see note above)
carry:
lda FAC1_mantissa+1
cmp #$80 ; Copy sign into carry
rts
+3
View File
@@ -0,0 +1,3 @@
This is an important floating point math routine, documented in many places.
XXX - try to find real-life example
@@ -0,0 +1,24 @@
;; #LAYOUT# STD * #TAKE
;; #LAYOUT# * BASIC_0 #TAKE
;; #LAYOUT# * * #IGNORE
;
; Math package - convert i16 with exponent in X and sign in C to FAC1
convert_i16_expsign_to_FAC1:
lda #$00
sta FAC1_mantissa+2 ; Clear high bytes of mantissa
sta FAC1_mantissa+3
stx FAC1_exponent
sta FAC1_sign
bcs @1
; Handle signed int
lda FAC1_mantissa+1 ; Clear sign bit of int 16
and #$7F
sta FAC1_mantissa+1
ldy #>CONST_NEG_32768 ; Subtract with 32768 to invert negative number
lda #<CONST_NEG_32768
jsr add_MEM_FAC1
@1: jmp normal_FAC1
+13
View File
@@ -0,0 +1,13 @@
;; #LAYOUT# STD * #TAKE
;; #LAYOUT# * BASIC_0 #TAKE
;; #LAYOUT# * * #IGNORE
;
; Math package - helper function to sign extend accumulator into Y
sign_extend_A_to_Y:
cmp #$80 ; Copy sign into carry
ldy #$00 ; Y = $00
bcc @1 ; Branch if not negative
dey ; Y = $FF
@1: rts