Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. ;throttle_divide.asm
  2. ;
  3. ;********************************************************************************
  4. ;* div16u                                                                        *
  5. ;* Second Level Subroutine                                                       *
  6. ;*                                                                               *
  7. ;* Program from Atmel file avr200.asm                                            *
  8. ;*                                                                               *
  9. ;* Since the 25kHz pwm cycle is 64 clock cycles long, this subroutine            *
  10. ;* requires 3.67 to 3.92 25kHz clock cycles.                                     *
  11. ;*                                                                               *
  12. ;* A single line was added which adds 3 to Cycle_count                           *
  13. ;*                                                                               *
  14. ;* Inputs:  HILOCAL2:HILOCAL1 and B_TEMPLOCAL1:B_TEMPLOCAL                       *
  15. ;* Returns: HILOCAL2:HILOCAL1 = HILOCAL2:HILOCAL1 / B_TEMPLOCAL1:B_TEMPLOCAL     *
  16. ;*          LOLOCAL2:LOLOCAL1 = remainder                                        *
  17. ;* Changed: B_TEMPLOCAL2                                                         *
  18. ;*                                                                               *
  19. ;* Calls:   Not allowed                                                          *  
  20. ;********************************************************************************
  21.  
  22.  
  23.    B_TEMPLOCAL2   dcnt16u              ; Local counter
  24.  
  25.    HILOCAL1       dd16uL               ; 16 bit Innput
  26.    HILOCAL2       dd16uH
  27.  
  28.    B_TEMPLOCAL    dv16uL               ; 16 bit Input
  29.    B_TEMPLOCAL1   dv16uH
  30.  
  31.    HILOCAL1       dres16uL             ; 16 bit Output
  32.    HILOCAL2       dres16uH
  33.  
  34.    LOWLOCAL1      drem16uL             ; 16 bit Remainder
  35.    LOWLOCAL2      drem16uH             ;
  36.  
  37.  
  38.  
  39. ;<ATMEL ROUTINE>
  40. ;***************************************************************************
  41. ;*
  42. ;* "div16u" - 16/16 Bit Unsigned Division
  43. ;*
  44. ;* This subroutine divides the two 16-bit numbers
  45. ;* "dd16uH:dd16uL" (dividend) and "dv16uH:dv16uL" (divisor).
  46. ;* The result is placed in "dres16uH:dres16uL" and the remainder in
  47. ;* "drem16uH:drem16uL".
  48. ;*  
  49. ;* Number of words   :19
  50. ;* Number of cycles  :235/251 (Min/Max)
  51. ;* Low registers used   :2 (drem16uL,drem16uH)
  52. ;* High registers used  :5 (dres16uL/dd16uL,dres16uH/dd16uH,dv16uL,dv16uH,
  53. ;*           dcnt16u)
  54. ;*
  55. ;***************************************************************************
  56.  
  57. ;***** Subroutine Register Variables
  58.  
  59. ;.def drem16uL=   r14                  ; Reassigned
  60. ;.def drem16uH=   r15
  61. ;.def dres16uL=   r16
  62. ;.def dres16uH=   r17
  63. ;.def dd16uL=     r16
  64. ;.def dd16uH=     r17
  65. ;.def dv16uL=     r18
  66. ;.def dv16uH=     r19
  67. ;.def dcnt16u=    r20
  68.  
  69. ;***** Code
  70.  
  71. div16u:
  72.    clr   drem16uL                      ; clear remainder Low byte
  73.    sub   drem16uH,drem16uH             ; clear remainder High byte and carry
  74.    ldi   dcnt16u,17                    ; init loop counter
  75.  
  76. d16u_1:  
  77.    rol   dd16uL                        ; shift left dividend
  78.    rol   dd16uH
  79.    dec   dcnt16u                       ; decrement counter
  80.    brne  d16u_2                        ; if done
  81.  
  82.    subi  Cycle_count,256-3             ; Add 3 to Cycle_count
  83.  
  84.    ret                                 ; return
  85.  
  86. d16u_2:
  87.    rol   drem16uL                      ; shift dividend into remainder
  88.    rol   drem16uH
  89.  
  90.    sub   drem16uL,dv16uL               ; remainder = remainder - divisor
  91.    sbc   drem16uH,dv16uH               ;
  92.  
  93.    brcc  d16u_3                        ;
  94.  
  95.    add   drem16uL,dv16uL               ; if result negative
  96.    adc   drem16uH,dv16uH               ; restore remainder
  97.    clc                                 ; clear carry to be shifted into result
  98.    rjmp  d16u_1                        ;
  99.  
  100. d16u_3:                                ; if result NOT negative
  101.    sec                                 ; set carry to be shifted into result
  102.    rjmp  d16u_1
  103.  
  104. ;<END ATMEL ROUTINE>
  105.  
  106. ;********************************************************************************
  107. ;* DIVIDE_16_SIMPLE                                                              *
  108. ;* Second Level Subroutine                                                       *
  109. ;*                                                                               *
  110. ;* Inputs:  dd16uH:dd16ul and dv16uL                                             *
  111. ;* Returns: dres16uH:dres16uL = dd8uH:dd8uL / 2^dv16uL                           *
  112. ;*                                                                               *
  113. ;* Changed: nothing else                                                         *
  114. ;*          N.B that dd16uH, dd16uL, dv16uH and dv16uL are aliases for:          *
  115. ;*          dd16uH=error_hi                                                      *
  116. ;*          dd16uL=error_lo                                                      *  
  117. ;*          dv16uH=B_TempX                                                       *
  118. ;*          dv16uL=B_TempX                                                       *
  119. ;*          dcnt16u=B_TempX                                                      *
  120. ;* Calls:   Not allowed                                                          *  
  121. ;********************************************************************************
  122.  
  123.  
  124. DIVIDE_16_SIMPLE:
  125.    inc   dv16uL
  126. DIVIDE_16_SIMPLE_LOOP:                          
  127.    dec   dv16uL                        ; decrement counter
  128.    brne  DIVIDE_BY_2
  129.    ret  
  130.  
  131. DIVIDE_BY_2:
  132.    asr   dd16uH                        ; divide by two  
  133.    ror   dd16uL
  134.    rjmp  DIVIDE_16_SIMPLE_LOOP
  135.    
  136.