            include "work:prog/assembler/ASM-One/LVO3.0/graphics_lib.i"
            include "include:hardware/custom.i"
            include "include:rtgmaster/rtgc2p.i"
            include "include:rtgmaster/rtgmaster.i"
            include "include:rtgmaster/rtgsublibs.i"
            include "include:rtgmaster/rtgAMI.i"
            ;include "include:intuition/screens.i"
            include "include:exec/memory.i"


 STRUCTURE Screen,0

    APTR sc_NextScreen          ; linked list of screens
    APTR sc_FirstWindow         ; linked list Screen's Windows

    WORD sc_LeftEdge            ; parameters of the screen
    WORD sc_TopEdge             ; parameters of the screen

    WORD sc_Width
    WORD sc_Height

    ; And so on...


    LABEL sc_SIZEOF

; This c2p is a conversion of the c2p "Cpu3Blit1.asm" to rtgmaster format
; It uses asynchrone blitting and is 030 optimized

; An example of how to use this routine:
;
;       move.w  #320,d0
;       move.w  #256,d1
;       clr.w   d3
;       move.l  #10240,d5
;       bsr     c2p1x1_cpu3blit1_queue_init
;       ...
;main:
;       lea     chunkyscreen,a0
;       move.l  screenptr,a1
;       bsr     c2p1x1_cpu3blit1_queue
;       bsr     effect
;       btst    #6,$bfe001
;       bne.s   main
;
; The screenswapping should be done in the c2p_waitblit routine,
; which is called by the C2P rout.
;
; I hope I didn't forget anything... :)
;



    moveq #0,d0
    rts
  dc.l "c2p!"
    dc.l 0
    dc.l 1
    dc.l 0

Structure
    dc.w CI_256+CI_128+CI_64+CI_EHB+CI_32+CI_16+CI_8+CI_4+CI_2
    dc.w CI_68060D+CI_68040+CI_68030D+CI_68020D
    dc.w 0
    dc.b 0
    dc.b 0
    dc.l c2p_1x1
    dc.w 1
    dc.w 1
    dc.w 0
    dc.b 'AMI '
    dc.l Name
    dc.l Init
    dc.l Expunge
    dc.l chunky2planar
    dc.l 0
    dc.l 0
    dc.l 0
    dc.b 0
    dc.b 0,0,0 ; Test
    even

Init:
    movem.l a0/a1/a2/a6/d0/d1/d2,-(sp)
    lea gfxname,a1
    moveq #0,d0
    movem.l a0/a6,-(sp)
    move.l $4,a6
    jsr -408(a6)
    movem.l (sp)+,a0/a6
    move.l d0,gfxbase
    move.l #0,rs_c2pdata(a0)
    move.l rsAMI_ScreenHandle(a0),a1
    clr.l d0
    clr.l d1
    move.w sc_Width(a1),d0
    move.w sc_Height(a1),d1
    move.l d1,d5
    mulu.l d0,d5
    lsr.l #3,d5
    clr.w d3
    jsr c2p1x1_cpu3blit1_queue_init
    movem.l (sp)+,a0/a1/a2/a6/d0/d1/d2
    move.l #0,d0
    rts
Error:
Name: dc.b '8 Bit 030 optimized c2p with Blitter',0
    even
gfxname: dc.b 'graphics.library',0
    even

Expunge:
    movem.l a6/d0/d1/d2/a0/a1/a2,-(sp)
    move.l gfxbase,a1
    move.l $4,a6
    jsr -414(a6)
    movem.l (sp)+,a6/d0/d1/d2/a0/a1/a2
    rts
chunky2planar:
                movem.l d0/d3/a3/d7,-(sp)
                move.l #0,d7
                cmp.l #c2p_1x1,d0
                beq .OK
                cmp.l #c2p_Best,d0
                beq .OK
                cmp.l #c2p_BestD,d0
                beq .OK
                cmp.l #c2p_Fastest,d0
                beq .OK
                cmp.l #c2p_FastestD,d0
                beq .OK
                cmp.l #c2p_Selected,d0
                beq .Selected
                cmp.l #c2p_SelectedD,d0
                beq .Selected
                move.l #c2p_warn_Wrong_Pixelmode,d7
                bra .OK
.Selected:
                move.l rs_c2pcurr(a3),d3
                cmp.l #c2p_1x1,d3
                beq .OK
                move.l #c2p_warn_Wrong_Pixelmode,d7
                bra .OK
.OK:
                cmp.l #1,.Merk
                beq c2p
                move.l #1,.Merk
                lea rsAMI_Bitmap1(a3),a3
                bra c2p
                move.l a3,-(sp)
                bra .Mistake
.Merk: dc.l 0
.Merk2: dc.l 0
.Mistake:
                move.l (sp)+,a3
                move.l #1,.Merk2
                movem.l (sp)+,d0/d3/a3/a7
                move.l d7,d0
                movem.l d2/a6,-(sp)
                move.l $4,a6
                move.l #0,a1
                jsr -294(a6)
                move.l d0,a1
                movem.l (sp)+,d0/a6
                move.l a6,-(sp)
                move.l $4,a6
                move.l d0,d1
                sub.l d0,d0
                bset d1,d0
                jsr -324(a6)
                move.l (sp)+,a6
                move.l #c2p_err_internal,d0
                rts


c2p:
_chunky2planar:
                ;a0 = chunky buffer
                ;a1 = first bitplane

        movem.l d2-d7/a2-a6,-(sp)
        jsr     c2p1x1_cpu3blit1_queue
        movem.l (sp)+,d2-d7/a2-a6
Raus:

        move.l d7,d0
        movem.l (sp)+,d0/d3/a3/d7
        movem.l d2/a6,-(sp)
        move.l $4,a6
        move.l #0,a1
        jsr -294(a6)
        move.l d0,a1
        movem.l (sp)+,d0/a6
        move.l a6,-(sp)
        move.l $4,a6
        move.l d0,d1
        sub.l d0,d0
        bset d1,d0
        jsr -324(a6)
        move.l (sp)+,a6
        move.l #0,d0
        rts


        ;IFND    C2P_DOUBLEBUFFER
C2P_DOUBLEBUFFER EQU 1
        ;ENDC
        IFND    CHUNKYXMAX
CHUNKYXMAX EQU  640
        ENDC
        IFND    CHUNKYYMAX
CHUNKYYMAX EQU  512
        ENDC

        section c2p,code

qblit
; Use this or put your own queue-handler here
        move.l  a6,-(sp)
        move.l  gfxbase,a6
        jsr     _LVOQBlit(a6)
        move.l  (sp)+,a6
        rts

; d0.w  chunkyx [chunky-pixels]
; d1.w  chunkyy [chunky-pixels]
; d3.w  scroffsy [screen-pixels]
; d5.l  bplsize [bytes] -- offset between one row in one bpl and the next bpl
c2p1x1_cpu3blit1_queue_init
        movem.l d2-d3,-(sp)
        lea     c2p_datanew(pc),a0
        andi.l  #$ffff,d0
        andi.l  #$ffff,d2
        move.l  d5,c2p_bplsize-c2p_data(a0)
        move.w  d1,c2p_chunkyy-c2p_data(a0)
        add.w   d3,d1
        mulu.w  d0,d1
        lsr.l   #3,d1
        subq.l  #2,d1
        move.l  d1,c2p_scroffs2-c2p_data(a0)
        mulu.w  d0,d3
        lsr.l   #3,d3
        move.l  d3,c2p_scroffs-c2p_data(a0)
        move.w  c2p_chunkyy-c2p_data(a0),d1
        mulu.w  d0,d1
        move.l  d1,c2p_pixels-c2p_data(a0)
        lsr.l   #4,d1
        move.l  d1,c2p_pixels16-c2p_data(a0)
        movem.l (sp)+,d2-d3
        rts

c2p_blitcleanup
        st      c2p_blitfin-c2p_bltnode(a1)
        sf      c2p_blitactive-c2p_bltnode(a1)
        rts

c2p_waitblit
        tst.b   c2p_blitactive(pc)
        beq.s   .n
.y      tst.b   c2p_blitfin(pc)
        beq.s   .y
.n
; This is where you would add your swap-screens-code.
        rts

; a0    c2pscreen
; a1    bitplanes

c2p1x1_cpu3blit1_queue
        movem.l d2-d7/a2-a6,-(sp)

        IFEQ    C2P_DOUBLEBUFFER
        bsr.s   c2p_waitblit
        ENDC

        lea     c2p_datanew(pc),a2
        move.l  a1,c2p_screen-c2p_data(a2)

        move.l  #$0f0f0f0f,a4
        move.l  #$00ff00ff,a5
        move.l  #$55555555,a6

        move.l  c2p_bufptrs,a1
        move.l  c2p_pixels-c2p_data(a2),a2
        add.l   a0,a2
        cmpa.l  a0,a2
        beq     .none

        move.l  (a0)+,d0
        move.l  (a0)+,d6
        move.l  (a0)+,a3
        move.l  (a0)+,d7
        move.l  a4,d5
        move.l  d6,d1                   ; Swap 4x1
        lsr.l   #4,d1
        eor.l   d0,d1
        and.l   d5,d1
        eor.l   d1,d0
        lsl.l   #4,d1
        eor.l   d6,d1

        move.l  a3,d6
        move.l  d7,d4
        lsr.l   #4,d4
        eor.l   d6,d4
        and.l   d5,d4
        eor.l   d4,d6
        lsl.l   #4,d4
        eor.l   d4,d7

        move.l  a5,d5
        move.l  d6,d2                   ; Swap 8x2, part 1
        lsr.l   #8,d2
        eor.l   d0,d2
        and.l   d5,d2
        eor.l   d2,d0
        lsl.l   #8,d2
        eor.l   d6,d2

        bra.s   .start
.x
        move.l  (a0)+,d0
        move.l  (a0)+,d6
        move.l  (a0)+,a3
        move.l  (a0)+,d7
        move.l  d1,(a1)+
        move.l  a4,d5
        move.l  d6,d1                   ; Swap 4x1
        lsr.l   #4,d1
        eor.l   d0,d1
        and.l   d5,d1
        eor.l   d1,d0
        lsl.l   #4,d1
        eor.l   d6,d1

        move.l  a3,d6
        move.l  d7,d4
        lsr.l   #4,d4
        move.l  d2,(a1)+
        eor.l   d6,d4
        and.l   d5,d4
        eor.l   d4,d6
        lsl.l   #4,d4
        eor.l   d4,d7

        move.l  a5,d5
        move.l  d6,d2                   ; Swap 8x2, part 1
        lsr.l   #8,d2
        eor.l   d0,d2
        and.l   d5,d2
        eor.l   d2,d0
        move.l  d3,(a1)+
        lsl.l   #8,d2
        eor.l   d6,d2
.start
        move.l  a6,d4
        move.l  d2,d3                   ; Swap 1x2, part 1
        lsr.l   d3
        eor.l   d0,d3
        and.l   d4,d3
        eor.l   d3,d0
        add.l   d3,d3
        eor.l   d3,d2

        move.l  d7,d3                   ; Swap 8x2, part 2
        lsr.l   #8,d3
        move.l  d0,(a1)+
        eor.l   d1,d3
        and.l   d5,d3
        eor.l   d3,d1
        lsl.l   #8,d3
        eor.l   d7,d3

        move.l  d3,d6                   ; Swap 1x2, part 2
        lsr.l   d6
        eor.l   d1,d6
        and.l   d4,d6
        eor.l   d6,d1
        add.l   d6,d6
        eor.l   d6,d3

        move.l  (a0)+,d0
        move.l  (a0)+,d6
        move.l  (a0)+,a3
        move.l  (a0)+,d7
        move.l  d1,(a1)+
        move.l  a4,d5
        move.l  d6,d1                   ; Swap 4x1
        lsr.l   #4,d1
        eor.l   d0,d1
        and.l   d5,d1
        eor.l   d1,d0
        lsl.l   #4,d1
        eor.l   d6,d1

        move.l  a3,d6
        move.l  d7,d4
        lsr.l   #4,d4
        move.l  d2,(a1)+
        eor.l   d6,d4
        and.l   d5,d4
        eor.l   d4,d6
        lsl.l   #4,d4
        eor.l   d4,d7

        move.l  a5,d5
        move.l  d6,d2                   ; Swap 8x2, part 1
        lsr.l   #8,d2
        eor.l   d0,d2
        and.l   d5,d2
        eor.l   d2,d0
        move.l  d3,(a1)+
        lsl.l   #8,d2
        eor.l   d6,d2

        move.l  a6,d4
        move.l  d2,d3                   ; Swap 1x2, part 1
        lsr.l   d3
        eor.l   d0,d3
        and.l   d4,d3
        eor.l   d3,d0
        add.l   d3,d3
        eor.l   d3,d2

        move.l  d7,d3                   ; Swap 8x2, part 2
        lsr.l   #8,d3
        move.l  d0,(a1)+
        eor.l   d1,d3
        and.l   d5,d3
        eor.l   d3,d1
        lsl.l   #8,d3
        eor.l   d7,d3

        move.l  d3,d6                   ; Swap 1x2, part 2
        lsr.l   d6
        eor.l   d1,d6
        and.l   d4,d6
        eor.l   d6,d1
        add.l   d6,d6
        eor.l   d6,d3

        cmp.l   a0,a2
        bne     .x
.x2
        move.l  d1,(a1)+
        move.l  d2,(a1)+
        move.l  d3,(a1)+

        IFNE    C2P_DOUBLEBUFFER
        bsr     c2p_waitblit
        move.l  c2p_bufptrs,d0
        move.l  c2p_bufptrs+4,c2p_bufptrs
        move.l  d0,c2p_bufptrs+4
        ENDC

        bsr     c2p_copyinitblock

        lea     c2p_data(pc),a2
        sf      c2p_blitfin-c2p_data(a2)
        st      c2p_blitactive-c2p_data(a2)
        lea     c2p_bltnode(pc),a1
        move.l  #c2p1x1_cpu3blit1_queue_41,c2p_bltroutptr-c2p_bltnode(a1)
        jsr     qblit

.none
        movem.l (sp)+,d2-d7/a2-a6
        rts

c2p1x1_cpu3blit1_queue_41               ; Pass 4, subpass 1, ascending
        move.w  #-1,bltafwm(a0)
        move.w  #-1,bltalwm(a0)
        move.l  c2p_bufptrs+4,d0
        add.l   #12,d0
        move.l  d0,bltapt(a0)
        addq.l  #2,d0
        move.l  d0,bltbpt(a0)
        move.l  c2p_bplsize-c2p_bltnode(a1),d0
        add.l   d0,d0
        add.l   c2p_screen-c2p_bltnode(a1),d0
        add.l   c2p_scroffs-c2p_bltnode(a1),d0
        move.l  d0,bltdpt(a0)
        move.w  #14,bltamod(a0)
        move.w  #14,bltbmod(a0)
        move.w  #0,bltdmod(a0)
        move.w  #$cccc,bltcdat(a0)
        move.w  #$0de4,bltcon0(a0)
        move.w  #$2000,bltcon1(a0)
        move.w  c2p_pixels16+2-c2p_bltnode(a1),bltsizv(a0)
        move.w  #1,bltsizh(a0)
        move.l  #c2p1x1_cpu3blit1_queue_42,c2p_bltroutptr-c2p_bltnode(a1)
        rts

c2p1x1_cpu3blit1_queue_42               ; Pass 4, subpass 2, ascending
        move.l  c2p_bufptrs+4,d0
        addq.l  #8,d0
        move.l  d0,bltapt(a0)
        addq.l  #2,d0
        move.l  d0,bltbpt(a0)
        move.l  c2p_bplsize-c2p_bltnode(a1),d0
        add.l   d0,d0
        add.l   c2p_bplsize-c2p_bltnode(a1),d0
        add.l   d0,d0
        add.l   c2p_screen-c2p_bltnode(a1),d0
        add.l   c2p_scroffs-c2p_bltnode(a1),d0
        move.l  d0,bltdpt(a0)
        move.w  c2p_pixels16+2-c2p_bltnode(a1),bltsizv(a0)
        move.w  #1,bltsizh(a0)
        move.l  #c2p1x1_cpu3blit1_queue_43,c2p_bltroutptr-c2p_bltnode(a1)
        rts

c2p1x1_cpu3blit1_queue_43               ; Pass 4, subpass 3, ascending
        move.l  c2p_bufptrs+4,d0
        addq.l  #4,d0
        move.l  d0,bltapt(a0)
        addq.l  #2,d0
        move.l  d0,bltbpt(a0)
        move.l  c2p_bplsize-c2p_bltnode(a1),d0
        add.l   d0,d0
        add.l   c2p_bplsize-c2p_bltnode(a1),d0
        add.l   c2p_screen-c2p_bltnode(a1),d0
        add.l   c2p_scroffs-c2p_bltnode(a1),d0
        move.l  d0,bltdpt(a0)
        move.w  c2p_pixels16+2-c2p_bltnode(a1),bltsizv(a0)
        move.w  #1,bltsizh(a0)
        move.l  #c2p1x1_cpu3blit1_queue_44,c2p_bltroutptr-c2p_bltnode(a1)
        rts

c2p1x1_cpu3blit1_queue_44               ; Pass 4, subpass 4, ascending
        move.l  c2p_bufptrs+4,d0
        move.l  d0,bltapt(a0)
        addq.l  #2,d0
        move.l  d0,bltbpt(a0)
        move.l  c2p_bplsize-c2p_bltnode(a1),d0
        lsl.l   #3,d0
        sub.l   c2p_bplsize-c2p_bltnode(a1),d0
        add.l   c2p_screen-c2p_bltnode(a1),d0
        add.l   c2p_scroffs-c2p_bltnode(a1),d0
        move.l  d0,bltdpt(a0)
        move.w  c2p_pixels16+2-c2p_bltnode(a1),bltsizv(a0)
        move.w  #1,bltsizh(a0)
        move.l  #c2p1x1_cpu3blit1_queue_45,c2p_bltroutptr-c2p_bltnode(a1)
        rts

c2p1x1_cpu3blit1_queue_45               ; Pass 4, subpass 5, descending
        move.l  c2p_bufptrs+4,d0
        subq.l  #4,d0
        add.l   c2p_pixels-c2p_bltnode(a1),d0
        move.l  d0,bltapt(a0)
        addq.l  #2,d0
        move.l  d0,bltbpt(a0)
        move.l  c2p_screen-c2p_bltnode(a1),d0
        add.l   c2p_scroffs2-c2p_bltnode(a1),d0
        move.l  d0,bltdpt(a0)
        move.w  #$2de4,bltcon0(a0)
        move.w  #$0002,bltcon1(a0)
        move.w  c2p_pixels16+2-c2p_bltnode(a1),bltsizv(a0)
        move.w  #1,bltsizh(a0)
        move.l  #c2p1x1_cpu3blit1_queue_46,c2p_bltroutptr-c2p_bltnode(a1)
        rts

c2p1x1_cpu3blit1_queue_46               ; Pass 4, subpass 6, descending
        move.l  c2p_bufptrs+4,d0
        subq.l  #8,d0
        add.l   c2p_pixels-c2p_bltnode(a1),d0
        move.l  d0,bltapt(a0)
        addq.l  #2,d0
        move.l  d0,bltbpt(a0)
        move.l  c2p_bplsize-c2p_bltnode(a1),d0
        lsl.l   #2,d0
        add.l   c2p_screen-c2p_bltnode(a1),d0
        add.l   c2p_scroffs2-c2p_bltnode(a1),d0
        move.l  d0,bltdpt(a0)
        move.w  c2p_pixels16+2-c2p_bltnode(a1),bltsizv(a0)
        move.w  #1,bltsizh(a0)
        move.l  #c2p1x1_cpu3blit1_queue_47,c2p_bltroutptr-c2p_bltnode(a1)
        rts

c2p1x1_cpu3blit1_queue_47               ; Pass 4, subpass 7, descending
        move.l  c2p_bufptrs+4,d0
        sub.l   #12,d0
        add.l   c2p_pixels-c2p_bltnode(a1),d0
        move.l  d0,bltapt(a0)
        addq.l  #2,d0
        move.l  d0,bltbpt(a0)
        move.l  c2p_bplsize-c2p_bltnode(a1),d0
        add.l   c2p_screen-c2p_bltnode(a1),d0
        add.l   c2p_scroffs2-c2p_bltnode(a1),d0
        move.l  d0,bltdpt(a0)
        move.w  c2p_pixels16+2-c2p_bltnode(a1),bltsizv(a0)
        move.w  #1,bltsizh(a0)
        move.l  #c2p1x1_cpu3blit1_queue_48,c2p_bltroutptr-c2p_bltnode(a1)
        rts

c2p1x1_cpu3blit1_queue_48               ; Pass 4, subpass 8, descending
        move.l  c2p_bufptrs+4,d0
        sub.l   #16,d0
        add.l   c2p_pixels-c2p_bltnode(a1),d0
        move.l  d0,bltapt(a0)
        addq.l  #2,d0
        move.l  d0,bltbpt(a0)
        move.l  c2p_bplsize-c2p_bltnode(a1),d0
        lsl.l   #2,d0
        add.l   c2p_bplsize-c2p_bltnode(a1),d0
        add.l   c2p_screen-c2p_bltnode(a1),d0
        add.l   c2p_scroffs2-c2p_bltnode(a1),d0
        move.l  d0,bltdpt(a0)
        move.w  c2p_pixels16+2-c2p_bltnode(a1),bltsizv(a0)
        move.w  #1,bltsizh(a0)
        moveq   #0,d0
        rts


c2p_copyinitblock
        movem.l a0-a1,-(sp)
        lea     c2p_datanew,a0
        lea     c2p_data,a1
        moveq   #16-1,d0
.copy   move.l  (a0)+,(a1)+
        dbf     d0,.copy
        movem.l (sp)+,a0-a1
        rts

        cnop 0,4
c2p_bltnode
        dc.l    0
c2p_bltroutptr
        dc.l    0
        dc.b    $40,0
        dc.l    0
c2p_bltroutcleanup
        dc.l    c2p_blitcleanup
c2p_blitfin dc.b 0
c2p_blitactive dc.b 0

        cnop    0,4

c2p_data
c2p_screen dc.l 0
c2p_scroffs dc.l 0
c2p_scroffs2 dc.l 0
c2p_bplsize dc.l 0
c2p_pixels dc.l 0
c2p_pixels16 dc.l 0
c2p_chunkyy dc.w 0
        ds.l    16
        cnop 0,4
c2p_datanew
        ds.l    16
c2p_bufptrs
        dc.l    c2p_blitbuf
        IFEQ    C2P_DOUBLEBUFFER
        dc.l    c2p_blitbuf
        ELSE
        dc.l    c2p_blitbuf+CHUNKYXMAX*CHUNKYYMAX
        ENDC
gfxbase dc.l 0      
        section bss_c,bss_c
c2p_blitbuf
        ds.l    81920
        IFNE    C2P_DOUBLEBUFFER
        ds.l    81920
        ENDC

