From 659dccd393ba65d216df663f8ad0f135a3a1dd02 Mon Sep 17 00:00:00 2001 From: FeralChild64 Date: Tue, 3 Mar 2020 20:56:40 +0100 Subject: [PATCH] floating point division written, not tested yet --- CREDITS.md | 1 + STATUS.md | 2 +- c64/basic/math/bb12.div_FAC2_FAC1.s | 20 ++++++-- c64/basic/math/div_mantissas.s | 79 +++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 c64/basic/math/div_mantissas.s diff --git a/CREDITS.md b/CREDITS.md index a1ebe654..fb5040c3 100644 --- a/CREDITS.md +++ b/CREDITS.md @@ -7,6 +7,7 @@ * Graham - for his PAL/NTSC detection routine * enthusi/Onslaught - for his example Turbo Tape 64 compatible loader * djmips - for fast 8x8 multiplication routine +* Verz - for the integer division routine ## for documentation diff --git a/STATUS.md b/STATUS.md index 05eb218a..2183672c 100644 --- a/STATUS.md +++ b/STATUS.md @@ -205,7 +205,7 @@ Floating point mathematical routines - not official, but well known and broadly | `$BAE2` | `mul10_FAC1` | DONE | | | `$BAFE` | `div10_FAC1_p` | PARTIAL | needs `div_FAC2_FAC1` | | `$BB0F` | `div_MEM_FAC1` | PARTIAL | needs `div_FAC2_FAC1` | -| `$BB12` | `div_FAC2_FAC1` | NOT DONE | some work already started | +| `$BB12` | `div_FAC2_FAC1` | PARTIAL | not tested yet | | `$BBA2` | `mov_MEM_FAC1` | DONE | | | `$BBA6` | `get_FAC2_via_INDEX` | NOT DONE | | | `$BBC7` | `mov_r_FAC1_TMP2` | PARTIAL | not fully tested yet | diff --git a/c64/basic/math/bb12.div_FAC2_FAC1.s b/c64/basic/math/bb12.div_FAC2_FAC1.s index 5f9cf4d5..5532ca46 100644 --- a/c64/basic/math/bb12.div_FAC2_FAC1.s +++ b/c64/basic/math/bb12.div_FAC2_FAC1.s @@ -22,7 +22,7 @@ // - https://codebase64.org/doku.php?id=base:kernal_floating_point_mathematics // -// XXX finish the implementation +// XXX test this code div_FAC2_FAC1: @@ -68,10 +68,22 @@ div_FAC2_FAC1: jsr div_FAC1_denorm jsr muldiv_RESHO_set_0 + jsr div_mantissas - // XXX + // Copy the result to FAC1 - XXX we probably already have similar code somewhere... + lda FAC2_mantissa+3 + sta FAC1_mantissa+3 + lda FAC2_mantissa+2 + sta FAC1_mantissa+2 + lda FAC2_mantissa+1 + sta FAC1_mantissa+1 + lda FAC2_mantissa+0 + sta FAC1_mantissa+0 + lda #$00 + sta FACOV - - STUB_IMPLEMENTATION() + // Normalize and quit + + jmp normal_FAC1 diff --git a/c64/basic/math/div_mantissas.s b/c64/basic/math/div_mantissas.s new file mode 100644 index 00000000..0e50d6c9 --- /dev/null +++ b/c64/basic/math/div_mantissas.s @@ -0,0 +1,79 @@ +// #LAYOUT# STD * #TAKE +// #LAYOUT# * BASIC_0 #TAKE +// #LAYOUT# * * #IGNORE + +// Math package - mantissa division, based on code by Verz +// +// divident: FAC2_mantissa, divisor: FAC1_mantissa, result: FAC2_mantissa, remainder: RESHO +// temporary storage: INDEX+0, INDEX+1, INDEX+2, INDEX+3 +// +// RESHO (remainder) should be set to 0 before +// +// see https://codebase64.org/doku.php?id=base:24bit_division_24-bit_result + +// XXX test this + +div_mantissas: + + ldx #40 // repeat for each bit + + // FALLTROUGH + +div_mantissas_loop: + + // Multiply divident by 2, msb -> Carry + + asl FAC2_mantissa+3 + rol FAC2_mantissa+2 + rol FAC2_mantissa+1 + rol FAC2_mantissa+0 + + // Multiply remainder by 2, lsb <- Carry + + rol RESHO+4 + rol RESHO+3 + rol RESHO+2 + rol RESHO+1 + rol RESHO+0 + + // Subtract divisor to see if it fits in + + lda RESHO+4 + sec + sbc FAC1_mantissa+3 + sta INDEX+3 + + lda RESHO+3 + sbc FAC1_mantissa+2 + sta INDEX+2 + + lda RESHO+2 + sbc FAC1_mantissa+1 + sta INDEX+1 + + lda RESHO+1 + sbc FAC1_mantissa+0 + sta INDEX+0 + + lda RESHO+0 + sbc #$00 + bcc div_mantissas_skip // if Carry is not set then divisor did not fit in yet + + sta RESHO+0 // else save substraction result as new remainder, + lda INDEX+0 + sta RESHO+1 + lda INDEX+1 + sta RESHO+2 + lda INDEX+3 + sta RESHO+4 + + inc FAC2_mantissa+3 // and INCrement result cause divisor fit in 1 times + + // FALLTROUGH + +div_mantissas_skip: + + dex + bne div_mantissas_loop + + rts