qq_43643991 2020-06-19 12:22 采纳率: 0%
浏览 915
已采纳

VHDL,ISE设计16位运算器,求代码注释?

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;
-- Uncomment the following library declaration if instantiating
-- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;
entity ALU is
Port ( A : in STD_LOGIC_VECTOR(15 downto 0);
B : in STD_LOGIC_VECTOR(15 downto 0);
Y : inout STD_LOGIC_VECTOR(15 downto 0);
FINISHED : inout STD_LOGIC :='0';
ENABLED : in STD_LOGIC;
OP : in STD_LOGIC_VECTOR(3 downto 0);
OFS : out STD_LOGIC;
CFS : out STD_LOGIC;
ZFS : out STD_LOGIC;
SFS : out STD_LOGIC);
end ALU;
architecture Behavioral of ALU is
begin
PROCESS(ENABLED)
VARIABLE temp : STD_LOGIC;
VARIABLE vec_15 : STD_LOGIC_VECTOR(14 DOWNTO 0);
VARIABLE int_A, int_B, int_Y : INTEGER RANGE 0 TO 65535;
BEGIN
IF(ENABLED = '1') THEN
FINISHED <= '0';
OFS <= '0';
CFS <= '0';
ZFS <= '0';
SFS <= '0';
int_A := CONV_INTEGER(A);
int_B := CONV_INTEGER(B);
CASE OP IS
WHEN "0000" => --ADD
Y <= A+B;
--int_Y := int_A + int_B;
--Y <= CONV_STD_LOGIC_VECTOR(int_Y,16);
if(A(15)='0' and B(15)='0' and Y(15)='1') then OFS <= '1'; end if;
if(A(15)='1' and B(15)='1' and Y(15)='0') then OFS <= '1'; CFS<='1'; end if;
if(Y = "0000000000000000") then ZFS <= '1'; end if;
if(Y(15) = '1') then SFS <= '1'; end if;
FINISHED <= '1';
WHEN "0001" => --SUB
int_Y := int_A - int_B;
if(int_Y = 0) then ZFS <= '1'; end if;
if(int_Y < 0) then SFS <= '1'; end if;
Y <= CONV_STD_LOGIC_VECTOR(int_Y,16);
FINISHED <= '1';
WHEN "0010" => --AND
Y <= (A AND B);
if(CONV_INTEGER(Y) = 0) then ZFS <= '1'; end if;
if(CONV_INTEGER(Y) < 0) then SFS <= '1'; end if;
FINISHED <= '1';
WHEN "0011" => --OR
Y <= A OR B;
FINISHED <= '1';
WHEN "0100" => --XOR
Y <= A XOR B;
FINISHED <= '1';
WHEN "0101" => --NOT
Y <= NOT A;
FINISHED <= '1';
WHEN "0110" => --SLL
if(B(3 downto 0) = "0000") then
Y <= TO_STDLOGICVECTOR(To_bitvector(A) SLL CONV_INTEGER(B(3 downto 0)));
else
Y <= TO_STDLOGICVECTOR(To_bitvector(A) SLL 1);
end if;
FINISHED <= '1';
WHEN "0111" => --SLA
if(B(3 downto 0) = "0000") then
Y <= TO_STDLOGICVECTOR(To_bitvector(A) SLA CONV_INTEGER(B(3 downto 0)));
else
Y <= TO_STDLOGICVECTOR(To_bitvector(A) SLA 1);
end if;
FINISHED <= '1';
WHEN "1000" => --SRL
if(B(3 downto 0) = "0000") then
Y <= TO_STDLOGICVECTOR(To_bitvector(A) SRL CONV_INTEGER(B(3 downto 0)));
else
Y <= TO_STDLOGICVECTOR(To_bitvector(A) SRL 1);
end if;
FINISHED <= '1';
WHEN "1001" => --SRA
if(B(3 downto 0) = "0000") then
Y <= TO_STDLOGICVECTOR(To_bitvector(A) SRA CONV_INTEGER(B(3 downto 0)));
else
Y <= TO_STDLOGICVECTOR(To_bitvector(A) SRA 1);
end if;
FINISHED <= '1';
WHEN "1010" => --ROL
if(B(3 downto 0) = "0000") then
Y <= TO_STDLOGICVECTOR(To_bitvector(A) ROL CONV_INTEGER(B(3 downto 0)));
else
Y <= TO_STDLOGICVECTOR(To_bitvector(A) ROL 1);
end if;
FINISHED <= '1';
WHEN OTHERS =>
Y <= "0000000000000000";
FINISHED <= '1';
END CASE;
END IF;
END PROCESS;
end Behavioral;

  • 写回答

1条回答 默认 最新

  • 丫丫杀无赦 2020-07-04 23:20
    关注

    IF(ENABLED = '1') THEN--初始化
    FINISHED <= '0';
    OFS <= '0';
    CFS <= '0';
    ZFS <= '0';
    SFS <= '0';
    int_A := CONV_INTEGER(A);
    int_B := CONV_INTEGER(B);
    CASE OP IS --选择功能
    WHEN "0000" => --ADD
    Y <= A+B;

    if(A(15)='0' and B(15)='0' and Y(15)='1') then OFS <= '1'; end if;--溢出
    if(A(15)='1' and B(15)='1' and Y(15)='0') then OFS <= '1'; CFS<='1'; end if;--溢出 进位
    if(Y = "0000000000000000") then ZFS <= '1'; end if;--为零
    if(Y(15) = '1') then SFS <= '1'; end if;--为负数
    FINISHED <= '1';
    WHEN "0001" => --SUB
    int_Y := int_A - int_B;
    if(int_Y = 0) then ZFS <= '1'; end if;
    if(int_Y < 0) then SFS <= '1'; end if;
    Y <= CONV_STD_LOGIC_VECTOR(int_Y,16);
    FINISHED <= '1';
    WHEN "0010" => --AND
    Y <= (A AND B);
    if(CONV_INTEGER(Y) = 0) then ZFS <= '1'; end if;
    if(CONV_INTEGER(Y) < 0) then SFS <= '1'; end if;
    FINISHED <= '1';
    WHEN "0011" => --OR
    Y <= A OR B;
    FINISHED <= '1';
    WHEN "0100" => --XOR
    Y <= A XOR B;
    FINISHED <= '1';
    WHEN "0101" => --NOT
    Y <= NOT A;
    FINISHED <= '1';
    WHEN "0110" => --SLL最高位丢失,最低位补0
    if(B(3 downto 0) = "0000") then
    Y <= TO_STDLOGICVECTOR(To_bitvector(A) SLL CONV_INTEGER(B(3 downto 0)));
    else
    Y <= TO_STDLOGICVECTOR(To_bitvector(A) SLL 1);
    end if;
    FINISHED <= '1';
    WHEN "0111" => --SLA
    if(B(3 downto 0) = "0000") then
    Y <= TO_STDLOGICVECTOR(To_bitvector(A) SLA CONV_INTEGER(B(3 downto 0)));
    else
    Y <= TO_STDLOGICVECTOR(To_bitvector(A) SLA 1);
    end if;
    FINISHED <= '1';
    WHEN "1000" => --SRL
    if(B(3 downto 0) = "0000") then
    Y <= TO_STDLOGICVECTOR(To_bitvector(A) SRL CONV_INTEGER(B(3 downto 0)));
    else
    Y <= TO_STDLOGICVECTOR(To_bitvector(A) SRL 1);
    end if;
    FINISHED <= '1';
    WHEN "1001" => --SRA
    if(B(3 downto 0) = "0000") then
    Y <= TO_STDLOGICVECTOR(To_bitvector(A) SRA CONV_INTEGER(B(3 downto 0)));
    else
    Y <= TO_STDLOGICVECTOR(To_bitvector(A) SRA 1);
    end if;
    FINISHED <= '1';
    WHEN "1010" => --ROL
    if(B(3 downto 0) = "0000") then
    Y <= TO_STDLOGICVECTOR(To_bitvector(A) ROL CONV_INTEGER(B(3 downto 0)));
    else
    Y <= TO_STDLOGICVECTOR(To_bitvector(A) ROL 1);
    end if;
    FINISHED <= '1';

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!