2 ; Fixed point cosine/sine functions.
4 ; Returns the cosine/sine for the given argument as angular degree.
5 ; Valid argument range is 0..360 for both functions. They will return
6 ; garbage if the argument is not in a valid range. Result is in 8.8 fixed
7 ; point format, so $100 is 1.0 and $FF00 is -1.0.
10 ; Ullrich von Bassewitz, 2009-10-29
13 .export _cc65_cos, _cc65_sin
16 ; ---------------------------------------------------------------------------
17 ; Sinus table covering values from 0..86° as 0.8 fixed point values. Values
18 ; for 87..90° are actually 1.0 (= $100), will therefore not fit in the table
19 ; and are covered specially in the code below.
24 .byte $00, $04, $09, $0D, $12, $16, $1B, $1F, $24, $28
25 .byte $2C, $31, $35, $3A, $3E, $42, $47, $4B, $4F, $53
26 .byte $58, $5C, $60, $64, $68, $6C, $70, $74, $78, $7C
27 .byte $80, $84, $88, $8B, $8F, $93, $96, $9A, $9E, $A1
28 .byte $A5, $A8, $AB, $AF, $B2, $B5, $B8, $BB, $BE, $C1
29 .byte $C4, $C7, $CA, $CC, $CF, $D2, $D4, $D7, $D9, $DB
30 .byte $DE, $E0, $E2, $E4, $E6, $E8, $EA, $EC, $ED, $EF
31 .byte $F1, $F2, $F3, $F5, $F6, $F7, $F8, $F9, $FA, $FB
32 .byte $FC, $FD, $FE, $FE, $FF, $FF, $FF
36 ; ---------------------------------------------------------------------------
37 ; Cosine function. Is actually implemented as cos(x) = sin(x+90)
50 ; If x is now larger than 360, we need to subtract 360.
62 ; ---------------------------------------------------------------------------
65 ; table lookup for 0..89°
66 ; sin(x) = sin(180-x) for 90°..179°
67 ; sin(x) = -sin(x-180) for 180..360°
69 ; Plus special handling for the values missing in the table.
73 ; If the high byte is non zero, argument is > 255
85 ; 90..179°. Value is identical to sin(180-val). Carry is set on entry.
87 ; 180-val := -val + 180.
89 ; -val := (val ^ $FF) + 1
91 ; 180-val = (val ^ $FF) + 1 + 180
92 ; Since carry is set, we can drop the "+ 1".
98 ; 0..89°. Values for 87..90° are actually 1.0. Since this format doesn't fit
99 ; into the table, we have to check for it manually.
110 ; 0..86°. Read the value from the table.
117 ; 180..360°. sin(x) = -sin(x-180). Since the argument is in range 0..180
118 ; after the subtraction, we don't need to handle the high byte.
126 ; 270..360°. Value is identical to -sin(180-val). Carry is set on entry.
128 ; 180-val := -val + 180.
130 ; -val := (val ^ $FF) + 1
132 ; 180-val = (val ^ $FF) + 1 + 180
133 ; Since carry is set, we can drop the "+ 1".
139 ; 180..269°. Values for 267..269° are actually -1.0. Since this format doesn't
140 ; fit into the table, we have to check for it manually.
151 ; 180..266°. Read the value from the table. Carry is clear on entry.