How to use C language structure with bit fields from ATEasy

Knowledge Base Article # Q200102

Read Prior Article Read Next Article
Summary Article show how to support C language structure with bit fields
  
Login to rate article
Solution:

ATEasy language, similar to Microsoft Visual Basic, does not have a bit field data type as C language structures. This does not prevent you from using bit fields structures when calling to C DLLs. In C, bit fields are grouped to integers (short, long, etc) and in ATEasy you substitute the group with the bit field basic type (for example DWord). To Access the specific field in ATEasy you use AND and SHR operators. For example, if in C you have the following structure:

typedef struct
{
  UINT32 data1 : 10;
  UINT32 data2 : 6;
  UINT32 data3 : 14;
  UINT32 data4 : 2 ;
} DWORDBITS;


In ATEasy the structure is defined as DWord (dwBits). Accessing data2 field is done as follows:

dwData2=(dwBits shr 10) and 0x3F


or

dwData2=(dwBits shr 10) and 0b111111


A more elegant and prone to changes (in the structure fields) is to use a procedure for setting and getting the fields values. Constants that hold the offset and the fields size can be also used as shown here:

DWORDBITS_DATA2_OFFSET : Word Const = 10
DWORDBITS_DATA2_SIZE : Word Const = 6

Procedure DWordBitsField(pdwBits, wOffset, dwSize, dwValue) : DWord
  pdwBits: Var DWord  
  wOffset : Val Word     ! offset of field, 0 based
  dwSize : Val DWord     ! # of bits for the field
  dwValue: [Val] DWord   ! optional, value of the field
  dwMask : DWord
{
  ! get or set DWordBits fields
  dwMask=(2ul ^ dwSize) -1  ! calc mask, 8 bit integer

  If ArgMissing(dwValue)  
    ! get and return the field value
    Return   (pdwBits shr wOffset) and dwMask  
  Else
    ! set the field and return all the bits
    pdw=(pdwBits and  not (dwMask shl wOffset))  or (dwValue shl wOffset)
    Return pdw
  Endif
}


Now to set data2 to 35 in dwBits:

dwData2=DWordbitsField(dwBits, DWORDBITS_DATA2_OFFSET, DWORDBITS_DATA2_SIZE, 35)


To get data2 from dwBits:

dwData2=DWordbitsField(dwBits, DWORDBITS_DATA2_OFFSET, DWORDBITS_DATA2_SIZE)


Notes:
  • When you import  a C header file that contains bit fields ATEasy substitute them with the basic type.
  • You can also use the ATEasy Bit() function in the internal library to access (get or set) a single bit.

Article Date 9/22/2008
Keywords ATEasy, Bit


Login to rate article

Read Prior Article Read Next Article
>