我正在尝试将有符号双数写入内存并回读相同的数字,回读是多余的,因为它只是在触发 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 上完成的
请帮忙 :)