      INCLUDE "EXEC/TYPES.I" 
      INCLUDE "GRAPHICS/GFX.I"
      INCLUDE "GRAPHICS/GFXBASE.I"
      INCLUDE "HARDWARE/CUSTOM.I"

 XREF  _AbsExecBase         ;exec library routines
 XREF  _LVOOpenLibrary
 XREF  _LVOCloseLibrary
 XREF  _LVOAllocMem 
 XREF  _LVOFreeMem
 XREF  _LVOPermit
 XREF  _LVOForbid
 XREF  _custom              ;custom register address



       CSECT text,code      ;omit this line for Metacomco or A68k assemblers
         
main:
  jsr     openlibraries     ;open the libraries
  tst.l   d0
  beq     exit              ;exit if there is an error
  jsr     init              ;initialise copper list
  tst.l   CListAddr
  beq     exit              ;exit if no copper list stored 
  jsr     _LVOForbid(a6)    ;disable task switching
  lea     _custom,a5        
  move.w  #$03a0,dmacon(a5) ;disable copper and bit plane DMA
  move.l  CListAddr,cop1lc(a5) ;put copper list address in register
  clr.w   copjmp1(a5)       ;trigger cooper activity
  move.w  #$8280,dmacon(a5) ;enable copper DMA
hangabout:
  jsr     playsig           ;is there a signal from the joysticks?
  tst.w   fire              ;quit if a fire button has been pressed
  bne     quit
  tst.w   fire2
  beq     hangabout
quit:  
  move.l   graphicsbase,a4
  move.l   gb_copinit(a4),cop1lc(a5) ;put old copper list in register
  clr.w    copjmp1(a5)       ;trigger copper activity
  move.w   #$83e0,dmacon(a5) ;enable all DMA
  jsr      _LVOPermit(a6)    ;enable task switching
  jsr     cleanup            ;free copper list memory
  jsr     closelibraries    ;close the libraries
exit:
  rts

init:
  move.l  _AbsExecBase,a6    ;allocate chip memory for copper list
  move.l  CListSize,d0
  move.l  #65539,d1
  jsr     _LVOAllocMem(a6)
  movem.l  d0,CListAddr      ;exit if no memory available
  beq     exit
  lea     CListStart,a0      ;copy copper list into chip memory
  move.l  CListAddr,a1
  move.l  CListSize,d0
  subi.l  #1,d0
CListCopy:
  move.b  (a0)+,(a1)+
  dbra     d0,CListCopy
  rts

cleanup:                     ;free copper list memory
  move.l   CListAddr,a1
  move.l   CListSize,d0
  jsr      _LVOFreeMem(a6)
  rts      


playsig:                    ;routine for reading both joysticks
 move.w   #0,xmove          ;clear previous values    
 move.w   #0,ymove
 move.w   #0,xmove2
 move.w   #0,ymove2
 move.w   #0,fire
 move.w   #0,fire2
 move.w   $dff00a,d0         ;move JOY0DAT to d0 (game port 0) 
 btst     #1,d0              ;test bit 1 for joystick right
 beq      left
 move.w   #1,xmove
left:
 btst     #9,d0              ;test bit 9 for joystick left
 beq      down
 move.w   #-1,xmove
down:
 move.w   d0,d1              ;copy d0 to d1
 lsr.w    #1,d1              ;move bit 1 to bit 0, and bit 9 to bit 8
 eor.w    d0,d1              ;exclusive OR d0 and d1
 btst     #0,d1              ;test bit 0 for joystick down
 beq      up
 move.w   #1,ymove
up:
 btst     #8,d1              ;test bit 8 for joystick up
 beq      jfire
 move.w   #-1,ymove
jfire:
 move.b   $bfe001,d1         ;CIA-A parallel port  A
 btst     #6,d1              ;test bit 6 for game port 0 fire button
 bne      skip
 move.w   #1,fire
skip:
 move.w   $dff00c,d0          ;move JOY1DAT to d0 (game port 1)    
 btst     #1,d0               ;test bit 1 for joystick right
 beq      left2              
 move.w   #1,xmove2
left2:
 btst     #9,d0               ;test bit 9 for joystick left
 beq      down2
 move.w   #-1,xmove2
down2:
 move.w   d0,d1                ;copy d0 to d1
 lsr.w    #1,d1                ;move bit 1 to bit 0,and bit 9 to bit 8
 eor.w    d0,d1                ;exclusive OR d0 and d1
 btst     #0,d1                ;test bit 0 for joystick down
 beq      up2
 move.w   #1,ymove2
up2:
 btst     #$8,d1               ;test bit 8 for joystick up
 beq      jfire2
 move.w   #-1,ymove2
jfire2:
 move.b   $bfe001,d1            ;CIA-A parallel port A
 btst     #7,d1                 ;test bit 7 for game port 1 fire button 
 bne      skip2
 move.w   #1,fire2
skip2:
 rts


openlibraries:            ;open graphics library
 move.l   _AbsExecBase,a6
 move.l   #graphicsname,a1
 clr.l    d0
 jsr      _LVOOpenLibrary(a6)
 movem.l  d0,graphicsbase
 rts

closelibraries:              ;close library
 move.l  _AbsExecBase,a6
 move.l  graphicsbase,a1
 jsr     _LVOCloseLibrary(a6)
 rts

                           ;SECTION  DATA,data
                           ;for Metacomco and A68k assemblers
      CSECT data           ;instead of this line           
graphicsname:
 dc.b  'graphics.library',0
 ds.w   0
graphicsbase:            ;stored library pointer
 ds.l 1
xmove:
 ds.w   1
ymove:
 ds.w   1
xmove2:
 ds.w   1
ymove2:
 ds.w   1
fire:
 ds.w   1
fire2:
 ds.w   1 
CListStart:             ;start of copper list
 dc.w   color,$0000     ;put black into background colour 
 dc.w   $100f,$fffe     ;wait until beam reaches line 16 
 dc.w   color,$0001     ;put dark blue into background colour
 dc.w   $200f,$fffe     ;etc
 dc.w   color,$0002
 dc.w   $300f,$fffe
 dc.w   color,$0003
 dc.w   $400f,$fffe
 dc.w   color,$0004
 dc.w   $500f,$fffe
 dc.w   color,$0005
 dc.w   $600f,$fffe
 dc.w   color,$0006
 dc.w   $700f,$fffe
 dc.w   color,$0007
 dc.w   $800f,$fffe
 dc.w   color,$0008
 dc.w   $900f,$fffe 
 dc.w   color,$0009
 dc.w   $a00f,$fffe
 dc.w   color,$000a
 dc.w   $b00f,$fffe
 dc.w   color,$000b
 dc.w   $c00f,$fffe
 dc.w   color,$000c
 dc.w   $d00f,$fffe
 dc.w   color,$000d
 dc.w   $e00f,$fffe
 dc.w   color,$000e
 dc.w   $f00f,$fffe
 dc.w   color,$000f
 dc.w   $ffff,$fffe    ;beam never gets to this position
CListSize:             ;size of copper list, 32 commands  * 4
 dc.l   128
CListAddr:             ;pointer to copper list
 ds.l    1 

 END 


