📄 addsub_ovcy.vhd
字号:
-- Description: Adder/Subtractor with carry/borrow and arbitrary data
-- width and overflow flag.
--
--
--
--
-------------------------------------------------------------------------------
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_arith.all;
-----------------------------ENTITY DECLARATION--------------------------------
entity addsub_ovcy is
generic (DWIDTH : integer := 4);
port (opa_i : in std_logic_vector(DWIDTH-1 downto 0); -- Operand A
opb_i : in std_logic_vector(DWIDTH-1 downto 0); -- Operand B
addsub_i : in std_logic; -- Add or subtract command
cy_i : in std_logic; -- Carry input
cy_o : out std_logic; -- Carry/borrow bit
ov_o : out std_logic; -- Overflow flag
rslt_o : out std_logic_vector(DWIDTH-1 downto 0)); -- Result
end addsub_ovcy;
-------------------------------------------------------------------------------
architecture rtl of addsub_ovcy is
begin
gen_equal_one: if (DWIDTH = 1) generate
-- purpose: Simple adder/subtractor with carry/borrow and overflow
-- type : combinational
-- inputs : opa_i, opb_i, addsub_i, cy_i
-- outputs: cy_o, rslt_o
p_addsub_ov: process (opa_i, opb_i, addsub_i, cy_i)
variable v_la : unsigned(1 downto 0);
variable v_lb : unsigned(1 downto 0);
variable v_lresult : std_logic_vector(2 downto 0);
begin -- process p_addsub
v_la(1) := opa_i(DWIDTH-1);
v_lb(1) := opb_i(DWIDTH-1);
if addsub_i = '1' then
v_la(0) := '1';
v_lb(0) := cy_i;
v_lresult := conv_unsigned(v_la,3) + unsigned(v_lb);
else
v_la(0) := '0';
v_lb(0) := cy_i;
v_lresult := conv_unsigned(v_la,3) - unsigned(v_lb);
end if;
cy_o <= v_lresult(2);
ov_o <= (cy_i and not(v_lresult(2))) or
(v_lresult(2) and not(cy_i));
rslt_o(DWIDTH-1) <= v_lresult(1);
end process p_addsub_ov;
end generate gen_equal_one;
gen_greater_one: if (DWIDTH > 1) generate
-- purpose: Simple adder/subtractor with carry/borrow and overflow
-- type : combinational
-- inputs : opa_i, opb_i, addsub_i, cy_i
-- outputs: cy_o, rslt_o
p_addsub_ov: process (opa_i, opb_i, addsub_i, cy_i)
variable v_a : unsigned(DWIDTH-1 downto 0);
variable v_b : unsigned(DWIDTH-1 downto 0);
variable v_result : std_logic_vector(DWIDTH downto 0);
variable v_la : unsigned(1 downto 0);
variable v_lb : unsigned(1 downto 0);
variable v_lresult : std_logic_vector(2 downto 0);
begin -- process p_addsub
v_a(DWIDTH-1 downto 1) := unsigned(opa_i(DWIDTH-2 downto 0));
v_b(DWIDTH-1 downto 1) := unsigned(opb_i(DWIDTH-2 downto 0));
v_la(1) := opa_i(DWIDTH-1);
v_lb(1) := opb_i(DWIDTH-1);
if addsub_i = '1' then
v_a(0) := '1';
v_b(0) := cy_i;
v_result := conv_unsigned(v_a,DWIDTH+1) + unsigned(v_b);
v_la(0) := '1';
v_lb(0) := v_result(DWIDTH);
v_lresult := conv_unsigned(v_la,3) + unsigned(v_lb);
else
v_a(0) := '0';
v_b(0) := cy_i;
v_result := conv_unsigned(v_a,DWIDTH+1) - unsigned(v_b);
v_la(0) := '0';
v_lb(0) := v_result(DWIDTH);
v_lresult := conv_unsigned(v_la,3) - unsigned(v_lb);
end if;
cy_o <= v_lresult(2);
ov_o <= (v_result(DWIDTH) and not(v_lresult(2))) or
(v_lresult(2) and not(v_result(DWIDTH)));
rslt_o(DWIDTH-2 downto 0) <= v_result(DWIDTH-1 downto 1);
rslt_o(DWIDTH-1) <= v_lresult(1);
end process p_addsub_ov;
end generate gen_greater_one;
end rtl;
-------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -