;don't draw colour if only 1..3 pixels of sprite are visible in this tile

       if SCROLLING
drawsprslicesq
        ex de,hl
        ld sp,hl
drawsprslices_tilegfx=$+1
        ld b,tilegfx/256 ;patched if needed
         scf
        ret ;show
       endif

drawsprslices
nextsprslice
;de=stack for show
;a=slice #
        ld c,a
        ld b,scrbuf/256
        ld a,(bc)
        ld l,a
        inc b
        ld a,(bc) ;sprite gfx
        ld h,a
        inc b
        ld sp,hl
        ld a,(bc) ;(x/8) - общий для всех слайсов спрайта
        inc b
        exx
        ld l,a
        exx
        ld a,(bc) ;(x&7)*24 + ((8-hgt)*3) + (drawsprkernel&0xff)
        ld lx,a
        inc b
       if COLOURSPRITES
       ld a,(bc) ;COLOUR
       ex af,af' ;'
       inc b
       endif
        ld a,(bc) ;scrbuf/256+y
        inc b
        exx
        ld h,a
;l=(x/8)
;h=scrbuf/256+y
;sp=sprite gfx
;hx=drawsprkernel/256
;lx=(x&7)*24 + ((8-hgt)*3) + (drawsprkernel&0xff)
        jp (ix)
        ;jp
;... выводилка (портит lx)
        ;ENDSPRSLICE

       if SCROLLING
        macro ENDSPRSLICE
        exx
        ld a,(bc) ;next slice
        ;or a
         dec a
        jp z,drawsprslicesq
        jp nextsprslice
        endm
       else
        macro ENDSPRSLICE
        exx
        ld a,(bc) ;next slice
        ;or a
         dec a
        jp nz,nextsprslice
        ex de,hl
        ld sp,hl
        ld b,tilegfx/256 ;patched if needed?
         scf
        ret ;show
        endm
       endif

;l=(x/8) ;h=scrbuf/256+y
;sp=gfx
       macro SPRRR0
       if AUTOMASK
        pop de ;gfx              ;+10
	ld a,d
	add a,a
	or d
         ld b,a
	rra
	or b
	or (hl)
        xor d
        ld (hl),a
        inc l                    ;+46
	ld a,e
	add a,a
	or e
         ld b,a
	rra
	or b
	or (hl)
        xor e
        ld (hl),a
        dec l                    ;+46
        inc h                    ;+4 ;106t ;no zigzag because we jump inside
       else
        pop de ;gfx
        pop bc ;mask             ;+20
        ld a,(hl)
        or b
        xor d
        ld (hl),a
        inc l                    ;+26
        ld a,(hl)
        or c
        xor e
        ld (hl),a
        dec l
        inc h                    ;+30 ;76t ;no zigzag because we jump inside
       endif
       endm
sprrr0hgt8
        SPRRR0
sprrr0hgt7
        SPRRR0
sprrr0hgt6
        SPRRR0
sprrr0hgt5
        SPRRR0
sprrr0hgt4
        SPRRR0
       if COLOURSPRITES
        ex af,af' ;'
        ld c,h
        ld h,attrbuf/256
        ld (hl),a
        inc l
        ld (hl),a
        dec l
        ld h,c
        ex af,af' ;'
       endif
sprrr0hgt3
        SPRRR0
sprrr0hgt2
        SPRRR0
sprrr0hgt1
        SPRRR0
        org $-2
        ENDSPRSLICE

;l=(x/8) ;h=scrbuf/256+y
;sp=gfx
       macro SPRRR1
       if AUTOMASK
        pop de ;gfx              ;+10
        xor a                    ;+4
        srl d
        rr e
        rra                      ;+20
        inc l
        inc l                    ;+8
	ld c,a
	add a,a
	or c
         ld b,a
	rra
	or b
        or (hl)
	xor c
        ld (hl),a
        dec l                    ;+46
	ld a,e
	add a,a
	or e
         ld b,a
	rra
	or b
	or (hl)
        xor e
        ld (hl),a
        dec l                    ;+46
	ld a,d
	add a,a
	or d
         ld b,a
	rra
	or b
	or (hl)
        xor d
        ld (hl),a
        inc h                    ;+46 ;180t
       else
        pop de ;gfx
        pop bc ;mask             ;+20
        xor a                    ;+4
        srl b
        rr c
        rra                      ;+20
        inc l
        inc l
        or (hl)
         ld (hl),a               ;+22
        xor a                    ;+4
        srl d
        rr e
        rra ;bcm = mask, dea = gfx ;+20
         xor (hl)
        ld (hl),a
        dec l                    ;+18
        ld a,(hl)
        or c
        xor e
        ld (hl),a
        dec l                    ;+26
        ld a,(hl)
        or b
        xor d
        ld (hl),a
        inc h                    ;+26 ;160t
       endif
       endm
sprrr1hgt8
        SPRRR1
sprrr1hgt7
        SPRRR1
sprrr1hgt6
        SPRRR1
sprrr1hgt5
        SPRRR1
sprrr1hgt4
        SPRRR1
       if COLOURSPRITES
        ex af,af' ;'
        ld c,h
        ld h,attrbuf/256
        ld (hl),a
        inc l
        ld (hl),a
        dec l
        ld h,c
        ex af,af' ;'
       endif
sprrr1hgt3
        SPRRR1
sprrr1hgt2
        SPRRR1
sprrr1hgt1
        SPRRR1
        org $-1
        ENDSPRSLICE

       macro SPRRR2
       if AUTOMASK
        pop de ;gfx              ;+10
        xor a                    ;+4
       dup 2
        srl d
        rr e
        rra
       edup                      ;+20*2
        inc l
        inc l                    ;+8
	ld c,a
	add a,a
	or c
         ld b,a
	rra
	or b
        or (hl)
	xor c
        ld (hl),a
        dec l                    ;+46
	ld a,e
	add a,a
	or e
         ld b,a
	rra
	or b
	or (hl)
        xor e
        ld (hl),a
        dec l                    ;+46
	ld a,d
	add a,a
	or d
         ld b,a
	rra
	or b
	or (hl)
        xor d
        ld (hl),a
        inc h                    ;+46 ;200t
       else
        pop de ;gfx
        pop bc ;mask             ;+20
        xor a                    ;+4
       dup 2
        srl b
        rr c
        rra
       edup                      ;+20*2
        inc l
        inc l
        or (hl)
         ld (hl),a               ;+22
        xor a                    ;+4
       dup 2
        srl d
        rr e
        rra
       edup ;bcm = mask, dea = gfx ;+20*2
         xor (hl)
        ld (hl),a
        dec l                    ;+18
        ld a,(hl)
        or c
        xor e
        ld (hl),a
        dec l                    ;+26
        ld a,(hl)
        or b
        xor d
        ld (hl),a
        inc h                    ;+26 ;200t
       endif
       endm
sprrr2hgt8
        SPRRR2
sprrr2hgt7
        SPRRR2
sprrr2hgt6
        SPRRR2
sprrr2hgt5
        SPRRR2
sprrr2hgt4
        SPRRR2
       if COLOURSPRITES
        ex af,af' ;'
        ld c,h
        ld h,attrbuf/256
        ld (hl),a
        inc l
        ld (hl),a
        ;inc l
        ;ld (hl),a
        ;dec l
        dec l
        ld h,c
        ex af,af' ;'
       endif
sprrr2hgt3
        SPRRR2
sprrr2hgt2
        SPRRR2
sprrr2hgt1
        SPRRR2
        org $-1
        ENDSPRSLICE

       macro SPRRR3
       if AUTOMASK
        pop de ;gfx              ;+10
        xor a                    ;+4
       dup 3
        srl d
        rr e
        rra
       edup                      ;+20*3
        inc l
        inc l                    ;+8
	ld c,a
	add a,a
	or c
         ld b,a
	rra
	or b
        or (hl)
	xor c
        ld (hl),a
        dec l                    ;+46
	ld a,e
	add a,a
	or e
         ld b,a
	rra
	or b
	or (hl)
        xor e
        ld (hl),a
        dec l                    ;+46
	ld a,d
	add a,a
	or d
         ld b,a
	rra
	or b
	or (hl)
        xor d
        ld (hl),a
        inc h                    ;+46 ;220t
       else
        pop de ;gfx
        pop bc ;mask             ;+20
        xor a                    ;+4
       dup 3
        srl b
        rr c
        rra
       edup                      ;+20*3
        inc l
        inc l
        or (hl)
         ld (hl),a               ;+22
        xor a                    ;+4
       dup 3
        srl d
        rr e
        rra
       edup ;bcm = mask, dea = gfx ;+20*3
         xor (hl)
        ld (hl),a
        dec l                    ;+18
        ld a,(hl)
        or c
        xor e
        ld (hl),a
        dec l                    ;+26
        ld a,(hl)
        or b
        xor d
        ld (hl),a
        inc h                    ;+26 ;240t
       endif
       endm
sprrr3hgt8
        SPRRR3
sprrr3hgt7
        SPRRR3
sprrr3hgt6
        SPRRR3
sprrr3hgt5
        SPRRR3
sprrr3hgt4
        SPRRR3
       if COLOURSPRITES
        ex af,af' ;'
        ld c,h
        ld h,attrbuf/256
        ld (hl),a
        inc l
        ld (hl),a
        ;inc l
        ;ld (hl),a
        ;dec l
        dec l
        ld h,c
        ex af,af' ;'
       endif
sprrr3hgt3
        SPRRR3
sprrr3hgt2
        SPRRR3
sprrr3hgt1
        SPRRR3
        org $-1
        ENDSPRSLICE

;l=(x/8) ;h=scrbuf/256+y
;sp=gfx
       macro SPRRR4
       if AUTOMASK
        pop de ;gfx
        ex de,hl ;+4
        xor a           ;+18
       dup 4
        add hl,hl
        rla
       edup             ;+60
        ex de,hl	;+4
;ade = gfx
        ld c,a
	add a,a
	or c
         ld b,a
	rra
	or b
        or (hl)
        xor c
        ld (hl),a
        inc l           ;+46
	ld a,d
	add a,a
	or d
         ld b,a
	rra
	or b
        or (hl)
        xor d
        ld (hl),a
        inc l           ;+46
	ld a,e
	add a,a
	or e
         ld b,a
	rra
	or b
        or (hl)
        xor e
        ld (hl),a
        dec l           ;+46
        dec l
        inc h           ;+8 ;228t
       else
        pop de ;gfx
        ex de,hl ;+4
        xor a           ;+18
       dup 4
        add hl,hl
        rla
       edup             ;+60
        ld b,h ;+4
        ld c,l ;+4
        ld lx,a ;+8    ;+16
        pop hl ;mask
        xor a           ;+14
       dup 4
        add hl,hl
        rla
       edup             ;+60
        ex de,hl ;Xbc = gfx, ade = mask
        or (hl)
        xor lx         ;+19
        ld (hl),a
        inc l           ;+11
        ld a,(hl)
        or d
        xor b
        ld (hl),a
        inc l           ;+26
        ld a,(hl)
        or e
        xor c
        ld (hl),a
        dec l           ;+26
        dec l
        inc h           ;+8 ;258t
       endif
       endm
sprrr4hgt8
        SPRRR4
sprrr4hgt7
        SPRRR4
sprrr4hgt6
        SPRRR4
sprrr4hgt5
        SPRRR4
sprrr4hgt4
        SPRRR4
       if COLOURSPRITES
        ex af,af' ;'
        ld c,h
        ld h,attrbuf/256
        ld (hl),a
        inc l
        ld (hl),a
        inc l
        ld (hl),a
        dec l
        dec l
        ld h,c
        ex af,af' ;'
       endif
sprrr4hgt3
        SPRRR4
sprrr4hgt2
        SPRRR4
sprrr4hgt1
        SPRRR4
        org $-3
        ENDSPRSLICE

       macro SPRRR5
       if AUTOMASK
        pop de ;gfx
        ex de,hl ;+4
        xor a           ;+18
       dup 3
        add hl,hl
        rla
       edup             ;+45
        ex de,hl	;+4
;ade = gfx
        ld c,a
	add a,a
	or c
         ld b,a
	rra
	or b
        or (hl)
        xor c
        ld (hl),a
        inc l           ;+46
	ld a,d
	add a,a
	or d
         ld b,a
	rra
	or b
        or (hl)
        xor d
        ld (hl),a
        inc l           ;+46
	ld a,e
	add a,a
	or e
         ld b,a
	rra
	or b
        or (hl)
        xor e
        ld (hl),a
        dec l           ;+46
        dec l
        inc h           ;+8 ;213t
       else
        pop de ;gfx
        ex de,hl ;+4
        xor a           ;+18
       dup 3
        add hl,hl
        rla
       edup             ;+45
        ld b,h ;+4
        ld c,l ;+4
        ld lx,a ;+8    ;+16
        pop hl ;mask
        xor a           ;+14
       dup 3
        add hl,hl
        rla
       edup             ;+45
        ex de,hl ;Xbc = gfx, ade = mask
        or (hl)
        xor lx         ;+19
        ld (hl),a
        inc l           ;+11
        ld a,(hl)
        or d
        xor b
        ld (hl),a
        inc l           ;+26
        ld a,(hl)
        or e
        xor c
        ld (hl),a
        dec l           ;+26
        dec l
        inc h           ;+8 ;228t
       endif
       endm
sprrr5hgt8
        SPRRR5
sprrr5hgt7
        SPRRR5
sprrr5hgt6
        SPRRR5
sprrr5hgt5
        SPRRR5
sprrr5hgt4
        SPRRR5
       if COLOURSPRITES
        ex af,af' ;'
        ld c,h
        ld h,attrbuf/256
        ;ld (hl),a
        inc l
        ld (hl),a
        inc l
        ld (hl),a
        dec l
        dec l
        ld h,c
        ex af,af' ;'
       endif
sprrr5hgt3
        SPRRR5
sprrr5hgt2
        SPRRR5
sprrr5hgt1
        SPRRR5
        org $-3
        ENDSPRSLICE

       macro SPRRR6
       if AUTOMASK
        pop de ;gfx
        ex de,hl ;+4
        xor a           ;+18
       dup 2
        add hl,hl
        rla
       edup             ;+30
        ex de,hl	;+4
;ade = gfx
        ld c,a
	add a,a
	or c
         ld b,a
	rra
	or b
        or (hl)
        xor c
        ld (hl),a
        inc l           ;+46
	ld a,d
	add a,a
	or d
         ld b,a
	rra
	or b
        or (hl)
        xor d
        ld (hl),a
        inc l           ;+46
	ld a,e
	add a,a
	or e
         ld b,a
	rra
	or b
        or (hl)
        xor e
        ld (hl),a
        dec l           ;+46
        dec l
        inc h           ;+8 ;198t
       else
        pop de ;gfx
        ex de,hl ;+4
        xor a           ;+18
       dup 2
        add hl,hl
        rla
       edup             ;+30
        ld b,h ;+4
        ld c,l ;+4
        ld lx,a ;+8    ;+16
        pop hl ;mask
        xor a           ;+14
       dup 2
        add hl,hl
        rla
       edup             ;+30
        ex de,hl ;Xbc = gfx, ade = mask
        or (hl)
        xor lx         ;+19
        ld (hl),a
        inc l           ;+11
        ld a,(hl)
        or d
        xor b
        ld (hl),a
        inc l           ;+26
        ld a,(hl)
        or e
        xor c
        ld (hl),a
        dec l           ;+26
        dec l
        inc h           ;+8 ;198t
       endif
       endm
sprrr6hgt8
        SPRRR6
sprrr6hgt7
        SPRRR6
sprrr6hgt6
        SPRRR6
sprrr6hgt5
        SPRRR6
sprrr6hgt4
        SPRRR6
       if COLOURSPRITES
        ex af,af' ;'
        ld c,h
        ld h,attrbuf/256
        ;ld (hl),a
        inc l
        ld (hl),a
        inc l
        ld (hl),a
        dec l
        dec l
        ld h,c
        ex af,af' ;'
       endif
sprrr6hgt3
        SPRRR6
sprrr6hgt2
        SPRRR6
sprrr6hgt1
        SPRRR6
        org $-3
        ENDSPRSLICE

;l=(x/8) ;h=scrbuf/256+y
;sp=gfx
       macro SPRRR7
       if AUTOMASK
        pop de ;gfx             ;+10
        xor a                   ;+4
        sla e
        rl d
        rla                     ;+20
	ld c,a
	add a,a
	or c
         ld b,a
	rra
	or b
        or (hl)
        xor c
        ld (hl),a
        inc l                   ;+46
	ld a,d
	add a,a
	or d
         ld b,a
	rra
	or b
        or (hl)
        xor d
        ld (hl),a
        inc l                   ;+46
	ld a,e
	add a,a
	or e
         ld b,a
	rra
	or b
        or (hl)
        xor e
        ld (hl),a
        dec l                   ;+46
        dec l
        inc h                   ;+8 ;180t
       else
        pop de ;gfx
        pop bc ;mask            ;+20
        xor a                   ;+4
        sla c
        rl b
        rla                     ;+20
        or (hl)
         ld (hl),a ;+7          ;+14
        xor a                   ;+4
        sla e
        rl d
        rla ;mbc = mask, ade = gfx      ;+20
         xor (hl) ;+3
        ld (hl),a
        inc l                   ;+18
        ld a,(hl)
        or b
        xor d
        ld (hl),a
        inc l                   ;+26
        ld a,(hl)
        or c
        xor e
        ld (hl),a
        dec l                   ;+26
        dec l
        inc h                   ;+8 ;160t
       endif
       endm
sprrr7hgt8
        SPRRR7
sprrr7hgt7
        SPRRR7
sprrr7hgt6
        SPRRR7
sprrr7hgt5
        SPRRR7
sprrr7hgt4
        SPRRR7
       if COLOURSPRITES
        ex af,af' ;'
        ld c,h
        ld h,attrbuf/256
        inc l
        ld (hl),a
        inc l
        ld (hl),a
        dec l
        dec l
        ld h,c
        ex af,af' ;'
       endif
sprrr7hgt3
        SPRRR7
sprrr7hgt2
        SPRRR7
sprrr7hgt1
        SPRRR7
        org $-3
        ENDSPRSLICE

       if DOTS

       macro DOT mask
        ld a,(hl)
        or mask;0xc0
        ld (hl),a
        ld a,h
        cp scrbuf/256+7
        jr z,1f;dot0q
        inc h
        ld a,(hl)
        or mask;0xc0
        ld (hl),a
1;dot0q
        ENDSPRSLICE
       endm

dot0
        DOT %11000000
dot1
        DOT %01100000
dot2
        DOT %00110000
dot3
        DOT %00011000
dot4
        DOT %00001100
dot5
        DOT %00000110
dot6
        DOT %00000011
dot7
        set 0,(hl)
        inc l
        set 7,(hl)
        ld a,h
        cp scrbuf/256+7
        jr z,1f;dot0q
        inc h
        set 7,(hl)
        dec l
        set 0,(hl)
1;dot0q
        ENDSPRSLICE

       endif ;DOTS
