Advertisement
DomMisterSoja

binttobcd

Apr 10th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 4.20 KB | None | 0 0
  1. --****************************************************
  2. -- Laboratório de Sistemas Digitais
  3. -- Circuito conversor binário para bcd
  4. -- Exemplo tirado do livro VHDL by Example (P. Chu)
  5. --
  6. --      IFCE Maracanaú - Circuitos Digitais
  7. --****************************************************
  8.  
  9. library ieee;
  10. use ieee.std_logic_1164.all;
  11. use ieee.numeric_std.all;
  12.  
  13. entity bintobcd is
  14.   port(
  15.     clk : in std_logic;
  16.     rst : in std_logic;
  17.     start : in std_logic;
  18.     bin : in std_logic_vector ( 12 downto 0 );
  19.     ready, done : out std_logic;
  20.     bcd3,bcd2,bcd1,bcd0 : out std_logic_vector (3 downto 0)
  21.   );
  22. end bintobcd;
  23.  
  24. architecture arch of bintobcd is
  25.   type state_type is (idle, op, done_state);
  26.     signal state_reg, state_next : state_type;
  27.     signal p2s_reg, p2s_next : std_logic_vector (12 downto 0);
  28.     signal n_reg, n_next : unsigned ( 3 downto 0 );
  29.     signal bcd3_reg, bcd2_reg, bcd1_reg, bcd0_reg : unsigned ( 3 downto 0 );
  30.     signal bcd3_next, bcd2_next, bcd1_next, bcd0_next : unsigned ( 3 downto 0 );
  31.     signal bcd3_tmp, bcd2_tmp, bcd1_tmp, bcd0_tmp : unsigned (3 downto 0 );
  32.    
  33.     begin
  34.     --registradores de estado e dados
  35.     process (clk, rst)
  36.       begin
  37.         if rst='1' then
  38.           state_reg <= idle;
  39.           p2s_reg <= (others => '0' );
  40.           n_reg <= (others => '0' );
  41.           bcd3_reg <= (others => '0' );
  42.           bcd2_reg <= (others => '0' );
  43.           bcd1_reg <= (others => '0' );
  44.           bcd0_reg <= (others => '0' );
  45.         elsif clk'event and clk='1' then
  46.           state_reg <= state_next;
  47.           p2s_reg <= p2s_next;
  48.           n_reg <= n_next;
  49.           bcd3_reg <= bcd3_next;
  50.           bcd2_reg <= bcd2_next;
  51.           bcd1_reg <= bcd1_next;
  52.           bcd0_reg <= bcd0_next;          
  53.         end if;          
  54.       end process;
  55.      
  56.       --lógico do próximo estado e operações da caminho de dados
  57.       process (state_reg, start, p2s_reg, n_reg, n_next, bin,
  58.             bcd0_reg,bcd1_reg, bcd2_reg, bcd3_reg, bcd0_tmp, bcd1_tmp,
  59.             bcd2_tmp, bcd3_tmp)
  60.             begin
  61.               state_next <= state_reg;
  62.               ready <= '0';
  63.               done <= '0';
  64.               p2s_next <= p2s_reg;
  65.               bcd0_next <= bcd0_reg;
  66.               bcd1_next <= bcd1_reg;
  67.               bcd2_next <= bcd2_reg;
  68.               bcd3_next <= bcd3_reg;
  69.               n_next <= n_reg;
  70.               case state_reg is
  71.               when idle =>
  72.                 ready <= '1';
  73.                 if start = '1' then
  74.                   state_next <= op;
  75.                   bcd3_next <= ( others => '0' );
  76.                   bcd2_next <= ( others => '0' );
  77.                   bcd1_next <= ( others => '0' );
  78.                   bcd0_next <= ( others => '0' );
  79.                   n_next <= "1101"; -- index
  80.                   p2s_next <= bin; -- entrada do registrador de deslocamento
  81.                 end if;
  82.                
  83.               when op =>
  84.                 -- desloca o bit binário
  85.                 p2s_next <= p2s_reg(11 downto 0) & '0';
  86.                 -- desloca 4 bits BCD
  87.                 bcd0_next <= bcd0_tmp(2 downto 0) & p2s_reg(12);
  88.                 bcd1_next <= bcd1_tmp(2 downto 0) & bcd0_tmp(3);
  89.                 bcd2_next <= bcd2_tmp(2 downto 0) & bcd1_tmp(3);
  90.                 bcd3_next <= bcd3_tmp(2 downto 0) & bcd2_tmp(3);
  91.                 n_next <= n_reg - 1;
  92.                 if n_next = 0 then
  93.                   state_next <= done_state;
  94.                 end if;
  95.                
  96.               when done_state =>
  97.                 state_next <= idle;
  98.                 done <= '1';                
  99.               end case;
  100.             end process;
  101.            
  102.             -- unidades de caminho de dados
  103.             bcd0_tmp <= bcd0_reg + 3 when bcd0_reg > 4 else bcd0_reg;
  104.             bcd1_tmp <= bcd1_reg + 3 when bcd1_reg > 4 else bcd1_reg;
  105.             bcd2_tmp <= bcd2_reg + 3 when bcd2_reg > 4 else bcd2_reg;
  106.             bcd3_tmp <= bcd3_reg + 3 when bcd3_reg > 4 else bcd3_reg;
  107.             -- saidas
  108.             bcd0 <= std_logic_vector(bcd0_reg);
  109.             bcd1 <= std_logic_vector(bcd1_reg);
  110.             bcd2 <= std_logic_vector(bcd2_reg);
  111.             bcd3 <= std_logic_vector(bcd3_reg);
  112.     end arch;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement