comp.lang.ada
 help / color / mirror / Atom feed
From: stt@dsd.camb.inmet.com (Tucker Taft)
Subject: Re: Flags in Ada?
Date: Fri, 28 Oct 1994 02:41:18 GMT
Date: 1994-10-28T02:41:18+00:00	[thread overview]
Message-ID: <CyD3Gv.Mpw@inmet.camb.inmet.com> (raw)
In-Reply-To: 38ob12$7po@gnat.cs.nyu.edu

In article <38ob12$7po@gnat.cs.nyu.edu>, Robert Dewar <dewar@cs.nyu.edu> wrote:
> ...
>Note that in Ada 9X, yet a third possibility is to use unsigned (modular)
>numbers, which also have logical operations defined, with constant values
>for the bits. This of course is even more low-level (and C-like, although
>in C, bit specs are being more widely used these days), but may be
>appropriate for algorithms where arithmetic and logical operations are
>intermixed in wierd ways.

One advantage of the modular types in Ada 9X is that they are "strong" 
types, just like all Ada numeric types.  This means that you can define
a "mask" for a particular modular type and be sure it is only
used to "and" with values of that type.  For example:

    type Control_Byte is mod 2**8;
    Start_Read_Bit  : constant Control_Byte := 2#1000_0000#;
    Start_Write_Bit : constant Control_Byte := 2#0100_0000#;
    Reset_All       : constant Control_Byte := 2#0000_0001#;

    type Status_Byte is mod 2**8;
    Char_Ready_Bit  : constant Status_Byte := 2#1000_0000#;
    Char_Mask       : constant Status_Byte := 2#0111_1111#;

    type Device_Type is record
        Status : Status_Byte;
        Control : Control_Byte;
    end record;
    pragma Convention(Assembler, Device_Type);

    Console : Device_Type;
    pragma Import(Assembler, Console);
    for Console'Address use 16#FFFF_F200#;
    pragma Volatile(Console);

 begin
   -- Reset console, then initiate read.
    Console.Control := Reset_All;
    Console.Control := Start_Read_Bit;

   -- busy wait until character ready, and then get character
    while (Console.Status and Char_Ready_Bit) = 0 loop
      -- busy wait (could keep a count to avoid an infinite loop)
        null;
    end loop;
    Next_Char := Console.Status and Char_Mask;

   ... -- etc.

Although record rep clauses would work just as well, the above
approach illustrates how modular types can be used, while still
getting a measure of safety.  The compiler will complain if you
use a Status_Byte mask on the Control register, or vice-versa.
Also, Ada's binary notation comes in quite handy for this kind
of bit twiddling.  In some ways using masks defined in binary
notation is more "graphic" than record rep clauses, making them
a little "bit" easier to read. ;-)

-Tucker Taft  stt@inmet.com
Intermetrics, Inc.



  reply	other threads:[~1994-10-28  2:41 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1994-10-27  5:06 Flags in Ada? tmoran
1994-10-27 13:47 ` Robert Dewar
1994-10-28  2:41   ` Tucker Taft [this message]
1994-10-30 13:31     ` Robert Dewar
  -- strict thread matches above, loose matches on Subject: below --
1994-10-28  3:59 tmoran
1994-10-28 13:43 ` Robert Dewar
1994-10-31 14:19   ` Norman H. Cohen
1994-11-02 14:06     ` Mats Weber
1994-11-03 23:08       ` Robert Dewar
1994-11-03 11:26     ` Robert Dewar
1994-10-27  5:05 tmoran
1994-10-27 13:29 ` Robert Dewar
1994-10-27 17:15 ` Norman H. Cohen
1994-10-28  3:51   ` Robert Dewar
1994-10-25 16:22 tmoran
1994-10-25 10:36 Andre Spiegel
1994-10-25 10:07 ` David Emery
1994-10-25 16:19 ` Norman H. Cohen
1994-10-26  3:19   ` tmoran
1994-10-26  9:59     ` David Emery
1994-10-26 22:32       ` Robert Dewar
1994-10-27 13:24         ` Norman H. Cohen
1994-10-27 15:15         ` John Volan
1994-10-31  9:29         ` David Emery
1994-10-27 22:34       ` Henry G. Baker
1994-10-26 14:33     ` Robert Dewar
1994-10-26 17:43     ` Norman H. Cohen
1994-10-26 15:54   ` Andre Spiegel
1994-10-26  0:36 ` Dale Stanbrough
1994-10-26 11:01   ` Robert Dewar
1994-10-27  8:23 ` Henri Altarac
1994-10-27 23:00   ` Robert Dewar
1994-10-31  9:32     ` David Emery
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox