Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 3.97 KB | None | 0 0
  1. library IEEE;
  2. use IEEE.STD_LOGIC_1164.ALL;
  3. use IEEE.STD_LOGIC_UNSIGNED.ALL;
  4.  
  5.  
  6. entity Binary_to_7Seg is
  7.     Port ( bin : in  STD_LOGIC_VECTOR (7 downto 0);
  8.               sign : in std_logic;
  9.               sign_7 : out  STD_LOGIC_VECTOR (6 downto 0);
  10.               ones_7 : out  STD_LOGIC_VECTOR (6 downto 0);
  11.            tens_7 : out  STD_LOGIC_VECTOR (6 downto 0);
  12.            hundreds_7 : out  STD_LOGIC_VECTOR (6 downto 0));
  13. end Binary_to_7Seg;
  14.  
  15. architecture Behavioral of Binary_to_7Seg is
  16.  
  17. function bcd_to_seg(Value: in std_logic_vector(0 to 3)) return std_logic_vector is
  18. begin
  19.    case Value is
  20.             when "0000"=> return "1000000";  -- '0'
  21.             when "0001"=> return "1111001";  -- '1'
  22.             when "0010"=> return "0100100";  -- '2'
  23.             when "0011"=> return "0110000";  -- '3'
  24.             when "0100"=> return "0011001";  -- '4'
  25.             when "0101"=> return "0010010";  -- '5'
  26.             when "0110"=> return "0000010";  -- '6'
  27.             when "0111"=> return "1111000";  -- '7'
  28.             when "1000"=> return "0000000";  -- '8'
  29.             when "1001"=> return "0010000";  -- '9'
  30.             when others=> return "1111111";
  31.     end case;
  32. end bcd_to_seg;
  33.  
  34. begin
  35.     process (bin)
  36.  
  37.     variable temp : STD_LOGIC_VECTOR (7 downto 0);
  38.      variable ones : STD_LOGIC_VECTOR (3 downto 0);
  39.      variable tens : STD_LOGIC_VECTOR (3 downto 0);
  40.      variable hundreds : STD_LOGIC_VECTOR (3 downto 0);
  41.      variable sign_7_temp : STD_LOGIC_VECTOR (6 downto 0);
  42.    
  43.     -- Variable to store the output BCD number organized as follows:
  44.     --   hundreds = bcd(11 downto 8)
  45.     --   tens = bcd(7 downto 4)
  46.     --   units = bcd(3 downto 0)
  47.     variable bcd : STD_LOGIC_VECTOR (11 downto 0) := (others => '0');
  48.    
  49.     begin
  50.         -- Zero the bcd variable.
  51.  
  52.         bcd := (others => '0');
  53.        
  54.         -- Read the input binary value into the temp variable.
  55.         temp(7 downto 0) := bin;
  56.          
  57.           if bin(7) = '1' and sign = '1' then
  58.                 temp := (not temp) + 1;
  59.                 sign_7_temp := "0111111";
  60.           end if;
  61.          
  62.           if bin(7) = '0' and sign = '1' then
  63.                 sign_7_temp := "0111001";
  64.           end if;
  65.          
  66.           if bin(7) = '0' and sign = '0' then
  67.                 sign_7_temp := "1111111";
  68.           end if;
  69.        
  70.         -- Cycle 8 times as we have 8 input bits.
  71.         for i in 0 to 7 loop
  72.        
  73.             if bcd(3 downto 0) > 4 then
  74.                 bcd(3 downto 0) := bcd(3 downto 0) + 3;
  75.             end if;
  76.            
  77.             if bcd(7 downto 4) > 4 then
  78.                 bcd(7 downto 4) := bcd(7 downto 4) + 3;
  79.             end if;
  80.            
  81.             -- Hundreds can't be > 4 for an 8-bit input number
  82.             -- so don't need to do anything to upper 4 bits of bcd.
  83.            
  84.             -- Shift bcd left by 1 bit, copy MSB of temp into LSB of bcd.
  85.             bcd := bcd(10 downto 0) & temp(7);
  86.            
  87.             -- Shift temp left by 1 bit.
  88.             temp := temp(6 downto 0) & '0';
  89.        
  90.         end loop;
  91.        
  92.         -- Extract outputs from bcd.
  93.         ones := STD_LOGIC_VECTOR(bcd(3 downto 0));
  94.         tens := STD_LOGIC_VECTOR(bcd(7 downto 4));
  95.         hundreds := STD_LOGIC_VECTOR(bcd(11 downto 8));
  96.          
  97.           ones_7 <= bcd_to_seg(bcd(3 downto 0));
  98.         tens_7 <= bcd_to_seg(bcd(7 downto 4));
  99.         hundreds_7 <= bcd_to_seg(bcd(11 downto 8));
  100.          
  101.           sign_7 <= sign_7_temp;
  102.          
  103.           if tens = "0000" and hundreds = "0000" and sign = '1' then
  104.                 tens_7 <= sign_7_temp;
  105.                 hundreds_7 <= "1111111";
  106.                 sign_7 <= "1111111";
  107.           end if;
  108.          
  109.           if tens /= "0000" and hundreds = "0000" and sign = '1' then
  110.                 hundreds_7 <= sign_7_temp;
  111.                 sign_7 <= "1111111";
  112.           end if;
  113.          
  114.           if tens = "0000" and hundreds = "0000" and sign = '0' then
  115.                 hundreds_7 <= "1111111";
  116.                 tens_7 <= "1111111";
  117.                 sign_7 <= "1111111";
  118.           end if;
  119.          
  120.           if tens /= "0000" and hundreds = "0000" and sign = '0' then
  121.                 hundreds_7 <= "1111111";
  122.                 sign_7 <= "1111111";
  123.           end if;
  124.    
  125.     end process;            
  126.  
  127. end Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement