EmbDev.net

Forum: FPGA, VHDL & Verilog Getting a vector from within another vector


von David A. (daveatol)


Rate this post
useful
not useful
Hi,

I'm very new to VHDL, so sorry in advance for my ignorant questions.

I would like to extract 8 bits from a couple of vectors. In the first 
case, the vector is 128bits and is accessed as if it were 16 bytes. The 
second vector is some large number of bits, and the byte to be extracted 
is not byte-aligned.

Here's the code I've tried:
1
byteData : buffer std_logic_vector(15 downto 0);
2
constant bitData : std_logic_vector(518 downto 0);
3
variable tempByte : std_logic_vector(7 downto 0);
4
5
tempByte := byteData((byteIndex * 8 + 7) downto (byteIndex * 8)) xor bitData((bitIndex+7) downto bitIndex);
6
7
-- bitIndex and byteIndex as std_logic_vectors too
Quartus doesn't like my use of the '*' operator.

What is the best way to do this kind of thing? Am I able to indirectly 
access the bits at certain offsets in the vectors? Should I be using 
shifts (e.g. sll) instead?

Thanks for any insight you can provide.

Cheers,
Dave

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


Rate this post
useful
not useful
Usually its a good idea to tell the exact error message...

What libraries do you use?
As a hint: most probably you have a problem with types.
1. A array index MUST be a integer
2. You cannot simply multiply a vector with an integer

von David A. (daveatol)


Rate this post
useful
not useful
Hi Lothar,

Thanks for the reply. Yes, there is a problem with types.

Ok, so after converting to integers, it compiles, thanks. Is there a 
better/more concise way to access an 8 bit vector within a longer 
std_logic_vector than the following?
1
tempByte_from_byte_aligned := byteData((to_integer(unsigned(byteIndex)) * 8 + 7) downto (to_integer(unsigned(byteIndex)) * 8));
2
tempByte_from_bit_aligned = bitData((to_integer(unsigned(bitIndex))+7) downto to_integer(unsigned(bitIndex)));


This is the info related to your reply, though not relevant anymore.

The errors for the line beginning "tempData :=" are:
1
Error (10327): VHDL error at ...: can't determine definition of operator ""*"" -- found 0 possible definitions
2
Error (10327): VHDL error at ...: can't determine definition of operator ""+"" -- found 0 possible definitions
3
Error (10476): VHDL error at ...: type of identifier "bitIndex" does not agree with its usage as "natural" type

My libraries are:
1
library  ieee;
2
use ieee.std_logic_1164.all;
3
use ieee.std_logic_unsigned.all;
4
use ieee.numeric_std.all;

Cheers,
Dave

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


Rate this post
useful
not useful
First: kick off the std_logic_unsigned.
Because together with the recommended numeric_std you can get double and 
ambiguous definitions for some operations...

Second: don't calculate with unconstrained vectors!
Use unsigned signals instead of std_logic vectors!
(and of course: with the numeric_std you cannot do calculations with 
vectors! You must cast the vector to signed or unsigned before 
calculations)

von David A. (daveatol)


Rate this post
useful
not useful
> Second: don't calculate with unconstrained vectors!
Why not? The few examples I've seen do it frequently. Are bitwise 
operations ok with vectors?

> Use unsigned signals instead of std_logic vectors!
> (and of course: with the numeric_std you cannot do calculations with
> vectors! You must cast the vector to signed or unsigned before
> calculations)
I did that and it compiles. I now have my entity's port input/output as 
std_logic and std_logic_vector, my variables and signals as unsigned 
(except my bitData array which is a constant std_logic_vector)... is 
this all ok?

Thanks again for your time :)

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


Rate this post
useful
not useful
David Atol wrote:
>> Second: don't calculate with unconstrained vectors!
> Why not? The few examples I've seen do it frequently.
Get a book from P. Ashenden (they're good) and you won't see 
std_logic_arith in there.

Just a little test: what number is "1001"?
Is it 9? Or is it -7?

What number is unsigned "1001"?
It is 9!

And most problematic is the multiplication: a signed mult is much 
different than a unsigned mult.
And so, all in all: don't do calculations with unconstrained vectors. 
Even if you can find it in a lot of books from the last millennium...

> Are bitwise operations ok with vectors?
Of course they are...

von David A. (daveatol)


Rate this post
useful
not useful
Lothar Miller wrote:
> Get a book from P. Ashenden (they're good) and you won't see
> std_logic_arith in there.
Ok I got one from last decade based on the one from last millennium... 
;p

>> Are bitwise operations ok with vectors?
> Of course they are...
As I thought. It makes sense that it should be fine for 
add/subtract/rotate/logical-shift but not mult/div/arithmetic-shift due 
to how the sign is handled.

Ok, thanks very much for your help.

von bernd_l (Guest)


Rate this post
useful
not useful
> As I thought. It makes sense that it should be fine for
> add/subtract/rotate/logical-shift but not mult/div/arithmetic-shift due
> to how the sign is handled.

drop add/subtract and others from your list, too! This should be handled 
with the numeric_std library-types signed and unsigned. std_logic_vector 
is just a bunch of signals! AND, OR, ... are great for these type but 
almost everything else should be a signed or unsigned type (or even an 
integer for simple counters and the like).

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.