annotate src/math.asm @ 629:237931377539

3.07 stable release
author heinrichsweikamp
date Fri, 29 Nov 2019 18:48:11 +0100
parents cd58f7fc86db
children 4050675965ea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
heinrichsweikamp
parents:
diff changeset
1 ;=============================================================================
heinrichsweikamp
parents:
diff changeset
2 ;
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
3 ; File math.asm combined next generation V3.04.3
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
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
13 #include "hwos.inc" ; mandatory header
0
heinrichsweikamp
parents:
diff changeset
14
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
15 math CODE
604
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
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
57 global addU16 ; ; sub_c:2 = sub_a:2 + sub_b:2 with USIGNED values
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
58 ; trashes WREG
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
59 addU16:
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
60 movf sub_a+0,W ; get 1st summand (low byte) to WREG
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
61 addwf sub_b+0,W ; add 2nd summand (low byte) and store result in WREG
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
62 movwf sub_c+0 ; copy result (low byte) to sub_c
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
63 movf sub_a+1,W ; get 1st summand (high byte) to WREG
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
64 addwfc sub_b+1,W ; add 2nd summand (high byte) and store result in WREG
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
65 movwf sub_c+1 ; copy result (high byte) to sub_c
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
66 return ; done
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
67
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
68
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
69 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
70 ; sets neg_flag if result is < 0
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
71 ; trashes WREG
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
72 sub16:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
73 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
74 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
75 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
76 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
77 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
78 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
79 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
80 btfss STATUS,N ; result negative ?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
81 return ; NO - result positive, done
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
82 bsf neg_flag ; YES - set flag
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
83 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
84 negf sub_c+0 ;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
85 btfsc STATUS,C ; - carry to be propagated ?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
86 incf sub_c+1,F ; YES - do it
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
87 return ; - done
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
88
0
heinrichsweikamp
parents:
diff changeset
89
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
90 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
91 ; sets neg_flag if result is < 0
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
92 ; trashes WREG
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
93 subU16:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
94 bcf neg_flag ; clear flag which will indicate if result is negative
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
95 movf sub_b+0,W ; get value to be subtracted, low byte
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
96 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
97 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
98 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
99 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
100 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
101 btfsc STATUS,C ; borrow to propagate (B == /CARRY) ?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
102 return ; NO - result positive, done
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
103 bsf neg_flag ; YES - set flag
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
104 comf sub_c+1 ; - do a 16 bit sign change
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
105 negf sub_c+0 ; - ...
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
106 btfsc STATUS,C ; - carry to be propagated ?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
107 incf sub_c+1,F ; YES - do it
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
108 return ; - done
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
109
0
heinrichsweikamp
parents:
diff changeset
110
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
111 global cmpU16 ; sub_a:2 - sub_b:2 with UNSIGNED values
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
112 ; sets neg_flag if result is < 0, but does not store result itself
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
113 ; trashes WREG
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
114 cmpU16:
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
115 bcf neg_flag ; clear flag which will indicate if result is negative
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
116 movf sub_b+0,W ; get value to be subtracted, low byte
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
117 subwf sub_a+0,W ; execute subtraction on low byte
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
118 movf sub_b+1,W ; get value to be subtracted, high byte
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
119 subwfb sub_a+1,W ; execute subtraction on high byte, considering borrow flag
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
120 btfss STATUS,C ; borrow to propagate (B == /CARRY) ?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
121 bsf neg_flag ; YES - result is negative, set flag
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
122 return ; done
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
123
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
124
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
125 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
126 ; trashes PRODL, PRODH, WREG
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
127 mult16x16:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
128 clrf xC+2 ; clear the high-order bits
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
129 clrf xC+3
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
130 ;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
131 movf xA+0,W ; do the "L" multiplication
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
132 mulwf xB+0
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
133 movff PRODL,xC+0 ; copy result to xC
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
134 movff PRODH,xC+1
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
135 ;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
136 movf xA+0,W ; do the "I" multiplication
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
137 mulwf xB+1
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
138 movf PRODL,W ; get the product's low byte...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
139 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
140 movf PRODH,W ; get the product's high byte...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
141 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
142 ;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
143 movf xA+1,W ; do the "O" multiplication
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
144 mulwf xB+0
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
145 movf PRODL,W ; get the product's low byte...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
146 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
147 movf PRODH,W ; get the product's high byte...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
148 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
149 clrf WREG ; clear WREG...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
150 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
151 ;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
152 movf xA+1,W ; do the "F" multiplication
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
153 mulwf xB+1
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
154 movf PRODL,W ; get the product's low byte...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
155 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
156 movf PRODH,W ; get the product's high byte...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
157 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
158 return
0
heinrichsweikamp
parents:
diff changeset
159
heinrichsweikamp
parents:
diff changeset
160
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
161 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
162 ; trashes WREG
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
163 div16x16:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
164 movlw .16 ; process 16 bits ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
165 movwf math_loop ; ... initialize loop counter
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
166 movff xA+0,xC+0 ; copy xA to xC
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
167 movff xA+1,xC+1 ; ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
168 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
169 clrf xA+1 ; ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
170 div16x16_1:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
171 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
172 rlcf xC+0,F ; shift left xC
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
173 rlcf xC+1,F ; ... shifting MSB out of xC...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
174 rlcf xA+0,F ; ... and into LSB of xA
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
175 rlcf xA+1,F ; ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
176 btfsc STATUS,C ; did the remainder overflow (carry set)?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
177 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
178 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
179 subwf xA+0,W ; - ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
180 movf xB+1,W ; - ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
181 subwfb xA+1,W ; - ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
182 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
183 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
184 div16x16_2:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
185 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
186 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
187 subwf xA+0,F ; - ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
188 movf xB+1,W ; - ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
189 subwfb xA+1,F ; - ...
0
heinrichsweikamp
parents:
diff changeset
190 div16x16_3:
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
191 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
192 bra div16x16_1 ; NO - loop
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
193 return ; YES - done
0
heinrichsweikamp
parents:
diff changeset
194
heinrichsweikamp
parents:
diff changeset
195
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
196 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
197 ; trashes WREG
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
198 div32x16:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
199 movlw .32 ; process 32 bits ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
200 movwf math_loop ; ... initialize loop counter
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
201 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
202 clrf xA+1 ; ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
203 div32x16_1:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
204 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
205 rlcf xC+0,F ; shift left xC
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
206 rlcf xC+1,F ; ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
207 rlcf xC+2,F ; ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
208 rlcf xC+3,F ; ... shifting MSB out of xC...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
209 rlcf xA+0,F ; ... and into LSB of xA
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
210 rlcf xA+1,F ; ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
211 btfsc STATUS,C ; did the remainder overflow (carry set)?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
212 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
213 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
214 subwf xA+0,W ; - ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
215 movf xB+1,W ; - ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
216 subwfb xA+1,W ; - ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
217 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
218 bra div32x16_3 ; YES - result bit = 0, keep LSB of xC+0 being 0
0
heinrichsweikamp
parents:
diff changeset
219 div32x16_2:
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
220 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
221 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
222 subwf xA+0,F ; - ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
223 movf xB+1,W ; - ...
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
224 subwfb xA+1,F ; - ...
0
heinrichsweikamp
parents:
diff changeset
225 div32x16_3:
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
226 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
227 bra div32x16_1 ; NO - loop
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
228 return ; YES - done
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
229
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
230
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
231 ;=============================================================================
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
232
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
233 ; ISR math functions
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
234
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
235 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
236 ; 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
237 ; we don't care what bit is inserted.
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
238 isr_shift_C31:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
239 rrcf isr_xC+3,F ; shift three bytes
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
240 rrcf isr_xC+2,F
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
241 rrcf isr_xC+1,F
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
242 decfsz WREG ; decrement loop counter, done?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
243 bra isr_shift_C31 ; NO - loop
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
244 return ; YES - done
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
245
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
246
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
247 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
248 ; trashes PRODL, PRODH, WREG
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
249 isr_unsigned_mult16x16:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
250 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
251 mulwf isr_xB+0
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
252 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
253 movff PRODH,isr_xC+1
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
254 ;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
255 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
256 mulwf isr_xB+1
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
257 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
258 movff PRODH, isr_xC+3
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
259 ;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
260 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
261 mulwf isr_xB+1
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
262 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
263 addwf isr_xC+1,F
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
264 movf PRODH,W
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
265 addwfc isr_xC+2,F ; propagate carry
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
266 clrf WREG
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
267 addwfc isr_xC+3,F ; propagate carry
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
268 ;
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
269 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
270 mulwf isr_xB+0
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
271 movf PRODL,W ; add cross product
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
272 addwf isr_xC+1,F
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
273 movf PRODH,W
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
274 addwfc isr_xC+2,F ; propagate carry
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
275 clrf WREG
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
276 addwfc isr_xC+3,F ; propagate carry
0
heinrichsweikamp
parents:
diff changeset
277 return
heinrichsweikamp
parents:
diff changeset
278
heinrichsweikamp
parents:
diff changeset
279
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 604
diff changeset
280 global isr_signed_mult16x16 ; isr_xC = isr_xA * isr_xB with SIGNED values
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
281 ; trashes PRODL, PRODH, WREG
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
282 isr_signed_mult16x16:
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
283 rcall isr_unsigned_mult16x16 ; do an unsigned multiplication first
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
284 ; ; manage sign extension of operand B
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
285 btfss isr_xB+1,7 ; is B negative ?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
286 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
287 movf isr_xA+0,W ; Yes - add -65536 * A
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
288 subwf isr_xC+2,F
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
289 movf isr_xA+1,W
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
290 subwfb isr_xC+3,F
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
291 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
292 btfss isr_xA+1,7 ; is A negative ?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
293 return ; NO - done
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
294 movf isr_xB+0,W ; Yes - add -65536 * B
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
295 subwf isr_xC+2,F
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
296 movf isr_xB+1,W
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
297 subwfb isr_xC+3,F
0
heinrichsweikamp
parents:
diff changeset
298 return
heinrichsweikamp
parents:
diff changeset
299
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 275
diff changeset
300 END