Bitfields

The Compiler allows a maximum bitfield width of 16 bits, with byte-size allocation units. The Compiler uses words only when a bitfield exceeds eight bits, or when using bytes causes more than two bits to be left unused. Allo cation order is from the least significant bit up to the most significant bit in the order of declaration. The following figure illustrates an allocation scheme.

Figure 1. Allocation of Six Bitfields

Allocation of Six Bitfields

The following example demonstrates a simple C code source with various ways to access bitfields, together with the produced code:

Listing: Demonstration of Bitfield Instructions for the HC(S)08 Compiler
#pragma DATA_SEG __SHORT_SEG _zpage /* place following variables into
 zero page */

#define BIT_SET(x,bitNo) ((x) |= 1<<(bitNo))

#define BIT_CLR(x,bitNo) ((x) &= ~(1<<(bitNo)))

char i, j;

struct {

  unsigned int b0:1;

  unsigned int b1:1;

} B;

void main(void) {

  /* demo using BSET/BCLR/BRSET/BRCLR */

  if (i&1) { /* BRCLR */

    /* BCLR: clearing a bit */

    i &= ~1;        /* using normal ANSI-C */

    BIT_CLR(j,0);   /* using a macro */

    __asm BCLR 0,i  /* using inline assembly */

  }

  if ((i&1) == 0) { /* BRSET */

    /* BSET: setting a bit */

    i |= 1;         /* using normal ANSI-C */

    BIT_SET(j,0);   /* using a macro */

    __asm BCLR 0,j  /* using inline assembly */

  }

  if (i&4) { /* BRCLR */

    i &= ~4; /* BCLR */

  }

  if ((i&4) == 4) { /* BRSET */

    i |= 4; /* BSET */

  }

  /* demo using bitfields in ANSI-C (warning: not portable, depends on 
     the compiler

     how bitfields are allocated! */

  if (B.b0) {

    B.b1 = 1;

  } else if (B.b1) {

    B.b0 = 0;

  }

}

#if 0

   13:  void main(void) {

   14:    /* demo using BSET/BCLR/BRSET/BRCLR */

   15:    if (i&1) { /* BRCLR */

   16:      /* BCLR: clearing a bit */

00000000 010006           BRCLR  0,i,*6       /abs = 0009

   17:      i &= ~1;        /* using normal ANSI-C */

00000003 1100             BCLR   0,i

   18:      BIT_CLR(j,0);   /* using a macro */

00000005 1100             BCLR   0,j

   19:      __asm BCLR 0,i    /* using inline assembly */

   20:    }

00000007 1100             BCLR   0,i

   21:    if ((i&1) == 0) { /* BRSET */

   22:      /* BSET: setting a bit */

00000009 000006           BRSET  0,i,*6       /abs = 0012

   23:      i |= 1;         /* using normal ANSI-C */

0000000C 1000             BSET   0,i

   24:      BIT_SET(j,0);   /* using a macro */

0000000E 1000             BSET   0,j

   25:    __asm BCLR 0,j    /* using inline assembly */

   26:    }

00000010 1100             BCLR   0,j

   27:    if (i&4) { /* BRCLR */

00000012 050002           BRCLR  2,i,*2       /abs = 0017

   28:      i &= ~4; /* BCLR */

   29:    }

00000015 1500             BCLR   2,i

   30:    if ((i&4) == 4) { /* BRSET */

00000017 050002           BRCLR  2,i,*2       /abs = 001C

   31:      i |= 4; /* BSET */

   32:    }

   33:    /* demo using bitfields in ANSI-C (warning: not portable,

            depends on the compiler

   34:      how bitfields are allocated! */

0000001A 1400             BSET   2,i

   35:    if (B.b0) {

0000001C 010003           BRCLR  0,B,*3       /abs = 0022

   36:      B.b1 = 1;

0000001F 1200             BSET   1,B

00000021 81               RTS

   37:    } else if (B.b1) {

00000022 0300FC           BRCLR  1,B,*-4      /abs = 0021

   38:      B.b0 = 0;

   39:    }

00000025 1100             BCLR   0,B

   40:  }

00000027 81               RTS

#endif