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

 XREF  _AbsExecBase         ;exec library routines
 XREF  _LVOOpenLibrary
 XREF  _LVOCloseLibrary


 XREF  _LVOLoadRGB4         ;graphics library routine
 XREF  _LVOBltBitMap
 XREF  _LVOInitView
 XREF  _LVOLoadView
 XREF  _LVOInitBitMap
 XREF  _LVOAllocRaster
 XREF  _LVOFreeRaster
 XREF  _LVOMakeVPort
 XREF  _LVOMrgCop
 XREF  _LVOInitVPort
 XREF  _LVOGetColorMap
 XREF  _LVOWaitTOF
 XREF  _LVOBltClear
 XREF  _LVOFreeColorMap
 XREF  _LVOFreeVPortCopLists
 XREF  _LVOInitRastPort
 XREF  _LVOSetRast
 XREF  _LVOSetAPen
 XREF  _LVORectFill

       CSECT text,code      ;omit this line for Metacomco or A68k assemblers
         
main:
  jsr     openlibraries     ;open the libraries
  tst.l   d0
  beq     abort1            ;exit if there is an error
  jsr     init              ;initialise the viewport
  tst.l   d0
  beq     abort1
  jsr     setcolours        ;set the colours
  move.l  gb_ActiView(a6),d0
  movem.l d0,oldview        ;store the old view address
  jsr     remakedisplay     ;load the new view
  jsr      stripes          ;put red stripes on tne screen
  clr.l    d6
  clr.l    d5
  move.w   100,d5
  move.w   100,d6 
hangabout:
  jsr     playsig           ;is there a signal from the joysticks?
  jsr     scroll            ;scroll thr display 
  tst.w   fire              ;quit if a fire button has been pressed
  bne     quit
  tst.w   fire2
  beq     hangabout
quit:
  move.l  graphicsbase,a6
  move.l  oldview,a1
  jsr     _LVOLoadView(a6)  ;replace the old view
  jsr     cleanup           ;free the memory used by the program
  jsr     closelibraries    ;close the libraries
  rts

scroll:
 add.w    xmove,d6          ;add the horizontal joystick movement 
 add.w    xmove2,d6         ;to the horizontal offset 
 cmp.w     #320,d6          ;test for screen limits
 bge       vertical
 tst.w    d6
 blt      vertical
 lea      rired,a5
 move.w   d6,ri_RxOffset(a5)    ;update the RasInfo x offset
vertical:
 add.w    ymove,d5          ;add the vertical joystick movement 
 add.w    ymove2,d5         ;to the vertical offset
 cmp.w     #200,d5          ;test for screen limits
 bge       scrollout
 tst.w    d5
 blt      scrollout
 lea      rired,a5
 move.w   d5,ri_RyOffset(a5)    ;update the RasInfo y offset
scrollout:
 jsr      remakedisplay     
  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


init:
 move.l   graphicsbase,a6
 lea      myview,a1         ;initialise a view structure,default values
 jsr      _LVOInitView(a6)
 lea      myview,a5
 move.l   #viewport,d0
 move.l   d0,v_ViewPort(a5) ;put in the viewport
 lea      viewport,a0       ;initialise it
 jsr      _LVOInitVPort(a6)
 lea      viewport,a5       ;set up the viewport's parameters
 move.w   #320,vp_DWidth(a5)
 move.w   #200,vp_DHeight(a5)
 move.w   #0,vp_DyOffset(a5) ;no x and y offsets
 move.w   #0,vp_DxOffset(a5)
 move.l   #0,vp_Next(a5)    ;this is the only viewport
 move.l   #rired,d0         ;attach the rasinfo structure
 move.l   d0,vp_RasInfo(a5)
 move.w   #0,vp_Modes(a5)   ;lo-res, no special features
 move.l   #32,d0            ;colourmap
 jsr      _LVOGetColorMap(a6)
 movem.l  d0,colourmap2
 move.l   d0,a5
 move.l   cm_ColorTable(a5),d1
 movem.l  d1,colourtable2
 lea      viewport,a5
 move.l   d0,vp_ColorMap(a5)
 lea      BMred,a0        ;the bitmap for the red playfield
 move.b   #3,d0
 move.w   #640,d1
 move.w   #600,d2
 jsr      _LVOInitBitMap(a6)
 lea      BMred,a5
 move.l   #2,d5
 lea      bm_Planes(a5),a4
redraster:                 ;allocate the bitplanes
 move.l   #640,d0
 move.l   #600,d1
 jsr      _LVOAllocRaster(a6)
 tst.l    d0
 beq      abort1
 move.l   d0,(a4)+
 dbra     d5,redraster
 rts

setcolours:             ;read each colourtable into the colourtable
 move.l   #31,d0
 lea      colours2,a5
 move.l   colourtable2,a4
colourloop2:
 move.w   (a5)+,(a4)+
 dbra     d0,colourloop2
 rts

remakedisplay:
 lea      myview,a0         ;set up the copper list for each viewport
 lea      viewport,a1
 jsr      _LVOMakeVPort(a6)
 lea      myview,a1        ;merge these copper lists
 jsr      _LVOMrgCop(a6)
 jsr      _LVOWaitTOF(a6)  ;wait for the top of the frame
 lea      myview,a1        ;load the new view
 jsr      _LVOLoadView(a6)
 rts
  
stripes:
 lea       redrastport,a1
 jsr      _LVOInitRastPort(a6)
 move.l   #BMred,rp_BitMap(a1)
 clr.l    d4
 clr.l    d1
 clr.l    d5
 move.l   #20,d2
 move.l   #399,d3
rstripe:
 movem.l   d0-d7/a0-a6,-(sp)
 move.l    d5,d0
 andi.l     #7,d0
 jsr      _LVOSetAPen(a6)
 movem.l   (sp)+,d0-d7/a0-a6
 move.l   d4,d0
 movem.l   d0-d7/a0-a6,-(sp)
 jsr      _LVORectFill(a6)   ;fill a vertical stripe
 movem.l   (sp)+,d0-d7/a0-a6
 add.l     #20,d2
 add.l     #20,d4
 add.l     #1,d5
 cmp.l     #31,d5
 bne       rstripe
 clr.l    d4
 clr.l    d1
 clr.l    d5
 move.l   #639,d2
 move.l   #10,d3
rstripe2:
 movem.l   d0-d7/a0-a6,-(sp)
 move.l    d5,d0
 and.l     #7,d0
 jsr      _LVOSetAPen(a6)
 movem.l   (sp)+,d0-d7/a0-a6
 move.l   d4,d0
 movem.l   d0-d7/a0-a6,-(sp)
 jsr      _LVORectFill(a6)   ;fill a horizontal stripe
 movem.l   (sp)+,d0-d7/a0-a6
 add.l     #20,d1
 add.l     #20,d3
 add.l     #1,d5
 cmp.l     #19,d5
 bne       rstripe2
 rts


cleanup:
 lea      viewport,a0
 jsr      _LVOFreeVPortCopLists(a6)
 move.l   colourmap2,a0
 jsr      _LVOFreeColorMap(a6)
 lea      BMred,a5
 move.l   #2,d5
 lea      bm_Planes(a5),a4
cleanred:
 move.l   (a4)+,a0
 move.l   #640,d0
 move.l   #600,d1
 jsr      _LVOFreeRaster(a6)
 add.l    #4,a0
 dbra     d5,cleanred
 rts
abort1:
 rts

openlibraries:            ;open graphics library
 move.l   _AbsExecBase,a6
 move.l   #graphicsname,a1
 clr.l    d0
 jsr      _LVOOpenLibrary(a6)
 movem.l  d0,graphicsbase
 tst.l    d0
 beq      abort1
 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
rired:                     ;RasInfo structure for viewport
 dc.l   0                  ;no next RasInfo
 dc.l   BMred              ;pointer to bitmap
 dc.w   100                ;x offset 
 dc.w   100                ;y offset 
viewport:                  ;space for viewport structure
 ds.b   vp_SIZEOF          ;initialised by the program
 ds.w   0
colours2:
 dc.w   0,$f00,$e00,$c00,$a00,$800,$600,$400
 dc.w   $eee,$ccc,$aaa,$999,$777,$666,$444,$ddd
 dc.w   0,0,0,0,0,0,0,0
 dc.w   0,0,0,0,0,0,0,0
oldview:                  ;oldview address
 ds.l 1
colourtable2:
 ds.l 1
colourmap2:
 ds.l 1
BMred:                   ;space for bitmap strucures
 ds.b   bm_SIZEOF        ;initialised by the program
 ds.w   0
redrastport:
 ds.b   rp_SIZEOF
 ds.w   0
myview:                  ;view structure
 ds.b   v_SIZEOF
 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 
 END 


