- 论坛徽章:
- 3
|
这个问题有时候挺让人郁闷的,其实关键在于一个标准如何统一的问题,因为大端小端已是无法逃避的问题了。
根据C语言标准里,“An implementation may allocate any addressable storage unit large enough to hold a bitfield.
If enough space remains, a bit-field that immediately follows another bit-field in a
structure shall be packed into adjacent bits of the same unit.”
关键什么叫相邻?
我们还是用硬件描述语言的思路去理解这个问题吧。
其实大端和小端并不是天壤之别,拓扑来看,不过是某些接线顺序的问题,
比如verilog,命一个16bit的wire,你会怎么命?
wire [15:0] sample;
还是
wire [0:15] sample;
无论怎么命吧,
assign sample = 16'h1234;
在两种形式下,sample的每个bit将会是什么?
于是我们再考虑,按照两种方式设计的硬件(假设上面的那个是大端,下面这个的是小端)
这个sample直接接1个16bit的ram的数据总线。
那么
struct {
unsigned char a;
unsigned char b:4;
unsigned char c:4;
} x;
这个x此时从数据总线里出来,假设就是前面的那个16'h1234,
小端的时候,
x.a代表sample[8:0],自然是0x34
s.b因为是相邻,所以代表sample[12:9],自然是0x2
s.c代表sample[16:13],自然是0x1
而大端的时候,
x.a代表sample[0:8],自然是0x12
s.b因为是相邻,所以代表sample[9:12],自然是0x3
s.c代表sample[16:13],自然是0x4
数字设计的时候我经常被这些东西搞晕掉。 |
|