【问题标题】:Unsigned VHDL conversion not working无符号 VHDL 转换不起作用
【发布时间】:2016-10-19 06:58:02
【问题描述】:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;

-- Uncomment the following library declaration if instantiating
-- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity fourToSixteenDecoder is
    port ( a : in std_logic_vector(0 to 3); 
              EN : in  STD_LOGIC;
              Y : out std_logic_vector(0 to 15));
end fourToSixteenDecoder;

architecture Behavioral of fourToSixteenDecoder is
begin
    process(a, EN)
    variable inputs : integer := conv_integer(unsigned(a));
    variable Y_c : std_logic_vector(0 to 15);
    begin
        Y_c := X"0000";
        if (EN = '1') then
            Y_c(inputs) := '1';
        elsif (EN = '0') then
            Y_c := X"0000";
        end if;
        Y <= Y_c;
    end process;

end Behavioral;

试图做一个4-16解码器,但是,我试图使用整数和SLV之间的转换来进行位索引分配,但是转换不起作用。

ERROR:HDLCompiler:806 - "..." Line 40: Syntax error near "b". 

也试过了

to_integer(unsigned())
integer(unsigned())
integer(to_unsigned())
to_integer(to_unsigned())
use IEEE.ARITH and IEEE.STD_LOGIC.UNSIGNED

没有办法。

【问题讨论】:

  • 这是 34 行长。那么第 40 行是哪一条呢?如果您将conv_integer 更改为to_integer,则发布的代码将编译。

标签: vhdl


【解决方案1】:

转换例程在 numeric_std 包中调用 to_integer,对于无符号数产生一个自然范围整数。

使用variable inputs : integer := to_integer(unsigned(a));,输入将被初始化为a 的初始值,并转换为整数(如果a 未初始化,则可能是0(所有'U's)。

inputs 没有其他赋值,inputs 不会随着 a 值的变化而变化。

将变量inputs声明替换为

    variable inputs: integer range 0 to 15;

将输入范围限制在Y_c的索引范围内。

添加

    inputs := to_integer(unsigned(a));

作为进程的第一个顺序语句。

两个作业:

        Y_c := X"0000";

是多余的。可以消除 elsif:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity fourToSixteenDecoder is
    port ( 
        a:  in  std_logic_vector(0 to 3); 
        EN: in  std_logic;
        Y:  out std_logic_vector(0 to 15)
    );
end entity fourToSixteenDecoder;

architecture Behavioral of fourToSixteenDecoder is
begin
    process(a, EN)
        -- variable inputs : integer := conv_integer(unsigned(a));
        variable inputs: integer range 0 to 15;
        variable Y_c:    std_logic_vector(0 to 15);
    begin
        inputs := to_integer(unsigned(a));  -- ADDED
        Y_c := X"0000";
        if  EN = '1' then
            Y_c(inputs) := '1';
        -- elsif (EN = '0') then
        --     Y_c := X"0000";
        end if;
        Y <= Y_c;
    end process;
end architecture Behavioral;

这会分析、阐述和模拟。注意 if 语句条件周围的括号是不需要的。

一个测试台:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity tb_4to16 is
end entity;

architecture fum of tb_4to16 is
    signal a:   std_logic_vector (0 to 3) := (others => '0');
    signal EN:  std_logic;
    signal Y:   std_logic_vector(0 to 15);
begin
DUT:
        entity work.fourtosixteendecoder
        port map (
            a => a,
            EN => EN,
            Y => Y
        );
STIMULI:
    process
    begin
        for i in 0 to 15 loop
            EN <= '0';
            a <= std_logic_vector(to_unsigned(i,4));
            wait for 10 ns;
            EN <='1';
            wait for 10 ns;
        end loop;
        EN <= '0';
        wait for 10 ns;
        wait;
    end process;
end architecture;

结果:

【讨论】:

    猜你喜欢
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    • 1970-01-01
    • 2016-08-10
    相关资源
    最近更新 更多