layer_base			EQU #4D00
mask_table			EQU #4F00
layer_buffer		EQU #5000
mix_layer_sample	EQU #5100	
comp_layers_table	EQU #5200
comp_layers			EQU #6000
	
;special SPR find in bank #C5
.TSPGADDL5
	push BC
	ld C,#C5
	jp USPGADD1	
	
;setup array of chars for scroll	
.lvl_getBGsprites
	ld A,1	;start on sprite 0
	ld B,5	;5 layers
	ld DE,layer_base	;start layers table @7D00
	
.lvl_getBGspritesLoop
	push AF
	push BC
	
	push DE
	call TSPGADDL5
	pop DE
	
	inc HL	;skip global flag
	
	ld B,32	;we have to get 32 chars
	
.lvl_getBGsprites_dump_pattern_loop	
	inc HL		;skip tile flags
	ld A,(HL)
	ld (DE),A	;low addr
	inc HL
	inc DE
	ld A,(HL)
	ld (DE),A	;high addr
	inc HL
	inc DE
	djnz lvl_getBGsprites_dump_pattern_loop	
	
	pop BC
	pop AF
	
	inc A	;next sprite
	djnz lvl_getBGspritesLoop
	
	ret
	
;do mix layers

.Mix_Layers
;get base address
	ld HL,layer_base+128
	ld BC,(pat_offset_addr)
	add HL,BC			;select current pixel offset
	
	ld A,L
	;and %11110000
	ld (MixLayerCorrectIX),A
	
	ld A,(pat_start)
	add A
	ld C,A
	ld B,0
	add HL,BC			;compute char offset
	
	push HL
	pop IX				;save in IX
	
;same thing for second layer (behind)
	ld HL,layer_base+192
	ld BC,(pat_offset_addr2)
	add HL,BC			;select current pixel offset
	
	ld A,L
	;and %11110000
	ld (MixLayerCorrectIY),A
	
	ld A,(pat_start2)
	add A
	ld C,A
	ld B,0
	add HL,BC			;compute char offset
	
	push HL
	pop IY
	
	ld B,8				;8 chars to process
	ld DE,layer_buffer	;layer buffer address

	
.Mix_Layers_loop
	ld L,(IX)
	inc IXL
	ld H,(IX)
	inc IXL
	ld A,IXL
	and %00001111
MixLayerCorrectIX equ $+1
	or 0
	ld IXL,A
exx	
	ld L,(IY)
	inc IYL
	ld H,(IY)
exx	
	inc IYL
	ld A,IYL
	and %00001111
MixLayerCorrectIY	equ $+1
	or 0
	ld IYL,A

	
	push BC
	push DE
	
	ld BC,mask_table
	
	call mix_layer_sample
	
	pop DE
	ld A,16
	add E
	ld E,A
	pop BC
	djnz Mix_Layers_loop
	
	ret
	
;speed layer 1	
.pat_offset_addr
	dw 0
.pat_offset
	db 0
.pat_start
	db 0
	
;speed layer 2
.pat_offset_addr2
	dw 0
.pat_offset2
	db 0
.pat_start2
	db 0	



	

	

	