[SOLVED] ways to determine odd and even count values in VHDL

Status
Not open for further replies.

rakeshk.r

Member level 2
Joined
Nov 12, 2013
Messages
47
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Visit site
Activity points
421
I guess there are several ways to find the odd or even count values. I have mentioned 1 method in full vhdl code and other 2 methods as snippet. I would like to know other methods to determine these odd or even count values and they should also be synthesizable. Could some one help me on this. Thank you.
Code:
library ieee;
use std_logic_1164.all;
use ieee.numeric_std.all;
entity blk1
port( clk : IN std_logic;
        rst : IN std_logic; 
        x  : OUT integer );
end blk1;

architecture test of blk1 is 
begin
p0: process (clk,rst)
variable i : integer;  -- counter variable
begin
if (rst = '1' ) then 
    i  :=  0;   
    x <= 0;
elsif (rising_edge(clk)) then 
-- method1: 
    i := i+1;          
    if (i mod 2 /= 0) then 
           x <= i ; -- odd counter values
    else 
           x <= i ; -- even counter values
    end if;
end if;
end process; 
end architecture blk1;
-- alternative methods
-- method2:
if ((-1)**i =-1) then -- note! this is not synthesizable
-- odd counter values
else
-- even counter values
end if;
-- method3:
if (i(0)='1') then -- assume here, 'i' is declared as , i : unsigned (7 downto 0);
-- odd counter values
else
-- even counter values
end if;
 

why not just convert to a signed type and check the LSB?

Also, your simulation will fall over when i=2^31-1 as it will go out of range and cause an error (it will work on hardware as it will just roll over to -2^31 - but integers wont in simulation).
 
@TrickyDicky In my actually code I have an enable signal which will make my counter variable go to zero. so it will take care of not going beyond the range. BTW I have to mutiply the counter value with 0.5 and take the integer part of the result to address my array. Right now I am keeping my counter as unsigned for now. But I am not sure if I can represent 0.5 as unsigned ? so this made to ask a question, what other methods exist to detect counter values. so I can see if something matches my need.
 
Last edited:

To detect odd and even, the simplest way is check the LSB. Without posting your real code, it is difficult to see what you mean. The code in the first post uses an integer which is a signed value.

As for fractional numbers, you can use unsigned without a problem - you just need to keep track of where the fraction/separation is.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…