How to determine machine cycles?

Thread Starter

wannaBinventor

Joined Apr 8, 2010
180
Is there a way that I can determine how many clock ticks it takes to perform one loop of my program without just single stepping through MPLAB SIM and counting the instruction executed?

I have plenty of delays and what not that would take forever to single step through. I thought about starting a timer at the top of the loop, recording the value at the end of the loop, displaying it on an LCD, and computing the cycles but I figured there had to be a more direct way.
 

thatoneguy

Joined Feb 19, 2009
6,359
Stopwatch or profiler, I don't think profiling is an option without plugins, most C compilers come with one that shows a graph of processor time spent in each function. This can be used to speed up the whole program or tweak it in other ways.

You could read the timer value at the start and end, multiply by prescaler and you'd have a constant number.
 

AlexR

Joined Jan 16, 2008
732
The usual way to determine machine cycles for a delay loop is to count them. The data sheet gives you a list of assembler instructions and the number of machine cycles each one take to execute. Simply add up the cycles for all the instructions in the loop and multiple by the number of times the loop is executed to get the total delay. Much simpler and faster than single-stepping and much more accurate than the stopwatch.
 

Markd77

Joined Sep 7, 2009
2,806
The stopwatch is fine, gives the exact number of cycles. Counting is tricky for me especially when some instructions take one or two cycles depending if there is a skip (like btfss).
 

MMcLaren

Joined Feb 14, 2010
861
... Much simpler and faster than single-stepping and much more accurate than the stopwatch.
What are you talking about? Stopwatch is accurate.

Just turn on the simulator, view Stopwatch, set clock speed, set Program Counter location, insert breakpoint, and go...

Here's a good general purpose fixed delay subsystem to play with (much more flexible than those fixed delay code generators);

Cheerful regards, Mike, K8LH

Rich (BB code):
;******************************************************************
;  K8LH DelayCy() subsystem macro generates four instructions     *
;******************************************************************
        radix   dec
clock   equ     8               ; 4, 8, 12, 16, 20 (MHz), etc.
usecs   equ     clock/4         ; cycles/microsecond multiplier
msecs   equ     clock/4*1000    ; cycles/millisecond multiplier

DelayCy macro   delay           ; 11..327690 cycle range
        movlw   high((delay-11)/5)+1
        movwf   delayhi
        movlw   low ((delay-11)/5)
        call    uDelay-((delay-11)%5)
        endm

;******************************************************************
;  example code for simulation testing                            *
;******************************************************************
        org     0x000
SimTest
        DelayCy(200*usecs)      ; <- put simulator PC here
        goto    $               ; <- put simulator break point here

;******************************************************************
;  K8LH DelayCy() subsystem 16-bit uDelay subroutine              *
;******************************************************************
        nop                     ; entry for (delay-11)%5 == 4     |B0
        nop                     ; entry for (delay-11)%5 == 3     |B0
        nop                     ; entry for (delay-11)%5 == 2     |B0
        nop                     ; entry for (delay-11)%5 == 1     |B0
uDelay  addlw   -1              ; subtract 5 cycle loop time      |B0
        skpc                    ; borrow? no, skip, else          |B0
        decfsz  delayhi,F       ; done?  yes, skip, else          |B0
        goto    uDelay          ; do another loop                 |B0
        return                  ;                                 |B0
;******************************************************************
        end
 
Last edited:
Top