www.pudn.com > ActivePowerMeter.zip > BinToBcd.vhd, change:2008-02-29,size:2042b


----------------------------------------------------------------------------------
-- Company: 
-- Engineer: 
-- 
-- Create Date:    23:30:05 02/28/2008 
-- Design Name: 
-- Module Name:    BinToBcd - Behavioral 
-- Project Name: 
-- Target Devices: 
-- Tool versions: 
-- Description: 
--
-- Dependencies: 
--
-- Revision: 
-- Revision 0.01 - File Created
-- Additional Comments: 
--
----------------------------------------------------------------------------------
-- Binary to n Digit BCD encoder;  
-- Input = n hexadecimal digits 
-- Output = n BCD digits 
-- Output = least significant n BCD digits of the BCD number (overflow lost) 
 
library IEEE; 
use IEEE.STD_LOGIC_1164.ALL; 
use IEEE.STD_LOGIC_ARITH.ALL; 
use IEEE.STD_LOGIC_UNSIGNED.ALL; 
 
entity BinToBcdEncoder_nDigits is 
    Generic ( n : integer := 3);  -- number of BCD digits 
    Port ( vecIn : in  STD_LOGIC_VECTOR (4*n-1 downto 0); 
           bcdOut : out  STD_LOGIC_VECTOR (4*n-1 downto 0)); 
end BinToBcdEncoder_nDigits; 
 
architecture Behavioral of BinToBcdEncoder_nDigits is 
 
  procedure DecAdj(Ain : in std_logic_vector(4*n-1 downto 0); 
                   Aout : out std_logic_vector(4*n-1 downto 0)) is  
 
    variable Ainternal: std_logic_vector(4*n downto 0);   
 
  begin 
  Ainternal :=	'0' & Ain; 
   
    for i in 0 to n-1 loop 
      if Ainternal(4*i+3 downto 4*i)> "1001" then 
		   Ainternal(4*i+4) := '1'; 
		end if; 
      if Ainternal(4*i+4) = '1'	then	 
        Ainternal(4*i+3 downto 4*i) := Ainternal(4*i+3 downto 4*i) + "0110"; 
      end if;		   
    end loop;		 
 
    Aout := Ainternal(4*n-1 downto 0); 
  end DecAdj; 
   
begin 
 
  SequentialInstructionUsage: process(vecIn) is 
 
    variable bcdInternal: STD_LOGIC_VECTOR (4*n-1 downto 0); 
 
  begin 
 
	 bcdInternal := (others => '0'); 
	  
    for j in 0 to 4*n-1 loop 
	   bcdInternal := (bcdInternal(4*n-1-1 downto 0) & vecIn(4*n-1-j));  
      DecAdj(Ain => bcdInternal, Aout => bcdInternal); 
    end loop; 
      bcdOut <= bcdInternal; 
 
  end process; 
 
end Behavioral;