diff --git a/STATUS.md b/STATUS.md index 236285dc..f21b68a8 100644 --- a/STATUS.md +++ b/STATUS.md @@ -179,17 +179,17 @@ Floating point mathematical routines - not official, but well known and broadly | `$B1AA` | `FACINX` | NOT DONE | convert FAC1 to 16-bit signed integer | | `$B391` | `GIVAYF` | NOT DONE | XXX - no stub added yet | | `$B7B5` | `STRVAL` | NOT DONE | XXX - no stub added yet | -| `$B850` | `FSUB` | PARTIAL | needs `CONUPK` and `FSUBT` | +| `$B850` | `FSUB` | PARTIAL | needs `FSUBT` | | `$B853` | `FSUBT` | NOT DONE | subtract FAC1 from FAC2 | -| `$B867` | `FADD` | PARTIAL | needs `CONUPK` and `FADDT` | +| `$B867` | `FADD` | PARTIAL | needs `FADDT` | | `$B86A` | `FADDT` | NOT DONE | add FAC2 to FAC1 | | `$B9EA` | `LOG` | NOT DONE | XXX - no stub added yet | -| `$BA28` | `FMUL` | PARTIAL | needs `CONUPK` and `FMULT` | +| `$BA28` | `FMUL` | PARTIAL | needs `FMULT` | | `$BA2B` | `FMULT` | NOT DONE | multiplies FAC1 by FAC2 | -| `$BA8C` | `CONUPK` | NOT DONE | fetch FAC2 from RAM location | +| `$BA8C` | `CONUPK` | PARTIAL | not tested yet | | `$BAE2` | `MUL10` | NOT DONE | multiply FAC1 by 10 | | `$BAFE` | `DIV10` | NOT DONE | divide FAC1 by 10, result is always positive | -| `$BB0F` | `FDIV` | PARTIAL | needs `CONUPK` and `FDIVT` | +| `$BB0F` | `FDIV` | PARTIAL | needs `FDIVT` | | `$BB12` | `FDIVT` | NOT DONE | divide FAC2 by FAC1, ignores sign | | `$BBA2` | `MOVFM` | NOT DONE | fetch FAC1 from RAM location | | `$BBD4` | `MOVMF` | NOT DONE | copy FAC1 to memory location, rounds if asked | diff --git a/c64/basic/,stubs_math/ba8c.CONUPK.s b/c64/basic/,stubs_math/ba8c.CONUPK.s index f31ee902..9b042643 100644 --- a/c64/basic/,stubs_math/ba8c.CONUPK.s +++ b/c64/basic/,stubs_math/ba8c.CONUPK.s @@ -10,7 +10,11 @@ // - .Y - address high byte // // Output: -// - .A - FAC1 exponent (XXX really FAC1?) +// - .A - FAC1 exponent (yes, really - Codebase64 is right) +// - .Y - 0 (tested with original ROMs) +// +// Preserves: +// - .X (tested with original ROMs) // // See also: // - [CM64] Computes Mapping the Commodore 64 - page 114 @@ -18,7 +22,61 @@ // - https://codebase64.org/doku.php?id=base:kernal_floating_point_mathematics // -// XXX provide implementation +// XXX test this CONUPK: - STUB_IMPLEMENTATION() + + // Noticed in the avilable materials: + // - https://www.c64-wiki.com/wiki/Floating_point_arithmetic + // - https://www.lemon64.com/forum/viewtopic.php?t=67576&sid=d41e77cdddd28d6f9c9d52ce5b4e8dc3 + // that at least some math routines use $22/$23 (INDEX+0 and INDEX+1) for pointer storage, + // confirmed that this routine is not an exception + + sty INDEX+1 + sta INDEX+0 + + // 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 + + ldy #$04 + + // Copy the mantissa - reverse byte order + + ldy #$04 + lda (INDEX), y + sta FAC2_mantissa+3 + + dey + lda (INDEX), y + sta FAC2_mantissa+2 + + dey + lda (INDEX), y + sta FAC2_mantissa+1 + + dey + lda (INDEX), y + ora #$80 // clear the sign bit + sta FAC2_mantissa+0 + + // Copy the sign + + lda (INDEX), y + bpl !+ // assumption: non-negative numbers are more frequent + lda #$FF + skip_2_bytes_trash_nvz +!: + lda $00 + + sta FAC2_sign + + // Copy the exponent + + dey + lda (INDEX), y + sta FAC2_exponent + + // Return the FAC1 exponent + + lda FAC1_exponent + rts diff --git a/c64/basic/math/ba8c.CONUPK.s b/c64/basic/math/ba8c.CONUPK.s new file mode 100644 index 00000000..9583c4a9 --- /dev/null +++ b/c64/basic/math/ba8c.CONUPK.s @@ -0,0 +1,82 @@ +// #LAYOUT# STD * #TAKE +// #LAYOUT# * BASIC_0 #TAKE +// #LAYOUT# * * #IGNORE + +// +// Math package - fetch FAC2 from RAM location +// +// Input: +// - .A - address low byte +// - .Y - address high byte +// +// Output: +// - .A - FAC1 exponent (yes, really - Codebase64 is right) +// - .Y - XXX 0? +// +// Preserves: +// - .X - (experimentation with original routine) +// +// See also: +// - [CM64] Computes Mapping the Commodore 64 - page 114 +// - https://www.c64-wiki.com/wiki/Floating_point_arithmetic +// - https://codebase64.org/doku.php?id=base:kernal_floating_point_mathematics +// + +// XXX test this + +CONUPK: + + // Not sure which temporary variables are used for storage by the original ROMs, + // but $22/$23 (INDEX+0 and INDEX+1) seem to be good candidates, at least some + // math routines use them as pointer storage, see: + // - https://www.c64-wiki.com/wiki/Floating_point_arithmetic + // - https://www.lemon64.com/forum/viewtopic.php?t=67576&sid=d41e77cdddd28d6f9c9d52ce5b4e8dc3 + + sty INDEX+1 + sta INDEX+0 + + // 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 + + ldy #$04 + + // Copy the mantissa - reverse byte order + + ldy #$04 + lda (INDEX), y + sta FAC2_mantissa+3 + + dey + lda (INDEX), y + sta FAC2_mantissa+2 + + dey + lda (INDEX), y + sta FAC2_mantissa+1 + + dey + lda (INDEX), y + ora #$80 // clear the sign bit + sta FAC2_mantissa+0 + + // Copy the sign + + lda (INDEX), y + bpl !+ // assumption: non-negative numbers are more frequent + lda #$FF + skip_2_bytes_trash_nvz +!: + lda $00 + + sta FAC2_sign + + // Copy the exponent + + dey + lda (INDEX), y + sta FAC2_exponent + + // Return the FAC1 exponent + + lda FAC1_exponent + rts diff --git a/c64/basic/,stubs_math/ba8c.interop b/c64/basic/math/ba8c.interop similarity index 100% rename from c64/basic/,stubs_math/ba8c.interop rename to c64/basic/math/ba8c.interop