Question

I have an issue with passing data between arrays for processing that I can't seem to iron out. (I'm running the code on a Nios II processor)

HAL Type Definitions: alt_u8 : Unsigned 8-bit integer. alt_u32 : Unsigned 32-bit integer.

The core in my FPGA takes in a 128 bits at a time for data processing. I have this working in my original code by passing 4 x 32 bit unsigned int to the function:

alt_u32 load[4] = {0x10101010, 0x10101010, 0x10101010, 0x10101010}; 

The function processes this data and using another array I retrieve the info.

data_setload(&context,&load);       //load data
data_process(&context);                    //process
memcpy(resultdata,context.result,4*sizeof(unsigned int));
for(i=0; i<4 ; i++){    
    printf("received 0x%X \n",resultdata[i]);       //print to screen
} 

Above works perfectly, but when I try combine it with the second part it does not work.

I have a buffer used to store data: alt_u8 rbuf[512];

When the data buffer becomes full I'm trying to transfer the contents of 'rbuf' to the array 'load'. The main problem is load[4] takes 4 by 32 bit unsigned int for processing. So I want to 'fill up' these 4 by 32 bit unsigned int with data from rbuf, process the data and save the result to an array. Then loop again and fill the array load[4] with the next set of data (from rbuf) and continue until rbuf is empty. (and pad with zeros if necessary)

alt_u8 rbuf[512];
alt_u8 store[512];
alt_u32 resultdata[512];
alt_u32 *reg;                
int d, k, j;

for (j=0; j<512; j++){
    read_byte(&ch);   //gets data
    rbuf[j]=ch;       //stores to array rbuf
}
printf(" rbuf is full \n");
memcpy(store,rbuf,512*sizeof(alt_u8));  //store gets the value in rbuf.

for(k=0;k<16;k++)  //for loop used take in 4 chars to one unsigned 32 bit int
{
    for(d=0;d<4;d++)    //store 4 chars into an one 32 bit unsigned int
    {
        *reg = (*reg<<8 | store[d])  ;
    }
    reg =+1;     //increment pointer to next address location(not working properly)
}            //loop back

reg = 0;      //set pointer address back to 0

for(j=0;j<16;j++)   //trying to process data from here 
{   
    memcpy(load,reg,4*sizeof(alt_u32));      //copy first 4 locations from 'reg' to 'load'
    data_setload(&context,&load);       //pass 'load' to function
    data_process(&context);                      //process 128 bits
    memcpy(resultdata,context.result,4*sizeof(alt_u32));  //results copied to 'resultdata'
    *reg = *reg + 4;            //increment pointer address by 4?
    *resultdata = *resultdata+4;        //increment resultdata address by 4 and loop again
}        
/** need to put data back in char form for displaying***/

for(k=0;k<16;k++)              //for loop used take chars from 32 unsigned int
{
   for(d=4;d>=0;d--)            //loads 4 chars FROM A 32 unsigned int
   {
      store[d] = *resultdata;
      *resultdata = *resultdata>>8;
   }
   resultdata =+1;  //increment pointer next address location
}
for(d=0; d<512 ; d++){
    printf("received 0x%X ",store[d]);

The end goal is to take:

Array_A of unsigned 8 bit copy it into an Array_B[4] of unsigned 32 bit >> Process the Array_B[4] with my HDL code. It requires the input to be 128bits. Then loop back and take the next 128 bits and process them.

Was it helpful?

Solution

reg is defined but not initialized, so it will be a null pointer and you are triying to write a value to it (*reg assigns value, reg assigns address).

Also, the k-d loop is wrong. If you got reg initialized correctly, then a really easy way to do that is:

 for(k=0;k<16;k += 4)              //for loop used take chars from 32 unsigned int
 {
    *rbuf = *((alt_u32*)&store[k]);
    rbuf++;
 }

that loop will take the four intengers stored as bytes in the beginning of store and copies them to where rbuf is pointing.

I'm nearly shure that's not what you want to achieve, but is what your code was trying to do. If you want to fully copy the store to where rbuf points then you can do this:

 for(k=0;k<512;k += 4)              //for loop used take chars from 32 unsigned int
 {
    *rbuf = *((alt_u32*)&store[k]);
    rbuf++;
 }

That will copy all the values stored at store to rbuf.

Also, a better, faster, and cleaner way:

memcpy(rbuf, &store, 512);
rbuf += 512 / sizeof(alt_u32);

Finally, if you just want to fill load with the first four integers, then you can do that:

for(k = 0; k < 4; k++)
{
    load[k] = *((alt_u32*)&rbuf[k * 4]);
}

or

memcpy(&load, &rbuf, 4 * sizeof(alt_u32));

then you don't need store for noting.

Finally, here is a full rewriten function with the minimum memory usage and best performance:

alt_u8 rbuf[512];
alt_u32 resultdata[128]; //fixed its size to 128, (512 / sizeof(alt_u32))

int j;

//Do the loop to load data in rbuf
for (j=0; j<512; j++)
    read_byte(&rbuf[j]);

printf(" rbuf is full \n");

//Loop through rbuf in 4 * 32 bits per iteration (4*4 bytes)
for(j = 0; j < 512; j+= sizeof(alt_u32) * 4)
{

    data_setload(&context, (alt_u32*)&rbuf[j]); //I assume this function expects an alt_u32 pointer to 4 alt_u32 values
    data_process(&context);
    memcpy(&resultdata[j / sizeof(alt_u32)], context.result, sizeof(alt_u32) * 4);//I assume context.result is a pointer, if not then add & before it

}

//Print received data
for(j=0; j<512 ; j++){
    printf("received 0x%X ",rbuf[d]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top