This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Hidden clock cycles in repeat block loop?

Hi,

I have a code which loops around 32 times and toggle a pin (GP[9]).  My system clock is 60MHz so I figure, given the code below, I should see the pin toggle at rate = 60MHz/(#of instructions * number of cycles per instruction). 

_asmReadADC:
 @BRC0_L = 32 || mmap()  ; Generate 16 cycles sclock.
 blockrepeat{
 T0 = *port(#0x1c0a)       -------- read serial port 1 register and put it in T0 (One system clock cycle to execute)
 T0 ^= 0x0200    ;BIT9     -------- Toggle data bit in GP[9] (One System clock cycle to execute)
 *port(#0x1c0a) = T0       -------- Toggle pin GP[9] (One system clock cycle to execute)
 }

 T0 = *port(#0x1c0a);
 T0 |= BIT9     ; Raise sclock high
 *port(#0x1c0a) = T0
 return

Two loops equal to one cycle (on and off) on GP[9].  So, given my system clock is 60MHz, I should see GP[9] toggles at 60MHz/(2 loops * 3 cycles per loop) = 10MHz.  However, I'm only seeing 3.3MHz (on the scope.)  This mean that the code in the repeat block above is actually taking 9 system clock cycles to execute.  How is this so?  Are there clock cycles in the repeat block (code shown above) that I haven't account for?

Thanks,

Lam

P.S.  Here is the disassembly code for the block of code shown above.

030930          $asmReadADC.asm:8:18$, asmReadADC:
030930 e63420_98                MOV #32,mmap(@BRC0)
030934 0e0008                   RPTB 0x3093f
030937 a4511c0a                 MOV port(#01c0ah),T0
03093B 7f020044                 XOR #512,T0,T0
03093F c4511c0a                 MOV T0,port(#01c0ah
)
030943 a4511c0a                 MOV port(#01c0ah),T0
030947 7e020044                 OR #512,T0,T0
03094B c4511c0a                 MOV T0,port(#01c0ah)
03094F 4804                     RET

  • Lam,

    GPIO access takes 3 cycles (back-to-back) and instruction fetching cycles should be added to each loop.  I ran the same code and got the same result.

    Instruction fetch cycles are depending on the instruciton length, so it can vary. Yes, the loop takes 9 cycles to be excuted.

    Here are some examples (I used GPIO11):

    1) example1 ==> each time takes 9 cycles

     *port(#0x1c0A) = #0x0800
     *port(#0x1c0A) = #0x0000

     *port(#0x1c0A) = #0x0800
     *port(#0x1c0A) = #0x0000

     *port(#0x1c0A) = #0x0800
     *port(#0x1c0A) = #0x0000

    .....

    2) example2 ==> your code, each loop takes 9 cycles

     

    3) example3 ==> within the loop, it taks 3 cycles to toggle GPIO.

      @BRC0_L = 32 || mmap()  ; Generate 16 cycles sclock.
      blockrepeat{
      *port(#0x1c0A) = #0x0800      

      *port(#0x1c0A) = #0x0000 

      *port(#0x1c0A) = #0x0800    

      *port(#0x1c0A) = #0x0000 

      *port(#0x1c0A) = #0x0800      

      *port(#0x1c0A) = #0x0000 

      }