Xilinx SDK Xil_In 内存对齐
SDK 的"xil_io.h"中提供了对地址的直接读写操作
不过我在实践中发现,对于Xil_In32(),当偏移为1不断读取时,会出现不正确的值。
我当时是在某连续若干地址中写了 0x0000 00ff(假如基址是0xc000 0000吧),但是读取的时候
0xc000 0001 读取的值是 0x0000 00ff;
0xc000 0002 读取的值也是 0x0000 00ff;
我就很纳闷了,这不科学啊,如果Xil_In32()是读取4个Bytes,那么0xc000 0002 读取的值应该是 0x0000 ff00;
后来我就怀疑是不是因为内存对齐导致的,我做了一个尝试,代码如下
Addr = 0xc5000000; u32 offset = 0; for(offset = 0; offset < VERT * HORI; ) { Xil_Out32LE(Addr+4*offset, 0x00abcdef); Xil_Out32LE(Addr+4*offset+4, 0x00123456); offset+=2; } u32 value; for(offset = 0; offset < 160; offset++) { value = Xil_In8(Addr+offset); printf("Mem 0x%x is 0x%x \r\n",Addr+offset,value); } for(offset = 0; offset < 160; offset++) { value = Xil_In32(Addr+offset); printf("Mem 0x%x is 0x%x \r\n",Addr+offset,value); }
最终输出结果如下
Mem 0xc5000000 is 0xef Mem 0xc5000001 is 0xcd Mem 0xc5000002 is 0xab Mem 0xc5000003 is 0x0 Mem 0xc5000004 is 0xef Mem 0xc5000005 is 0xcd Mem 0xc5000006 is 0xab Mem 0xc5000007 is 0x0 Mem 0xc5000008 is 0xef Mem 0xc5000009 is 0xcd Mem 0xc500000a is 0xab Mem 0xc500000b is 0x0 Mem 0xc500000c is 0xef Mem 0xc500000d is 0xcd Mem 0xc500000e is 0xab Mem 0xc500000f is 0x0 Mem 0xc5000010 is 0xef Mem 0xc5000011 is 0xcd Mem 0xc5000012 is 0xab Mem 0xc5000013 is 0x0 Mem 0xc5000014 is 0xef Mem 0xc5000015 is 0xcd Mem 0xc5000016 is 0xab Mem 0xc5000017 is 0x0 Mem 0xc5000018 is 0xef Mem 0xc5000019 is 0xcd Mem 0xc500001a is 0xab Mem 0xc500001b is 0x0 Mem 0xc500001c is 0xef Mem 0xc500001d is 0xcd Mem 0xc500001e is 0xab Mem 0xc500001f is 0x0 Mem 0xc5000020 is 0xef Mem 0xc5000021 is 0xcd Mem 0xc5000022 is 0xab Mem 0xc5000023 is 0x0 Mem 0xc5000024 is 0xef Mem 0xc5000025 is 0xcd Mem 0xc5000026 is 0xab Mem 0xc5000027 is 0x0 Mem 0xc5000028 is 0xef Mem 0xc5000029 is 0xcd Mem 0xc500002a is 0xab Mem 0xc500002b is 0x0 Mem 0xc500002c is 0xef Mem 0xc500002d is 0xcd Mem 0xc500002e is 0xab Mem 0xc500002f is 0x0 Mem 0xc5000030 is 0xef Mem 0xc5000031 is 0xcd Mem 0xc5000032 is 0xab Mem 0xc5000033 is 0x0 Mem 0xc5000034 is 0xef Mem 0xc5000035 is 0xcd Mem 0xc5000036 is 0xab Mem 0xc5000037 is 0x0 Mem 0xc5000038 is 0xef Mem 0xc5000039 is 0xcd Mem 0xc500003a is 0xab Mem 0xc500003b is 0x0 Mem 0xc500003c is 0xef Mem 0xc500003d is 0xcd Mem 0xc500003e is 0xab Mem 0xc500003f is 0x0 Mem 0xc5000040 is 0xef Mem 0xc5000041 is 0xcd Mem 0xc5000042 is 0xab Mem 0xc5000043 is 0x0 Mem 0xc5000044 is 0xef Mem 0xc5000045 is 0xcd Mem 0xc5000046 is 0xab Mem 0xc5000047 is 0x0 Mem 0xc5000048 is 0xef Mem 0xc5000049 is 0xcd Mem 0xc500004a is 0xab Mem 0xc500004b is 0x0 Mem 0xc500004c is 0xef Mem 0xc500004d is 0xcd Mem 0xc500004e is 0xab Mem 0xc500004f is 0x0 Mem 0xc5000050 is 0xef Mem 0xc5000051 is 0xcd Mem 0xc5000052 is 0xab Mem 0xc5000053 is 0x0 Mem 0xc5000054 is 0xef Mem 0xc5000055 is 0xcd Mem 0xc5000056 is 0xab Mem 0xc5000057 is 0x0 Mem 0xc5000058 is 0xef Mem 0xc5000059 is 0xcd Mem 0xc500005a is 0xab Mem 0xc500005b is 0x0 Mem 0xc500005c is 0xef Mem 0xc500005d is 0xcd Mem 0xc500005e is 0xab Mem 0xc500005f is 0x0 Mem 0xc5000060 is 0xef Mem 0xc5000061 is 0xcd Mem 0xc5000062 is 0xab Mem 0xc5000063 is 0x0 Mem 0xc5000064 is 0xef Mem 0xc5000065 is 0xcd Mem 0xc5000066 is 0xab Mem 0xc5000067 is 0x0 Mem 0xc5000068 is 0xef Mem 0xc5000069 is 0xcd Mem 0xc500006a is 0xab Mem 0xc500006b is 0x0 Mem 0xc500006c is 0xef Mem 0xc500006d is 0xcd Mem 0xc500006e is 0xab Mem 0xc500006f is 0x0 Mem 0xc5000070 is 0xef Mem 0xc5000071 is 0xcd Mem 0xc5000072 is 0xab Mem 0xc5000073 is 0x0 Mem 0xc5000074 is 0xef Mem 0xc5000075 is 0xcd Mem 0xc5000076 is 0xab Mem 0xc5000077 is 0x0 Mem 0xc5000078 is 0xef Mem 0xc5000079 is 0xcd Mem 0xc500007a is 0xab Mem 0xc500007b is 0x0 Mem 0xc500007c is 0xef Mem 0xc500007d is 0xcd Mem 0xc500007e is 0xab Mem 0xc500007f is 0x0 Mem 0xc5000080 is 0xef Mem 0xc5000081 is 0xcd Mem 0xc5000082 is 0xab Mem 0xc5000083 is 0x0 Mem 0xc5000084 is 0xef Mem 0xc5000085 is 0xcd Mem 0xc5000086 is 0xab Mem 0xc5000087 is 0x0 Mem 0xc5000088 is 0xef Mem 0xc5000089 is 0xcd Mem 0xc500008a is 0xab Mem 0xc500008b is 0x0 Mem 0xc500008c is 0xef Mem 0xc500008d is 0xcd Mem 0xc500008e is 0xab Mem 0xc500008f is 0x0 Mem 0xc5000090 is 0xef Mem 0xc5000091 is 0xcd Mem 0xc5000092 is 0xab Mem 0xc5000093 is 0x0 Mem 0xc5000094 is 0xef Mem 0xc5000095 is 0xcd Mem 0xc5000096 is 0xab Mem 0xc5000097 is 0x0 Mem 0xc5000098 is 0xef Mem 0xc5000099 is 0xcd Mem 0xc500009a is 0xab Mem 0xc500009b is 0x0 Mem 0xc500009c is 0xef Mem 0xc500009d is 0xcd Mem 0xc500009e is 0xab Mem 0xc500009f is 0x0 Mem 0xc5000000 is 0xabcdef Mem 0xc5000001 is 0xabcdef Mem 0xc5000002 is 0xabcdef Mem 0xc5000003 is 0xabcdef Mem 0xc5000004 is 0xabcdef Mem 0xc5000005 is 0xabcdef Mem 0xc5000006 is 0xabcdef Mem 0xc5000007 is 0xabcdef Mem 0xc5000008 is 0xabcdef Mem 0xc5000009 is 0xabcdef Mem 0xc500000a is 0xabcdef Mem 0xc500000b is 0xabcdef Mem 0xc500000c is 0xabcdef Mem 0xc500000d is 0xabcdef Mem 0xc500000e is 0xabcdef Mem 0xc500000f is 0xabcdef Mem 0xc5000010 is 0xabcdef Mem 0xc5000011 is 0xabcdef Mem 0xc5000012 is 0xabcdef Mem 0xc5000013 is 0xabcdef Mem 0xc5000014 is 0xabcdef Mem 0xc5000015 is 0xabcdef Mem 0xc5000016 is 0xabcdef Mem 0xc5000017 is 0xabcdef Mem 0xc5000018 is 0xabcdef Mem 0xc5000019 is 0xabcdef Mem 0xc500001a is 0xabcdef Mem 0xc500001b is 0xabcdef Mem 0xc500001c is 0xabcdef Mem 0xc500001d is 0xabcdef Mem 0xc500001e is 0xabcdef Mem 0xc500001f is 0xabcdef Mem 0xc5000020 is 0xabcdef Mem 0xc5000021 is 0xabcdef Mem 0xc5000022 is 0xabcdef Mem 0xc5000023 is 0xabcdef Mem 0xc5000024 is 0xabcdef Mem 0xc5000025 is 0xabcdef Mem 0xc5000026 is 0xabcdef Mem 0xc5000027 is 0xabcdef Mem 0xc5000028 is 0xabcdef Mem 0xc5000029 is 0xabcdef Mem 0xc500002a is 0xabcdef Mem 0xc500002b is 0xabcdef Mem 0xc500002c is 0xabcdef Mem 0xc500002d is 0xabcdef Mem 0xc500002e is 0xabcdef Mem 0xc500002f is 0xabcdef Mem 0xc5000030 is 0xabcdef Mem 0xc5000031 is 0xabcdef Mem 0xc5000032 is 0xabcdef Mem 0xc5000033 is 0xabcdef Mem 0xc5000034 is 0xabcdef Mem 0xc5000035 is 0xabcdef Mem 0xc5000036 is 0xabcdef Mem 0xc5000037 is 0xabcdef Mem 0xc5000038 is 0xabcdef Mem 0xc5000039 is 0xabcdef Mem 0xc500003a is 0xabcdef Mem 0xc500003b is 0xabcdef Mem 0xc500003c is 0xabcdef Mem 0xc500003d is 0xabcdef Mem 0xc500003e is 0xabcdef Mem 0xc500003f is 0xabcdef Mem 0xc5000040 is 0xabcdef Mem 0xc5000041 is 0xabcdef Mem 0xc5000042 is 0xabcdef Mem 0xc5000043 is 0xabcdef Mem 0xc5000044 is 0xabcdef Mem 0xc5000045 is 0xabcdef Mem 0xc5000046 is 0xabcdef Mem 0xc5000047 is 0xabcdef Mem 0xc5000048 is 0xabcdef Mem 0xc5000049 is 0xabcdef Mem 0xc500004a is 0xabcdef Mem 0xc500004b is 0xabcdef Mem 0xc500004c is 0xabcdef Mem 0xc500004d is 0xabcdef Mem 0xc500004e is 0xabcdef Mem 0xc500004f is 0xabcdef Mem 0xc5000050 is 0xabcdef Mem 0xc5000051 is 0xabcdef Mem 0xc5000052 is 0xabcdef Mem 0xc5000053 is 0xabcdef Mem 0xc5000054 is 0xabcdef Mem 0xc5000055 is 0xabcdef Mem 0xc5000056 is 0xabcdef Mem 0xc5000057 is 0xabcdef Mem 0xc5000058 is 0xabcdef Mem 0xc5000059 is 0xabcdef Mem 0xc500005a is 0xabcdef Mem 0xc500005b is 0xabcdef Mem 0xc500005c is 0xabcdef Mem 0xc500005d is 0xabcdef Mem 0xc500005e is 0xabcdef Mem 0xc500005f is 0xabcdef Mem 0xc5000060 is 0xabcdef Mem 0xc5000061 is 0xabcdef Mem 0xc5000062 is 0xabcdef Mem 0xc5000063 is 0xabcdef Mem 0xc5000064 is 0xabcdef Mem 0xc5000065 is 0xabcdef Mem 0xc5000066 is 0xabcdef Mem 0xc5000067 is 0xabcdef Mem 0xc5000068 is 0xabcdef Mem 0xc5000069 is 0xabcdef Mem 0xc500006a is 0xabcdef Mem 0xc500006b is 0xabcdef Mem 0xc500006c is 0xabcdef Mem 0xc500006d is 0xabcdef Mem 0xc500006e is 0xabcdef Mem 0xc500006f is 0xabcdef Mem 0xc5000070 is 0xabcdef Mem 0xc5000071 is 0xabcdef Mem 0xc5000072 is 0xabcdef Mem 0xc5000073 is 0xabcdef Mem 0xc5000074 is 0xabcdef Mem 0xc5000075 is 0xabcdef Mem 0xc5000076 is 0xabcdef Mem 0xc5000077 is 0xabcdef Mem 0xc5000078 is 0xabcdef Mem 0xc5000079 is 0xabcdef Mem 0xc500007a is 0xabcdef Mem 0xc500007b is 0xabcdef Mem 0xc500007c is 0xabcdef Mem 0xc500007d is 0xabcdef Mem 0xc500007e is 0xabcdef Mem 0xc500007f is 0xabcdef Mem 0xc5000080 is 0xabcdef Mem 0xc5000081 is 0xabcdef Mem 0xc5000082 is 0xabcdef Mem 0xc5000083 is 0xabcdef Mem 0xc5000084 is 0xabcdef Mem 0xc5000085 is 0xabcdef Mem 0xc5000086 is 0xabcdef Mem 0xc5000087 is 0xabcdef Mem 0xc5000088 is 0xabcdef Mem 0xc5000089 is 0xabcdef Mem 0xc500008a is 0xabcdef Mem 0xc500008b is 0xabcdef Mem 0xc500008c is 0xabcdef Mem 0xc500008d is 0xabcdef Mem 0xc500008e is 0xabcdef Mem 0xc500008f is 0xabcdef Mem 0xc5000090 is 0xabcdef Mem 0xc5000091 is 0xabcdef Mem 0xc5000092 is 0xabcdef Mem 0xc5000093 is 0xabcdef Mem 0xc5000094 is 0xabcdef Mem 0xc5000095 is 0xabcdef Mem 0xc5000096 is 0xabcdef Mem 0xc5000097 is 0xabcdef Mem 0xc5000098 is 0xabcdef Mem 0xc5000099 is 0xabcdef Mem 0xc500009a is 0xabcdef Mem 0xc500009b is 0xabcdef Mem 0xc500009c is 0xabcdef Mem 0xc500009d is 0xabcdef Mem 0xc500009e is 0xabcdef Mem 0xc500009f is 0xabcdef
由于输出了160组,所以为方便观察,只取一个循环单元如下:
Mem 0xc5000000 is 0xef
Mem 0xc5000001 is 0xcd
Mem 0xc5000002 is 0xab
Mem 0xc5000003 is 0x0
Mem 0xc5000004 is 0x56
Mem 0xc5000005 is 0x34
Mem 0xc5000006 is 0x12
Mem 0xc5000007 is 0x0
Mem 0xc5000000 is 0xabcdef
Mem 0xc5000001 is 0xabcdef
Mem 0xc5000002 is 0xabcdef
Mem 0xc5000003 is 0xabcdef
Mem 0xc5000004 is 0x123456
Mem 0xc5000005 is 0x123456
Mem 0xc5000006 is 0x123456
Mem 0xc5000007 is 0x123456
观察可以发现,确实是因为内存对齐,32位读回时,由于0xc5000001不是32整数倍,所以向下取整,实际读取的是0xc5000000
读取时内存对齐的,那么写入呢?
我暂时还不用关心这个,所以没做验证,有兴趣的自己把上面代码稍作修改就可以验证了。