Indexing the bits of an Integer?

T

Takuon Soho

I wanted to output a 1 or 0 signal
based on bit number 2 of a 4 bit counter.
i.e. if bit 2 is high, set the line high
and if it is low, set the line low.

Using the below code
where my_counter is a subtype of integer (4 bit counter).
i.e.
subtype counter_ty is integer range 0 to 15; -- 4 bit counter,
variable my_counter : counter_ty := 0;

-- Next line compiles but will not sythesize
some_signal <= conv_std_logic_vector(my_counter, 4)(2);

The code compiles OK but refuses to synthesize
with the message something like "complex indexes not supported".

How can I get at the bit 2 of the counter so as to output
a hi or low signal?? It looks like you cannot directly
index the bit of an integer i.e. some_integer(2);

Thanks
Tak
 
B

Brian Drummond

I wanted to output a 1 or 0 signal
based on bit number 2 of a 4 bit counter.
subtype counter_ty is integer range 0 to 15; -- 4 bit counter,
variable my_counter : counter_ty := 0;
How can I get at the bit 2 of the counter so as to output
a hi or low signal?? It looks like you cannot directly
index the bit of an integer i.e. some_integer(2);

You can't. To extract a bit from something indexable, you have to use an
indexable data type, such as one based on a vector of bits.

Best is to include standard libraries std_logic_1164 and numeric_std,
and to make "mycounter" of type unsigned. Then any ranges extracted from
it are also unsigned; single bits will be of type std_logic.

Arithmetic between unsigned (or signed) and integer works; and you can
easily convert to the underlying std_logic_vector type whenever you need
to.

- Brian
 
T

Takuon Soho

OK, thanks.

I was trying to save space by using 4 bit integer subtype.

Will use unsigned instead.

Tak
 
C

Charles Gardiner

There may sometimes be reasons for using an integer sub-type as type for
a counter (indexing etc.). With a small function you can get the bit
position you were looking for:

Library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
.. . . .
sig1 <= get_bit(your_int, your_pos);
.. . . .
.. . . .
function get_bit(iv : integer; pos; integer) return std_logic;
.. . . .
function get_bit(iv : integer; pos; integer) return std_logic is
constant size_c : integer := 32; -- An arbitrary value larger than
-- the highest bit-pos you will

-- look for
begin
-- assumes you count from 1 up to MSB
if ((to_unsigned(iv, size_c) and
to_unsigned(2 ** (pos -1), size_c)) =
to_unsigned(0, size_c)) then
return '0';
else
return '1';
end if;
end get_bit;

Hope this helps,
Charles
 
B

Brian Drummond

OK, thanks.

I was trying to save space by using 4 bit integer subtype.

Will use unsigned instead.

That shouldn't save any space over using a 4-bit unsigned.
There may be a slight difference in simulation speed, but that shouldn't
matter until you are doing really large simulations.

- Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top