annotate src/math.asm @ 618:7b3903536213

work on new battery menu
author heinrichsweikamp
date Mon, 04 Feb 2019 22:57:24 +0100
parents ca4556fb60b9
children c40025d8e750
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
heinrichsweikamp
parents:
diff changeset
1 ;=============================================================================
heinrichsweikamp
parents:
diff changeset
2 ;
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
3 ; File math.asm REFACTORED VERSION V2.99d
0
heinrichsweikamp
parents:
diff changeset
4 ;
heinrichsweikamp
parents:
diff changeset
5 ; Math subroutines
heinrichsweikamp
parents:
diff changeset
6 ;
heinrichsweikamp
parents:
diff changeset
7 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved.
heinrichsweikamp
parents:
diff changeset
8 ;=============================================================================
heinrichsweikamp
parents:
diff changeset
9 ; HISTORY
heinrichsweikamp
parents:
diff changeset
10 ; 2011-08-03 : [mH] moving from OSTC code
heinrichsweikamp
parents:
diff changeset
11
heinrichsweikamp
parents:
diff changeset
12
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
13 #include "hwos.inc" ; mandatory header
0
heinrichsweikamp
parents:
diff changeset
14
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
15 math CODE
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
16
0
heinrichsweikamp
parents:
diff changeset
17 ;=============================================================================
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
18
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
19 global convert_time ; converts hi:lo in minutes to hours (up:hi) and minutes (lo)
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
20 ; also usable for conversion of seconds to minutes and seconds
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
21 ; trashes xA, xB, xC
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
22 convert_time:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
23 movff lo,xA+0 ; copy hi:lo to xA
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
24 movff hi,xA+1 ;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
25 movlw d'60' ; set divisor to 60
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
26 movwf xB+0 ; write 60 to xB
0
heinrichsweikamp
parents:
diff changeset
27 clrf xB+1 ;
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
28 rcall div16x16 ; xC = xA / xB with xA as remainder
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
29 movff xC+1,up ; result is hours / minutes, copy to up (high byte)
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
30 movff xC+0,hi ; result is hours / minutes, copy to hi (low byte)
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
31 movff xA+0,lo ; remainder is minutes / seconds, copy to lo
0
heinrichsweikamp
parents:
diff changeset
32 return
heinrichsweikamp
parents:
diff changeset
33
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
34
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
35 global div16 ; divA:2 = divA:2 / 2^WREG
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
36 ; trashes WREG
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
37 div16:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
38 bcf STATUS,C ; clear carry
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
39 rrcf divA+1 ; rotate right high byte, carry into MSB, LSB into carry
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
40 rrcf divA+0 ; rotate right low byte, carry into MSB, LSB into carry
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
41 decfsz WREG ; decrement counter, done?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
42 bra div16 ; NO - loop
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
43 return ; YES - done
0
heinrichsweikamp
parents:
diff changeset
44
heinrichsweikamp
parents:
diff changeset
45
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
46 global mult16 ; xA:2 = xA:2 * 2^WREG
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
47 ; trashes WREG
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
48 mult16:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
49 bcf STATUS,C ; clear carry
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
50 rlcf divA+0,F ; rotate left low byte, carry into LSB, MSB into carry
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
51 rlcf divA+1,F ; rotate left high byte, carry into LSB, MSB into carry
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
52 decfsz WREG ; decrement counter, done?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
53 bra mult16 ; NO - loop
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
54 return ; YES - done
0
heinrichsweikamp
parents:
diff changeset
55
heinrichsweikamp
parents:
diff changeset
56
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
57 global sub16 ; sub_c:2 = sub_a:2 - sub_b:2 with SIGNED values
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
58 ; sets neg_flag if result is < 0
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
59 ; trashes WREG
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
60 sub16:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
61 bcf neg_flag ; clear flag which will indicate if result is negative
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
62 movf sub_b+0,W ; get value to be subtracted, low byte
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
63 subwf sub_a+0,W ; execute subtraction on low byte
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
64 movwf sub_c+0 ; copy result to output variable, low byte
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
65 movf sub_b+1,W ; get value to be subtracted, high byte
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
66 subwfb sub_a+1,W ; execute subtraction on high byte, considering borrow flag
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
67 movwf sub_c+1 ; copy result to output variable, high byte
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
68 btfss STATUS,N ; result negative ?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
69 return ; NO - result positive, done
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
70 bsf neg_flag ; YES - set flag
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
71 comf sub_c+1 ; - do a 16 bit sign change
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
72 negf sub_c+0 ;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
73 btfsc STATUS,C ; - carry to be propagated ?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
74 incf sub_c+1,F ; YES - do it
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
75 return ; - done
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
76
0
heinrichsweikamp
parents:
diff changeset
77
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
78 global subU16 ; sub_c:2 = sub_a:2 - sub_b:2 with UNSIGNED values
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
79 ; sets neg_flag if result is < 0
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
80 ; trashes WREG
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
81 subU16:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
82 bcf neg_flag ; clear flag which will indicate if result is negative
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
83 movf sub_b+0,W ; get Value to be subtracted, low byte
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
84 subwf sub_a+0,W ; execute subtraction on low byte
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
85 movwf sub_c+0 ; copy result to output variable, low byte
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
86 movf sub_b+1,W ; get value to be subtracted, high byte
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
87 subwfb sub_a+1,W ; execute subtraction on high byte, considering borrow flag
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
88 movwf sub_c+1 ; copy result to output variable, high byte
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
89 btfsc STATUS,C ; borrow to propagate (B == /CARRY) ?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
90 return ; NO - result positive, done
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
91 bsf neg_flag ; YES - set flag
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
92 comf sub_c+1 ; - do a 16 bit sign change
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
93 negf sub_c+0 ;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
94 btfsc STATUS,C ; - carry to be propagated ?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
95 incf sub_c+1,F ; YES - do it
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
96 return ; - done
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
97
0
heinrichsweikamp
parents:
diff changeset
98
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
99 global mult16x16 ; xC:4 = xA:2 * xB:2 with UNSIGNED values
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
100 ; trashes PRODL, PRODH, WREG
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
101 mult16x16:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
102 clrf xC+2 ; clear the high-order bits
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
103 clrf xC+3
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
104 ;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
105 movf xA+0,W ; do the "L" multiplication
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
106 mulwf xB+0
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
107 movff PRODL,xC+0 ; copy result to xC
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
108 movff PRODH,xC+1
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
109 ;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
110 movf xA+0,W ; do the "I" multiplication
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
111 mulwf xB+1
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
112 movf PRODL,W ; get the product's low byte...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
113 addwf xC+1,F ; ... and add it to xC+1
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
114 movf PRODH,W ; get the product's high byte...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
115 addwfc xC+2,F ; ... and add it to xC+2 obeying carry bit from xC+1
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
116 ;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
117 movf xA+1,W ; do the "O" multiplication
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
118 mulwf xB+0
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
119 movf PRODL,W ; get the product's low byte...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
120 addwf xC+1,F ; ... and add it to xC+1
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
121 movf PRODH,W ; get the product's high byte...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
122 addwfc xC+2,F ; ... and add it to xC+2 obeying carry bit from xC+1
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
123 clrf WREG ; clear WREG...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
124 addwfc xC+3,F ; ... add add it to xC+3 obeying carry bit from xC+2 (can only happen in "O" multiplication)
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
125 ;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
126 movf xA+1,W ; do the "F" multiplication
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
127 mulwf xB+1
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
128 movf PRODL,W ; get the product's low byte...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
129 addwf xC+2,F ; ... and add it to xC+2
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
130 movf PRODH,W ; get the product's high byte...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
131 addwfc xC+3,F ; ... and add it to xC+3 obeying carry bit from xC+2
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
132 return
0
heinrichsweikamp
parents:
diff changeset
133
heinrichsweikamp
parents:
diff changeset
134
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
135 global div16x16 ; xC:2 = xA:2 / xB:2 with xA as remainder
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
136 ; trashes WREG
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
137 div16x16:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
138 movlw .16 ; process 16 bits ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
139 movwf math_loop ; ... initialize loop counter
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
140 movff xA+0,xC+0 ; copy xA to xC
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
141 movff xA+1,xC+1 ; ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
142 clrf xA+0 ; clear xA, will now be used to hold the remainder
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
143 clrf xA+1 ; ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
144 div16x16_1:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
145 bcf STATUS,C ; clear carry flag to shift in a zero bit
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
146 rlcf xC+0,F ; shift left xC
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
147 rlcf xC+1,F ; ... shifting MSB out of xC...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
148 rlcf xA+0,F ; ... and into LSB of xA
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
149 rlcf xA+1,F ; ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
150 btfsc STATUS,C ; did the remainder overflow (carry set)?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
151 bra div16x16_2 ; YES - directly generate a result bit = 1
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
152 movf xB+0,W ; NO - compute remainder - divisor = xA - xB, trash result to WREG
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
153 subwf xA+0,W ; - ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
154 movf xB+1,W ; - ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
155 subwfb xA+1,W ; - ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
156 btfss STATUS,C ; - remainder < divisor (-> borrow flag set, equals carry flag cleared) ?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
157 bra div16x16_3 ; YES - result bit = 0, keep LSB of xC+0 being 0
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
158 div16x16_2:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
159 bsf xC+0,0 ; NO - result bit = 1, set LSB of xC+0 to 1
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
160 movf xB+0,W ; - subtract divisor from remainder "for real": xA = xA - xB
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
161 subwf xA+0,F ; - ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
162 movf xB+1,W ; - ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
163 subwfb xA+1,F ; - ...
0
heinrichsweikamp
parents:
diff changeset
164 div16x16_3:
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
165 decfsz math_loop,F ; decrement loop counter, all bits done?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
166 bra div16x16_1 ; NO - loop
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
167 return ; YES - done
0
heinrichsweikamp
parents:
diff changeset
168
heinrichsweikamp
parents:
diff changeset
169
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
170 global div32x16 ; xC:4 = xC:4 / xB:2 with xA as remainder
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
171 ; trashes WREG
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
172 div32x16:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
173 movlw .32 ; process 32 bits ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
174 movwf math_loop ; ... initialize loop counter
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
175 clrf xA+0 ; clear xA, will be used to hold the remainder
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
176 clrf xA+1 ; ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
177 div32x16_1:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
178 bcf STATUS,C ; clear carry flag to shift in a zero bit
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
179 rlcf xC+0,F ; shift left xC
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
180 rlcf xC+1,F ; ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
181 rlcf xC+2,F ; ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
182 rlcf xC+3,F ; ... shifting MSB out of xC...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
183 rlcf xA+0,F ; ... and into LSB of xA
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
184 rlcf xA+1,F ; ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
185 btfsc STATUS,C ; did the remainder overflow (carry set)?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
186 bra div32x16_2 ; YES - directly generate a result bit = 1
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
187 movf xB+0,W ; NO - compute remainder - divisor = xA - xB, trash result to WREG
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
188 subwf xA+0,W ; - ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
189 movf xB+1,W ; - ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
190 subwfb xA+1,W ; - ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
191 btfss STATUS,C ; - remainder < divisor (-> borrow flag set, equals carry flag cleared) ?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
192 bra div32x16_3 ; YES - result bit = 0, keep LSB of xC+0 being 0
0
heinrichsweikamp
parents:
diff changeset
193 div32x16_2:
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
194 bsf xC+0,0 ; NO - result bit = 1, set LSB of xC+0 to 1
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
195 movf xB+0,W ; - subtract divisor from remainder "for real": xA = xA - xB
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
196 subwf xA+0,F ; - ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
197 movf xB+1,W ; - ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
198 subwfb xA+1,F ; - ...
0
heinrichsweikamp
parents:
diff changeset
199 div32x16_3:
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
200 decfsz math_loop,F ; decrement loop counter, all bits done?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
201 bra div32x16_1 ; NO - loop
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
202 return ; YES - done
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
203
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
204
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
205 ;=============================================================================
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
206
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
207 ; ISR math functions
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
208
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
209 global isr_shift_C31 ; 24 bit shift, repeated WREG times, dedicated to a specific usage:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
210 ; Because less than 8 bits are shifted and only C[2:1] is needed,
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
211 ; we don't care what bit is inserted.
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
212 isr_shift_C31:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
213 rrcf isr_xC+3,F ; shift three bytes
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
214 rrcf isr_xC+2,F
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
215 rrcf isr_xC+1,F
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
216 decfsz WREG ; decrement loop counter, done?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
217 bra isr_shift_C31 ; NO - loop
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
218 return ; YES - done
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
219
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
220
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
221 global isr_unsigned_mult16x16 ; isr_xC = isr_xA * _isr_xB with UNSIGNED values
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
222 ; trashes PRODL, PRODH, WREG
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
223 isr_unsigned_mult16x16:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
224 movf isr_xA+0,W ; multiply a[0] * b[0]
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
225 mulwf isr_xB+0
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
226 movff PRODL,isr_xC+0 ; store product to c[1]:c[0]
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
227 movff PRODH,isr_xC+1
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
228 ;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
229 movf isr_xA+1,W ; multiply a[1] * b[1]
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
230 mulwf isr_xB+1
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
231 movff PRODL, isr_xC+2 ; store product to c[3]:c[2]
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
232 movff PRODH, isr_xC+3
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
233 ;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
234 movf isr_xA+0,W ; multiply a[0] * b[1]
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
235 mulwf isr_xB+1
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
236 movf PRODL,W ; add cross product to c[3]:c[2]:c[1]
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
237 addwf isr_xC+1,F
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
238 movf PRODH,W
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
239 addwfc isr_xC+2,F ; propagate carry
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
240 clrf WREG
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
241 addwfc isr_xC+3,F ; propagate carry
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
242 ;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
243 movf isr_xA+1,W ; multiply a[1] * b[0]
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
244 mulwf isr_xB+0
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
245 movf PRODL,W ; add cross product
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
246 addwf isr_xC+1,F
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
247 movf PRODH,W
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
248 addwfc isr_xC+2,F ; propagate carry
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
249 clrf WREG
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
250 addwfc isr_xC+3,F ; propagate carry
0
heinrichsweikamp
parents:
diff changeset
251 return
heinrichsweikamp
parents:
diff changeset
252
heinrichsweikamp
parents:
diff changeset
253
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
254 global isr_signed_mult16x16 ; isr_xC = isr_xA * _isr_xB with SIGNED values
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
255 ; trashes PRODL, PRODH, WREG
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
256 isr_signed_mult16x16:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
257 rcall isr_unsigned_mult16x16 ; do an unsigned multiplication first
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
258 ; ; manage sign extension of operand B
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
259 btfss isr_xB+1,7 ; is B negative ?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
260 bra isr_signed_mult_checkA ; NO - continue checking operand A
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
261 movf isr_xA+0,W ; Yes - add -65536 * A
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
262 subwf isr_xC+2,F
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
263 movf isr_xA+1,W
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
264 subwfb isr_xC+3,F
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
265 isr_signed_mult_checkA ; manage sign extension of operand B
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
266 btfss isr_xA+1,7 ; is A negative ?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
267 return ; NO - done
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
268 movf isr_xB+0,W ; Yes - add -65536 * B
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
269 subwf isr_xC+2,F
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
270 movf isr_xB+1,W
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
271 subwfb isr_xC+3,F
0
heinrichsweikamp
parents:
diff changeset
272 return
heinrichsweikamp
parents:
diff changeset
273
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
274 END