VHDL - How to adding 1 to STD_LOGIC_VECTOR?

General Tech Learning Aids/Tools 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

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) work correctly --but other process have error by adding 1.

profilepic.png
manpreet 2 years ago

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;

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.