I am just playing around with the Teensy 4.0 and try to understand how it works. I am currently executing the following program:
I watch the led-pin with an oscilloscope and see a 150MHz squarewave, so one loop execution costs (only) 4 cycles. If I run the program with no nops it runs also with 4 cycles per loop. Can you explain how the teensy "removes" the nops at runtime? Generated code is as follows:
So the code really contains the nops, so the cpu must get rid of them at runtime. Can you explain the behaviour? If I use 7 nops the loop runtime increases to 5 cycles. So no longer all nops are ignored. Very interesting behaviour.
And I do this all just because I am curious. It's no real need for me to understand but it would be nice.
Code:
int led = 13;
void setup() {
pinMode(led, OUTPUT);
Serial.begin(115200);
while(!Serial){} ;
Serial.println("teensy40dualIssue5nops1a...") ;
delay(200) ;
}
void loop() {
cli() ; // following loop should not be interrupted
while(1){ // loop executes with 150MHz at 600MHz clock
CORE_PIN13_PORTSET = CORE_PIN13_BITMASK; // led on
asm volatile("nop"); // nop-1
asm volatile("nop"); // nop-2
asm volatile("nop"); // nop-3
asm volatile("nop"); // nop-4
asm volatile("nop"); // nop-5
CORE_PIN13_PORTCLEAR = CORE_PIN13_BITMASK; // led off
}
}
Code:
void loop() {
cli() ; // following loop should not be interrupted
dc: b672 cpsid i
while(1){ // loop executes with 150MHz at 600MHz clock
CORE_PIN13_PORTSET = CORE_PIN13_BITMASK; // led on
de: 4a06 ldr r2, [pc, #24] ; (f8 <loop+0x1c>)
e0: 2308 movs r3, #8
e2: f8c2 3084 str.w r3, [r2, #132] ; 0x84
asm volatile("nop"); // nop-1
e6: bf00 nop
asm volatile("nop"); // nop-2
e8: bf00 nop
asm volatile("nop"); // nop-3
ea: bf00 nop
asm volatile("nop"); // nop-4
ec: bf00 nop
asm volatile("nop"); // nop-5
ee: bf00 nop
CORE_PIN13_PORTCLEAR = CORE_PIN13_BITMASK; // led off
f0: f8c2 3088 str.w r3, [r2, #136] ; 0x88
f4: e7f5 b.n e2 <loop+0x6>
f6: bf00 nop
f8: 42004000 .word 0x42004000
And I do this all just because I am curious. It's no real need for me to understand but it would be nice.