【问题标题】:VHDL how to assign a input vector value to an integer signalVHDL如何将输入向量值分配给整数信号
【发布时间】:2015-04-19 02:00:25
【问题描述】:

我是 VHDL 新手,我正在尝试制作一个计数器,它接收来自输入的值并计数到给定值然后输出 1;

例如,输入是一个4位向量“1011”

我尝试设置一个整数信号 a = input = 1011 = 11 十进制,那么如果 b = a = 11 输出 1,否则输出 0 和 b=b+1

我知道我可以通过一系列 if 语句来做到这一点,但我想知道是否有更好的方法,比如将值直接从输入向量分配给整数信号?感谢任何可以提供帮助的人!

【问题讨论】:

  • 我认为这需要澄清一下。因此,您将value 传递给模块,它的count 被初始化为0,然后使用参考时钟输入递增到value,然后输出逻辑1?什么时候重置?
  • 您可能需要某种形式的 if 语句,因为这将允许您使用寄存器定义顺序行为。
  • 非常感谢您的快速回复,是的,这就是我想要做的,我知道我可以通过 if 语句来做到这一点,比如 if input="0000" then a = 0,但我在想有没有更简单的方法呢?

标签: vhdl counter


【解决方案1】:

这是未经测试的,但它是您所追求的一般架构。在 VHDL 中使用 if 语句不是坏习惯;它们是定义顺序(非组合)逻辑所必需的;您只需要谨慎使用它们即可。

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

entity Counter is port (
     enable:  in std_logic; -- Used to increment the counter. (Active high.)
     value:   in std_logic_vector(0 to 3);
     clk:     in std_logic; -- Used to clock the counter.
     reset:   in std_logic; -- Reset the counter. (Active high.)
     output:  out std_logic -- Generates a logic high once the count has been reached.
);
end Counter;

architecture Behavioral of Counter is

    signal count: unsigned(0 to 3);

begin
    process(clk,reset)
    begin
        -- If reset goes high, reset the count.
        if reset='1' then
            count <= "0000";                -- Reset the counter.
            output <= '0';                  -- Set the output low.
        elsif(clk'event and clk='1') then  -- If not reset, and the rising edge of the input clock...
            if enable='1' then             -- If the counter is enabled...
                if count=unsigned(value) then        -- If the count reached the input value...
                    output <= '1';          -- Set the output high.
                else
                    count <= count + 1;    -- Increment the counter.
                end if;
            end if;
        end if;
    end process;
end Behavioral;

【讨论】:

  • 这比我第一次做的要好,当我尝试将信号创建为整数时,我做得太复杂了,然后我不得不使用 16 个 if 语句来关联输入值和整数值,多谢!另外我发现我们实际上可以使用整数类型作为输入,但是我不确定这在合成时是否有效,非常感谢帮助我!!!
  • 别担心,老兄,很高兴能帮到你。
  • 不推荐使用std_logic_arith和std_logic_unsigned,请使用std_numeric。
  • 正如 Paebbles 所说,使用 std_logic_arith 和 std_logic_unsigned 被认为是不好的做法,并且存在一些问题。您应该改用库 NUMERIC_STD。我已经相应地编辑了答案,但我的编辑被拒绝了。
  • 我使用这个例子重新编写了代码:en.wikibooks.org/wiki/VHDL_for_FPGA_Design/… 我没有过多关注库调用。为什么它被认为是不好的做法?是否已弃用?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多