8259 very simple basic interupt coding?

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
Ben_3D

8259 very simple basic interupt coding?

Post by Ben_3D »

Hi everyone again :),
I'm playing around with my assembly code again...and again in protected mode...but I'm having a few problems...well lots of problems, trying to get the 8259 to trigger interrupts...as its suppose to count down, then trigger an interrupt to irq0...doesn't seem to be working though.

The code simple sets up the irq's and only the timer interrupt...I have put a delay in my interrupt routine of about 1 second...but I would have thought the routine would be called over and over agian...which is what I'm looking for.

Thanks,

Ben

Code: Select all

cli

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
; Disable all IRQs.
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
disable_irqs:
          push    eax
          push    edx

          mov     al, 0xFF
          mov     dx, 0x21
          out     dx, al      ; outb(0x21, 0xFF)

          mov     al, 0xFF
          mov     dx, 0xA1
          out     dx, al      ; outb(0x21, 0xFF)

          pop     edx
          pop     eax

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

          mov     cl, 0x20                            ; PIC 1.
          mov     ch, 0x28                            ; PIC 2.
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
; Remap PICs to:  CL = pic1  CH = pic2
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
remap_pics:

     ; IWC1
     ;------
          mov     al, 0x11
          mov     dx, 0x20
          out     dx, al      ; outb(0x20, 0x11)

          mov     al, 0x11
          mov     dx, 0xA0
          out     dx, al      ; outb(0xA0, 0x11)
 
     ; IWC2
     ;------
          mov     al, cl
          mov     dx, 0x21
          out     dx, al      ; outb(0x21, pic1)

          mov     al, ch
          mov     dx, 0xA1
          out     dx, al      ; outb(0xA1, pic2)

     ; IWC3
     ;------
          mov     al, 0x04
          mov     dx, 0x21
          out     dx, al      ; outb(0x21, 4)

          mov     al, 0x02
          mov     dx, 0xA1
          out     dx, al      ; outb(0xA1, 2)

     ; IWC4
     ;------
          mov     al, 0x01
          mov     dx, 0x21
          out     dx, al      ; outb(0x21, 0x01)

          mov     al, 0x01
          mov     dx, 0xA1
          out     dx, al      ; outb(0xA1, 0x01)

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

; Enable IRQ0 (timer) at the 8259 PIC chips, disable others:
;(Port 0x21 bit 0 is timer)
          mov     al, 0xFE
          mov     dx, 0x21
          out     dx, al      ; outb(0x21, ~0x01)

          mov     al, 0xFF
          mov     dx, 0xA1
          out     dx, al      ; outb(0x21, ~0x00)

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

;EOI for IRQ 0-7
mov     al, 0x20
        mov dx, 0x20
        out dx, al      ; outb(0x20, 0x20)
        
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

sti              ; Interrupts back..
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:8259 very simple basic interupt coding?

Post by Pype.Clicker »

you need to acknowledge every IRQ you receive to the 8259 so that it knows it can deliver the next IRQ (roughly speaking). So you should have "out 0x20, 0x20" before your IRQ0 handler issues the IRET instruction.
Ben_3D

Re:8259 very simple basic interupt coding?

Post by Ben_3D »

Hi Pype.Clicker,
well your right, seems to have fixed it...strangly enough I was doing:
outb(0xA0,0x20)
outb(0x20,0x20)
which I thought did all the EOI for IRQ's 0-15...but this does only 8-15 and doesn't do the timer IRQ 0 as you said...just
outb(0x20,0x20) on its own.

Thanks

Ben

:)



Works with:
;EOI for IRQ 0-7
mov al, 0x20
mov dx, 0x20
out dx, al ; outb(0x20, 0x20)

Was doing an
For IRQ 8-15, both 8259 chips must get EOI:
mov al, 0x20
mov dx, 0xA0
out dx, al ; outb(0xA0, 0x20)

mov al, 0x20
mov dx, 0x20
out dx, al ; outb(0x20, 0x20)
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:8259 very simple basic interupt coding?

Post by Pype.Clicker »

Ben_3D wrote: Hi Pype.Clicker,
well your right, seems to have fixed it...strangly enough I was doing:
outb(0xA0,0x20)
outb(0x20,0x20)
which I thought did all the EOI for IRQ's 0-15...but this does only 8-15 and doesn't do the timer IRQ 0 as you said...just
outb(0x20,0x20) on its own.
Strange indeed, maybe the datasheet have the final answer... People integrating 8259 logic in chipsets tends to produce interfaces that lead to very weird results when you're getting out of the 'standard tracks'. I can even remember of an implementation that continued to deliver slave IRQs after i completely trashed the master/slave pin configuration.
Ben_3D

Re:8259 very simple basic interupt coding?

Post by Ben_3D »

I was wondering, I've set my timer chip as follows:


    mov   al,36 ; outb(0x43, 0x36)
    out   43h,al   

    mov   al,0    ; default is 0x0000 (65536) (18.2 per sec)
    out   40h,al ; out(0x40,countdown_lsb)         
    mov   al,0   
    out   40h,al ; out(0x40 countdown_msb)

So it would be 18.2 times a second...so if I keep count in my interrupt, every time my counter reaches 18 for example, this would be approx 1 second.

Now I'm running this simple test code in bochs under windows at the moment...so I'm not sure if its because I'm running it under windows...but I'm sure the interrupt is being called a lot faster..more like 1000 times a second or something like that.

I could be missing something....maybe my maths just isn't as good as it used to be or something.

Thanks for all your wisdom and help :)
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:8259 very simple basic interupt coding?

Post by Pype.Clicker »

Ben_3D wrote: I could be missing something....maybe my maths just isn't as good as it used to be or something.
Well, what you're missing is the fact that bochs emulation is *not* realtime, though you can finetune the Instructions Per Second so that it almost become. That means when you ask bochs to wait for 2 seconds, that will be 2 seconds in *its* world (so the virtual devices will have time to react, etc more or less like real ones), but not in *ours*.
AR

Re:8259 very simple basic interupt coding?

Post by AR »

Or you can add:

Code: Select all

clock: sync=realtime, time0=local
To your Bochs file. Note that doing this may stuff up the OS in the emulated environment as pre-emptible kernels may quickly become overloaded with PIT interrupts and overflow the stack.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:8259 very simple basic interupt coding?

Post by Pype.Clicker »

tnx. added to the FAQ
Post Reply