0

我在数组之间传递数据以进行处理时遇到问题,我似乎无法解决。(我在 Nios II 处理器上运行代码)

HAL 类型定义: alt_u8 :无符号 8 位整数。alt_u32 :无符号 32 位整数。

我的 FPGA 中的内核一次需要 128 位进行数据处理。通过将 4 x 32 位 unsigned int 传递给函数,我可以在我的原始代码中使用它:

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

该函数处理这些数据并使用另一个数组检索信息。

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
} 

上面的作品完美,但是当我尝试将它与第二部分结合时它不起作用。

我有一个用于存储数据的缓冲区:alt_u8 rbuf[512];

当数据缓冲区变满时,我试图将“rbuf”的内容传输到数组“load”。主要问题是 load[4] 需要 4 x 32 位 unsigned int 进行处理。所以我想用来自 rbuf 的数据“填充”这 4 个 32 位无符号整数,处理数据并将结果保存到一个数组中。然后再次循环并用下一组数据(来自 rbuf)填充数组 load[4] 并继续直到 rbuf 为空。(必要时用零填充)

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]);

最终目标是采取:

无符号 8 位的 Array_A 将其复制到无符号 32 位的 Array_B[4] >> 使用我的 HDL 代码处理 Array_B[4]。它要求输入为 128 位。然后循环返回并获取接下来的 128 位并处理它们。

4

1 回答 1

0

reg 已定义但未初始化,因此它将是一个空指针,您正在尝试向它写入一个值(*reg 分配值,reg 分配地址)。

此外,kd 循环是错误的。如果您正确初始化了 reg,那么一个非常简单的方法是:

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

该循环将在 store 的开头将四个整数存储为字节并将它们复制到 rbuf 指向的位置。

我几乎确定这不是您想要实现的目标,而是您的代码试图做的。如果您想将存储完全复制到 rbuf 指向的位置,那么您可以这样做:

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

这会将存储在 store 中的所有值复制到 rbuf。

此外,还有一种更好、更快、更清洁的方法:

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

最后,如果您只想用前四个整数填充负载,那么您可以这样做:

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

或者

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

那么你不需要商店来记录。

最后,这是一个完全重写的函数,具有最少的内存使用和最佳性能:

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]);
于 2014-04-24T20:23:17.507 回答