home *** CD-ROM | disk | FTP | other *** search
/ M.u.C.S. Disc 2000 / MUCS2000.iso / falcon / whip.031 / vlm / nailspin.1_2 / source / fimath.i < prev    next >
Text File  |  1999-06-20  |  5KB  |  154 lines

  1. * Math library. Fixed point and integer based.
  2. * 68020 or better required.
  3.  
  4. * Very fast and accurate squareroot algorithm.
  5. * Quite lengthy, though: 66 bytes.
  6. * INPUT: d1.l: value to calculate the squareroot of (integer)
  7. * OUTPUT: d0.l: squareroot of value (16.16 fixed point)
  8. CALC_ATARISQRT:
  9.     moveq    #1,d2
  10.     ror.l    #2,d2
  11.     moveq    #$F,d3
  12. .loop1:    cmp.l    d2,d1
  13.     bgt.s    .endloop1
  14.     add.l    d1,d1
  15.     lsr.l    #1,d2
  16.     dbf    d3,.loop1
  17.     moveq    #0,d0
  18.     bra.s    .is_null
  19. .endloop1:
  20.  
  21.     sub.l    d2,d1
  22.     move.l    d2,d0
  23.     lsr.l    #1,d2
  24. .loop2:    lsr.l    #1,d2
  25.     add.l    d2,d0
  26.     cmp.l    d0,d1
  27.     bgt.s    .endloop2
  28.     sub.l    d2,d0
  29.     add.l    d1,d1
  30.     dbf    d3,.loop2
  31.     bra.s    .end
  32. .endloop2:
  33.  
  34.     sub.l    d0,d1
  35.     add.l    d2,d0
  36.     add.l    d1,d1
  37.     dbf    d3,.loop2
  38.  
  39. .end:    add.l    d0,d0
  40.     addi.l    #$00008000,d0
  41. .is_null:
  42.     rts
  43.  
  44. *==========================================================================
  45. * Sinewave table generator.
  46. * By EarX/~fUn~, 10-5-1998
  47. * 68020 or higher is required!
  48. *==========================================================================
  49.  
  50. ******** GLOBAL CONSTANTS ********
  51.  
  52. * >WARNING< for these equ's: when using a new 'sintbllen' you must
  53. * recalculate 'cos1' and 'sin1'!
  54. sintbllen:    equ    2048        * MUST BE A EXPONENTIAL VALUE OF 2!
  55. sin1:        equ    13176774    * sin(2π/2048)*2^32
  56. cos1:        equ    4294947083    * cos(2π/2048)*2^32
  57.  
  58. ******** MACROS ********
  59.  
  60. * Macro that returns the modulo of a given angle.
  61. * INPUT: angle: type: data-register (word) or RAM (word)
  62. Do_SinModulo:    MACRO    angle
  63.         andi.w    #sintbllen-1,\1
  64.         ENDM
  65.  
  66. * Macro that returns sine & cosine of a given angle.
  67. * PRECONDITION: INIT_SINETABLE has been called!
  68. * INPUT: base: type: address-register or address or relative address
  69. *        inpreg: type: data-register or address-register (lower word)
  70. *                contains: angle (0=0°, sintbllen=360°)
  71. * OUTPUT: sinreg: type: data-register (long) or address-register
  72. *                 contains: sine value (signed: -32768 to 32767)
  73. *         cosreg: type: data-register (long) or address-register
  74. *                 contains: cosine value (signed: -32768 to 32767)
  75. Get_SinCos:    MACRO    base,inpreg,sinreg,cosreg
  76.         movem.w    (\1,\2.w*4),\3/\4
  77.         ENDM
  78.  
  79. * Macro that returns sine of a given angle.
  80. * PRECONDITION: INIT_SINETABLE has been called!
  81. * INPUT: base: type: address-register or address or relative address
  82. *        inpreg: type: data-register or address-register (lower word)
  83. *                contains: angle (0=0°, sintbllen=360°)
  84. * OUTPUT: sinreg: type: data-register (long) or address-register
  85. *                 contains: sine value (signed: -32768 to 32767)
  86. Get_Sin:    MACRO    base,inpreg,sinreg
  87.         move.w    (\1,\2.w*4),\3
  88.         ENDM
  89.  
  90. * Macro that returns cosine of a given angle.
  91. * PRECONDITION: INIT_SINETABLE has been called!
  92. * INPUT: base: type: address-register or address or relative address
  93. *        inpreg: type: data-register or address-register (lower word)
  94. *                contains: angle (0=0°, sintbllen=360°)
  95. * OUTPUT: cosreg: type: data-register (long) or address-register
  96. *                 contains: cosine value (signed: -32768 to 32767)
  97. Get_Cos:    MACRO    base,inpreg,sinreg,cosreg
  98.         move.w    2(\1,\2.w*4),\3
  99.         ENDM
  100.  
  101. * Creates the a combined sine and cosine table for quick fetching.
  102. * Macro is exactly 96 bytes in length :-)
  103. * INPUT: a0: address of sine_tbl
  104. Init_SineTable:    MACRO
  105.         moveq    #$ffffffff,d0        * /cos(0)=1
  106.         lsr.l    #1,d0            * \(=$7fffffff)
  107.         moveq    #0,d1            * sin(0)=0
  108.         move.l    #sin1,d6
  109.         move.w    #sintbllen/4-1,d7
  110.  
  111. .genlop:    swap    d0            * Get high-word of cosa
  112.         swap    d1            * Get high-word of sina
  113.         move.w    d1,2+(sintbllen)*3(a0)    * Copy sina in cos-4th quadrant
  114.         move.w    d0,sintbllen*1(a0)    * Copy cosa in sin-2nd quadrant
  115.         sub.w    d1,2+(sintbllen)*1(a0)    * Copy -sina in cos-2nd quadrant
  116.         sub.w    d0,sintbllen*3(a0)    * Copy -cosa in sin-4th quadrant
  117.         sub.w    d0,2+(sintbllen)*2(a0)    * Copy -cosa in cos-3rd quadrant
  118.         sub.w    d1,sintbllen*2(a0)    * Copy -sina in sin-3rd quadrant
  119.         move.w    d1,(a0)+        * Save sina (16 bit signed value) in first quadrant
  120.         move.w    d0,(a0)+        * Save cosa (16 bit signed value) in first quadrant
  121.         swap    d0            * Change cosa back to fixedpoint
  122.         swap    d1            * Change sina back to fixedpoint
  123.         move.l    d1,d4            * / Backup sina 
  124.         move.l    d0,d5            * | and cosa
  125.         move.l    d1,d2            * | for use in
  126.         move.l    d0,d3            * \ multiplications.
  127.         mulu.l    d6,d3:d1        * d3:=sin1*sina
  128.         mulu.l    #cos1,d2:d0        * d2:=cos1*cosa
  129.         mulu.l    d6,d1:d5        * d0:=sin1*cosa
  130.         mulu.l    #cos1,d0:d4        * d1:=cos1*sina
  131.         sub.l    d3,d2            * d2:=(cos1*cosa)-(sin1*sina)
  132.         add.l    d0,d1            * sina:=(sin1*cosa)+(cos1*sina)
  133.         move.l    d2,d0            * cosa:=(cos1*cosa)-(sin1*sina)
  134.         dbra    d7,.genlop
  135.         ENDM
  136.  
  137. * Creates a tangens table by using the sine/cosine table.
  138. * INPUT: sincos_tbl: address register
  139. * OUTPUT: tan_tbl: address register
  140. Init_TanTable:    MACRO    sincos_tbl,tan_tbl
  141.         move.w    #sintbllen-1,d7
  142. .gentanloop:    movem.w    (/1)+,d0-d1
  143.         move.w    d0,d0
  144.         beq.s    .notandiv
  145.         lsl.l    #8,d1
  146.         divs.w    d0,d1
  147. .notandiv:    move.w    d1,(/2)+
  148.         dbra    d7,.gentanloop
  149.         ENDM
  150.  
  151.     BSS
  152.  
  153. random:    DS.L    1
  154.