; -graphics primitives
;assembly source code
      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  _LVODelay            ;dos library routine

 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

main:
  jsr     openlibraries     ;open the libraries
  tst.l   d0
  beq     abort1            ;exit if there is an error
  jsr     init              ;initialise the viewports
  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     greyarea          ;make the grey playfield into a window frame
  jsr     stripes           ;coloured vertical stripes
  move.l  #200,d1
  move.l  dosbase,a6
  jsr     _LVODelay(a6)     ;delay a few seconds
  jsr     cyclecolours      ;cycle the reds,blues and greens
  move.l  #200,d1
  move.l  dosbase,a6
  jsr     _LVODelay(a6)     ;delay a few seconds
  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

init:
 move.l   graphicsbase,a6
 lea      myview,a1         ;initialise a view structure,default values
 jsr      _LVOInitView(a6)
 lea      myview,a5
 move.l   #vpblue,d0
 move.l   d0,v_ViewPort(a5) ;put in the name of the first viewport
 lea      vpblue,a0
 jsr      _LVOInitVPort(a6) ;initialise the first viewport
 lea      vpblue,a5
 move.w   #320,vp_DWidth(a5) ;width
 move.w   #48,vp_DHeight(a5) ;height
 move.w   #0,vp_DyOffset(a5) ;starting at the top left hand corner
 move.w   #0,vp_DxOffset(a5)
 move.l   #vpredandgrey,vp_Next(a5) ;the next viewport on the list
 move.l   #riblue,d0
 move.l   d0,vp_RasInfo(a5) ;attach the rasinfo structure
 move.l   #32,d0            ;get a pointer a colourmap
 jsr      _LVOGetColorMap(a6)
 movem.l  d0,colourmap      ;store it
 move.l   d0,a5
 move.l   cm_ColorTable(a5),d1
 movem.l  d1,colourtable   ;get the pointer to the colourmap's colourtable
 lea      vpblue,a5
 move.l   d0,vp_ColorMap(a5) ;attach the colourmap to the viewport
 lea      BMblue,a0
 move.b   #3,d0
 move.w   #320,d1
 move.w   #48,d2
 jsr      _LVOInitBitMap(a6) ;initialise the blue bitmap (320 x 48 x 3)
 lea      BMblue,a5
 move.l   #2,d5
 lea      bm_Planes(a5),a4
blueraster:                ;allocate memory for all the blue bitplanes
 move.l   #320,d0
 move.l   #48,d1
 jsr      _LVOAllocRaster(a6)
 tst.l    d0
 beq      abort1
 move.l   d0,(a4)+
 dbra     d5,blueraster

 lea      vpredandgrey,a0 ;set up the second viewport with dual playfields
 jsr      _LVOInitVPort(a6)
 lea      vpredandgrey,a5  ;set up the viewport's parameters
 move.w   #320,vp_DWidth(a5)
 move.w   #100,vp_DHeight(a5)
 move.w   #50,vp_DyOffset(a5) ;starts at line 50
 move.w   #0,vp_DxOffset(a5)
 move.l   #vpgreen,vp_Next(a5) ;next viewport on the list
 move.l   #rired,d0       ;attach the first rasinfo structure
 move.l   d0,vp_RasInfo(a5)
 move.w   #(V_DUALPF+V_PFBA),vp_Modes(a5) ;dualplayfields,2nd field in front
 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      vpredandgrey,a5
 move.l   d0,vp_ColorMap(a5)
 lea      BMgrey,a0       ;the bitmap for the grey playfield
 move.b   #3,d0
 move.w   #320,d1
 move.w   #100,d2
 jsr      _LVOInitBitMap(a6)
 lea      BMgrey,a5
 move.l   #2,d5
 lea      bm_Planes(a5),a4
greyraster:                 ;grey bitplanes
 move.l   #320,d0
 move.l   #100,d1
 jsr      _LVOAllocRaster(a6)
 tst.l    d0
 beq      abort1
 move.l   d0,(a4)+
 dbra     d5,greyraster
 lea      BMred,a0        ;the bitmap for the red playfield
 move.b   #3,d0
 move.w   #320,d1
 move.w   #100,d2
 jsr      _LVOInitBitMap(a6)
 lea      BMred,a5
 move.l   #2,d5
 lea      bm_Planes(a5),a4
redraster:                 ;red bitplanes
 move.l   #320,d0
 move.l   #100,d1
 jsr      _LVOAllocRaster(a6)
 tst.l    d0
 beq      abort1
 move.l   d0,(a4)+
 dbra     d5,redraster

 lea      vpgreen,a0      ;set up third viewport
 jsr      _LVOInitVPort(a6)
 lea      vpgreen,a5
 move.w   #320,vp_DWidth(a5)
 move.w   #48,vp_DHeight(a5)
 move.w   #152,vp_DyOffset(a5) ;starts at line 152
 move.w   #0,vp_DxOffset(a5)
 move.l   #0,vp_Next(a5)
 move.l   #rigreen,d0
 move.l   d0,vp_RasInfo(a5)
 move.w   #0,vp_Modes(a5)
 move.l   #32,d0
 jsr      _LVOGetColorMap(a6)
 movem.l  d0,colourmap3
 move.l   d0,a5
 move.l   cm_ColorTable(a5),d1
 movem.l  d1,colourtable3
 lea      vpgreen,a5
 move.l   d0,vp_ColorMap(a5)
 lea      BMgreen,a0
 move.b   #5,d0             ;five bitplanes and 32 colours
 move.w   #320,d1
 move.w   #48,d2
 jsr      _LVOInitBitMap(a6)
 lea      BMgreen,a5
 move.l   #4,d5
 lea      bm_Planes(a5),a4
greenraster:
 move.l   #320,d0
 move.l   #48,d1
 jsr      _LVOAllocRaster(a6)
 tst.l    d0
 beq      abort1
 move.l   d0,(a4)+
 dbra     d5,greenraster
 rts

setcolours:             ;read each colourtable into the colourtable
 move.l   #31,d0        ;of the viewports colourmap
 lea      colours,a5
 move.l   colourtable,a4
colourloop:
 move.w   (a5)+,(a4)+
 dbra     d0,colourloop
 move.l   #31,d0
 lea      colours2,a5
 move.l   colourtable2,a4
colourloop2:
 move.w   (a5)+,(a4)+
 dbra     d0,colourloop2
 move.l   #31,d0
 lea      colours3,a5
 move.l   colourtable3,a4
colourloop3:
 move.w   (a5)+,(a4)+
 dbra     d0,colourloop3
 rts

remakedisplay:
 lea      myview,a0         ;set up the copper list for each viewport
 lea      vpblue,a1
 jsr      _LVOMakeVPort(a6)
 lea      myview,a0
 lea      vpredandgrey,a1
 jsr      _LVOMakeVPort(a6)
 lea      myview,a0
 lea      vpgreen,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
  
cyclecolours:
  move.l  #64,d7           ;64 times
cycloop:
  move.l  #20,d1
  move.l  dosbase,a6
  jsr     _LVODelay(a6)    ;short delay
  jsr     cycle            ;cycle the colours
  dbra    d7,cycloop
  rts
cycle:
 move.l   graphicsbase,a6  ;cycle the first 8 colours of each colourtable
 lea      vpblue,a0
 move.l   colours,d1
 lea      colours,a5
 jsr      minicycle
 move.l   colours2,d1
 lea      colours2,a5
 lea      vpredandgrey,a0
 jsr      minicycle
 move.l   colours3,d1
 lea      colours3,a5
 lea      vpgreen,a0
 jsr      minicycle
 rts
minicycle:
 move.l   a5,a1
 lea      4(a5),a4
 move.l   (a4)+,(a5)+
 move.l   (a4)+,(a5)+
 move.l   (a4)+,(a5)+
 move.l    d1,(a5)
 move.l    #8,d0
 jsr       _LVOLoadRGB4(a6)   ;load the new colours
 rts
greyarea:
 move.l   graphicsbase,a6     ;initialise a rastport
 lea      greyrastport,a1
 jsr      _LVOInitRastPort(a6)
 move.l   #BMgrey,rp_BitMap(a1) ;attach the grey bitmap to it
 lea      greyrastport,a1
 move.l   #4,d0
 jsr      _LVOSetAPen(a6)     ;set the foreground pen to colour 4
 lea      greyrastport,a1
 move.l   #0,d0
 move.l   #319,d2
 move.l   #0,d1
 move.l   #100,d3
 jsr      _LVORectFill(a6)   ;fill a large rectangle
 lea      greyrastport,a1
 move.l   #0,d0
 jsr      _LVOSetAPen(a6)    ;set the foreground pen to colour 0
 lea      greyrastport,a1
 move.l   #100,d0
 move.l   #196,d2
 move.l   #20,d1
 move.l   #80,d3
 jsr      _LVORectFill(a6)   ;fill a small rectangle
 rts
stripes:
 lea      bluerastport,a1
 jsr      _LVOInitRastPort(a6)
 move.l   #BMblue,rp_BitMap(a1)
 clr.l    d4
 clr.l    d1
 clr.l    d5
 move.l   #39,d2
 move.l   #47,d3
bstripe:
 movem.l   d0-d7/a0-a6,-(sp)
 move.l    d5,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     #40,d2
 add.l     #40,d4
 add.l     #1,d5
 cmp.l     #8,d5
 bne       bstripe
 lea       redrastport,a1
 jsr      _LVOInitRastPort(a6)
 move.l   #BMred,rp_BitMap(a1)
 move.l   #100,d4
 clr.l    d1
 clr.l    d5
 move.l   #112,d2
 move.l   #99,d3
rstripe:
 movem.l   d0-d7/a0-a6,-(sp)
 move.l    d5,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     #12,d2
 add.l     #12,d4
 add.l     #1,d5
 cmp.l     #8,d5
 bne       rstripe
 lea       greenrastport,a1
 jsr      _LVOInitRastPort(a6)
 move.l   #BMgreen,rp_BitMap(a1)
 clr.l    d4
 clr.l    d1
 clr.l    d5
 move.l   #9,d2
 move.l   #47,d3
gstripe:
 movem.l   d0-d7/a0-a6,-(sp)
 move.l    d5,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     #10,d2
 add.l     #10,d4
 add.l     #1,d5
 cmp.l     #32,d5
 bne       gstripe
 rts
cleanup:
 lea      vpblue,a0          ;free all copperlists,colourmaps and rasters
 jsr      _LVOFreeVPortCopLists(a6)
 move.l   colourmap,a0
 jsr      _LVOFreeColorMap(a6)
 lea      BMblue,a5
 move.l   #2,d5
 lea      bm_Planes(a5),a4
cleanblue:
 move.l   (a4)+,a0
 move.l   #320,d0
 move.l   #48,d1
 jsr      _LVOFreeRaster(a6)
 dbra     d5,cleanblue
 lea      vpredandgrey,a0
 jsr      _LVOFreeVPortCopLists(a6)
 move.l   colourmap2,a0
 jsr      _LVOFreeColorMap(a6)
 lea      BMgrey,a5
 move.l   #2,d5
 lea      bm_Planes(a5),a4
cleangrey:
 move.l   (a4)+,a0
 move.l   #320,d0
 move.l   #100,d1
 jsr      _LVOFreeRaster(a6)
 add.l    #4,a0
 dbra     d5,cleangrey
 lea      BMred,a5
 move.l   #2,d5
 lea      bm_Planes(a5),a4
cleanred:
 move.l   (a4)+,a0
 move.l   #320,d0
 move.l   #100,d1
 jsr      _LVOFreeRaster(a6)
 add.l    #4,a0
 dbra     d5,cleanred
 lea      vpgreen,a0
 jsr      _LVOFreeVPortCopLists(a6)
 move.l   colourmap3,a0
 jsr      _LVOFreeColorMap(a6)
 lea      BMgreen,a5
 move.l   #4,d5
 lea      bm_Planes(a5),a4
cleangreen:
 move.l   (a4)+,a0
 move.l   #320,d0
 move.l   #48,d1
 jsr      _LVOAllocRaster(a6)
 tst.l    d0
 beq      abort1
 add.l    #4,a0
 dbra     d5,cleangreen
 rts
abort1:
 rts
openlibraries:            ;open dos and graphics libraries
 move.l   _AbsExecBase,a6
 move.l   #dosname,a1
 clr.l    d0
 jsr      _LVOOpenLibrary(a6)
 movem.l  d0,dosbase
 tst.l    d0
 beq      abort1
 move.l   #graphicsname,a1
 clr.l    d0
 jsr      _LVOOpenLibrary(a6)
 movem.l  d0,graphicsbase
 tst.l    d0
 beq      abort1
 rts

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

 CSECT  data

 
dosname:
 dc.b  'dos.library',0
 ds.w   0
graphicsname:
 dc.b  'graphics.library',0
 ds.w   0
riblue:                    ;rasinfo structure for first viewport
 dc.l   0                  ;no next rasinfo
 dc.l   BMblue             ;pointer to blue bitmap
 ds.w   2                  ;zero x and y offsets
rired:                     ;rasinfo structure for second viewport
 dc.l   rigrey             ;pointer to next rasinfo, for 2nd playfield
 dc.l   BMred              ;pointer to red bitmap
 ds.w   2
rigrey:
 dc.l   0                  ;no next rasinfo
 dc.l   BMgrey             ;pointer to grey bitmap
 ds.w   2
rigreen:                   ;rasinfo structure for third viewport
 dc.l   0
 dc.l   BMgreen            ;pointer to green bitmap
 ds.w   2
vpredandgrey:              ;space for viewport structures
 ds.b   vp_SIZEOF          ;initialised by the program
 ds.w   0
vpblue:
 ds.b   vp_SIZEOF
 ds.w   0
vpgreen:
 ds.b   vp_SIZEOF
 ds.w   0
colours:                   ;colourtables
 dc.w   $0,$00f,$00e,$00c,$00a,$008,$006,$004
 dc.w   0,0,0,0,0,0,0,0
 dc.w   0,0,0,0,0,0,0,0
 dc.w   0,0,0,0,0,0,0,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
colours3:
 dc.w   0,$0f0,$0e0,$0c0,$0a0,$080,$060,$040
 dc.w   $0f1,$0f2,$0f3,$0f4,$0f5,$0f6,$0f7,$0f8
 dc.w   $0c1,$0c2,$0c3,$0c4,$0c5,$0c6,$0c7,$0c8
 dc.w   $081,$082,$083,$084,$085,$086,$087,$088

 SECTION   mem,BSS
oldview:                  ;oldview address
 ds.l 1
colourtable:              ;colourtable addresses
 ds.l 1
colourtable2:
 ds.l 1
colourtable3:
 ds.l 1
colourmap:                ;colourmap addresses
 ds.l 1
colourmap2:
 ds.l 1
colourmap3:
 ds.l 1
BMred:                   ;space for bitmap strucures
 ds.b   bm_SIZEOF        ;initialised by the program
 ds.w   0
BMgrey:
 ds.b   bm_SIZEOF
 ds.w   0
BMblue:
 ds.b   bm_SIZEOF
 ds.w   0
BMgreen:
 ds.b   bm_SIZEOF
 ds.w   0
bluerastport:            ;rastport structures
 ds.b   rp_SIZEOF
 ds.w   0
redrastport:
 ds.b   rp_SIZEOF
 ds.w   0
greyrastport:
 ds.b   rp_SIZEOF
 ds.w   0
greenrastport:
 ds.b   rp_SIZEOF
 ds.w   0
myview:                  ;view structure
 ds.b   v_SIZEOF
 ds.w   0
dosbase:                 ;stored library pointers
 ds.l 1
graphicsbase:
 ds.l 1

