From 51064f6b7086c51d0c78b67918d29940674f0bca Mon Sep 17 00:00:00 2001 From: FeralChild64 Date: Fri, 31 Jan 2020 20:58:07 +0100 Subject: [PATCH] implemented ROUND math routine, corrected some others --- STATUS.md | 1 + c64/basic/,stubs_math/bb12.interop | 3 +- c64/basic/,stubs_math/bcf3.interop | 3 +- c64/basic/,stubs_math/bddd.interop | 3 +- c64/basic/math/CONUPK.s | 2 +- c64/basic/math/MOVFM.s | 2 +- c64/basic/math/NORMAL.s | 6 +-- c64/basic/math/ROUND.s | 61 ++++++++++++++++++++++++++++++ c64/basic/math/bc1b.ROUND.jmp.s | 7 ++++ c64/basic/math/bc1b.interop | 4 ++ 10 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 c64/basic/math/ROUND.s create mode 100644 c64/basic/math/bc1b.ROUND.jmp.s create mode 100644 c64/basic/math/bc1b.interop diff --git a/STATUS.md b/STATUS.md index 8d4e76d4..3bd045d0 100644 --- a/STATUS.md +++ b/STATUS.md @@ -196,6 +196,7 @@ Floating point mathematical routines - not official, but well known and broadly | `$BBD4` | `MOVMF` | NOT DONE | copy FAC1 to memory location, rounds if asked | | `$BBFC` | `MOVEF` | NOT DONE | copy FAC2 to FAC1 | | `$BC0F` | `MOVFA` | NOT DONE | copy FAC1 to FAC2, skips rounding | +| `$BC1B` | `ROUND` | PARTIAL | not tested | | `$BC2B` | `SIGN` | NOT DONE | evaluate FAC1 sign | | `$BC39` | `SGN` | NOT DONE | evaluate FAC1 sign, to FAC1 | | `$BC58` | `ABS` | NOT DONE | absolute value | diff --git a/c64/basic/,stubs_math/bb12.interop b/c64/basic/,stubs_math/bb12.interop index 0d18777c..ba5a12d8 100644 --- a/c64/basic/,stubs_math/bb12.interop +++ b/c64/basic/,stubs_math/bb12.interop @@ -1,3 +1,4 @@ This is an important floating point math routine, documented in many places. -XXX - try to find real-life example +Example code utilizing the routine: +- https://www.lemon64.com/forum/viewtopic.php?t=67576&sid=d41e77cdddd28d6f9c9d52ce5b4e8dc3 diff --git a/c64/basic/,stubs_math/bcf3.interop b/c64/basic/,stubs_math/bcf3.interop index 0d18777c..ba5a12d8 100644 --- a/c64/basic/,stubs_math/bcf3.interop +++ b/c64/basic/,stubs_math/bcf3.interop @@ -1,3 +1,4 @@ This is an important floating point math routine, documented in many places. -XXX - try to find real-life example +Example code utilizing the routine: +- https://www.lemon64.com/forum/viewtopic.php?t=67576&sid=d41e77cdddd28d6f9c9d52ce5b4e8dc3 diff --git a/c64/basic/,stubs_math/bddd.interop b/c64/basic/,stubs_math/bddd.interop index 0d18777c..ba5a12d8 100644 --- a/c64/basic/,stubs_math/bddd.interop +++ b/c64/basic/,stubs_math/bddd.interop @@ -1,3 +1,4 @@ This is an important floating point math routine, documented in many places. -XXX - try to find real-life example +Example code utilizing the routine: +- https://www.lemon64.com/forum/viewtopic.php?t=67576&sid=d41e77cdddd28d6f9c9d52ce5b4e8dc3 diff --git a/c64/basic/math/CONUPK.s b/c64/basic/math/CONUPK.s index a6ad4722..1b721579 100644 --- a/c64/basic/math/CONUPK.s +++ b/c64/basic/math/CONUPK.s @@ -38,7 +38,7 @@ CONUPK: // Now copy the data from RAM to FAC2, for the format description see: // - https://www.c64-wiki.com/wiki/Floating_point_arithmetic#Representation_in_the_C-64 - // Copy the mantissa - reverse byte order + // Copy the mantissa ldy #$04 lda (INDEX), y diff --git a/c64/basic/math/MOVFM.s b/c64/basic/math/MOVFM.s index 9f6c510c..844aa6da 100644 --- a/c64/basic/math/MOVFM.s +++ b/c64/basic/math/MOVFM.s @@ -35,7 +35,7 @@ MOVMF: // Now copy the data from RAM to FAC2, for the format description see: // - https://www.c64-wiki.com/wiki/Floating_point_arithmetic#Representation_in_the_C-64 - // Copy the mantissa - reverse byte order + // Copy the mantissa ldy #$04 lda (INDEX), y diff --git a/c64/basic/math/NORMAL.s b/c64/basic/math/NORMAL.s index f1e8fb61..6edbafde 100644 --- a/c64/basic/math/NORMAL.s +++ b/c64/basic/math/NORMAL.s @@ -44,10 +44,10 @@ normal_by_bit: clc rol FACOV - rol FAC1_mantissa+0 - rol FAC1_mantissa+1 - rol FAC1_mantissa+2 rol FAC1_mantissa+3 + rol FAC1_mantissa+2 + rol FAC1_mantissa+1 + rol FAC1_mantissa+0 bcc NORMAL // branch if next iteration needed rts diff --git a/c64/basic/math/ROUND.s b/c64/basic/math/ROUND.s new file mode 100644 index 00000000..b7706782 --- /dev/null +++ b/c64/basic/math/ROUND.s @@ -0,0 +1,61 @@ +// #LAYOUT# STD * #TAKE +// #LAYOUT# * BASIC_0 #TAKE +// #LAYOUT# * * #IGNORE + +// +// Math package - rounds FAC1 before copying +// +// See also: +// - [CM64] Computes Mapping the Commodore 64 - page 115 +// - https://codebase64.org/doku.php?id=base:kernal_floating_point_mathematics +// + +// XXX test this + +ROUND: + + // If number is 0, do not do anything + + lda FAC1_exponent + beq round_done + + lda FACOV + bpl round_zero_FACOV // branch if it is enough to clear FACOV + + // Increment the 4 bytes of matissa + + // Curse you, who invented this format! Had the mantissa been little endian, + // we could have used INW on 65C02 and above, the code would have been smaller! + + inc FAC1_mantissa+3 + bne round_zero_FACOV + inc FAC1_mantissa+2 + bne round_zero_FACOV + inc FAC1_mantissa+1 + bne round_zero_FACOV + inc FAC1_mantissa+0 + bne round_zero_FACOV + + // If we are here, it means mantissa is 0; put it to minimum allowed and increment exponent + + lda #$80 + sta FAC1_mantissa+0 + inc FAC1_exponent + bne round_zero_FACOV + + // If w are here, it means the exponent wrapped around to $00; put it back to $FF + + dec FAC1_exponent + + // FALLTROUGH + +round_zero_FACOV: + + lda #$00 + sta FACOV + + // FALLTROUGH + +round_done: + + rts diff --git a/c64/basic/math/bc1b.ROUND.jmp.s b/c64/basic/math/bc1b.ROUND.jmp.s new file mode 100644 index 00000000..e6306152 --- /dev/null +++ b/c64/basic/math/bc1b.ROUND.jmp.s @@ -0,0 +1,7 @@ +// #LAYOUT# STD * #TAKE +// #LAYOUT# * BASIC_0 #TAKE +// #LAYOUT# * * #IGNORE + +// Just a compatibility jump - our routine is longer than original one + + jmp ROUND diff --git a/c64/basic/math/bc1b.interop b/c64/basic/math/bc1b.interop new file mode 100644 index 00000000..ba5a12d8 --- /dev/null +++ b/c64/basic/math/bc1b.interop @@ -0,0 +1,4 @@ +This is an important floating point math routine, documented in many places. + +Example code utilizing the routine: +- https://www.lemon64.com/forum/viewtopic.php?t=67576&sid=d41e77cdddd28d6f9c9d52ce5b4e8dc3