0
|
1 ; OSTC - diving computer code
|
|
2 ; Copyright (C) 2008 HeinrichsWeikamp GbR
|
|
3
|
|
4 ; This program is free software: you can redistribute it and/or modify
|
|
5 ; it under the terms of the GNU General Public License as published by
|
|
6 ; the Free Software Foundation, either version 3 of the License, or
|
|
7 ; (at your option) any later version.
|
|
8
|
|
9 ; This program is distributed in the hope that it will be useful,
|
|
10 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 ; GNU General Public License for more details.
|
|
13
|
|
14 ; You should have received a copy of the GNU General Public License
|
|
15 ; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
16
|
|
17
|
|
18 ; Math routines
|
|
19 ; written by: Matthias Heinrichs, info@heinrichsweikamp.com
|
|
20 ; written: 10/30/05
|
|
21 ; last updated: 06/21/07
|
|
22 ; known bugs:
|
|
23 ; ToDo: clean up!
|
|
24
|
88
|
25
|
0
|
26 convert_time: ; converts hi:lo in minutes to hours (hi) and minutes (lo)
|
|
27 movff lo,xA+0 ; divide by 60...
|
|
28 movff hi,xA+1 ;
|
|
29 movlw d'60' ;
|
|
30 movwf xB+0 ;
|
|
31 clrf xB+1 ;
|
|
32 rcall div16x16 ; xA/xB=xC with xA as remainder
|
|
33 movff xC+0,hi ; Hours
|
|
34 movff xA+0,lo ; =remaining minutes (0.....59)
|
|
35 return
|
|
36
|
|
37 div16:
|
|
38 ; divA=divA/2^divB (divB: 8Bit only!)
|
|
39 bcf STATUS,C
|
|
40 rrcf divA+1
|
|
41 rrcf divA
|
|
42 decfsz divB
|
|
43 bra div16
|
|
44 return
|
|
45
|
|
46 div32:
|
|
47 ; xC=xC(32Bit)/2^divB (divB: 8Bit only!)
|
|
48 bcf STATUS,C
|
|
49 rrcf xC+3
|
|
50 rrcf xC+2
|
|
51 rrcf xC+1
|
|
52 rrcf xC+0
|
|
53 decfsz divB
|
|
54 bra div32
|
|
55 return
|
|
56
|
|
57 invert_xC:
|
|
58 movf xC+1, w ; inverses xC+0:xC+1
|
|
59 sublw 0xFF
|
|
60 movwf xC+1
|
|
61 movf xC+0, w
|
|
62 bcf STATUS,C
|
|
63 sublw 0xFF
|
|
64 movwf xC+0
|
|
65 return
|
|
66
|
|
67
|
|
68 sub16:
|
|
69 ; sub_c = sub_a - sub_b
|
|
70 bcf neg_flag
|
74
|
71 movf sub_b+0, W ; Get Value to be subtracted
|
|
72 subwf sub_a+0, W ; Do the High Byte
|
|
73 movwf sub_c+0
|
|
74 movf sub_b+1, W ; Get the Value to be Subbed
|
|
75 subwfb sub_a+1, W
|
|
76 movwf sub_c+1
|
|
77 btfsc STATUS,C
|
0
|
78 return ; result positve
|
|
79 ; sub_c = sub_a - sub_b
|
|
80 bsf neg_flag ; result negative
|
|
81 movff sub_c+0,sub_b+0
|
|
82 movff sub_c+1,sub_b+1
|
|
83 setf sub_a
|
|
84 setf sub_a+1
|
74
|
85 movf sub_b+0, W ; Get Value to be subtracted
|
|
86 subwf sub_a+0, W ; Do the High Byte
|
|
87 movwf sub_c+0
|
|
88 movf sub_b+1, W ; Get the Value to be Subbed
|
|
89 subwfb sub_a+1, W
|
0
|
90 movwf sub_c+1
|
|
91 return
|
|
92
|
|
93
|
|
94 mult16x16:
|
|
95 ;xA*xB=xC
|
|
96 clrf xC+2 ; Clear the High-Order Bits
|
|
97 clrf xC+3
|
|
98 movf xA, w ; Do the "L" Multiplication first
|
|
99 mulwf xB
|
|
100 movf PRODL, w ; Save result
|
|
101 movwf xC
|
|
102 movf PRODH, w
|
|
103 movwf xC+1
|
|
104 movf xA, w ; Do the "I" Multiplication
|
|
105 mulwf xB+1
|
|
106 movf PRODL, w ; Save the Most Significant Byte First
|
|
107 addwf xC+1, f
|
|
108 movf PRODH, w
|
|
109 addwfc xC+2, f ; Add to the Last Result
|
|
110 movf xA+1, w ; Do the "O" Multiplication
|
|
111 mulwf xB
|
|
112 movf PRODL, w ; Add the Lower Byte Next
|
|
113 addwf xC+1, f
|
|
114 movf PRODH, w ; Add the High Byte First
|
|
115 addwfc xC+2, f
|
|
116 btfsc STATUS, C ; Add the Carry
|
|
117 incf xC+3, f
|
|
118 movf xA+1, w ; Do the "F" Multiplication
|
|
119 mulwf xB+1
|
|
120 movf PRODL, w
|
|
121 addwf xC+2, f
|
|
122 movf PRODH, w
|
|
123 addwfc xC+3, f
|
|
124 return
|
|
125
|
|
126
|
|
127 div16x16: ;xA/xB=xC with xA as remainder
|
|
128 ;uses divB as temp variable
|
|
129 clrf xC+0
|
|
130 clrf xC+1
|
|
131 MOVF xB+0,W ; Check for zero
|
|
132 IORWF xB+1,W ;
|
|
133 BTFSC STATUS,Z ; Check for zero
|
|
134 RETLW H'FF' ; return 0xFF if illegal
|
|
135 MOVLW 1 ; Start count at 1
|
|
136 MOVWF divB ; Clear Count
|
|
137 div16x16_1
|
|
138 BTFSC xB+1,7 ; High bit set ?
|
|
139 bra div16x16_2 ; Yes then continue
|
|
140 INCF divB,F ; Increment count
|
|
141
|
|
142 bcf STATUS,C
|
|
143 rlcf xB+0,F
|
|
144 rlcf xB+1,F
|
|
145 bra div16x16_1
|
|
146 div16x16_2:
|
|
147 ; Shift result left
|
|
148 bcf STATUS,C
|
|
149 rlcf xC+0,F
|
|
150 rlcf xC+1,F
|
|
151
|
|
152 ; Reduce Divisor
|
|
153
|
|
154 MOVF xB,W ; Get low byte of subtrahend
|
|
155 SUBWF xA,F ; Subtract DST(low) - SRC(low)
|
|
156 MOVF xB+1,W ; Now get high byte of subtrahend
|
|
157 BTFSS STATUS,C ; If there was a borrow, rather than
|
|
158 INCF xB+1,W ; decrement high byte of dst we inc src
|
|
159 SUBWF xA+1,F ; Subtract the high byte and we're done
|
|
160
|
|
161
|
|
162 BTFSC STATUS, C ; Did it reduce?
|
|
163 bra div16x16_3 ; No, so it was less than
|
|
164
|
|
165 movf xB+0,W ; Reverse subtraction
|
|
166 addwf xA+0,F
|
|
167 movf xB+1,W
|
|
168 addwfc xA+1,F
|
|
169
|
|
170 bra div16x16_4 ; Continue the process
|
|
171 div16x16_3:
|
|
172 BSF xC+0,0 ; Yes it did, this gets a 1 bit
|
|
173 div16x16_4:
|
|
174 DECF divB,F ; Decrement N_COUNT
|
|
175 BTFSC STATUS,Z ; If its not zero then continue
|
|
176 return
|
|
177
|
|
178 bcf STATUS,C
|
|
179 rrcf xB+1,F
|
|
180 rrcf xB+0,F
|
|
181
|
|
182 bra div16x16_2 ; Next bit.
|
|
183
|
|
184 div32x16: ; xC:4 / xB:2 = xC+3:xC+2 with xC+1:xC+0 as remainder
|
|
185 ; Setup
|
|
186 movlw .32 ; setup shift counter
|
|
187 movwf divB
|
|
188 movf xC+3,W ; move ACCb to ACCf
|
|
189 movwf xA+1
|
|
190 movf xC+2,W
|
|
191 movwf xA+0
|
|
192 movf xC+1,W ; move ACCc to ACCe
|
|
193 movwf sub_a+1
|
|
194 movf xC+0,W
|
|
195 movwf sub_a+0
|
|
196 clrf xC+3
|
|
197 clrf xC+2
|
|
198 clrf xC+1
|
|
199 clrf xC+0
|
|
200 clrf sub_b+1
|
|
201 clrf sub_b+0
|
|
202 div32x16_2
|
|
203 bcf STATUS,C
|
|
204 rlcf sub_a+0,F
|
|
205 rlcf sub_a+1,F
|
|
206 rlcf xA+0,F
|
|
207 rlcf xA+1,F
|
|
208 rlcf sub_b+0,F
|
|
209 rlcf sub_b+1,F
|
|
210 movf xB+1,W
|
|
211 subwf sub_b+1,W ; check if a>d
|
|
212 btfss STATUS,Z
|
|
213 goto div32x16_3
|
|
214 movf xB+0,W
|
|
215 subwf sub_b+0,W ; if msb equal then check lsb
|
|
216 div32x16_3
|
|
217 btfss STATUS,C ; carry set if d>a
|
|
218 goto div32x16_4
|
|
219 movf xB+0,W ; d-a into d
|
|
220 subwf sub_b+0,F
|
|
221 btfss STATUS,C
|
|
222 decf sub_b+1,F
|
|
223 movf xB+1,W
|
|
224 subwf sub_b+1,F
|
|
225 bsf STATUS,C ; shift a 1 into b (result)
|
|
226 div32x16_4
|
|
227 rlcf xC+0,F
|
|
228 rlcf xC+1,F
|
|
229 rlcf xC+2,F
|
|
230 rlcf xC+3,F
|
|
231 decfsz divB,F ; loop until all bits checked
|
|
232 goto div32x16_2
|
|
233 return
|
|
234
|
|
235
|
|
236 isr_div16:
|
|
237 ; divA=divA/2^divB (divB: 8Bit only!)
|
|
238 bcf STATUS,C
|
|
239 rrcf isr_divA+1
|
|
240 rrcf isr_divA
|
|
241 decfsz isr_divB
|
|
242 bra isr_div16
|
|
243 return
|
|
244
|
|
245 isr_div32:
|
|
246 ; xC=xC(32Bit)/2^divB (divB: 8Bit only!)
|
|
247 bcf STATUS,C
|
|
248 rrcf isr_xC+3
|
|
249 rrcf isr_xC+2
|
|
250 rrcf isr_xC+1
|
|
251 rrcf isr_xC+0
|
|
252 decfsz isr_divB
|
|
253 bra isr_div32
|
|
254 return
|
|
255
|
|
256 isr_invert_xC:
|
|
257 movf isr_xC+1, w ; inverses xC+0:xC+1
|
|
258 sublw 0xFF
|
|
259 movwf isr_xC+1
|
|
260 movf isr_xC+0, w
|
|
261 bcf STATUS,C
|
|
262 sublw 0xFF
|
|
263 movwf isr_xC+0
|
|
264 return
|
|
265
|
|
266
|
|
267 isr_sub16:
|
|
268 ; sub_c = sub_a - sub_b
|
|
269 bcf neg_flag_isr
|
|
270 movf isr_sub_b+0, w ; Get Value to be subtracted
|
|
271 subwf isr_sub_a+0, w ; Do the High Byte
|
|
272 movwf isr_sub_c+0
|
|
273 movf isr_sub_b+1, w ; Get the Value to be Subbed
|
|
274 subwfb isr_sub_a+1, w
|
|
275 movwf isr_sub_c+1
|
74
|
276 btfsc STATUS,C
|
0
|
277 return ; result positve
|
|
278 ; sub_c = sub_a - sub_b
|
|
279 bsf neg_flag_isr ; result negative
|
|
280 movff isr_sub_c+0,isr_sub_b+0
|
|
281 movff isr_sub_c+1,isr_sub_b+1
|
|
282 setf isr_sub_a
|
|
283 setf isr_sub_a+1
|
|
284 movf isr_sub_b+0, w ; Get Value to be subtracted
|
|
285 subwf isr_sub_a+0, w ; Do the High Byte
|
|
286 movwf isr_sub_c+0
|
|
287 movf isr_sub_b+1, w ; Get the Value to be Subbed
|
|
288 subwfb isr_sub_a+1, w
|
|
289 movwf isr_sub_c+1
|
|
290 return
|
|
291
|
|
292
|
|
293 isr_mult16x16:
|
|
294 ;xA*xB=xC
|
|
295 clrf isr_xC+2 ; Clear the High-Order Bits
|
|
296 clrf isr_xC+3
|
|
297 movf isr_xA, w ; Do the "L" Multiplication first
|
|
298 mulwf isr_xB
|
|
299 movf PRODL, w ; Save result
|
|
300 movwf isr_xC
|
|
301 movf PRODH, w
|
|
302 movwf isr_xC+1
|
|
303 movf isr_xA, w ; Do the "I" Multiplication
|
|
304 mulwf isr_xB+1
|
|
305 movf PRODL, w ; Save the Most Significant Byte First
|
|
306 addwf isr_xC+1, f
|
|
307 movf PRODH, w
|
|
308 addwfc isr_xC+2, f ; Add to the Last Result
|
|
309 movf isr_xA+1, w ; Do the "O" Multiplication
|
|
310 mulwf isr_xB
|
|
311 movf PRODL, w ; Add the Lower Byte Next
|
|
312 addwf isr_xC+1, f
|
|
313 movf PRODH, w ; Add the High Byte First
|
|
314 addwfc isr_xC+2, f
|
|
315 btfsc STATUS, C ; Add the Carry
|
|
316 incf isr_xC+3, f
|
|
317 movf isr_xA+1, w ; Do the "F" Multiplication
|
|
318 mulwf isr_xB+1
|
|
319 movf PRODL, w
|
|
320 addwf isr_xC+2, f
|
|
321 movf PRODH, w
|
|
322 addwfc isr_xC+3, f
|
|
323 return
|
|
324 |