TEAM-ADA Archives

Team Ada: Ada Programming Language Advocacy


Options: Use Classic View

Use Monospaced Font
Show Text Part by Default
Condense Mail Headers

Topic: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Sender: "Team Ada: Ada Advocacy Issues (83 & 95)" <[log in to unmask]>
X-To: Carlos Luque Dengra <[log in to unmask]>
Date: Mon, 15 Apr 2002 15:30:43 -0500
Reply-To: "David C. Hoos" <[log in to unmask]>
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset="iso-8859-1"
From: "David C. Hoos" <[log in to unmask]>
Parts/Attachments: text/plain (132 lines)
Is this elegant enough?

with Ada.Text_IO;
with Ada.Unchecked_Conversion;
procedure Test_Register_Bits
   type Register_Bits_Type is array (Natural range 0 .. 7) of Boolean;
   pragma Pack (Register_Bits_Type);

   type Octet_Type is mod 256;

   function To_Bits is new Ada.Unchecked_Conversion
     (Source => Octet_Type,
      Target => Register_Bits_Type);

   function To_Octet is new Ada.Unchecked_Conversion
     (Source => Register_Bits_Type,
      Target => Octet_Type);

   Register_Bits : Register_Bits_Type;
   Octet         : Octet_Type;

   -- Set all of the odd-numbered bits true, even-numbered bits false;
   for B in Register_Bits'Range loop
      Register_Bits (B) := B mod 2 = 1;
   end loop;

   -- Copy the register bits to an octet;
   Octet := To_Octet (Register_Bits);

   -- See what we have;
   for B in Register_Bits'range loop
        ("Bit" & Natural'Image (B) & " is on? " &
         Boolean'Image (Register_Bits (B)));
   end loop;
     ("Register contents:" & Octet_Type'Image (Octet));

   -- Now, set the octet value to 96
   Octet := 96;

   -- Copy the octet to the register bits
   Register_Bits := To_Bits (Octet);
   -- See what we have;
   for B in Register_Bits'range loop
        ("Bit" & Natural'Image (B) & " is on? " &
         Boolean'Image (Register_Bits (B)));
   end loop;
     ("Register contents:" & Octet_Type'Image (Octet));

end Test_Register_Bits;

To "Mask" modular types, simply use the and, or, and xor
operators on operands of the same type.

----- Original Message -----
From: "Carlos Luque Dengra" <[log in to unmask]>
To: <[log in to unmask]>
Sent: Monday, April 15, 2002 1:11 AM
Subject: Some questions on record attributes

for the development for the port parallel interface, I've planned to map the
unsigned_8 value to a record of bits of type (on,off).
Do you know how to mask an integer or unsigned_8?

As far as I haven't found an ellgant way I've done this:

I've the next definitions:

type flag is (on, off);

 type Registro_Byte is
         Bit7 : Flag;
      end record;

data : registro_byte;
register : unsigned_8;

(...) I load an 8-bit integer into the register:

register:= 128; (this is done via DOSPORTS libraries)

I want data to be a map of the register bit:

    if Integer (Shift_Right (register , 0) mod 2 ) = 1 then
         data.Bit0 := On;
         data.Bit0 := Off;
      end if;

    if Integer (Shift_Right (register , 1) mod 2 ) = 1 then
         data.Bit1 := On;
         data.Bit1 := Off;
      end if;

and so on 'til BIT7.

Do you know how to assign the values using a loop?

I don't know how to do a loop that works like the next false one:

for ix in 0..7 loop
  if Integer (Shift_Right (register , ix ) mod 2 ) = 1 then
         data.BIT(ix) := On; -- wrong line
         data.Bit(ix) := Off; -- wrong line
      end if;
end loop;

Thank you in advance.