Record multi-bit fields

Record multi-bit fields

Language reference ›› Types ›› Records ››
Parent Previous Next

Introduced to match any SFR particular multi-bit / slice fields (such as ADCON0.CHS), a new PMP-specific syntax has been added for field declaration:

 

multi-bit-field-declaration = identifier ":" BITS "[" numeric-constant "]" ";" .


<numeric-constant>:

An expression that returns a value in the 1..32 range, specifying the number of bits of the field.

 

A BITS field may be accessed as any other numeric field. It is assumed to be unsigned.

A small BITS field (up to 8-bit) cannot cross a BYTE boundary (e.g.: a BITS[3] followed by a BITS[6]).

A big BITS field (more than 8-bit) must start at a byte boundary (Previous BOOLEAN and BITS fields count must be zero or a multiple of 8).


BOOLEAN and BITS fields are grouped in one or several consecutive bytes at the end of the record, in the declaration order.

The programmer must not use code that presumes that a record field position in memory follows the record declaration order.

 

Memory representation: Individual bits and bit slices are allocated LSB first (from right to left).

 

A multi-bit declaration is only allowed within a RECORD declaration:



TYPE
  tMyRecordBits = RECORD
      X:   BYTE;    // bits 0..7 of first byte
      B1:  BOOLEAN; // bit 0 of second byte (same as B1: BITS[1])
      BF1: BITS[3]; // bits 1..3 of 2nd byte
      BF2: BITS[4]; // bits 4..7 of 2nd byte
  END;
  // Fake a PIC18's T0CON into the PIC16's OPTION_REG
  tT0CON = RECORD
    T0PS:       BITS[3];
    PSA:        BOOLEAN;
    T0SE:       BOOLEAN;
    T0CS:       BOOLEAN;
    INTEGD:     BOOLEAN;
    NOT_RAB_PU: BOOLEAN;
  END;

 

This 2nd example is also allowed but the data is generated in a different order:

In the following structure, X will be allocated as the first byte, Y as the second, Z as the third and fourth and finally B1, BF1 and BF2 will be allocated as the fifth byte.

The rule is that BOOLEAN and BITS fields are packed together at the end of the record.


TYPE
  tMyRecordBits = RECORD
      X: BYTE;      // bits 0..7 of 1st byte
      B1: BOOLEAN// bit 0 of 5th byte
      Y: BYTE;      // 2nd byte
      BF1: BITS[3]; // Bits 1..3 of 5th byte
      BF2: BITS[4]; // Bits 4..7 of 5th byte
      Z: WORD;      // 3rd and 4th byte
  END;