inittile
       if SCROLLING
        ld hl,tilegfx
        ld c,3
mktilerrs0
        ld a,h
        add a,8+COLOUR
        ld d,a
        ld e,0
       push de
        ld a,c
        ld bc,256*(8+COLOUR)
        ldir
        ld c,a
       pop hl
       push hl
        ld b,8
mktilerr0
        xor a
        ex af,af' ;'
        xor a
        ld l,0xff
mktilerr1
        ex af,af' ;'
        rl (hl)
        ex af,af' ;'
        rl (hl)
        dec l
        jr nz,mktilerr1
        inc h
        djnz mktilerr0
       pop hl
        dec c
        jr nz,mktilerrs0
        
       if COLOUR
        ld hl,tilegfx+(0x900*2)+0x800+1
        ld de,tilegfx+(0x900*2)+0x800
        ld bc,0xff
        ldir
        ld hl,tilegfx+(0x900*3)+0x800+1
        ld de,tilegfx+(0x900*3)+0x800
        ld bc,0xff
        ldir
       endif
       endif

;set all tile flags to FLAG_SHOWTILEFROMMEM (sprites will change this to FLAG_DRAWTILE)
        ld hl,tilemap
        ld b,+(TILEMAPHGT+7)/8
cleartilemap0
         ld (hl),FLAG_SHOWTILEFROMMEM
         inc l
        jr nz,$-3
        inc h
         ld (hl),l
         inc l
        jr nz,$-2
        inc h
        djnz cleartilemap0
;set kernel for RET (L=left tile, H=right tile)
        ld a,0xc9 ;"ret"
        ld (FLAG_SKIPTILE*0x0101),a
        ld a,0xd0 ;"ret nc"
        ld hl,showlefttilefrommem
        ld (FLAG_SKIPTILE*256+FLAG_SHOWTILEFROMMEM),a
        ld (FLAG_SKIPTILE*256+FLAG_SHOWTILEFROMMEM+2),hl
        ld hl,showrighttilefrommem
        ld (FLAG_SHOWTILEFROMMEM*256+FLAG_SKIPTILE),a
        ld (FLAG_SHOWTILEFROMMEM*256+FLAG_SKIPTILE+2),hl
        ld hl,showleftandrighttilesfrommem
        ld (FLAG_SHOWTILEFROMMEM*256+FLAG_SHOWTILEFROMMEM),a
        ld (FLAG_SHOWTILEFROMMEM*256+FLAG_SHOWTILEFROMMEM+2),hl
        ld a,0xc3 ;"jp"
        ld (FLAG_SKIPTILE*256+FLAG_DRAWTILE),a
        ld (FLAG_SKIPTILE*256+FLAG_SHOWTILEFROMMEM+1),a ;ret nc:jp
        ld (FLAG_DRAWTILE*256+FLAG_SKIPTILE),a
        ld (FLAG_DRAWTILE*256+FLAG_DRAWTILE),a
        ld (FLAG_DRAWTILE*256+FLAG_SHOWTILEFROMMEM),a
        ld (FLAG_SHOWTILEFROMMEM*256+FLAG_SKIPTILE+1),a ;ret nc:jp
        ld (FLAG_SHOWTILEFROMMEM*256+FLAG_DRAWTILE),a
        ld (FLAG_SHOWTILEFROMMEM*256+FLAG_SHOWTILEFROMMEM+1),a ;ret nc:jp
        ;ld a,0xd2 ;"jp nc" can't use (no place for 6 b in FLAG_..., scrbuf=0x0800)
        ld hl,showlefttile
        ld (FLAG_SKIPTILE*256+FLAG_DRAWTILE+1),hl
        ld hl,showlefttile_righttilefrommem
        ld (FLAG_SHOWTILEFROMMEM*256+FLAG_DRAWTILE+1),hl
        ld hl,showrighttile
        ld (FLAG_DRAWTILE*256+FLAG_SKIPTILE+1),hl
        ld hl,showrighttile_lefttilefrommem
        ld (FLAG_DRAWTILE*256+FLAG_SHOWTILEFROMMEM+1),hl
        ld hl,showleftandrighttiles
        ld (FLAG_DRAWTILE*256+FLAG_DRAWTILE+1),hl
        ret

       macro DPOKEHL addr,data
       if (data&0xff00)==(_hl&0xff00)
        ld l,data&0xff
       else
        ld hl,data
       endif
        ld (addr),hl
_hl=data
       endm

       macro INITEOL
_hl=0
       if TILEMAPTOPSKIP<=0
        DPOKEHL tilemap+0x0000+scrwid,tileseol0
       endif
       if TILEMAPTOPSKIP<=1
        DPOKEHL tilemap+0x0020+scrwid,tileseol1
       endif
       if TILEMAPTOPSKIP<=2
        DPOKEHL tilemap+0x0040+scrwid,tileseol2
       endif
       if TILEMAPTOPSKIP<=3
        DPOKEHL tilemap+0x0060+scrwid,tileseol3
       endif
       if TILEMAPTOPSKIP<=4
        DPOKEHL tilemap+0x0080+scrwid,tileseol4
       endif
       if TILEMAPTOPSKIP<=5
        DPOKEHL tilemap+0x00a0+scrwid,tileseol5
       endif
       if TILEMAPTOPSKIP<=6
        DPOKEHL tilemap+0x00c0+scrwid,tileseol6
       endif
        DPOKEHL tilemap+0x00e0+scrwid,tileseol7
        DPOKEHL tilemap+0x0200+scrwid,tileseol8
        DPOKEHL tilemap+0x0220+scrwid,tileseol9
        DPOKEHL tilemap+0x0240+scrwid,tileseol10
        DPOKEHL tilemap+0x0260+scrwid,tileseol11
        DPOKEHL tilemap+0x0280+scrwid,tileseol12
        DPOKEHL tilemap+0x02a0+scrwid,tileseol13
        DPOKEHL tilemap+0x02c0+scrwid,tileseol14
        DPOKEHL tilemap+0x02e0+scrwid,tileseol15
       if TILEMAPHGT>16
       if TILEMAPBOTTOMSKIP<=7
        DPOKEHL tilemap+0x0400+scrwid,tileseol16
       endif
       if TILEMAPBOTTOMSKIP<=6
        DPOKEHL tilemap+0x0420+scrwid,tileseol17
       endif
       if TILEMAPBOTTOMSKIP<=5
        DPOKEHL tilemap+0x0440+scrwid,tileseol18
       endif
       if TILEMAPBOTTOMSKIP<=4
        DPOKEHL tilemap+0x0460+scrwid,tileseol19
       endif
       if TILEMAPBOTTOMSKIP<=3
        DPOKEHL tilemap+0x0480+scrwid,tileseol20
       endif
       if TILEMAPBOTTOMSKIP<=2
        DPOKEHL tilemap+0x04a0+scrwid,tileseol21
       endif
       if TILEMAPBOTTOMSKIP<=1
        DPOKEHL tilemap+0x04c0+scrwid,tileseol22
       endif
       if TILEMAPBOTTOMSKIP<=0
        DPOKEHL tilemap+0x04e0+scrwid,tileseol23
       endif
       endif
       endm
