package functions is type int_arr is array(Integer range<>) of INTEGER; type bit_arr is array(Integer range<>) of Bit; function int_Wired_OR (input : int_arr) return INTEGER; function bit_Wired_OR (input : bit_arr) return Bit; function bit_Wired_AND (input : bit_arr) return Bit; function bits_to_int (input : Bit_Vector) return INTEGER ; function twocomp_bits_to_int (input : Bit_Vector) return INTEGER ; function INT_TO_BIN32 (INPUT: in INTEGER) return BIT_VECTOR; function INT_TO_BIN25 (INPUT: in INTEGER) return BIT_VECTOR; function INT_TO_BIN16 (INPUT: in INTEGER) return BIT_VECTOR; function twocomp_INT_TO_BIN16 (INPUT: in INTEGER) return BIT_VECTOR; function int_to_bin4 (input : in integer) return Bit_Vector; function int_to_bin8 (input : in integer) return Bit_Vector; function twocomp_int_to_bin8 (input : in integer) return Bit_Vector; function "+" (Left, Right : Bit_Vector) return Bit_Vector; function "+" (Left : Bit_Vector; Right : Integer) return Bit_Vector; function "+" (Left : Integer; Right : Bit_Vector) return Bit_Vector; function "+" (Left : Bit_Vector; Right : Bit) return Bit_Vector; function "*" (Left, Right : Bit_Vector) return Bit_Vector; function "*" (Left : Bit_Vector; Right : Integer) return Bit_Vector; function "*" (Left : Integer; Right : Bit_Vector) return Integer; function "-" (Left, Right : Bit_Vector) return Bit_Vector; function "-" (Left : Bit_Vector; Right : Integer) return Bit_Vector; function "-" (Left : Integer; Right : Bit_Vector) return Bit_Vector; function "/" (Left, Right : Bit_Vector) return Bit_Vector; function "/" (Left : Bit_Vector; Right : Integer) return Bit_Vector; function "/" (Left : Integer; Right : Bit_Vector) return Integer; procedure sig_incr(signal input : inout Bit_Vector); procedure var_incr(input : inout Bit_Vector); procedure left_shift (InpReg : inout Bit_Vector); procedure sig_left_shift (signal InpReg : inout Bit_Vector); procedure right_shift (InpReg : inout Bit_Vector); procedure sig_right_shift (signal InpReg : inout Bit_Vector); end functions; package body functions is function int_Wired_OR(input : int_arr) return INTEGER is variable resolved_value : INTEGER := 0; begin for i in input'RANGE loop if input(i) > 0 then resolved_value := input(i); end if; end loop; return resolved_value; end int_Wired_OR; function bits_to_int (input : Bit_Vector )return INTEGER is variable ret_val : INTEGER := 0; begin for i in input'RANGE loop if input(i) = '1' then ret_val := 2**i + ret_val; end if; end loop; return ret_val; end bits_to_int; function INT_TO_BIN16 (INPUT: in INTEGER) return BIT_VECTOR is variable TEMP_A, TEMP_B: INTEGER := 0; variable OUTPUT: BIT_VECTOR(15 downto 0); begin TEMP_A := INPUT; Loop3 : for I in 15 downto 0 loop TEMP_B := TEMP_A/(2**I); TEMP_A := TEMP_A rem (2**I); if (TEMP_B = 1) then OUTPUT(I) := '1'; else OUTPUT(I) := '0'; end if; end loop Loop3; return OUTPUT; end INT_TO_BIN16; function INT_TO_BIN32 (INPUT: in INTEGER) return BIT_VECTOR is variable TEMP_A, TEMP_B: INTEGER := 0; variable OUTPUT: BIT_VECTOR(31 downto 0); begin TEMP_A := INPUT; Loop3 : for I in 31 downto 0 loop TEMP_B := TEMP_A/(2**I); TEMP_A := TEMP_A rem (2**I); if (TEMP_B = 1) then OUTPUT(I) := '1'; else OUTPUT(I) := '0'; end if; end loop Loop3; return OUTPUT; end INT_TO_BIN32; function INT_TO_BIN25 (INPUT: in INTEGER) return BIT_VECTOR is variable TEMP_A, TEMP_B: INTEGER := 0; variable OUTPUT: BIT_VECTOR(24 downto 0); begin TEMP_A := INPUT; Loop3 : for I in 24 downto 0 loop TEMP_B := TEMP_A/(2**I); TEMP_A := TEMP_A rem (2**I); if (TEMP_B = 1) then OUTPUT(I) := '1'; else OUTPUT(I) := '0'; end if; end loop Loop3; return OUTPUT; end INT_TO_BIN25; function int_to_bin4 (INPUT: in INTEGER) return BIT_VECTOR is variable TEMP_A, TEMP_B: INTEGER := 0; variable OUTPUT: BIT_VECTOR(3 downto 0); begin TEMP_A := INPUT; Loop3 : for I in 3 downto 0 loop TEMP_B := TEMP_A/(2**I); TEMP_A := TEMP_A rem (2**I); if (TEMP_B = 1) then OUTPUT(I) := '1'; else OUTPUT(I) := '0'; end if; end loop Loop3; return OUTPUT; end int_to_bin4; function int_to_bin8 (INPUT: in INTEGER) return BIT_VECTOR is variable TEMP_A, TEMP_B: INTEGER := 0; variable OUTPUT: BIT_VECTOR(7 downto 0); begin TEMP_A := INPUT; Loop3 : for I in 7 downto 0 loop TEMP_B := TEMP_A/(2**I); TEMP_A := TEMP_A rem (2**I); if (TEMP_B = 1) then OUTPUT(I) := '1'; else OUTPUT(I) := '0'; end if; end loop Loop3; return OUTPUT; end int_to_bin8; function bit_Wired_OR (input : bit_arr) return Bit is variable resolved_value : Bit; begin for i in input'RANGE loop if input(i) = '1' then return '1'; end if; end loop; return '0'; end bit_Wired_OR; function bit_Wired_AND (input : bit_arr) return Bit is variable resolved_value : Bit; begin for i in input'RANGE loop if input(i) = '0' then return '0'; end if; end loop; return '1'; end bit_Wired_AND; function "+" (Left, Right : Bit_Vector) return Bit_Vector is variable return_vector : Bit_Vector(Left'Length downto 0); variable carry : Bit := '0'; variable dummy : Bit_Vector(0 to 2); variable i : Integer; begin for i in Left'Reverse_Range loop dummy(0) := Left(i); dummy(1) := Right(i); dummy(2) := carry; case dummy is when "000" => return_vector(i) := '0'; carry := '0'; when "001" => return_vector(i) := '1'; carry := '0'; when "010" => return_vector(i) := '1'; carry := '0'; when "011" => return_vector(i) := '0'; carry := '1'; when "100" => return_vector(i) := '1'; carry := '0'; when "101" => return_vector(i) := '0'; carry := '1'; when "110" => return_vector(i) := '0'; carry := '1'; when "111" => return_vector(i) := '1'; carry := '1'; end case; end loop; return_vector(Left'Length) := carry; return return_vector; end; function "+" (Left : Bit_Vector; Right : Integer) return Bit_Vector is variable vector : Bit_Vector(Left'Range); variable return_vector : Bit_Vector(Left'Length downto 0); variable Right_rem, Right_val : Integer; variable carry : Bit := '0'; begin Right_val := Right; for i in Left'Reverse_Range loop Right_rem := Right_val rem 2; Right_val := Right_val / 2; if Right_rem = 1 then vector(i) := '1'; else vector(i) := '0'; end if; end loop; return_vector := vector + Left; return return_vector; end; function "+" (Left : Integer; Right : Bit_Vector) return Bit_Vector is variable vector : Bit_Vector(Right'Range); variable return_vector : Bit_Vector(Right'Length downto 0); variable Left_rem, Left_val : Integer; variable carry : Bit := '0'; begin Left_val := Left; for i in Right'Reverse_Range loop Left_rem := Left_val rem 2; Left_val := Left_val / 2; if Left_rem = 1 then vector(i) := '1'; else vector(i) := '0'; end if; end loop; return_vector := vector + Left; return return_vector; end; function "+" (Left: Bit_Vector; Right: Bit) return Bit_Vector is variable return_vector: Bit_Vector(Left'Range); begin for i in Left'Reverse_Range loop return_vector(i) := Left(i); end loop; return_vector(0) := Right; return return_vector; end; function "-" (Left, Right : Bit_Vector) return Bit_Vector is variable return_vector : Bit_Vector(Left'Length downto 0); variable borrow : Bit := '0'; variable dummy : Bit_Vector(2 downto 0); variable i : Integer; begin for i in Left'Reverse_Range loop dummy(2) := Left(i); dummy(1) := Right(i); dummy(0) := borrow; case dummy is when "000" => return_vector(i) := '0'; borrow := '0'; when "001" => return_vector(i) := '1'; borrow := '1'; when "010" => return_vector(i) := '1'; borrow := '1'; when "011" => return_vector(i) := '0'; borrow := '1'; when "100" => return_vector(i) := '1'; borrow := '0'; when "101" => return_vector(i) := '0'; borrow := '0'; when "110" => return_vector(i) := '0'; borrow := '0'; when "111" => return_vector(i) := '1'; borrow := '1'; end case; end loop; return_vector(Left'Length) := borrow; return return_vector; end; function "-" (Left : Bit_Vector; Right : Integer) return Bit_Vector is variable vector : Bit_Vector(Left'Range); variable return_vector : Bit_Vector(Left'Length downto 0); variable Right_rem, Right_val : Integer; variable carry : Bit := '0'; begin Right_val := Right; for i in Left'Reverse_Range loop Right_rem := Right_val rem 2; Right_val := Right_val / 2; if Right_rem = 1 then vector(i) := '1'; else vector(i) := '0'; end if; end loop; return_vector := Left - vector; return return_vector; end; function "-" (Left : Integer; Right : Bit_Vector) return Bit_Vector is variable vector : Bit_Vector(Right'Range); variable return_vector : Bit_Vector(Right'Length downto 0); variable Left_rem, Left_val : Integer; variable carry : Bit := '0'; begin Left_val := Left; for i in Right'Reverse_Range loop Left_rem := Left_val rem 2; Left_val := Left_val / 2; if Left_rem = 1 then vector(i) := '1'; else vector(i) := '0'; end if; end loop; return_vector := Left - vector; return return_vector; end; function "*" (Left,Right : Bit_Vector) return Bit_Vector is variable return_vector : Bit_Vector((Left'Length + Right'Length -1) downto 0); variable Right_val : Integer; variable Left_rem,Left_val : Integer; begin Left_val := bits_to_int(Left); Right_val := bits_to_int(Right); Left_val := Left_val * Right_val; for i in return_vector'Reverse_Range loop Left_rem := Left_val rem 2; Left_val := Left_val / 2; if Left_rem = 1 then return_vector(i) := '1'; else return_vector(i) := '0'; end if; end loop; return return_vector; end; function "*" (Left : Bit_Vector; Right : Integer) return Bit_Vector is variable return_vector : Bit_Vector(Left'Range); variable Left_rem,Left_val : Integer; begin Left_val := bits_to_int(Left); Left_val := Left_val * Right; for i in Left'Range loop Left_rem := Left_val rem 2; Left_val := Left_val / 2; if Left_rem = 1 then return_vector(i) := '1'; else return_vector(i) := '0'; end if; end loop; return return_vector; end; function "*" (Left : Integer; Right : Bit_Vector) return Integer is variable return_val : Integer; begin return_val := bits_to_int(Right); return_val := Left * return_val; return return_val; end; function "/" (Left,Right : Bit_Vector) return Bit_Vector is variable return_vector : Bit_Vector((Left'Length -1) downto 0); variable Right_val : Integer; variable Left_rem,Left_val : Integer; begin Left_val := bits_to_int(Left); Right_val := bits_to_int(Right); Left_val := Left_val / Right_val; for i in return_vector'Reverse_Range loop Left_rem := Left_val rem 2; Left_val := Left_val / 2; if Left_rem = 1 then return_vector(i) := '1'; else return_vector(i) := '0'; end if; end loop; return return_vector; end; function "/" (Left : Bit_Vector; Right : Integer) return Bit_Vector is variable return_vector : Bit_Vector(Left'Range); variable Left_rem,Left_val : Integer; begin Left_val := bits_to_int(Left); Left_val := Left_val / Right; for i in Left'Range loop Left_rem := Left_val rem 2; Left_val := Left_val / 2; if Left_rem = 1 then return_vector(i) := '1'; else return_vector(i) := '0'; end if; end loop; return return_vector; end; function "/" (Left : Integer; Right : Bit_Vector) return Integer is variable return_val : Integer; begin return_val := bits_to_int(Right); return_val := Left / return_val; return return_val; end; procedure left_shift(InpReg : inout Bit_Vector) is begin if (InpReg'LEFT >= InpReg'Right) then for i in InpReg'RANGE loop if (i > (InpReg'LOW)) then InpReg(i) := InpReg(i-1); end if; end loop; InpReg(InpReg'LOW) := '0'; else for i in InpReg'RANGE loop if i < InpReg'HIGH then InpReg(i) := InpReg(i+1); end if; end loop; InpReg(InpReg'HIGH) := '0'; end if; end left_shift; procedure sig_left_shift(signal InpReg : inout Bit_Vector) is begin if (InpReg'LEFT >= InpReg'Right) then for i in InpReg'RANGE loop if i > InpReg'LOW then InpReg(i) <= InpReg(i-1); end if; end loop; InpReg(InpReg'LOW) <= '0'; else for i in InpReg'RANGE loop if i < InpReg'HIGH then InpReg(i) <= InpReg(i+1); end if; end loop; InpReg(InpReg'HIGH) <= '0'; end if; end sig_left_shift; procedure right_shift(InpReg : inout Bit_Vector) is begin if (InpReg'Left >= InpReg'Right) then for i in InpReg'REVERSE_RANGE loop if i < InpReg'HIGH then InpReg(i) := InpReg(i+1); end if; end loop; InpReg(InpReg'HIGH) := '0'; else for i in InpReg'RANGE loop if i > InpReg'LOW then InpReg(i) := InpReg(i-1); end if; end loop; InpReg(InpReg'LOW) := '0'; end if; end right_shift; procedure sig_right_shift(signal InpReg : inout Bit_Vector) is begin if (InpReg'Left >= InpReg'Right) then for i in InpReg'REVERSE_RANGE loop if i < InpReg'HIGH then InpReg(i) <= InpReg(i+1); end if; end loop; InpReg(InpReg'HIGH) <= '0'; else for i in InpReg'RANGE loop if i > InpReg'LOW then InpReg(i) <= InpReg(i-1); end if; end loop; InpReg(InpReg'LOW) <= '0'; end if; end sig_right_shift; procedure var_incr(input : inout Bit_Vector) is variable calc_vec : Bit_Vector(input'Length downto 0); begin calc_vec := input + 1; input := calc_vec(input'Range); end var_incr; procedure sig_incr(signal input : inout Bit_Vector) is variable calc_vec : Bit_Vector(input'Length downto 0); begin calc_vec := input + 1; input <= calc_vec(input'Range); end sig_incr; function twocomp_bits_to_int (input : Bit_Vector )return INTEGER is variable ret_val : INTEGER := 0; begin for i in input'RANGE loop if (i < input'HIGH) then if (input(input'HIGH) = '0') then if input(i) = '1' then ret_val := 2**i + ret_val; end if; else if input(i) = '0' then ret_val := 2**i + ret_val; end if; end if; end if; end loop; if (input(input'HIGH) = '1') then ret_val := ret_val + 1; ret_val := 0 - ret_val; end if; return ret_val; end twocomp_bits_to_int; function twocomp_int_to_bin8 (INPUT: in INTEGER) return BIT_VECTOR is variable TEMP_A, TEMP_B: INTEGER := 0; variable OUTPUT: BIT_VECTOR(7 downto 0); variable comp_input, abs_val: integer; begin if (input < 0) then abs_val := 0 - input; comp_input := 2**7-abs_val; assert (comp_input >= 0) ; else comp_input := input; end if; TEMP_A := comp_input; for I in 6 downto 0 loop TEMP_B := TEMP_A/(2**I); TEMP_A := TEMP_A rem (2**I); if (TEMP_B = 1) then OUTPUT(I) := '1'; else OUTPUT(I) := '0'; end if; end loop; if (input < 0) then OUTPUT(7) := '1'; else OUTPUT(7) := '0'; end if; return OUTPUT; end twocomp_int_to_bin8; function twocomp_int_to_bin16 (INPUT: in INTEGER) return BIT_VECTOR is variable TEMP_A, TEMP_B: INTEGER := 0; variable OUTPUT: BIT_VECTOR(15 downto 0); variable comp_input, abs_val: integer; begin if (input < 0) then abs_val := 0 - input; comp_input :=2**15-abs_val; assert(comp_input >= 0); else comp_input := input; end if; TEMP_A := comp_input; for I in 14 downto 0 loop TEMP_B := TEMP_A/(2**I); TEMP_A := TEMP_A rem (2**I); if (TEMP_B = 1) then OUTPUT(I) := '1'; else OUTPUT(I) := '0'; end if; end loop; if (input < 0) then OUTPUT(15) := '1'; else OUTPUT(15) := '0'; end if; return OUTPUT; end twocomp_int_to_bin16; end functions;