View Issue Details

IDProjectCategoryView StatusLast Update
0033165FPCCompilerpublic2018-02-16 19:49
ReporterTimm Thaler Assigned ToFlorian  
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
PlatformEmbeddedOSAVR 
Product Version3.1.1 
Fixed in Version3.1.1 
Summary0033165: R1 has no valid value in ISR routine
DescriptionWhile calling a ISR in AVR controller register R1 is not pushed. R1 is taken to be ZERO, but sometimes it is not.

This code
[code=laz]procedure timer0_interrupt; alias: 'TIMER0_COMPA_ISR'; interrupt; public;
begin
  inc(sseccnt);
  if sseccnt >= 25000 then begin // 1 second
    sseccnt := 0;[/code]
will be compiled to
[code=asm]TIMER0_COMPA_ISR:
    push r26
    push r20
    push r19
    push r18
    push r0
    in r0,63
    push r0
# [25] inc(sseccnt);
    lds r20,(U_sEM_INT_ss_SSECCNT)
    lds r19,(U_sEM_INT_ss_SSECCNT+1)
    ldi r18,1
    add r20,r18
    adc r19,r1 <= here R1 = 0 is assumed
    sts (U_sEM_INT_ss_SSECCNT),r20
    sts (U_sEM_INT_ss_SSECCNT+1),r19[/code]

R1 is assumed to be zero, but if the ISR is called within a multiply part R1:R0 holds the results of the multiplication.

A word multiply somewhere in the main program gives
[code=asm]# [50] mean := (15 * mean + vval) div 16;
    lds r18,(U_sEM_DEFINE_ss_MEAN)
    lds r19,(U_sEM_DEFINE_ss_MEAN+1)
    ldi r22,15
    mov r21,r1
    movw r2,r18
    mul r2,r22
    movw r18,r0 <= from here R1:R0 holds the return of the mul
    mul r3,r22
    add r19,r0
    mul r2,r21
    add r19,r0
    clr r1 <= here R1 is ZERO again
    mov r2,r19
    mov r21,r1
 [/code]

So of the ISR is called within mul and clr R1, R1 may have any other value than ZERO.
Steps To Reproducedoing a word calculation in an ISR that uses R1, or zeroing a variable
doing a lot of word multiplies in main program which uses mul instruction
=> the word calculation in the ISR will went wrong
Additional InformationFPC 3.1.1
Compiler options -O2 or -O3, others I didn't try

Bugfix: If R1 is used in ISR, push R1, clr R1 at entry, pop R1 at return of ISR.
TagsNo tags attached.
Fixed in Revision38233
FPCOldBugId
FPCTarget
Attached Files

Activities

Timm Thaler

2018-02-13 23:22

reporter   ~0106378

I am running FPC 3.1.1, SVN-Revision 57250, dated 2018-02-04 at Lazarus 1.9.0. and it is not fixed there.

Florian

2018-02-13 23:27

administrator   ~0106379

> FPC 3.1.1, SVN-Revision 57250

Very unlikely, FPC is currently at svn revision 38235, I guess you are confusing it with the lazarus svn version.

Timm Thaler

2018-02-14 00:09

reporter   ~0106380

Got it.

Sorry to bother You: Shouldn't be the clr after saving the S-Reg cause clr changes the S-Reg? (http://www.microchip.com/webdoc/avrassembler/avrassembler.wb_CLR.html)

    push r1
    push r0
    clr r1
    in r0,63
    push r0

should be

    push r1
    push r0
    in r0,63
    push r0
    clr r1

Florian

2018-02-16 19:49

administrator   ~0106405

Thanks, also fixed.

Issue History

Date Modified Username Field Change
2018-02-13 13:03 Timm Thaler New Issue
2018-02-13 18:47 Florian Fixed in Revision => 38233
2018-02-13 18:47 Florian Status new => resolved
2018-02-13 18:47 Florian Fixed in Version => 3.1.1
2018-02-13 18:47 Florian Resolution open => fixed
2018-02-13 18:47 Florian Assigned To => Florian
2018-02-13 23:22 Timm Thaler Note Added: 0106378
2018-02-13 23:22 Timm Thaler Status resolved => feedback
2018-02-13 23:22 Timm Thaler Resolution fixed => reopened
2018-02-13 23:27 Florian Note Added: 0106379
2018-02-13 23:27 Florian Status feedback => resolved
2018-02-13 23:27 Florian Resolution reopened => fixed
2018-02-14 00:09 Timm Thaler Note Added: 0106380
2018-02-14 00:09 Timm Thaler Status resolved => feedback
2018-02-14 00:09 Timm Thaler Resolution fixed => reopened
2018-02-16 19:49 Florian Note Added: 0106405
2018-02-16 19:49 Florian Status feedback => resolved
2018-02-16 19:49 Florian Resolution reopened => fixed