summaryrefslogtreecommitdiffstats
path: root/arch/x86/ic/Pic.cpp
blob: 77d9041a34ed90e9f7a5da4d6ef795126751ba73 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "Pic.h"


bool SingletonPic::isEntityAfter = false;

ICW1::ICW1(const uint8 icw)
{
    word = byteHigh | (icw & 0x0f);
};

ICW1::ICW1()
{
    word = wordDefault;
};

bool ICW1::isExpectIcw4()
{
    return word & IC4;
};

const uint8 ICW1::toByteMasterPic() const
{
    return word;
}

const uint8 ICW1::toByteSlavePic() const
{
    return word;
}

const uint8 ICW1::getNumber() const
{
    return 1;
};

ICW2::ICW2()
{
    lvtAddr = mngIdt.getIrqInterruptStart();
};

ICW2::ICW2(const uint8 icw)
{
    if(icw >= 0xff - Pic::numberIrq) lvtAddr = MngIdt::firstSystemIntNumber;
    else lvtAddr = icw;
};

const uint8 ICW2::getNumber() const
{
    return 2;
};

const uint8 ICW2::toByteMasterPic() const
{
    if(lvtAddr >= 0xff - Pic::numberIrq) return MngIdt::firstSystemIntNumber;
    else return lvtAddr;
};

const uint8 ICW2::toByteSlavePic() const
{
    if(lvtAddr >= 0xff - 8) return MngIdt::firstSystemIntNumber + 8;
    else return lvtAddr + 8;
};

ICW3::ICW3()
{
    irqConnPrimaryPic = irqTwoLine;
    irqConnSecondaryPic = irqTwoLineBinary;
};

ICW3::ICW3(const uint8 icwMaster)
{
    uint8 oneAmount = 0;
    uint8 icwMasterCpy = icwMaster;

    while(icwMasterCpy)
    {
        oneAmount += icwMasterCpy & 0x01;
        icwMasterCpy >>= 1;
    }

    if(oneAmount > 1)
    {
        *this = ICW3();
    }
    else
    {
        irqConnPrimaryPic = icwMaster;

        uint8 icwMasterBinary = 0;
        icwMasterCpy = icwMaster;

        while(icwMasterCpy)
        {
            icwMasterBinary++;
            icwMasterCpy >>= 1;
        }
        irqConnSecondaryPic = icwMasterBinary;
    }
};


const uint8 ICW3::toByteMasterPic() const
{
    return irqConnPrimaryPic;
};

const uint8 ICW3::toByteSlavePic() const
{
    return irqConnSecondaryPic;
};

const uint8 ICW3::getNumber() const
{
    return 3;
};

ICW4::ICW4()
{
    word = defaultWord;
};

ICW4::ICW4(uint8 icw)
{
    word = icw & 0x1f;
};

const uint8 ICW4::toByteMasterPic() const
{
    return word;
};

const uint8 ICW4::toByteSlavePic() const
{
    return word;
};

const uint8 ICW4::getNumber() const
{
    return 4;
};

bool Pic::isIcwsCorrect(const ICW *icws[numberWords])
{
    for(uint8 idx = 0; idx < numberWords; idx++)
    {
        const ICW *currIcw = icws[idx];
        if(currIcw->getNumber() != idx + 1)
        {
            return false;
        }
    }

    return true;
};

void Pic::disableIrq(uint8 irqNumber)
{
    bool isMasterPic = irqNumber < 8;
    uint8 irqBits = 0x1 << (isMasterPic ? irqNumber : irqNumber - 8);
    uint8 imr = PortOps::inb(isMasterPic ? PICM_DATA : PICS_DATA);

    uint8 port = isMasterPic ? PICM_DATA : PICS_DATA;
    PortOps::outb(port, imr | irqBits);
};

bool Pic::enableIrq(uint8 irqNumber)
{
    bool isMasterPic = irqNumber < 8;
    uint8 irqBits = ~(0x1 << (isMasterPic ? irqNumber : irqNumber - 8));
    uint8 imr = PortOps::inb(isMasterPic ? PICM_DATA : PICS_DATA);

    if(!mngIdt.isInterruptSet(mngIdt.getIrqInterruptStart() + irqNumber))
    {
        // ISR not set in IDT
        return true;
    }

    uint8 port = isMasterPic ? PICM_DATA : PICS_DATA;
    PortOps::outb(port, imr & irqBits);

    return false;
};

void Pic::disable()
{
    // Stop recive interrupts
    PortOps::outb(PICM_DATA, 0xff);
    PortOps::outb(PICS_DATA, 0xff);
};

Pic::Pic(const ICW *icws[numberWords])
{
    if(!isIcwsCorrect(icws))
    {
        *this = Pic();
        return;
    }

    // Set ICW1 to command registers
    PortOps::outb(PICM_CTRL, icws[NULL]->toByteMasterPic());
    PortOps::outb(PICS_CTRL, icws[NULL]->toByteSlavePic());

    for(uint8 idx = 1; idx < numberWords; idx++)
    {
        const ICW *currIcw = icws[idx];

        if((icws[0]->toByteMasterPic() & IC4) or idx < 3)
        {
            PortOps::outb(PICM_DATA, currIcw->toByteMasterPic());
            PortOps::outb(PICS_DATA, currIcw->toByteSlavePic());
        }
    }

    // Clear data registers
    PortOps::outb(PICM_DATA, 0x0);
    PortOps::outb(PICS_DATA, 0x0);

    // Set properties
    icw1 = ICW1(*(ICW1 *)icws[0]);
    icw2 = ICW2(*(ICW2 *)icws[1]);
    icw2 = ICW2(*(ICW2 *)icws[2]);
    icw2 = ICW2(*(ICW2 *)icws[3]);

    // Disable interrupt recieve
    Pic::disable();
};

Pic::Pic()
{
    ICW1 icw1;
    ICW2 icw2;
    ICW3 icw3;
    ICW4 icw4;

    const ICW *icws[numberWords] = {&icw1, &icw2, &icw3, &icw4};
    *this = Pic(icws);
};

uint8 Pic::getIsrMaster()
{
    PortOps::outb(PICM_CTRL, readIsr);
    return PortOps::inb(PICM_CTRL);
};

uint8 Pic::getIsrSlave()
{
    PortOps::outb(PICS_CTRL, readIsr);
    return PortOps::inb(PICS_CTRL);
};

void Pic::sendEoi()
{
    // Recognize is the interrupt running and on which PIC is it
    // Further, check AEOI bit in ICW4
    // Send EOI if AEOI bit not set
    uint16 isrFull = (uint16)getIsrSlave() << Pic::numberIrq | (uint16)getIsrMaster();
    bool intOnMaster = (isrFull & 0x00ff) > 0;
    bool intOnSlave = (isrFull & 0xff00) > 0;

    // Interrupt not executed - EOI not send
    if(intOnMaster == 0 and intOnSlave == 0) return;
    // Interrupt expected only on master PIC
    else if(intOnMaster and !intOnSlave)
    {
        PortOps::outb(PICM_CTRL, eoiCommand);
    }
    // Interrupt expected only on slave PIC
    else if(!intOnMaster and intOnSlave)
    {
        PortOps::outb(PICS_CTRL, eoiCommand);
    }
};

SingletonPic::SingletonPic() : Pic()
{
    if(isEntityAfter == false)
    {
        isEntityFirst = true;
        isEntityAfter = true;
    }
    else
    {
        isEntityFirst = false;
    }
};

SingletonPic::SingletonPic(const ICW *icws[numberWords]) : Pic(icws)
{
    if(isEntityAfter == false)
    {
        isEntityFirst = true;
        isEntityAfter = true;
    }
    else
    {
        isEntityFirst = false;
    }
};

Pic *SingletonPic::getInstance()
{
    if(isEntityFirst) return (Pic *)this;
    return nullptr;
};