diff --git a/STATUS.md b/STATUS.md index ddd0274d..49b5c4c7 100644 --- a/STATUS.md +++ b/STATUS.md @@ -193,7 +193,7 @@ Floating point mathematical routines - not official, but well known and broadly | `$B853` | `sub_FAC2_FAC1` | PARTIAL | needs `add_FAC2_FAC1` | | `$B862` | `add_align_exponents` | PARTIAL | not tested yet | | `$B867` | `add_MEM_FAC1` | PARTIAL | needs `add_FAC2_FAC1` | -| `$B86A` | `add_FAC2_FAC1` | NOT DONE | | +| `$B86A` | `add_FAC2_FAC1` | PARTIAL | not tested yet | | `$B8FE` | `normal_FAC1` | PARTIAL | not tested yet | | `$B947` | `inv_FAC1_mantissa` | NOT DONE | | | `$B983` | `MULSHF` | NOT DONE | | diff --git a/c64/basic/,stubs_math/b86a.add_FAC2_FAC1.s b/c64/basic/,stubs_math/b86a.add_FAC2_FAC1.s deleted file mode 100644 index bf18b367..00000000 --- a/c64/basic/,stubs_math/b86a.add_FAC2_FAC1.s +++ /dev/null @@ -1,57 +0,0 @@ -// #LAYOUT# STD * #TAKE -// #LAYOUT# X16 BASIC_0 #TAKE-OFFSET 2000 -// #LAYOUT# * BASIC_0 #TAKE -// #LAYOUT# * * #IGNORE - -// -// Math package - add FAC2 to FAC1 -// -// Input: -// - .A - must load FAC1 exponent ($61) beforehand to set the zero flag -// -// Note: -// - load FAC2 after FAC1, or mimic the Kernals sign comparison (XXX do we need it?) -// -// See also: -// - [CM64] Computes Mapping the Commodore 64 - page 112 -// - https://www.c64-wiki.com/wiki/Floating_point_arithmetic -// - https://codebase64.org/doku.php?id=base:kernal_floating_point_mathematics -// - -// XXX provide implementation - -add_FAC2_FAC1: - - // Check if FAC2 is 0; if so, leave FAC1 without changes - - lda FAC2_exponent - beq add_FAC2_FAC1_done - - // If FAC1 is 0, just copy FAC2 to FAC1 - - lda FAC1_exponent - beq_16 mov_FAC2_FAC1 - - // Make sure the exponents are aligned - - jsr add_align_exponents - - // Check whether signs are the same - - lda FAC1_sign - eor FAC2_sign - - bmi add_FAC2_FAC1_sub // branch if signs are opposite - - - - - -add_FAC2_FAC1_sub: - - STUB_IMPLEMENTATION() - - -add_FAC2_FAC1_done: - - rts \ No newline at end of file diff --git a/c64/basic/math/b86a.add_FAC2_FAC1.s b/c64/basic/math/b86a.add_FAC2_FAC1.s new file mode 100644 index 00000000..4565af0b --- /dev/null +++ b/c64/basic/math/b86a.add_FAC2_FAC1.s @@ -0,0 +1,114 @@ +// #LAYOUT# STD * #TAKE +// #LAYOUT# X16 BASIC_0 #TAKE-OFFSET 2000 +// #LAYOUT# * BASIC_0 #TAKE +// #LAYOUT# * * #IGNORE + +// +// Math package - add FAC2 to FAC1 +// +// Input: +// - .A - must load FAC1 exponent ($61) beforehand to set the zero flag +// +// Note: +// - load FAC2 after FAC1, or mimic the Kernals sign comparison (XXX do we need it?) +// +// See also: +// - [CM64] Computes Mapping the Commodore 64 - page 112 +// - https://www.c64-wiki.com/wiki/Floating_point_arithmetic +// - https://codebase64.org/doku.php?id=base:kernal_floating_point_mathematics +// + +// XXX test this + +add_FAC2_FAC1: + + // Check if FAC2 is 0; if so leave FAC1 without changes + + lda FAC2_exponent + beq add_FAC2_FAC1_done + + // If FAC1 is 0, if so just copy FAC2 to FAC1 + + lda FAC1_exponent + beq_16 mov_FAC2_FAC1 + + // Make sure the exponents are aligned + + jsr add_align_exponents + + // Check whether signs are the same + + lda FAC1_sign + eor FAC2_sign + + bmi add_FAC2_FAC1_sub // branch if signs are opposite + + // Perform the addition + + clc + lda FAC2_mantissa+3 + adc FAC1_mantissa+3 + sta FAC1_mantissa+3 + + lda FAC2_mantissa+2 + adc FAC1_mantissa+2 + sta FAC1_mantissa+2 + + lda FAC2_mantissa+1 + adc FAC1_mantissa+1 + sta FAC1_mantissa+1 + + lda FAC2_mantissa+0 + adc FAC1_mantissa+0 + sta FAC1_mantissa+0 + + bcc add_FAC2_FAC1_normalize // end if no need to adapt the exponent + + inc FAC1_exponent + bne add_FAC2_FAC1_normalize // end if no overflow + + // Overflow - XXX shouldnt we mark it somewhere in memory? + + lda #$FF + sta FAC1_exponent + sta FAC1_mantissa+3 + sta FAC1_mantissa+2 + sta FAC1_mantissa+1 + sta FAC1_mantissa+0 + + // FALLTROUGH + +add_FAC2_FAC1_done: + + rts + +add_FAC2_FAC1_sub: + + // Perform the subtration + + sec + lda FAC1_mantissa+3 + sbc FAC2_mantissa+3 + sta FAC1_mantissa+3 + + lda FAC1_mantissa+2 + sbc FAC2_mantissa+2 + sta FAC1_mantissa+2 + + lda FAC1_mantissa+1 + sbc FAC2_mantissa+1 + sta FAC1_mantissa+1 + + lda FAC1_mantissa+0 + sbc FAC2_mantissa+0 + sta FAC1_mantissa+0 + + bcs add_FAC2_FAC1_normalize // end if no need to adapt the exponent + + dec FAC1_exponent + + // FALLTROUGH + +add_FAC2_FAC1_normalize: + + jmp normal_FAC1 diff --git a/c64/basic/,stubs_math/b86a.interop b/c64/basic/math/b86a.interop similarity index 100% rename from c64/basic/,stubs_math/b86a.interop rename to c64/basic/math/b86a.interop