LumaOS  v0.1.116
An experimental x86-64 OS
Loading...
Searching...
No Matches
pic.c
Go to the documentation of this file.
1
9
10#include <stdint.h>
11#include <arch/x86_64/pic.h>
12
13#include <arch/x86_64/io.h>
14
15#include "kernel/log.h"
16
17static uint8_t master_mask, slave_mask;
18
19void pic_remap(uint8_t master_offset, uint8_t slave_offset)
20{
21 // Initialization Sequence
22 outb(PIC_MASTER_COMMAND_PORT, 0x11); // Master: Start Init
23 io_wait();
24 outb(PIC_SLAVE_COMMAND_PORT, 0x11); // Slave: Start Init
25 io_wait();
26
27 outb(PIC_MASTER_DATA_PORT, master_offset); // Master: Vector Offset
28 io_wait();
29 outb(PIC_SLAVE_DATA_PORT, slave_offset); // Slave: Vector Offset
30 io_wait();
31
32 outb(PIC_MASTER_DATA_PORT, 0x04); // Master: Slave Location
33 io_wait();
34 outb(PIC_SLAVE_DATA_PORT, 0x02); // Slave: Cascade Identity
35 io_wait();
36
37 outb(PIC_MASTER_DATA_PORT, 0x01); // Master: 8086 Mode
38 io_wait();
39 outb(PIC_SLAVE_DATA_PORT, 0x01); // Slave: 8086 Mode
40 io_wait();
41
42 // Set Masks
43 outb(PIC_MASTER_DATA_PORT, 0xFF); // Master: Mask Everything
44 io_wait();
45 outb(PIC_SLAVE_DATA_PORT, 0xFF); // Slave: Mask Everything
46 io_wait();
47
48 master_mask = 0xFF;
49 slave_mask = 0xFF;
50
51
52}
53
54void pic_set_mask(uint8_t irq)
55{
56 uint8_t bit = irq % 8;
57
58 uint8_t mask;
59 if (irq >= 8) mask = slave_mask;
60 else mask = master_mask;
61
62 mask = mask | (1 << bit); // Calculate the mask (XOR the correct irq bit)
63
64 if (irq >= 8)
65 {
66 outb(PIC_SLAVE_DATA_PORT, mask);
67 slave_mask = mask;
68 }
69 else
70 {
71 outb(PIC_MASTER_DATA_PORT, mask);
72 master_mask = mask;
73 }
74
75 LOG_DEBUG("master: %2x slave: %2x\n", master_mask, slave_mask);
76
77}
78
79void pic_clear_mask(uint8_t irq)
80{
81 uint8_t bit = irq % 8;
82
83 uint8_t mask;
84 if (irq >= 8) mask = slave_mask;
85 else mask = master_mask;
86
87 mask = mask & ~(1 << bit); // Calculate the mask
88
89 if (irq >= 8)
90 {
91 outb(PIC_SLAVE_DATA_PORT, mask);
92 slave_mask = mask;
93 }
94 else
95 {
96 outb(PIC_MASTER_DATA_PORT, mask);
97 master_mask = mask;
98 }
99
100 LOG_DEBUG("master: %2x slave: %2x\n", master_mask, slave_mask);
101}
102
103void pic_send_eoi(uint8_t irq)
104{
105 if (irq >= 8) outb(PIC_SLAVE_COMMAND_PORT, 0x20);
106 outb(PIC_MASTER_COMMAND_PORT, 0x20);
107}
Provides utilities for IO operations with the hardware.
void outb(uint16_t port, uint8_t value)
Outputs a byte to the specified port.
Definition io.h:22
void io_wait(void)
Write 0 to unused port 0x80 to perform a short pause.
Definition io.h:83
Logging utilities for the kernel.
void pic_send_eoi(uint8_t irq)
Sends an EOI to tell the PIC that the interrupt is over.
Definition pic.c:103
void pic_remap(uint8_t master_offset, uint8_t slave_offset)
Initializes the PIC using ICWs and mask all lines.
Definition pic.c:19
void pic_set_mask(uint8_t irq)
Masks (Disables) a line.
Definition pic.c:54
void pic_clear_mask(uint8_t irq)
Unmasks (Enables) a line.
Definition pic.c:79
Handles the legacy PIC for IRQ interrupts.