我想将数字除以512,这意味着我需要将其除以9。例如,在我的代码中,我希望将二进制形式的数字26乘以100,然后再除以512。除以512我所要做的就是将数字26 * 100右移9倍。但是当我执行shift_right
命令时,出现以下错误:
错误(10511):Multiplier_VHDL处的VHDL合格表达式错误.vhd(34):合格表达式中指定的SHIFT_RIGHT类型必须与上下文隐含表示的std_logic_vector类型匹配
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
entity Multiplier_VHDL is
GENERIC (
display_resolution : INTEGER := 23; -- counter to get to the lowest frequency
display_counter: INTEGER := 8); -- counter to get to 97KHz frequency
port (
Nibble1 : in std_logic_vector(display_counter downto 0) := "000011010"; -- 26 in binary form
Nibble2 : in std_logic_vector(display_counter downto 0);
Result: out std_logic_vector(17 downto 0));
end entity Multiplier_VHDL;
architecture Behavioral of Multiplier_VHDL is
signal number : unsigned(display_counter downto 0) := "001100100"; -- 100 in binary form
begin
Result <= std_logic_vector(unsigned(Nibble1) * unsigned(number));
Result <= (shift_right(unsigned(number), display_counter + 1));
end architecture Behavioral;
答案 0 :(得分:0)
shift_right返回无符号或带符号,这取决于您提供的内容。因此,您尝试将无符号写入std_logic_vector(结果的类型为std_logic_vector)。
此外,number
已经是unsigned
类型,因此无需再次将其强制转换为unsigned
。
但是我给你+1点,因为使用了numeric_std而不是std_logic_arith。
答案 1 :(得分:0)
上下文表示此处的整个语句。参见IEEE Std 1076-2008 12.5过载解析的上下文。请注意,Result
是std_logic_vector。
尝试Result <= std_logic_vector(shift_right(number, display_counter + 1));
该代码具有两个用于Result
的驱动程序(这两个并发分配在流程语句中详细说明)。
似乎需要在体系结构中声明另一个信号来保存乘法结果(例如dividend
)或采用第一个Result
赋值的右侧表达式并在适当的位置使用它第二number
中Result
的值。如果不将驱动程序单独设置为Result
,您的代码将无法提供有用的结果。
第一种方法,使用新信号:
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity multiplier_vhdl is
generic (
display_resolution: integer := 23;
display_counter: integer := 8
);
port (
nibble1: in std_logic_vector(display_counter downto 0) := "000011010";
nibble2: in std_logic_vector(display_counter downto 0);
result: out std_logic_vector(17 downto 0)
);
end entity multiplier_vhdl;
architecture behavioral of multiplier_vhdl is
signal number: unsigned(display_counter downto 0) := "001100100";
signal dividend: unsigned(result'range);
begin
dividend <= unsigned(nibble1) * number;
result <= std_logic_vector(shift_right(dividend, display_counter + 1));
end architecture behavioral;
请注意,尚未引用nibble2。
第二种方法如下:
architecture behavioral1 of multiplier_vhdl is
signal number: unsigned(display_counter downto 0) := "001100100";
-- signal dividend: unsigned(result'range);
begin
-- dividend <= unsigned(nibble1) * number;
result <= std_logic_vector(shift_right(unsigned(nibble1) * number, display_counter + 1));
end architecture behavioral1;
使用符合-2008修订VHDL的IEEE标准的工具链,并使用软件包numeric_std_unsigned代替numeric_std(将std_logic_vector处理为算术运算的unsigned):
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std_unsigned.all;
entity multiplier_vhdl is
generic (
display_resolution: integer := 23;
display_counter: integer := 8
);
port (
nibble1: in std_logic_vector(display_counter downto 0) := "000011010";
nibble2: in std_logic_vector(display_counter downto 0);
result: out std_logic_vector(17 downto 0)
);
end entity multiplier_vhdl;
architecture behavioral2 of multiplier_vhdl is
signal number: std_logic_vector(display_counter downto 0) := "001100100";
begin
result <= shift_right(nibble1 * number, display_counter + 1);
end architecture behavioral2;
消除了所有类型转换。
具有适当的实体use子句的所有三种体系结构都进行了分析,精心设计和模拟,显示出没有边界错误和类型不匹配。