Remapping the PIC, too much hexadecimal numbers

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
skwee
Member
Member
Posts: 73
Joined: Mon Jan 12, 2009 3:11 am

Remapping the PIC, too much hexadecimal numbers

Post by skwee »

I started to rewrite my kernel, and came to the part where I have to write the PIC.
Last time I did I just used James Molloy's code (man your site is great!), now I'm trying to understand what is going in the following short 10 lines of code:

Code: Select all

  outb(0x20, 0x11);
  outb(0xA0, 0x11);
  outb(0x21, 0x20);
  outb(0xA1, 0x28);
  outb(0x21, 0x04);
  outb(0xA1, 0x02);
  outb(0x21, 0x01);
  outb(0xA1, 0x01);
  outb(0x21, 0x0);
  outb(0xA1, 0x0);
  • Port 0x20 is Command port of master PIC, and 0xA0 is command port of slave PIC.
    0x21 is Data port of master PIC, and 0xA1 is command port of slave PIC.
  • I also know that 0x11 is the initialization control world which start the initialization sequence of both PICs.
  • The next word 0x20 and 0x28 correspondingly are the starting offsets of each PIC (the new offsets we want to remap PICs on).
  • 0x04 and 0x02 as I understood are the pins by them the PICs communication (Master knows that slave connected to it via pin 4 and slave know that its connected to master by pin 2).
  • Now what is 0x01? From a document I found in google it says "ICW4 is used for telling that we are working in a 80x86 architecture and if the interruption is handled automatically or if it needs help from software." Can somebody expand on it?
  • The same goes for 0x0. Again from the same document I mentioned earlier the author uses 0xFF instead of 0x0 with a comment that 0xFF "disable all IRQs", is that mean that 0x0 Enables all IRQs?
Thanks a lot for help!
TCP/IP: Connecting people...
tantrikwizard
Member
Member
Posts: 153
Joined: Sun Jan 07, 2007 9:40 am
Contact:

Re: Remapping the PIC, too much hexadecimal numbers

Post by tantrikwizard »

Now what is 0x01? From a document I found in google it says "ICW4 is used for telling that we are working in a 80x86 architecture and if the interruption is handled automatically or if it needs help from software." Can somebody expand on it?
What exactly isn't clear? To perform a proper initialization the system needs to be in a known state. There's some features in the 8259 chip that arent used on x86 systems so they're disabled on the 8259. According to the spec ICW4 layout is:
  • 7 - Reserved
  • 6 - Reserved
  • 5 - Reserved
  • 4 - 1=nested mode, 0=not nested mode
  • 3:2 - 00=nonbuffered, 10=buffer slave, 11=buffer master
  • 1 - 1=auto EOI, 0=normal EOI
  • 0 - 1=8086 mode, 0=MCS-80/85 mode
So sending a 0x01 as the ICW4 sets the PICs in 8086 mode, not nested, nonbuffered, and manual EOI.
is that mean that 0x0 Enables all IRQs?

Yes, after initialzation (ICW4 has been sent) ports 0x21 and 0xa1 become R/W interrupt mask registers.
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: Remapping the PIC, too much hexadecimal numbers

Post by gravaera »

What you need is a quick rundown on how to understand Hex and its relationship with Binary, and also a look at these tutorials by neon: http://www.brokenthorn.com/Resources/

The Hex article may be found on the wiki.
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
User avatar
skwee
Member
Member
Posts: 73
Joined: Mon Jan 12, 2009 3:11 am

Re: Remapping the PIC, too much hexadecimal numbers

Post by skwee »

tantrikwizard
Thank you I got it now!

gravaera
I know hex and binary well enough, but thanks for the reference.
TCP/IP: Connecting people...
User avatar
~
Member
Member
Posts: 1228
Joined: Tue Mar 06, 2007 11:17 am
Libera.chat IRC: ArcheFire

Re: Remapping the PIC, too much hexadecimal numbers

Post by ~ »

Code: Select all

;//Initialization Command Word 1
;//Initialization Command Word 1
;//Initialization Command Word 1
;//Send ICW1:
;;;
;//ICW1 bitset:
;// Bits:
;//   bit 0 -- ICW4 needed          (1) or ICW4 not needed          (0)
;//   bit 1 -- cascade mode         (0) or single mode              (1)
;//   bit 2 -- IDT entry is 8 bytes (0) or 4 bytes                  (1)
;//   bit 3 -- edge triggered mode  (0) or level triggered mode     (1)
;//   bit 4 -- ICW1 is being issued (1) or ICW1 is not being issued (0)
;//  bits 5-7 are only used if bit 2 is 0 (000)
;
 outportb(0x20,00010001b); ;//PIC1
 outportb(0xA0,00010001b); ;//PIC2




;//Initialization Command Word 2
;//Initialization Command Word 2
;//Initialization Command Word 2
;//Send ICW2:
;;;
;//ICW2 bitset:
;// Bits:
;//  bit  0-2 -- reserved (00)
;//  bits 3-7 -- first INT number to be handled by each PIC
;
 outportb(0x21,0x28); ;//PIC1: INT28h-2Fh (like in Real Mode but now it's INT (08h+20h))
 outportb(0xA1,0x70); ;//PIC2: INT70h-77h (like in Real Mode)




;//Initialization Command Word 3
;//Initialization Command Word 3
;//Initialization Command Word 3
;//Send ICW3:
;;;
;//ICW3 bitset:
;// Bits:
;//  bit 0 -- PIC attached, "IRQ 0 to PIC2" for PIC1 and "IRQ  8" for PIC2  (1)
;//        or cascaded disabled for "IRQ 0" for PIC1 and "IRQ  8" for PIC2  (0)
;//
;//  bit 1 -- PIC attached, "IRQ 1 to PIC2" for PIC1 and "IRQ  9" for PIC2  (1)
;//        or cascaded disabled for "IRQ 1" for PIC1 and "IRQ  9" for PIC2  (0)
;//
;//  bit 2 -- PIC attached, "IRQ 2 to PIC2" for PIC1 and "IRQ 10" for PIC2  (1)
;//        or cascaded disabled for "IRQ 2" for PIC1 and "IRQ 10" for PIC2  (0)
;//
;//  bit 3 -- PIC attached, "IRQ 3 to PIC2" for PIC1 and "IRQ 11" for PIC2  (1)
;//        or cascaded disabled for "IRQ 3" for PIC1 and "IRQ 11" for PIC2  (0)
;//
;//  bit 4 -- PIC attached, "IRQ 4 to PIC2" for PIC1 and "IRQ 12" for PIC2  (1)
;//        or cascaded disabled for "IRQ 4" for PIC1 and "IRQ 12" for PIC2  (0)
;//
;//  bit 5 -- PIC attached, "IRQ 5 to PIC2" for PIC1 and "IRQ 13" for PIC2  (1)
;//        or cascaded disabled for "IRQ 5" for PIC1 and "IRQ 13" for PIC2  (0)
;//
;//  bit 6 -- PIC attached, "IRQ 6 to PIC2" for PIC1 and "IRQ 14" for PIC2  (1)
;//        or cascaded disabled for "IRQ 6" for PIC1 and "IRQ 14" for PIC2  (0)
;//
;//  bit 7 -- PIC attached, "IRQ 7 to PIC2" for PIC1 and "IRQ 15" for PIC2  (1)
;//        or cascaded disabled for "IRQ 7" for PIC1 and "IRQ 15" for PIC2  (0)
;//
;
 outportb(0x21,00000100b); ;//PIC1: connection to slave in IRQ2
 outportb(0xA1,00000010b); ;//PIC2: this is the hook from IRQ9 to the IRQ2 in PIC2




;//Initialization Command Word 4
;//Initialization Command Word 4
;//Initialization Command Word 4
;//Send ICW4:
;;;
;//ICW4 bitset:
;// Bits:
;//   bit 0   -- 8086/8088 mode (1) or 8085 mode                 (0)
;//   bit 1   -- manual EOI     (0) or auto EOI                  (1) Interrupt Acknowledge
;//  bits 2-3 --               [00] == nonbuffered mode      == [00]
;//                            (01) == nonbuffered mode      == (01)
;//                            (10) == buffered mode/slave   == (10)
;//                            (11) == buffered mode/master  == (11)
;//   bit 4   -- normal mode    (0) or special fully-nested mode (1)
;//  bits 5-7 -- reserved     (000)
;
 outportb(0x21,00000001b); ;//PIC1
 outportb(0xA1,00000001b); ;//PIC2




;//Output Control Word 1
;//Output Control Word 1
;//Output Control Word 1
;//Send OCW1:
;;;
;//OCW1 bitset:
;// Bits:
;//  bit 0 -- disable IRQ0 in PIC1 or IRQ8  in PIC2 (1) or enable it (0)
;//  bit 1 -- disable IRQ1 in PIC1 or IRQ9  in PIC2 (1) or enable it (0)
;//  bit 2 -- disable IRQ2 in PIC1 or IRQ10 in PIC2 (1) or enable it (0)
;//  bit 3 -- disable IRQ3 in PIC1 or IRQ11 in PIC2 (1) or enable it (0)
;//  bit 4 -- disable IRQ4 in PIC1 or IRQ12 in PIC2 (1) or enable it (0)
;//  bit 5 -- disable IRQ5 in PIC1 or IRQ13 in PIC2 (1) or enable it (0)
;//  bit 6 -- disable IRQ6 in PIC1 or IRQ14 in PIC2 (1) or enable it (0)
;//  bit 7 -- disable IRQ7 in PIC1 or IRQ15 in PIC2 (1) or enable it (0)
;
 outportb(0x21,11111111b); ;//PIC1, disables also the IRQ2 slave line response
 outportb(0xA1,11111111b); ;//PIC2
Last edited by ~ on Wed Oct 06, 2010 6:55 pm, edited 8 times in total.
User avatar
neon
Member
Member
Posts: 1568
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Remapping the PIC, too much hexadecimal numbers

Post by neon »

Hello,

This might help. More speficially the section "Initialization Control Words (ICW)".
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
skwee
Member
Member
Posts: 73
Joined: Mon Jan 12, 2009 3:11 am

Re: Remapping the PIC, too much hexadecimal numbers

Post by skwee »

~ and neon
Thank you both a lot!
TCP/IP: Connecting people...
Post Reply