;=================== INIT PART ========================
mkc2p
mkc2p_4x4
	ld hl,mkc2p_old_buf+#44
	ld de,mkc2p_old_buf+#45
	ld bc,3
	ld (hl),b
	ldir ;clear current chunk gfx
	ld bc,c2pgfx_4x4+15
	ld de,mkc2p_old_buf
	ld lx,17
mkc2p_4x4_0
	ld hl,mkc2p_old_curbuf ;current chunk gfx (4 bytes)
	push bc
	ld bc,4
	ldir
	pop bc
	ld a,(bc)
	rlca
	rlca
	and 3 ;chunk line # to fix
	 add a,mkc2p_old_curbuf&#ff
	ld l,a
	ld a,(bc) ;chunk line data
	ld (hl),a
	rld ;copy %....RRRR to %LLLL....
	dec bc
	dec lx
	jr nz,mkc2p_4x4_0
;make c2p procedures
;(есть другой вариант расположения: jp в chunkmap'е, код в %1111RRRR LLLL0000, но chunkmap у нас в медленной памяти)
;(непосредственно писать чанки %LLLL0000, %1111RRRR неудобно, хотя есть эффекты, где это будет выгодно)
;%111RRRR0 111LLLL0 ;#de..#fe
;jr ... ;[except the last]
;ld h,a
;ld (hl),#nn/b(0)/c(#ff)/d(#55)/e(#aa)
;inc h
;ld (hl),#nn/b(0)/c(#ff)/d(#55)/e(#aa)
;inc h
;ld (hl),#nn/b(0)/c(#ff)/d(#55)/e(#aa)
;inc h
;ld (hl),#nn/b(0)/c(#ff)/d(#55)/e(#aa)
;inc l
;ret ;14b (16*14 = 224 = #e0)

;TODO make maxcolor=black

	ld ix,tpackcolor
	ld c,0 ;right chunk (left for C2PMIRROR)
mkc2p_4x4_1
	ld a,c
	add a,a
	add a,chdata_4x4/256 ;#100-(17*2)
	ld d,a
	ld e,chdata_4x4/256 ;#100-(17*2) ;start of jr's
	ld h,a
	ld l,#fe ;start of procedures
;make procedures
	ld b,9 ;number of procedures
mkc2p_4x4_3
	ld a,(ix) ;color for procedure
	inc ix
	call mkc2p_4x4_pp ;make procedure
	djnz mkc2p_4x4_3
;make jr's
	ld b,0 ;left chunk (right for C2PMIRROR)
mkc2p_4x4_2
	ld a,(ix) ;N for left chunk (right for C2PMIRROR)
	inc ix
	call mkc2p_4x4_jr ;make jr to a'th procedure
	inc b
	ld a,b
	cp 16 ;no 17th jr
	jr nz,mkc2p_4x4_2
	inc c
	ld a,c
	cp 17
	jr nz,mkc2p_4x4_1

;line 0 (to 1): ld sp:ld l,#20:ld a,#4c:ret
;line 1 (to 2): ld sp:exa:ld a,#48:ret ;l=#40 (+4)
;line 2 (to 3): ld sp:ld l,#40:exa:ret (-3)
;line 3 (to 4): ld sp:exa:ret ;l=#60 (-3)
;...
;line 14 (to 15): ld sp:ld l,#e0:exa:ret (-3)
;line 15 (to 16): ld sp:ld a,#50:ret ;l=#e0
;---
;line 16 (to 17): ld sp:ld l,#00:ld a,#54:ret
;line 17 (to 18): ld sp:exa:ld a,#50:ret (+4)
;total gain from [ld l] (64x32): 7*16 = 112 t
;total gain from exa (64x32): 2*(13*3-4) = 70 t TODO

;make chmap_4x4_nl
;every line:
;ld sp,chmap_4x4+#N00 ;N=1..
;[ld l,scr&#ff] ;for chmwid==64 odd nl only
;ld a,scr/256 ;TODO use exa (проблема, если scrtop не с начала трети)
;ret ;max 8 b
	ld h,chmap_4x4_nl/256
	if C2PMIRROR
	ld de,scrtop+(chmwid/2)-1
	else
	ld de,scrtop
	endif
	ld b,chmhgt
mkc2p_4x4_nl0
	ld l,chmap_4x4_nl&#ff
	ld a,d
	add a,4
	ld d,a
	and 7
	jr nz,mkc2p_4x4_nl_ddeq
	ld a,e
	sub -32
	ld e,a
	sbc a,a
	and -8
	add a,d
	ld d,a
mkc2p_4x4_nl_ddeq
	;de=scr_nl
	ld (hl),#31 ;ld sp
	inc hl
	ld (hl),chmap_4x4&#ff
	inc hl
	ld a,chmap_4x4/256+chmhgt+1
	sub b
	ld (hl),a ;a=chmap_4x4/256+1..
	inc hl
	if (C2PMIRROR==0)&&(chmwid==64)
	bit 2,d
	jr z,mkc2p_4x4_nl_ldlq
	endif
	ld (hl),#2e ;ld l
	inc hl
	ld (hl),e
	inc hl
mkc2p_4x4_nl_ldlq
	ld (hl),#3e ;ld a
	inc hl
	ld (hl),d
	inc hl
	ld (hl),#c9 ;ret
	inc h
	djnz mkc2p_4x4_nl0
	ret

mkc2p_4x4_jr
;make jr to a'th procedure
;+#fe=0th procedure
;+#fe+#0e=1st
;+#fe+2*#0e=2nd
;...
	;inc a
	;cp 9 ;number of procedures
	;jr nz,$+3
	; xor a ;last procedure
	;ld h,a ;N ;8->0, 0->1...
	;add a,a
	;add a,a
	;add a,a
	;sub h
	;add a,a
	;add a,#fe
	ld l,a ;N*14+#fe
	ld a,#18 ;jr
	ld (de),a
	inc de
	ld a,l
	sub e
	dec a
	ld (de),a
	inc de
	ret nz
mkc2p_4x4_jr_fast
	dec de
	dec de
	ld a,#26 ;ld h, (faster than jr)
	ld (de),a
	inc de
	inc de
	ret

mkc2p_4x4_pp
;a=left chunk*4 (right for C2PMIRROR)
;c=right chunk (left for C2PMIRROR)
	push ix
	push bc
	push de
	push hl
;right chunk addr
	;ld a,b ;left chunk (right for C2PMIRROR)
	;add a,a
	;add a,a
	ld lx,a
;left chunk addr
	ld a,c ;right chunk (left for C2PMIRROR)
	add a,a
	add a,a
	ld e,a
	ld d,mkc2p_old_buf/256
	ld hx,d;mkc2p_old_buf/256
	ld (hl),#67 ;ld h,a
	ld b,4
mkc2p_4x4_pp0
	inc hl
	ld a,(de) ;right chunk (left for C2PMIRROR)
	xor (ix) ;left chunk (right for C2PMIRROR)
	if C2PMIRROR
	and %11110000
	else
	and %00001111
	endif
	xor (ix) ;left chunk (right for C2PMIRROR)
	inc ix
	inc de
	ld (hl),#70 ;ld (hl),b
	;or a ;cp #00
	jr z,mkc2p_4x4_standard
	inc (hl) ;ld (hl),#71 ;ld (hl),c
	cp #ff
	jr z,mkc2p_4x4_standard
	inc (hl) ;ld (hl),#72 ;ld (hl),d
	cp #55
	jr z,mkc2p_4x4_standard
	inc (hl) ;ld (hl),#73 ;ld (hl),e
	cp #aa
	jr z,mkc2p_4x4_standard
	ld (hl),#36 ;ld (hl),N
	inc hl
	ld (hl),a ;N
mkc2p_4x4_standard
	inc hl
	ld (hl),#24 ;inc h
	djnz mkc2p_4x4_pp0
	if C2PMIRROR
	ld (hl),#2d ;dec l
	else
	ld (hl),#2c ;inc l
	endif
	inc hl
	ld (hl),#c9 ;ret
	pop hl
	ld  c,14
	add hl,bc
	pop de
	pop bc
	pop ix
	ret

c2pgfx_4x4
;%LL00gggg, LL=chunk line # to fix
;или дешевле положить графику всех чанков здесь?
	if C2PGFX==1 ;diamond
	db #0f
	db #8f
	db #0e
	db #8b
	db #4f
	db #cf
	db #4d
	db #c7
	db #0a
	db #8a
	db #08
	db #82
	db #45
	db #c5
	db #44
	db #c1
	endif

	if C2PGFX==2 ;noise
	db #cf
	db #0f
	db #8f
	db #4f
	db #87
	db #0b
	db #4d
	db #ce
	db #0a
	db #83
	db #cc
	db #45
	db #c8
	db #02
	db #81
	db #44
	endif

	if C2PGFX==3 ;triangle
	db #cf
	db #ce
	db #8f
	db #cc
	db #8e
	db #4f
	db #c8
	db #8c
	db #4e
	db #0f
	db #88
	db #4c
	db #0e
	db #48
	db #0c
	db #08
	endif

	if C2PGFX==4 ;dot
	db #cf
	db #c7
	db #c3
	db #c1
	db #8f
	db #4f
	db #0f
	db #0e
	db #0c
	db #08
	db #4e
	db #8e
	db #86
	db #82
	db #46
	db #44
	endif

	if C2PGFX==5 ;inverse dot
	db #4f
	db #4b
	db #8f
	db #8d
	db #89
	db #49
	db #0f
	db #07
	db #03
	db #01
	db #41
	db #81
	db #cf
	db #ce
	db #cc
	db #c8
	endif

;can hold chunkmap (64x32!) together with jr, blurred:
;(keep equal colors, adjacent colors, 0, max)
;color=%111cccc0 (can add 17th color in #de)
;82(114) bytes: chunkmap + clipping borders
;174(142) bytes: jr*10(8) + 11(9) procedures
;for example:
;even Lcolor: use Rcolors 0,2,4,6,8,10,12,14,16,L-1,L+1 (11 procedures)
;odd Lcolor: use Rcolors 0,3,5,7,9,11,13,16,L-1,L+1, 1(dark Lcolor) or 15(light Lcolor) (11 procedures)
;or use main Rcolors: 0,(2),4,(6),8,(10),12,(14),16
tpackcolor
;for 11 procedures:
;colors  0123456789ABCDEFG
;main:   ^   ^   ^   ^   ^
	;01234 5 6 7 8 9 A (Lcolor=0)
	;01234 5 6 7 8 9 A (Lcolor=1)
	;01234 5 6 7 8 9 A (Lcolor=2)
	;0 12345 6 7 8 9 A (Lcolor=3)
	;0 12345 6 7 8 9 A (Lcolor=4)
	;0 1 23456 7 8 9 A (Lcolor=5)
	;0 1 23456 7 8 9 A (Lcolor=6)
	;0 1 2 34567 8 9 A (Lcolor=7)
	;0 1 2 34567 8 9 A (Lcolor=8)
	;0 1 2 34567 8 9 A (Lcolor=9)
	;0 1 2 3 45678 9 A (Lcolor=A)
	;0 1 2 3 45678 9 A (Lcolor=B)
	;0 1 2 3 4 56789 A (Lcolor=C)
	;0 1 2 3 4 56789 A (Lcolor=D)
	;0 1 2 3 4 5 6789A (Lcolor=E)
	;0 1 2 3 4 5 6789A (Lcolor=F)
	;0 1 2 3 4 5 6789A (Lcolor=G)
;for 10 procedures:
;colors  0123456789ABCDEFG
;main:   ^   ^   ^   ^   ^
	;012 3 4 5 6 7 8 9 (Lcolor=0)
	;012 3 4 5 6 7 8 9 (Lcolor=1)
	;0123 4 5 6 7 8  9 (Lcolor=2)
	;0 123 4 5 6 7 8 9 (Lcolor=3)
	;0 1234 5 6 7 8  9 (Lcolor=4)
	;0 1 234 5 6 7 8 9 (Lcolor=5)
	;0 1 2345 6 7 8  9 (Lcolor=6)
	;0 1 2 345 6 7 8 9 (Lcolor=7)
	;0 1 2 3456  7 8 9 (Lcolor=8)
	;0 1 2 3 456 7 8 9 (Lcolor=9)
	;0  1 2 3 4567 8 9 (Lcolor=A)
	;0 1 2 3 4 567 8 9 (Lcolor=B)
	;0  1 2 3 4 5678 9 (Lcolor=C)
	;0 1 2 3 4 5 678 9 (Lcolor=D)
	;0  1 2 3 4 5 6789 (Lcolor=E)
	;0 1 2 3 4 5 6 789 (Lcolor=F)
	;0 1 2 3 4 5 6 789 (Lcolor=G)

	macro dbr_ br
	db br*4
	endm

	macro dbr br0,br1,br2,br3,br4,br5,br6,br7,br8
	dbr_ br8
	dbr_ br0
	dbr_ br1
	dbr_ br2
	dbr_ br3
	dbr_ br4
	dbr_ br5
	dbr_ br6
	dbr_ br7
	endm

	macro dpr_ pr
	if pr==8
	db (14*0+#fe)&#ff
	else
	db (14*(pr+1)+#fe)&#ff
	endif
	endm

	macro dpr pr0,pr1,pr2,pr3,pr4,pr5,pr6,pr7,pr8,pr9,pr10,pr11,pr12,pr13,pr14,pr15
	dpr_ pr0
	dpr_ pr1
	dpr_ pr2
	dpr_ pr3
	dpr_ pr4
	dpr_ pr5
	dpr_ pr6
	dpr_ pr7
	dpr_ pr8
	dpr_ pr9
	dpr_ pr10
	dpr_ pr11
	dpr_ pr12
	dpr_ pr13
	dpr_ pr14
	dpr_ pr15
	endm
;for 9 procedures:
;colors  0123456789ABCDEFG
;main:   ^   ^   ^   ^   ^
	;012<3<>4<5<6<>7>8 (Lcolor=0)
	dbr 0,1,2,4,7,9,#b,#e,#10
	dpr 0,1,2,2,3,3,4,4,4,5,5,6,6,7,7,8;8
	;012<3<4<5<6<>7<>8 (Lcolor=1)
	dbr 0,1,2,4,6,8,#a,#d,#10
	dpr 0,1,2,2,3,3,4,4,5,5,6,6,7,7,7,8;8
	;0123<>4<>5<>6>7>8 (Lcolor=2)
	dbr 0,1,2,3,6,9,#c,#e,#10
	dpr 0,1,2,3,3,4,4,4,5,5,5,6,6,7,7,8;8
	;0<123<4<5<6<>7<>8 (Lcolor=3)
	dbr 0,2,3,4,6,8,#a,#d,#10
	dpr 0,0,1,2,3,3,4,4,5,5,6,6,7,7,7,8;8
	;0<1234<>5<>6<>7>8 (Lcolor=4)
	dbr 0,2,3,4,5,8,#b,#e,#10
	dpr 0,0,1,2,3,4,4,5,5,5,6,6,6,7,7,8;8
	;0<1>234<5<6<>7<>8 (Lcolor=5)
	dbr 0,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=G)
	dbr 0,2,5,7,9,#c,#e,#f,#10
	dpr 0,0,1,1,2,2,3,3,4,4,4,5,5,6,6,7;8
;or
	;01234<>5<>6<>7<>8 (Lcolor=0)
	;01234<>5<>6<>7<>8 (Lcolor=1)
	;01234  5  6  7  8 (Lcolor=2)
	;0 1234 5  6  7  8 (Lcolor=3)
	;0 1234 5  6  7  8 (Lcolor=4)
	;0  12345  6  7  8 (Lcolor=5)
	;0  1 2345 6  7  8 (Lcolor=6)
	;0  1 2345 6  7  8 (Lcolor=7)
	;0  1  23456  7  8 (Lcolor=8)
	;0  1  2 3456 7  8 (Lcolor=9)
	;0  1  2 3456 7  8 (Lcolor=A)
	;0  1  2  34567  8 (Lcolor=B)
	;0  1  2  3 4567 8 (Lcolor=C)
	;0  1  2  3 4567 8 (Lcolor=D)
	;0  1  2  3  45678 (Lcolor=E)
	;0  1  2  3  45678 (Lcolor=F)
	;0  1  2  3  45678 (Lcolor=G)
;TODO прописать сценарий записи процедур и джампов, а генератор упростить


;can't hold chunkmap together with jr:
;#00..#01 addr
;#02..#41 chunkmap
;#42..#bf c2p (9 procedures)
;#c0..#dd jr
;#de..#f9 c2p (2 procedures)
;#fe..#ff INT stack
;#00..#01 addr
;#02..#41 chunkmap
;#42..#87 c2p (5 procedures)
;#fe..#ff INT stack
 ;jr #b = #50-#d8 = #78
 ;jr #c = #5e-#da = #84 IMPOSSIBLE
 ;jr #d = #6c-#dc = #90 IMPOSSIBLE
 ;jr #e = #7a-#de = #9c IMPOSSIBLE
 ;jr #f = no jr

;can hold chunkmap (64x48!) together with jp:
;color=#d1/#d4/#d7/#da/ #dd/#e0/#e3/#e6/ #e9/#ec/#ef/#f2/ #f5/#f8/#fb/#fe
;[can add 17th color in #ce]
;+#0aa: 7 procedures
;+#1aa: 2 procedures + jp's(except the last) + 1 procedure
;+#2aa: 6 procedures
;#0c..#a9 chunkmap + clipping borders (158 bytes)
;#aa..#10b procedures (max 7)

;can hold chunkmap (64x37) together with jp/jr:
;color=#db/#dd/#df/#e1/ #e3/#e5/#e7/#e9/ #eb/#ee/#f1/#f4/#f7/ #fa/#fc/#fe
;+#00b..#06a: chunkmap + clipping borders (96 bytes)
;+#06b..#10b: 8 procedures + jr*8: jp*5: jr:jr + 1 procedure
;+#10b..#16a: chunkmap + clipping borders (96 bytes)
;+#16f..#1ff: 7 procedures (jr #16f-#0fc=#73, jr #17d-#0fe=#7f)

;can't hold chunkmap (64x36) together with jp/jr:
;+#08a..#0bf: chunkmap (54 bytes!!!)
;+#0c0..#189: [4+]2 procedures + jr*2: jp*4: jr*9 + 10 procedures

;can hold chunkmap (64x32!) together with jr+jp, no clipping!:
;color=%111cccc0
;+#08a..#0cd: chunkmap (68 bytes)
;+#0ce..#189: jp*6<-jr*6: jr*9 + 10 procedures
