;; MIT License
;; 
;; Copyright (c) 2021 capAI / Aitana Devis  Ávila (@AitanaDevis) / Carlos Peñarrubia Morcillo  (@carlospm12) / Pablo Ruiz Ponce (@pabloruizp)
;; 
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;; 
;; The above copyright notice and this permission notice shall be included in all
;; copies or substantial portions of the Software.
;; 
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.

.include "cpctelera.h.s"
.include "interrupt.h.s"
.include "entity.h.s"

.globl cpct_akp_musicPlay_asm
.globl _player


interruption_counter:
   .db 6

;;///////////////////////////////////////////////////////////////////////
;; INTERRUPTION MANAGER, IN CHARGE OF PLAYING OR STOPPING THE MUSIC DURING THE GAME
;;
;; REGISTROS MODIFICADOS: AF, AF’, BC, DE, HL, IX, IY
;;///////////////////////////////////////////////////////////////////////
man_interrupt::
   cpctm_push af, bc, de, hl, ix, iy ;;saves all registers we are going to destroy later
   
   ld a, (interruption_counter)
   dec a
   jr nz, _dont_execute
   ld ix, #_player
   ld a, P_PREV(ix)
   cp #1 
   jr z, _stop ;;si está muerto paramos de reproducir
   
   ;;si está vivo, reproducimos
   call cpct_akp_musicPlay_asm ;;DESTROYS: AF, AF’, BC, DE, HL, IX, IY


   ld a, #6
_dont_execute:
   ld (interruption_counter), a
   cpctm_pop iy, ix, hl, de, bc, af
   ret

_stop:
   call cpct_akp_stop_asm
   cpctm_pop iy, ix, hl, de, bc, af
   ret
