Ada record representation

徘徊边缘 提交于 2019-12-11 06:17:37

问题


I have a record type as follows:

type Rec_T is record
   a : Bit_12_T;
   b : Bit_4_T;
end record;

where Bit_12_T is mod 2**12 and Bit_4_T is mod 2**4.

To tell the compiler the precise alignment of this record, I use the the for use record statement. However, I want to split the a field bewteen the bytes, so I try to do it as follows:

for Rec_T use record
   a at 0 range 0 .. 7; -- The 1st byte
   a at 1 range 0 .. 3; -- The 2nd byte
   b at 1 range 4 .. 7;
end record;

Clearly, this is not the way to do it as the compiler complains that "component clause is previously given at line ...".

Question: Is it possible to have a component split between the bytes and how to do it? If it is not possible, should I have a_high and a_low and then use some bit operations to merge them together?


回答1:


Think of the location as an array of bits, not as a sequence of bytes. Hence this should work:

for Rec_T use record
   a at 0 range 0 .. 11; -- bits 0..7 of the 1st byte and bits 0..3 of the 2nd byte
   b at 0 range 12 .. 15; -- bits 4..7 of the second byte
end record;
for Rec_T'Size use 16;

For more information see documentation here, specifically the example at then end of the page, that shows a similar situation.



来源:https://stackoverflow.com/questions/39095262/ada-record-representation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!