EmbDev.net

Forum: FPGA, VHDL & Verilog VHDL, MSF 5 bit counter


von Edoardo B. (edob95)


Rate this post
useful
not useful
Hello everyone,
I'm an italian student, so I apologize for my english, and I'm new in 
the forum, I have to do an exercise: the creation by MSF of a 5-bit 
counter using VHDL.
I have various input signals: a STOP signal which stops the count at the 
moment, a RESTART signal that resume the count, an UP_DOWN signal to 
choose the direction of the count and the signal of RESET.
I have alse 3 output signals: a signal END_C which indicates the end of 
the count, a signal START_C which indicates the start and a Q signal 
which indicates the value of the count.
This is my VHDL code:
1
library ieee;
2
use ieee.std_logic_1164.all;
3
use ieee.std_logic_unsigned.all;
4
5
entity esercizio3 is
6
port(CK, stop, restart, reset, up_down: in std_logic;
7
       end_c, start_c: out std_logic;
8
      Q: out std_logic_vector(4 downto 0));
9
end esercizio3;
10
11
architecture arc of esercizio3 is
12
    type stato is(ST0, ST1, ST2, ST3);
13
    signal ps, ns: stato;
14
    signal temp: std_logic_vector(4 downto 0);
15
    begin
16
      seq_proc: process(CK, RESET)
17
       begin
18
        if(RESET='0')then
19
        ps<=ST0;
20
        elsif(rising_edge(CK)) then
21
        ps<=ns;
22
        end if;
23
      end process seq_proc;
24
25
      comb_proc:process(RESTART, STOP, UP_DOWN, PS)
26
      begin
27
      end_c<='0';
28
      start_c<='0';
29
       case ps is
30
       when ST0=>
31
         temp<="00000";
32
         if(stop='0')then
33
         ns<=ST0;
34
         elsif(up_down='0')then
35
         ns<=ST1;
36
         else
37
         ns<=ST2;
38
         end if;
39
40
      when ST1=>
41
        temp<=temp-"00001";
42
        if(stop='0')then
43
        ns<=ST3;
44
        elsif(up_down='0')then
45
        ns<=ST1;
46
        else
47
        ns<=ST2;
48
        end if;
49
50
     when ST2=>
51
        temp<=temp+"00001";
52
        if(stop='0')then
53
        ns<=ST3;
54
        elsif(up_down<='0')then
55
        ns<=ST1;
56
        else
57
        ns<=ST2;
58
        end if;
59
60
     when ST3=>
61
        if(Restart='1')then
62
        ns<=ST3;
63
        elsif(up_down='0')then
64
        ns<=ST1;
65
        else
66
        ns<=ST2;
67
        end if;
68
        when others=>
69
        ns<=ST0;
70
     end case;
71
     Q<=temp;
72
     if(temp="00000")then
73
     start_c<='1';
74
     elsif(temp="11111")then
75
     end_c<='1';
76
     end if;
77
   end process comb_proc;
78
end arc;

The compilation is successful but I get an error in model sim which is:

#**Error: (vsim-3601) Iteration limit reached at time 15ns.

I suppose there is a loop that does not allow proper execution.
Can you help me fix it? Thanks you.

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


Rate this post
useful
not useful
Edoardo Bernardi wrote:
> Can you help me fix it?
Your counter is inside a non-clocked process and forms therefore a 
combinatorial loop. Try to translate this: (its German) 
http://www.lothar-miller.de/s9y/archives/42-Kombinatorische-Schleifen.html

To keep things short: a counter must have a clock signal.

von Edoardo B. (edob95)


Rate this post
useful
not useful
Ok so i have an infinite loop but how can I move the counter into a 
cloked process?? Can you please write me a couple of code strings, 
because this is the first time I realze a MSF using VHDL and I'm not 
practical with it??
Thanks you.

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


Rate this post
useful
not useful
One minor question in advance: what's MSF?

Edoardo Bernardi wrote:
> so i have an infinite loop
No. You have a combinatorial loop. That's something entirely 
different...

von Achim S. (Guest)


Rate this post
useful
not useful
Lothar Miller wrote:
> One minor question in advance: what's MSF?

I guess a "macchina a stati finiti" (known as FSM in other language ;-)

von Edoardo B. (edob95)


Rate this post
useful
not useful
Yes I guess FSM but I wrote it in italian.
Anyway how can I solve my problem? I tried to move the counter into a
cloked process in different way but anything worked.

von Achim S. (Guest)


Rate this post
useful
not useful
Edoardo Bernardi wrote:
> I tried to move the counter into a
> cloked process in different way but anything worked.

What exactly is not working? How does your code look like now, and what 
error messages do you get?

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


Rate this post
useful
not useful
Achim S. wrote:
> What exactly is not working?
With the code above the counter forms a (gated) combinatorial loop.

Edoardo Bernardi wrote:
> Anyway how can I solve my problem?
If you wnat to use the 2 process style (1 clocked for the flipflops plus 
1 combinatorial), then you must implement a counter_present and a 
counter_next, as you did already for the FSM.
Keep in mind: even each counter is a finite state machine with defined.

First some hints:
1. Why do your states have such indifferent names like ST0, ST1, ST2, 
ST3?
Use SPEAKING names for your own data type.

2. And think about clever identation. It helps reading and understanding 
source code very much.

3. Do NOT use std_logic_vectors for calculations:
1
    signal temp: std_logic_vector(4 downto 0);
2
    :
3
        temp<=temp-"00001";
4
    :
5
        temp<=temp+"00001";
No one knows whether this calculation above is signed or unsigned...

4. "up_down" is a stupid name for a signal. Does it mean the counter 
counts UP and DOWN at the same time? Or what? Is '0' = UP or is '1' = 
UP?

5. "when others =>"
In your own type stato there is no other sate! All of the 4 states 
ST!..4 are explicitly decoded in the case. So: why "when others"?


But if I had to do that job with using of a FSM for the counter 
management, this could be my result:
1
library ieee;
2
use ieee.std_logic_1164.all;
3
use ieee.numeric_std.all;
4
5
entity esercizio3 is
6
port(CK, stop, restart, reset, up_down: in std_logic;
7
     end_c, start_c: out std_logic;
8
     Q: out std_logic_vector(4 downto 0)
9
    );
10
end esercizio3;
11
12
architecture arc of esercizio3 is
13
  type stato is(IDLE, COUNT);
14
  signal state: stato;
15
  signal cnt: integer range 0 to 31;
16
begin
17
   seq_proc: process(CK)
18
   begin
19
      if rising_edge(CK) then
20
21
         case state is
22
            when IDLE =>
23
               if restart='1' then
24
                  state <= COUNT;
25
               end if;
26
27
            when COUNT =>
28
               if up_down='0' then  -- count UP
29
                  if cnt=31 then  cnt <= 0; -- overflow
30
                  else            cnt <= cnt+1;
31
                  end if;
32
               else                 -- count DOWN
33
                  if cnt=0 then  cnt <= 31; -- underflow
34
                  else           cnt <= cnt-1;
35
                  end if;
36
               end if;
37
               if stop='1' then
38
                  state <= IDLE;
39
               end if;
40
         end case;
41
42
         -- handle the reset as a synchronous signal
43
         if reset='1' then
44
            cnt <= 0;
45
            state <= IDLE;
46
         end if;
47
      end if;
48
   end process;
49
50
   -- some concurrent assignments
51
   Q <= std_logic_vector(to_unsigned(cnt,5));   
52
   start_c <= '1' when cnt=0  else '0';
53
   end_c   <= '1' when cnt=31 else '0';
54
55
end arc;

: Edited by Moderator
von Achim S. (Guest)


Rate this post
useful
not useful
Lothar Miller wrote:
> With the code above the counter forms a (gated) combinatorial loop.

that's true, and in fact I had phrased my question wrongly. What I 
really wanted to know was how the actual code looks like and what 
error messages Edoardo gets, after he moved the counter to a clocked 
process.

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


Rate this post
useful
not useful
Achim S. wrote:
> how the actual code looks like and what error messages Edoardo gets,
> after he moved the counter to a clocked process.
Indeed, the latest code and its problems may be of interest here...  ;-)

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.