EmbDev.net

Forum: FPGA, VHDL & Verilog VHDL Counter Problem (Please help)


von Icy S. (icysnow)


Rate this post
useful
not useful
I am not able to do the increment and the testbench for this code.

Question:

A system has a 3-bit input D_IN which is read in at every positive edge 
of a clock input CLK. If the current D_IN is greater than the previous 
D_IN by at least 2, a 3-bit output Count is incremented. If D_IN is 0 
for 3 consecutive CLK cycles, the count is reset. When Count reaches 6, 
the system will assert an output Alarm and the Count will not increase 
further, till it is reset by giving 0s at D_IN for 3 consecutive cycles.

Test case:
1
Clk  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
2
3
D_IN 0 0 0 2 4 7 6 0 2 4  6  3  5  7  0  0  0  0

code
1
library IEEE;
2
use IEEE.STD_LOGIC_1164.ALL;
3
use IEEE.NUMERIC_STD.ALL;
4
5
entity johnson_counter is
6
port ( 
7
    D_in  : in std_logic_vector(2 downto 0);
8
    alarm : out std_logic;
9
    CLK_I : in std_logic
10
    );
11
end johnson_counter;
12
13
architecture Behavioral of johnson_counter is
14
15
signal xnew, xold, count1, count2: unsigned(2 downto 0):=(others => '0');
16
17
begin
18
19
20
process(CLK_I)
21
begin
22
if( rising_edge(CLK_I) ) then
23
 xnew <= std_logic_vector(unsigned(D_in));
24
 xnew <= xnew - "010";
25
    if (xnew => xold) then
26
        count1 <= count1 + "1";
27
    if(xnew = 0) then
28
            count2 <= count2 + "1";
29
      if(xnew /= 0) then
30
            count2 <= "0";
31
      if(count2 = "011") then
32
            alarm <= "1";
33
    end if;
34
      end if;
35
      end if;
36
      end if;
37
end if;
38
end process;

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


Rate this post
useful
not useful
Icy Snow wrote:
> code
Whats the problem with it? Whats the problem at all?

Is this any good for:
> xold
It isn't used in the code, so it must be obsolete...

This will create an error:
1
  xnew <= std_logic_vector(unsigned(D_in)); -- what type is xnew?

This does not behave like you desired it:
1
 process(CLK_I)
2
 begin
3
 if( rising_edge(CLK_I) ) then
4
   xnew <= std_logic_vector(unsigned(D_in));
5
   xnew <= xnew - "010";
You know about the behaviour of signal in a process?
You will find out, that the fist assignment is simply ignored, and 
therefore xnew is simply a down-counter with a decrement of 2. In fact 
you could write this process without any change in behaviour like this:
1
 process(CLK_I)
2
 begin
3
 if( rising_edge(CLK_I) ) then
4
   xnew <= xnew - "010";

> Test case:
> Clk  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
> D_IN 0 0 0 2 4 7 6 0 2 4  6  3  5  7  0  0  0  0
Did you create a test bench out of this data? How does it look like?


All in all you are a lucky man: you don't have to do last milleniums 
technology like structural description of a multiplier built of 
halfadders. Insted you got a problem fairly near to what is the daily 
work of a FPGA designer.

BTW:
Did you see this above the edit box?
1
Formatting options
2
    [vhdl]VHDL code[/vhdl]

: Edited by Moderator
von and (Guest)


Rate this post
useful
not useful
this
1
if (xnew => xold) then
...
won't work as you expect it to work.
Try:
1
if (xnew >= xold) then

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.