// SPDX-License-Identifier: MPL-2.0
#include "cpu/pic.h"
#include "cpu/asm.h"
void sh_pic_remap() {
  sh_asm_outb(SH_PIC1_CMD,0x11);
  sh_asm_outb(SH_PIC2_CMD,0x11);
  sh_asm_outb(SH_PIC1_DATA,0x20);
  sh_asm_outb(SH_PIC2_DATA,0x28);
  sh_asm_outb(SH_PIC1_DATA,0x04);
  sh_asm_outb(SH_PIC2_DATA,0x02);
  sh_asm_outb(SH_PIC1_DATA,0x01);
  sh_asm_outb(SH_PIC2_DATA,0x01);
  sh_asm_outb(SH_PIC1_DATA,0xFE);
  sh_asm_outb(SH_PIC2_DATA,0xFF);
}
void sh_pic_unmask(sh_uint8 irq) {
  sh_uint16 port;
  sh_uint8 value;
  if (irq<8) {
    port=SH_PIC1_DATA;
  } else {
    port=SH_PIC2_DATA;
    irq-=8;
  }
  value=sh_asm_inb(port);
  value&= (sh_uint8)~((sh_uint8)(1u<<irq));
  sh_asm_outb(port,value);
}
void sh_pic_mask(sh_uint8 irq) {
  sh_uint16 port;
  sh_uint8 value;
  if (irq<8) {
    port=SH_PIC1_DATA;
  } else {
    port=SH_PIC2_DATA;
    irq-=8;
  }
  value=sh_asm_inb(port);
  value|=(sh_uint8)(1u<<irq);
  sh_asm_outb(port,value);
}
void sh_pic_send_eoi(sh_uint8 irq) {
  if (irq>=8) sh_asm_outb(SH_PIC2_CMD,0x20);
  sh_asm_outb(SH_PIC1_CMD,0x20);
}
