comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam@spam.com>
Subject: Re: A question re meaning/use of the "for ... use ..."
Date: Sun, 05 Dec 2004 16:52:55 GMT
Date: 2004-12-05T16:52:55+00:00	[thread overview]
Message-ID: <H1Hsd.1987$yr1.256@newsread3.news.pas.earthlink.net> (raw)
In-Reply-To: <mailman.173.1102262931.10401.comp.lang.ada@ada-france.org>

Stephen Leake wrote:

> There is no standard attribute that returns the internal
> representation specified by the enumeration representation clause.
> However, GNAT provides the non-standard 'Enum_Rep for this purpose.

I have no problem with 'Pos returning the abstract position number, but 
do think something like GNAT's 'Enum_Rep should be standard, along with 
a conversion the other way, equivalent to 'Val ('Enum_Val?).

For the OP, an enum rep is often used for interfacing to H/W, along with 
what is known as "change of representation". Consider a memory-mapped 
sensor. The application may interface to it via a package:

package Sensor_IF is
    type Raw_Sensor_Value is (Not_Ready, Empty, Normal, Full, Overflow);
    subtype Sensor_Value is Raw_Sensor_Value range Empty .. Overflow;

    Hardware_Failure : exception;

    function Read return Sensor_Value;
end Sensor_IF;

The H/W memory location uses 4 bits, with the following interpretation:

2#0000# = Not ready
2#0001# = Empty
2#0010# = Normal
2#0100# = Full
2#1000# = Overflow

So the implementation of the package might contain something like:

package body Sensor_IF is
    type HW_Value is new Raw_Sensor_Value;
    for HW_Value use (Not_Ready => 2#0000#,
                      Empty     => 2#0001#,
                      Normal    => 2#0010#,
                      Full      => 2#0100#,
                      Overflow  => 2#1000#);

    Register_Address : constant System.Address := ...;

    -- This is a pedagogical example and ignores many of the complexities
    -- of real systems of this type, which have to deal with values like
    -- 2#0101# and MSBs that have to be masked off. Often readings have
    -- to be triggered by writing to a control register, and there are
    -- timing issues ...

    Register : HW_Value;
    for Register'Address use Register_Address;
    pragma Volatile (Register);

    function Read return Sensor_Value is
       Max_Tries : constant := ...;

       Result : HW_Value;
    begin -- Read
       Try : for I in 1 .. Max_Tries loop
          Result := Register;

          if Result /= Not_Ready then
             return Sensor_Value (Result);
          end if;
       end loop Try;

       raise Hardware_Failure;
    end Read;
end Sensor_IF;

-- 
Jeff Carter
"We burst our pimples at you."
Monty Python & the Holy Grail
16




  reply	other threads:[~2004-12-05 16:52 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-05 15:27 A question re meaning/use of the "for ... use ..." Erik J Pessers
2004-12-05 15:47 ` Martin Krischik
2004-12-05 15:59 ` Stephen Leake
2004-12-05 16:52   ` Jeffrey Carter [this message]
2004-12-06 19:59     ` Randy Brukardt
2004-12-07  1:36       ` Jeffrey Carter
2004-12-07  2:40         ` David C. Hoos, Sr.
2004-12-07 20:59         ` Randy Brukardt
2004-12-08  1:41           ` Jeffrey Carter
2004-12-08  8:40           ` Martin Dowie
2004-12-08 16:23             ` Georg Bauhaus
2004-12-08  3:18       ` Keith Thompson
2004-12-08 13:48         ` David C. Hoos
2004-12-08 19:50         ` Randy Brukardt
2004-12-08 23:00           ` Keith Thompson
2004-12-09 23:06             ` Randy Brukardt
2004-12-10  2:26               ` Keith Thompson
2004-12-10 19:42                 ` Randy Brukardt
2004-12-10 21:18                   ` Keith Thompson
2004-12-11  0:53                     ` Keith Thompson
2004-12-10  3:13             ` David C. Hoos
2004-12-10  9:23               ` Keith Thompson
2004-12-10 13:24                 ` David C. Hoos
replies disabled

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