6

With the shift_left function of ieee.numeric_std, I want to shift a signal to left and insert 1 or 0 from the right.

signal qo: signed (3 downto 0) := (others=>'0');
qo <= shift_left(qo,1);

That will only insert 0 from right. I want to insert 1 upon some conditions.

Cœur
  • 37,241
  • 25
  • 195
  • 267
mahmood
  • 23,197
  • 49
  • 147
  • 242

2 Answers2

5

Instead of using the shift_left function, how about using slicing and concatenation:

qo <= qo(2 downto 0) & '1';

or

qo <= qo(2 downto 0) & '0';

Personally, I recommend using slicing and concatenation for shifts and rotates. There are issues with the operators (sra etc) and, as you can see, using slicing and concatenation gives you complete control.

Matthew Taylor
  • 13,365
  • 3
  • 17
  • 44
  • OK. According to this topic https://stackoverflow.com/questions/9018087/shift-a-std-logic-vector-of-n-bit-to-right-or-left it is said to be good to use nmeric_std since it allows a lot of functions and methods to ease the coding. – mahmood Oct 10 '17 at 12:39
  • @mahmood Well, it's up to you and depends on what you mean by "ease the coding". – Matthew Taylor Oct 10 '17 at 12:43
1

Inserting '1' at the rightmost position of a signal of type signed means incrementing its value by 1. Likewise, not inserting '1' means incrementing its value by 0.

This is best expressed by the + operator which is overloaded by the numeric_std package:

-- Id: A.8
function "+" (L: SIGNED; R: INTEGER) return SIGNED;
-- Result subtype: SIGNED(L'LENGTH-1 downto 0).
-- Result: Adds a SIGNED vector, L, to an INTEGER, R.

So simply use:

shift_left(qo, 1) + 1

respectively

shift_left(qo, 1) + 0

This works irrespective of how qo is defined, so if you later change the length of qo you don't need to adjust any slicing operations.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • This is a clever trick! I personally find it a bit less intuitive. To fix your issue with the selected answer, you could use a VHDL tick attribute `'left` to make `qo` flexible, like `qo <= qo(qo'left downto 0);` – Russell Nov 04 '22 at 00:57