debug=0
        include "defs.asm"
;lispstack=#6100

IMVEC=#8000;#be00
IMSTACK=#8140;#bf40
ON_INT=#8181;#bfbf
STACK=ON_INT

basetime=192
endtime=-1;(5*192)

timer=23728

bright=64

rebyte=0x6000
rebyte1=0xe000 ;pg7
repic=0xd000;0xcf00
repic1=0x9000
;во время загрузки (картинка в mlz, примерно 24K, с адреса 0x8000..0x8fff):
;rebyte1.mlz -> pg7 rebyte1 (0xe000)
;перегоняем весь упакованный блок в конец памяти
;rebyte.mlz -> rebyte (0x6000)
;repic1.mlz -> repic1 (0x9000)
;repic.mlz -> repic (0xd000 или чуть ниже)


	device pentagon1024

	org #6100
begin
	xor a
	out (#fe),a
	ld (#5d10),a ;иначе не работает лоадер
	ld (iy+1),#cc
	;ld sp,STACK

	;ld hl,module
	;call pt3player.INIT

	;ld hl,wasimer
	;ld de,ON_INT
	;ld bc,szimer
	;ldir
	if IMVEC==#3bff
           ;LD hl,#c9fb
           ;LD (#fff4),hl
	   ld a,#c3
	   ld (#fff4),a
           ld hl,ON_INT
           ld (#fff5),hl
	   ld a,#18
	   ld (#ffff),a
	ld a,IMVEC/256
	else
           ;ld hl,ON_INT
           ;ld (IMVEC+255),hl
	ld hl,IMVEC
	ld a,h
	ld d,ON_INT/256
	ld (hl),d
	inc l
	jr nz,$-2
	inc h
	ld (hl),d
	endif
	ld i,a

        ;ld hl,wasrebyte+8191
        ;ld de,rebyte+8191
        ;ld bc,8192
        ;lddr
        
        ;call loader
        ;ld a,#10
        ;call setpg
        
;init

	;ld hl,#5800
	;ld de,#5801
	;ld (hl),7*8+bright
	;ld bc,767
	;ldir ;зачерняем, чтобы не было кривых атрибутов

	ld hl,#4000
	ld de,#4001
        ld bc,#17ff
        ld (hl),l;#ff
        ldir

        call mkpush
        
	jp maingo

        if 1==0
loader
        ld hl,tfiles
        ld e,#50
loader0
        ld a,(hl)
        or a
        ret z
        ld a,e
        call setpg
        call loadpage
        inc e
        ld a,e
        cp #58
        jr nz,$+4
        ld e,#d0
        jr loader0
loadpage
        push de
        ld de,#5cdd
        ld bc,9
        ldir
        push hl
        ld c,#a ;find
        call #3d13
        ld a,c
        ld c,#8 ;load descr
        call #3d13
        xor a
        ld (23801),a ;?
        ld hl,#c000
        ld de,(#5ce8)
        ld a,3
        ld c,#e
        call #3d13
        pop hl
        pop de
        ret
tfiles
        db "frame0  f"
        db "frame1  f"
        db "frame2  f"
        db "frame3  f"
        db "frame4  f"
        db "frame5  f"
        db "frame6  f"
        db "frame7  f"
        db "frame8  f"
        db "frame9  f"
        db "frame10 f"
        db "frame11 f"
        db "frame12 f"
        db "frame13 f"
        db "frame14 f"
        db "frame15 f"
        db 0
        endif
        
	include "pt3play.asm"
module
	incbin "testmusic.pt3"

        
;=====================================
	ds ON_INT-$
	;org #8000
wasimer
	;disp ON_INT
	ld (imstack),sp
        ex (sp),hl
        ld (imjp),hl
        pop hl
        push bc
	ld sp,IMSTACK
	push af
	;push bc
	push de
	push hl
	
        exx
        push bc
        push de
        push hl
        
	push ix
	ex af,af'
	push af
	push iy

	ld hl,(timer)
	inc hl
	ld (timer),hl

        call mcnoise ;reter;multicolor
intcall2=$+1
	call reter;pt3player.PLAY
        
	 ld iy,23610
	 rst #38
	
timersec=$+1
	ld a,50
	dec a
	jr nz,timernsec
	;call showfps
	ld a,50
timernsec
	ld (timersec),a

	pop iy
	pop af
	ex af,af'
	pop ix

        pop hl
        pop de
        pop bc
        exx
        
	pop hl
	pop de
	;pop bc
	pop af
imstack=$+1
	ld sp,0
        pop bc
	ei
imjp=$+1
	jp 0
;timer
	;dw 0
	;ent
szimer=$-wasimer

        if 1==0
fillline
        ld b,#20
        push hl
        ld (hl),a
        inc hl
        djnz $-2
        pop hl
        inc h
        ld a,h
        and 7
        ret nz
        ld a,l
        add a,32
        ld l,a
        ret c
        ld a,h
        sub 8
        ld h,a
        ret
        endif

shufflebuf=0x4000

shufflelines
shufflelines0
        push hl
;copy column to buf (linear)
        ld de,shufflebuf
        ld b,192
shufflelines1
        ld a,(hl)
        ld (de),a
        inc e
        push de
        ld de,64
        add hl,de
        pop de
        djnz shufflelines1
        pop hl
        push hl
;copy column from buf (non-linear)
        ld de,shufflebuf
        ld b,192
shufflelines2
        ld a,(de)
        ld (hl),a
        ld a,e
        add a,8
        ld e,a
        and 56
        jr nz,shufflelines2_nochr
        ld a,e
        sub 63
        ld e,a
        and 7
        jr nz,shufflelines2_nochr
        ld a,e
        add a,56
        ld e,a
shufflelines2_nochr
        push de
        ld de,64
        add hl,de
        pop de
        djnz shufflelines2
        pop hl
        inc l
        ld a,l
        and 63
        jr nz,shufflelines0
        ret

maingo

        ld bc,0x7ffd
        ld a,0x17
        out (c),a
        ld hl,wasrebyte1
        ld de,rebyte1
        call DEC40

	;ld hl,#d800
	;ld de,#d801
	;ld (hl),4*8+bright
	;ld bc,767
	;ldir ;зачерняем, чтобы не было кривых атрибутов

        ld bc,0x7ffd
        ld a,0x10
        out (c),a

        ;ld hl,wasrebyte
        ;ld de,rebyte
        ;call DEC40

        ld hl,packedmoveend-1
        ld de,0xffff
        ld bc,packedmovesize
        lddr
        ld hl,packedmovestart
        ld de,rebyte
        call DEC40
        ld hl,packedmovestart+wasrebyte_sz
        ld de,repic1
        call DEC40
        ld hl,packedmovestart+wasrebyte_sz+wasrepic1_sz
        ld de,repic
        call DEC40

;переставить нечётные строки между картинками? нельзя (у них разные rebyte)
        ;ld hl,repic
        ;ld de,

        ;ld hl,repic1
        ;call shufflelines
        ;ld hl,repic
        ;call shufflelines

	halt
       
	im 2
	ei 

        if 1==0
        ld hl,#6000
        ld e,192/3
rgb0
        ld a,#42
        call fillline
        ld a,#44
        call fillline
        ld a,#41
        call fillline
        dec e
        jr nz,rgb0
        
        ld bc,#eff7
        ld a,#20 ;hardware multicolor
        ;out (c),a
        endif
        
	ei
	halt
	
        ;ld hl,pt3player.PLAY
        ;ld (intcall2),hl

	ld hl,(timer)
	ld (oldtimer),hl
mainloop
        ;halt
        if 1==1
        ld a,r
        and 15
        jr z,$+2
        and 7
        jr z,mainloop_skiptestmc
        ld ix,0
        rl (ix) ;проверка настройки мультиколора на вшивость
mainloop_skiptestmc
        endif
        
        ld hl,framesdrawn
        inc (hl)
        
	ld hl,(timer)
oldtimer=$+1
	ld de,0
	ld (oldtimer),hl
	or a
	sbc hl,de
	jp z,mainloop
	ld b,l
	;call moveall
	;djnz $-3
	ld hl,(timer)
	ld de,endtime-28;20
	ld (oldtimer),hl
	or a
	sbc hl,de
	jp c,mainloop
quit

        jp $

reter
        ret

tachunk
        ;db 0x0, 0xa, 0x2, 0x8
        ;db 0x5, 0xf, 0x7, 0xd
        ;db 0x4, 0xe, 0x6, 0xc
        ;db 0x1, 0xb, 0x3, 0x9
        
        ;db 0x7, 0xe, 0x2, 0xa
        ;db 0x9, 0x0, 0xc, 0x4
        ;db 0xd, 0xb, 0x6, 0x1
        ;db 0x3, 0x5, 0x8, 0xf
        
        ;db 0x0,0xd,0xa,0x7
        ;db 0x4,0x1,0xe,0xb
        ;db 0x8,0x5,0x2,0xf
        ;db 0xc,0x9,0x6,0x3
       
        db 0x0,0xa,0x7,0xd
        db 0x4,0xe,0xb,0x1
        db 0x8,0x2,0xf,0x5
        db 0xc,0x6,0x3,0x9
       
        db -1
        
        if 1==0
drawnoise
        ;di
        ld (drawnoisesp),sp
        ld sp,repic
curachunk=$+1
        ld hl,tachunk
        ld a,(hl)
        or a
        jp p,$+3+3+1
         ld hl,tachunk
         ld a,(hl)
        inc hl
        ld (curachunk),hl
        add a,a
        add a,rebyte/256
        ld h,a ;rebyte/256 ;+512*(4*y + x)
        ld lx,192
drawnoise0
        dup 32/2
        pop bc ;4pix + 4pix
        ld l,c
        ld a,(hl) ;%abcd0000
        inc h
        ld l,b
        or (hl)   ;%0000efgh
        ld (de),a
        inc e
        pop bc ;4pix + 4pix
        ld l,b
        ld a,(hl) ;%0000efgh
        dec h
        ld l,c
        or (hl)   ;%abcd0000
        ld (de),a
        inc e
        edup
        org $-1
        ld a,e
        sub 31
        ld e,a
        
        bit 0,d
        jr z,drawnoise_skipnewphase ;for R-Mode
        ld a,h
        ;add a,4*2
        ;or rebyte/256
         cp rebyte/256+16 -(4*2)
         jr c,$+4
          sub 16
         add a,4*2
        ld h,a        
drawnoise_skipnewphase
        
        inc d
        ld a,d
        and 7
        jp nz,drawnoisenchr

        ld a,h
         cp rebyte/256+16 -(1*2)
         jr c,$+4
          sub 16
         add a,1*2
        ld h,a        

        ld a,e
        sub -32
        ld e,a
        sbc a,a
        and -8
        add a,d
        ld d,a
drawnoisenchr
        dec lx
        jp nz,drawnoise0
drawnoisesp=$+1
        ld sp,0
        ret
        endif

        macro SLOWDOWN
        if PENT
        ds 3
        endif
        endm

mcnoise
        ld (mcloop0sp),sp
        ld bc,0x7ffd
        ld a,0x17
        out (c),a
mcnoisepicpg=$+1
        ld de,0x1018 ;or +7
        exx
        
        ld ix,mcpush1q
        ld sp,0x5800+0x300
mcnoisepusheraddr=$+1
        jp prog
mcpush1q
        if PENT
        ds 1
        else
        ds 11 ;10=глюки справа вверху на +3
        endif
mcnoisepicH=$+2
        ld sp,repic
mcnoisescraddrH=$+2
        ld de,0x4000
curchunkH=$+1
        ld h,rebyte1/256
        ;jr $
mcloop0
;max +122 (119-1, 120-1 или 126-1 (ld h,N))
         exx
         out (c),d ;screen change at +138 (nowait)
         exx
;+142
        pop bc ;4pix + 4pix
        ld l,c
        ld a,(hl) ;%abcd0000 ;slowmem
        inc h
        ld l,b
        or (hl)   ;%0000efgh ;slowmem
        ld (de),a ;slowmem
;+185
        pop bc ;4pix + 4pix
        ld l,b
        ld a,(hl) ;%0000efgh ;slowmem
        ld l,c
;+210
         pop bc ;4pix + 4pix ;!!!!!!!!!
        inc e
        dec h
        or (hl)   ;%abcd0000 ;slowmem
;+235 = +7
        ld (de),a ;slowmem (write at t-state +16)
;+16
        inc e
        ld l,c
        ld a,(hl) ;%abcd0000 ;slowmem
        inc h
        ld l,b
        or (hl)   ;%0000efgh ;slowmem
;+48
        ld (de),a ;slowmem (write at t-state +56(55))
;+56
        pop bc ;4pix + 4pix
;+66
        inc e
         nop ;!!!!!!!!!!!!!!!
        ld l,b
        ld a,(hl) ;%0000efgh ;slowmem
        dec h
        ld l,c
        or (hl)   ;%abcd0000 ;slowmem
;+104
        ld (de),a ;slowmem (write at t-state +112(111))
;+112
;-----------------
         inc e
         exx
         out (c),e ;screen change at +132 (nowait) - нельзя сдвинуть назад!
         exx
;+136
        pop bc ;4pix + 4pix
        ld l,c
        ld a,(hl) ;%abcd0000 ;slowmem
        inc h
        ld l,b
        or (hl)   ;%0000efgh ;slowmem
        ld (de),a ;slowmem
        inc e ;47
;+183
        pop bc ;4pix + 4pix
        ld l,b
        ld a,(hl) ;%0000efgh ;slowmem
        ld l,c
;+208
         pop bc ;4pix + 4pix
        dec h
        or (hl)   ;%abcd0000 ;slowmem
;+229 = +1
        ld (de),a ;slowmem (write at t-state +8)
;+8
        inc e
        ld l,c
        ld a,(hl) ;%abcd0000 ;slowmem
        inc h
        ld l,b
        or (hl)   ;%0000efgh ;slowmem
;+40
        ld (de),a ;slowmem (write at t-state +48(47))
;+48
        pop bc ;4pix + 4pix
;+58
        inc e
         nop ;!!!!!!!!!!!!!!!
        ld l,b
        ld a,(hl) ;%0000efgh ;slowmem
        dec h
        ld l,c
        or (hl)   ;%abcd0000 ;slowmem
;+96
        ld (de),a ;slowmem (write at t-state +104(103))
;+104
        SLOWDOWN
         inc e
         jr nz,mcloop0 ;+120
;+115
        inc d
;+119
mcloop1
;max +122 (119-1, 120-1 или 126-1 (ld h,N))
         exx
         out (c),d ;screen change at +138 (nowait)
         exx
;+142
        pop bc ;4pix + 4pix
        ld l,c
        ld a,(hl) ;%abcd0000 ;slowmem
        inc h
        ld l,b
        or (hl)   ;%0000efgh ;slowmem
        ld (de),a ;slowmem
;+185
        pop bc ;4pix + 4pix
        ld l,b
        ld a,(hl) ;%0000efgh ;slowmem
        ld l,c
;+210
         pop bc ;4pix + 4pix ;!!!!!!!!!
        inc e
        dec h
        or (hl)   ;%abcd0000 ;slowmem
;+235 = +7
        ld (de),a ;slowmem (write at t-state +16)
;+16
        inc e
        ld l,c
        ld a,(hl) ;%abcd0000 ;slowmem
        inc h
        ld l,b
        or (hl)   ;%0000efgh ;slowmem
;+48
        ld (de),a ;slowmem (write at t-state +56(55))
;+56
        pop bc ;4pix + 4pix
;+66
        inc e
         nop ;!!!!!!!!!!!!!!!
        ld l,b
        ld a,(hl) ;%0000efgh ;slowmem
        dec h
        ld l,c
        or (hl)   ;%abcd0000 ;slowmem
;+104
        ld (de),a ;slowmem (write at t-state +112(111))
;+112
;-----------------
         inc e
         exx
         out (c),e ;screen change at +132 (nowait) - нельзя сдвинуть назад!
         exx
;+136
        pop bc ;4pix + 4pix
        ld l,c
        ld a,(hl) ;%abcd0000 ;slowmem
        inc h
        ld l,b
        or (hl)   ;%0000efgh ;slowmem
        ld (de),a ;slowmem
        inc e ;47
;+183
        pop bc ;4pix + 4pix
        ld l,b
        ld a,(hl) ;%0000efgh ;slowmem
        ld l,c
;+208
         pop bc ;4pix + 4pix
        dec h
        or (hl)   ;%abcd0000 ;slowmem
;+229 = +1
        ld (de),a ;slowmem (write at t-state +8)
;+8
        inc e
        ld l,c
        ld a,(hl) ;%abcd0000 ;slowmem
        inc h
        ld l,b
        or (hl)   ;%0000efgh ;slowmem
;+40
        ld (de),a ;slowmem (write at t-state +48(47))
;+48
        pop bc ;4pix + 4pix
;+58
        inc e
         nop ;!!!!!!!!!!!!!!!
        ld l,b
        ld a,(hl) ;%0000efgh ;slowmem
        dec h
        ld l,c
        or (hl)   ;%abcd0000 ;slowmem
;+96
        ld (de),a ;slowmem (write at t-state +104(103))
;+104
        SLOWDOWN
         inc e
         jr nz,mcloop1 ;+120
;+115
        inc d
;+119
mcHpatch=$+1
        ld h,0
;+126
mcloop2
;+126(128)
         exx
         out (c),d ;screen change at +138 (nowait)
         exx
;+142+4(+2)
        pop bc ;4pix + 4pix
        ld l,c
        ld a,(hl) ;%abcd0000 ;slowmem
        inc h
        ld l,b
        or (hl)   ;%0000efgh ;slowmem
        ld (de),a ;slowmem
;+185+4(+2)
        pop bc ;4pix + 4pix
        ld l,b
        ld a,(hl) ;%0000efgh ;slowmem
        ld l,c
;+210+4(+2)
        dec h
        or (hl)   ;%abcd0000 ;slowmem
;+221+4(+2)
         inc e ;!!!!!!!!!!!!!!!!!!!!!!!
         pop bc ;4pix + 4pix ;!!!!!!!!!
;+235+4(+2) = +11(+2)
        ld (de),a ;slowmem (write at t-state +16+8(18..20))
;+16+8
        inc e
        ld l,c
        ld a,(hl) ;%abcd0000 ;slowmem
        inc h
        ld l,b
        or (hl)   ;%0000efgh ;slowmem
;+48+8
        ld (de),a ;slowmem (write at t-state +56(55)+8)
;+56+8
        pop bc ;4pix + 4pix
;+66+8
        inc e
         nop ;!!!!!!!!!!!!!!!
        ld l,b
        ld a,(hl) ;%0000efgh ;slowmem
        dec h
        ld l,c
        or (hl)   ;%abcd0000 ;slowmem
;+104+8
        ld (de),a ;slowmem (write at t-state +112(111)+8)
;+112+8
;-----------------
         inc e
         exx
         out (c),e ;screen change at +132+8 (nowait)
         exx
;+136+8
        pop bc ;4pix + 4pix
        ld l,c
        ld a,(hl) ;%abcd0000 ;slowmem
        inc h
        ld l,b
        or (hl)   ;%0000efgh ;slowmem
        ld (de),a ;slowmem
        inc e ;47
;+183+8
        pop bc ;4pix + 4pix
        ld l,b
        ld a,(hl) ;%0000efgh ;slowmem
        ld l,c
;+208+8
         pop bc ;4pix + 4pix ;!!!!
        dec h
        or (hl)   ;%abcd0000 ;slowmem
;+229+8 = +1+8
        ld (de),a ;slowmem (write at t-state +8+8)
;+8+8
        inc e
        ld l,c
        ld a,(hl) ;%abcd0000 ;slowmem
        inc h
        ld l,b
        or (hl)   ;%0000efgh ;slowmem
;+40+8
        ld (de),a ;slowmem (write at t-state +48(47)+8)
;+48+8
        pop bc ;4pix + 4pix
;+58+8
        inc e
         nop ;!!!!!!!!!!!!!!!
        ld l,b
        ld a,(hl) ;%0000efgh ;slowmem
        dec h
        ld l,c
        or (hl)   ;%abcd0000 ;slowmem
;+96+8
        ld (de),a ;slowmem (write at t-state +104(103)+8)
;+104+8
        SLOWDOWN
         inc e
         jr nz,mcloop2 ;+120+8
;+115+8
        inc d
;+119+8
mcloop3
;+126(128)
         exx
         out (c),d ;screen change at +138 (nowait)
         exx
;+142+4(+2)
        pop bc ;4pix + 4pix
        ld l,c
        ld a,(hl) ;%abcd0000 ;slowmem
        inc h
        ld l,b
        or (hl)   ;%0000efgh ;slowmem
        ld (de),a ;slowmem
;+185+4(+2)
        pop bc ;4pix + 4pix
        ld l,b
        ld a,(hl) ;%0000efgh ;slowmem
        ld l,c
;+210+4(+2)
        dec h
        or (hl)   ;%abcd0000 ;slowmem
         inc e ;!!!!!!!!!!!!!!!!!!!!!!!
         pop bc ;4pix + 4pix ;!!!!!!!!!
;+235+4(+2) = +11(+2)
        ld (de),a ;slowmem (write at t-state +16+8)
;+16+8
        inc e
        ld l,c
        ld a,(hl) ;%abcd0000 ;slowmem
        inc h
        ld l,b
        or (hl)   ;%0000efgh ;slowmem
;+48+8
        ld (de),a ;slowmem (write at t-state +56(55)+8)
;+56+8
        pop bc ;4pix + 4pix
;+66+8
        inc e
         nop ;!!!!!!!!!!!!!!!
        ld l,b
        ld a,(hl) ;%0000efgh ;slowmem
        dec h
        ld l,c
        or (hl)   ;%abcd0000 ;slowmem
;+104+8
        ld (de),a ;slowmem (write at t-state +112(111)+8)
;+112+8
;-----------------
         inc e
         exx
         out (c),e ;screen change at +132+8 (nowait)
         exx
;+136+8
        pop bc ;4pix + 4pix
        ld l,c
        ld a,(hl) ;%abcd0000 ;slowmem
        inc h
        ld l,b
        or (hl)   ;%0000efgh ;slowmem
        ld (de),a ;slowmem
        inc e ;47
;+183+8
        pop bc ;4pix + 4pix
        ld l,b
        ld a,(hl) ;%0000efgh ;slowmem
        ld l,c
;+208+8
         pop bc ;4pix + 4pix ;!!!!
        dec h
        or (hl)   ;%abcd0000 ;slowmem
;+229+8 = +1+8
        ld (de),a ;slowmem (write at t-state +8+8)
;+8+8
        inc e
        ld l,c
        ld a,(hl) ;%abcd0000 ;slowmem
        inc h
        ld l,b
        or (hl)   ;%0000efgh ;slowmem
;+40+8
        ld (de),a ;slowmem (write at t-state +48(47)+8)
;+48+8
        pop bc ;4pix + 4pix
;+58+8
        inc e
         nop ;!!!!!!!!!!!!!!!
        ld l,b
        ld a,(hl) ;%0000efgh ;slowmem
        dec h
        ld l,c
        or (hl)   ;%abcd0000 ;slowmem
;+96+8
        ld (de),a ;slowmem (write at t-state +104(103)+8)
;+104+8
        SLOWDOWN
         inc e
         jr nz,mcloop3 ;+120+8
;+115+8
mcloop0sp=$+1
        ld sp,0
        
        ld hl,(mcnoisepusheraddr)
        ld a,h
        xor (prog^prog1)/256
        ld h,a
        ld a,l
        xor (prog^prog1)&0xff
        ld l,a
        ld (mcnoisepusheraddr),hl
        
;выводим картинки в порядке:
;scr0 block0
;scr1 block3
;scr0 block1
;scr1 block4
;scr0 block2
;scr1 block5
;scr0 block3
;scr1 block0
;scr0 block4
;scr1 block1
;scr0 block5
;scr1 block2
        ld a,(curchunkH) ;rebyte/256 ;+512*(4*y + x)
         xor 0x80 ;каждый фрейм меняем выводимый экран
        and 0xe0
        ld c,a
curpicblocksaddr=$+1
        ld hl,tpicblocks
        ld a,(hl)
        or a
        jp nz,$+3+3+1
         ld hl,tpicblocks
         ld a,(hl)
        ld (mcnoisepicH),a
        inc hl
        ld a,(hl)
        ld (mcnoisescraddrH),a
        inc hl
        ld a,(hl)
         ;and 0x17
        ld (mcnoisepicpg+1),a
        xor 8
        ld (mcnoisepicpg),a
        inc hl
        ld (curpicblocksaddr),hl
        ld a,(hl)
        or a
        jr nz,noendpic
;когда картинка кончилась:
curachunk=$+1
        ld hl,tachunk
        ;ld hl,(curachunk)
        ld a,(hl)
        or a
        jp p,$+3+3+1
         ld hl,tachunk
         ld a,(hl)
        inc hl
        ld (curachunk),hl
        add a,a
        or c
        ld (curchunkH),a ;rebyte/256 ;+512*(4*y + x)
noendpic
        ld a,(curchunkH) ;rebyte/256 ;+512*(4*y + x)
         or a
         jp p,nochangephase
        add a,8*2
nochangephase
        and 0x1f
        or c
        ld (curchunkH),a ;rebyte/256 ;+512*(4*y + x)
        add a,4*2
        and 0x1f
        or c
        ld (mcHpatch),a
        ret

pg0=0x10
pg7=0x17
scr0=0x4000
scr1=0xc000

;ещё каждый фрейм меняем фазу гигаскрина
tpicblocks
        db (repic + 0*0x800)/256,(scr0+0*0x400)/256,pg0
        db (repic1+ 3*0x800)/256,(scr1+3*0x400)/256,pg7+8
        db (repic + 1*0x800)/256,(scr0+1*0x400)/256,pg0
        db (repic1+ 4*0x800)/256,(scr1+4*0x400)/256,pg7+8
        db (repic + 2*0x800)/256,(scr0+2*0x400)/256,pg0
        db (repic1+ 5*0x800)/256,(scr1+5*0x400)/256,pg7+8
        db (repic + 3*0x800)/256,(scr0+3*0x400)/256,pg0
        db (repic1+ 0*0x800)/256,(scr1+0*0x400)/256,pg7+8
        db (repic + 4*0x800)/256,(scr0+4*0x400)/256,pg0
        db (repic1+ 1*0x800)/256,(scr1+1*0x400)/256,pg7+8
        db (repic + 5*0x800)/256,(scr0+5*0x400)/256,pg0
        db (repic1+ 2*0x800)/256,(scr1+2*0x400)/256,pg7+8
        db 0

showfps
	ld a,(framesdrawn)
	push de
	ld de,#4000
	ld bc,10-256
	inc b
	sub c
	jr nc,$-2
	add a,c
	ld c,a
	ld a,b
	call showdig
	ld a,c
	call showdig
         ;ld a,0x0d
         ;call _ttyputa
	pop de
	xor a
	ld (framesdrawn),a
	ret
showdig
	add a,"0"
         ;jp _ttyputa
showchar
	push de
	add a,a
	ld h,15
	ld l,a
	add hl,hl
	add hl,hl
	ld b,8
showdig0
	ld a,(hl)
	ld (de),a
	inc l
	inc d
	djnz showdig0
	pop de
	inc e
	ret
framesdrawn
	db 0

_tables=#8000;endfree
	macro BEGIN256 size
_codes=$
	org _tables-size
	.ALIGN 256
_tables=$
	endm
	macro END256
	org _codes
	endm

setpg
        push bc
        ld (#5b5c),a
        ld bc,#7ffd
        out (c),a
        pop bc
        ret
        
        ;INCLUDE "rmode.asm"
        include "megaLZunpack.asm"

        display "active code size=",$-0x8000
        
;0: G/M - низ(нечет) scr0
;1: R/C - низ(нечет) scr1
;2: B/Y - верх(чёт) scr0
;3: W - верх(чёт) scr1
;frame1: W (scr1), G/M (scr0)
;frame2: B/Y (scr0), R/C (scr1)
mkpush
	ld hl,progpush
	ld de,wasattr0+767 ;G/M - низ(нечет)
	;ld bc,#5b00
        ld lx,0x44
	call mkpusher ;низ(нечет) первого фрейма
	;ld de,wasattr3+767 ;W - верх(чёт)
	;ld bc,#db00
	;ld ix,#4747
	;call mkpusher ;верх(чёт) первого фрейма
	ld hl,prog1push2
	ld de,wasattr1+767 ;R/C - низ(нечет)
	;ld bc,#db00
        ld lx,0x42
	call mkpusher ;низ(нечет) второго фрейма
	ld hl,prog1push
	ld de,wasattr2+767 ;B/Y - верх(чёт)
	;ld bc,#5b00
	ld lx,0x41
	;call mkpusher ;верх(чёт) второго фрейма
mkpusher
        if 1==0
;ix=два разрешённых значения
;bc=HH
;de=HL
;hl=LH
;af=LL
	ld (hl),#31
	inc hl
	ld (hl),c
	inc hl
	ld (hl),b
	inc hl
	ld b,hx
	ld a,lx
	ld (hl),#01
	inc hl
	ld (hl),b
	inc hl
	ld (hl),b
	inc hl
	ld (hl),#11
	inc hl
	ld (hl),a;b
	inc hl
	ld (hl),b;a
	inc hl
	ld (hl),#21
	inc hl
	ld (hl),a
	inc hl
	ld (hl),a
	inc hl
	ld (hl),#e5 ;push hl
	inc hl
	ld (hl),#f1 ;pop af
	inc hl
	ld (hl),#21
	inc hl
	ld (hl),b;a
	inc hl
	ld (hl),a;b
	inc hl
        endif
	ld b,24;scrdatahgt
mkpusher0
	push de
	ld c,#20/2
mkpusher1
	ld a,(de)
	dec de
	xor lx
	sub 1
	sbc a,a
	and #20
	ld (hl),a
	ld a,(de)
	dec de
	xor lx
	sub 1
	sbc a,a
	and #10
	or (hl)
	;a=#00/#10/#20/#30
	add a,#c5
	ld (hl),a ;push bc/de/hl/af
	inc hl
	dec c
	jr nz,mkpusher1
	pop de
	call udeattr
	djnz mkpusher0
	;ld (hl),#dd ;ix
	;inc hl
	;ld (hl),#e9 ;jp (hl)
	;inc hl
	ret
udeattr
	ld a,e
	sub 32
	ld e,a
	ret nc
	dec d
	ret

prog
;generate pushes here
;2 procedures like this:
;ld hl:push hl:pop af
;ld bc
;ld de
;ld hl
;push bc/de/hl/af * 384
;ld sp,0xd800+0x300
;ещё раз то же
;jp (ix)
;bc=HH ;C/M/Y
;de=LH
;hl=HL
;af=LL ;R/G/B
        ld hl,0x4444 ;!!!
        push hl
        pop af
        ld bc,0x4343 ;!!!
        ld h,a
        ld l,c
        ld d,b
        ld e,a
progpush
        dup 384
        push bc
        edup
        ld sp,0xd800+0x300
        ld hl,0x4747
        push hl
        pop af
        ld bc,0x4747
        ld h,a
        ld l,c
        ld d,b
        ld e,a
        dup 384
        push bc
        edup
        jp (ix)
prog1
        ld hl,0x4141 ;!!!
        push hl
        pop af
        ld bc,0x4646 ;!!!
        ld h,a
        ld l,c
        ld d,b
        ld e,a
prog1push
        dup 384
        push bc
        edup
        ld sp,0xd800+0x300
        ld hl,0x4242
        push hl
        pop af
        ld bc,0x4545
        ld h,a
        ld l,c
        ld d,b
        ld e,a
prog1push2
        dup 384
        push bc
        edup
        jp (ix)

wasattr0
        incbin "c0"
wasattr1
        incbin "c1"
wasattr2
        incbin "c2"
;wasattr3
;        incbin "c3"
        
waspacked
;во время загрузки (картинка в mlz, примерно 24K, с адреса 0x8000..0x8fff):
;rebyte1.mlz -> pg7 rebyte1 (0xe000)
;перегоняем весь упакованный блок в конец памяти
;rebyte.mlz -> rebyte (0x6000)
;repic1.mlz -> repic1 (0x9000)
;repic.mlz -> repic (0xd000 или чуть ниже)
wasrebyte1
        incbin "rebyte1.mlz"
wasrebyte1_sz=$-wasrebyte1
wasrebyte
        incbin "rebyte.mlz"
wasrebyte_sz=$-wasrebyte
wasrepic1
        incbin "repic1.mlz"
wasrepic1_sz=$-wasrepic1
wasrepic
        incbin "repic.mlz"
wasrepic_sz=$-wasrepic
packedmoveend=$
packedmovesize=wasrebyte_sz+wasrepic1_sz+wasrepic_sz
packedmovestart=0x10000-packedmovesize

activeend
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

end

	display "End=",end
	;display "Free after end=",/d,endmem-end
	display "Size ",/d,end-begin," bytes"
	
	savebin "maincode.c",begin,end-begin
	
	LABELSLIST "..\us\user.l"
