Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 2.33 KB | None | 0 0
  1. architecture Behavioral of feladat_2 is
  2.     signal c: STD_LOGIC_VECTOR(4 downto 0) := (others => '0');
  3.     signal tmp: STD_LOGIC_VECTOR(3 downto 0) := (others => '0');
  4. begin
  5. process(alusel,a,b)
  6.     variable res: STD_LOGIC_VECTOR(3 downto 0) := (others => '0');
  7.     variable tmp_s: STD_LOGIC_VECTOR(4 downto 0) := (others => '0');
  8.     variable mul_tmp: STD_LOGIC_VECTOR(7 downto 0) := (others => '0');
  9.     variable mul_res: STD_LOGIC_VECTOR(15 downto 0) := (others => '0');
  10. begin
  11.     cf <= '0';
  12.     ovf <= '0';
  13.     z <= (others => '0');
  14.     res := "0000";
  15.  
  16.     case alusel is
  17.         when "000" =>
  18.             res := a;
  19.         when "001" => -- a + b
  20.             tmp_s := ('0' & a) + ('0' & b);
  21.             res := tmp_s(3 downto 0);
  22.             cf <= tmp_s(4);
  23.             ovf <= (tmp_s(3) xor a(3) xor b(3)) xor tmp_s(4);
  24.            
  25.         when "010" => -- a - b
  26.             tmp_s := ('0' & a) - ('0' & b);
  27.             res := tmp_s(3 downto 0);
  28.             cf <= tmp_s(4);
  29.             ovf <= (tmp_s(3) xor a(3) xor b(3)) xor tmp_s(4);
  30.            
  31.         when "011" => -- b - a
  32.             tmp_s := ('0' & b) - ('0' & a);
  33.             res := tmp_s(3 downto 0);
  34.             cf <= tmp_s(4);
  35.             ovf <= (tmp_s(3) xor a(3) xor b(3)) xor tmp_s(4);
  36.            
  37.         when "100" => -- a * b
  38.             res := (others => '0');
  39.             mul_tmp := a * b;
  40.            
  41.             z(7 downto 0) <= mul_tmp(7 downto 0);
  42.             -- using mul_res as copy of z but as variable so usable as right hand side operand
  43.             -- this way avoiding making z inout parameter
  44.             mul_res(7 downto 0) := mul_tmp(7 downto 0);
  45.            
  46.             -- sign extension (since two 4 bit number multiplication will fit only on 8 bit therefore
  47.             -- I assume the result need sign extensions since the result represented in 16 bit
  48.             if mul_tmp(7) = '1' then
  49.                 z(15 downto 8) <= "11111111";
  50.                 mul_res(15 downto 8) := "11111111";
  51.             else -- altough technically the value can be be undefined or X, we ignore that, assuming '0'
  52.                 z(15 downto 8) <= "00000000";
  53.                 mul_res(15 downto 8) := "00000000";
  54.             end if;
  55.         when "101" =>
  56.             res := a and b;
  57.         when "110" =>
  58.             res := a or b;
  59.         when "111" =>
  60.             res := a xor b;
  61.         when others =>
  62.             res := "0000";
  63.     end case;
  64.    
  65.     y <= res; -- saving the result to the output singal
  66.    
  67.     -- setting negative flag
  68.     if res(3) = '1' or mul_res(15) = '1' then
  69.         nf <= '1';
  70.     else
  71.         nf <= '0';
  72.     end if;
  73.    
  74.     -- setting zero flag
  75.     if res = "0000" and mul_res = "0000000000000000" then
  76.         zf <= '1';
  77.     else
  78.         zf <= '0';
  79.     end if;
  80.        
  81. end process;
  82. end Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement