;TODO interrupt:
;if in draw, write FLAG_DRAWTILE (RETILE не поможет, если там был спрайт) and call drawleftandrighttiles
;if eol spoiled, restore
;if in sprite, restore gfx by checksum

;ret = 10t for 2 tiles
;or (hl):ret nz:inc l = 16t for 1 tile
;in draw kernel: ret or ret nc:jp show or jp show...jr nc,draw instead of 2*patch every line

        macro LDLY
       if COLOUR
        ld iy,+(_ly/8+0x58-(0x40/8))*256+_ly ;hy=attraddr/256, ly=scraddr/256
       else
        ld ly,_ly
       endif
        endm

        macro DRAWSPRSLICES y,newsp ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
        ccf
        ret nc ;draw next line
_newly=(y/8)*8+0x40;screen/256
       if _newly != _ly
_ly=_newly
        LDLY
       endif
        ld de,newsp
        ld a,(sprslicepointers+y)
         dec a
        jp nz,drawsprslices
         ex de,hl
        ld sp,hl
        ret ;show ;ret z выигрывает, если строк без спрайтов больше, чем со спрайтами
        ;jp drawsprslices
        endm

        macro DRAWSPRSLICESSCF y,newsp ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
        jr c,1f;tileseol23nextline ;draw next line
_newly=(y/8)*8+0x40;screen/256
       if _newly != _ly
_ly=_newly
        LDLY
       endif
        ld de,newsp
        ld a,(sprslicepointers+y)
         dec a
        jp nz,drawsprslices
         ex de,hl
        ld sp,hl
         scf
        ret ;show
1f
        endm

       macro DRAWSPRSLICESSKIPTOP y,newsp ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
       if TILEMAPTOPSKIP <= y
        DRAWSPRSLICES y,newsp
       endif
       endm

       macro DRAWSPRSLICESSKIP y,newsp ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
       if TILEMAPBOTTOMSKIP < (23-y)
        DRAWSPRSLICES y,newsp
       endif
       if TILEMAPBOTTOMSKIP == (23-y)
        DRAWSPRSLICESSCF y,newsp
       endif
       endm

drawtiles
;a=tilegfx/256
;1.NC:draw tiles checked by FLAG_DRAWTILE (TODO не вызывать >330t, если режим скролла, а в текущей строке нет спрайтов?)
;2.drawsprslices
;3.CY:show checked tiles and restore flags
       if SCROLLING
        ld (drawsprslices_tilegfx),a
        ld (on_int_tilegfx),a
       endif
        ld b,a;tilegfx/256
        ld hx,drawsprkernel/256
        ld (tilessp),sp
        ld sp,tilemaptop
_ly=0x40;screen/256
        LDLY
         or a
        ret ;draw
tileseol0
        DRAWSPRSLICESSKIPTOP 0,tilemap+0x0000 ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
tileseol1
        DRAWSPRSLICESSKIPTOP 1,tilemap+0x0020 ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
tileseol2
        DRAWSPRSLICESSKIPTOP 2,tilemap+0x0040 ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
tileseol3
        DRAWSPRSLICESSKIPTOP 3,tilemap+0x0060 ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
tileseol4
        DRAWSPRSLICESSKIPTOP 4,tilemap+0x0080 ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
tileseol5
        DRAWSPRSLICESSKIPTOP 5,tilemap+0x00a0 ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
tileseol6
        DRAWSPRSLICESSKIPTOP 6,tilemap+0x00c0 ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
tileseol7
        DRAWSPRSLICESSCF 7,tilemap+0x00e0 ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
        ld sp,tilemap+0x0200
         or a
        ret ;draw
tileseol8
        DRAWSPRSLICES 8,tilemap+0x0200 ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
tileseol9
        DRAWSPRSLICES 9,tilemap+0x0220 ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
tileseol10
        DRAWSPRSLICES 10,tilemap+0x0240 ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
tileseol11
        DRAWSPRSLICES 11,tilemap+0x0260 ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
tileseol12
        DRAWSPRSLICES 12,tilemap+0x0280 ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
tileseol13
        DRAWSPRSLICES 13,tilemap+0x02a0 ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
tileseol14
        DRAWSPRSLICES 14,tilemap+0x02c0 ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
tileseol15
        DRAWSPRSLICESSCF 15,tilemap+0x02e0 ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
       if TILEMAPHGT>16
        ld sp,tilemap+0x0400
         or a
        ret ;draw
tileseol16
        DRAWSPRSLICESSKIP 16,tilemap+0x0400 ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
tileseol17
        DRAWSPRSLICESSKIP 17,tilemap+0x0420 ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
tileseol18
        DRAWSPRSLICESSKIP 18,tilemap+0x0440 ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
tileseol19
        DRAWSPRSLICESSKIP 19,tilemap+0x0460 ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
tileseol20
        DRAWSPRSLICESSKIP 20,tilemap+0x0480 ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
tileseol21
        DRAWSPRSLICESSKIP 21,tilemap+0x04a0 ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
tileseol22
        DRAWSPRSLICESSKIP 22,tilemap+0x04c0 ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
tileseol23
        DRAWSPRSLICESSKIP 23,tilemap+0x04e0 ;в конце sp=de:scf:ret(show):ccf:ret nc(draw next line)
       endif
tilessp=$+1
        ld sp,0
        ret

;ly=screen line/256
;b=tilegfx/256
;NC=drawtobuf, CY=showfrombuf
drawlefttile
        ld hl,-2+256
        add hl,sp
        ld c,(hl) ;tile
        ld a,l
        and 0x1f
        ld l,a ;in scrbuf
         ld e,b
        ld h,scrbuf/256
        dup 8+COLOUR
        ld a,(bc)
        ld (hl),a
        inc b
        inc h
        edup
        org $-2
         ld b,e;tilegfx/256
        ret ;NC
showlefttile
         jr nc,drawlefttile
       if SCROLLING
;не бывает
        ld hl,-1
        add hl,sp
        ld (hl),FLAG_SHOWTILEFROMMEM
        dec l
        ld (hl),FLAG_SHOWTILEFROMMEM
       else
        ld hl,-2
        add hl,sp
        ld (hl),FLAG_RETILE
       endif
         ld e,l ;in screen
         ld d,ly
        ld a,l
        and 0x1f
        ld l,a ;in scrbuf
        ld h,scrbuf/256
        dup 8
        ld a,(hl)
        ld (de),a
        inc d
        inc h
        edup
        org $-2
       if COLOUR
        ld d,hy
        inc h
        ld a,(hl)
        ld (de),a
         scf
       else
         scf
       endif
        ret
showlefttile_righttilefrommem
         jr nc,drawlefttile
       if SCROLLING
        ld hl,-2
        add hl,sp
        ld (hl),FLAG_SHOWTILEFROMMEM
        inc l
       else
        ld hl,-2
        add hl,sp
        ld (hl),FLAG_RETILE
        inc l
        ld (hl),FLAG_SKIPTILE
       endif
        inc h
         ld e,l ;in screen
         ld d,ly
        ld l,(hl) ;right tile
        ld h,b;tilegfx/256
        dup 8
        ld a,(hl)
        ld (de),a
        inc d
        inc h
        edup
        org $-2
       if COLOUR
        ld c,d
        ld d,hy
        inc h
        ld a,(hl)
        ld (de),a
       endif
        dec e ;in screen
        ld a,e
        and 0x1f
        ld l,a ;in scrbuf
       if COLOUR
        ld h,attrbuf/256
        ld a,(hl)
        ld (de),a
        dec h
        ld d,c
       else
        ld h,scrbuf/256+7
       endif
        dup 8
        ld a,(hl)
        ld (de),a
        dec d
        dec h
        edup
        org $-2
         scf
        ret

drawrighttile
        ld hl,-1+256
        add hl,sp
        ld c,(hl)
        ld a,l
        and 0x1f
        ld l,a ;in scrbuf
         ld e,b
        ld h,scrbuf/256
        dup 8+COLOUR
        ld a,(bc)
        ld (hl),a
        inc b
        inc h
        edup
        org $-2
         ld b,e;tilegfx/256
        ret ;NC
showrighttile
         jr nc,drawrighttile
       if SCROLLING
;не бывает
        ld hl,-2
        add hl,sp
        ld (hl),FLAG_SHOWTILEFROMMEM
        inc l
        ld (hl),FLAG_SHOWTILEFROMMEM
       else
        ld hl,-1
        add hl,sp
        ld (hl),FLAG_RETILE
       endif
         ld e,l ;in screen
         ld d,ly
        ld a,l
        and 0x1f
        ld l,a ;in scrbuf
        ld h,scrbuf/256
        dup 8
        ld a,(hl)
        ld (de),a
        inc d
        inc h
        edup
        org $-2
       if COLOUR
        ld d,hy
        inc h
        ld a,(hl)
        ld (de),a
         scf
       else
         scf
       endif
        ret
showrighttile_lefttilefrommem
         jr nc,drawrighttile
       if SCROLLING
        ld hl,-1
        add hl,sp
        ld (hl),FLAG_SHOWTILEFROMMEM
        dec l
       else
        ld hl,-1
        add hl,sp
        ld (hl),FLAG_RETILE
        dec l
        ld (hl),FLAG_SKIPTILE
       endif
        inc h
         ld e,l ;in screen
         ld d,ly
        ld l,(hl) ;left tile
        ld h,b;tilegfx/256
        dup 8
        ld a,(hl)
        ld (de),a
        inc d
        inc h
        edup
        org $-2
       if COLOUR
        ld c,d
        ld d,hy
        inc h
        ld a,(hl)
        ld (de),a
       endif
        inc e ;in screen
        ld a,e
        and 0x1f
        ld l,a ;in scrbuf
       if COLOUR
        ld h,attrbuf/256
        ld a,(hl)
        ld (de),a
        dec h
        ld d,c
       else
        ld h,scrbuf/256+7
       endif
        dup 8
        ld a,(hl)
        ld (de),a
        dec d
        dec h
        edup
        org $-2
         scf
        ret

drawleftandrighttiles
        ld hl,-1+256
        add hl,sp                ;+21
drawleftandrighttiles_hl
        ld e,(hl) ;right tile
        dec l
        ld c,(hl) ;left tile
        ld a,l                   ;+22
        and 0x1f
        ld l,a ;in scrbuf
        ld h,scrbuf/256          ;+15
        dup 8+COLOUR
        ld a,(bc)
        ld (hl),a
        inc b
        inc h
        edup
        org $-2
        ld c,e ;right tile
        inc l ;scrbuf            ;+176
        dup 8+COLOUR
        ld a,(bc)
        ld (hl),a
        dec b
        dec h
        edup
        org $-2                  ;+168
        ret ;NC                  ;+10 ;412t/2tiles
showleftandrighttiles
         jr nc,drawleftandrighttiles
       if SCROLLING
        ld hl,-1
        add hl,sp
        ld (hl),FLAG_SHOWTILEFROMMEM
        dec l
        ld (hl),FLAG_SHOWTILEFROMMEM
       else ;то же
        ld hl,-1
        add hl,sp
        ld (hl),FLAG_RETILE
        dec l
        ld (hl),FLAG_RETILE
       endif
         ld e,l ;in screen
         ld d,ly
        ld c,h ;>=8 for ldi keeping b
        ld a,l
        and 0x1f
        ld l,a ;in scrbuf
        ld h,scrbuf/256
        dup 4
        ldi
        ld a,(hl)
        ld (de),a
        inc d
        inc h
        ldd
        ld a,(hl)
        ld (de),a
        inc d
        inc h
        edup
        org $-2
       if COLOUR
        ld d,hy
        inc h
        ldi
        ld a,(hl)
        ld (de),a
         scf
       endif
         scf
        ret

;ly=screen line/256
;b=tilegfx/256
;CY=drawtobuf, NC=showfrombuf
showlefttilefrommem
       if SCROLLING
;не бывает
        ld hl,-1
        add hl,sp ;CY
        ld (hl),FLAG_SHOWTILEFROMMEM
        dec l
       else
        ld hl,-2
        add hl,sp ;CY
        ld (hl),FLAG_SKIPTILE
       endif
        inc h
         ld e,l ;in screen
         ld d,ly
        ld l,(hl) ;tile
        ld h,b;tilegfx/256
        dup 8
        ld a,(hl)
        ld (de),a
        inc d
        inc h
        edup
        org $-2
       if COLOUR
        ld d,hy
        inc h
        ld a,(hl)
        ld (de),a
       endif
        ret ;CY
showrighttilefrommem
       if SCROLLING
;не бывает
        ld hl,-2
        add hl,sp ;CY
        ld (hl),FLAG_SHOWTILEFROMMEM
        inc l
       else
        ld hl,-1
        add hl,sp ;CY
        ld (hl),FLAG_SKIPTILE
       endif
        inc h
         ld e,l ;in screen
         ld d,ly
        ld l,(hl) ;tile
        ld h,b;tilegfx/256
        dup 8
        ld a,(hl)
        ld (de),a
        inc d
        inc h
        edup
        org $-2
       if COLOUR
        ld d,hy
        inc h
        ld a,(hl)
        ld (de),a
       endif
        ret ;CY
showleftandrighttilesfrommem
;the most optimized!!! used for scroll
       if SCROLLING
        ld hl,-1+256
        add hl,sp ;NC
        scf ;TODO CY<->NC глобально?
       else
        ld hl,-2
        add hl,sp ;CY
        ld (hl),FLAG_SKIPTILE
        inc l
        ld (hl),FLAG_SKIPTILE
        inc h
       endif
        ld c,(hl) ;right tile
        dec l
         ld e,l ;in screen
         ld d,ly
        ld l,(hl) ;left tile
        ld h,b;tilegfx/256
        dup 8
        ld a,(hl)
        ld (de),a
        inc d
        inc h
        edup
        org $-2
       if COLOUR
        ld d,hy
        inc h
        ld a,(hl)
        ld (de),a
       endif
        inc e ;in screen
        ld l,c ;right tile
       if COLOUR
        ld a,(hl)
        ld (de),a
        ld d,ly
        ld h,b
        dup 8
        ld a,(hl)
        ld (de),a
        inc d
        inc h
        edup
       else
        dup 8
        ld a,(hl)
        ld (de),a
        dec d
        dec h
        edup
       endif
        org $-2
        ret ;CY
