Tuesday, March 24, 2009

VHDL considerations...

While programming in VHDL can't be considered a very easy deal, there are certain things on it that are very easy to do... i lost 2 days with one of those things...

Making a crash course into VHDL sintax (and assuming that you know how a computer works in hardware terms), you control bits in vhdl... yes... bits... the simplest think in computers world... of course that it isn't very easy to do complex things of a thing so simple, but anyone understands that shifting bits is just picking a bunch of them and putting them a little bit aside.

In VHDL you can create arrays - vectors - of bits, and you can access each bit or a pack of them by indexing them, sounds scary, but it's very easy actually, one with no knowledge in programming should have no problem doing a static shifter in vhdl.

To index bits, you need to 'create' them... well ... there is a lot of mambo-jambo in VHDL for doing declalations, and while i mastered most of VHDL sintax i still fail in the most basic stuff (well, i'm always learning from mistakes but it doesn't seem enough xD).

Well... lets say you have a array of bits, better saying a vector, in vhdl it is called std_logic_vector, and it's simple a vector of std_logic elements.. std_logic elements are the representation of a signal value in hardware (well, kinda) they can be 1, 0 or other ugly stuff you really don't wanna know...

Ok... we have our vector puppy in a signal, this should be human readable (or i'm loosing my human side):

signal puppy : std_logic_vector (31 downto 0);

So puppy has 32 bits ordered like 31 down to 0... how nice...

if you want a bit from puppy, you just do like this:

puppy(3)

And that is bit number 3... it's 4th bit if you count from right, since there is a bit 0... easy to get right?

Now harder! >:D you want... the 8 first bits... that will be hard, right?

puppy(7 downto 0)

Damn... was too easy... well... this is being great but grabbing bits without interact with them is a bit... worthless... well... you have a nice operator for concatenate bits! So now we can make our shifter... to concatenate bits you just use & operator like so:

'0'&"0000001"

So ... easy to get ('0' is a bit with zero value .... and "0000001" is a vector of bits) ... so 8 bits with the least significant bit (if you consider the right the least significant bit) at '1'

Now ... how to shift? You should get it now without any help, let's shift our puppy 8 bits to right!

"00000000"&puppy(31 downto 8)

Easy... right? I mean... you couldn't fail that... no way.... right? I did fail at doing that... and lost 2 days of work >:(

I wanted to shift one bit to right, i did like this (taking puppy as an example):

'0' & accu(30 downto 0)

That simply replaces the last bit with '0' ... >___>''

PS: Sorry for the length of this post... 

No comments: