You are on page 1of 1

1 #include "pic.

h"
2 #include "libc/pio.h"
3
4 /**
5 * https://wiki.osdev.org/PIC#Protected_Mode
6 * In protected mode, the IRQs 0 to 7 conflict with the CPU exception which are
reserved by Intel up until 0x1F.
7 * Consequently it is difficult to tell the difference between an IRQ or an
software error.
8 * It is thus recommended to change the PIC's offsets, also known as remapping
the PIC, IRQ table
9 * so that IRQs use non-reserved vectors.
10 */
11 void pci_init()
12 {
13 // save masks
14 byte b1, b2;
15 b1 = in_byte(PIC_MASTER_DATA_PORT);
16 b2 = in_byte(PIC_SLAVE_DATA_PORT);
17
18 // When you enter protected mode the first command you will need to give the two
PICs is the initialise command (code 0x11)
19 out_byte(PIC_MASTER_COMMAND_PORT, PIC_INITIALIZATION_COMMAND);
20 io_wait();
21 out_byte(PIC_SLAVE_COMMAND_PORT, PIC_INITIALIZATION_COMMAND);
22 io_wait();
23 /**
24 * https://wiki.osdev.org/PIC#Initialisation
25 * This command makes the PIC wait for 3 extra "initialisation words" on the data
port.
26 * These bytes give the PIC:
27 * 1. Its vector offset. (ICW2)
28 * 2. Tell it how it is wired to master/slaves. (ICW3)
29 * 3. Gives additional information about the environment. (ICW4)
30 */
31 // ICW2: set the master PIC's offset
32 out_byte(PIC_MASTER_DATA_PORT, PIC_MASTER_OFFSET);
33 io_wait();
34 // ICW2: set the slave PIC's offset
35 out_byte(PIC_SLAVE_DATA_PORT, PIC_SLAVE_OFFSET);
36 io_wait();
37 // ICW3: tell Master PIC that there is a slave PIC at IRQ2 (0000 0100)
38 out_byte(PIC_MASTER_DATA_PORT, 0x04);
39 io_wait();
40 // ICW3: tell Slave PIC its cascade identity (0000 0010)
41 out_byte(PIC_SLAVE_DATA_PORT, 0x02);
42 io_wait();
43 // ICW4: Gives additional information about the environment
44 out_byte(PIC_MASTER_DATA_PORT, PIC_8086_MODE);
45 io_wait();
46 out_byte(PIC_SLAVE_DATA_PORT, PIC_8086_MODE);
47 io_wait();
48 // restore masks
49 out_byte(PIC_MASTER_DATA_PORT, b1);
50 out_byte(PIC_SLAVE_DATA_PORT, b2);
51 }

You might also like