Page 1 of 1

Dont follow how to remapIRQS<Solved handy info on Pic>

Posted: Wed Jun 25, 2008 5:40 pm
by eax
Hiya still going nuts reading lots of theory tonight :-), I am getting someplace with brans tutorial now but theres one piece of code I dont follow going with whats mentioned on the page in question regarding irqs

I get that the following exert from brans tutorial OK
Any IBM PC/AT Compatible computer (anything with a 286 and later processor) has 2 chips that are used to manage IRQs. These 2 chips are known as the Programmable Interrupt Controllers or PICs. These PICs also go by the name '8259'. One 8259 acts as the 'Master' IRQ controller, and one is the 'Slave' IRQ controller. The slave is connected to IRQ2 on the master controller. The master IRQ controller is connected directly to the processor itself, to send signals. Each PIC can handle 8 IRQs. The master PIC handles IRQs 0 to 7, and the slave PIC handles IRQs 8 to 15. Remember that the slave controller is connected to the primary controller through IRQ2: This means that every time an IRQ from 8 to 15 occurs, IRQ2 fires at exactly the same time.

When a device signals an IRQ, remember that an interrupt is generated, and the CPU pauses whatever it's doing to call the ISR to handle the corresponding IRQ. The CPU then performs whatever necessary action (like reading from the keyboard, for example), and then it must tell the PIC that the interrupt came from that the CPU has finished executing the correct routine. The CPU tells the right PIC that the interrupt is complete by writing the command byte 0x20 in hex to the command register for that PIC. The master PIC's command register exists at I/O port 0x20, while the slave PIC's command register exists at I/O port 0xA0.

Before we get into writing our IRQ management code, we need to also know that IRQ0 to IRQ7 are originally mapped to IDT entries 8 through 15. IRQ8 to IRQ15 are mapped to IDT entries 0x70 through 0x78. If you remember the previous section of this tutorial, IDT entries 0 through 31 are reserved for exceptions. Fortunately, the Interrupt Controllers are 'programmable': You can change what IDT entries that their IRQs are mapped to. For this tutorial, we will map IRQ0 through IRQ15 to IDT entries 32 through 47. To start us off, we must add some ISRs to 'start.asm' in order to service our
ok so

. The master PIC's command register exists at I/O port 0x20, while the slave PIC's command register exists at I/O port 0xA0.

I think Im fine with this but I dont follow at all how in practice these irqs get remapped with the below code, have translated hex to decimal to see if I can fathom where the re-assignments are taking place or exactly whats happening in practice but can envisage it and would appreciate some input on this small exert if possible

[Cant understand]

Code: Select all

void irq_remap(void)
{
    outportb(0x20, 0x11);
    outportb(0xA0, 0x11);
    outportb(0x21, 0x20);
    outportb(0xA1, 0x28);
    outportb(0x21, 0x04);
    outportb(0xA1, 0x02);
    outportb(0x21, 0x01);
    outportb(0xA1, 0x01);
    outportb(0x21, 0x0);
    outportb(0xA1, 0x0);
}
there doesnt seem to be enough lines, 10 lines but that manages to remap 15 irqs?

outportb(0x20, 0x11); decimal of 20h = 32; decimal of 11h = 17 ,
;is this 32 referance to starting irq 32 or the command register? 0x20
so I figured
i.e irq 0 to 15 , reassigned to idt entrys 32-47
outportb(0xA0, 0x11);


A0 = 160, 11h = 32 ;at this point I figured the 32 was of some relevance but I cant find any pattern of numbers to help me understand in the following code?
outportb(0x21, 0x20);

21h = 33, 0x20
outportb(0xA1, 0x28);


A1 = 161 ; hmm is this other commands or something? outwith 20 and A0? This is where Im getting confused here
0x28 = 40
outportb(0x21, 0x04);

21h = 33 , 4
outportb(0xA1, 0x02);

A1 = 161, 2
outportb(0x21, 0x01);

21h = 33, 1
outportb(0xA1, 0x01);

A1h = 161, 1
outportb(0x21, 0x0);

33,0
outportb(0xA1, 0x0);
161,0

Im stumped as to how that remaps all the irqs mentioned but Im clearly missing something, would really appreciate some insight into how this code all works, what it is doing?

Re: Dont follow how so many irqs are remapped in small codepiece

Posted: Wed Jun 25, 2008 6:03 pm
by suthers
Ok, 0x20 and 0xA0 are the address of the master and slave pic respectively.
sending 0x11 to them tells them to get ready to be sent data.
The the second tells them where to remap the first IRQ they control to and the rest follow on logically.
So here you send 32 to the master pic, so it maps irq 0 to interrupt 32 and then the irq 1 to 33, etc....
same with the slave pic.... (which you send 40 to...) except it would map irq 8 to whatever you send to it...
In case your interested, the next to send a set of flags to tell each pic how to handle the interrupts (I'm not sure how they work, I've just always sent 0x1...).
And the the last two (sending 0 to both pics) tells the pic that the 'transaction' has finished....
Jules

edit: added clarification notes...

Re: Dont follow how so many irqs are remapped in small codepiece

Posted: Wed Jun 25, 2008 6:38 pm
by eax
Thanks a bunch for that, it really helps(actually helps majorly!). I can accept how it is actually happening in practice now,its late here but will take another look tommrow.

Just curious :) so 11h is the signal sent to master 20 and slave A0 , which is a signal to tell the PIC getting ready to send data(Or in my case get ready to get messed up hehe)

Its this next part thats interesting, thanks to your feedback I can see the logic as to why it would work! However I notice the numbers

32dec gets sent to port 21h, Im thinking master is 20h so is 21h some kind of storage port for the master pic?

because
40dec gets sent to port A1 , when A0 is the slave ?

Either way that really helps, Im just asking all this because last time around I dived in to OS dev and in hindsight that was not agood idea! so this time am trying to make sure I am not skipping any theory :) (No doubt I will stillmake many mistakes but this time around some of it is making more sense )

Re: Dont follow how so many irqs are remapped in small codepiece

Posted: Wed Jun 25, 2008 6:48 pm
by blound
if you really want to understand the pic well i suggest reading http://www.brokenthorn.com/Resources/OSDevPic.html

Re: Dont follow how so many irqs are remapped in small codepiece

Posted: Wed Jun 25, 2008 7:01 pm
by eax
Thank you!

Its 3am approx. here but I will definitely read the information at that link tomorrow. At some point well in the future I would love to be able to use some of the standard C library's so Im expecting I have got an extreme amount of reading to do if I am to have any chance of this. (Hopefully the indispensable hardware book 4th edition that I have ordered will help me some of the time along the way)

Update:

Couldn't resist having a sneak peek before going hehe :) Its tempting to start on that right now but better get some time of the PC for a few hours!, anyway cheers for article , looks really interesting.

Just to finally say thats me checking out that pic article now at broken thorn, its a very good article, I think retaining it all in my memory(no pun intended!) will be the hardest part. Just getting to the part about command registers so its getting to the point where It will hopefully aid me when I eventually start coding. About a year ago I used the bokenthorn site and osdev and maybe its just me but there seems to be a heck of alot more diverse info now which is great.

Actually leta see if Ive got this right now :)

outportb(0x20, 0x11);
outportb(0xA0, 0x11);
outportb(0x21, 0x20);
outportb(0xA1, 0x28);
outportb(0x21, 0x04);
outportb(0xA1, 0x02);
outportb(0x21, 0x01);
outportb(0xA1, 0x01);
outportb(0x21, 0x0);
outportb(0xA1, 0x0);

outportb(0x20, 0x11); Send initialisation control word 1 // 00010001b into primary pic command register
outportb(0xA0, 0x11); Send initialisation control word 1// 00010001b into slave pic command register

Next because of set init bit we need to send an ICW 2 , this is sent through primary and slave software ports 0x21 and 0xA1
outportb(0x21, 0x20); ; this is the IVT address start for the first 8 irqs? starts at 32
outportb(0xA1, 0x28); his is the IVT address start for the next 8 irqs? starts at 40

Next we send ICW 3 , ICW3 is made up of 0 to 7 bits

0 = irq 0
1 = irq 1
2 = irq 2
3 = irq 3
4 = irq 4
5 = irq 5
6 = irq 6
7 = irq 7 (8th irq)

in the x86 architecture irq 2 line is the line used to connect the master pic to the slave pic so we must set bit 2 to 1b and sent the final binary number to the 0x21 register on the primary pic
bit3 2 1 0
outportb(0x21, 0x04); 0,1,0,0

next need to do the same and send to secondary Pic the same set irq bit but considering the secondary pic only uses bits 0 through to 2 that means its impossible to send 0x04 so instead we send the binary value
4 2 1
0 1 0
outportb(0xA1, 0x02

Now that both pics are connected through IRQ 2 we need to send a base interrupt number that both pics can use, also because bit 0 of ICW 1 was set in the article example this means the pic is expecting and ICW 4 as per article flag description in ICW1 example. So The ICW 4 gets sent finally to both PICS and this ICW determines how everything is to operate, looking at the article the only but I will need to worry abput for the time being is the 80x86bit which is set to 1

outportb(0x21, 0x01);
outportb(0xA1, 0x01);

Finally null out the data registers

outportb(0x21, 0x0);
outportb(0xA1, 0x0);

Yes I have took much from the article :) , great article! Just figure I would relate parts of the article to the code I initially asked about. I know I still have heaps more to do but feel I have gained a bit from reading that!