2

我正在尝试将有符号双数写入内存并回读相同的数字,回读是多余的,因为它只是在触发 PL(可编程逻辑)FPGA Fabric 访问此数据之前验证内存中是否存在正确的数据并执行一项任务。

我将文件读入双精度(联合的一部分),然后通过无符号长整数(联合的一部分)写入内存。但是写入内存之前和我读出之后的数据是错误的,它只是最后一个字节。(详见代码和注释)

union floatpun {
    double dw;
    unsigned long lw;
};

void *lookup_slave_by_phy_addr(const int fd, const off_t target, const size_t mapsize)
{
  void *map_base, *virt_addr;

  /* Map one page */
  map_base = mmap(0, mapsize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~(mapsize-1));
  if (map_base == (void *) -1) { FATAL; }
  printf("Memory mapped at address %p.\n", map_base);
  fflush(stdout);

  virt_addr = map_base + (target & (mapsize-1));
  return virt_addr;
}


int main(int argc, char *argv[])
{  
  union floatpun conv;

  FILE *fp;
  fp = fopen("/media/card/numbers.txt", "r");
  fscanf(fp,"%lf",&conv.dw);  // 0.009101592004299160 Reads this number from the file, which is correct as expected.
  printf("The value Read from the File is %lx \n",conv.lw); // Prints 3f 82 a3 da ff ff ff fe, which is also correct.
  fclose(fp);

  int fd;
  if ((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) { FATAL; }
  printf("/dev/mem opened.\n");
  fflush(stdout);

  void *weights;
  // Map systemMemory master phy address range 0000 0008 7000 0000 -> 0000 0008 7FFF FFFF
  weights       = lookup_slave_by_phy_addr(fd,strtoul("0x0000000870000000",0,0),MAP_SIZE_256M);
    *((unsigned char *) (weights+0x00))  = conv.lw;   // Writing into the mempory
    SysMem= *((unsigned char *) (weights+0x00));    // Reading it out from the memory
    printf("Read %lx\n", SysMem);                   // When this is printed I get only FE, the last byte of the data read from the file, I have read the following 0x01, 02 03 also, which are all junk data like 0x69 0x95 0x9A

  close(fd);
  return 0;
}


我在写入内存或从内存读取时犯了什么错误,我希望将这个完整的 64 位写入内存。或者我应该手动将每个字节写入一个内存字节,内存字(32位)不是可寻址的吗?或者如果不是,我可以让它成为一个可寻址的词吗?

此外,这是在 Zynq 和 Petalinux 上完成的

请帮忙 :)

4

2 回答 2

1

你的问题在这里:

    *((unsigned char *) (weights+0x00))  = conv.lw;   // Writing into the mempory
    SysMem= *((unsigned char *) (weights+0x00));    // Reading it out from the memory

您将您的转换(void *) weights(unsigned char *),然后将值存储conv.lw在该指针位置。但是通过进行类型转换,您已经明确告诉编译器
您只想编写一个. 同样,当您回读它时,您再次将其转换为 an ,因此您仅从该位置读取一个单字节。 unsigned charconv.lw
weights(unsigned char *)

如果您改为执行以下操作:

    *((unsigned long *) (weights+0x00))  = conv.lw;   // Writing into the mempory
    SysMem= *((unsigned long *) (weights+0x00));    // Reading it out from the memory

您将写入和读取conv.lw.

还有一些原因使您尝试做的事情不可移植,包括:unsigned long在 32 位架构上通常只有 4 个字节,并且取消引用从其他类型转换的指针是(至少有时)未定义的行为。

于 2019-05-23T01:30:25.513 回答
0

赋值*((unsigned char *) (weights+0x00)) = conv.lw;只写一个unsigned charSysMem= *((unsigned char *) (weights+0x00));只读一个unsigned char

要写入和读取 a double,请使用:

* (double *) weights = conv.dw;
double x = * (double *) weights;
printf("%a\n", x); // Or use %g or %f.

或者:

memcpy(weights, conv.dw, sizeof conv.dw);
double x;
memcpy(x, weights, sizeof x);
printf("%a\n", x); // Or use %g or %f.

第一种方法需要weights针对您的平台进行适当的对齐(看起来就像在您的示例代码中一样)。这段代码不应该直接存在别名问题,因为它作为conv.dw正确的类型(通过写到那里创建。doubleweightsdoubleweightsdouble

第二种方法应该不管对齐方式都可以工作,前提weights是在您的过程中可以正确访问 in 的地址。

无论哪种情况,都不需要工会。可以直接使用double,无需使用unsigned long.

于 2019-05-23T01:41:32.567 回答