Tracing the attached HEX in AvrStudio Simulator V2. This bit of code is part of the scanline rendered, the last pixel of the last tile on the line. An Timer1 overflow interrupt happens right after that final pixel OUT. This is the expected behavior as per the simulator and what I can see also on the hardware.
Code: Select all
videomode13core.s, Line 579:
out VIDEO,r16
add ZL,r24
adc ZH,r18
lpm XL,Z+ <-- Timer1 rollover happens during this instruction. Before execution TCNT1=0xFFFD.
mainloop:
out VIDEO,r17 <-- Before execution TCNT1=0x0000 and TOV1 flag is set. After execution of the OUT, the interrupt vector is executed.
brts romloop
Code: Select all
videomode13core.s, Line 579:
out VIDEO,r16
add ZL,r24
adc ZH,r18
lpm XL,Z+ <-- Timer1 rollover happens during this instruction. Before execution TCNT1=0xFFFD. After, in update_hardware, the timers are rolled over, TOV flag is set then later the interrupts flags check is done and the vector is taken right away.
mainloop:
out VIDEO,r17 <-- this is never called and we are one cycle short on that scanline.
brts romloop
From the Atmega644 Datasheet on Timer1:
I could not find any detailed diagrams explaining this kind of interrupt timing. It may have to do with how the AVR pipelines the instructions.14.9.1 Normal Mode
The simplest mode of operation is the Normal mode (WGMn3:0 = 0). In this mode the counting
direction is always up (incrementing), and no counter clear is performed. The counter simply
overruns when it passes its maximum 16-bit value (MAX = 0xFFFF) and then restarts from the
BOTTOM (0x0000). In normal operation the Timer/Counter Overflow Flag (TOVn) will be set in
the same timer clock cycle as the TCNTn becomes zero.