Search code examples
c8051

Is there a way to address a single bit in C?


I've done some researching on bit addressable microcontrollers. The only one that came across in my path is the Intel MCS-51(wiki page) which is still used very commonly today... I was wondering if you can directly address a bit in C, for example on the SFR region wiki 8051 memory architecture.

The bits that I address in the SFR, are they bit addressed directly, or is it a bitwise operation in a bitfield that is byte addressed or is it something else completely?

Specifically, from here: Check if a single bit is set, it looks like the bit is directly manipulated with a MOV, I'm wondering if this is possible in C (with extensions) or does it only look like bitwise operation, but in the background there are some compiler stuff that uses bytes only?

As a follow up question, are there any bit addressable modern processors?


Solution

  • This is not uncommon. For example, SDCC (Small Device C Compiler) is a popular compiler for MCS-51. You'll find the manual here. The most relevant section is 3.4.1, it describes the language extensions for MCS-51:

    3.4.1.6 __bit

    This is a data-type and a storage class specifier. When a variable is declared as a bit, it is allocated into the bit addressable memory of 8051, e.g.:

     __bit test_bit;
    

    Writing 1 to this variable generates the assembly code:

     D2*00 setb _test_bit
    

    The bit addressable memory consists of 128 bits which are located from 0x20 to 0x2f in data memory. Apart from this 8051 specific storage class most architectures support ANSI-C bitfields4. In accordance with ISO/IEC 9899 bits and bitfields without an explicit signed modifier are implemented as unsigned.