VHDL算法shift_left
VHDL arithmetic shift_left
用ieee.numeric_std
的shift_left
函数,我想把一个信号左移,从右边插入1
或0
signal qo: signed (3 downto 0) := (others=>'0');
qo <= shift_left(qo,1);
那只会从右边插入 0
。我想在某些条件下插入 1
。
不使用 shift_left
函数,使用 slicing 和 concatenation:
怎么样?
qo <= qo(2 downto 0) & '1';
或
qo <= qo(2 downto 0) & '0';
就我个人而言,我建议使用切片和串联来进行移位和旋转。运算符(sra
等)存在问题,如您所见,使用切片和串联可以让您完全控制。
在signed
类型信号的最右边插入'1'
意味着将其值增加1。同样,不是插入'1'
表示将其值增加 0.
这最好用 +
运算符表示,即 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.
所以只需使用:
shift_left(qo, 1) + 1
分别
shift_left(qo, 1) + 0
这与 qo
的定义方式无关,因此如果您稍后更改 qo
的长度,则无需调整任何切片操作。
用ieee.numeric_std
的shift_left
函数,我想把一个信号左移,从右边插入1
或0
signal qo: signed (3 downto 0) := (others=>'0');
qo <= shift_left(qo,1);
那只会从右边插入 0
。我想在某些条件下插入 1
。
不使用 shift_left
函数,使用 slicing 和 concatenation:
qo <= qo(2 downto 0) & '1';
或
qo <= qo(2 downto 0) & '0';
就我个人而言,我建议使用切片和串联来进行移位和旋转。运算符(sra
等)存在问题,如您所见,使用切片和串联可以让您完全控制。
在signed
类型信号的最右边插入'1'
意味着将其值增加1。同样,不是插入'1'
表示将其值增加 0.
这最好用 +
运算符表示,即 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.
所以只需使用:
shift_left(qo, 1) + 1
分别
shift_left(qo, 1) + 0
这与 qo
的定义方式无关,因此如果您稍后更改 qo
的长度,则无需调整任何切片操作。