volatile int i=0;
char in_interrupt_handler = 0;
int main (void){
// PORTA as output
DDRA = 0xFF;
PORTA = 0xFF;
// IR 2 with falling edge
MCUCSR = MCUCSR | (1<<ISC2);
// ext IR 2 activated
GICR = GICR | (1<<INT2);
// global IR enable
sei();
while (1){
while(i < 300){
sts 0x0062, r25
i++;
sts 0x0061, r24
}
i = 0;
}
}
SIGNAL (SIG_INTERRUPT2){
in_interrupt_handler = 1;
if (i < 150)
PORTA = 0x00;
else if (i < 300)
PORTA = 0x55;
PORTA = 0xFF;
in_interrupt_handler = 0;
}
[] ((in_interrupt_handler == 1) -> (i >= 0 && i <= 300))
i is greater or equal to 0 and less or equal to 300.
Looking at the C source code, the formula seems to be true. But if you look at the translation of i++ you see that it yields two assembler instructions to store the 2-byte integer variable bytewise.
Since the interrupt can be triggered after each assembler instruction the formula is not fulfilled.
.
.
.
-------------------- variables --------------------
| name | O | S | value |
---------------------------------------------------
| i | 97 | 2 | 255 |
| in_interrupt_handler | 96 | 1 | 0 |
---------------------------------------------------
-------------------- registers --------------------
| 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 00 00 00 00 00 00 00 00 00 01 63 00 5F 04 28 01 |
| 00 F8 FE FF 00 00 00 00 00 00 20 00 00 00 00 00 |
| 00 00 00 00 00 00 00 00 00 00 FF FF 00 00 00 00 |
| 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 00 00 00 00 40 00 00 00 00 00 00 20 00 5F 04 A0 |
---------------------------------------------------
----------------------------------------------------- processes -----------------------------------------------------
| name | c source | od source | nipsvm pc |
---------------------------------------------------------------------------------------------------------------------
| main program | i++; | 0x194: sts 98, r25 | 0x000009A9 |
| interrupt handler 0 | | 0x3: int. handler 18 executable? | 0x00001F79 |
---------------------------------------------------------------------------------------------------------------------
--- process 0 sts 98, r25 --->
-------------------- variables --------------------
| name | O | S | value |
---------------------------------------------------
| i | 97 | 2 | 511 |
| in_interrupt_handler | 96 | 1 | 0 |
---------------------------------------------------
-------------------- registers --------------------
| 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 00 00 00 00 00 00 00 00 00 01 63 00 5F 04 28 01 |
| 00 F8 FE FF 00 00 00 00 00 00 20 00 00 00 00 00 |
| 00 00 00 00 00 00 00 00 00 00 FF FF 00 00 00 00 |
| 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 00 00 00 00 40 00 00 00 00 00 00 20 00 5F 04 A0 |
---------------------------------------------------
----------------------------------------------------- processes -----------------------------------------------------
| name | c source | od source | nipsvm pc |
---------------------------------------------------------------------------------------------------------------------
| main program | i++; | 0x198: sts 97, r24 | 0x000009F6 |
| interrupt handler 0 | | 0x3: int. handler 18 executable? | 0x00001F79 |
---------------------------------------------------------------------------------------------------------------------
--- process 1 int. handler 18 executable? --->
-------------------- variables --------------------
| name | O | S | value |
---------------------------------------------------
| i | 97 | 2 | 511 |
| in_interrupt_handler | 96 | 1 | 0 |
---------------------------------------------------
-------------------- registers --------------------
| 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 00 00 00 00 00 00 00 00 00 01 63 00 5F 04 28 01 |
| 00 F8 FE FF 00 00 00 00 00 00 20 00 00 00 00 00 |
| 00 00 00 00 00 00 00 00 00 00 FF FF 00 00 00 00 |
| 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 00 00 00 00 40 00 00 00 00 00 00 20 00 5D 04 20 |
---------------------------------------------------
----------------------------------------------------- processes -----------------------------------------------------
| name | c source | od source | nipsvm pc |
---------------------------------------------------------------------------------------------------------------------
| main program | i++; | 0x198: sts 97, r24 | 0x000009F6 |
| interrupt handler 0 | SIGNAL (SIG_INTERRUPT2){ | 0x214: push r1 | 0x00001150 |
---------------------------------------------------------------------------------------------------------------------
.
.
.
-------------------- variables --------------------
| name | O | S | value |
---------------------------------------------------
| i | 97 | 2 | 511 |
| in_interrupt_handler | 96 | 1 | 0 |
---------------------------------------------------
-------------------- registers --------------------
| 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 00 00 00 00 00 00 00 00 00 01 63 00 5F 04 28 01 |
| 00 F8 FE FF 00 00 00 00 00 00 20 00 00 00 00 00 |
| 00 00 00 00 00 00 00 00 00 00 FF FF 00 00 00 00 |
| 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 00 00 00 00 40 00 00 00 00 00 00 20 00 57 04 22 |
---------------------------------------------------
----------------------------------------------------- processes -----------------------------------------------------
| name | c source | od source | nipsvm pc |
---------------------------------------------------------------------------------------------------------------------
| main program | i++; | 0x198: sts 97, r24 | 0x000009F6 |
| interrupt handler 0 | in_interrupt_handler = 1; | 0x230: ldi r24, 1 | 0x00001441 |
---------------------------------------------------------------------------------------------------------------------
--- process 1 ldi r24, 1 --->
-------------------- variables --------------------
| name | O | S | value |
---------------------------------------------------
| i | 97 | 2 | 511 |
| in_interrupt_handler | 96 | 1 | 0 |
---------------------------------------------------
-------------------- registers --------------------
| 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 00 00 00 00 00 00 00 00 01 01 63 00 5F 04 28 01 |
| 00 F8 FE FF 00 00 00 00 00 00 20 00 00 00 00 00 |
| 00 00 00 00 00 00 00 00 00 00 FF FF 00 00 00 00 |
| 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 00 00 00 00 40 00 00 00 00 00 00 20 00 57 04 22 |
---------------------------------------------------
----------------------------------------------------- processes -----------------------------------------------------
| name | c source | od source | nipsvm pc |
---------------------------------------------------------------------------------------------------------------------
| main program | i++; | 0x198: sts 97, r24 | 0x000009F6 |
| interrupt handler 0 | in_interrupt_handler = 1; | 0x232: sts 96, r24 | 0x00001485 |
---------------------------------------------------------------------------------------------------------------------
--- process 1 sts 96, r24; process 2 (no command information) --->
-------------------- variables --------------------
| name | O | S | value |
---------------------------------------------------
| i | 97 | 2 | 511 |
| in_interrupt_handler | 96 | 1 | 1 |
---------------------------------------------------
-------------------- registers --------------------
| 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 00 00 00 00 00 00 00 00 01 01 63 00 5F 04 28 01 |
| 00 F8 FE FF 00 00 00 00 00 00 20 00 00 00 00 00 |
| 00 00 00 00 00 00 00 00 00 00 FF FF 00 00 00 00 |
| 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 00 00 00 00 40 00 00 00 00 00 00 20 00 57 04 22 |
---------------------------------------------------
----------------------------------------------------- processes -----------------------------------------------------
| name | c source | od source | nipsvm pc |
---------------------------------------------------------------------------------------------------------------------
| main program | i++; | 0x198: sts 97, r24 | 0x000009F6 |
| interrupt handler 0 | if (i < 150) | 0x236: lds r24, 97 | 0x000014CA |
---------------------------------------------------------------------------------------------------------------------
i has the value 255=0x00FF and the next value 256=0x0100 has to be stored to the corresponding memory lcoation, the execution of the first sts instruction yields the value 511=0x01FF for i.
If the interrupt is triggered in this state, the execution of the interrupt handler code finally leads to a state which falsifies the formula.