Author Topic: CRC calculation help (C Code)  (Read 1507 times)

0 Members and 1 Guest are viewing this topic.

Offline RerouterTopic starter

  • Super Contributor
  • ***
  • Posts: 4694
  • Country: au
  • Question Everything... Except This Statement
CRC calculation help (C Code)
« on: May 30, 2015, 12:26:51 am »
I am trying to understand a block of code for calculating a CRC in the hopes of improving / modifying it, To me it seems odd that each bytes while loop spins away 255 times?

Code: [Select]
Var17 = 0x05;   //DATA for calculation
Var18 = 0x00;
Var19 = 0x00;   
Var20 = 0x00;



Var14 = 0xFFFF; //CRC initial value
Var15 = 0;      //counter
Var16 = 0;     
While(Var15 != 255)
{
Var15 = Var15 + 1;
Var14 = Var14 ^ (Var17);

While(Var16 != 8)
{       
Var16 = Var16 + 1;   
 
if((Var14 & 0x0001) != 0) {Var14 = (Var14 >> 1) ^ 0x8408; }
else {Var14 = (Var14 >> 1); }
}
}

Var14 = Var14 & 65535;
Var15 = 0;      //counter
Var16 = 0;

While(Var15 != 255)
{
Var15 = Var15 + 1;
Var14 = Var14 ^ (Var18);

While(Var16 != 8)
{       
Var16 = Var16 + 1;
if((Var14 & 0x0001) != 0) {Var14 = (Var14 >> 1) ^ 0x8408; }
else {Var14 = (Var14 >> 1); }
}
}

Var14 = Var14 & 65535;
Var15 = 0;      //counter
Var16 = 0;     

While(Var15 != 255)
{
Var15 = Var15 + 1;
Var14 = Var14 ^ (Var19);

While(Var16 != 8)
{
Var16 = Var16 + 1;     
if((Var14 & 0x0001) != 0) {Var14 = (Var14 >> 1) ^ 0x8408; }
else {Var14 = (Var14 >> 1); }
}
}

Var14 = Var14 & 65535;
Var15 = 0;      //counter
Var16 = 0;     

While(Var15 != 255)
{
Var15 = Var15 + 1;
Var14 = Var14 ^ (Var20);

While(Var16 != 8)
{       
Var16 = Var16 + 1;     
if((Var14 & 0x0001) != 0) {Var14 = (Var14 >> 1) ^ 0x8408; }
else {Var14 = (Var14 >> 1); }
}
}

Var14 = Var14 & 65535;
input data is 05000000, I've substituted it in the code to try and make things clearer,  expected result is 6D76 or 766D, (do not know the eadianess) If i could please get some help understanding just what it is doing, or suggestions for making it run better, it would be appreciated,  only thing to note is a do not think the script language supports look up tables,


I've been shown in the past that the same CRC can be calacutled by the settings below, but this really confuses me, as with both cases i haven't been able to replicate its output on any online calculator:

CRC 16 Bits
Initial: FFFF
Poly: 1021
Xor In/Out: 0
Reflect in/ Out: True,
« Last Edit: May 30, 2015, 12:28:41 am by Rerouter »
 

Offline marshallh

  • Supporter
  • ****
  • Posts: 1462
  • Country: us
    • retroactive
Re: CRC calculation help (C Code)
« Reply #1 on: May 30, 2015, 02:26:59 am »
polynomial is 0x1021.

So polynomial is x^13 + x^5 + 1  (same as x^0)

That may help you. I'm not sure why that code is written like that, I usually do it in HDL
Verilog tips
BGA soldering intro

11:37 <@ktemkin> c4757p: marshall has transcended communications media
11:37 <@ktemkin> He speaks protocols directly.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf