From e56e1cf1ac91aba455412ba155fc273d40871a97 Mon Sep 17 00:00:00 2001 From: Rikard Nordgren Date: Mon, 30 Mar 2026 21:36:38 +0200 Subject: [PATCH] Update round_FAC1 --- STATUS.md | 2 +- src/basic/math/bc1b.round_FAC1.jmp.s | 10 ---- src/basic/math/bc1b.round_FAC1.s | 33 +++++++++++++ src/basic/math/round_FAC1.s | 61 ------------------------- src/basic/math_mov/bc0f.mov_FAC1_FAC2.s | 1 + tools/similarity.c | 5 +- 6 files changed, 37 insertions(+), 75 deletions(-) delete mode 100644 src/basic/math/bc1b.round_FAC1.jmp.s create mode 100644 src/basic/math/bc1b.round_FAC1.s delete mode 100644 src/basic/math/round_FAC1.s diff --git a/STATUS.md b/STATUS.md index c251fc18..1b8b17ef 100644 --- a/STATUS.md +++ b/STATUS.md @@ -237,7 +237,7 @@ Floating point mathematical routines - not official, but well known and broadly | `$BBFC` | `mov_FAC2_FAC1` | DONE | From the Microsoft BASIC, original name: MOVFA | | `$BC0C` | `mov_r_FAC1_FAC2` | DONE | From the Microsoft BASIC, original name: MOVAF | | `$BC0F` | `mov_FAC1_FAC2` | DONE | From the Microsoft BASIC, original name: MOVEF | -| `$BC1B` | `round_FAC1` | PARTIAL | not fully tested yet | +| `$BC1B` | `round_FAC1` | DONE | From the Microsoft BASIC, original name: ROUND | | `$BC2B` | `sgn_FAC1_A` | DONE | From the Microsoft BASIC, original name: SIGN | | `$BC39` | `sgn_FAC1` | DONE | From the Microsoft BASIC, original name: SGN | | `$BC3C` | `convert_A_to_FAC1` | DONE | From the Microsoft BASIC, original name: FLOAT | diff --git a/src/basic/math/bc1b.round_FAC1.jmp.s b/src/basic/math/bc1b.round_FAC1.jmp.s deleted file mode 100644 index 81b1ef11..00000000 --- a/src/basic/math/bc1b.round_FAC1.jmp.s +++ /dev/null @@ -1,10 +0,0 @@ -;; #LAYOUT# STD * #TAKE -;; #LAYOUT# X16 BASIC_0 #TAKE-OFFSET 2000 -;; #LAYOUT# * BASIC_0 #TAKE -;; #LAYOUT# * * #IGNORE - -; Just a compatibility jump - our routine is longer than original one - -round_FAC1: - jmp round_FAC1_impl - diff --git a/src/basic/math/bc1b.round_FAC1.s b/src/basic/math/bc1b.round_FAC1.s new file mode 100644 index 00000000..bb7911b6 --- /dev/null +++ b/src/basic/math/bc1b.round_FAC1.s @@ -0,0 +1,33 @@ +;; #LAYOUT# STD * #TAKE +;; #LAYOUT# X16 BASIC_0 #TAKE-OFFSET 2000 +;; #LAYOUT# * BASIC_0 #TAKE +;; #LAYOUT# * * #IGNORE + + + +; This file is under the MIT license, it contains code released by Microsoft Corporation. +; See LICENSE for more information. + +; Math package - rounds FAC1 before copying +; +; This is verified to be identical to the original Microsoft implementation where it was named ROUND. +; +; Alternative entry point: INCRND to increment mantissa and normalize if carry +; +; See also: +; - [CM64] Computes Mapping the Commodore 64 - page 115 +; - https://codebase64.org/doku.php?id=base:kernal_floating_point_mathematics +; + + +round_FAC1: + + lda FAC1_exponent ; Zero? + beq MOVRTS ; Yes. Done rounding + asl FACOV ; ROUND? + bcc MOVRTS ; No. MSB off. +INCRND: + jsr inc_FAC1 ; Yes, add one to LSB + bne MOVRTS ; No carry means done + jmp RNDSHF ; Squeeze MSB in and RTS + ; CF=1 since inc_FAC1 doesn't touch CF diff --git a/src/basic/math/round_FAC1.s b/src/basic/math/round_FAC1.s deleted file mode 100644 index e8437d57..00000000 --- a/src/basic/math/round_FAC1.s +++ /dev/null @@ -1,61 +0,0 @@ -;; #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_FAC1_impl: - - ; If number is 0, do not do anything - - lda FAC1_exponent - beq round_FAC1_done - - lda FACOV - bpl round_FAC1_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_FAC1_zero_FACOV - inc FAC1_mantissa+2 - bne round_FAC1_zero_FACOV - inc FAC1_mantissa+1 - bne round_FAC1_zero_FACOV - inc FAC1_mantissa+0 - bne round_FAC1_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_FAC1_zero_FACOV - - ; If we are here, it means the exponent wrapped around to $00; put it back to $FF - - dec FAC1_exponent - - ; FALLTROUGH - -round_FAC1_zero_FACOV: - - lda #$00 - sta FACOV - - ; FALLTROUGH - -round_FAC1_done: - - rts diff --git a/src/basic/math_mov/bc0f.mov_FAC1_FAC2.s b/src/basic/math_mov/bc0f.mov_FAC1_FAC2.s index 65c69a65..ebc56341 100644 --- a/src/basic/math_mov/bc0f.mov_FAC1_FAC2.s +++ b/src/basic/math_mov/bc0f.mov_FAC1_FAC2.s @@ -38,4 +38,5 @@ mov_FAC1_FAC2: dex bne @1 stx FACOV ; Always zero +MOVRTS: rts diff --git a/tools/similarity.c b/tools/similarity.c index bd3067cc..07bfbee6 100644 --- a/tools/similarity.c +++ b/tools/similarity.c @@ -20,7 +20,7 @@ int matches[MAX_SIZE]; int phase_mask[MAX_SIZE+MAX_SIZE]; -#define NUM_CHUNKS 7 +#define NUM_CHUNKS 6 // Pairs of start, end memory addresses of known exact similarity with the open source Microsoft BASIC unsigned short microsoft_basic_chunks[NUM_CHUNKS * 2] = { 0xB391, 0xB39D, @@ -28,8 +28,7 @@ unsigned short microsoft_basic_chunks[NUM_CHUNKS * 2] = { 0xB849, 0xB97D, 0xB983, 0xB9BB, 0xBA8C, 0xBAB6, - 0xBB8F, 0xBC1A, - 0xBC2B, 0xBCF2, + 0xBB8F, 0xBCF2, };