ScrollLayer: movem.l    D6-D7/A3/A5,-(A7)
           movea.l    A1,A3                ;layers in a3
           move.l     D0,D7                ;x dimension in d7
           move.l     D1,D6                ;y dimension in d6
           movea.l    _GfxBase(A6),A6      ;gfx base
           movea.l    A3,A5
            jsr        _LVOLockLayerRom(A6)
           tst.l      lr_SuperBitMap(A3)              ; test if layer is superbitmapped 32(a3)
            beq.b      JL_0_30                        ; if not -> adjust only the coordinates values
           movea.l    A3,A0                           ; else
            jsr        _LVOSyncSBitMap(A6)            ;
JL_0_30:   move.w     lr_Scroll_X(A3),D0              ; scrollx=scrollx+x
           ext.l      D0
           add.l      D7,D0
           move.w     D0,lr_Scroll_X(A3)
           move.w     lr_Scroll_Y(A3),D0              ; scrolly=scrolly+y
           ext.l      D0
           add.l      D6,D0
           move.w     D0,lr_Scroll_Y(A3)
           tst.l      lr_SuperBitMap(A3)
            beq.b      JL_0_62
           movea.l    A3,A0                           ;
            jsr        _LVOCopySBitMap(A6)            ; do this if layer is superbitmapped
           ;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,-(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,8(A7)
           moveq.l    #8,D1           ; BMA_WIDTH
            jsr        _LVOGetBitMapAttr(A6)
           subq.l     #1,D0
           move.w     D0,-$C(A5)
           movea.l    $c(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)     ;a6 contains gfxbase
           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        -$10(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
           unlk a5
          rts

;all this routine uses sysbase gfxbase
LayersRoutine2:    link     A5,#-$4
           movem.l    D7/A2-A3,-(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    gb_ExecBase(A6),A6
            jsr        _LVOInitSemaphore(A6)
           movea.l    (A7)+,A6
           moveq.l    #0,D7
          bra.b      3$
2$:        move.l    A5,-(A7)
           movea.l    A2,A5
            jsr        _LVOLockLayerRom(A6)
           movea.l    (A7)+,A5
           addq.l     #1,D7
3$:        move.b     $5B(A3),D0
           extb.l      D0
           cmp.l      D0,D7
            blt.b      2$
           movem.l    (A7)+,D7/A2-A3
           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

