home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / microcrn / issue_41.arc / RAY.FIG < prev    next >
Text File  |  1988-03-18  |  6KB  |  201 lines

  1. ;Figure 2 from "Graphics in 3-D". Micro Cornucopia Magazine Issue #41
  2.  
  3. ;34010 Multiplication and Division.
  4.  
  5. fxPt          equ 16         ;define fixed point position.
  6. One           equ 10000H     ;our representation of the number one.
  7.  
  8. fxMultiply:
  9. ;Out:         a0 = a0 * a1
  10.   mpys        a1, a0         ;signed integer multiplication
  11.   sla         32-fxPt, a0    ;line up high order bits
  12.   srl         fxPt, a1       ;line up low order bits
  13.   or          a1, a0         ;combine them
  14.   rets
  15.  
  16. fxDivide:
  17. ;Out:         a0 = a0 / a1
  18.   move        a1, a2         ;save divisor
  19.   move        a0, a1         ;build dividend in 64 bit register,
  20.                              ;with fxPt shift
  21.   sra         32-fxPt, a0    ;high order bits
  22.   sll         fxPt, a1       ;low order bits
  23.   divs        a2, a0         ;signed integer division
  24.   rets
  25.  
  26. ;END OF LISTING
  27.  
  28.  
  29.  
  30.  
  31. ;Figure 3 from "Graphics in 3-D". Micro Cornucopia Magazine Issue #41
  32.  
  33. ;Fixed Point Square Root
  34.  
  35. fxSqRoot:
  36. ;compute square root of a by iterating x = (x + a / x) / 2
  37. ;out:      a0 = sqrt( a0 )
  38.   mmtm     sp, a4, a5, a6     ;save registers which hold intermediate values.
  39.   btst     31, a0             ;test for negative argument
  40.   jrnz     fs_00              ;if negative, refuse to compute square root.
  41.   movi     10, a6             ;Iteration limit, we'll be
  42.                               ;done long before this.
  43.   move     a0, a4             ;Starting interation value
  44.   move     a0, a5             ;Remember what number we're square rooting
  45.  fs_01:                       ;Start of iteration loop
  46.   move     a5, a0             ;Get a
  47.   move     a4, a1             ;and current x
  48.   callr    fxDivide           ;a0 = a / x
  49.   add      a0, a4             ;a4 = x + a / x
  50.   sra       1, a4             ;a4 = ( x + a / x ) / 2
  51.   sub      a4, a0             ;compare with old x (sort of)
  52.   sra       1, a0             ;give or take a bit
  53.   dsjne    a6, fs_01          ;if not stable repeat
  54.   move     a4, a0             ;result to a0
  55. fs_00:
  56.   mmfm     sp, a4, a5, a6     ;restore some registers.
  57.   rets
  58.  
  59. ;END OF LISTING
  60.  
  61.  
  62.  
  63.  
  64.  
  65. ;Figure 4 from "Graphics in 3-D". Micro Cornucopia Magazine Issue #41
  66.  
  67. ;Basic Vector Arithmetic
  68.  
  69.  .data
  70. vTemp0        .space 60H       ;for intermediate values
  71.  
  72.  .text
  73. vMove:
  74. ;Out:     a9 = a8
  75.   move    *a8(px), a0, 1
  76.   move    a0, *a9(px), 1
  77.   move    a8(py), a0, 1
  78.   move    a0, *a9(py), 1
  79.   move    *a8(pz), a0, 1
  80.   move    a0, *a9(pz), 1
  81.   rets
  82.  
  83. vAdd:
  84. ;Out:     a10 = a8 + a9
  85.   move    *a8(px), a0, 1
  86.   move    *a9(px), a1, 1
  87.   add     a1, a0
  88.   move    a0, *a10(px), 1
  89.   move    *a8(py), a0, 1
  90.   move    *a9(py), a1, 1
  91.   add     a1, a0
  92.   move    a0, *a10(py), 1
  93.   move    *a8(pz), a0, 1
  94.   move    *a9(pz), a1, 1
  95.   add     a1, a0
  96.   move    a0, *a10(pz), 1
  97.   rets
  98.  
  99. vSub:
  100. ;Out:     a10 = a8 - a9
  101.   move    *a8(px), a0, 1
  102.   move    *a9(px), a1, 1
  103.   sub     a1, a0
  104.   move    a0, *a10(px), 1
  105.   move    *a8(py), a0, 1
  106.   move    *a9(py), a1, 1
  107.   sub     a1, a0
  108.   move    a0, *a10(py), 1
  109.   move    *a8(pz), a0, 1
  110.   move    *a9(pz), a1, 1
  111.   sub     a1, a0
  112.   move    a0, *a10(pz), 1
  113.   rets
  114.  
  115. vDot:
  116. ;Out:     a0 = a8 . a9
  117.   mmtm    sp, a4
  118.   move    *a8, a0, 1       ;Multiply x components
  119.   move    *a9, a1, 1
  120.   calla   fxMultiply
  121.   move    a0, a4           ;start accumulating sum
  122.   move    *a8(py), a0, 1
  123.   move    *a9(py), a1, 1
  124.   calla   fxMultiply       ;Multiply y components
  125.   add     a0, a4           ;add to sum
  126.   move    *a8(pz), a0, 1
  127.   move    *a9(pz), a1, 1
  128.   calla   fxMultiply       ;Multiply z components
  129.   add     a4, a0           ;return sum of products
  130.   mmfm    sp, a4
  131.   rets
  132.  
  133. vScale:
  134. ;Out:      a10 = a9 * a8 (a9=scalar, a8=vector)
  135.   move     *a8(px), a0, 1
  136.   move     a9, a1
  137.   calla    fxMultiply       ;scale x component
  138.   move     a0, *a10(px), 1
  139.   move     *a8(py), a0, 1
  140.   move     a9, a1
  141.   calla    fxMultiply       ;scale y component
  142.   move     a0, *a10(py), 1
  143.   move     *a8(pz), a0, 1
  144.   move     a9, a1
  145.   calla    fxMultiply       ;scale z component
  146.   move     a0, *a10(pz), 1
  147.   rets
  148.  
  149. vCalibrate:
  150. ;Out:      a0 = a8 projected onto and calibrate by the unit vector a9
  151.   mmtm     sp, a4, a8
  152.   callr    vDot               ;a9.a8
  153.   move     a0, a4
  154.   move     a9, a8
  155.   callr    vDot               ;a8.a8
  156.   move     a0, a1
  157.   move     a4, a0
  158.   calla    fxDivide           ;a0=(a9.a8)/(a8.a8)
  159.   mmfm     sp, a4, a8
  160.   rets
  161.  
  162. vProjection:
  163. ;Out:       a10 = projection of a8 onto a9
  164.   mmtm      sp, a8, a9
  165.   callr     vCalibrate        ;get projection multiplier
  166.   move      a9, a8
  167.   move      a0, a9
  168.   callr     vScale            ;scale a9
  169.   mmfm      sp, a8, a9
  170.   rets
  171.  
  172. vPerpendicular:
  173. ;Out:       a10 = component of a8 perpendicular to a9
  174.   mmtm      sp, a4, a8, a9, a10
  175.   move      a10, a4
  176.   movi      vTemp0, a10
  177.   callr     vProjection         ;temp = projection
  178.   move      a10, a9
  179.   move      a4, a10
  180.   callr     vSub                ;subtract projection to get
  181. perpendicular
  182.   mmfm      sp, a4, a8, a9, a10
  183.   rets
  184.  
  185. vNormalize:
  186. ;Out:         a9 = normalized a8
  187.   mmtm        sp, a8, a9, a10
  188.   move        a9, a10
  189.   move        a8, a9
  190.   callr       vDot               ;a8.a8 = square of size
  191.   calla       fxSqRoot           ;size to a0
  192.   move        a0, a1
  193.   movi        One, a0
  194.   calla       fxDivide           ;invert size
  195.   move        a0, a9
  196.   callr       vScale             ;then scale
  197.   mmfm        sp, a8, a9, a10
  198.   rets
  199.  
  200. ;END OF LISTING
  201.