home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Misc / phxa400.lha / PhxAss / Examples / VectorBalls.asm < prev    next >
Encoding:
Assembly Source File  |  1994-12-28  |  9.4 KB  |  408 lines

  1. **
  2. **    V E C T O R B A L L S
  3. **
  4. **     by Frank Wille 1994
  5. **
  6. **     Assembler: PhxAss V4.xx
  7. **     Linker:      PhxLnk V4.xx
  8. **
  9.  
  10.  
  11. ** Imported Functions **
  12.  
  13.     xref    demoStartup
  14. ;        (IN: - / OUT: a4=SmallDataBase,a6=CUSTOM)
  15.  
  16.     xref    demoCleanup
  17. ;        (IN: - / OUT: d0=0)
  18.  
  19.     xref    demoStdView
  20. ;        (IN: d0=Width,d1=Height,d2=HStrt,d3=VStrt,d4=Depth,a0=CList,
  21. ;         a1=DisplayMem / OUT: a0=newCList)
  22.  
  23.     xref    demoColors
  24. ;        (IN: a0=CList,a1=ColorTab,d0=nColors / OUT: a0=CList)
  25.  
  26.  
  27.  
  28.  
  29. ** Includes **
  30.  
  31. ;    incdir    "..."     or set PHXASSINC environment variable
  32.     include "hardware/custom_all.i"
  33.     include "hardware/dmabits.i"
  34.     include "hardware/intbits.i"
  35.     include "hardware/cia.i"
  36.  
  37.  
  38.  
  39. ** Defines **
  40.  
  41. WIDTH        = 336            ; Bitmap dimensions
  42. HEIGHT        = 128
  43. DEPTH        = 4
  44. BPR        = WIDTH>>3        ; Bytes per row
  45. DISPHEIGHT    = 176            ; last line of display (bitmap pos.)
  46. HSTART        = $79            ; Display start position (beam pos.)
  47. VSTART        = $4c
  48. MIRRSTART    = 90            ; first line for reflecting surface
  49. VBWIDTH     = 16            ; Vector Ball dimensions
  50. VBHEIGHT    = 16
  51. SINTABVALS    = 256            ; number of entries in sine table
  52. ORGX        = 240            ; bitmap position of spatial origin
  53. ORGY        = 80
  54. ORGZ        = 200
  55. NUMOBS        = 52            ; number of vector balls
  56.  
  57. ; DblBuf structure
  58.         rsreset
  59. dblbuf_CList    rs.l    1        ; copper list and its
  60. dblbuf_Bitmap    rs.l    1        ;  bitmap pointer
  61. dblbuf_SIZE    rs
  62.  
  63. ; BobCoords structure
  64.         rsreset
  65. bobc_next    rs.l 1            ; MinNode header
  66. bobc_prev    rs.l 1
  67. bobc_x        rs.w 1            ; object's 3D coords
  68. bobc_y        rs.w 1
  69. bobc_z        rs.w 1
  70. bobc_reserved    rs.w 1            ; longword align
  71. bobc_SIZE    rs
  72.  
  73.  
  74.  
  75. ** Macros **
  76.  
  77.     macro    WAITBLIT
  78. 1\@$:    btst    #6,DMACONR(a6)        ; wait until blitter ready
  79.     bne.s    1\@$
  80.     endm
  81.  
  82. LASTLIN set    0
  83.     macro    COPWAIT         ; COPWAIT <line>
  84.     ifge    \1-256            ; wait for line >= 256?
  85.     iflt    LASTLIN-256
  86.     move.l    #$ffe1fffe,(a0)+
  87.     endc
  88.     endc
  89.     move.l    #(\1&$ff)<<24|$0ffffe,(a0)+
  90. LASTLIN set    \1
  91.     endm
  92.  
  93.     macro    COPEND
  94.     move.l    #$fffffffe,(a0)
  95. LASTLIN set    0
  96.     endm
  97.  
  98.  
  99.  
  100. ** Code **
  101.  
  102.     near    a4,-2            ; __MERGED Small Data (a4)
  103.  
  104.     code
  105.  
  106.  
  107.     bsr    demoStartup        ; -> a4=SmallData Base, a6=CUSTOM
  108.     bsr    make_ytable        ; pre-calculate line offsets
  109.     bsr    initCopperLists
  110.     move.w    #$8200|DMAF_RASTER|DMAF_COPPER|DMAF_BLITTER|DMAF_DISK,DMACON(a6)
  111. loop:
  112.     bsr    drawVBalls        ; call main routine
  113. 1$:    move.w    INTREQR(a6),d0        ; wait for copper interrupt, which
  114.     and.w    #INTF_COPER,d0        ;  occurs always at end of display
  115.     beq.s    1$
  116.     move.w    toggle(a4),d0        ; swap copper lists and bitmaps
  117.     lea    DblBufInfo(a4),a0
  118.     move.l    dblbuf_CList(a0,d0.w),COP1LC(a6)
  119.     eor.w    #dblbuf_SIZE,d0     ; toggle
  120.     move.w    d0,toggle(a4)
  121.     move.l    dblbuf_Bitmap(a0,d0.w),workBitmap(a4)
  122.     btst    #6,CIAA|PRA        ; loop until left mouse button pressed
  123.     bne.s    loop
  124.     bra    demoCleanup        ; cleanup and quit to CLI or WB
  125.  
  126.  
  127. drawVBalls:
  128.     moveq    #1,d0
  129.     ror.l    #8,d0            ; $01000000 (use D only, D=0)
  130.     WAITBLIT
  131.     move.w    d0,BLTDMOD(a6)        ; use blitter to clear bitmap
  132.     move.l    d0,BLTCON0(a6)
  133.     move.l    workBitmap(a4),BLTDPT(a6)
  134.     move.w    #((DEPTH*HEIGHT)<<6)|(WIDTH>>4),BLTSIZE(a6)
  135.  
  136.     lea    boblist(a4),a3        ; init empty Bob list header -> a3
  137.     move.l    a3,(a3)
  138.     addq.l    #4,(a3)
  139.     move.l    a3,8(a3)
  140.     lea    bobnodes(a4),a2     ; a2 = first Bob node
  141.     lea    SinCosTab(pc),a0    ; a0/a1 = Sine/Cosine table
  142.     lea    SINTABVALS/2(a0),a1
  143.     movem.w alpha(a4),d0/d1     ; d0 = alpha, d1 = beta
  144.     move.w    (a0,d0.w),d5        ; d5 = MSW: sin(alpha) | LSW: cos(alpha)
  145.     swap    d5
  146.     move.w    (a1,d0.w),d5
  147.     move.w    (a0,d1.w),d6        ; d6 = MSW: sin(beta) | LSW: cos(beta)
  148.     swap    d6
  149.     move.w    (a1,d1.w),d6
  150.  
  151.     moveq    #NUMOBS-1,d7        ; *** Coordinate Calculation Loop ***
  152.     lea    vballcoords(pc),a5    ; a5 = spatial positions of all VBalls
  153. rotbobs_loop:
  154.     movem.w (a5)+,d0-d1        ; d0=x, d1=y, z=0
  155.     move.w    d1,d2
  156.     muls    d5,d1            ; * rotate about x-axis *
  157.     swap    d5
  158.     add.l    d1,d1
  159.     clr.w    d1
  160.     swap    d1            ; y' = y*cos(alpha) [- z*sin(alpha)]
  161.     muls    d5,d2
  162.     swap    d5
  163.     add.l    d2,d2
  164.     swap    d2            ; z' = y*sin(alpha) [+ z*cos(alpha)]
  165.     move.w    d0,d3
  166.     move.w    d1,d4
  167.     muls    d6,d0            ; * rotate about y-axis *
  168.     swap    d6
  169.     muls    d6,d4
  170.     add.l    d4,d0
  171.     add.l    d0,d0
  172.     clr.w    d0
  173.     swap    d0            ; x' = x*cos(beta) + z*sin(beta)
  174.     muls    d6,d3
  175.     swap    d6
  176.     muls    d6,d2
  177.     sub.l    d3,d2
  178.     add.l    d2,d2
  179.     swap    d2            ; z' = -x*sin(beta) + z*cos(beta)
  180.     add.w    #ORGX,d0
  181.     add.w    #ORGY,d1
  182.     add.w    #ORGZ,d2
  183.     move.w    d2,d4            ; projection
  184.     add.w    #$100,d4
  185.     beq.s    1$
  186.     lsl.l    #8,d0
  187.     divs    d4,d0
  188.     lsl.l    #8,d1
  189.     divs    d4,d1
  190. 1$:    movem.w d0-d2,bobc_x(a2)    ; write transformed coords. into node
  191.     move.l    (a3),a1
  192.     bra.s    3$
  193. 2$:    cmp.w    bobc_z(a1),d2        ; insert into list, sorted by z-coord.
  194.     bge.s    4$
  195.     move.l    d0,a1
  196. 3$:    move.l    bobc_next(a1),d0
  197.     bne.s    2$
  198. 4$:    move.l    a1,(a2)         ; insert node a2 before node a1
  199.     addq.l    #4,a1
  200.     move.l    (a1),a0
  201.     move.l    a0,4(a2)
  202.     move.l    a2,(a0)
  203.     move.l    a2,(a1)
  204.     lea    bobc_SIZE(a2),a2    ; create next Bob node
  205.     dbf    d7,rotbobs_loop
  206.  
  207.     moveq    #NUMOBS-1,d7        ; Bob counter
  208.     move.w    #(DEPTH*VBHEIGHT)<<6|(VBWIDTH+16)>>4,d6  ; d6 = BLTSIZE
  209.     moveq    #15,d5            ; d5 = shift mask
  210.     move.l    #%111111001010,d4    ; d4 Use ABCD  Minterm: ABC|ABc|aBC|abC
  211.     lea    vballimg,a0                ; a0 = image pointer
  212.     lea    (VBWIDTH>>3)*VBHEIGHT*DEPTH(a0),a1  ; a1 = mask pointer
  213.     move.l    (a3),d2         ; d2 = first Bob node
  214.     lea    yoffsets(a4),a3     ; a3 = offset table for y-coords
  215.     move.l    workBitmap(a4),a5    ; a5 = bitmap pointer
  216.     moveq    #-1,d0
  217.     clr.w    d0            ; BLTAFWM/BLTALWM = $ffff0000
  218.     move.l    #(-2<<16)|((WIDTH-VBWIDTH-16)>>3),d1  ; Bob / Bitmap modulos
  219.     WAITBLIT
  220.     move.l    d0,BLTAFWM(a6)
  221.     move.l    d1,BLTAMOD(a6)        ; A and D modulo
  222.     swap    d1
  223.     move.l    d1,BLTCMOD(a6)        ; C and B modulo
  224. blit_loop:
  225.     move.l    d2,a2
  226.     move.l    bobc_next(a2),d2    ; next Bob node
  227.     movem.w bobc_x(a2),d0-d1    ; d0 = x, d1 = y
  228.     add.w    d1,d1
  229.     move.w    (a3,d1.w),d1        ; fetch line-offset
  230.     lea    (a5,d1.w),a2
  231.     move.w    d0,d1
  232.     lsr.w    #4,d0            ; + 2*(x/16)
  233.     add.w    d0,d0
  234.     add.w    d0,a2            ; -> a2 destination pointer
  235.     and.w    d5,d1            ; x & 15
  236.     ror.w    #4,d1            ; A/B shift (moved to bit 15-12)
  237.     move.l    d4,d0
  238.     or.w    d1,d0
  239.     swap    d0
  240.     move.w    d1,d0            ; d0 = BLTCON0 | BLTCON1
  241.     WAITBLIT
  242.     move.l    a0,BLTBPT(a6)        ; B = Image
  243.     move.l    a1,BLTAPT(a6)        ; A = Mask
  244.     move.l    a2,BLTCPT(a6)        ; C,D = Destination
  245.     move.l    a2,BLTDPT(a6)
  246.     move.l    d0,BLTCON0(a6)
  247.     move.w    d6,BLTSIZE(a6)        ; blit it!
  248.     dbf    d7,blit_loop
  249.  
  250.     move.w    #(SINTABVALS-1)<<1,d2    ; change rotation angles
  251.     movem.w alpha(a4),d0-d1     ; d0 = alpha, d1 = beta
  252.     addq.w    #2,d0
  253.     and.w    d2,d0
  254.     addq.w    #4,d1
  255.     and.w    d2,d1
  256.     movem.w d0-d1,alpha(a4)
  257.     rts
  258.  
  259.  
  260. initCopperLists:
  261.     move.w    #$0020,BEAMCON0(a6)    ; enable PAL-display (to get more time)
  262.     lea    clist1,a0
  263.     lea    dispmem1,a1
  264.     move.l    a1,workBitmap(a4)    ; work with Bitmap #1 first
  265.     movem.l a0-a1,DblBufInfo+dblbuf_CList(a4)  ; save CList and Bitmap poin-
  266.     bsr.s    initCList$               ;  ters for double buffering
  267.     lea    clist2,a0
  268.     lea    dispmem2,a1
  269.     move.l    a0,COP1LC(a6)        ; use copper list #2 first
  270.     tst.w    COPJMP1(a6)
  271.     movem.l a0-a1,DblBufInfo+dblbuf_SIZE+dblbuf_CList(a4)
  272. initCList$:
  273.     move.l    #INTREQ<<16|INTF_COPER,(a0)+ ; reset copper interrupt flag
  274.     move.w    #WIDTH,d0
  275.     move.w    #DISPHEIGHT,d1
  276.     move.w    #HSTART,d2
  277.     moveq    #VSTART,d3
  278.     moveq    #%1000|DEPTH,d4     ; Interleaved bitplanes!
  279.     bsr    demoStdView
  280.     lea    NrmColors(pc),a1
  281.     moveq    #16,d0
  282.     bsr    demoColors        ; init all 16 colors at top of frame
  283.     COPWAIT VSTART+MIRRSTART
  284.     move.l    #COLOR00<<16|$313,(a0)+ ; purple mirror surface
  285.     COPWAIT VSTART+MIRRSTART+29
  286.     lea    MirrColors(pc),a1
  287.     moveq    #14,d0
  288.     bsr    demoColors        ; reflected colors
  289.     COPWAIT VSTART+MIRRSTART+30
  290.     move.l    #(BPL1MOD<<16)|(((-2*DEPTH-1)*BPR)&$ffff),(a0)+ ; mirror effect
  291.     move.l    #(BPL2MOD<<16)|(((-2*DEPTH-1)*BPR)&$ffff),(a0)+
  292.     COPWAIT VSTART+DISPHEIGHT
  293.     move.l    #INTREQ<<16|$8000|INTF_COPER,(a0)+ ; invoke copper interrupt
  294.     COPEND                       ;  at end of display
  295.     rts
  296.  
  297.  
  298. make_ytable:                ; calculate an offset for any
  299.     lea    yoffsets(a4),a0     ;  y-coordinate
  300.     moveq    #0,d0
  301.     move.w    #HEIGHT-1,d1
  302. 1$:    move.w    d0,(a0)+
  303.     add.w    #BPR*DEPTH,d0
  304.     dbf    d1,1$
  305.     rts
  306.  
  307.  
  308. NrmColors:                ; normal Demo colors
  309.     dc.w    $000,$aad,$ddf,$88c,$66a,$459,$337,$226
  310.     dc.w    $114,$b8a,$a78,$857,$745,$534,$c22,$114
  311.  
  312. MirrColors:                ; Mirror colors
  313.     dc.w    $313,$988,$988,$869,$758,$646,$435,$324
  314.     dc.w    $114,$756,$645,$534,$423,$423
  315.  
  316.  
  317. vballcoords:                ; object coordinates
  318.     dc.w    -$b0,-$20,-$a0,-$20,-$90,-$10,-$b0,-$10         ;'P'(8)
  319.     dc.w    -$a0,0,-$b0,0,-$b0,$10,-$b0,$20
  320.     dc.w    -$70,-$20,-$70,-$10,-$70,0,-$70,$10,-$70,$20        ;'H'(11)
  321.     dc.w    -$60,0,-$50,-$20,-$50,-$10,-$50,0,-$50,$10,-$50,$20
  322.     dc.w    -$30,-$20,-$30,-$10,-$20,0,-$30,$10,-$30,$20        ;'X'(9)
  323.     dc.w    -$10,-$20,-$10,-$10,-$10,$10,-$10,$20
  324.     dc.w    $20,-$20,$10,-$10,$10,0,$10,$10,$10,$20         ;'A'(10)
  325.     dc.w    $20,0,$30,-$10,$30,0,$30,$10,$30,$20
  326.     dc.w    $70,-$20,$60,-$20,$50,-$10,$60,0            ;'S'(7)
  327.     dc.w    $70,$10,$60,$20,$50,$20
  328.     dc.w    $b0,-$20,$a0,-$20,$90,-$10,$a0,0            ;'S'(7)
  329.     dc.w    $b0,$10,$a0,$20,$90,$20
  330.  
  331.  
  332. SinCosTab:                ; Sine and Cosine table
  333. twopi    equ.d    6.2831853072
  334. ang    set.d    0
  335.     rept    SINTABVALS+SINTABVALS/4
  336. sinval    int    sin(ang)*32767
  337.     dc.w    sinval
  338. ang    set.d    ang+twopi/SINTABVALS
  339.     endr
  340.  
  341.  
  342.  
  343.  
  344.     section "__MERGED",bss
  345.  
  346. boblist:                ; (uninitialized) MinList for
  347.     ds.l    3            ;  depth-sorted Bob Nodes
  348. bobnodes:
  349.     ds.b    NUMOBS*bobc_SIZE    ; all Bob Nodes containing 3d coords
  350. alpha:
  351.     ds.w    1            ; rotation about x-axis
  352. beta:
  353.     ds.w    1            ; rotation about y-axis
  354. yoffsets:
  355.     ds.w    HEIGHT            ; Offset table for any y-coordinate
  356.  
  357. DblBufInfo:
  358.     ds.l    2*dblbuf_SIZE        ; copper list and bitmap pointers
  359. workBitmap:
  360.     ds.l    1            ; current Bitmap for drawing objects
  361. toggle:
  362.     ds.w    1            ; toggle switch for double buffering
  363.  
  364.  
  365.  
  366.  
  367.     section "VBallImage",data,chip
  368.  
  369. vballimg:                ; Ball image, interleaved
  370.     incbin    "VBallImage.ilvd"    ; size: 16x16x4 (mask appended)
  371.  
  372.  
  373.  
  374.  
  375.     section "CopperLists",bss,chip
  376.  
  377. clist1:
  378.     ds.l    2*DEPTH     ; BPLxPT
  379.     ds.l    7        ; BPLxMOD,DIWSTRT/STOP,DDFSTRT/STOP,BPLCON0
  380.     ds.l    1<<DEPTH    ; COLORxx
  381.     ds.l    28        ; mirror colors, mirror effect, etc. - some
  382.                 ;  extra longwords included :-)
  383.  
  384. clist2:
  385.     ds.l    2*DEPTH
  386.     ds.l    7
  387.     ds.l    1<<DEPTH
  388.     ds.l    28
  389.  
  390.  
  391.  
  392.  
  393.     section "DisplayMem_1",bss,chip
  394.  
  395. dispmem1:
  396.     ds.b    BPR*HEIGHT*DEPTH
  397.  
  398.  
  399.  
  400.  
  401.     section "DisplayMem_2",bss,chip
  402.  
  403. dispmem2:
  404.     ds.b    BPR*HEIGHT*DEPTH
  405.  
  406.  
  407.     end
  408.