Search code examples
vhdl

VHDL - How to add 1 to STD_LOGIC_VECTOR?


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;

-- entity part contain R for output of Register
    
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 ;

-- it have to parallel process    

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;

-- my error in this step    

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

end behavioral;

Main process (reg) works correctly, but other process has error adding 1.


Solution

  • Sorry to say, but there are quite a lot of things wrong with your code...

    1. You are mixing combinatorial and sequential code in the same process, which is poor coding style. If coded correctly, there really is no need for the variable R_temp.
    2. The sensitivity list is a simulation aid only, yet you are trying to use it as a conditional (when inc changes, increment R). This will not work in hardware. When starting out with VHDL, my suggestion is to use VHDL_2008 and always use process(all) as the sensitivity list. This avoids beginner errors, since it reflects what synthesis does.
    3. You are creating a combinatorial loop in the second process. This will not show up in simulation because of 2. above, but will cause errors in synthesis.
    4. As already mentioned by baldyHDL, you are assigning to R in multiple processes.
    5. Prefer unsigned to std_logic_vector when dealing with numbers. Generally you should not include std_logic_arith, nor std_logic_unsigned, just std_logic_1164 and numeric_std.
    6. Adding a std_logic value (such as '1') is not standard, or at least not supported by all tools. Simply use an integer instead: R <= R + 1;

    By your code, I gather that you are trying to write a counter with increment, load and clear signals. I don't just want to give you the code (that would ruin the learning experience for you), but try to fit the entire counter into a single process, using the following template:

    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;