【问题标题】:calculating average of std_logic_vector variables in a function计算函数中 std_logic_vector 变量的平均值
【发布时间】:2011-08-10 19:56:16
【问题描述】:

我正在尝试用 VHDL 编写两个封装函数,平均两个和四个 8 位 std_logic_vector 输入信号。当我实现以下 avgtwo 代码时,我得到的答案比我的模拟中预期的大 2 倍。这就像返回操作'sum(sum'high downto 1)'没有取sum的高8位......我错过了什么?

    library IEEE;
    use IEEE.STD_LOGIC_1164.all;
    use IEEE.STD_LOGIC_UNSIGNED.all;
    use IEEE.NUMERIC_STD.ALL;

    package my_package is
        function avgtwo(v1, v2 : std_logic_vector) return std_logic_vector;
        function avgfour(v1, v2, v3, v4 : std_logic_vector) return std_logic_vector;
    end my_package;

    package body my_package is
        function avgtwo
        (
            v1, v2 : std_logic_vector
        )
        return std_logic_vector is
        variable sum : std_logic_vector(v1'high+1 downto 0) := (others => '0');
        begin
            sum := std_logic_vector(unsigned(v1)) + std_logic_vector(unsigned(v2));
            return sum(sum'high downto 1);
        end function avgtwo;


        function avgfour
        (
            v1, v2, v3, v4 : std_logic_vector
        )
        return std_logic_vector is
        variable sum : std_logic_vector(v1'high+2 downto 0) := (others => '0');
        begin
            sum := std_logic_vector(unsigned(v1)) + std_logic_vector(unsigned(v2)) + std_logic_vector(unsigned(v3)) + std_logic_vector(unsigned(v4));
            return sum(sum'high downto 2);
        end function avgfour;

    end my_package;

【问题讨论】:

    标签: function package average


    【解决方案1】:

    我发现我做错了什么。您必须确保添加两个与接收变量大小相同的 std_logic_vector。所以我编辑了总结行如下:

    在'avgtwo'函数中:

    sum := std_logic_vector(unsigned('0' & v1)) + std_logic_vector(unsigned('0' & v2));
    

    与 avgfour 函数类似 - 但使用“00”而不是“0”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-26
      • 2021-02-14
      • 1970-01-01
      • 1970-01-01
      • 2020-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多