added exponent alignment routine, fix for FAC1 normalization

This commit is contained in:
FeralChild64 committed 2020-02-03 21:37:27 +01:00
1 parent c62f53058e
commit b0d0177114
8 files changed
+259 -85

No files matched your search

+169
View File
@@ -0,0 +1,169 @@
// #LAYOUT# STD * #TAKE
// #LAYOUT# * BASIC_0 #TAKE
// #LAYOUT# * * #IGNORE
//
// Math package - align FAC1 and FAC2 exponents for addition
//
// See also:
// - [CM64] Computes Mapping the Commodore 64 - page 115
// - https://www.c64-wiki.com/wiki/BASIC-ROM
// XXX finish and test this
add_align_exponents:
// XXX assume here none of the exponents is 0, check if this is true for orignal ROMs
lda FAC1_exponent
cmp FAC2_exponent
beq add_align_exponents_done // branch if exponents the same
bcs add_align_exponent_FAC2
// FALLTROUGH
add_align_exponent_FAC1:
// First check the exponent difference
lda FAC2_exponent
sec
sbc FAC1_exponent
cmp #$28
bcs add_align_exponent_FAC1_zero // branch if difference is very high
cmp #$08
bcc add_align_exponent_FAC1_bit
// FALLTROUGH
add_align_exponent_FAC1_byte:
// Add 8 to exponent, divide mantissa by 256
lda FAC1_exponent
clc
adc #$08
sta FAC1_exponent
lda FAC1_mantissa+3
sta FACOV
lda FAC1_mantissa+2
sta FAC1_mantissa+3
lda FAC1_mantissa+1
sta FAC1_mantissa+2
lda FAC1_mantissa+0
sta FAC1_mantissa+1
lda #$00
sta FAC1_mantissa+0
beq add_align_exponent_FAC1 // branch always - next iteration
add_align_exponent_FAC1_bit:
// Increment exponent, divide mantissa by 2
inc FAC1_exponent
clc
ror FAC1_mantissa+0
ror FAC1_mantissa+1
ror FAC1_mantissa+2
ror FAC1_mantissa+3
ror FACOV
lda FAC1_exponent
cmp FAC2_exponent
bne add_align_exponent_FAC1_bit // branch if next iteration needed
rts
add_align_exponent_FAC1_zero:
lda #$00
sta FAC1_mantissa+0
sta FAC1_mantissa+1
sta FAC1_mantissa+2
sta FAC1_mantissa+3
sta FACOV
lda FAC2_exponent
sta FAC1_exponent
// FALLTROUGH
add_align_exponents_done:
rts
add_align_exponent_FAC2:
// First check the exponent difference, Carry already set here
lda FAC1_exponent
sbc FAC2_exponent
cmp #$20
bcs add_align_exponent_FAC2_zero // branch if difference is very high
cmp #$08
bcc add_align_exponent_FAC2_bit
// FALLTROUGH
add_align_exponent_FAC2_byte:
// Add 8 to exponent, divide mantissa by 256
lda FAC2_exponent
clc
adc #$08
sta FAC2_exponent
lda FAC2_mantissa+2
sta FAC2_mantissa+3
lda FAC2_mantissa+1
sta FAC2_mantissa+2
lda FAC2_mantissa+0
sta FAC2_mantissa+1
lda #$00
sta FAC2_mantissa+0
beq add_align_exponent_FAC2 // branch always - next iteration
add_align_exponent_FAC2_bit:
// Increment exponent, divide mantissa by 2
inc FAC2_exponent
clc
ror FAC2_mantissa+0
ror FAC2_mantissa+1
ror FAC2_mantissa+2
ror FAC2_mantissa+3
lda FAC1_exponent
cmp FAC2_exponent
bne add_align_exponent_FAC2_bit // branch if next iteration needed
rts
add_align_exponent_FAC2_zero:
lda #$00
sta FAC2_mantissa+0
sta FAC2_mantissa+1
sta FAC2_mantissa+2
sta FAC2_mantissa+3
lda FAC1_exponent
sta FAC2_exponent
rts
@@ -0,0 +1,8 @@
// #LAYOUT# STD * #TAKE
// #LAYOUT# * BASIC_0 #TAKE
// #LAYOUT# * * #IGNORE
// Just a compatibility jump - our routine is longer than original one
jmp add_align_exponents
+3
View File
@@ -0,0 +1,3 @@
This is an important floating point math routine, documented in many places.
XXX - try to find real-life example
+2 -5
View File
@@ -19,11 +19,8 @@
mov_FAC1_MEM:
// Checked in the available documentation:
// - https://www.c64-wiki.com/wiki/Floating_point_arithmetic
// - https://www.lemon64.com/forum/viewtopic.php?t=67576&sid=d41e77cdddd28d6f9c9d52ce5b4e8dc3
// that $22/$23 (INDEX+0 and INDEX+1) are at least sometimes used for pointer storage.
// Confirmed by experimenting with original ROM that this is the case here too.
// Original routines use $22/$23 location for a vector too, see here:
// - https://codebase64.org/doku.php?id=base:kernal_floating_point_mathematics
sty INDEX+1
stx INDEX+0
+2 -5
View File
@@ -23,11 +23,8 @@
mov_MEM_FAC1:
// Checked in the available documentation:
// - https://www.c64-wiki.com/wiki/Floating_point_arithmetic
// - https://www.lemon64.com/forum/viewtopic.php?t=67576&sid=d41e77cdddd28d6f9c9d52ce5b4e8dc3
// that $22/$23 (INDEX+0 and INDEX+1) are at least sometimes used for pointer storage.
// Confirmed by experimenting with original ROM that this is the case here too.
// Original routines use $22/$23 location for a vector too, see here:
// - https://codebase64.org/doku.php?id=base:kernal_floating_point_mathematics
sty INDEX+1
sta INDEX+0
+2 -5
View File
@@ -26,11 +26,8 @@
mov_MEM_FAC2:
// Checked in the available documentation:
// - https://www.c64-wiki.com/wiki/Floating_point_arithmetic
// - https://www.lemon64.com/forum/viewtopic.php?t=67576&sid=d41e77cdddd28d6f9c9d52ce5b4e8dc3
// that $22/$23 (INDEX+0 and INDEX+1) are at least sometimes used for pointer storage.
// Confirmed by experimenting with original ROM that this is the case here too.
// Original routines use $22/$23 location for a vector too, see here:
// - https://codebase64.org/doku.php?id=base:kernal_floating_point_mathematics
sty INDEX+1
sta INDEX+0
+25 -23
View File
@@ -16,30 +16,32 @@
normal_FAC1:
lda FAC1_exponent
beq normal_FAC1_end // branch if our float is 0
beq normal_FAC1_end // branch if our float is 0
lda FAC1_mantissa+3
beq normal_FAC1_by_byte // branch if we can move the whole byte
// FALLTROUGH
normal_FAC1_check_mantissa_byte:
lda FAC1_mantissa+0
beq normal_FAC1_by_byte // branch if we can move the whole byte
// FALLTROUGH
normal_FAC1_check_mantissa_bit:
lda FAC1_mantissa+0 // XXX optimize for 65CE02?
and #$80
bne normal_FAC1_end // branch if already normalized
bne normal_FAC1_end // branch if already normalized
// FALLTROUGH
normal_FAC1_by_bit:
// First check the current exponent
// Decrement the exponent
lda FAC1_exponent
cmp #$01
bne !+
// Exponent is 1 - just mark the whole number as 0 and finish
lda #$00
sta FAC1_exponent
rts
!:
dec FAC1_exponent
beq normal_FAC1_end // exponent reached 0, nothing more to do
// Multiply the mantissa by 2
clc
@@ -49,7 +51,7 @@ normal_FAC1_by_bit:
rol FAC1_mantissa+1
rol FAC1_mantissa+0
bcc normal_FAC1 // branch if next iteration needed
bcc normal_FAC1_check_mantissa_bit // branch if next iteration needed
rts
normal_FAC1_by_byte:
@@ -63,19 +65,19 @@ normal_FAC1_by_byte:
lda #$00
!:
sta FAC1_exponent
beq normal_FAC1_end // branch if our float reached 0
beq normal_FAC1_end // branch if our float reached 0
// Now multiply the mantissa by 8 by moving the whole bytes
lda FAC1_mantissa+2
sta FAC1_mantissa+3
lda FAC1_mantissa+1
sta FAC1_mantissa+2
lda FAC1_mantissa+0
sta FAC1_mantissa+0
lda FAC1_mantissa+2
sta FAC1_mantissa+1
lda FAC1_mantissa+3
sta FAC1_mantissa+2
lda FACOV
sta FAC1_mantissa+0
sta FAC1_mantissa+3
lda #$00
sta FACOV
@@ -87,7 +89,7 @@ normal_FAC1_by_byte:
ora FAC1_mantissa+2
ora FAC1_mantissa+3
bne normal_FAC1 // not 0 - branch to next iteration
bne normal_FAC1_check_mantissa_byte // not 0 - branch to next iteration
// Our mantissa reached 0 - mark exponent as 0 and finish