Files
novaos/kernel/arch/pic/pic.c
2025-06-13 16:09:47 -04:00

172 lines
4.0 KiB
C

#ifdef __TESTING__
#include <kernel/_kernel.h>
#include <stdio.h>
#endif
#include <kernel/x86/io.h>
#include <kernel/x86/pic.h>
/** PIC I/O ports **/
#define PIC1 0x20 /** Master PIC **/
#define PIC2 0xA0 /** Slave PIC **/
/** PIC helper defines **/
#define PIC1_COMMAND (PIC1)
#define PIC1_DATA (PIC1 + 1)
#define PIC2_COMMAND (PIC2)
#define PIC2_DATA (PIC2 + 1)
/** PIC Commands **/
#define ICW1_ICW4 0x01 /** Indicates ICW4 will be present **/
#define ICW1_SINGLE 0x02 /** Single (cascade mode) **/
#define ICW1_INTERVAL4 0x04 /** Call address interval 4 (8) **/
#define ICW1_LEVEL 0x08 /** Level triggered (edge) mode **/
#define ICW1_INIT 0x10 /** Initialization **/
#define ICW4_8086 0x01 /** 8086/88 (MCS-80/85) mode **/
#define ICW4_AUTO 0x02 /** Auto (normal) EOI **/
#define ICW4_BUF_SLAVE 0x08 /** Buffered mode/slave **/
#define ICW4_BUF_MASTER 0x0C /** Buffered mode/master **/
#define ICW4_SFNM 0x10 /** Special fully nested (not) **/
#define PIC_EOI 0x20 /** End-of-interrupt command code **/
#define PIC_READ_IRR 0x0a /** OCW3 irq ready next CMD read **/
#define PIC_READ_ISR 0x0b /** OCW3 irq service next CMD read **/
void PIC_sendEOI(uint8_t irq)
{
if (irq >= 8) // if we're over the PIC1 limit
outb(PIC2_COMMAND, PIC_EOI);
outb(PIC1_COMMAND, PIC_EOI); // if the IRQ came from the slave, it must go to both PICs
}
void PIC_remap(int offset1, int offset2)
{
#ifdef __TESTING__
kinfo("Remapping the PIC...");
#endif
// The io_wait calls are necessary for older machines, to give the PIC time to react
//
// After the init, the PIC requires 3 init words
// ICW2 // its vector offset
// ICW3 // how its wired to the master/slave
// ICW4 // additional info about the environment
outb(PIC1_COMMAND, ICW1_INIT | ICW1_ICW4); // starts the init sequence, in cascade mode
io_wait();
outb(PIC2_COMMAND, ICW1_INIT | ICW1_ICW4);
io_wait();
outb(PIC1_DATA, offset1); // ICW2 - the offset for master
io_wait();
outb(PIC2_DATA, offset2); // same as above for slave
io_wait();
outb(PIC1_DATA, 4); // ICW3 - Tells master theres a slave at IRQ2
io_wait();
outb(PIC2_DATA, 2); // ICW3 - Tells slave the cascade identity
io_wait();
outb(PIC1_DATA, ICW4_8086); // ICW4 - Use 8086 mode (not 8080 mode)
io_wait();
outb(PIC2_DATA, ICW4_8086);
io_wait();
// Unmask the PICs
outb(PIC1_DATA, 0);
outb(PIC2_DATA, 0);
#ifdef __TESTING__
kinfo("Remapped the PIC!");
#endif
}
void pic_disable(void)
{ // Mask the PIC interrupts to disable them
outb(PIC1_DATA, 0xFF);
outb(PIC2_DATA, 0xFF);
#ifdef __TESTING__
kinfo("Masked off the PIC");
#endif
}
void IRQ_set_mask(uint8_t IRQline) // Masked IRQlines are ignored by the PIC, masked IRQ2 will fully ignore the slave
{
uint16_t port;
uint8_t value;
if (IRQline < 8) {
port = PIC1_DATA;
} else {
port = PIC2_DATA;
IRQline -= 8;
}
value = inb(port) | (1 << IRQline);
outb(port, value);
#ifdef __TESTING__
kinfo("Masked IRQ line");
printf("IRQ line: %d\n", IRQline);
#endif
}
void IRQ_clear_mask(uint8_t IRQline)
{
uint16_t port;
uint8_t value;
if (IRQline < 8) {
port = PIC1_DATA;
} else {
port = PIC2_DATA;
IRQline -= 8;
}
value = inb(port) & ~(1 << IRQline);
outb(port, value);
#ifdef __TESTING__
kinfo("Cleared mask from IRQ line");
printf("IRQ line: %d\n", IRQline);
#endif
}
static uint16_t __pic_get_irq_reg(int ocw3)
{
/** OCW3 to PIC CMD to get the register values
* PIC2 is chained, and represents IRQs 8-1.
* PIC1 is IRQs 0-7, with 2 being the chain **/
outb(PIC1_COMMAND, ocw3);
outb(PIC2_COMMAND, ocw3);
return (inb(PIC2_COMMAND) << 8) | inb(PIC1_COMMAND);
}
uint16_t pic_get_irr(void)
{
return __pic_get_irq_reg(PIC_READ_IRR);
}
uint16_t pic_get_isr(void)
{
return __pic_get_irq_reg(PIC_READ_ISR);
}
#undef PIC1
#undef PIC2
#undef PIC1_COMMAND
#undef PIC1_DATA
#undef PIC2_COMMAND
#undef PIC2_DATA
#undef ICW1_ICW4
#undef ICW1_SINGLE
#undef ICW1_INTERVAL4
#undef ICW1_LEVEL
#undef ICW1_INIT
#undef ICW4_8086
#undef ICW4_AUTO
#undef ICW4_BUF_SLAVE
#undef ICW4_BUF_MASTER
#undef ICW4_SFNM
#undef PIC_EOI
#undef PIC_READ_IRR
#undef PIC_READ_ISR