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

; 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... :)
;

TRUE EQU 1
FALSE EQU 0

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

Structure
    dc.w CI_256+CI_64+CI_EHB
    dc.w CI_68060D+CI_68040+CI_68030D+CI_68020D
    dc.w 0
    dc.b 0
    dc.b 0
    dc.l c2p_2x2
    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 d2-d7/a0-a6,-(a7)
        clr.l d0
        clr.l d1
        move.w #320,d0
        move.w #256,d1
        move.l $4,_SysBase
        movem.l d0-d7/a0-a6,-(sp)
        lea gfxname,a1
        moveq #0,d0
        move.l $4,a6
        jsr -408(a6)
        move.l d0,_GfxBase
        movem.l (sp)+,d0-d7/a0-a6
        mulu.w  d1,d0
        move.l  d0,GameScreenPixels
        move.l  d0,d1
        lsr.l   #3,d0
        move.l  d0,PlaneSize
        lsr.l   #1,d0
        move.w  d0,PixelsDiv16

        move.l  _SysBase,a6
        move.l  d1,d0
        mulu.l  #3,d0
        move.l  #MEMF_CHIP!MEMF_CLEAR,d1
        jsr     _LVOAllocVec(a6)
        move.l  d0,BlitBuffer1
        beq.s   .fail
        add.l   GameScreenPixels,d0
        move.l  d0,BlitBuffer2
        add.l   GameScreenPixels,d0
        move.l  d0,BlitBuffer3

        moveq   #FALSE,d0
.fail   movem.l (a7)+,d2-d7/a0-a6
        rts
Error:
Name: dc.b '8 Bit 030 optimized c2p with Blitter',0
    even
gfxname: dc.b 'graphics.library',0
    even

Expunge:
       movem.l d2-d7/a2-a6,-(a7)
        move.l  _SysBase,a6
        move.l  BlitBuffer1(pc),a1
        jsr     _LVOFreeVec(a6)
        move.l _GfxBase,a1
        jsr -414(a6)
        movem.l (a7)+,d2-d7/a2-a6
        rts
chunky2planar:
                movem.l d0/d3/a3/d7,-(sp)
                move.l #0,d7
                cmp.l #c2p_2x2,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_2x2,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
                sub.l d3,d3
                move.b bm_Depth(a3),d3
                cmp.l #8,d3
                beq c2p
                cmp.l #6,d3
                beq 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     _START_CPU2_BLIT2_8bpl
        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

_START_CPU2_BLIT2_8bpl:
        movem.l d2-d7/a2-a6,-(a7)
.waitb  tst.l   BlitterDone(pc)
        bne.s   .waitb

        move.l  a1,DrawBuf
        move.l  a0,a1
        add.l   GameScreenPixels(pc),a1
        move.l  BlitBuffer1(pc),a2
        move.l  #$0f0f0f0f,d6
        move.l  #$00ff00ff,d7

        move.l  (a0)+,d0
        move.l  (a0)+,d1
        move.l  (a0)+,d2
        move.l  (a0)+,d3
        move.l  d0,d4           ; AaBbCcDd      2
        and.l   d7,d4           ; ..Bb..Dd      2
        eor.l   d4,d0           ; Aa..Cc..      2
        lsl.l   #8,d4           ; Bb..Dd..      4
        move.l  d2,d5           ; IiJjKkLl      2
        and.l   d7,d5           ; ..Jj..Ll      2
        eor.l   d5,d2           ; Ii..Kk..      2
        lsr.l   #8,d2           ; ..Ii..Kk      4
        or.l    d2,d0           ; AaIiCcKk      2
        or.l    d5,d4           ; BbJjDdLl      2
        move.l  d1,d2           ; EeFfGgHh      2
        and.l   d7,d2           ; ..Ff..Hh      2
        eor.l   d2,d1           ; Ee..Gg..      2
        lsl.l   #8,d2           ; Ff..Hh..      4
        move.l  d3,d5           ; MmNnOoPp      2
        and.l   d7,d5           ; ..Nn..Pp      2
        eor.l   d5,d3           ; Mm..Oo..      2
        lsr.l   #8,d3           ; ..Mm..Oo      4
        or.l    d3,d1           ; EeMmGgOo      2
        or.l    d5,d2           ; FfNnHhPp      2
        move.l  d0,d3           ; AaIiCcKk      2
        and.l   d6,d3           ; .a.i.c.k      2
        eor.l   d3,d0           ; A.I.C.K.      2
        lsl.l   #4,d3           ; a.i.c.k.      4
        move.l  d1,d5           ; EeMmGgOo      2
        and.l   d6,d5           ; .e.m.g.o      2
        or.l    d5,d3           ; aeimcgko      2
        move.l  d3,a3           ;(a2)+          2
        eor.l   d5,d1           ; E.M.G.O.      2
        lsr.l   #4,d1           ; .E.M.G.O      4
        or.l    d1,d0           ; AEIMCGKO      2
        move.l  d0,a4           ;(a2)+          2
        move.l  d4,d1           ; BbJjDdLl      2
        and.l   d6,d1           ; .b.j.d.l      2
        eor.l   d1,d4           ; B.J.D.L.      2
        lsl.l   #4,d1           ; b.j.d.l.      4
        move.l  d2,d5           ; FfNnHhPp      2
        and.l   d6,d5           ; .f.n.h.p      2
        or.l    d5,d1           ; bfjndhlp      2
        move.l  d1,a5           ;(a2)+          2
        eor.l   d5,d2           ; F.N.H.P.      2
        lsr.l   #4,d2           ; .F.N.H.P      4
        or.l    d2,d4           ; BFJNDHLP      2
        move.l  d4,a6           ;(a2)+          2

.inner  move.l  (a0)+,d0
        move.l  (a0)+,d1
        move.l  (a0)+,d2
        move.l  (a0)+,d3
        move.l  a3,(a2)+
        move.l  d0,d4           ; AaBbCcDd      2
        and.l   d7,d4           ; ..Bb..Dd      2
        eor.l   d4,d0           ; Aa..Cc..      2
        lsl.l   #8,d4           ; Bb..Dd..      4
        move.l  d2,d5           ; IiJjKkLl      2
        and.l   d7,d5           ; ..Jj..Ll      2
        eor.l   d5,d2           ; Ii..Kk..      2
        lsr.l   #8,d2           ; ..Ii..Kk      4
        or.l    d2,d0           ; AaIiCcKk      2
        or.l    d5,d4           ; BbJjDdLl      2
        move.l  d1,d2           ; EeFfGgHh      2
        move.l  a4,(a2)+
        and.l   d7,d2           ; ..Ff..Hh      2
        eor.l   d2,d1           ; Ee..Gg..      2
        lsl.l   #8,d2           ; Ff..Hh..      4
        move.l  d3,d5           ; MmNnOoPp      2
        and.l   d7,d5           ; ..Nn..Pp      2
        eor.l   d5,d3           ; Mm..Oo..      2
        lsr.l   #8,d3           ; ..Mm..Oo      4
        or.l    d3,d1           ; EeMmGgOo      2
        or.l    d5,d2           ; FfNnHhPp      2
        move.l  d0,d3           ; AaIiCcKk      2
        and.l   d6,d3           ; .a.i.c.k      2
        move.l  a5,(a2)+
        eor.l   d3,d0           ; A.I.C.K.      2
        lsl.l   #4,d3           ; a.i.c.k.      4
        move.l  d1,d5           ; EeMmGgOo      2
        and.l   d6,d5           ; .e.m.g.o      2
        or.l    d5,d3           ; aeimcgko      2
        move.l  d3,a3           ;(a2)+          2
        eor.l   d5,d1           ; E.M.G.O.      2
        lsr.l   #4,d1           ; .E.M.G.O      4
        or.l    d1,d0           ; AEIMCGKO      2
        move.l  d0,a4           ;(a2)+          2
        move.l  d4,d1           ; BbJjDdLl      2
        move.l  a6,(a2)+
        and.l   d6,d1           ; .b.j.d.l      2
        eor.l   d1,d4           ; B.J.D.L.      2
        lsl.l   #4,d1           ; b.j.d.l.      4
        move.l  d2,d5           ; FfNnHhPp      2
        and.l   d6,d5           ; .f.n.h.p      2
        or.l    d5,d1           ; bfjndhlp      2
        move.l  d1,a5           ;(a2)+          2
        eor.l   d5,d2           ; F.N.H.P.      2
        lsr.l   #4,d2           ; .F.N.H.P      4
        or.l    d2,d4           ; BFJNDHLP      2
        move.l  d4,a6           ;(a2)+          2
        cmp.l   a1,a0           ;               2
        blt.b   .inner
        move.l  a3,(a2)+
        move.l  a4,(a2)+
        move.l  a5,(a2)+
        move.l  a6,(a2)+

        moveq   #-1,d0
        move.l  d0,BlitterDone

        move.l  #Blit31,BlitRout

        move.l  _GfxBase,a6
        lea     MyBlitNode(pc),a1
        jsr     _LVOQBlit(a6)

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

Blit31  moveq   #-1,d0
        move.l  d0,bltafwm(a0)
        move.w  #$cccc,bltcdat(a0)
        move.w  #2,bltamod(a0)
        move.w  #2,bltbmod(a0)
        clr.w   bltdmod(a0)
        move.l  GameScreenPixels(pc),d0
        lsr.l   #2,d0
        move.w  d0,bltsizv(a0)

        move.l  BlitBuffer1(pc),d0
        move.l  d0,bltapt(a0)
        addq.l  #2,d0
        move.l  d0,bltbpt(a0)
        move.l  BlitBuffer2(pc),bltdpt(a0)
        move.w  #$0DE4,bltcon0(a0)
        move.w  #$2000,bltcon1(a0)
        move.w  #$0001,bltsizh(a0)
        move.l  #Blit32,BlitRout
        rts

Blit32  move.l  BlitBuffer1(pc),d0
        add.l   GameScreenPixels(pc),d0
        subq.l  #4,d0
        move.l  d0,bltapt(a0)
        addq.l  #2,d0
        move.l  d0,bltbpt(a0)
        move.l  GameScreenPixels(pc),d0
        lsr.l   #1,d0
        subq.l  #2,d0
        add.l   BlitBuffer3(pc),d0
        move.l  d0,bltdpt(a0)
        move.w  #$2DE4,bltcon0(a0)
        move.w  #$0002,bltcon1(a0)
        move.w  #$0001,bltsizh(a0)
        move.l  #Blit41,BlitRout
        rts

Blit41  move.w  #$1DE4,bltcon0(a0)      ;bitplane 0
        move.w  #2,bltcon1(a0)
        move.w  #$aaaa,bltcdat(a0)
        move.w  #6,bltamod(a0)
        move.w  #6,bltbmod(a0)
        clr.w   bltdmod(a0)
        move.w  PixelsDiv16(pc),bltsizv(a0)

        move.l  GameScreenPixels(pc),d0
        lsr.l   #1,d0
        subq.l  #8,d0
        add.l   BlitBuffer3(pc),d0
        move.l  d0,bltapt(a0)
        addq.l  #4,d0
        move.l  d0,bltbpt(a0)

        move.l  DrawBuf(pc),d0
        add.l   PlaneSize(pc),d0
        subq.l  #2,d0
        move.l  d0,bltdpt(a0)
        move.w  #1,bltsizh(a0)
        move.l  #Blit42,BlitRout
        rts

Blit42  move.l  GameScreenPixels(pc),d0 ;bitplane 2
        lsr.l   #1,d0
        subq.l  #8,d0
        add.l   BlitBuffer2(pc),d0
        move.l  d0,bltapt(a0)
        addq.l  #4,d0
        move.l  d0,bltbpt(a0)

        move.l  DrawBuf(pc),d0
        add.l   PlaneSize(pc),d0
        add.l   PlaneSize(pc),d0
        add.l   PlaneSize(pc),d0
        subq.l  #2,d0
        move.l  d0,bltdpt(a0)
        move.w  #1,bltsizh(a0)
        move.l  #Blit43,BlitRout
        rts

Blit43  move.l  GameScreenPixels(pc),d0 ;bitplane 4
        lsr.l   #1,d0
        subq.l  #6,d0
        add.l   BlitBuffer3(pc),d0
        move.l  d0,bltapt(a0)
        addq.l  #4,d0
        move.l  d0,bltbpt(a0)

        move.l  DrawBuf(pc),d0
        move.l  PlaneSize(pc),d1
        lsl.l   #2,d1
        add.l   PlaneSize(pc),d0
        add.l   d1,d0
        subq.l  #2,d0
        move.l  d0,bltdpt(a0)
        move.w  #1,bltsizh(a0)
        move.l  #Blit44,BlitRout
        rts

Blit44  move.l  GameScreenPixels(pc),d0 ;bitplane 6
        lsr.l   #1,d0
        subq.l  #6,d0
        add.l   BlitBuffer2(pc),d0
        move.l  d0,bltapt(a0)
        addq.l  #4,d0
        move.l  d0,bltbpt(a0)

        move.l  DrawBuf(pc),d0
        move.l  PlaneSize(pc),d1
        lsl.l   #2,d1
        add.l   PlaneSize(pc),d0
        add.l   PlaneSize(pc),d0
        add.l   PlaneSize(pc),d0
        add.l   d1,d0
        subq.l  #2,d0
        move.l  d0,bltdpt(a0)
        move.w  #1,bltsizh(a0)
        move.l  #Blit45,BlitRout
        rts

Blit45  move.w  #$0DE4,bltcon0(a0)      ;bitplane 1
        move.w  #$1000,bltcon1(a0)

        move.l  BlitBuffer3(pc),d0
        move.l  d0,bltapt(a0)
        addq.l  #4,d0
        move.l  d0,bltbpt(a0)
        move.l  DrawBuf(pc),d0
        add.l   PlaneSize(pc),d0
        move.l  d0,bltdpt(a0)
        move.w  #1,bltsizh(a0)
        move.l  #Blit46,BlitRout
        rts

Blit46  move.l  BlitBuffer2(pc),d0      ;bitplane 3
        move.l  d0,bltapt(a0)
        addq.l  #4,d0
        move.l  d0,bltbpt(a0)
        move.l  DrawBuf(pc),d0
        add.l   PlaneSize(pc),d0
        add.l   PlaneSize(pc),d0
        add.l   PlaneSize(pc),d0
        move.l  d0,bltdpt(a0)
        move.w  #1,bltsizh(a0)
        move.l  #Blit47,BlitRout
        rts

Blit47  move.l  BlitBuffer3(pc),d0      ;bitplane 5
        addq.l  #2,d0
        move.l  d0,bltapt(a0)
        addq.l  #4,d0
        move.l  d0,bltbpt(a0)
        move.l  DrawBuf(pc),d0
        move.l  PlaneSize(pc),d1
        lsl.l   #2,d1
        add.l   PlaneSize(pc),d0
        add.l   d1,d0
        move.l  d0,bltdpt(a0)
        move.w  #1,bltsizh(a0)
        move.l  #Blit48,BlitRout
        rts

Blit48  move.l  BlitBuffer2,d0          ;bitplane 7
        addq.l  #2,d0
        move.l  d0,bltapt(a0)
        addq.l  #4,d0
        move.l  d0,bltbpt(a0)
        move.l  DrawBuf(pc),d0
        move.l  PlaneSize(pc),d1
        lsl.l   #2,d1
        add.l   PlaneSize(pc),d0
        add.l   PlaneSize(pc),d0
        add.l   PlaneSize(pc),d0
        add.l   d1,d0
        move.l  d0,bltdpt(a0)
        move.w  #1,bltsizh(a0)
        move.l  #Blit31,BlitRout
        moveq   #0,d0
        rts

BlitCleanup
        clr.l   BlitterDone
        rts

        cnop 0,4
MyBlitNode  dc.l    0
BlitRout    dc.l    0
            dc.b    $40
            dc.b    0
            dc.w    0
            dc.w    0
            dc.l    BlitCleanup

BlitBuffer1 dc.l    0
BlitBuffer2 dc.l    0
BlitBuffer3 dc.l    0
BlitterDone dc.l    0
DrawBuf     dc.l    0
BlitBuf     dc.l    0
PlaneSize   dc.l    0
PixelsDiv16 dc.l    0
GameScreenPixels
            dc.l    0
_SysBase    dc.l    0
_GfxBase    dc.l    0
