VHDL - 如何将1添加到STD_LOGIC_VECTOR?

Moh*_*ati 3 vhdl

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
use ieee.numeric_std.all;
Run Code Online (Sandbox Code Playgroud)

- 实体部分包含R输出寄存器

entity register_16 is
    port(   input:  in std_logic_vector(15 downto 0);
        ld, inc, clk, clr:  in std_logic;
        R: buffer std_logic_vector(15 downto 0));
end register_16 ;
Run Code Online (Sandbox Code Playgroud)

- 它必须并行处理

architecture behavioral of register_16 is
begin


reg: process (input, ld, clk, clr)
    variable R_temp : std_logic_vector(15 downto 0);
begin
    if (clr='1') then
        R_temp := b"0000000000000000";
    elsif (clk'event and clk='1') then
        if (ld='1') then
            R_temp := input;
        end if;
    end if;
    R <= R_temp;
end process reg;
Run Code Online (Sandbox Code Playgroud)

- 这一步有误

inc_R: process (inc)
begin
    R <= R+'1';
end process inc_R;


end behavioral ;
Run Code Online (Sandbox Code Playgroud)

- 主进程(reg)正常工作 - 但是其他进程因添加1而出错.

Sim*_*mon 10

你必须再次将向量转换为一个整数,这是一个例子:

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

signal in  : std_logic_vector( 7 downto 0 );
signal out : std_logic_vector( 7 downto 0 );

out <= std_logic_vector(to_unsigned(to_integer(unsigned( in )) + 1, 8));
Run Code Online (Sandbox Code Playgroud)

有关类型转换的好照片,请看这里:

http://www.bitweenie.com/listings/vhdl-type-conversion/


zen*_*hoy 6

很抱歉,您的代码存在很多问题......

  1. 您在同一过程中混合组合和顺序代码,这是一种糟糕的编码风格.如果编码正确,则实际上不需要变量R_temp.
  2. 灵敏度列表仅是模拟辅助,但您尝试将其用作条件(当inc更改时,增加R).这在硬件中不起作用.当开始使用VHDL时,我的建议是使用VHDL_2008并始终使用process(all)作为灵敏度列表.这避免了初学者错误,因为它反映了合成的作用.
  3. 您正在第二个过程中创建组合循环.由于上述2.这将不会出现在模拟中,但会导致合成错误.
  4. 正如baldyHDL已经提到的,您在多个进程中分配给R.
  5. 在处理数字时,首选unsigned to std_logic_vector.通常,您不应该包括std_logic_arith和std_logic_unsigned,只需要std_logic_1164和numeric_std.
  6. 添加std_logic值(例如"1")不是标准的,或者至少不是所有工具都支持.只需使用整数:R <= R + 1;

通过您的代码,我收集到您正在尝试使用增量,负载和清除信号来编写计数器.我不只是想给你代码(这会破坏你的学习经验),而是尝试使用以下模板将整个计数器整合到一个进程中:

process(clk) begin
    if(rising_edge(clk)) then
        -- Your code here vvv
        if(clr = '1') then

        elsif(ld = '1') then

        elsif(inc = '1') then

        end if;
        -- Your code here ^^^
    end if;
end process;
Run Code Online (Sandbox Code Playgroud)