【问题标题】:Left bit shifter in VHDLVHDL中的左移位器
【发布时间】:2021-02-18 18:32:18
【问题描述】:

我需要使用 if then 语句在 VHDL 中创建一个左移位器。我不知道我已经写了什么。我附上了照片和下面的代码。非常感谢!

My Code

The problem im trying to do

LIBRARY ieee;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.NUMERIC_STD.ALL;

entity shifter is
PORT ( 
            input: IN UNSIGNED (7 DOWNTO 0);
            shift_cntrl : IN UNSIGNED (1 DOWNTO 0);
            shift_out : OUT UNSIGNED (15 DOWNTO 0)
        );
        end shifter;
    
    architecture arch of shifter is
    
    begin 
    process (input, shift_cntrl)
    begin
    
    if (shift_cntrl="00") then shift_out<=input;
    
    else if (shift_cntrl="11") then shift_out<=input;
    
    else if (shift_cntrl="01") then shift_out<=input+4;
    
    else if (shift_cntrl="10") then shift_out<=input+8;
    
    end if;
    end process;
    end arch;

    
    
    

【问题讨论】:

  • 二次乘法的优势在于将十进制整数文字设置为另一个操作数的长度,并产生两倍于其长度的结果。综合起来,这将转化为转变。例如,shift_out &lt;= input * 4; 将左移 2,而右侧表达式将为分配目标的每个元素提供匹配元素。提供由 shift_control 指定的移位距离的明确指示(添加似乎无效)。将else ifs 更正为elsifs。

标签: vhdl quartus


【解决方案1】:

如果您使用的是 VHDL-2008,那么您可以使用 sll 运算符来执行移位,例如,移位一位:

shift_out <= input sll 1;

否则,您可以简单地使用 & 符号在输入值的末尾附加一些位,例如移动一位:

shift_out <= input & '0';

您在执行此操作时需要小心,因为右侧的表达式应该与左侧的信号具有相同的位数。在您的示例中,输入为 8 位,而 shift_out 为 16 位,因此您还需要进行一些零填充。对于 1 位移位,您可以这样做:

 shift_out <= '0000000' & input & '0';

【讨论】:

    【解决方案2】:

    这个问题没有详细说明,但我相信这是你想要做的:

    library ieee;
    use ieee.std_logic_1164.all;
    
    entity shifter is
        port ( 
            input:        in std_ulogic_vector (7 downto 0);
            shift_cntrl:  in std_ulogic_vector (1 downto 0);
            shift_out:   out std_ulogic_vector (15 downto 0)
        );
    end shifter;
    
    architecture arch of shifter is
    begin 
        process (input, shift_cntrl)
        begin
            if    shift_cntrl = "00" then shift_out <= x"00" & input;
            elsif shift_cntrl = "11" then shift_out <= x"00" & input;
            elsif shift_cntrl = "01" then shift_out <= x"0"  & input & x"0";
            elsif shift_cntrl = "10" then shift_out <= input & x"00";
            end if;
        end process;
    end arch;
    

    我已将界面更改为使用“std_ulogic_vector”,应该更频繁地使用它。不过我还没有机会对此进行测试,因为这也需要编写一个测试平台。

    您的代码按原样存在许多问题,“else if”不是有效的 VHDL,应将其替换为“elsif”。无符号加 4 也不会执行移位,它只会将值加 4。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-03
      • 2016-09-02
      • 1970-01-01
      相关资源
      最近更新 更多