home *** CD-ROM | disk | FTP | other *** search
/ Hacker / Hacker.iso / HACKER / DECOMP / DECAF / basidefi.adb < prev    next >
Encoding:
Text File  |  1996-09-19  |  22.0 KB  |  571 lines

  1. --
  2. -- Copyright (C) 1996 Ada Resource Association (ARA), Columbus, Ohio.
  3. -- Author: Gilles Demailly
  4. --
  5. --
  6. -- Permission to use, copy, modify, and distribute this software and its
  7. -- documentation for any purpose and without fee is hereby granted,
  8. -- provided that the above copyright and authorship notice appear in all
  9. -- copies and that both that copyright notice and this permission notice
  10. -- appear in supporting documentation.
  11. -- 
  12. -- The ARA makes no representations about the suitability of this software
  13. -- for any purpose.  It is provided "as is" without express
  14. -- or implied warranty.
  15. -- 
  16.  
  17. with Ada.Unchecked_Conversion;
  18. with System;
  19. -- the constant Default_Bit_Order provided by package System
  20. -- allow us to code only one body for package Basic_Definitions
  21.  
  22.  
  23. package body Basic_Definitions is
  24.  
  25.    -- Most significant bit for all integer types
  26.    Msb    : constant := 0;
  27.    
  28.    -- Less significant bit for each integer type
  29.    Lsb_8  : constant := 7;
  30.    Lsb_16 : constant := 15;
  31.    Lsb_32 : constant := 31;
  32.    Lsb_64 : constant := 63;
  33.    
  34.    -- index type of types represented by 8, 16, 32 and 64 bits
  35.    -----------------------------------------------------------
  36.    subtype Bits_8_Index  is Natural range Msb .. Lsb_8;
  37.    subtype Bits_16_Index is Natural range Msb .. Lsb_16;
  38.    subtype Bits_32_Index is Natural range Msb .. Lsb_32;
  39.    subtype Bits_64_Index is Natural range Msb .. Lsb_64;
  40.    
  41.    -- boolean arrays types (size 8, 16, 32 and 64) used to 
  42.    -- make logical operations for integer types based
  43.    -- on 8, 16, 32 and 64 bits
  44.    -------------------------------------------------------
  45.    type Bits_8  is array (Bits_8_Index'Range)  of Boolean;
  46.    pragma Pack (Bits_8);
  47.  
  48.    type Bits_16 is array (Bits_16_Index'Range) of Boolean;
  49.    pragma Pack (Bits_16);
  50.  
  51.    type Bits_32 is array (Bits_32_Index'Range) of Boolean;
  52.    pragma Pack (Bits_32);
  53.  
  54.    type Bits_64 is array (Bits_64_Index'Range) of Boolean;
  55.    pragma Pack (Bits_64);
  56.    
  57.    -- subtypes used to limit shift operations on 8, 16, 32 and 64 bits
  58.    -- wide integer types
  59.    -------------------------------------------------------------------
  60.    subtype Allowed_Shift_8  is Natural range 0..Integer_8_Size;
  61.    subtype Allowed_Shift_16 is Natural range 0..Integer_16_Size;
  62.    subtype Allowed_Shift_32 is Natural range 0..Integer_32_Size;
  63.    subtype Allowed_Shift_64 is Natural range 0..Integer_64_Size;
  64.    
  65.    -- null constants used to initialize results
  66.    --------------------------------------------
  67.    Null_Bits_8  : constant Bits_8  := (others => False);
  68.    Null_Bits_16 : constant Bits_16 := (others => False);
  69.    Null_Bits_32 : constant Bits_32 := (others => False);
  70.    Null_Bits_64 : constant Bits_64 := (others => False);
  71.  
  72.  
  73.    -- conversion functions (both sides) between boolean arrays
  74.    -- and integer types represented by 8, 16, 32 and 64 bits
  75.    -----------------------------------------------------------
  76.  
  77.    function To_Bits is new Ada.Unchecked_Conversion (Unsigned_8,  Bits_8);
  78.    function To_Bits is new Ada.Unchecked_Conversion (Unsigned_16, Bits_16);
  79.    function To_Bits is new Ada.Unchecked_Conversion (Unsigned_32, Bits_32);
  80.    function To_Bits is new Ada.Unchecked_Conversion (Unsigned_64, Bits_64);
  81.    
  82.    function To_Unsigned is new Ada.Unchecked_Conversion (Bits_8,  Unsigned_8);
  83.    function To_Unsigned is new Ada.Unchecked_Conversion (Bits_16, Unsigned_16);
  84.    function To_Unsigned is new Ada.Unchecked_Conversion (Bits_32, Unsigned_32);
  85.    function To_Unsigned is new Ada.Unchecked_Conversion (Bits_64, Unsigned_64);
  86.    
  87.  
  88.  
  89.    -- shift functions strategy :
  90.    --  all these functions make first a conversion to a boolean array
  91.    --  then shift boolean values (the order depends on the constant
  92.    --  System.Default_Bit_Order which enable us to provide portable
  93.    --  code) and then make a conversion back to the integer type
  94.  
  95.    function Shift_Left
  96.                (Value  : Unsigned_8;
  97.                 Amount : Natural)
  98.             return Unsigned_8 is
  99.      Shift      : constant Allowed_Shift_8 := Allowed_Shift_8 (Amount);
  100.      Value_Bits : constant Bits_8          := To_Bits (Value);
  101.      Result     : Bits_8                   := Null_Bits_8;
  102.    begin
  103.       if Shift < Integer_8_Size then
  104.          case System.Default_Bit_Order is
  105.             when System.High_Order_First =>
  106.                Result (Msb .. (Lsb_8 - Shift)) := Value_Bits (Shift .. Lsb_8);
  107.             when System.Low_Order_First =>
  108.                Result (Shift .. Lsb_8) := Value_Bits (Msb .. (Lsb_8 - Shift));
  109.          end case;
  110.       end if;
  111.       return To_Unsigned (Result);
  112.    end Shift_Left;
  113.  
  114.    function Shift_Right
  115.                (Value  : Unsigned_8;
  116.                 Amount : Natural)
  117.             return Unsigned_8 is
  118.      Shift      : constant Allowed_Shift_8 := Allowed_Shift_8 (Amount);
  119.      Value_Bits : constant Bits_8          := To_Bits (Value);
  120.      Result     : Bits_8                   := Null_Bits_8;
  121.    begin
  122.       if Shift < Integer_8_Size then
  123.          case System.Default_Bit_Order is
  124.             when System.High_Order_First =>
  125.                Result (Shift .. Lsb_8) := Value_Bits (Msb .. (Lsb_8 - Shift));
  126.             when System.Low_Order_First =>
  127.                Result (Msb .. (Lsb_8 - Shift)) := Value_Bits (Shift .. Lsb_8);
  128.          end case;
  129.       end if;
  130.       return To_Unsigned (Result);
  131.    end Shift_Right;
  132.  
  133.    function Shift_Left
  134.                (Value  : Unsigned_16;
  135.                 Amount : Natural)
  136.             return Unsigned_16 is
  137.      Shift      : constant Allowed_Shift_16 := Allowed_Shift_16 (Amount);
  138.      Value_Bits : constant Bits_16          := To_Bits (Value);
  139.      Result     : Bits_16                   := Null_Bits_16;
  140.    begin
  141.       if Shift < Integer_16_Size then
  142.          case System.Default_Bit_Order is
  143.             when System.High_Order_First =>
  144.                Result (Msb .. (Lsb_16 - Shift)) := Value_Bits (Shift .. Lsb_16);
  145.             when System.Low_Order_First =>
  146.                Result (Shift .. Lsb_16) := Value_Bits (Msb .. (Lsb_16 - Shift));
  147.          end case;
  148.       end if;
  149.       return To_Unsigned (Result);
  150.    end Shift_Left;
  151.  
  152.    function Shift_Right
  153.                (Value  : Unsigned_16;
  154.                 Amount : Natural)
  155.             return Unsigned_16 is
  156.      Shift      : constant Allowed_Shift_16 := Allowed_Shift_16 (Amount);
  157.      Value_Bits : constant Bits_16          := To_Bits (Value);
  158.      Result     : Bits_16                   := Null_Bits_16;
  159.    begin
  160.       if Shift < Integer_16_Size then
  161.          case System.Default_Bit_Order is
  162.             when System.High_Order_First =>
  163.                Result (Shift .. Lsb_16) := Value_Bits (Msb .. (Lsb_16 - Shift));
  164.             when System.Low_Order_First =>
  165.                Result (Msb .. (Lsb_16 - Shift)) := Value_Bits (Shift .. Lsb_16);
  166.          end case;
  167.       end if;
  168.       return To_Unsigned (Result);
  169.    end Shift_Right;
  170.  
  171.    function Shift_Left
  172.                (Value  : Unsigned_32;
  173.                 Amount : Natural)
  174.             return Unsigned_32 is
  175.      Shift      : constant Allowed_Shift_32 := Allowed_Shift_32 (Amount);
  176.      Value_Bits : constant Bits_32          := To_Bits (Value);
  177.      Result     : Bits_32                   := Null_Bits_32;
  178.    begin
  179.       if Shift < Integer_32_Size then
  180.          case System.Default_Bit_Order is
  181.             when System.High_Order_First =>
  182.                Result (Msb .. (Lsb_32 - Shift)) := Value_Bits (Shift .. Lsb_32);
  183.             when System.Low_Order_First =>
  184.                Result (Shift .. Lsb_32) := Value_Bits (Msb .. (Lsb_32 - Shift));
  185.          end case;
  186.       end if;
  187.       return To_Unsigned (Result);
  188.    end Shift_Left;
  189.  
  190.    function Shift_Right
  191.                (Value  : Unsigned_32;
  192.                 Amount : Natural)
  193.             return Unsigned_32 is
  194.      Shift      : constant Allowed_Shift_32 := Allowed_Shift_32 (Amount);
  195.      Value_Bits : constant Bits_32          := To_Bits (Value);
  196.      Result     : Bits_32                   := Null_Bits_32;
  197.    begin
  198.       if Shift < Integer_32_Size then
  199.          case System.Default_Bit_Order is
  200.             when System.High_Order_First =>
  201.                Result (Shift .. Lsb_32) := Value_Bits (Msb .. (Lsb_32 - Shift));
  202.             when System.Low_Order_First =>
  203.                Result (Msb .. (Lsb_32 - Shift)) := Value_Bits (Shift .. Lsb_32);
  204.          end case;
  205.       end if;
  206.       return To_Unsigned (Result);
  207.    end Shift_Right;
  208.  
  209.    function Shift_Left
  210.                (Value  : Unsigned_64;
  211.                 Amount : Natural)
  212.             return Unsigned_64 is
  213.      Shift      : constant Allowed_Shift_64 := Allowed_Shift_64 (Amount);
  214.      Value_Bits : constant Bits_64          := To_Bits (Value);
  215.      Result     : Bits_64                   := Null_Bits_64;
  216.    begin
  217.       if Shift < Integer_64_Size then
  218.          case System.Default_Bit_Order is
  219.             when System.High_Order_First =>
  220.                Result (Msb .. (Lsb_64 - Shift)) := Value_Bits (Shift .. Lsb_64);
  221.             when System.Low_Order_First =>
  222.                Result (Shift .. Lsb_64) := Value_Bits (Msb .. (Lsb_64 - Shift));
  223.          end case;
  224.       end if;
  225.       return To_Unsigned (Result);
  226.    end Shift_Left;
  227.  
  228.    function Shift_Right
  229.                (Value  : Unsigned_64;
  230.                 Amount : Natural)
  231.             return Unsigned_64 is
  232.      Shift      : constant Allowed_Shift_64 := Allowed_Shift_64 (Amount);
  233.      Value_Bits : constant Bits_64          := To_Bits (Value);
  234.      Result     : Bits_64                   := Null_Bits_64;
  235.    begin
  236.       if Shift < Integer_64_Size then
  237.          case System.Default_Bit_Order is
  238.             when System.High_Order_First =>
  239.                Result (Shift .. Lsb_64) := Value_Bits (Msb .. (Lsb_64 - Shift));
  240.             when System.Low_Order_First =>
  241.                Result (Msb .. (Lsb_64 - Shift)) := Value_Bits (Shift .. Lsb_64);
  242.          end case;
  243.       end if;
  244.       return To_Unsigned (Result);
  245.    end Shift_Right;
  246.  
  247.    --
  248.    -- "and", "or", "not" functions strategy :
  249.    --  all these functions make first a conversion of the argument(s)
  250.    --  to boolean array(s) to apply the logical operation 
  251.    --  and then make a conversion back to the integer type
  252.    --
  253.  
  254.    function "and" (Left, Right : Unsigned_8)  return Unsigned_8 is
  255.    begin
  256.       return To_Unsigned (To_Bits (Left) and To_Bits (Right));
  257.    end "and";
  258.  
  259.    function "and" (Left, Right : Unsigned_16) return Unsigned_16 is
  260.    begin
  261.       return To_Unsigned (To_Bits (Left) and To_Bits (Right));
  262.    end "and";
  263.  
  264.    function "and" (Left, Right : Unsigned_32) return Unsigned_32 is
  265.    begin
  266.       return To_Unsigned (To_Bits (Left) and To_Bits (Right));
  267.    end "and";
  268.  
  269.    function "and" (Left, Right : Unsigned_64) return Unsigned_64 is
  270.    begin
  271.       return To_Unsigned (To_Bits (Left) and To_Bits (Right));
  272.    end "and";
  273.    
  274.    function "or" (Left, Right : Unsigned_8)  return Unsigned_8 is
  275.    begin
  276.       return To_Unsigned (To_Bits (Left) or To_Bits (Right));
  277.    end "or";
  278.  
  279.    function "or" (Left, Right : Unsigned_16) return Unsigned_16 is
  280.    begin
  281.       return To_Unsigned (To_Bits (Left) or To_Bits (Right));
  282.    end "or";
  283.  
  284.    function "or" (Left, Right : Unsigned_32) return Unsigned_32 is
  285.    begin
  286.       return To_Unsigned (To_Bits (Left) or To_Bits (Right));
  287.    end "or";
  288.  
  289.    function "or" (Left, Right : Unsigned_64) return Unsigned_64 is
  290.    begin
  291.       return To_Unsigned (To_Bits (Left) or To_Bits (Right));
  292.    end "or";
  293.    
  294.    function "xor" (Left, Right : Unsigned_8)  return Unsigned_8 is
  295.    begin
  296.       return To_Unsigned (To_Bits (Left) xor To_Bits (Right));
  297.    end "xor";
  298.  
  299.    function "xor" (Left, Right : Unsigned_16) return Unsigned_16 is
  300.    begin
  301.       return To_Unsigned (To_Bits (Left) xor To_Bits (Right));
  302.    end "xor";
  303.  
  304.    function "xor" (Left, Right : Unsigned_32) return Unsigned_32 is
  305.    begin
  306.       return To_Unsigned (To_Bits (Left) xor To_Bits (Right));
  307.    end "xor";
  308.  
  309.    function "xor" (Left, Right : Unsigned_64) return Unsigned_64 is
  310.    begin
  311.       return To_Unsigned (To_Bits (Left) xor To_Bits (Right));
  312.    end "xor";
  313.    
  314.    function "not" (Right : Unsigned_8)  return Unsigned_8 is
  315.    begin
  316.       return To_Unsigned (not To_Bits (Right));
  317.    end "not";
  318.  
  319.    function "not" (Right : Unsigned_16) return Unsigned_16 is
  320.    begin
  321.       return To_Unsigned (not To_Bits (Right));
  322.    end "not";
  323.  
  324.    function "not" (Right : Unsigned_32) return Unsigned_32 is
  325.    begin
  326.       return To_Unsigned (not To_Bits (Right));
  327.    end "not";
  328.  
  329.    function "not" (Right : Unsigned_64) return Unsigned_64 is
  330.    begin
  331.       return To_Unsigned (not To_Bits (Right));
  332.    end "not";
  333.  
  334.    --
  335.    -- bit_at, Extract_Bits, Replace_Bits functions strategy :
  336.    --  all these functions make first a conversion to a boolean array
  337.    --  then perform the operation on the boolean values (the order 
  338.    --  depends on the constant System.Default_Bit_Order which enable us 
  339.    --  to provide portable code) and then make a conversion back 
  340.    --  to the integer type
  341.    --
  342.  
  343.    function Bit_At (Value : Unsigned_8;  Index : Natural) return Boolean is
  344.       Value_Bits : constant Bits_8       := To_Bits (Value);
  345.       Real_Index : constant Bits_8_Index := Bits_8_Index (Index);
  346.    begin
  347.       case System.Default_Bit_Order is
  348.          when System.High_Order_First =>
  349.             return Value_Bits (Real_Index);
  350.          when System.Low_Order_First =>
  351.             return Value_Bits (Lsb_8 - Real_Index);
  352.       end case;
  353.    end Bit_At;
  354.    
  355.    function Bit_At (Value : Unsigned_16; Index : Natural) return Boolean is
  356.       Value_Bits : constant Bits_16       := To_Bits (Value);
  357.       Real_Index : constant Bits_16_Index := Bits_16_Index (Index);
  358.    begin
  359.       case System.Default_Bit_Order is
  360.          when System.High_Order_First =>
  361.             return Value_Bits (Real_Index);
  362.          when System.Low_Order_First =>
  363.             return Value_Bits (Lsb_16 - Real_Index);
  364.       end case;
  365.    end Bit_At;
  366.  
  367.    function Bit_At (Value : Unsigned_32; Index : Natural) return Boolean is
  368.       Value_Bits : constant Bits_32       := To_Bits (Value);
  369.       Real_Index : constant Bits_32_Index := Bits_32_Index (Index);
  370.    begin
  371.       case System.Default_Bit_Order is
  372.          when System.High_Order_First =>
  373.             return Value_Bits (Real_Index);
  374.          when System.Low_Order_First =>
  375.             return Value_Bits (Lsb_32 - Real_Index);
  376.       end case;
  377.    end Bit_At;
  378.  
  379.    function Bit_At (Value : Unsigned_64; Index : Natural) return Boolean is
  380.       Value_Bits : constant Bits_64       := To_Bits (Value);
  381.       Real_Index : constant Bits_64_Index := Bits_64_Index (Index);
  382.    begin
  383.       case System.Default_Bit_Order is
  384.          when System.High_Order_First =>
  385.             return Value_Bits (Real_Index);
  386.          when System.Low_Order_First =>
  387.             return Value_Bits (Lsb_64 - Real_Index);
  388.       end case;
  389.    end Bit_At;
  390.    
  391.    function Extract_Bits
  392.                (Value  : Unsigned_8;
  393.                 Start  : Natural;
  394.                 Amount : Natural)
  395.             return Unsigned_8 is
  396.       Value_Bits : constant Bits_8       := To_Bits (Value);
  397.       Real_Start : constant Bits_8_Index := Bits_8_Index (Start);
  398.       Real_Last  : constant Bits_8_Index := Bits_8_Index (Start + Amount -1);
  399.       Res_Start  : constant Bits_8_Index := Bits_8_Index (Lsb_8 - Amount +1);
  400.       Result     : Bits_8                := Null_Bits_8;
  401.    begin
  402.       case System.Default_Bit_Order is
  403.          when System.High_Order_First =>
  404.             Result (Res_Start .. Lsb_8) := Value_Bits (Real_Start .. Real_Last);
  405.          when System.Low_Order_First =>
  406.             Result (0 .. Lsb_8 - Res_Start) := 
  407.               Value_Bits (Lsb_8 - Real_Last .. Lsb_8 - Real_Start);
  408.       end case;
  409.       return To_Unsigned (Result);
  410.    end Extract_Bits;
  411.    
  412.    function Extract_Bits
  413.                (Value  : Unsigned_16;
  414.                 Start  : Natural;
  415.                 Amount : Natural)
  416.             return Unsigned_16 is
  417.       Value_Bits : constant Bits_16       := To_Bits (Value);
  418.       Real_Start : constant Bits_16_Index := Bits_16_Index (Start);
  419.       Real_Last  : constant Bits_16_Index := Bits_16_Index (Start + Amount -1);
  420.       Res_Start  : constant Bits_16_Index := Bits_16_Index (Lsb_16 - Amount +1);
  421.       Result     : Bits_16                := Null_Bits_16;
  422.    begin
  423.       case System.Default_Bit_Order is
  424.          when System.High_Order_First =>
  425.             Result (Res_Start .. Lsb_16) := 
  426.               Value_Bits (Real_Start .. Real_Last);
  427.          when System.Low_Order_First =>
  428.             Result (0 .. Lsb_16 - Res_Start) := 
  429.               Value_Bits (Lsb_16 - Real_Last .. Lsb_16 - Real_Start);
  430.       end case;
  431.       return To_Unsigned (Result);
  432.    end Extract_Bits;
  433.    
  434.    function Extract_Bits
  435.                (Value  : Unsigned_32;
  436.                 Start  : Natural;
  437.                 Amount : Natural)
  438.             return Unsigned_32 is
  439.       Value_Bits : constant Bits_32       := To_Bits (Value);
  440.       Real_Start : constant Bits_32_Index := Bits_32_Index (Start);
  441.       Real_Last  : constant Bits_32_Index := Bits_32_Index (Start + Amount -1);
  442.       Res_Start  : constant Bits_32_Index := Bits_32_Index (Lsb_32 - Amount +1);
  443.       Result     : Bits_32                := Null_Bits_32;
  444.    begin
  445.       case System.Default_Bit_Order is
  446.          when System.High_Order_First =>
  447.             Result (Res_Start .. Lsb_32) := 
  448.               Value_Bits (Real_Start .. Real_Last);
  449.          when System.Low_Order_First =>
  450.             Result (0 .. Lsb_32 - Res_Start) := 
  451.               Value_Bits (Lsb_32 - Real_Last .. Lsb_32 - Real_Start);
  452.       end case;
  453.       return To_Unsigned (Result);
  454.    end Extract_Bits;
  455.    
  456.    function Extract_Bits
  457.                (Value  : Unsigned_64;
  458.                 Start  : Natural;
  459.                 Amount : Natural)
  460.             return Unsigned_64 is
  461.       Value_Bits : constant Bits_64       := To_Bits (Value);
  462.       Real_Start : constant Bits_64_Index := Bits_64_Index (Start);
  463.       Real_Last  : constant Bits_64_Index := Bits_64_Index (Start + Amount -1);
  464.       Res_Start  : constant Bits_64_Index := Bits_64_Index (Lsb_64 - Amount +1);
  465.       Result     : Bits_64                := Null_Bits_64;
  466.    begin
  467.       case System.Default_Bit_Order is
  468.          when System.High_Order_First =>
  469.             Result (Res_Start .. Lsb_64) := 
  470.               Value_Bits (Real_Start .. Real_Last);
  471.          when System.Low_Order_First =>
  472.             Result (0 .. Lsb_64 - Res_Start) := 
  473.               Value_Bits (Lsb_64 - Real_Last .. Lsb_64 - Real_Start);
  474.       end case;
  475.       return To_Unsigned (Result);
  476.    end Extract_Bits;
  477.    
  478.    function Replace_Bits
  479.                (Value  : Unsigned_8;
  480.                 Start  : Natural;
  481.                 Amount : Natural;
  482.                 Source : Unsigned_8)
  483.             return Unsigned_8 is
  484.       Value_Bits : constant Bits_8       := To_Bits (Value);
  485.       Src_Bits   : constant Bits_8       := To_Bits (Source);
  486.       Real_Start : constant Bits_8_Index := Bits_8_Index (Start);
  487.       Real_Last  : constant Bits_8_Index := Bits_8_Index (Start + Amount -1);
  488.       Src_Start  : constant Bits_8_Index := Bits_8_Index (Lsb_8 - Amount +1);
  489.       Result     : Bits_8                := Value_Bits;
  490.    begin
  491.       case System.Default_Bit_Order is
  492.          when System.High_Order_First =>
  493.             Result (Real_Start .. Real_Last) := Src_Bits (Src_Start .. Lsb_8);
  494.          when System.Low_Order_First =>
  495.             Result (Lsb_8 - Real_Last .. Lsb_8 - Real_Start) := 
  496.               Src_Bits (0 .. Lsb_8 - Src_Start);
  497.       end case;
  498.       return To_Unsigned (Result);
  499.    end Replace_Bits;
  500.    
  501.    function Replace_Bits
  502.                (Value  : Unsigned_16;
  503.                 Start  : Natural;
  504.                 Amount : Natural;
  505.                 Source : Unsigned_16)
  506.             return Unsigned_16 is
  507.       Value_Bits : constant Bits_16       := To_Bits (Value);
  508.       Src_Bits   : constant Bits_16       := To_Bits (Source);
  509.       Real_Start : constant Bits_16_Index := Bits_16_Index (Start);
  510.       Real_Last  : constant Bits_16_Index := Bits_16_Index (Start + Amount -1);
  511.       Src_Start  : constant Bits_16_Index := Bits_16_Index (Lsb_16 - Amount +1);
  512.       Result     : Bits_16                := Value_Bits;
  513.    begin
  514.       case System.Default_Bit_Order is
  515.          when System.High_Order_First =>
  516.             Result (Real_Start .. Real_Last) := Src_Bits (Src_Start .. Lsb_16);
  517.          when System.Low_Order_First =>
  518.             Result (Lsb_16 - Real_Last .. Lsb_16 - Real_Start) := 
  519.               Src_Bits (0 .. Lsb_16 - Src_Start);
  520.       end case;
  521.       return To_Unsigned (Result);
  522.    end Replace_Bits;
  523.    
  524.    function Replace_Bits
  525.                (Value  : Unsigned_32;
  526.                 Start  : Natural;
  527.                 Amount : Natural;
  528.                 Source : Unsigned_32)
  529.             return Unsigned_32 is
  530.       Value_Bits : constant Bits_32       := To_Bits (Value);
  531.       Src_Bits   : constant Bits_32       := To_Bits (Source);
  532.       Real_Start : constant Bits_32_Index := Bits_32_Index (Start);
  533.       Real_Last  : constant Bits_32_Index := Bits_32_Index (Start + Amount -1);
  534.       Src_Start  : constant Bits_32_Index := Bits_32_Index (Lsb_32 - Amount +1);
  535.       Result     : Bits_32                := Value_Bits;
  536.    begin
  537.       case System.Default_Bit_Order is
  538.          when System.High_Order_First =>
  539.             Result (Real_Start .. Real_Last) := Src_Bits (Src_Start .. Lsb_32);
  540.          when System.Low_Order_First =>
  541.             Result (Lsb_32 - Real_Last .. Lsb_32 - Real_Start) := 
  542.               Src_Bits (0 .. Lsb_32 - Src_Start);
  543.       end case;
  544.       return To_Unsigned (Result);
  545.    end Replace_Bits;
  546.    
  547.    function Replace_Bits
  548.                (Value  : Unsigned_64;
  549.                 Start  : Natural;
  550.                 Amount : Natural;
  551.                 Source : Unsigned_64)
  552.             return Unsigned_64 is
  553.       Value_Bits : constant Bits_64       := To_Bits (Value);
  554.       Src_Bits   : constant Bits_64       := To_Bits (Source);
  555.       Real_Start : constant Bits_64_Index := Bits_64_Index (Start);
  556.       Real_Last  : constant Bits_64_Index := Bits_64_Index (Start + Amount -1);
  557.       Src_Start  : constant Bits_64_Index := Bits_64_Index (Lsb_64 - Amount +1);
  558.       Result     : Bits_64                := Value_Bits;
  559.    begin
  560.       case System.Default_Bit_Order is
  561.          when System.High_Order_First =>
  562.             Result (Real_Start .. Real_Last) := Src_Bits (Src_Start .. Lsb_64);
  563.          when System.Low_Order_First =>
  564.             Result (Lsb_64 - Real_Last .. Lsb_64 - Real_Start) := 
  565.               Src_Bits (0 .. Lsb_64 - Src_Start);
  566.       end case;
  567.       return To_Unsigned (Result);
  568.    end Replace_Bits;
  569.       
  570. end Basic_Definitions;
  571.