VHDL 8位乘法器,3位输入和4位输入,如何补偿输出的位数?
VHDL 8-bit multiplier, 3-bit input and 4-bit input, how to compensate for number of bits in output?
我一直在研究接受 2 个输入、一个 3 位输入和一个 4 位输入的 vhdl 程序。 3 位输入表示“2 的 n 次方”,即输入 010(即 2)等于 2^2=4。输入 110(即 6)将产生 2^6,即 64。这将乘以从 0000 到 1111 的 4 位输入,并将答案存储为 8 位。然而,当我尝试在 VHDL 中解决这个问题时,我不断收到错误 "Expression error at midterm_q_one.vhd(34): expression has 12 elements, but must have 8 elements"。我是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;
8 位 temp
与 8 位 four_bit
相乘得到 12 位的结果,它被分配给 8 位 hex
,因此错误留言 "expression has 12 elements, but must have 8 elements".
建议:获取非标准 (Synopsys) STD_LOGIC_ARITH
和 STD_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));
我看到了两种消除错误的方法。
最简单的是:
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 位在数学上应该表示什么?
我一直在研究接受 2 个输入、一个 3 位输入和一个 4 位输入的 vhdl 程序。 3 位输入表示“2 的 n 次方”,即输入 010(即 2)等于 2^2=4。输入 110(即 6)将产生 2^6,即 64。这将乘以从 0000 到 1111 的 4 位输入,并将答案存储为 8 位。然而,当我尝试在 VHDL 中解决这个问题时,我不断收到错误 "Expression error at midterm_q_one.vhd(34): expression has 12 elements, but must have 8 elements"。我是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;
8 位 temp
与 8 位 four_bit
相乘得到 12 位的结果,它被分配给 8 位 hex
,因此错误留言 "expression has 12 elements, but must have 8 elements".
建议:获取非标准 (Synopsys) STD_LOGIC_ARITH
和 STD_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));
我看到了两种消除错误的方法。
最简单的是:
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 位在数学上应该表示什么?