.include "cpctelera_functions.h.s"
.include "cpctelera.h.s"
.include "interrupciones.h.s"
.include "game_manager.h.s"
.include "sys/render_system.h.s"
.include "entity_manager.h.s"

;;;;;;;;;INTERRUPCIONES ;;;;;;;;;;
.macro setNextManIR direccion
      ld        hl, #direccion
      ld        (0x39), hl
.endm

.macro setNumbIR number
      ld        a, #number
      ld        (num_i), a
.endm

num_i:: .db 6

man_ir1::
      push af
      push bc
      push hl
      
      setNumbIR 1
      setNextManIR man_ir2
      cpctm_setBorder_asm HW_BLACK
      pop hl
      pop bc
      pop af
      ei
      reti

man_ir2::
      push af
      push bc
      push hl
      
      setNumbIR 2
      setNextManIR man_ir3
      cpctm_setBorder_asm HW_GREEN
      pop hl
      pop bc
      pop af
      ei
      reti

man_ir3::
      push af
      push bc
      push hl
      
      setNumbIR 3
      setNextManIR man_ir4
      cpctm_setBorder_asm HW_RED
      pop hl
      pop bc
      pop af
      ei
      reti

man_ir4::
      push af
      push bc
      push hl
      
      setNumbIR 4
      setNextManIR man_ir5
      cpctm_setBorder_asm HW_PINK
      pop hl
      pop bc
      pop af
      ei
      reti

man_ir5::
      push af
      push bc
      push hl
      
      setNumbIR 5
      setNextManIR man_ir6
      cpctm_setBorder_asm HW_YELLOW
      pop hl
      pop bc
      pop af
      ei
      reti

man_ir6::
      push af
      push bc
      push hl
      setNumbIR 6
      setNextManIR man_ir1
      cpctm_setBorder_asm HW_ORANGE
      pop hl
      pop bc
      pop af
      ei
      reti


set_man_ir::
      im       1
      call     cpct_waitVSYNC_asm
      halt
      halt
      call     cpct_waitVSYNC_asm
      di

      ;; Poner JP en 0x38
      ld       a, #0xC3     ;; JP = 0xC3
      ld       (0x38), a
      ;; Direccion de salto
      ld       hl, #man_ir1
      ld       (0x39), hl
      ei
      ret

;; SEGUNDA FORMA DE INTERRUPCIONES

contadorInterrupciones: .db 06
int_handler::
      push     af
      push     bc
      push     de
      push     hl
      

      ld       a, (contadorInterrupciones)
      dec      a
      jr       nz, _cont
_zero:
      ;;call     cpct_akp_musicPlay_asm

      call    cpct_scanKeyboard_if_asm

      ld       a, #6
_cont:

      ld       (contadorInterrupciones), a

      ld       h, a
      ld       l, #16
      call     cpct_setPALColour_asm

      pop      hl
      pop      de
      pop      bc
      pop      af
      
      ei
      reti

set_int_handler:
      call     cpct_waitVSYNC_asm
      halt
      halt
      call     cpct_waitVSYNC_asm

      ld       hl, #0x38
      ld       (hl), #0xC3
      inc      hl 
      ld       (hl), #<int_handler
      inc      hl 
      ld       (hl), #>int_handler
      inc      hl
      ld       (hl), #0xC9

      ret
      