Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   VHDL (http://www.velocityreviews.com/forums/f18-vhdl.html)
-   -   Indexing the bits of an Integer? (http://www.velocityreviews.com/forums/t23427-indexing-the-bits-of-an-integer.html)

 Takuon Soho 03-04-2005 05:07 AM

Indexing the bits of an Integer?

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

 Neo 03-04-2005 07:05 AM

Re: Indexing the bits of an Integer?

assign it to a temp signal and then use bit-2 of that signal.

 Brian Drummond 03-04-2005 01:08 PM

Re: Indexing the bits of an Integer?

On Fri, 04 Mar 2005 05:07:14 GMT, "Takuon Soho" <Tak@somwhere.net>
wrote:

>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

 Takuon Soho 03-04-2005 03:29 PM

Re: Indexing the bits of an Integer?

OK, thanks.

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

Tak

"Brian Drummond" <brian@shapes.demon.co.uk> wrote in message
news:tugg21lsrri1or1qcho9ev6gibcv8fd41c@4ax.com...
> On Fri, 04 Mar 2005 05:07:14 GMT, "Takuon Soho" <Tak@somwhere.net>
> wrote:
>
>>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

 Charles Gardiner 03-04-2005 09:09 PM

Re: Indexing the bits of an Integer?

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

 Brian Drummond 03-06-2005 09:56 AM

Re: Indexing the bits of an Integer?

On Fri, 04 Mar 2005 15:29:38 GMT, "Takuon Soho" <Tak@somwhere.net>
wrote:

>OK, thanks.
>
>I was trying to save space by using 4 bit integer subtype.
>

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

 Takuon Soho 03-07-2005 04:34 AM

Re: Indexing the bits of an Integer?

Thanks, this seems quite useful.

Tak

"Charles Gardiner" <gardiner.charles@munich.de> wrote in message
news:d0aiqn\$kf\$04\$1@news.t-online.com...
> 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

 All times are GMT. The time now is 02:58 AM.