0
votes

After a lot of reading about interrupt handling etcetera, i still can figure out the full process of interrupt handling from the very beginning.

For example: A division by zero. The CPU fetches the instruction to divide a number by zero and send it to the ALU. Assuming the the ALU started the process of the division or run some checks before starting it.

  1. How the exception is signaled to the CPU ?
  2. How the CPU knows what exception has occurred from only one bit signal ? Is there a register that is reads after it gets interrupted to know this ? 2.How my application catches the exception? Do i need to write some function to catch a specipic SIGNAL or something else? And when i write expcepion handling routine like Try {} Catch {} And an exception occurres how can i know what exeption is thrown and handle it well ?

The most important part that bugs me is for example when an interupt is signaled from the keyboard to the PIC the pic in his turn signals to the CPU that an interrupt occurred by changing the wite INT. But how does the CPU knows what device need to be served ? What is the processes the CPU is doing when his INTR pin turns on ? Does he has a routine that checks some register that have a value of the interrupt (that set by the PIC when it turns on the INT wire? )

Please don't ban the post, it's really important for me to understand this topic, i read a researched a couple of weaks but connot connect the dots in my head.

Thanks.

2
Maybe read wiki.osdev.org/Interrupts (assuming x86 arch) - BadZen
Well then, how does the part "When an interrupt comes in, the IDT (which is setup by the OS in advance) is used to jump to code portion of the OS, which handles the interrupt...." not answer your question? - BadZen
How the CPU knows what interrupt was issued ? - user3601643
Read the section "Interrupt Overview" in the document I linked to above... For exceptions, the CPU internal circuitry state "knows" the interrupt. For hardware interrupts, each interrupt has a pin or pin state configuration. For software interrupts, the interrupt is given in the instruction. (If you are looking for the exact pin configurations used for triggering specific hardware interrupts on a given processor model / arch, you'll have to refer to the hardware specs for that model or architecture - although the document I linked does describe PIC interrupts at a high-level) - BadZen
(the short answer in the latter case is that the PIC signals INTR, and the processor goes into a wait state to receive the IRQ number from the PIC via IO. It's a little bit more complicated dance than just that tho - suprious IRQ, multiple PIC, etc, etc. See hardware specs for detail) - BadZen

2 Answers

1
votes

There are typically several thing associated with interrupts other than just a pin. Normally for more recent micro-controllers there is a interrupt vector placed on memory that addresses each interrupt call, and a register that signals the interrupt event/flag.

When a event that is handled by an interruption occurs and a specific flag is set. Depending on priority's and current state of the CPU the context switch time may vary for example a low priority interrupt flagged duding a higher priority interrupt will have to wait till the high priority interrupt is finished. In the event that nesting is possible than higher priority interrupts may interrupt lower priority interrupts.

In the particular case of exceptions like dividing by 0, that indeed would be detected by the ALU, the CPU may offer or not a derived interruption that we will call in events like this. For other types of exceptions an interrupt might not be available and the CPU would just act accordingly for example rebooting.

As a conclusion the interrupt events would occur in the following manner:

  1. Interrupt event is flagged and the corresponding flag on the register is set
  2. When the time comes the CPU will switch context to the interruption handler function.
  3. At the end of the handler the interruption flag is cleared and the CPU is ready to re-flag the interrupt when the next event comes.

Deciding between interrupts arriving at the same time or different priority interrupts varies with different hardware.

0
votes

It may be simplest to understand interrupts if one starts with the way they work on the Z80 in its simplest interrupt mode. That processor checks the state of a pin called /IRQ at a certain point during each instruction; if the pin is asserted and an "interrupt enabled" flag is set, then when it is time to fetch the next instruction the processor won't advance the program counter or read a byte from memory, but instead disable the "interrupt enabled" flag and "pretend" that it read an "RST 38h" instruction. That instruction behaves like a single-byte "CALL 0038h" instruction, pushing the program counter and transferring control to that address.

Code at 0038h can then poll various peripherals if they need any service, use an "ei" instruction to turn the "interrupt enabled" flag back on, and perform a "ret". If no peripheral still has an immediate need for service at that point, code can then resume with whatever it was doing before the interrupt occurred. To prevent problems if the interrupt line is still asserted when the "ret" is executed, some special logic will ensure that the interrupt line will be ignored during that instruction (or any other instruction which immediately follows "ei"). If another peripheral has developed a need for service while the interrupt handler was running, the system will return to the original code, notice the state of /IRQ while it processes the first instruction after returning, and then restart the sequence with the RST 38h.

In the simple Z80 approach, there is only one kind of interrupt; any peripheral can assert /IRQ, and if any peripheral does so the Z80 will need to ask every peripheral if it wants attention. In more advanced systems, it's possible to have many different interrupts, so that when a peripheral needs service control can be dispatched to a routine which is designed to handle just that peripheral. The same general principles still apply, however: an interrupt effectively inserts a "call" instruction into whatever the processor was doing, does something to ensure that the processor will be able to service whatever needed attention without continuously interrupting that process [on the Z80, it simply disables interrupts, but systems with multiple interrupt sources can leave higher-priority sources enabled while servicing lower ones], and then returns to whatever the processor had been doing while re-enabling interrupts.