 incdir include_i:
 include exec/exec.i
 include LVOS.I

_SysBase equ $26
_GfxBase equ $22
gb_LayersBase equ $1c8

 MC68020

 STRUCTURE QuickLayers,0
    UWORD QL_MATCHWORD
    APTR  QL_MATCHTAG
    APTR  QL_ENDSKIP
    UBYTE QL_FLAGS
    UBYTE QL_VERSION
    UBYTE QL_TYPE
    BYTE  QL_PRI
    APTR  QL_NAME
    APTR  QL_IDSTRING
    APTR  QL_INIT
    LABEL QuickLayers_SIZE

 SECTION "MakeQuickLayers",CODE
 cnop 0,4

Head:  moveq.l #-$01,D0
 rtS 
 
RomTag:  dc.w RTC_MATCHWORD
  dc.l RomTag
  dc.l EndTag
  dc.b $81
  dc.b 0
  dc.b 9
  dc.b 0
LibVer: dc.l QLayersName
LibName: dc.l QLayersVer
InitTab: dc.l InitTAB
  dc.b "$VER: quicklayers.library 0.48 68020+ (01.08.2003) by Sante Nocciolino",13,10,0,0
QLayersVer:  dc.b "quicklayers.library 0.48 68020+ (01.08.2003) by Sante Nocciolino",13,10,0,0
QLayersName:  dc.b "quicklayers.library",0
GfxName:  dc.b "graphics.library",0,0
UtilityName:  dc.b "utility.library",0
LayersName:  dc.b "layers.library",0,0
  ds.w 1
AL_0_F4:  ds.l 1

InitTAB:  dc.l $3C
AL_0_FC: dc.l FuncTab
AL_0_100: dc.l DataTab
AL_0_104: dc.l InitRoutine
FuncTab: dc.l AJL_0_2A6
AL_0_10C: dc.l AJL_0_2B4
AL_0_110: dc.l null_func
AL_0_114: dc.l null_func
  dc.l $FFFFFFFF

DataTab:  dc.b $A0,$08,$09,$00
  dc.b $A0,$09,$B0,$00
  dc.w $800A
  dc.l QLayersName
  dc.b $A0,$0E,$06,$00
  dc.w $9014,0  ; version
  dc.w $9016,48  ; revision
  dc.w $8018
  dc.l QLayersVer
  ds.l 1

InitRoutine:  btst #AFB_68020,AttnFlags+1(A6)
   beq.w Exit
  movem.l D2-D3/A2-A5,-(A7)
  movea.l D0,A5
  move.l D0,AL_0_F4
  move.l A6,$24(A5)    ;SysBase
  move.l A0,$34(A5)
  lea LayersName(PC),A1
  moveq.l #$27,D0
   jsr _LVOOpenLibrary(A6)
  move.l D0,$30(A5)   ;LayersBase
   beq.w JL_0_260
  lea GfxName(PC),A1
  moveq.l #$27,D0
   jsr _LVOOpenLibrary(A6)
  move.l D0,$28(A5)       ;GfxBase
   beq.w JL_0_260
  move.l D0,GfxBase
  lea UtilityName(PC),A1
  moveq.l #$27,D0
   jsr _LVOOpenLibrary(A6)
  move.l D0,$2C(A5)
   beq.w JL_0_260
  movea.l $30(A5),A1
  movea.l -$7C(A1),A2
  movea.l $A(A1),A1
   jsr _LVOFindResident(A6)       ;find layers.library
  tst.l D0
   beq.w JL_0_260
  movea.l D0,A3
  move.l $6(A3),D0                 ;rt_endskip
  sub.l A3,D0                      ;calculate layersbase dimension
  move.l D0,D2
  moveq.l #$00,D1
   jsr _LVOAllocVec(A6)
  move.l D0,$38(A5)
   beq.w JL_0_260
  movea.l D0,A4
  movea.l A3,A0
  movea.l A4,A1
  move.l D2,D0
   jsr _LVOCopyMem(A6)
  move.l A4,D3
  sub.l A3,D3
   jsr _LVOForbid(A6)
  move.l $6(A3),D1
  lea $1A(A3),A0
JL_0_1E4:  cmp.l A0,D1
   ble.b JL_0_20A
  cmpi.w #$4EBA,(A0)+
   bne.b JL_0_1E4
  movea.l A0,A1
  move.w (A0)+,D0
  adda.w D0,A1
  cmpa.l A1,A2
   bne.b JL_0_1E4
  move.l A0,D0
  subq.l #4,D0
  add.l D3,D0
  movea.l D0,A1
  move.w #$4EAE,(A1)+
  move.w #$FF82,(A1)
 bra.b JL_0_1E4
 
JL_0_20A:  movea.l $16(A3),A3
  movea.l $4(A3),A2
  lea $2(A2),A3
  moveq.l #$00,D2
JL_0_218:  subq.w #6,D2
  moveq.l #$00,D0
  move.w (A3)+,D0
  cmpi.w #$FFFF,D0
   beq.b JL_0_23A
  add.l A2,D0
  movea.l $30(A5),A1
  cmp.l $2(A1,D2.W),D0
   bne.b JL_0_218
  add.l D3,D0
  movea.l D2,A0
   jsr _LVOSetFunction(A6)
 bra.b JL_0_218
 
JL_0_23A:  lea L_0_266(PC),A2
JL_0_23E:  cmpi.l #$FFFFFFFF,(A2)
   beq.b JL_0_256
  movea.l $30(A5),A1
  movea.l (A2)+,A0
  move.l (A2)+,D0
   jsr _LVOSetFunction(A6)
  move.l D0,(A2)+
 bra.b JL_0_23E
 
JL_0_256:   jsr _LVOCacheClearU(A6)
   jsr _LVOPermit(A6)
  move.l A5,D0
JL_0_260:  movem.l (A7)+,D2-D3/A2-A5
Exit: rtS
 
L_0_266:  dc.l _LVOCreateUpfrontHookLayer ;$FFFFFF46
  dc.l  CreateUpfrontHookLayer
AL_0_26E:  ds.l 1
  dc.l _LVOCreateUpfrontLayer ;-36 $FFFFFFDC
  dc.l CreateUpfrontLayer
  ds.l 1
  dc.l _LVOMoveLayer ;-60 $FFFFFFC4
  dc.l MoveLayer
  ds.l 1
  dc.l _LVOMoveSizeLayer ;-180 $FFFFFF4C
  dc.l MoveSizeLayer
AL_0_292:  ds.l 1
  dc.l _LVOSizeLayer   ;-66 $FFFFFFBE
  dc.l SizeLayer
  ds.l 1
  dc.l _LVODoHookClipRects
  dc.l DoHookClipRects
  ds.l 1
  dc.l _LVOSwapBitsRastPortClipRect
  dc.l SwapBitsRastPortClipRect
  ds.l 1
  dc.l _LVOScrollLayer
  dc.l ScrollLayer
  ds.l 1
  dc.l $FFFFFFFF

AJL_0_2A6:  addq.w #1,$20(A6)
  bclr #$3,$22(A6)
  move.l A6,D0
 rtS 
 
AJL_0_2B4:  moveq.l #$00,D0
  subq.w #1,$20(A6)
   bne.b JL_0_2C6
  btst #$3,$22(A6)
   beq.b JL_0_2C6
null_func:  moveq.l #$00,D0
JL_0_2C6: rtS 
 
GfxBase:  ds.l 1
EndTag:  dc.b "Annamaria",0

 SECTION "QuickLayers",CODE
 cnop 0,4
CreateUpfrontLayer:
  move.l A3,-(A7)
  suba.l A3,A3
   bsr.b CreateUpfrontHookLayer
  movea.l (A7)+,A3
 rtS 
 cnop 0,4
CreateUpfrontHookLayer:  movem.l D3-D5/A2-A5,-(A7)
  move.l D4,D5
  andi.l #$44,D5
  tst.l D5
   beq.b JL_1_2C
  movea.l AL_0_26E,A5
L_1_22:   jsr (A5)
  movem.l (A7)+,D3-D5/A2-A5
 rtS 
  cnop 0,4
JL_1_2C:  move.l D0,D5
  movea.l A0,A5
  movea.l A6,A3
  lea $18(A0),A0
  movea.l _SysBase(A6),A6
   jsr _LVOObtainSemaphore(A6)
  movea.l A3,A6
  move.l D4,D0
  andi.w #$FFFD,D0
  ori.w #$1,D0
  movea.w #$1,A3
  movea.l A5,A0
  move.l D0,D4
  move.l D5,D0
   jsr _LVOCreateBehindHookLayer(A6)   ;<-
  movea.l D0,A4
  tst.l D0
   beq.b JL_1_B2
  suba.l A0,A0
  movea.l A4,A1
   jsr _LVOUpfrontLayer(A6)
  move.l $4(A7),D0
  moveq.l #$03,D1
  moveq.l #$00,D3
  and.l D1,D0
  moveq.l #-$04,D1
  and.w $1E(A4),D1
  move.w D1,D3
  or.l D0,D3
  move.w D3,$1E(A4)
  movea.l $10(A7),A3
  move.l A3,$76(A4)
  movea.l A6,A2
  movea.l _GfxBase(a6),A6
  movea.l $9C(A4),A0
   jsr _LVOClearRegion(A6)
  movea.l A2,A6
  andi.w #$F97F,$1E(A4)
  movea.w #$1,A0
  cmpa.l A3,A0
   beq.b JL_1_B2
  suba.l A2,A2
  movea.l $C(A4),A1
  movea.l A3,A0
   bsr.w DoHookClipRects
JL_1_B2:  movea.l A6,A3
  lea $18(A5),A0
  movea.l _SysBase(A6),A6
   jsr _LVOReleaseSemaphore(A6)
  movea.l A3,A6
  move.l A4,D0
  movem.l (A7)+,D3-D5/A2-A5
 rtS 
 
  cnop 0,4
SizeLayer:  movem.l D2-D3,-(A7)
  move.l D0,D2
  clr.l D0
  move.l D1,D3
  clr.l D1
  movea.l A1,A0
   bsr.b MoveSizeLayer
  movem.l (A7)+,D2-D3
 rtS 
 
  cnop 0,4
MoveLayer:  movem.l D2-D3,-(A7)
  clr.l D2
  clr.l D3
  movea.l A1,A0
   bsr.b MoveSizeLayer
  movem.l (A7)+,D2-D3
 rtS 
 
  cnop 0,4
MoveSizeLayer:  movea.l D1,A1
  moveq.l #$07,D1
  and.w $1E(A0),D1
  subq.w #2,D1
   beq.b JL_1_110
  move.l A1,D1
  movea.l AL_0_292,A1
L_1_10C:   jsr (A1)
 rtS 

 cnop 0,4
JL_1_110:  movem.l D4-D7/A3-A5,-(A7)
  move.l A1,D5
  move.l D0,D4
   bne.b JL_1_128
  tst.l A1
   bne.b JL_1_128
  tst.l D2
   bne.b JL_1_128
  tst.l D3
   beq.w JL_1_234
JL_1_128:  lea $10(A0),A4
  movea.w (A4)+,A1
  adda.l D4,A1
  move.w A1,D6
  movea.w (A4)+,A2
  adda.l D5,A2
  swap D6
  move.w A2,D6
  movea.w (A4)+,A5
  adda.l D4,A5
  adda.l D2,A5
  move.w A5,D7
  move.w (A4)+,D0
  ext.l D0
  add.l D5,D0
  add.l D3,D0
  swap D7
  move.w D0,D7
  cmpa.w A1,A5
   blt.b JL_1_156
  cmp.w A2,D0
   bge.b JL_1_160
JL_1_156:  clr.l D0
  movem.l (A7)+,D4-D7/A3-A5
 rtS 
 
  cnop 0,4
JL_1_160:  movea.l A0,A5
  move.w #$0001,AL_1_23C
  lea ([$44,A5],$18.W),A0
  movea.l A6,A3
  movea.l _SysBase(A6),A6
   jsr _LVOObtainSemaphore(A6)
  movea.l A3,A6
  movea.l $44(A5),A0
  cmpa.l (A0),A5
   beq.b JL_1_1EE
  movea.l $8(A5),A3
 bra.b JL_1_19E
 cnop 0,4
JL_1_18C:  tst.l $8(A3)
   beq.b JL_1_19C
  clr.w AL_1_23C
 bra.b JL_1_1FC
 
 cnop 0,4
JL_1_19C:  movea.l (A3),A3
JL_1_19E:  tst.l A3
   bne.b JL_1_18C
  tst.w AL_1_23C(PC)
   beq.b JL_1_1FC
  movea.l (A5),A3
 bra.b JL_1_1E2
 
 cnop 0,4
JL_1_1B0:  lea $10(A3),A0
  movea.l A0,A1
  move.w D7,D1
  cmp.w $2(A1),D1
   blt.b JL_1_1E0
  swap D7
  move.w D7,D1
  cmp.w (A1),D1
   blt.b JL_1_1E0
  move.w D6,D1
  cmp.w $6(A1),D1
   bgt.b JL_1_1E0
  swap D6
  move.w D6,D1
  cmp.w $4(A1),D1
   bgt.b JL_1_1E0
  clr.w AL_1_23C
 bra.b JL_1_1FC
 cnop 0,4
JL_1_1E0:  movea.l (A3),A3
JL_1_1E2:  tst.l A3
   bne.b JL_1_1B0
  tst.w AL_1_23C(PC)
   beq.b JL_1_1FC
JL_1_1EE:  moveq.l #-$03,D0
  and.w $1E(A5),D0
  ori.w #$1,D0
  move.w D0,$1E(A5)
JL_1_1FC:  move.l D4,D0
  move.l D5,D1
  movea.l A5,A0
  movea.l AL_0_292,A1
L_1_208:   jsr (A1)
  tst.w AL_1_23C(PC)
   beq.b JL_1_220
  moveq.l #-$02,D0
  and.w $1E(A5),D0
  ori.w #$2,D0
  move.w D0,$1E(A5)
JL_1_220:  lea ([$44,A5],$18.W),A0
  movea.l A6,A3
  movea.l _SysBase(A6),A6
   jsr _LVOReleaseSemaphore(A6)
  movea.l A3,A6
JL_1_234:  moveq.l #$01,D0
  movem.l (A7)+,D4-D7/A3-A5
 rtS 
 cnop 0,4
AL_1_23C:  ds.w 1
 cnop 0,4
DoHookClipRects: move.l     A0,D0
           subq.l     #1,D0
            bne.b      2$
          rtS
 cnop 0,4
2$:        movem.l    D2-D7/A2-A5,-(A7)
           move.l     A2,D6
           movea.l    (A1),A2
           move.l     A0,D7
           move.l     A1,D5
           move.l     $4(A1),-(A7)
           subq.l     #8,A7
           move.l     A2,D4
            bne.b      3$
           movea.l    D7,A0
           movea.l    D4,A1
           movea.l    D5,A2
           movea.l    D6,A3
           movea.l    D6,A4
           moveq.l    #$00,D2
           moveq.l    #$00,D3
           pea        11$(PC)
          bra.w      13$
    cnop 0,4
3$:
;--------------------
           move.l     A6,d2
           lea        $48(A2),A0   ;lr_lock
           movea.l    $26(A6),A6
            jsr        _LVOObtainSemaphore(A6)
           movea.l    d2,A6
;--------------------
           move.w     $10(A2),D2
           sub.w      $2C(A2),D2
           move.w     $12(A2),D3
           sub.w      $2E(A2),D3
           movea.l    A7,A1
           movea.l    D6,A0
           move.l     D6,-(A7)
            beq.b      4$
           move.l     A1,D6

           move.w     (A0)+,D0
           add.w      D2,D0
           move.w     D0,(A1)+
           move.w     (A0)+,D0
           add.w      D3,D0
           move.w     D0,(A1)+
           move.w     (A0)+,D0
           add.w      D2,D0
           move.w     D0,(A1)+
           move.w     (A0)+,D0
           add.w      D3,D0
           move.w     D0,(A1)+

4$:        lea        $8(A2),A5
           subq.l     #8,A7
5$:        move.l     (A5),D0
            beq.w      8$
           movea.l    D0,A5
           lea        $10(A5),A3
           tst.l      D6
            beq.b      6$
           movea.l    A3,A0
           movea.l    D6,A1
           movea.l    A7,A2
;---------start-------------
           move.l     A2,-(A7)
           move.w     (A0)+,D0
           move.w     (A1)+,D1
           cmp.w      D0,D1
            ble.b      18$
           move.w     D1,D0
18$:       move.w     D0,(A2)+
           move.w     (A0)+,D0
           move.w     (A1)+,D1
           cmp.w      D0,D1
            ble.b      19$
           move.w     D1,D0
19$:       move.w     D0,(A2)+
           move.w     (A0)+,D0
           move.w     (A1)+,D1
           cmp.w      D0,D1
            bge.b      20$
           move.w     D1,D0
20$:       move.w     D0,(A2)+
           move.w     (A0)+,D0
           move.w     (A1)+,D1
           cmp.w      D0,D1
            bge.b      21$
           move.w     D1,D0
21$:       move.w     D0,(A2)+
           movea.l    (A7)+,A2
;----------end----------
           move.l     (A2)+,D0
           move.l     (A2)+,D1
           cmp.w      D0,D1
            blt.b      5$
           cmp.l      D0,D1
            blt.b      5$
           movea.l    A7,A3
6$:        movea.l    A3,A4
           movea.l    D4,A1
           movea.l    D7,A0
           movea.l    D5,A2
           tst.l      $8(A5)
            bne.b      7$
            bsr.w      13$
          bra.b      5$
    cnop 0,4
7$:        tst.l      $C(A5)
            beq.b      5$
           moveq.l    #$0F,D1
           and.w      $10(A5),D1
           sub.w      $10(A5),D1
           move.w     $4(A4),D0
           add.w      D1,D0
           swap       D0
           move.w     $6(A4),D0
           sub.w      $12(A5),D0
           move.l     D0,-(A7)
           add.w      (A4),D1
           swap       D1
           move.w     $2(A4),D1
           sub.w      $12(A5),D1
           move.l     D1,-(A7)
           movea.l    A7,A3
           move.l     $4(A2),-(A7)
           move.l     $C(A5),$4(A2)
            bsr.w      13$
           move.l     (A7)+,$4(A2)
           addq.l     #8,A7
          bra.w      5$
    cnop 0,4
8$:        addq.l     #8,A7
           move.l     (A7)+,D6
           movea.l    D4,A2
           move.l     $20(A2),D0
            beq.b      11$
           movea.l    D5,A0
           move.l     D0,$4(A0)
           moveq.l    #$00,D2
           moveq.l    #$00,D3
           lea        $24(A2),A5
9$:        move.l     (A5),D0
            beq.b      11$
           movea.l    D0,A5
           lea        $10(A5),A3
           tst.l      D6
            beq.b      10$
           movea.l    A3,A0
           movea.l    D6,A1
           movea.l    A7,A2
;-----------start-----------
           move.l     A2,-(A7)
           move.w     (A0)+,D0
           move.w     (A1)+,D1
           cmp.w      D0,D1
            ble.b      118$
           move.w     D1,D0
118$:       move.w     D0,(A2)+
           move.w     (A0)+,D0
           move.w     (A1)+,D1
           cmp.w      D0,D1
            ble.b      119$
           move.w     D1,D0
119$:       move.w     D0,(A2)+
           move.w     (A0)+,D0
           move.w     (A1)+,D1
           cmp.w      D0,D1
            bge.b      120$
           move.w     D1,D0
120$:       move.w     D0,(A2)+
           move.w     (A0)+,D0
           move.w     (A1)+,D1
           cmp.w      D0,D1
            bge.b      121$
           move.w     D1,D0
121$:       move.w     D0,(A2)+
           movea.l    (A7)+,A2
;-----------end-----------
           move.l     (A2)+,D0
           move.l     (A2)+,D1
           cmp.w      D0,D1
            blt.b      9$
           swap       D0
           swap       D1
           cmp.w      D0,D1
            blt.b      9$
           movea.l    A7,A3
10$:       movea.l    A3,A4
           movea.l    D4,A1
           movea.l    D7,A0
           movea.l    D5,A2
            bsr.b      13$
          bra.b      9$
    cnop 0,4
11$:       addq.l     #8,A7
           movea.l    D5,A0
           move.l     (A7)+,$4(A0)
           tst.l      D4
            beq.b      12$
           movea.l    D4,A0
;------------------------
           move.l     a6,d2
           lea        $48(A0),A0
           movea.l    $26(A6),A6
            jsr        _LVOReleaseSemaphore(A6)
           movea.l    d2,a6
;------------------------
12$:       movem.l    (A7)+,D2-D7/A2-A5
          rtS
    cnop 0,4
13$:       move.l     A0,D0
            beq.b      DHCRSub1
           subq.l     #1,D0
            bne.b      14$
          rtS
    cnop 0,4
14$:       move.w     $2(A4),D0
           sub.w      D3,D0
           ext.l      D0
           move.l     D0,-(A7)
           move.w     (A4),D0
           sub.w      D2,D0
           ext.l      D0
           move.l     D0,-(A7)
           move.l     $4(A3),-(A7)
           move.l     (A3),-(A7)
           move.l     A1,-(A7)
           movea.l    A7,A1
           move.l     A6,-(A7)
           movea.l    $8(A0),A6
15$:        jsr        (A6)
           movea.l    (A7)+,A6
           lea        $14(A7),A7
          rtS
    cnop 0,4
DHCRSub1:  movem.l    D2-D7/A6,-(A7)
           movea.l    $4(A2),A1
           movea.l    A1,A0
           moveq.l    #$00,D2
           move.w     (A3),D2
           moveq.l    #$00,D3
           move.w     $2(A3),D3
           moveq.l    #$00,D4
           move.w     $4(A3),D4
           sub.l      D2,D4
           addq.l     #1,D4
           moveq.l    #$00,D5
           move.w     $6(A3),D5
           sub.l      D3,D5
           addq.l     #1,D5
           move.l     D2,D0
           move.l     D3,D1
           moveq.l    #$00,D6
           moveq.l    #-$01,D7
           movea.l    $22(A6),A6
            jsr        _LVOBltBitMap(A6)
           movem.l    (A7)+,D2-D7/A6
          rtS
 cnop 0,4
SwapBitsRastPortClipRect:
    movem.l d2-d7/a2-a6,-(sp)
    move.l  a0,a5
    move.l  a1,a3
    move.l  $22(a6),a6      ;gfxbase
    move.l  12(a3),a0

    moveq   #8,d1       ;BMA_WIDTH
    jsr _LVOGetBitMapAttr(a6)
    ;move.w (a0),d0   ;bytes per row
    ;lsl.w #3,d0      ; * 8

    moveq   #16,d4
    add.l   d4,d0
    move.l  12(a3),a0
    move.w 2(a0),d1         ;this is the same of :moveq.l #BMA_HEIGTH,d1 -> jsr _VLOGetBitMapAttr(a6)
    move.b 5(a0),d2         ;this is the same of: moveq.l #BMA_DEPTH,d1 -> jsr _LVOGetBitMapAttr(a6)
    clr.l   d3
    move.l  4(a5),a0
     jsr _LVOAllocBitMap(a6)       ; sizex, sizey, depth, flags, friend_bitmap
    move.l  d0,-(sp)       ;<-     ;  d0     d1      d2    d3        a0
    move.w  16(a3),a1
    move.w  18(a3),d1
    ext.l   d1
    move.w  20(a3),a4
    sub.l   a1,a4
    move.w  22(a3),a2
    sub.l   d1,a2
    move.l  4(a5),a0
    exg.l d0,a1
    move.w  16(a3),d2
    moveq   #15,d4
    and.l   d4,d2
    move.l  a4,d4
    addq.l  #1,d4
    move.l  a2,d5
    addq.l  #1,d5
    moveq   #63,d6
    not.b   d6
    moveq   #0,d7
    not.b   d7
    lea (0).w,a2
     jsr _LVOBltBitMap(a6)
    move.w  16(a3),d2
    ext.l   d2
    move.w  18(a3),d3
    ext.l   d3
    move.w  20(a3),d4
    ext.l   d4
    sub.l   d2,d4
    move.w  22(a3),a4
    sub.l   d3,a4
    move.l  12(a3),a0
    move.w  16(a3),d0
    moveq   #15,d1
    and.l   d1,d0
    clr.l   d1
    move.l  4(a5),a1
    addq.l  #1,d4
    move.l  a4,d5
    addq.l  #1,d5
     jsr _LVOBltBitMap(a6)
    move.l  12(a3),a0
     jsr _LVOFreeBitMap(a6)
    move.l  (sp)+,12(a3)    ;<-
    movem.l (sp)+,d2-d7/a2-a6
    rts
 cnop 0,4
ScrollLayer:    movem.l    D6-D7/A3/A5,-(A7)
           movea.l    A1,A3
           move.l     D0,D7
           move.l     D1,D6
           movea.l    _GfxBase(A6),A6      ;gfx base
           movea.l    A3,A5
            jsr        _LVOLockLayerRom(A6)
           tst.l      $20(A3)
            beq.b      JL_0_30
           movea.l    A3,A0
            jsr        _LVOSyncSBitMap(A6)
JL_0_30:   move.w     $2C(A3),D0
           ext.l      D0
           add.l      D7,D0
           move.w     D0,$2C(A3)
           move.w     $2E(A3),D0
           ext.l      D0
           add.l      D6,D0
           move.w     D0,$2E(A3)
           tst.l      $20(A3)
            beq.b      JL_0_62
           movea.l    A3,A0
            jsr        _LVOCopySBitMap(A6)
           movea.l    A3,A0
            jsr        LayersRoutine1(PC)   ; call with gfxbase in a6
JL_0_62:   movea.l    A3,A5
            jsr        _LVOUnlockLayerRom(A6)
           movea.l gb_LayersBase(a6),a6       ;layers base
           movem.l    (A7)+,D6-D7/A3/A5
          rtS

; all this routine only uses gfx base
; now this routine is called with gfxbase in a6
LayersRoutine1: link       A5,#-$B8
           movem.l    A2-A3/A6,-(A7)
           move.l     A0,-$B4(A5)
           movea.l    -$B4(A5),A3
           movea.l    $20(A3),A0
           moveq.l    #$00,D0
           move.w     D0,-$10(A5)
           move.w     D0,-$E(A5)
           move.l     A0,$C(A7)
           moveq.l    #$08,D1           ; BMA_WIDTH
            jsr        _LVOGetBitMapAttr(A6)
           subq.l     #1,D0
           move.w     D0,-$C(A5)
           movea.l    $10(A7),A0
           move.w 2(a0),d1         ;this is the same of :moveq.l #BMA_HEIGTH,d1 -> jsr _VLOGetBitMapAttr(a6)
           subq.l     #1,D0
           move.w     D0,-$A(A5)
           pea        -$10(A5)
           movea.l    $44(A3),A0
           lea        -$B0(A5),A1
            jsr        LayersRoutine2(PC)
           addq.w     #4,A7
           movea.l    $24(A3),A2
          bra.b      3$

2$:        movea.l    (A2),A0
           move.l     A3,(A2)
           movea.l    -$B4(A5),A3
           move.l     A2,$3C(A3)
           movea.l    A0,A2
3$:        movea.l    $3C(A3),A3
           move.l     A2,D0
            bne.b      2$
           move.l     A3,-$74(A5)
           move.l     (A3),-$74(A5)
           suba.l     A0,A0
           move.l     A0,-$8C(A5)
           move.l     A0,(A3)
           lea        -$B0(A5),A0
           move.l     A0,$8(A3)
           move.l     A3,-$A8(A5)
           movea.l    -$B4(A5),A1
           move.w     $2C(A1),D0
           move.w     D0,$10(A3)
           move.w     $2E(A1),D0
           move.w     D0,$12(A3)
           move.w     $2C(A1),D0
           add.w      $14(A1),D0
           sub.w      $10(A1),D0
           move.w     D0,$14(A3)
           move.w     $2E(A1),D0
           add.w      $16(A1),D0
           sub.w      $12(A1),D0
           move.w     D0,$16(A3)
           lea        LayersRoutine4(PC),A1
           lea        -8(A5),A2
           suba.l     A3,A3
            jsr LayersRoutine3(pc)
           movea.l -$b4(a5),a0
           move.l -$8c(a5),$24(a0)
           move.l -$74(a5),(a3)
           move.l a3,$3c(a0)
           movem.l (A7)+,a2-a3/a6
           unlk a5
          rts

;all this routine only uses gfx base
LayersRoutine2:    link       A5,#-$4
           movem.l    D7/A2-A3/A6,-(A7)
           movea.l    A0,A3
           movea.l    A1,A2
           lea        $10(A2),A0
           movea.l    $8(A5),A1
           move.l     (A1)+,(A0)+
           move.l     (A1)+,(A0)+
           movea.l    $8(A5),A0
           move.w     $4(A0),D0
           sub.w      (A0),D0
           addq.w     #1,D0
           move.w     D0,$86(A2)
           move.w     $6(A0),D0
           sub.w      $2(A0),D0
           addq.w     #1,D0
           move.w     D0,$88(A2)
           clr.l      $9C(A2)
           move.l     A3,$44(A2)
           lea        $48(A2),A0
           move.l     A6,-(A7)
           movea.l    $26(A6),A6
            jsr        -$22E(A6)
           movea.l    (A7)+,A6
           moveq.l    #$00,D7
          bra.b      3$
2$:        movem.l    A5-A6,-(A7)
           movea.l    $22(A6),A6
           movea.l    A2,A5
            jsr        -$1B0(A6)
           movem.l    (A7)+,A5-A6
           addq.l     #1,D7
3$:        move.b     $5B(A3),D0
           ext.w      D0
           ext.l      D0
           cmp.l      D0,D7
            blt.b      2$
           movem.l    (A7)+,D7/A2-A3/A6
           unlk       A5
          rtS

; this routine doesn't use any base
LayersRoutine3:
  movem.l A2-A5,-(A7)
  movea.l A0,A4
  movea.l A1,A5
  movea.l A2,A0
  lea $10(A4),A1
   bsr.b 3$
  tst.w D0
   beq.b 2$
  movea.l A2,A0
  lea $10(A4),A1
  subq.l #8,A7
  movea.l A7,A2
   bsr.b 5$
  movea.l $8(A4),A1
   bsr.b 11$
  addq.l #8,A7
2$:
  movem.l (A7)+,A2-A5
 rtS 
 
3$:
  moveq.l #$00,D0
  move.w $4(A0),D1
  cmp.w (A1),D1
   blt.b 4$
  move.w (A0),D1
  cmp.w $4(A1),D1
   bgt.b 4$
  move.w $6(A0),D1
  cmp.w $2(A1),D1
   blt.b 4$
  move.w $2(A0),D1
  cmp.w $6(A1),D1
   bgt.b 4$
  moveq.l #$01,D0
4$:
 rtS 
 
5$:
  move.l A2,-(A7)
  move.w (A0)+,D0
  move.w (A1)+,D1
  cmp.w D0,D1
   ble.b 6$
  move.w D1,D0
6$:
  move.w D0,(A2)+
  move.w (A0)+,D0
  move.w (A1)+,D1
  cmp.w D0,D1
   ble.b 7$
  move.w D1,D0
7$:
  move.w D0,(A2)+
  move.w (A0)+,D0
  move.w (A1)+,D1
  cmp.w D0,D1
   bge.b 8$
  move.w D1,D0
8$:
  move.w D0,(A2)+
  move.w (A0)+,D0
  move.w (A1)+,D1
  cmp.w D0,D1
   bge.b 9$
  move.w D1,D0
9$:
  move.w D0,(A2)+
  movea.l (A7)+,A2
 rtS 
 
10$:
  movea.l (A1),A1
11$:
  move.l A1,D0
   bne.b 13$
  move.l A3,-(A7)
  move.l A2,-(A7)
  move.l A4,-(A7)
12$:
   jsr (A5)
  lea $C(A7),A7
 rtS 
 
13$:
  move.l $8(A1),D0
   beq.b 10$
  lea $10(A1),A0
  exg.l A2,A1
   bsr.w 3$
  exg.l A2,A1
  tst.w D0
   beq.b 10$
  move.w $12(A1),D0
  cmp.w $2(A2),D0
   ble.b 14$
  move.l A1,-(A7)
  move.l (A2),-(A7)
  move.l $4(A2),-(A7)
  subq.w #1,D0
  move.w D0,$6(A2)
   bsr.b 10$
  move.l (A7)+,$4(A2)
  move.l (A7)+,(A2)
  movea.l (A7)+,A1
  move.w $12(A1),$2(A2)
14$:
  move.w $16(A1),D0
  cmp.w $6(A2),D0
   bge.b 15$
  move.l A1,-(A7)
  move.l (A2),-(A7)
  move.l $4(A2),-(A7)
  addq.w #1,D0
  move.w D0,$2(A2)
   bsr.b 10$
  move.l (A7)+,$4(A2)
  move.l (A7)+,(A2)
  movea.l (A7)+,A1
  move.w $16(A1),$6(A2)
15$:
  move.w $10(A1),D0
  cmp.w (A2),D0
   ble.b 16$
  move.l A1,-(A7)
  move.l (A2),-(A7)
  move.l $4(A2),-(A7)
  subq.w #1,D0
  move.w D0,$4(A2)
   bsr.w 10$
  move.l (A7)+,$4(A2)
  move.l (A7)+,(A2)
  movea.l (A7)+,A1
  move.w $10(A1),(A2)
16$:
  move.w $14(A1),D0
  cmp.w $4(A2),D0
   bge.b 17$
  move.l A1,-(A7)
  move.l (A2),-(A7)
  move.l $4(A2),-(A7)
  addq.w #1,D0
  move.w D0,(A2)
   bsr.w 10$
  move.l (A7)+,$4(A2)
  move.l (A7)+,(A2)
  movea.l (A7)+,A1
  move.w $14(A1),$4(A2)
17$:
 rtS 

LayersRoutine4: movem.l    A2-A4,-(A7)
           movea.l    $8(A7),A3
           movea.l    $C(A7),A2
           movea.l    $3C(A3),A0
           move.l     (A0),$3C(A3)
           suba.l     A1,A1
           move.l     A1,$C(A0)
           move.l     A1,$8(A0)
           lea        $10(A0),A1
           movea.l    A2,A4
           move.l     (A4)+,(A1)+
           move.l     (A4)+,(A1)+
           move.l     $24(A3),(A0)
           move.l     A0,$24(A3)
           movem.l    (A7)+,A2-A4
          rtS

 

 
 End
