New standard procedure BITS

Topics related to the use of Oberon language features
Locked
cfbsoftware
Site Admin
Posts: 493
Joined: Fri Dec 31, 2010 12:30 pm
Contact:

New standard procedure BITS

Post by cfbsoftware » Sun Jun 03, 2012 12:07 am

Release v4.2 of Astrobe for Cortex-M3 has a new Oberon language extension - the built-in procedure BITS:

Code: Select all

PROCEDURE BITS(i: INTEGER): SET;
BITS is a feature of the language Component Pascal (i.e. Oberon-2+) which we use to develop the Astrobe Oberon compiler. BITS takes an INTEGER parameter and returns a SET with the same bit pattern. It is a type transfer procedure like ORD rather than a type conversion procedure. By definition, the following expressions, where i is an INTEGER and s is a SET, are TRUE:

Code: Select all

  BITS(i) = SYSTEM.VAL(SET, i)
  ORD(BITS(i)) = i
  BITS(ORD(s)) = s
BITS is convenient to use in expressions which are a mixture of INTEGERs, masks and bit fields.

For example, in the Astrobe library procedure Serial.SetFormat, to modify the wordlength in the ULCR register we have to clear the six least significant bits of ULCR and replace them with the integer value: (wordlength - 5). In earlier versions of Astrobe, one way to do this would be to write:

Code: Select all

PROCEDURE SetFormat*(wordLength, stopBits: INTEGER; parity: SET);
VAR
  ULCR: SET;
BEGIN
  SYSTEM.GET(MCU.U0LCR, ULCR);
  ULCR := ULCR - {0..5} + parity + SYSTEM.VAL(SET, (wordLength - 5));
 
In v4.2 this last statement simplifies to:

Code: Select all

  ULCR := ULCR - {0..5} + parity + BITS(wordLength - 5);
Note that SYSTEM.VAL can still be used if you prefer.

The following examples show the use of BITS with a constant value and the equivalent SET constants:

Code: Select all

  BITS(0) = {}
  BITS(1) = {0}
  BITS(3) = {0, 1}
  BITS(0FFFFFFFFH) = {0..31}

Locked