Pic18f opcode number cycles or what is faster ... Where is to see it ? How is to examine it ?

General discussion on mikroC PRO for PIC.
Author
Message
cpservicespb
Posts: 35
Joined: 18 Oct 2022 21:43

Pic18f opcode number cycles or what is faster ... Where is to see it ? How is to examine it ?

#1 Post by cpservicespb » 07 Nov 2022 13:00

I have MicroE board with Pic18f2550 on it.
Where is able to see hom many cycles is spent to process commands supported by the device ?

For example how many cycles is necessary to run

Code: Select all

ANDWF       LATA+0, 0
And what is faster:
1.

Code: Select all

var |= (abc & ~m) >> n;
where is abc - unsigned int value ; m - mask (for example 0xfd, n - bit number.
or
2.

Code: Select all

 var |= (0U << n) | (b << n)
where is n - bit number, b - bit value (o or 1)

By the way, code 2 is occupied less flash than code 1.

How is to examine it ?

hexreader
Posts: 1785
Joined: 27 Jun 2010 12:07
Location: England

Re: Pic18f opcode number cycles or what is faster ... Where is to see it ? How is to examine it ?

#2 Post by hexreader » 07 Nov 2022 13:47

Data sheet, listing file and debugger will give the information requested.

Actual timings will vary hugely according to data types and variable values.
Here is what the debugger told me for some guessed at variable types and values:

Code: Select all

//    PIC18F2550  on EasyPICv7 board with 8MHz 2-pin crystal fitted
// build the project and run the debugger and step-over to get cycles count (delta)
// you are using StartUSB board, so you will need to run software simulator debugger, not hardware debugger

unsigned int abc;
unsigned int var;
unsigned char b = 1;
unsigned char m = 0xfd;
unsigned char nn = 5;

void main(){

    asm     ANDWF       LATA+0, 0                                               //  how many cycles is necessary to run
                                                                                // debugger shows 1 cycle (0.5 us with 8MHz clock)
                                                                                // data sheet indicates 1 cycle

    var |= (abc & ~m) >> nn;                                                    // where is abc - unsigned int value ; m - mask (for example 0xfd, n - bit number.
                                                                                // debugger shows 57 cycles (28.5 us with 8MHz clock)
                                                                                // .lst file shows the assembly instructions used (view - listing)
                                                                                
    var |= (0U << nn) | (b << nn);                                              // where is n - bit number, b - bit value (o or 1)
                                                                                // debugger shows 94 cycles (47 us with 8MHz clock)
                                                                                // .lst file shows the assembly instructions used

    while(1);                                                                   // loop forever to aid debugging
}
Start every day with a smile...... (get it over with) :)

cpservicespb
Posts: 35
Joined: 18 Oct 2022 21:43

Re: Pic18f opcode number cycles or what is faster ... Where is to see it ? How is to examine it ?

#3 Post by cpservicespb » 07 Nov 2022 16:00

Thanks.

Some if my assumptions regarding cycles were correct.

cpservicespb
Posts: 35
Joined: 18 Oct 2022 21:43

Re: Pic18f opcode number cycles or what is faster ... Where is to see it ? How is to examine it ?

#4 Post by cpservicespb » 09 Nov 2022 14:24

hexreader wrote:
07 Nov 2022 13:47
and debugger will give the information requested
Unfortunately I can not launch the debugger from Micro C Pro IDE with
"Could not start debugger.
Last know programmed file is not .... fw.hex.
Please reprogram mcu."

Although this fw is flashed via Bootloader by Mikroelektronika USB HID Bootloader v2.8 to MCU.
When I try to Build + Propgram I get "registry entry for mikroprog suite programmer was not found".

StartUSB board is connected to the same PC where MicroC 7.6 PRO is installed.

How is to do it working ?

hexreader
Posts: 1785
Joined: 27 Jun 2010 12:07
Location: England

Re: Pic18f opcode number cycles or what is faster ... Where is to see it ? How is to examine it ?

#5 Post by hexreader » 09 Nov 2022 15:41

Select build, not build and program

Use software debugger (simulator), not hardware debugger
Start every day with a smile...... (get it over with) :)

cpservicespb
Posts: 35
Joined: 18 Oct 2022 21:43

Re: Pic18f opcode number cycles or what is faster ... Where is to see it ? How is to examine it ?

#6 Post by cpservicespb » 09 Nov 2022 16:48

At the time:
- Build type = ICD Debug;
-Debugger = Software;
During start debugging:
"Couldn not start debugger.
Last known programmed file is not up to date.
Please reprogram MCU."

I have just before it reflashed by the freshest image.

Is it necessary to change Build type to "Release" ?

hexreader
Posts: 1785
Joined: 27 Jun 2010 12:07
Location: England

Re: Pic18f opcode number cycles or what is faster ... Where is to see it ? How is to examine it ?

#7 Post by hexreader » 09 Nov 2022 17:43

- Build type = release

This setting allows for software simulator debug only.

You do not even need to connect your StartUSB board. Everything is simulated - not real

You cannot do ICD debug unless you own either a Cedgrip programmer/debugger or a mikroProg programmer

Even then you would need to solder a 5-pin header to CN1 on your board
Start every day with a smile...... (get it over with) :)


cpservicespb
Posts: 35
Joined: 18 Oct 2022 21:43

Re: Pic18f opcode number cycles or what is faster ... Where is to see it ? How is to examine it ?

#9 Post by cpservicespb » 11 Nov 2022 13:22

I created new project for debugging purposes only:

Code: Select all

void main() {

const struct PortsStruct* pPort;
struct PortsStruct* pPortTmp;
unsigned char portNumP;
unsigned int pPortNum;
unsigned int* DataLineByteTmp = 0;



const struct PortsStruct Ports[9] = {
................. Here is some elements with some data
    };

pPort = &Ports;

portNumP = 4;


pPortNum = (pPort + portNumP - 1);


DataByteTmp = *(pPortTmp)->DataByte;

}
I see at Watch window only Ports and pPort.
Other are not visible.

And se breakpoints here do nothing.

Why ?

hexreader
Posts: 1785
Joined: 27 Jun 2010 12:07
Location: England

Re: Pic18f opcode number cycles or what is faster ... Where is to see it ? How is to examine it ?

#10 Post by hexreader » 11 Nov 2022 13:52

I see at Watch window only Ports and pPort.
You did better than me

That code does not compile, even if I add some elements to Ports[9]

Could you post example code that compiles please?
Start every day with a smile...... (get it over with) :)

cpservicespb
Posts: 35
Joined: 18 Oct 2022 21:43

Re: Pic18f opcode number cycles or what is faster ... Where is to see it ? How is to examine it ?

#11 Post by cpservicespb » 11 Nov 2022 14:07

Here is:

Code: Select all

typedef struct PortsStruct { 
        unsigned char PortNum;
        unsigned int* DLByte;
        unsigned int* PLByte;
        unsigned char DLBit;  // should be sbit, but error
        unsigned char PLBit;  // should be sbit, but error
        // sbit DLBit at LATA3_bit;
} PortsStruct, *pPortsStruct;



void main() {

const struct PortsStruct* pPort;
struct PortsStruct* pPortTmp;
unsigned char portNumP;
unsigned int pPortNum;
unsigned int* DLByteTmp = 0;

unsigned int Tmp;

const struct PortsStruct Ports[9] = {
         {1, &LATA, &LATA, 0, 1},   // Port1[0] - LATA RA0 & RA1
         {2, &LATA, &LATA, 2, 3},   // Port2[1] - LATA RA2 & RA3
         {3, &LATA, &LATA, 4, 5},   // Port3[2] - LATA RA4 & RA5
         {4, &LATB, &LATB, 0, 1},   // Port4[3] - LATB RB0 & RB1
         {5, &LATB, &LATB, 4, 8},   // Port5[4] - LATB RB2 & RB3
         {6, &LATB, &LATB, 4, 5},   // Port6[5] - LATB RB4 & RB5
         {7, &LATC, &LATC, 0, 1},   // Port7[6] - LATC RC0 & RC1
         {8, &LATC, &LATC, 2, 3},   // Port8[7] - LATC RC2 & RC6
         {9, &LATC, &LATC, 7, 7}    // Port9[8] - LATC RC7
    };

pPort = &Ports;

portNumP = 4;


pPortNum = (pPort + portNumP - 1);

DLByteTmp = *(pPortTmp)->DLByte;

}
At the final I want to make the code working, but at the time i is not.
I can not get unsigned int address (&LATB for example) stored in DLByte (oxf8a) of structure PortsStruct as 4-th element of array Ports using pointer to the array.

cpservicespb
Posts: 35
Joined: 18 Oct 2022 21:43

Re: Pic18f opcode number cycles or what is faster ... Where is to see it ? How is to examine it ?

#12 Post by cpservicespb » 11 Nov 2022 14:17

I saw assembler listing and therea re no ay commands.
So strange for me, because, at least + command have to be as add ro something else.

hexreader
Posts: 1785
Joined: 27 Jun 2010 12:07
Location: England

Re: Pic18f opcode number cycles or what is faster ... Where is to see it ? How is to examine it ?

#13 Post by hexreader » 11 Nov 2022 14:41

The compiler is smart

If the compiler thinks that some code does nothing useful, then it will optimise that bit of code away to nothing.

This is why your listing file shows most of the code missing

Real code will usually make use of all variables, so this would not usually be an issue

Your test code does not "use" the results, so one option is to "use" the results in any way you prefer.
I usually send the results of variables to any old LAT register (which is sometimes handy for view attached LEDs)

There is a second issue. There is a strange debugger issue, whereby local variables may not show.
A work-around is to use only global variables - at least while debugging.
Annoying, but sometimes necessary.

Using only global variables has the added benefit of fooling the compiler into compiling all code without optimising away unused code

This seems to work for me:

Code: Select all

//    PIC18F2550  on StartUSB board with 8MHz 2-pin crystal fitted
// build the project and run the debugger and step-over to get cycles count (delta)
// you are using StartUSB board, so you will need to run software simulator debugger, not hardware debugger

// I see at Watch window only Ports and pPort.
// Other are not visible.

// At the final I want to make the code working, but at the time i is not.
// I can not get unsigned int address (&LATB for example) stored in DLByte (oxf8a)
//        of structure PortsStruct as 4-th element of array Ports using pointer to the array.

typedef struct PortsStruct {
        unsigned char PortNum;
        unsigned int* DLByte;
        unsigned int* PLByte;
        unsigned char DLBit;  // should be sbit, but error
        unsigned char PLBit;  // should be sbit, but error
        // sbit DLBit at LATA3_bit;
} PortsStruct, *pPortsStruct;

// all variables global to avoid debugger bug where variables do not show in watch window
const struct PortsStruct* pPort;
struct PortsStruct* pPortTmp;
unsigned char portNumP;
unsigned int pPortNum;
unsigned int* DLByteTmp = 0;

unsigned int Tmp;

void main() {

  const struct PortsStruct Ports[9] = {
         {1, &LATA, &LATA, 0, 1},   // Port1[0] - LATA RA0 & RA1
         {2, &LATA, &LATA, 2, 3},   // Port2[1] - LATA RA2 & RA3
         {3, &LATA, &LATA, 4, 5},   // Port3[2] - LATA RA4 & RA5
         {4, &LATB, &LATB, 0, 1},   // Port4[3] - LATB RB0 & RB1
         {5, &LATB, &LATB, 4, 8},   // Port5[4] - LATB RB2 & RB3
         {6, &LATB, &LATB, 4, 5},   // Port6[5] - LATB RB4 & RB5
         {7, &LATC, &LATC, 0, 1},   // Port7[6] - LATC RC0 & RC1
         {8, &LATC, &LATC, 2, 3},   // Port8[7] - LATC RC2 & RC6
         {9, &LATC, &LATC, 7, 7}    // Port9[8] - LATC RC7
  };

    pPort = &Ports;
    portNumP = 4;
    pPortNum = (pPort + portNumP - 1);
    DLByteTmp = *(pPortTmp)->DLByte;

    // compiler will optimise away any unused variables, so let's send variables to LATC to "use" them
    //LATC = portNumP;                                                          // pointless unless debugging  - comment out if using gobal variables
    //LATC = pPortNum;                                                          // pointless unless debugging  - comment out if using gobal variables
    //LATC = DLByteTmp;                                                         // pointless unless debugging  - comment out if using gobal variables

    while(1);                                                                   // infinite loop to aid debugging
}
Start every day with a smile...... (get it over with) :)

cpservicespb
Posts: 35
Joined: 18 Oct 2022 21:43

Re: Pic18f opcode number cycles or what is faster ... Where is to see it ? How is to examine it ?

#14 Post by cpservicespb » 11 Nov 2022 15:45

Thanks.
I will try ...

I solved it temporarily by LATX = variable;


So, can you "using your fresh sign" tell, why

Code: Select all

 pPortNum = (pPort + portNumP - 1);
do not work - "Cannot assign ' DebugMain.c" ?

Declaration:

Code: Select all

struct PortsStruct* pPortTmp, pPortNum;
unsigned char portNumP = 4;
I get

Code: Select all

pPort = &Ports; 
addrfess of structure array - i. e 1st element (0 index) and want to get portNumP element (portNumP - 1 indfex) by pointer, as well.
For this I do

Code: Select all

pPortNum = pPort + portNumP - 1;

hexreader
Posts: 1785
Joined: 27 Jun 2010 12:07
Location: England

Re: Pic18f opcode number cycles or what is faster ... Where is to see it ? How is to examine it ?

#15 Post by hexreader » 11 Nov 2022 15:59

Sorry, but I do not understand

Maybe someone else does?

.. or maybe post a full example project, rather than little bits and pieces?
Start every day with a smile...... (get it over with) :)

Post Reply

Return to “mikroC PRO for PIC General”