; a0 -> rastport, a1 -> cliprect
SwapBitsRastPortClipRect: movem.l d2-d7/a2-a6,-(sp)
    move.l  rp_BitMap(a0),a5           ;rastport bitmap in a5
    move.l  a1,a3           ;cliprect in a3
    move.l  _GfxBase(a6),a6      ;gfxbase
    move.l  cr_BitMap(a3),a4
    move.l a4,a0
    moveq   #8,d1       ;BMA_WIDTH
    jsr _LVOGetBitMapAttr(a6)
    ;move.w (a0),d0   ;bytes per row
    ;lsl.w #3,d0      ; * 8
    moveq   #16,d4              ; we need a bitmap of the size of the original plus 16
    add.l   d4,d0               ; because of alignemnt restrictions of cliprects
    move.l  a4,a0
    move.w bm_Rows(a0),d1         ;this is the same of :moveq.l #BMA_HEIGTH,d1 -> jsr _VLOGetBitMapAttr(a6)
    move.b bm_Depth(a0),d2         ;this is the same of: moveq.l #BMA_DEPTH,d1 -> jsr _LVOGetBitMapAttr(a6)
    clr.l   d3
    move.l  a5,a0
     jsr _LVOAllocBitMap(a6)       ; sizex, sizey, depth, flags, friend_bitmap
    move.l  d0,-(a7)                ;  d0     d1      d2    d3        a0
    move.w  cr_MinX(a3),a1
    move.w  cr_MinY(a3),d1
    move.w  cr_MaxX(a3),d4
    move.w  cr_MaxY(a3),d5
    sub.w a1,d4
    sub.w d1,d5
    addq.l  #1,d4
    addq.l  #1,d5
    move.l d4,-(a7)    ;save maxX-minX onto stack
    move.l d5,-(a7)     ;save maxy-miny onto stack
    move.l  a5,a0
    move.w a1,d2 ;move.w  cr_MinX(a3),d2
    exg.l d0,a1
    moveq #15,d7        ; align XDest to cliprect
    and.l   d7,d2       ;
    move.l   #$c0,d6
    moveq #-1,d7
    suba.l a2,a2             ; copy rast port bitmap to new bitmap
     jsr _LVOBltBitMap(a6)   ;srcBitMap,xSrc,ySrc,destBitMap,xDest,yDest,xSize,ySize,minterm,mask,tempA
                             ;a0         d0   d1   a1         d2    d3    d4    d5     d6     d7   a2
;***** this piece of code is not needed in simple refresh layers
;***** copies the cliprect bitmaps in the rastport
.smart: move.l  a4,a0
    move.w  cr_MinX(a3),d2
    move.w  cr_MinY(a3),d3
    move.w  d2,d0
    move.l (a7)+,d5         ;restore maxy-miny
    move.l (a7)+,d4         ;restore maxx-minx
    moveq   #15,d1
    and.l   d1,d0           ; align xSrc
    clr.l   d1
    move.l  a5,a1
    suba.l a2,a2
    moveq.l #-1,d7
    move.l #$c0,d6
     jsr _LVOBltBitMap(a6)
;*****

.exit: move.l  a4,a0
     jsr _LVOFreeBitMap(a6)     ;a0 bitmap to free
    move.l  (a7)+,cr_BitMap(a3)
    movem.l (sp)+,d2-d7/a2-a6
    rts

