From 85f921a17263d8688f8cf8dbe7d58611c8b863d4 Mon Sep 17 00:00:00 2001 From: Rikard Nordgren Date: Tue, 14 Apr 2026 19:43:03 +0200 Subject: [PATCH] Update tan_FAC1 --- STATUS.md | 2 +- src/aliases/,aliases_ram_lowmem.s | 3 ++- src/basic/math_trans/e2b4.tan_FAC1.s | 40 +++++++++++++++++++--------- tools/similarity.c | 5 ++-- 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/STATUS.md b/STATUS.md index 4bf4dc02..e9093411 100644 --- a/STATUS.md +++ b/STATUS.md @@ -264,7 +264,7 @@ Floating point mathematical routines - not official, but well known and broadly | `$E0BE` | `rnd_generate` | DONE | | | `$E264` | `cos_FAC1` | DONE | From the Microsoft BASIC, original name: COS | | `$E26B` | `sin_FAC1` | DONE | From the Microsoft BASIC, original name: SIN | -| `$E2B4` | `tan_FAC1` | PARTIAL | not fully tested yet | +| `$E2B4` | `tan_FAC1` | DONE | From the Microsoft BASIC, original name: TAN | | `$E30E` | `atn_FAC1` | DONE | From the Microsoft BASIC, original name: ATN |
diff --git a/src/aliases/,aliases_ram_lowmem.s b/src/aliases/,aliases_ram_lowmem.s index d0fb307e..d221f916 100644 --- a/src/aliases/,aliases_ram_lowmem.s +++ b/src/aliases/,aliases_ram_lowmem.s @@ -53,7 +53,8 @@ !addr FORPNT = $49 ; $49-$4A -- NOT IMPLEMENTED -- !addr OPPTR = $4B ; $4B-$4C helper variable for expression computation, [!] our usage details are different !addr OPMASK = $4D ; -- NOT IMPLEMENTED -- - !addr DEFPNT = $4E ; $4E-$4F -- NOT IMPLEMENTED -- + !addr DEFPNT = $4E ; $4E-$4F + !addr TEMPF3 = DEFPNT !addr DSCPNT = $50 ; $50-$52 temporary area fro string handling, [!] our usage might differ !addr FOUR6 = $53 ; size of variable content (float = 5, integer = 2, string descriptor = 3) !addr JMPER = $54 ; $54-$56 -- NOT IMPLEMENTED -- diff --git a/src/basic/math_trans/e2b4.tan_FAC1.s b/src/basic/math_trans/e2b4.tan_FAC1.s index db9277ee..59c52d72 100644 --- a/src/basic/math_trans/e2b4.tan_FAC1.s +++ b/src/basic/math_trans/e2b4.tan_FAC1.s @@ -2,9 +2,14 @@ ;; #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 - tangent of FAC1 in radians ; +; This is verified to be almost identical to the original Microsoft implementation where it was named TAN. +; The difference is the optimization of the jsr to mov_r_FAC1_MEM +; ; See also: ; - [CM64] Computes Mapping the Commodore 64 - page 210 ; - https://www.c64-wiki.com/wiki/Floating_point_arithmetic @@ -18,17 +23,28 @@ tan_FAC1: - jsr mov_r_FAC1_TMP1 ; TMP1 = x - jsr sin_FAC1 ; FAC1 = sin(x) - jsr mov_r_FAC1_TMP2 ; TMP2 = sin(x) - lda #TEMPF1 - ldy #0 - jsr mov_MEM_FAC1 ; FAC1 = x - jsr COS ; FAC1 = cos(x) - lda #TEMPF2 - ldy #0 - jsr mov_MEM_FAC2 ; FAC2 = sin(x) - jmp div_FAC2_FAC1 ; FAC1 = tan(x) + + jsr mov_r_FAC1_TMP1 ; Mov FAC1 into temporary + lda #$00 + sta TANSGN ; Remember whether to negate + jsr sin_FAC1 ; Compute the sin + ldx #TEMPF3 + jsr mov_r_FAC1_MEM ; In the original BASIC this does jsr GMOVMF that jmps to mov_r_FAC1_MEM + lda #TEMPF1 + jsr mov_MEM_FAC1 ; Put this memory loc into FAC1 + lda #$00 + sta FAC1_sign ; Start off positive + lda TANSGN + jsr COSC ; Compute cosine + lda #TEMPF3 +GFDIV: + jmp div_MEM_FAC1 ; Divide sine by cosine and return +COSC: + pha + jmp SIN1 } else { diff --git a/tools/similarity.c b/tools/similarity.c index 7d4ba857..e2144418 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 11 +#define NUM_CHUNKS 12 // 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, @@ -32,7 +32,8 @@ unsigned short microsoft_basic_chunks[NUM_CHUNKS * 2] = { 0xBDB3, 0xBDC1, 0xBDDD, 0xBF51, 0xBFB4, 0xBFEC, - 0xE264, 0xE2B3, + 0xE264, 0xE2C2, // Two byte gap here is an optimization + 0xE2C5, 0xE2DF, 0xE2EF, 0xE37A, };