EmbDev.net

Forum: FPGA, VHDL & Verilog decoder in vhdl dont work in simulation.


von ee_vhdl (Guest)


Rate this post
useful
not useful
hi, i wrote i code for a 3 to 8 decoder in modelsim and it dosent work 
proprly in the simulation.
when i force "000" it works, meaning the output is "10000000" and also 
it works with "001" that gives the output "01000000". but the other 
inputs give those same outputs over and over agian. not only that, but 
when i force "010" in the input it enters "000" in the input for some 
reason.

code:
1
library ieee;
2
use ieee.std_logic_1164.all; 
3
4
entity Decoder is 
5
port (  a  : in std_logic_vector (2 downto 0); 
6
        en : in std_logic; 
7
        f  : out std_logic_vector (7 downto 0)
8
); 
9
end Decoder; 
10
11
architecture bhv of Decoder is 
12
begin 
13
  process (en,a) 
14
  begin
15
  if en = '1' then
16
      if    a="000" then f<="10000000" after 1 ps; 
17
      elsif a="001" then f<="01000000" after 1 ps; 
18
      elsif a="010" then f<="00100000" after 1 ps; 
19
      elsif a="011" then f<="00010000" after 1 ps; 
20
      elsif a="100" then f<="00001000" after 1 ps; 
21
      elsif a="101" then f<="00000100" after 1 ps; 
22
      elsif a="110" then f<="00000010" after 1 ps;
23
      elsif a="111" then f<="00000001" after 1 ps; 
24
      else null; 
25
      end if; 
26
    end if; 
27
  end process; 
28
end bhv;

: Edited by Moderator
von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Rate this post
useful
not useful
ee_vhdl wrote:
> when i force "000"
> ...
> but when i force "010"
Why don't you simply write a test bench with 20 lines of code?
Then you will not need that questionable "force"-construct.

ee_vhdl wrote:
> else null;
You know that
a) you are bulding a latch with this    or
b) this line is absolutely useless?

von ee_vhdl (Guest)


Rate this post
useful
not useful
what do you mean about the 20 line code?
i have  a project that a decoder is just one of the components, i need 
to shoe the prop' that the decoder works inside the simulation.

why is the null useless?

von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Rate this post
useful
not useful
ee_vhdl wrote:
> i have  a project that a decoder is just one of the components, i need
> to shoe the prop' that the decoder works inside the simulation.
So write a test bench for that single component "decoder".

> what do you mean about the 20 line code?
That testbench will be not much longer than 20 lines of VHDL code.

> why is the null useless?
In which case could it be usefull? How could that case happen?
I would write the last lines if that if-elsif-query like that:
1
      elsif a="110" then f<="00000010" after 1 ps;
2
      else               f<="00000001" after 1 ps; 
3
      end if;

> after 1 ps;
My hint here: do not use symbolic delays in a behavioural description. 
You may encounter curious effects now and then...

von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Attached files:

Rate this post
useful
not useful
Lothar M. wrote:
> That testbench will be not much longer than 20 lines of VHDL code.
Ok, skipping some blank lines...
1
LIBRARY ieee;
2
USE ieee.std_logic_1164.ALL;
3
 
4
ENTITY tb_decoder IS
5
END tb_decoder;
6
 
7
ARCHITECTURE behavior OF tb_decoder IS 
8
9
    COMPONENT Decoder
10
    PORT( a  : IN  std_logic_vector(2 downto 0);
11
          en : IN  std_logic;
12
          f  : OUT  std_logic_vector(7 downto 0));
13
    END COMPONENT;
14
15
   signal a : std_logic_vector(2 downto 0) := (others => '0');
16
   signal en : std_logic := '1';
17
   signal f : std_logic_vector(7 downto 0);
18
19
BEGIN
20
   uut: Decoder PORT MAP ( a => a,  en => en, f => f);
21
22
   a <= "000" after  0ns, "001" after 10ns, "010" after 20ns, "011" after 30ns,
23
        "100" after 40ns, "101" after 50ns, "110" after 60ns, "111" after 70ns;
24
25
END;
Simulation result: your decoder looks fine.
See the attached WF_decoder_1.png

BUT...
there are latches in the desgign: when en='0' then the last result is 
stored (WF_decoder_2.png). Thats not the usual behaviour for such a 
device. I would change the code in a way that the result is "00000000" 
when en='0'.
1
architecture bhv of Decoder is 
2
begin 
3
  process (en,a) 
4
  begin
5
    if en = '1' then
6
      if    a="000" then f<="10000000" after 1 ps; 
7
      elsif a="001" then f<="01000000" after 1 ps; 
8
      elsif a="010" then f<="00100000" after 1 ps; 
9
      elsif a="011" then f<="00010000" after 1 ps; 
10
      elsif a="100" then f<="00001000" after 1 ps; 
11
      elsif a="101" then f<="00000100" after 1 ps; 
12
      elsif a="110" then f<="00000010" after 1 ps;
13
      else               f<="00000001" after 1 ps; 
14
      end if; 
15
    else
16
      f<="00000000";
17
    end if; 
18
  end process; 
19
end bhv;
The result can be seen in WF_decoder_2.png

The test bench for tha last two waveforms looks like this:
1
:
2
:
3
   en <= '0' after 85 ns, '1' after 115ns, '0' after 135ns, '1' after 145ns;
4
   
5
   a <= "000" after  0ns, "001" after 10ns, "010" after 20ns, "011" after 30ns,
6
        "100" after 40ns, "101" after 50ns, "110" after 60ns, "111" after 70ns,
7
        "000" after 80ns, "001" after 90ns, "010" after 100ns, "011" after 110ns,
8
        "100" after 120ns, "101" after 130ns, "110" after 140ns, "111" after 150ns;
9
END;

: Edited by Moderator
von ee_vhdl (Guest)


Rate this post
useful
not useful
thx a lot man!

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.