Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- architecture Behavioral of feladat_2 is
- signal c: STD_LOGIC_VECTOR(4 downto 0) := (others => '0');
- signal tmp: STD_LOGIC_VECTOR(3 downto 0) := (others => '0');
- begin
- process(alusel,a,b)
- variable res: STD_LOGIC_VECTOR(3 downto 0) := (others => '0');
- variable tmp_s: STD_LOGIC_VECTOR(4 downto 0) := (others => '0');
- variable mul_tmp: STD_LOGIC_VECTOR(7 downto 0) := (others => '0');
- variable mul_res: STD_LOGIC_VECTOR(15 downto 0) := (others => '0');
- begin
- cf <= '0';
- ovf <= '0';
- z <= (others => '0');
- res := "0000";
- case alusel is
- when "000" =>
- res := a;
- when "001" => -- a + b
- tmp_s := ('0' & a) + ('0' & b);
- res := tmp_s(3 downto 0);
- cf <= tmp_s(4);
- ovf <= (tmp_s(3) xor a(3) xor b(3)) xor tmp_s(4);
- when "010" => -- a - b
- tmp_s := ('0' & a) - ('0' & b);
- res := tmp_s(3 downto 0);
- cf <= tmp_s(4);
- ovf <= (tmp_s(3) xor a(3) xor b(3)) xor tmp_s(4);
- when "011" => -- b - a
- tmp_s := ('0' & b) - ('0' & a);
- res := tmp_s(3 downto 0);
- cf <= tmp_s(4);
- ovf <= (tmp_s(3) xor a(3) xor b(3)) xor tmp_s(4);
- when "100" => -- a * b
- res := (others => '0');
- mul_tmp := a * b;
- z(7 downto 0) <= mul_tmp(7 downto 0);
- -- using mul_res as copy of z but as variable so usable as right hand side operand
- -- this way avoiding making z inout parameter
- mul_res(7 downto 0) := mul_tmp(7 downto 0);
- -- sign extension (since two 4 bit number multiplication will fit only on 8 bit therefore
- -- I assume the result need sign extensions since the result represented in 16 bit
- if mul_tmp(7) = '1' then
- z(15 downto 8) <= "11111111";
- mul_res(15 downto 8) := "11111111";
- else -- altough technically the value can be be undefined or X, we ignore that, assuming '0'
- z(15 downto 8) <= "00000000";
- mul_res(15 downto 8) := "00000000";
- end if;
- when "101" =>
- res := a and b;
- when "110" =>
- res := a or b;
- when "111" =>
- res := a xor b;
- when others =>
- res := "0000";
- end case;
- y <= res; -- saving the result to the output singal
- -- setting negative flag
- if res(3) = '1' or mul_res(15) = '1' then
- nf <= '1';
- else
- nf <= '0';
- end if;
- -- setting zero flag
- if res = "0000" and mul_res = "0000000000000000" then
- zf <= '1';
- else
- zf <= '0';
- end if;
- end process;
- end Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement