VHDL 8位乘法器,3位输入和4位输入,如何补偿输出中的位数?

时间:2015-11-30 03:52:02

标签: vhdl number-formatting bit multiplication 8-bit

我一直致力于一个接受2个输入,3位输入和4位输入的vhdl程序。 3位输入表示“2的幂为n”,即010的输入(2为2)将等于2 ^ 2 = 4。 110(即6)的输入将产生2 ^ 6,即64.这将乘以从0000到1111的4位输入,并且答案存储为8位。但是,当我尝试用VHDL解决这个问题时,我不断收到错误“midterm_q_one.vhd中的表达式错误(34):表达式有12个元素,但必须有8个元素”。我是VHDL的新手,在线搜索收效甚微。我希望我的输出方式,在本例中为十六进制,将我的2个输入的乘积存储为8位值,但不知道如何。任何帮助将不胜感激,下面是我的代码。谢谢!

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

entity midterm_q_one is
port(en: in std_logic;
     reset: in std_logic;
      three_bit: in std_logic_vector(2 downto 0);
      four_bit: in std_logic_vector(3 downto 0);
      hex: out std_logic_vector(7 downto 0)
);
end midterm_q_one;

architecture arch of midterm_q_one is
signal temp : std_logic_vector(7 downto 0);
begin
    process(en, reset, three_bit, four_bit)
    begin
if(reset = '1') then
    temp <= "00000000";--reset to decimal 0
elsif(en = '1') then
        case three_bit is
            when "000" => temp <= "00000001";--1
            when "001" => temp <= "00000010";--2
            when "010" => temp <= "00000100";--4
            when "011" => temp <= "00001000";--8
            when "100" => temp <= "00010000";--16
            when "101" => temp <= "00100000";--32
            when "110" => temp <= "01000000";--64
            when "111" => temp <= "10000000";--128
       end case;
end if;
hex <= temp * four_bit;
    end process;
    end arch;

2 个答案:

答案 0 :(得分:1)

8位temp与8位four_bit的乘法给出了12位结果,该结果被分配给8位hex,因此出现错误消息& #34;表达式有12个元素,但必须有8个元素&#34;。

建议:获取非标准(Synopsys)STD_LOGIC_ARITHSTD_LOGIC_UNSIGNED,并开始使用标准numeric_std包。

使用numeric_std,您可以使用以下命令调整结果大小:

library ieee;
use ieee.numeric_std.all;
...
hex <= std_logic_vector(resize(unsigned(temp) * unsigned(four_bit), hex'length));

答案 1 :(得分:1)

我可以看到两种摆脱错误的方法。

最简单的是:

architecture simple of midterm_q_one is
begin
    process (en, reset, four_bit, three_bit)
    begin
        if reset = '1' then
            hex <= (others => '0');
        elsif en = '1' then 
            hex <= SHL("0000" & four_bit, three_bit);
        end if;
    end process;
end architecture;

这仍然需要了解您想要的8位,或者您是否希望将值限制为x"FF"或者是否需要8位最佳产品:

architecture best_product of midterm_q_one is
begin
    process (en, reset, four_bit, three_bit)
        variable intermed: std_logic_vector (11 downto 0);
    begin
        if reset = '1' then
            intermed := (others => '0');
        elsif en = '1' then 
            intermed := SHL("0000" & four_bit, three_bit);
        end if;
        hex <= intermed(11 downto 4);
    end process;
end architecture;

夹紧:

architecture saturate_clamp of midterm_q_one is
begin
    process (en, reset, four_bit, three_bit)
        variable intermed: std_logic_vector (11 downto 0);
    begin
        if reset = '1' then
            intermed := (others => '0');
        elsif en = '1' then 
            intermed := SHL("0000" & four_bit, three_bit);
        end if;
        if intermed(11) = '1' or intermed(10) = '1' or 
           intermed(9)  = '1' or intermed(8)  = '1' then
            hex <= x"FF";
        else 
            hex <= intermed(7 downto 0);
        end if;
    end process;
end architecture;

应该以数学方式表示的8位是什么?