From ee736cfcbcc4beb352456c7a342bb29e4b0e46fc Mon Sep 17 00:00:00 2001 From: Rikard Nordgren Date: Sat, 25 Jan 2025 18:14:31 +0100 Subject: [PATCH] Add int to float conversions --- STATUS.md | 8 ++--- src/basic/,stubs_math/b391.GIVAYF.s | 22 -------------- .../,stubs_math/bc3c.convert_A_to_FAC1.s | 14 --------- .../,stubs_math/bc44.convert_i16_to_FAC1.s | 13 --------- src/basic/math/b391.GIVAYF.s | 27 +++++++++++++++++ src/basic/{,stubs_math => math}/b391.interop | 0 .../b3a2.convert_Y_to_FAC1.s | 14 +++++++-- src/basic/{,stubs_math => math}/b3a2.interop | 0 src/basic/math/bc3c.convert_A_to_FAC1.s | 24 +++++++++++++++ src/basic/{,stubs_math => math}/bc3c.interop | 0 src/basic/math/bc44.convert_i16_to_FAC1.s | 29 +++++++++++++++++++ src/basic/{,stubs_math => math}/bc44.interop | 0 src/basic/math/convert_i16_expsign_to_FAC1.s | 24 +++++++++++++++ src/basic/math/sign_extend_A_to_Y.s | 13 +++++++++ strings/85648565 | 4 +++ strings/A8A90085 | 3 ++ 16 files changed, 139 insertions(+), 56 deletions(-) delete mode 100644 src/basic/,stubs_math/b391.GIVAYF.s delete mode 100644 src/basic/,stubs_math/bc3c.convert_A_to_FAC1.s delete mode 100644 src/basic/,stubs_math/bc44.convert_i16_to_FAC1.s create mode 100644 src/basic/math/b391.GIVAYF.s rename src/basic/{,stubs_math => math}/b391.interop (100%) rename src/basic/{,stubs_math => math}/b3a2.convert_Y_to_FAC1.s (57%) rename src/basic/{,stubs_math => math}/b3a2.interop (100%) create mode 100644 src/basic/math/bc3c.convert_A_to_FAC1.s rename src/basic/{,stubs_math => math}/bc3c.interop (100%) create mode 100644 src/basic/math/bc44.convert_i16_to_FAC1.s rename src/basic/{,stubs_math => math}/bc44.interop (100%) create mode 100644 src/basic/math/convert_i16_expsign_to_FAC1.s create mode 100644 src/basic/math/sign_extend_A_to_Y.s create mode 100644 strings/85648565 create mode 100644 strings/A8A90085 diff --git a/STATUS.md b/STATUS.md index 392ef05c..249ca2f9 100644 --- a/STATUS.md +++ b/STATUS.md @@ -199,8 +199,8 @@ Floating point mathematical routines - not official, but well known and broadly | `$AF9A` | `compute_ST` | NOT DONE | | | `$B1AA` | `FACINX` | NOT DONE | convert FAC1 to 16-bit signed integer | | `$B1BF` | `convert_FAC1_to_s16` | NOT DONE | | -| `$B391` | `GIVAYF` | NOT DONE | 16-bit signed integer to FAC1 | -| `$B3A2` | `convert_Y_to_FAC1` | NOT DONE | | +| `$B391` | `GIVAYF` | PARTIAL | not fully tested yet | +| `$B3A2` | `convert_Y_to_FAC1` | PARTIAL | not fully tested yet | | `$B794` | `convert_A_to_FAC1` | NOT DONE | | | `$B7B5` | `STRVAL` | NOT DONE | imports string to FAC1 | | `$B7F7` | `convert_FAC1_to_ADDR` | NOT DONE | | @@ -233,8 +233,8 @@ Floating point mathematical routines - not official, but well known and broadly | `$BC1B` | `round_FAC1` | PARTIAL | not fully tested yet | | `$BC2B` | `sgn_FAC1_A` | DONE | | | `$BC39` | `sgn_FAC1` | NOT DONE | | -| `$BC3C` | `convert_A_to_FAC1` | NOT DONE | | -| `$BC44` | `convert_i16_to_FAC1` | NOT DONE | | +| `$BC3C` | `convert_A_to_FAC1` | PARTIAL | not fully tested yet | +| `$BC44` | `convert_i16_to_FAC1` | PARTIAL | not fully tested yet | | `$BC58` | `abs_FAC1` | NOT DONE | | | `$BC5B` | `FCOMP` | NOT DONE | compare FAC1 with RAM location | | `$BC9B` | `QINT` | NOT DONE | convert FAC1 to 32 bit signed integer | diff --git a/src/basic/,stubs_math/b391.GIVAYF.s b/src/basic/,stubs_math/b391.GIVAYF.s deleted file mode 100644 index b2b0d282..00000000 --- a/src/basic/,stubs_math/b391.GIVAYF.s +++ /dev/null @@ -1,22 +0,0 @@ -;; #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 - address high byte (XXX really, not the opposite?) -; - .Y - address low byte -; -; See also: -; - [CM64] Computes Mapping the Commodore 64 - page 107 -; - https://www.c64-wiki.com/wiki/Floating_point_arithmetic -; - -; XXX provide implementation - -GIVAYF: - - +STUB_IMPLEMENTATION diff --git a/src/basic/,stubs_math/bc3c.convert_A_to_FAC1.s b/src/basic/,stubs_math/bc3c.convert_A_to_FAC1.s deleted file mode 100644 index 5d34e1cd..00000000 --- a/src/basic/,stubs_math/bc3c.convert_A_to_FAC1.s +++ /dev/null @@ -1,14 +0,0 @@ -;; #LAYOUT# STD * #TAKE -;; #LAYOUT# X16 BASIC_0 #TAKE-OFFSET 2000 -;; #LAYOUT# * BASIC_0 #TAKE -;; #LAYOUT# * * #IGNORE - -; -; Math package - XXX -; -; - https://codebase64.org/doku.php?id=base:asm_include_file_for_basic_routines -; - https://codebase64.org/doku.php?id=base:kernal_floating_point_mathematics -; -; XXX add name, label, documentation, implement, test - - +STUB_IMPLEMENTATION diff --git a/src/basic/,stubs_math/bc44.convert_i16_to_FAC1.s b/src/basic/,stubs_math/bc44.convert_i16_to_FAC1.s deleted file mode 100644 index c7992238..00000000 --- a/src/basic/,stubs_math/bc44.convert_i16_to_FAC1.s +++ /dev/null @@ -1,13 +0,0 @@ -;; #LAYOUT# STD * #TAKE -;; #LAYOUT# X16 BASIC_0 #TAKE-OFFSET 2000 -;; #LAYOUT# * BASIC_0 #TAKE -;; #LAYOUT# * * #IGNORE - -; -; Math package - XXX -; -; - https://codebase64.org/doku.php?id=base:kernal_floating_point_mathematics -; -; XXX add name, label, documentation, implement, test - - +STUB_IMPLEMENTATION diff --git a/src/basic/math/b391.GIVAYF.s b/src/basic/math/b391.GIVAYF.s new file mode 100644 index 00000000..92ccbb9c --- /dev/null +++ b/src/basic/math/b391.GIVAYF.s @@ -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 diff --git a/src/basic/,stubs_math/b391.interop b/src/basic/math/b391.interop similarity index 100% rename from src/basic/,stubs_math/b391.interop rename to src/basic/math/b391.interop diff --git a/src/basic/,stubs_math/b3a2.convert_Y_to_FAC1.s b/src/basic/math/b3a2.convert_Y_to_FAC1.s similarity index 57% rename from src/basic/,stubs_math/b3a2.convert_Y_to_FAC1.s rename to src/basic/math/b3a2.convert_Y_to_FAC1.s index dbd6d250..12f3bdad 100644 --- a/src/basic/,stubs_math/b3a2.convert_Y_to_FAC1.s +++ b/src/basic/math/b3a2.convert_Y_to_FAC1.s @@ -4,13 +4,21 @@ ;; #LAYOUT# * * #IGNORE ; -; Math package - XXX +; 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 ; -; XXX add name, label, documentation, implement, test - +STUB_IMPLEMENTATION +convert_Y_to_FAC1: + lda #$00 + sta FAC1_mantissa+1 + sty FAC1_mantissa + jmp convert_i16_to_FAC1 diff --git a/src/basic/,stubs_math/b3a2.interop b/src/basic/math/b3a2.interop similarity index 100% rename from src/basic/,stubs_math/b3a2.interop rename to src/basic/math/b3a2.interop diff --git a/src/basic/math/bc3c.convert_A_to_FAC1.s b/src/basic/math/bc3c.convert_A_to_FAC1.s new file mode 100644 index 00000000..11ef115b --- /dev/null +++ b/src/basic/math/bc3c.convert_A_to_FAC1.s @@ -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 diff --git a/src/basic/,stubs_math/bc3c.interop b/src/basic/math/bc3c.interop similarity index 100% rename from src/basic/,stubs_math/bc3c.interop rename to src/basic/math/bc3c.interop diff --git a/src/basic/math/bc44.convert_i16_to_FAC1.s b/src/basic/math/bc44.convert_i16_to_FAC1.s new file mode 100644 index 00000000..bf8a956b --- /dev/null +++ b/src/basic/math/bc44.convert_i16_to_FAC1.s @@ -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 diff --git a/src/basic/,stubs_math/bc44.interop b/src/basic/math/bc44.interop similarity index 100% rename from src/basic/,stubs_math/bc44.interop rename to src/basic/math/bc44.interop diff --git a/src/basic/math/convert_i16_expsign_to_FAC1.s b/src/basic/math/convert_i16_expsign_to_FAC1.s new file mode 100644 index 00000000..a04c02c1 --- /dev/null +++ b/src/basic/math/convert_i16_expsign_to_FAC1.s @@ -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 #