0

我一直在研究一个接受 2 个输入、一个 3 位输入和一个 4 位输入的 vhdl 程序。3 位输入表示“2 的 n 次方”,即输入 010(即 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;
4

2 回答 2

1

8 位temp与 8位相乘four_bit得到 12 位结果,该结果分配给 8 位hex,因此错误消息“表达式有 12 个元素,但必须有 8 个元素”。

建议:获取非标准(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));
于 2015-11-30T07:04:12.963 回答
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 位应该在数学上表示什么?

于 2015-11-30T08:39:49.650 回答