首页 > 解决方案 > std_logic_vector (to_unsigned(X, Y));

问题描述

这是一个测试台,我有这些信号:

signal  DATA_INPUT  :std_logic_vector(0 to  31);
signal  rand_num    :integer;

我试图通过以下方式将随机数放入这个 32 位信号中:

DATA_INPUT  <=  std_logic_vector(to_unsigned(rand_num, 32));

我的问题是,我需要超过 31 位的数字,但是当随机数超过这个数字时:2147483647 这是 INTEGER'high,我收到这个错误:

near "4294967295": (vcom-119) Integer value exceeds INTEGER'high.
# ** Error: tb.vhd: (vcom-1144) Value -1 (of type
std.STANDARD.NATURAL) is out of range 0 to 2147483647.

我尝试修改 TO_UNSIGNED() 函数并将 NATURAL 输入更改为其他内容,但没有。

这是来自 IEEE 和 RANDOOM GENERATOR 进程的 TO_UNSIGNED 函数:

function TO_UNSIGNED(ARG, SIZE: NATURAL) return UNSIGNED is
        variable RESULT: UNSIGNED (SIZE-1 downto 0);
        variable i_val: NATURAl := ARG;
        begin
            if (SIZE < 1) then return NAU; end if;
            for i in 0 to RESULT'left loop
            if (i_val MOD 2) = 0 then
               RESULT(i) := '0';
            else RESULT(i) := '1';
              end if;
            i_val := i_val/2;
            end loop;
            if not(i_val=0) then
            assert NO_WARNING 
            report "numeric_std.TO_UNSIGNED : vector truncated"
            severity WARNING;
            end if;
        return RESULT;
    end TO_UNSIGNED;

随机发生器:

process
    variable seed1, seed2   :positive;
    variable rand   :real;
    variable    range_of_rand   :real:= 46340.0;
begin
    uniform(seed1, seed2, rand);
    rand_num <= integer(rand*range_of_rand);
    wait for 1 ns;
end process;

标签: vhdlmodelsim

解决方案


您可以通过组合两个来制作一个新的更大的随机数。

最简单的解决方案是将两个随机整数转换为向量,然后连接,直到获得所需的位数。这为您提供了 64 位:

DATA_INPUT <= std_logic_vector(to_unsigned(rand_num, 32)) & std_logic_vector(to_unsigned(rand_num, 32));

推荐阅读