Electronic – Polling vs Interrupt: which one is first in a bare-bone piece of hardware

interruptsmicroprocessor

First of all, I must say that I have a computer science background, and I don't have much knowledge about hardware. I would like to ask this question because in software, what I often see is a loop, e.g: while loop, for…loop…

In my understanding, polling is like you check for an event occur every certain amount of time (might require the use of clock?)

Interrupt is like we don't care if it "loops" or not, when the event comes, it will "signal"…

What about in hardware? What is actually equivalent to a "while loop" or "for loop" in hardware? I assume it is a mix of polling and interrupts…but I wonder which one is really first? Isn't it it that when we check for something, we must poll it first? (maybe I'm a bit biased on polling here).

Best Answer

Something to know is that interrupts require special hardware to work, and work at a layer 'outside' what you can manage purely with a programming language like C. Typically different compilers and different chips expose interrupt controls in different ways. Some have magical function names, or magical preprocessor macros, that signal the compiler to use the special instructions and memory layouts that are needed to manage the interrupts.

In the case of polling, the software running has some loops, checks some value, then branches when the state of interest has been reached. This is the normal programming you are familiar with.

With an interrupt, the chip has hardware to halt execution of the software, and jump to a special location for the interrupt handler. The interrupt handler often needs to save the state of the halted software, so that after the handler does its job, it can return to the software and pick up where it left off.

Neither method is "first", and while the interrupt style of programming can make some applications considerably faster, virtually any application can be written to use either method. Different applications are better suited to one or the other.