Search code examples
vhdl

VHDL Increment Signal doesn't work properly


I have a problem with my vhdl code. I have a signal whitch I want to increment every phase. So i wrote this code:

--module.vhd enter image description here

library ieee; 

use ieee.std_logic_1164.all; 

--use ieee.numeric_std.all;     

use ieee.std_logic_unsigned.all;   



entity module is 

  port( CLK, RESET: in std_logic; 

    MODE: in  std_logic_vector(1 downto 0); 

    Q: out std_logic_vector(3 downto 0)); 

end; 

 

architecture arch of module is 

--##INSERT YOUR CODE HERE 

signal Q_INT : std_logic_vector(3 downto 0); --local signal 

begin 



process(RESET,CLK)

begin

if CLK = '1' and CLK'event then

    if RESET ='1' then

      Q <= (others => '0');

 else

    Q <= Q_INT;

    

 case MODE is

    when "00" => Q_INT<= Q_INT + "0001";

    when "01" => Q_INT<= Q_INT +"0010";

    when "10" => Q_INT<= Q_INT +"0011";

    when "11" => Q_INT<= Q_INT +"0100";

    when others => null;

    end case;

    Q <= Q_INT;       

end if;

end if;

end process;





    --if (MODE = "00") then

     --Q_INT <= Q_INT + "0001";

     --elsif (MODE = "01") then

     --Q_INT <=Q_INT + "0010";

     --elsif (MODE = "10") then

     --Q_INT <=Q_INT + "0011";

     --elsif (MODE = "11") then

     --Q_INT <=Q_INT + "0100";

     --end if;

     --Q <= Q_INT;

--end process;

--##INSERT YOUR CODE HERE 

end;

And the testbench look like this:

library ieee;

use ieee.std_logic_1164.all;



entity TB_MODULE is

end TB_MODULE;



architecture TESTBENCH of TB_MODULE is

  constant tbase: time:=10 ns;

  constant tcheck: time:=1 ns;

  signal TB_CLK: std_logic;



  component MODULE

    port( CLK, RESET: in std_logic; 

      MODE: in  std_logic_vector(1 downto 0); 

      Q: out std_logic_vector(3 downto 0)); 

  end component;

  signal TB_RESET: std_logic;

  signal TB_MODE: std_logic_vector(1 downto 0);

  signal TB_Q: std_logic_vector(3 downto 0);

  signal expTB_Q: std_logic_vector(3 downto 0);

begin



  CLOCK: process 

    begin 

     TB_CLK <='1';

     wait for tbase/2;

     TB_CLK <='0';

     wait for tbase/2;

  end process CLOCK;



  DUT: MODULE port map(TB_CLK, TB_RESET, TB_MODE, TB_Q);



  STIMULI: process 

  begin

    TB_RESET<='1','0' after 1*tbase, '0' after 2*tbase;



    TB_MODE<="00", "00" after 1*tbase, "01" after 2*tbase, "10" after 3*tbase, 

         "11" after  4*tbase, "00" after  5*tbase; 

     

    expTB_q<=x"0", x"1" after 1*tbase, x"3" after 2*tbase, x"6" after 3*tbase, 

         x"A" after  4*tbase, x"B" after  5*tbase, x"C" after  6*tbase; --then incrementing

    wait; -- end simulation

  end process STIMULI;  



  --simmuli templates

  --STIMULI: process 

  --begin

  --  TB_RESET<='1','0' after 1*tbase, '0' after 2*tbase;

  --

  --  TB_A<='0', '0' after 1*tbase, '1' after 2*tbase, '0' after 3*tbase, 

  --       '0' after  4*tbase, '1' after  5*tbase; 

  --  TB_B<='0', '0' after 1*tbase, '0' after 2*tbase, '0' after 3*tbase, 

  --       '1' after  4*tbase, '1' after  5*tbase;

  --

  --  expTB_q<='0', '0' after 1*tbase, '0' after 2*tbase, '0' after 3*tbase, 

  --       '0' after  4*tbase, '1' after  5*tbase; 

  --  wait; -- end simulation

  --end process STIMULI;  

  --STIMULI: process 

  --begin

      --TB_A<=0', '0' after 1*tbase, '0' after 2*tbase, '0' after 3*tbase, 

      --     '0' after  4*tbase, '0' after  5*tbase, '0' after  6*tbase, '0' after  7*tbase,

      --     '1' after  8*tbase, '1' after  9*tbase, '1' after 10*tbase, '1' after 11*tbase,

      --     '1' after 12*tbase, '1' after 13*tbase, '1' after 14*tbase, '1' after 15*tbase; 

      --TB_B<='0', '0' after 1*tbase, '0' after 2*tbase, '0' after 3*tbase, 

      --     '1' after  4*tbase, '1' after  5*tbase, '1' after  6*tbase, '1' after  7*tbase,

      --     '0' after  8*tbase, '0' after  9*tbase, '0' after 10*tbase, '0' after 11*tbase,

      --     '1' after 12*tbase, '1' after 13*tbase, '1' after 14*tbase, '1' after 15*tbase;

      --TB_C<='0', '0' after 1*tbase, '1' after 2*tbase, '1' after 3*tbase, 

      --     '0' after  4*tbase, '0' after  5*tbase, '1' after  6*tbase, '1' after  7*tbase,

      --     '0' after  8*tbase, '0' after  9*tbase, '1' after 10*tbase, '1' after 11*tbase,

      --     '0' after 12*tbase, '0' after 13*tbase, '1' after 14*tbase, '1' after 15*tbase;

      --TB_D<='0', '1' after 1*tbase, '0' after 2*tbase, '1' after 3*tbase, 

      --     '0' after  4*tbase, '1' after  5*tbase, '0' after  6*tbase, '1' after  7*tbase,

      --     '0' after  8*tbase, '1' after  9*tbase, '0' after 10*tbase, '1' after 11*tbase,

      --     '0' after 12*tbase, '1' after 13*tbase, '0' after 14*tbase, '1' after 15*tbase;

      --expTB_bus<="0000", "0001" after 1*tbase, "0010" after 2*tbase, 

      --   "0011" after  3*tbase, "0100" after  4*tbase, "0101" after  5*tbase, 

      --   "0110" after  6*tbase, "0111" after  7*tbase, "1000" after  8*tbase, 

      --   "1001" after  9*tbase, "1010" after 10*tbase, "1011" after 11*tbase,

      --   "1100" after 12*tbase, "1101" after 13*tbase, "1110" after 14*tbase, 

      --   "1111" after 15*tbase;

      --expTB_buxhex<=x"0", x"1" after 1*tbase, x"2" after 2*tbase, x"3" after 3*tbase, 

      --   x"4" after  4*tbase, x"5" after  5*tbase, x"6" after  6*tbase, x"7" after  7*tbase,  

      --   x"8" after  8*tbase, x"9" after  9*tbase, x"A" after 10*tbase, x"B" after 11*tbase,

      --   x"C" after 12*tbase, x"D" after 13*tbase, x"E" after 14*tbase, x"F" after 15*tbase;

  --end process STIMULI;  



end TESTBENCH;

Now my Problem is that i don't get the right values for Q_Int. It's always an 'X'. (But i want to get a number. Thanks for the enter image description herehelp


Solution

  • Your code is syntactically correct, and will increment Q_int by the required amount. The problem is that Q_int is initialised to "UUUU" and is not assigned a value on reset. And when you add anything to "UUUU", you get "UUUU".

    The answer here is to either: Give Q_int and initial value: eg.

    signal Q_int : std_logic_vector(3 downto 0) := "0000";
    

    or assign Q_int a value when reset occurs

    if RESET ='1' then
      Q_int <= "0000";
      --etc
    

    The second case will also avoid the reset->clock enable connection you will have created by not resetting Q_int when Q is reset.

    On a side note, you are using non-standard VHDL library std_logic_unsigned . It is recommended that you stick to standard vhdl library numeric_std and use the unsigned type, or with VHDL 2008 you can use the ieee.numeric_std_unsigned package, that allows you to use std_logic_vector as a numerical unsigned value.