Help in providing dynamic array elements to a CRC generation function?

Hello All,

I created payload by dynamic arrays in a packet class. Now I want to use these dynamically created payload bytes in CRC generation for polynomial x^16 + x^12 + x^5 + 1, for data width 8 bits.
Following is the code of CRC I am using:

function [15:0] cal_crc( byte payload);//[1:10]);
//int j;
//for ( integer j = 1; j <= 10 ; j++)
begin
bit [15:0] crc;
reg [7:0] d;
reg [15:0] c;
reg [15:0] newcrc;

     d = payload;//[j];   
     c = crc;
     
     newcrc[0] = d[4] ^ d[0] ^ c[8] ^ c[12];
     newcrc[1] = d[5] ^ d[1] ^ c[9] ^ c[13];
     newcrc[2] = d[6] ^ d[2] ^ c[10] ^ c[14];
     newcrc[3] = d[7] ^ d[3] ^ c[11] ^ c[15];
     newcrc[4] = d[4] ^ c[12];
     newcrc[5] = d[5] ^ d[4] ^ d[0] ^ c[8] ^ c[12] ^ c[13];
     newcrc[6] = d[6] ^ d[5] ^ d[1] ^ c[9] ^ c[13] ^ c[14];
     newcrc[7] = d[7] ^ d[6] ^ d[2] ^ c[10] ^ c[14] ^ c[15];
     newcrc[8] = d[7] ^ d[3] ^ c[0] ^ c[11] ^ c[15];
     newcrc[9] = d[4] ^ c[1] ^ c[12];
     newcrc[10] = d[5] ^ c[2] ^ c[13];
     newcrc[11] = d[6] ^ c[3] ^ c[14];
     newcrc[12] = d[7] ^ d[4] ^ d[0] ^ c[4] ^ c[8] ^ c[12] ^ c[15];
     newcrc[13] = d[5] ^ d[1] ^ c[5] ^ c[9] ^ c[13];
     newcrc[14] = d[6] ^ d[2] ^ c[6] ^ c[10] ^ c[14];
     newcrc[15] = d[7] ^ d[3] ^ c[7] ^ c[11] ^ c[15];
   
     $display("The value of CRC result newcrc = %h ",newcrc); 
     return newcrc; 
   end
   endfunction : cal_crc

AND THIS IS HOW I GENERATED payload through dynamic array:-
class transaction extends uvm_sequence_item;//uvm_transaction; //uvm_sequence_item;
`uvm_object_utils(transaction)

rand bit [7:0] sync;
rand bit [15:0] sof;
rand bit [15:0] header;
rand bit[7:0] payload;
rand bit [15:0] crc;
rand bit [7:0] eof;

constraint data_size_c { payload.size inside { [1 : 10]};};

Can some one please help me that how shall I provide these payload bytes in the CRC function. As you can see in the cal_crc function, I tried to do it by passing argument by value. but its not working. Please help me with this. Its kinda urgent.
Any help is appreciated.

Thanks,
Swapnil

You will either need to call your cal_crc function for each byte of your payload, or modify your function to take a dynamic array as an argument.

function bit [15:0] cal_crc( bit [7:0] payload[]);
  bit [15:0] newcrc;
  bit [15:0] crc;
  crc = '0;
  foreach(payload[j])
    begin
      bit  [7:0] d;
      reg [15:0] c;
      d = payload[j];
      ....// polynomial goes here
      crc = crc ^ newcrc; // or whatever needs to be done fore each byte
  end
  return crc;
endfunction

Hi Dave,
I’am facing a different problem. This is my code:

localparam CRC32POL = 32'h04C11DB7;
function automatic bit[31:0] genCRC32(input bit [7:0] databyte_stream[]);    
	int unsigned i, j;
    bit [31:0] crc32_val = 32'hffffffff; // shiftregister,startvalue 
    bit [7:0]  data;
    //The result of the loop generate 32-Bit-mirrowed CRC
    for (i = 0; i < databyte_stream.size; i++)  // Byte-Stream
    begin
        data = databyte_stream[i];
        for (j=0; j < 8; j++) // Bitwise from LSB to MSB
        begin
            if ((crc32_val[0]) != (data[0])) begin
                crc32_val = (crc32_val >> 1) ^ CRC32POL;
            end else begin
                crc32_val >>= 1;
            end
            data >>= 1;
        end
    end
    crc32_val ^= 32'hffffffff; //invert results*/
	return crc32_val;
endfunction : genCRC32

I call this function further in my global code, like this: crc_data = genCRC32(data_for_crc).

I compare the result obtained by this function with an online CRC calculator but this funtion does not give the good result.

Could you help me plz? Its kinda urgent.

Thanks

Fatima

In reply to FatimaTazi:
Please explain what a “good result” should be. What algorithm are you trying to implement?

I’am trying to implement the CRC32/MPEG-2 algorithm.

The “good result” is the result given by this CRC online calulator: https://crccalc.com/.

I just discover that I should inverse my polynom because inside my code I inverse also the data but I still having a wrong result.

Thanks

Fatima