c - Balancing branches in regard to cycles used -
i have loop several conditional branches in it. loop supposed run same amount of cycles no matter branches taken.
to achieve filled shorter branches nops (using asm("nop")) until of equal length longer branches. achieved wanted.
now have lot more branches , want automatically balance branches. compiling avr-gcc.
is there way this?
as requested: using atmega 1284p , avr-gcc implementation.
well didn't specify whether coding in asm or in c, since use branches call "asm()"... if using c, can call millis() @ beginning of loop, , call @ end too. have calculate maximum duration of loop. subtract 2 millis values , compare difference maximum duration of loop. yea lilbit confusing, here code:
#define max_duration 1000 //let's 1 second, should calculate while(yourcondition) { temp1 = millis(); //do branches temp2 = millis(); delay(max_duration-(temp2-temp1)); } while if coding in asm, have @ first disable interrupts in order not have longer loops. can setup 16bit timer, if processor has any, greatest prescaler , checking timer value instead of millis , make delay function, done as:
delay: ;put millisecond wait in r17:r16 ldi r18, 200 ldi r19, 26 ;200*26* (3 cicles each little loop) = 1 millisecond of delay 16mhz oscillator mov r20, r17 delay_loop: dec r19 brne delay_loop ldi r19, 26 dec r18 brne delay_loop ldi r18, 200 dec r17 brne delay_loop mov r17, r20 dec r16 brne delay_loop ret hoping instrunction set similar mine. next time specify code using , processor targeting
Comments
Post a Comment