OSAL_Math.s51 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*******************************************************************************
  2. * Filename: OSAL_Math.s51
  3. * Revised: $Date: 2010-02-23 10:47:02 -0800 (Tue, 23 Feb 2010) $
  4. * Revision: $Revision: 21759 $
  5. *
  6. * Description: This module contains optimized math routines needed to
  7. * perform efficient as possible calculations for BLE operations
  8. * such as timer drift adjustments.
  9. *
  10. * Copyright 2010 Texas Instruments Incorporated. All rights reserved.
  11. *
  12. * IMPORTANT: Your use of this Software is limited to those specific rights
  13. * granted under the terms of a software license agreement between the user
  14. * who downloaded the software, his/her employer (which must be your employer)
  15. * and Texas Instruments Incorporated (the "License"). You may not use this
  16. * Software unless you agree to abide by the terms of the License. The License
  17. * limits your use, and you acknowledge, that the Software may not be modified,
  18. * copied or distributed unless embedded on a Texas Instruments microcontroller
  19. * or used solely and exclusively in conjunction with a Texas Instruments radio
  20. * frequency transceiver, which is integrated into your product. Other than for
  21. * the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  22. * works of, modify, distribute, perform, display or sell this Software and/or
  23. * its documentation for any purpose.
  24. * YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  25. * PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  26. * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  27. * NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  28. * TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  29. * NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  30. * LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  31. * INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  32. * OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  33. * OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  34. * (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  35. *
  36. * Should you have any questions regarding your right to use this Software,
  37. * contact Texas Instruments Incorporated at www.TI.com.
  38. *******************************************************************************/
  39. MODULE OSAL_Math.s51
  40. /*******************************************************************************
  41. *
  42. * @fn osalMcuDivide31By16To16
  43. *
  44. * @brief This function is used to divide a 31 bit dividend by a 16 bit
  45. * divisor and return a packed 16 bit quotient and 16 bit
  46. * remainder.
  47. *
  48. * C Syntax
  49. * uint32 osalMcuDivide31By16To16( uint32 dividend, uint16 divisor );
  50. *
  51. * Preserved registers used:
  52. * R7:R6
  53. *
  54. * Note: This routine takes ~25.6us @32MHz. With C overhead, the
  55. * time is ~32us.
  56. *
  57. * input parameters
  58. *
  59. * @param R5:R4:R3:R2 - 31 bit dividend.
  60. * @param XDATA Stack - 16 bit divisor.
  61. *
  62. * After initialization, the following register mapping is used:
  63. * R7:R6 - divisor
  64. * R5:R4 - quotient
  65. * R3:R2 - upper half of dividend; will become the remainder
  66. * R1:R0 - lower half of dividend
  67. * B - loop counter
  68. *
  69. * output parameters
  70. *
  71. * @param None.
  72. *
  73. * @return R5:R4:R3:R2 - The 16 bit quotient is in R5:R4 and the 16 bit
  74. * remainder is in R3:R2.
  75. *
  76. ******************************************************************************/
  77. PUBLIC osalMcuDivide31By16To16
  78. FUNCTION osalMcuDivide31By16To16,0201H
  79. EXTERN ?XSP
  80. RSEG DATA_Z:DATA:NOROOT(0)
  81. savedR6:
  82. DS 1
  83. savedR7:
  84. DS 1
  85. savedRemHi:
  86. DS 1
  87. savedRemLow:
  88. DS 1
  89. savedDPH:
  90. DS 1
  91. savedDPL:
  92. DS 1
  93. RSEG NEAR_CODE:CODE:NOROOT(2)
  94. osalMcuDivide31By16To16:
  95. #ifdef DEBUG_GPIO
  96. //SETB P0.1
  97. #endif
  98. // initialize loop counter
  99. MOV B,#16
  100. // save preserved registers R6 and R7
  101. MOV savedR6,R6
  102. MOV savedR7,R7
  103. // save DPTR (which ever one is currently selected)
  104. MOV savedDPL,DPL
  105. MOV savedDPH,DPH
  106. // get divisor from the XDATA stack
  107. MOV DPL,?XSP
  108. MOV DPH,?XSP+1
  109. MOVX A,@DPTR
  110. XCH A,R6
  111. INC DPTR
  112. MOVX A,@DPTR
  113. XCH A,R7
  114. // restore DPTR
  115. MOV DPL,savedDPL
  116. MOV DPH,savedDPH
  117. // move lower dividend R3:R2 to R1:R0
  118. MOV A,R3
  119. XCH A,R1
  120. MOV A,R2
  121. XCH A,R0
  122. // move upper dividend R5:R4 to what will be the remainder R3:R2
  123. MOV A,R5
  124. XCH A,R3
  125. MOV A,R4
  126. XCH A,R2
  127. TopOfLoop:
  128. // Step 1: Shift dividend and remainder left, feeding dividend into remainder.
  129. CLR C
  130. MOV A,R0
  131. RLC A
  132. XCH A,R0
  133. MOV A,R1
  134. RLC A
  135. XCH A,R1
  136. MOV A,R2
  137. RLC A
  138. XCH A,R2
  139. MOV A,R3
  140. RLC A
  141. XCH A,R3
  142. // Step 2: Subtract divisor from remainder. If there is a borrow, then restore
  143. // the remainder.
  144. CLR C
  145. MOV A,R2
  146. SUBB A,R6
  147. MOV savedRemLow,A ; save remainder in case it needs to be restored
  148. MOV A,R3
  149. SUBB A,R7
  150. MOV savedRemHi,A ; save remainder in case it needs to be restored
  151. // Step 3: Shift quotient left, feeding complement of borrow bit
  152. // Note: Remainder is left unchanged if there was a borrow.
  153. CPL C
  154. JNC ShiftQuotient
  155. //
  156. MOV R3,savedRemHi
  157. MOV R2,savedRemLow
  158. ShiftQuotient:
  159. MOV A,R4
  160. RLC A
  161. XCH A,R4
  162. MOV A,R5
  163. RLC A
  164. XCH A,R5
  165. // Step 4: Decrement count and loop if not zero.
  166. DJNZ B,TopOfLoop
  167. // restore R6 and R7
  168. MOV R6,savedR6
  169. MOV R7,savedR7
  170. #ifdef DEBUG_GPIO
  171. //CLR P0.1
  172. #endif
  173. RET
  174. /*******************************************************************************
  175. *******************************************************************************/
  176. END