                    CSECT   text

                    INCLUDE "exec/types.i"
                    INCLUDE "graphics/gfx.i"

tempBM:             EQU     -$002e          ; (a5) offset for tempBM
tempBM_Depth:       EQU     tempBM+bm_Depth
tempBM_Plane0:      EQU     tempBM+bm_Planes
param_src:          EQU     $0008           ; (a5) offset for src (long)
param_col:          EQU     $000e           ; (a5) offset for col (word)
param_dst:          EQU     $0010           ; (a5) offset for dst (long)
param_rep:          EQU     $0016           ; (a5) offset for src (word)


                    XDEF    _killcolor      ; tell blink where killcolor is

                    XREF    __base          ; holds bottom of stack address
                    XREF    __xcovf         ; stack overflow abort function

                    XREF    _BltBitMap      ; ? #pragma commented out in proto

_killcolor:         link      a5,#-$002e    ; local storage
                    cmpa.l    __base(a4),a7 ; check stack overflow
                    bcs.w     __xcovf       ; if so, abort
                    movem.l   d2/d4-d7/a2-a3,-(a7)  ; save non-scratch
                    movea.l   param_src(a5),a3  ; load register variables >src
                    move.w    param_col(a5),d7  ; col   | Note I didn't actually
                    movea.l   param_dst(a5),a2  ; dst   | specify that src was
                    move.w    param_rep(a5),d6  ; rep   | a register variable - nice!

                    movea.l   a3,a0         ; prep for copying src to tempBM
                    lea       tempBM(a5),a1 ; tempBM address
                    moveq     #$09,d0       ; size in longs of a bitmap
do_copy:            move.l    (a0)+,(a1)+   ;
                    dbf       d0,do_copy    ; funky little op-code
                    move.b    #01,tempBM_Depth(a5)
                                            ; set tempBM.Depth to 1
                                            ; Note it writes directly to the
                                            ; local storage

                    btst      #$0000,d7     ; test bit zero of col
                    beq.b     skip1         ; if zero minterm = BLT_INVERT
                    moveq     #$60,d0       ; trick to get 0c0
                    add.l     d0,d0         ; see!
                    bra.b     skip2         ;
skip1:              moveq     #$30,d0       ;
skip2:              move.l    d0,d4         ; place result into minterm

                    moveq     #$00,d0       ; push params onto the stack
                    move.b    d4,d0         ; minterm into d0
                    clr.l     -(a7)         ; NULL
                    pea       $0001         ; 0x01  (the write mask)
                    move.l    d0,-(a7)      ; minterm (note byte as long)
                    pea       $00c8         ; SIZEY (note pea trick!)
                    pea       $0140         ; SIZEX
                    moveq     #$00,d1
                    move.l    d1,-(a7)      ; MINY
                    move.l    d1,-(a7)      ; MINX
                    move.l    a2,-(a7)      ; dst
                    move.l    d1,-(a7)      ; MINY
                    move.l    d1,-(a7)      ; MINX
                    pea       tempBM(a5)    ; &tempBM
                    jsr       _BltBitMap(pc)    ; call _BltBitMap
                    lea       $002c(a7),a7  ; fix stack back up

                    moveq     #$01,d5       ; j = 1
makemask:           moveq     #$00,d0       ; clear d0
                    move.b    bm_Depth(a3),d0  ; load src->Depth byte
                    cmp.l     d0,d5         ; j < it ?
                    bge.b     ent_writeback ; no, so first loop finished

                    move.l    d5,d0         ; update tempBM.Planes[0]
                    asl.l     #2,d0         ; make byte count a long count,
                                            ; to index src->Planes[j].
                    move.l    bm_Planes(a3,d0.l),tempBM_Plane0(a5)
                                            ; into tempBM.Planes[0]
                    moveq     #$01,d0       ; load d0 with 1
                    move.l    d0,d1         ; and d1
                    asl.l     d5,d1         ; shift left by j (REM: 1<<j)
                    move.l    d7,d2         ;
                    and.w     d1,d2         ; col & bit
                    beq.b     skip3         ; if zero minterm = BLT_PRE_INV
                    moveq     #$44,d1       ; trick to get 0x088
                    add.l     d1,d1
                    bra.b     skip4
skip3:              moveq     #$22,d1       ; 0x022
skip4:              move.l    d1,d4         ; store result into minterm

                    moveq     #$00,d1       ; push parameters again
                    move.b    d4,d1
                    clr.l     -(a7)
                    move.l    d0,-(a7)
                    move.l    d1,-(a7)
                    pea       $00c8
                    pea       $0140
                    moveq     #00,d2
                    move.l    d2,-(a7)
                    move.l    d2,-(a7)
                    move.l    a2,-(a7)
                    move.l    d2,-(a7)
                    move.l    d2,-(a7)
                    pea       tempBM(a5)
                    jsr       _BltBitMap(pc)    ; call _BltBitMap
                    lea       $002c(a7),a7  ; fix the stack
                    addq.l    #1,d5         ; inc j
                    bra.b     makemask      ; back around loop

ent_writeback:      moveq     #$00,d5
writeback:          moveq     #$00,d0
                    move.b    bm_Depth(a3),d0
                    cmp.l     d0,d5
                    bge.b     finis         ; have written them all back

                    move.l    d5,d0
                    asl.l     #2,d0         ; update tempBM.Planes[0]
                    move.l    bm_Planes(a3,d0.l),tempBM_Plane0(a5)

                    moveq     #$01,d0
                    move.l    d0,d1
                    asl.l     d5,d1
                    move.l    d6,d2
                    and.w     d1,d2
                    beq.b     skip5
                    moveq     #$77,d1
                    add.l     d1,d1
                    bra.b     skip6
skip5:              moveq     #$22,d1
skip6:              move.l    d1,d4

                    moveq     #$00,d1
                    move.b    d4,d1
                    clr.l     -(a7)
                    move.l    d0,-(a7)
                    move.l    d1,-(a7)
                    pea       $00c8
                    pea       $0140
                    moveq     #$00,d2
                    move.l    d2,-(a7)
                    move.l    d2,-(a7)
                    pea       -$002e(a5)
                    move.l    d2,-(a7)
                    move.l    d2,-(a7)
                    move.l    a2,-(a7)
                    jsr       _BltBitMap(pc)
                    lea       $002c(a7),a7      ; fix stack
                    addq.l    #1,d5             ; inc j
                    bra.b     writeback         ; back around loop

finis:              movem.l   (a7)+,d2/d4-d7/a2-a3
                    unlk      a5                ; lose local storage
                    rts

                    END
