From 5b8dc457ab45b8a2f3300d436678a5da987d053c Mon Sep 17 00:00:00 2001 From: Rikard Nordgren Date: Thu, 29 Jan 2026 20:23:59 +0100 Subject: [PATCH] Add shiftr_FAC --- STATUS.md | 1 + src/basic/math/b983.shiftr_FAC.s | 65 ++++++++++++++++++++++++++++++++ tools/similarity.c | 3 +- 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 src/basic/math/b983.shiftr_FAC.s diff --git a/STATUS.md b/STATUS.md index 0e484ed3..d670befd 100644 --- a/STATUS.md +++ b/STATUS.md @@ -213,6 +213,7 @@ Floating point mathematical routines - not official, but well known and broadly | `$B8FE` | `normal_FAC1` | DONE | | | `$B947` | `inv_FAC1_mantissa` | DONE | From the Microsoft BASIC, original name: NEGFAC | | `$B96F` | `inc_FAC1` | DONE | From the Microsoft BASIC, original name: INCFAC | +| `$B983` | `shiftr_FAC1` | DONE | From the Microsoft BASIC, original name: SHIFTR | | `$B9EA` | `log_FAC1` | PARTIAL | not fully tested yet | | `$BA28` | `mul_MEM_FAC1` | DONE | | | `$BA2B` | `mul_FAC2_FAC1` | DONE | | diff --git a/src/basic/math/b983.shiftr_FAC.s b/src/basic/math/b983.shiftr_FAC.s new file mode 100644 index 00000000..6cf67738 --- /dev/null +++ b/src/basic/math/b983.shiftr_FAC.s @@ -0,0 +1,65 @@ +;; #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 - shift a floating point register right +; +; The main entry point is shiftr_FAC which shifts any FAC to the right with sign extension +; Inputs: +; - A is the negative of the number of bits to shift +; - X points to to the bytes to be shifted +; Returns: +; - Y = 0 +; - CF = 0 +; - A = overflowed bits +; +; Alternative entry point shift_for_mul_RES will shift the RES register for multiplication. +; Alternative entry point short_shift_FAC will makes a short shift without sign extension +; +; This is verified to be identical to the original Microsoft implementation where it was named SHIFTR. +; + +shift_for_mul_RES: + ldx #RESHO-1 + +.bytes: + ldy 4,X ; Shift entire bytes + sty FACOV + ldy 3,X + sty 4,X + ldy 2,X + sty 3,X + ldy 1,X + sty 2,X + ldy BITS + sty 1,X + +shiftr_FAC: + adc #8 + bmi .bytes ; More than or equal to 8 bits remaining + beq .bytes + sbc #8 ; Go back to previous value + tay + lda FACOV + bcs shiftr_FAC +.sext: + asl 1,X + bcc @2 + inc 1,X +@2: + ror 1,X ; Undo previous shift + ror 1,X +short_shift_FAC: + ror 2,X + ror 3,X + ror 4,X + ror + iny + bne .sext +shftrt: + clc + rts diff --git a/tools/similarity.c b/tools/similarity.c index 66de278b..bfeabe25 100644 --- a/tools/similarity.c +++ b/tools/similarity.c @@ -20,11 +20,12 @@ int matches[MAX_SIZE]; int phase_mask[MAX_SIZE+MAX_SIZE]; -#define NUM_CHUNKS 4 +#define NUM_CHUNKS 5 // Pairs of start, end memory addresses of known exact similarity with the open source Microsoft BASIC unsigned short microsoft_basic_chunks[NUM_CHUNKS * 2] = { 0xB849, 0xB84F, 0xB947, 0xB97D, + 0xB983, 0xB9BB, 0xBBA2, 0xBC1A, 0xBC58, 0xBC5A };