      INCLUDE "EXEC/TYPES.I"

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

 XREF  _AbsExecBase             ;library references
 XREF  _LVOCloseLibrary
 XREF  _LVOOpenLibrary
 XREF  _LVOBltClear
 XREF  _LVOLoadView
 XREF  _LVOInitBitMap
 XREF  _LVOFreeRaster
 XREF  _LVOAllocRaster
 XREF  _LVOExit
 XREF  _LVOAllocMem 
 XREF  _LVOFreeMem
 XREF  _LVOPermit
 XREF  _LVOForbid
 XREF  _custom              ;custom register address

 XREF   readfile

 XDEF  dosbase

       CSECT text,code      ;omit this line for Metacomco or A68k assembler
main:
  jsr     openlibraries     ;open the libraries
  tst.l   d0
  beq     exit              ;exit if there is an error
  move.l  graphicsbase,a6
  move.l  gb_ActiView(a6),d0
  movem.l d0,oldview        ;save old view address
  jsr     init              ;initialise screan raster
  tst.l   d0
  beq     exit
  move.l  #filename,d0      ;in this case the first sprite, 'punk'
  move.l  #Spritedata2,d1   ;address of sprite data
  add.l   #4,d1             ;allow for 2 control words at beginning
  move.l  #colourlist16,d2  ;list for colours for sprites 1 and 2
  jsr     readfile          ;read the sprite file using  module spriteread
  andi.b  #255,d0           ;sprite height
  lea     Spritedata2,a0    ;control words address
  move.b  Spritedata2,d2    ;add sprite start position to height 
  add.b   d2,d0
  move.b  d0,2(a0)          ;put sprite finish in second control word
  move.l  #filename2,d0     ;in this case the second sprite, 'plain'
  move.l  #Spritedata3,d1   ;address of sprite data
  add.l   #4,d1
  move.l  #colourlist20,d2  ;list for colours for sprites 3 and 4
  jsr     readfile          ;read the sprite file using  module spriteread
  andi.b  #255,d0           ;sprite height
  lea     Spritedata3,a0    ;control words address
  move.b  Spritedata3,d2    ;add sprite start position to height
  add.b   d2,d0
  move.b  d0,2(a0)          ;put sprite finish in second control word
  jsr     clistinit         ;initialise copper list     
  tst.l   CListAddr         ;exit if no memory for copper list
  beq     exit
  move.l   _AbsExecBase,a6
  jsr     _LVOForbid(a6)    ;disable task switching
  lea     _custom,a5        
  move.w  #$03a0,dmacon(a5) ;disable copper and bit plane DMA
  move.l  #31,d0            ;copy colours into register  
  lea     color(a5),a1
  lea     colourlist,a2
copycols:
  move.w  (a2)+,(a1)+
  dbra    d0,copycols
  move.l  CListAddr,cop1lc(a5) ;put copper list address in register
  clr.w   copjmp1(a5)          ;trigger copper activity
  move.w  #$3081,diwstrt(a5)   ;set start and finish of display area
  move.w  #$30c1,diwstop(a5)
  move.w  #$0038,ddfstrt(a5)   ;set bit plane start and finish
  move.w  #$00d0,ddfstop(a5)   
  move.w  #$1200,bplcon0(a5)   ;one bitplane, colour display
  clr.w   bplcon1(a5)          ;no scrolling
  clr.w   bpl1mod(a5)          ;null modulos
  clr.w   bpl2mod(a5)
  move.w  #$83f0,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   #$83f0,dmacon(a5) ;enable all DMA
  move.l   _AbsExecBase,a6
  jsr      _LVOPermit(a6)    ;enable task switching
quit2:
  move.l  _AbsExecBase,a6
  jsr      clistcleanup      ;clear copperlist memory
  move.l  oldview,a1         ;replace old view
  move.l  graphicsbase,a6 
  jsr     _LVOLoadView(a6) 
  jsr     cleanup            ;release memory 
  jsr     closelibraries     ;close the libraries
exit:
  rts

init:                          ;allocate memory for the screen
  move.l  graphicsbase,a6
  move.l  #320,d0
  move.l  #256,d1
  jsr     _LVOAllocRaster(a6)
  tst.l   d0
  beq     exit
  movem.l d0,screenadd
  rts

clistinit:
  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
  move.l  #520*8,d0
  move.l  #65539,d1
  jsr     _LVOAllocMem(a6)
  movem.l  d0,Spriteadd      ;exit if no memory available
  beq     exit
  lea     Spritedata1,a1     ;copy sprite data into chip memory
  move.l  #520*8-1,d6        ;see sprite data  in data section 
  move.l  d0,a2
Spritecopy:
  move.b  (a1)+,(a2)+
  dbra     d6,Spritecopy
  move.l   #7,d6             ;eight sprites
  move.l  CListAddr,a0       ;start the sprite copper list
  move.l  Spriteadd,a3       ;address of start of sprite data
  move.w  #sprpt,d3          ;register address of first sprite pointer
sprlist:
  move.l  a3,d1              ;next sprite data address
  move.w  d3,(a0)+           ;copy next regster address into copper list
  add.w   #2,d3              ;update register address
  swap    d1
  move.w  d1,(a0)+           ;copy high word of sprite data address
  move.w  d3,(a0)+           ;copy next register address into copper list
  add.w   #2,d3              ;update register address
  swap    d1
  move.w  d1,(a0)+           ;copy low word of sprite data address
  add.l   #520,a3            ;until all sprites are processed
  dbra    d6,sprlist
  lea     screenadd,a3
  move.w  #bplpt,d3          ;register address for bit plane pointer
  move.l  (a3),d1            ;bit plane address
  move.w  d3,(a0)+           ;copy next register address into copper list 
  add.w   #2,d3              ;update register address
  swap    d1
  move.w  d1,(a0)+           ;copy high word of bit plane address
  move.w  d3,(a0)+           ;copy next register address into copper list
  add.w   #2,d3              ;update register address
  swap    d1
  move.w  d1,(a0)+           ;copy low word of bit plane address
  move.l  #$fffffffe,(a0)
  rts

clistcleanup:                     ;free copper list memory
  move.l   _AbsExecBase,a6
  move.l   CListAddr,a1
  move.l   CListSize,d0
  jsr      _LVOFreeMem(a6)
  move.l   #8*520,d0
  move.l   Spriteadd,a1
  jsr      _LVOFreeMem(a6)
  rts      

openlibraries:                    ;dos library for file reading
  move.l  _AbsExecBase,a6
  move.l  #dosname,a1
  clr.l   d0
  jsr     _LVOOpenLibrary(a6)
  movem.l d0,dosbase
  tst.l   d0
  beq     exit
  move.l  #graphicsname,a1        ;graphics lirary for commands and pointers
  clr.l   d0
  jsr     _LVOOpenLibrary(a6)
  movem.l d0,graphicsbase
  rts

cleanup:                     ;release raster memory
  move.l  graphicsbase,a6
  move.l  screenadd,a0
  move.l  #320,d0
  move.l  #256,d1
  jsr     _LVOFreeRaster(a6)
  rts

closelibraries:              
  move.l  _AbsExecBase,a6
  move.l  graphicsbase,a1
  jsr     _LVOCloseLibrary(a6)
  move.l  dosbase,a1
  jsr     _LVOCloseLibrary(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


 CSECT  data
screenadd:
 ds.l   1  
filename:                              ;filenames of IFF sprite files
 dc.b  'punk',0
 ds.w    0
filename2:
 dc.b  'plain',0
 ds.w    0
dosname:
 dc.b  'dos.library',0                 ;library names
 ds.w   0
graphicsname:
 dc.b  'graphics.library',0
 ds.w   0
graphicsbase:             ;library addresses              
 ds.l    1
dosbase:
 ds.l    1
oldview:                  ;address of old view
 ds.l    1
colourlist:
 dc.w 0,0,0,0,0,0,0,0
 dc.w 0,0,0,0,0,0,0,0
colourlist16:             
 dc.w $0,15,10,5            ;sprites 1 & 2 
colourlist20:
 dc.w $0,$f00,$a00,$500            ;sprites 3 & 4 
 dc.w $000,$077,$444,$777 ;sprites 5 & 6 - cyan,dark grey, light grey
 dc.w $000,$fc0,$f77,$0ff ;sprites 7 & 8 - orange,pink,cyan
xmove:                                 ;joystick data
 ds.w   1
ymove:
 ds.w   1
xmove2:
 ds.w   1
ymove2:
 ds.w   1
fire:
 ds.w   1
fire2:
 ds.w   1 
Spritedata1:             ;start of sprite data
 dc.w   $a05a,$c400 
 dc.w   0,$0440
 dc.w   0,$07c0
 dc.w   0,$0540
 dc.w   0,$0100
 dc.w   0,$07c0
 dc.w   0,$1ff0
 dc.w   $3ff8,$3ff8
 dc.w   $7ffc,$7ffc
 dc.w   $fffe,$fffe
 dc.w   $fffe,$fffe
 dc.w   $e31e,$e31e
 dc.w   $e31e,$e31e
 dc.w   $e73e,$e73e
 dc.w   $fffe,$fffe
 dc.w   $fffe,$fffe
 dc.w   $a492,$a492
 dc.w   $a492,$a492
 dc.w   $b6da,$b6da
 dc.w   $924a,$924a
 dc.w   $d24a,$d24a
 dc.w   0,0
 ds.w   216
Spritedata2:             ;start of sprite data space
 dc.w   $a06a,$a000      ;control words, start positions 
 dc.w   0,0 
 ds.w   254              ;data read in from file
 dc.w   0,0
Spritedata3:             ;start of sprite data
 dc.w   $b07a,$0000      ;control words,start positions  
 dc.w   0,0
 ds.w   254              ;data read in from file
 dc.w   0,0
Spritedata4:             ;start of sprite data
 dc.w   $b08a,$c400 
 dc.w   $0440,0
 dc.w   $07c0,0
 dc.w   $0540,0
 dc.w   $0100,0
 dc.w   $07c0,0
 dc.w   $1ff0,0
 dc.w   $3ff8,0
 dc.w   $7ffc,0
 dc.w   $fffe,0
 dc.w   $fffe,0
 dc.w   $e31e,0
 dc.w   $e31e,0
 dc.w   $e73e,0
 dc.w   $fffe,0
 dc.w   $fffe,0
 dc.w   $a492,0
 dc.w   $a492,0
 dc.w   $b6da,0
 dc.w   $924a,0
 dc.w   $d24a,0
 dc.w   0,0
 ds.w   216
Spritedata5:             ;start of sprite data
 dc.w   $c09a,$d400 
 dc.w   $0440,0
 dc.w   $07c0,0
 dc.w   $0540,0
 dc.w   $0100,0
 dc.w   $07c0,0
 dc.w   $1ff0,0
 dc.w   $3ff8,0
 dc.w   $7ffc,0
 dc.w   $fffe,0
 dc.w   $fffe,0
 dc.w   $e31e,0
 dc.w   $e31e,0
 dc.w   $e73e,0
 dc.w   $fffe,0
 dc.w   $fffe,0
 dc.w   $a492,0
 dc.w   $a492,0
 dc.w   $b6da,0
 dc.w   $924a,0
 dc.w   $d24a,0
 dc.w   0,0
 ds.w   216
Spritedata6:             ;start of sprite data
 dc.w   $c0aa,$d400 
 dc.w   0,$0440
 dc.w   0,$07c0
 dc.w   0,$0540
 dc.w   0,$0100
 dc.w   0,$07c0
 dc.w   0,$1ff0
 dc.w   0,$3ff8
 dc.w   0,$7ffc
 dc.w   $fffe,$fffe
 dc.w   $fffe,$fffe
 dc.w   $e31e,$e31e
 dc.w   $e31e,$e31e
 dc.w   $e73e,$e73e
 dc.w   $fffe,$fffe
 dc.w   $fffe,$fffe
 dc.w   $a492,0
 dc.w   $a492,0
 dc.w   $b6da,0
 dc.w   $924a,0
 dc.w   $d24a,0
 dc.w   0,0
 ds.w   216
Spritedata7:             ;start of sprite data
 dc.w   $d0ba,$e400 
 dc.w   0,$0440
 dc.w   0,$07c0
 dc.w   0,$0540
 dc.w   0,$0100
 dc.w   0,$07c0
 dc.w   0,$1ff0
 dc.w   0,$3ff8
 dc.w   0,$7ffc
 dc.w   $fffe,$fffe
 dc.w   $fffe,$fffe
 dc.w   $e31e,$e31e
 dc.w   $e31e,$e31e
 dc.w   $e73e,$e73e
 dc.w   $fffe,$fffe
 dc.w   $fffe,$fffe
 dc.w   $a492,0
 dc.w   $a492,0
 dc.w   $b6da,0
 dc.w   $924a,0
 dc.w   $d24a,0
 dc.w   0,0
 ds.w   216
Spritedata8:             ;start of sprite data
 dc.w   $d0ca,$e400 
 dc.w   $0440,0
 dc.w   $07c0,0
 dc.w   $0540,0
 dc.w   $0100,0
 dc.w   $07c0,0
 dc.w   $1ff0,0
 dc.w   $3ff8,0
 dc.w   $7ffc,0
 dc.w   $fffe,$fffe
 dc.w   $fffe,$fffe
 dc.w   $e31e,$e31e
 dc.w   $e31e,$e31e
 dc.w   $e73e,$e73e
 dc.w   $fffe,$fffe
 dc.w   $fffe,$fffe
 dc.w   $a492,0
 dc.w   $a492,0
 dc.w   $b6da,0
 dc.w   $924a,0
 dc.w   $d24a,0
 dc.w   0,0
 ds.w   216
CListSize:             ;size of copper list, at least 64 commands  * 4
 dc.l   1024            ;plus 4 * 8 for bitplane pointers, + 1 for last 
CListAddr:             ;pointer to copper list
 ds.l    1 
Spriteadd:
 ds.l    1
 END 

       
