            ;include "work:prog/assembler/ASM-One/include/intuition/screens.i"
            include "work:prog/assembler/ASM-One/LVO3.0/graphics_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... :)
;



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

Structure2
    dc.w CI_256+CI_64+CI_EHB+CI_16
    dc.w CI_68060D+CI_68040+CI_68030D+CI_68020D
    dc.w 0
    dc.b 0
    dc.b 0
    dc.l c2p_1x1|c2p_2x2
    dc.w 16
    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
 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

    WORD sc_MouseY              ; position relative to upper-left
    WORD sc_MouseX              ; position relative to upper-left

    WORD sc_Flags               ; see definitions below

    APTR sc_Title               ; null-terminated Title text
    APTR sc_DefaultTitle        ; for Windows without ScreenTitle


    ; And so on... :)

    LABEL sc_SIZEOF     ; actually, you have no business talking about
                        ; or relying on the size of a screen structure

Init:
        movem.l d0-d7/a0-a6,-(sp)
        move.l $4.w,a6
        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.w #320,d0
        move.w #256,d1
        move.w d0,ScreenWidthInPixels
        move.w d1,ScreenHeight
        lsr.l #2,d0
        mulu d1,d0
        move.l #81920,d0 ;test
        move.l #MEMF_CHIP|MEMF_CLEAR,d1
        move.l $4,a6
        ;jsr -198(a6)
        tst.l d0
        beq Nirvana_India_Delhi_Guru_Win95
        move.l d0,size1
        ; >>>> width should be an even MULT of 16 pixels !
        move.l #256*256*8*4,d0; Test
        move.l #MEMF_FAST,d1
        jsr -198(a6)
        tst.l d0
        beq Nirvana_India_Delhi_Guru_Win95
        move.l d0,size2
        move.l size2,a0
        bsr prec_chk_table
        movem.l (sp)+,d0-d7/a0-a6
        move.l #0,d0
        rts
Nirvana_India_Delhi_Guru_Win95:
        movem.l (sp)+,d0-d7/a0-a6
        rts
        add.l #1000,a7
        move.l #13,$4 ;hehehe, if it CRASHES, then FOR REAL !!!
        rts

Error:
Name: dc.b '8 Bit 030 optimized c2p with Blitter',0
    even
gfxname: dc.b 'graphics.library',0
    even
ScreenWidthInPixels: dc.w 0
ScreenHeight: dc.w 0
Expunge:
    movem.l a6/d0/d1/d2/a0/a1/a2,-(sp)
    move.l $4,a6
    move.l size1,a1
    move.l #81920,d0
    ;jsr -210(a6)
    move.l size2,a1
    move.l #256*256*8*4,d0
    jsr -210(a6)
    movem.l (sp)+,a6/d0/d1/d2/a0/a1/a2
    rts
size1: dc.l 0
size2: dc.l 0
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
                sub.l d3,d3
                ;move.b bm_Depth(a3),d3
                bra c2p; Test
                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)
        move.l size2,a2
        clr.l d0
        move.w #320*256/32-1,d0
        bsr chunky_planar




        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

prec_chk_table: moveq.l #0,d0                           ;to be converted.
                move.l  a0,a1                           ;save the register.

prec_ctt:       move.w  #%100000000,d3
                moveq.l #7,d4
                moveq.l #%1,d5
                moveq.l #0,d6
                moveq.l #8-1,d7                         ;this many planes.

prec_inn:       move.w  d0,d1
                and.w   d3,d1
                ror.w   d4,d1
                move.b  d0,d2
                and.b   d5,d2
                ror.b   d6,d2
                add.b   d2,d1
                move.b  d1,(a0)+                        ;store plane x.

                rol.w   #1,d3
                addq.w  #1,d4
                rol.b   #1,d5
                addq.w  #1,d6

                dbra    d7,prec_inn                     ;do all the planes.

                addq.l  #1,d0                           ;next possibility.
                cmp.l   #$10000,d0                      ;all done?
                bne.s   prec_ctt                        ;nope. keep on converting.

;««««««««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»
;               shift and copy
;               INPUT:
;               a0      = output.
;               a1      = pointer to the table.
;««««««««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»

                moveq.l #3-1,d0                         ;this many times.
three_times:    moveq.l #0,d1
many_a_times:   move.l  (a1)+,d2
                rol.l   #2,d2                           ;the shift.
                move.l  d2,(a0)+
                addq.l  #1,d1
                cmp.l   #256*256*8/4,d1
                bne.s   many_a_times                    ;just do it.

                dbra    d0,three_times                  ;do it. two megabytes full. ;)

                rts

chunky_planar:  move.l  #256*256*8,d7                   ;speed up.

                moveq.l #0,d1
                add.l   #256*256*8*3,a2                 ;back to start.
                move.w  (a0)+,d1
                lsl.l   #3,d1                           ;d1 = d1*8. (2×.L).
                lea     (a2,d1.l),a3                    ;a3 = the eight planes.
                move.l  (a3)+,d3                        ;d3 = p0.B,p1.B,p2.B,p3.B.
                moveq.l #0,d1
                move.l  (a3),d4                         ;d4 = p4.B,p5.B,p6.B,p7.B.

                move.w  (a0)+,d1                        ;d1 = two pixels.
                sub.l   d7,a2                           ;next list.
                lsl.l   #3,d1                           ;d1 = d1*8. (2×.L).
                lea     (a2,d1.l),a3                    ;a3 = the eight planes.
                or.l    (a3)+,d3                        ;d3 = p0.B,p1.B,p2.B,p3.B.
                moveq.l #0,d1
                or.l    (a3),d4                         ;d4 = p4.B,p5.B,p6.B,p7.B.

                move.w  (a0)+,d1                        ;d1 = two pixels.
                sub.l   d7,a2                           ;next list.
                lsl.l   #3,d1                           ;d1 = d1*8. (2×.L).
                lea     (a2,d1.l),a3                    ;a3 = the eight planes.
                or.l    (a3)+,d3                        ;d3 = p0.B,p1.B,p2.B,p3.B.
                moveq.l #0,d1
                or.l    (a3),d4                         ;d4 = p4.B,p5.B,p6.B,p7.B.

                move.w  (a0)+,d1                        ;d1 = two pixels.
                sub.l   d7,a2                           ;next list.
                lsl.l   #3,d1                           ;d1 = d1*8. (2×.L).
                lea     (a2,d1.l),a3                    ;a3 = the eight planes.
                or.l    (a3)+,d3                        ;d3 = p0.B,p1.B,p2.B,p3.B.
                moveq.l #0,d1
                or.l    (a3),d4                         ;d4 = p4.B,p5.B,p6.B,p7.B.

                movem.l d3/d4,-(SP)

                add.l   #256*256*8*3,a2                 ;back to start.
                move.w  (a0)+,d1
                lsl.l   #3,d1                           ;d1 = d1*8. (2×.L).
                lea     (a2,d1.l),a3                    ;a3 = the eight planes.
                move.l  (a3)+,d3                        ;d3 = p0.B,p1.B,p2.B,p3.B.
                moveq.l #0,d1
                move.l  (a3),d4                         ;d4 = p4.B,p5.B,p6.B,p7.B.

                move.w  (a0)+,d1                        ;d1 = two pixels.
                sub.l   d7,a2                           ;next list.
                lsl.l   #3,d1                           ;d1 = d1*8. (2×.L).
                lea     (a2,d1.l),a3                    ;a3 = the eight planes.
                or.l    (a3)+,d3                        ;d3 = p0.B,p1.B,p2.B,p3.B.
                moveq.l #0,d1
                or.l    (a3),d4                         ;d4 = p4.B,p5.B,p6.B,p7.B.

                move.w  (a0)+,d1                        ;d1 = two pixels.
                sub.l   d7,a2                           ;next list.
                lsl.l   #3,d1                           ;d1 = d1*8. (2×.L).
                lea     (a2,d1.l),a3                    ;a3 = the eight planes.
                or.l    (a3)+,d3                        ;d3 = p0.B,p1.B,p2.B,p3.B.
                moveq.l #0,d1
                or.l    (a3),d4                         ;d4 = p4.B,p5.B,p6.B,p7.B.

                move.w  (a0)+,d1                        ;d1 = two pixels.
                sub.l   d7,a2                           ;next list.
                lsl.l   #3,d1                           ;d1 = d1*8. (2×.L).
                lea     (a2,d1.l),a3                    ;a3 = the eight planes.
                or.l    (a3)+,d3                        ;d3 = p0.B,p1.B,p2.B,p3.B.
                moveq.l #0,d1
                or.l    (a3),d4                         ;d4 = p4.B,p5.B,p6.B,p7.B.

                movem.l d3/d4,-(SP)

                add.l   #256*256*8*3,a2                 ;back to start.
                move.w  (a0)+,d1
                lsl.l   #3,d1                           ;d1 = d1*8. (2×.L).
                lea     (a2,d1.l),a3                    ;a3 = the eight planes.
                move.l  (a3)+,d3                        ;d3 = p0.B,p1.B,p2.B,p3.B.
                moveq.l #0,d1
                move.l  (a3),d4                         ;d4 = p4.B,p5.B,p6.B,p7.B.

                move.w  (a0)+,d1                        ;d1 = two pixels.
                sub.l   d7,a2                           ;next list.
                lsl.l   #3,d1                           ;d1 = d1*8. (2×.L).
                lea     (a2,d1.l),a3                    ;a3 = the eight planes.
                or.l    (a3)+,d3                        ;d3 = p0.B,p1.B,p2.B,p3.B.
                moveq.l #0,d1
                or.l    (a3),d4                         ;d4 = p4.B,p5.B,p6.B,p7.B.

                move.w  (a0)+,d1                        ;d1 = two pixels.
                sub.l   d7,a2                           ;next list.
                lsl.l   #3,d1                           ;d1 = d1*8. (2×.L).
                lea     (a2,d1.l),a3                    ;a3 = the eight planes.
                or.l    (a3)+,d3                        ;d3 = p0.B,p1.B,p2.B,p3.B.
                moveq.l #0,d1
                or.l    (a3),d4                         ;d4 = p4.B,p5.B,p6.B,p7.B.

                move.w  (a0)+,d1                        ;d1 = two pixels.
                sub.l   d7,a2                           ;next list.
                lsl.l   #3,d1                           ;d1 = d1*8. (2×.L).
                lea     (a2,d1.l),a3                    ;a3 = the eight planes.
                or.l    (a3)+,d3                        ;d3 = p0.B,p1.B,p2.B,p3.B.
                moveq.l #0,d5
                or.l    (a3),d4                         ;d4 = p4.B,p5.B,p6.B,p7.B.

                movem.l d3/d4,-(SP)

                add.l   #256*256*8*3,a2                 ;back to start.
                move.w  (a0)+,d5
                lsl.l   #3,d5                           ;d5 = d5*8. (2×.L).
                lea     (a2,d5.l),a3                    ;a3 = the eight planes.
                move.l  (a3)+,d1                        ;d1 = p0.B,p1.B,p2.B,p3.B.
                moveq.l #0,d5
                move.l  (a3),d2                         ;d2 = p4.B,p5.B,p6.B,p7.B.

                move.w  (a0)+,d5                        ;d5 = two pixels.
                sub.l   d7,a2                           ;next list.
                lsl.l   #3,d5                           ;d5 = d5*8. (2×.L).
                lea     (a2,d5.l),a3                    ;a3 = the eight planes.
                or.l    (a3)+,d1                        ;d1 = p0.B,p1.B,p2.B,p3.B.
                moveq.l #0,d5
                or.l    (a3),d2                         ;d2 = p4.B,p5.B,p6.B,p7.B.

                move.w  (a0)+,d5                        ;d5 = two pixels.
                sub.l   d7,a2                           ;next list.
                lsl.l   #3,d5                           ;d5 = d5*8. (2×.L).
                lea     (a2,d5.l),a3                    ;a3 = the eight planes.
                or.l    (a3)+,d1                        ;d1 = p0.B,p1.B,p2.B,p3.B.
                moveq.l #0,d5
                or.l    (a3),d2                         ;d2 = p4.B,p5.B,p6.B,p7.B.

                move.w  (a0)+,d5                        ;d5 = two pixels.
                sub.l   d7,a2                           ;next list.
                lsl.l   #3,d5                           ;d5 = d5*8. (2×.L).
                lea     (a2,d5.l),a3                    ;a3 = the eight planes.
                or.l    (a3)+,d1                        ;d1 = p0.B,p1.B,p2.B,p3.B.
                or.l    (a3),d2                         ;d2 = p4.B,p5.B,p6.B,p7.B.

                movem.l (SP)+,d3-d5/a3-a5

;««««««««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»
;               now do the output
;««««««««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»

                move.l  d1,d7
                lsl.l   #8,d7                           ;.B p0.
                move.l  d3,d6
                swap    d6
                move.b  d7,d6                           ;.B,.B p0.
                move.l  d5,d7
                ror.l   #8,d7
                move.w  d6,d7                           ;.B,.B,.B p0.
                move.l  a4,d6
                and.l   #$00ffffff,d7
                and.l   #$ff000000,d6
                add.l   d6,d7                           ;.B,.B,.B,.B p0.
                move.l  d7,(a1)                         ;plane 0 done.
                add.l   #10240,a1                       ;next plane.

                move.l  d1,d7
                swap    d7                              ;.B p1.
                move.l  d3,d6
                ror.l   #8,d6
                move.b  d7,d6                           ;.B,.B p1.
                move.l  d5,d7
                move.w  d6,d7                           ;.B,.B,.B p1.
                move.l  a4,d6
                rol.l   #8,d6
                and.l   #$00ffffff,d7
                and.l   #$ff000000,d6
                add.l   d6,d7                           ;.B,.B,.B,.B p1.
                move.l  d7,(a1)                         ;plane 1 done.
                add.l   #10240,a1                       ;next plane.

                move.l  d1,d7
                ror.w   #8,d7                           ;.B p2.
                move.w  d3,d6
                move.b  d7,d6                           ;.B,.B p2.
                move.l  d5,d7
                rol.l   #8,d7
                move.w  d6,d7                           ;.B,.B,.B p2.
                move.l  a4,d6
                swap    d6
                and.l   #$00ffffff,d7
                and.l   #$ff000000,d6
                add.l   d6,d7                           ;.B,.B,.B,.B p2.
                move.l  d7,(a1)                         ;plane 2 done.
                add.l   #10240,a1                       ;next plane.

                rol.w   #8,d3
                move.b  d1,d3                           ;.B,.B p3.
                swap    d5
                move.w  d3,d5                           ;.B,.B,.B p3.
                move.l  a4,d3
                ror.l   #8,d3
                and.l   #$00ffffff,d5
                and.l   #$ff000000,d3
                add.l   d5,d3                           ;.B,.B,.B,.B p3.
                move.l  d3,(a1)                         ;plane 3 done.
                add.l   #10240,a1                       ;next plane.

                move.l  d2,d7
                rol.l   #8,d7                           ;.B p4.
                move.l  d4,d6
                swap    d6
                move.b  d7,d6                           ;.B,.B p4.
                move.l  a3,d7
                ror.l   #8,d7
                move.w  d6,d7                           ;.B,.B,.B p4.
                move.l  a5,d6
                and.l   #$00ffffff,d7
                and.l   #$ff000000,d6
                add.l   d6,d7                           ;.B,.B,.B,.B p4.
                move.l  d7,(a1)                         ;plane 4 done.
                add.l   #10240,a1                       ;next plane.

                move.l  d2,d7
                swap    d7                              ;.B p5.
                move.l  d4,d6
                ror.l   #8,d6
                move.b  d7,d6                           ;.B,.B p5.
                move.l  a3,d7
                move.w  d6,d7                           ;.B,.B,.B p5.
                move.l  a5,d6
                rol.l   #8,d6
                and.l   #$00ffffff,d7
                and.l   #$ff000000,d6
                add.l   d6,d7                           ;.B,.B,.B,.B p5.
                move.l  d7,(a1)                         ;plane 5 done.
                add.l   #10240,a1                       ;next plane.

                move.l  d2,d7
                ror.w   #8,d7                           ;.B p6.
                move.w  d4,d6
                move.b  d7,d6                           ;.B,.B p6.
                move.l  a3,d7
                rol.l   #8,d7
                move.w  d6,d7                           ;.B,.B,.B p6.
                move.l  a5,d6
                swap    d6
                and.l   #$00ffffff,d7
                and.l   #$ff000000,d6
                add.l   d6,d7                           ;.B,.B,.B,.B p6.
                move.l  d7,(a1)                         ;plane 6 done.
                add.l   #10240,a1                       ;next plane.

                rol.w   #8,d4
                move.b  d2,d4                           ;.B,.B p7.
                move.l  a3,d6
                swap    d6
                move.w  d4,d6                           ;.B,.B,.B p7.
                move.l  a5,d2
                ror.l   #8,d2
                and.l   #$00ffffff,d6
                and.l   #$ff000000,d2
                add.l   d6,d2                           ;.B,.B,.B,.B p7.
                move.l  d2,(a1)                         ;plane 7 done.
                sub.l   #10240*7-4,a1                   ;next .L.

                dbra    d0,chunky_planar                ;do the next 32 pixels.
                rts
