From 7b1f2f071ff8099ffef78e67ba44e7196ffd3c86 Mon Sep 17 00:00:00 2001 From: tmeissner Date: Sat, 22 Aug 2015 01:26:46 +0200 Subject: [PATCH] Add functions count_ones() & xor_reduce New functions added: * count_ones(): returns number of 1 in a given std_logic_vector * xor_reduce(): returns the xor of all bits in a std_logic_vector Functions even_parity() & odd_parity() now use the xor_reduce function to calc the parity. --- common/UtilsP.vhd | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/common/UtilsP.vhd b/common/UtilsP.vhd index a9f9fac..6bef32a 100644 --- a/common/UtilsP.vhd +++ b/common/UtilsP.vhd @@ -13,9 +13,13 @@ package UtilsP is function or_reduce (data : in std_logic_vector) return std_logic; function or_reduce (data : in boolean_vector) return boolean; + function xor_reduce (data : in std_logic_vector) return std_logic; + function even_parity (data : in std_logic_vector) return std_logic; function odd_parity (data : in std_logic_vector) return std_logic; + function count_ones (data : in std_logic_vector) return natural; + end package UtilsP; @@ -66,23 +70,34 @@ package body UtilsP is end function or_reduce; - function even_parity (data : in std_logic_vector) return std_logic is + function xor_reduce (data : in std_logic_vector) return std_logic is variable v_return : std_logic := '0'; begin - for i in data'range loop + for i in data'range loop v_return := v_return xor data(i); end loop; return v_return; + end function xor_reduce; + + + function even_parity (data : in std_logic_vector) return std_logic is + begin + return xor_reduce(data); end function even_parity; function odd_parity (data : in std_logic_vector) return std_logic is - variable v_return : std_logic := '1'; + begin + return not(xor_reduce(data)); + end function odd_parity; + + + function count_ones (data : in std_logic_vector) return natural is + variable v_return : natural := 0; begin for i in data'range loop - v_return := v_return xor data(i); + v_return := v_return + 1; end loop; - return v_return; - end function odd_parity; + end function count_ones; end package body UtilsP;