// MT MK3 OM v0.6 [Cheman]
// Copyleft 2017, 2019 by The Mojon Twins

// Simple arkos interface (CPC)
#define ARKOS_SFX_CHANNEL 1	

void init_arkos (void) {
// ARKOS INIT

	#asm
		di
	#endasm

	// Init arkos (turn it off, in fact!)

	#asm
		; Turn off arkos play routine
		ld a, 201				; Insert a RET!
		ld (PLY_PLAY), a
	#endasm

	// Init interrupts

	#asm
		ld a, 195
		ld (0x38), a

		ld HL,_interrupcion
		ld (0x0039),HL
		
		jp term
	._int_original  
		defw 0
	._interrupcion
		push af
		push bc
		push hl
		push de
		push ix
		push iy
		exx
		ex af, af
		push af
		push bc
		push de
		push hl
		
		ld a, (_isrc)
		inc a
		ld (_isrc), a

		ld a, (_frames_counter)
		add a, 1
		ld (_frames_counter), a

	.___reduce
		cp  6
		jr  c, ___noreduce
		sub 6
		jr  ___reduce
	.___noreduce		

		;and 0x1f
		;or  0x40
		;ld  bc, 0x7F10
		;out (c), c
		;out (c), a


		ld a, (_arkc)
		inc a

		cp 6
		jp nz, _noplayer

		call PLY_PLAY

		xor a
	._noplayer
		ld (_arkc), a

		pop hl 
		pop de 
		pop bc
		pop af 
		ex af, af
		exx
		pop iy
		pop ix
		pop de 
		pop hl 
		pop bc 
		pop af
		ei

		ret
	.term
	#endasm
	//halt_me ();



	#ifndef TAPE
		// Decompress ARKOS player and SFX
		// The binary ARKOS+SFX is loaded @ BASE_MUSIC
		// Which is the buffer where we descompress music later on

		unpack ((unsigned char *) (BASE_MUSIC+SIZE_ARKOS_COMPRESSED), (unsigned char *) (BASE_SFX));
		unpack ((unsigned char *) (BASE_MUSIC), (unsigned char *) (BASE_ARKOS));		
	#endif
	
	
}


void music_play (unsigned char n) {
	// Unpack song to buffer.
	unpack ((unsigned char*)(musics[n]), (unsigned char*)(BASE_MUSIC));

	// Call PLY_INIT
	#asm
		ld de, BASE_MUSIC
		call PLY_INIT
	#endasm

	// Sound bank.
	#asm
		ld de, BASE_SFX
		call PLY_SFX_INIT
	#endasm

	// Turn on arkos play routine
	#asm
		ld a, 175
		ld (PLY_PLAY), a
	#endasm

}

void sfx_play (unsigned char n) {
	#asm
		ld a, (_nota_fx)
		ld e, a
		ld a, (_canal_fx)
		ld h, 15
		ld d, 0
		ld bc, 0
		call PLY_SFX_PLAY
	#endasm
}

void music_stop (void) {
	#asm
		call PLY_STOP

		; Turn off arkos play routine
		ld a, 201
		ld (PLY_PLAY), a
	#endasm
}

// void music_fade_out (void) {
	// #asm
		// ;	ld e,Volume (0=full volume, 16 or more=no volume)
		// ;	call PLY_SetFadeValue
		// ld de, 16
		// call PLY_SetFadeValue
	// #endasm
// }
