--  Record representation clauses (D196): the one layout for storage, 'Size, the
--  representation attributes and Unchecked_Conversion; bit fields read and written in place
--  (an unsigned field's top bit is not a sign), from a record to a scalar and back, between
--  two layouts, and a natural layout's sizes; fixed point's bits are its count of smalls.
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Unchecked_Conversion;
with Interfaces; use Interfaces;
procedure Main is
   type Mode is (Off, Slow, Fast, Turbo);
   type Control is record
      Enable : Boolean;
      Speed  : Mode;
      Count  : Integer range 0 .. 31;
      Level  : Integer range -8 .. 7;
   end record with Size => 16;
   for Control use record
      Enable at 0 range 0 .. 0;
      Speed  at 0 range 1 .. 2;
      Count  at 0 range 3 .. 7;
      Level  at 1 range 0 .. 3;
   end record;
   type Pair is record
      Lo, Hi : Unsigned_16;
   end record;
   type Money is delta 0.01 digits 10;
   type Cents is range -2 ** 63 .. 2 ** 63 - 1;
   function To_Bits is new Ada.Unchecked_Conversion (Control, Unsigned_16);
   function From_Bits is new Ada.Unchecked_Conversion (Unsigned_16, Control);
   function To_Word is new Ada.Unchecked_Conversion (Pair, Unsigned_32);
   function To_Cents is new Ada.Unchecked_Conversion (Money, Cents);
   C : Control := (Enable => True, Speed => Fast, Count => 5, Level => -3);
   D : Control;
   P : constant Pair := (Lo => 1, Hi => 2);
begin
   Put_Line (Unsigned_16'Image (To_Bits (C)));
   C.Count := 31;
   C.Level := 7;
   C.Enable := False;
   Put_Line (Unsigned_16'Image (To_Bits (C)) & Integer'Image (C.Count) & Integer'Image (C.Level) & " " & Mode'Image (C.Speed) & " " & Boolean'Image (C.Enable));
   D := From_Bits (16#0F_2B#);
   Put_Line (Boolean'Image (D.Enable) & " " & Mode'Image (D.Speed) & Integer'Image (D.Count) & Integer'Image (D.Level) & " " & Boolean'Image (D = C));
   Put_Line (Integer'Image (Control'Size) & Integer'Image (C'Size) & Integer'Image (C.Level'Position) & Integer'Image (C.Level'First_Bit) & Integer'Image (C.Level'Last_Bit) & Integer'Image (C.Count'First_Bit));
   Put_Line (Unsigned_32'Image (To_Word (P)) & Integer'Image (Pair'Size) & Integer'Image (P.Hi'Position) & Integer'Image (Pair'Alignment) & Cents'Image (To_Cents (1.25)));
end Main;
