VHDL 中的通用移位算术
我正在设计通用移位算术运算符。 除了按照下面介绍的方式使用 32 位多路复用器(解码器)之外,还有更好的方法来实现它吗?
ENTITY isra IS
PORT (
clk: in std_logic;
rst: in std_logic;
di: in std_logic_vector (31 downto 0);
sel: in std_logic_vector (31 downto 0);
res: out std_logic_vector (31 downto 0) := (others => '0')
);
END isra;
PROCESS
BEGIN
WAIT UNTIL clk'EVENT AND clk = '1';
IF rst = '1' THEN
res <= (others => '0');
ELSE
CASE sel IS
when X"00000001" => res <= to_stdlogicvector(to_bitvector(a) sra 1);
when X"00000002" => res <= to_stdlogicvector(to_bitvector(a) sra 2);
...
when X"0000001F" => res <= to_stdlogicvector(to_bitvector(a) sra 31);
when others => res <= (others => '0');
END CASE;
END IF;
END PROCESS;
I am designing universal shift arithmetic operator.
Is there a better way to achieve it besides using the 32bit multiplexer (decoder) in a way presented bellow?
ENTITY isra IS
PORT (
clk: in std_logic;
rst: in std_logic;
di: in std_logic_vector (31 downto 0);
sel: in std_logic_vector (31 downto 0);
res: out std_logic_vector (31 downto 0) := (others => '0')
);
END isra;
PROCESS
BEGIN
WAIT UNTIL clk'EVENT AND clk = '1';
IF rst = '1' THEN
res <= (others => '0');
ELSE
CASE sel IS
when X"00000001" => res <= to_stdlogicvector(to_bitvector(a) sra 1);
when X"00000002" => res <= to_stdlogicvector(to_bitvector(a) sra 2);
...
when X"0000001F" => res <= to_stdlogicvector(to_bitvector(a) sra 31);
when others => res <= (others => '0');
END CASE;
END IF;
END PROCESS;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在没有任何循环或 case 语句的情况下使用 SRA 函数:
请注意,您需要将 sel 设置为无符号,而不是 std_logic_vector:
如果您不希望这样做,您仍然可以将 sel 转换为无符号。您还需要使用 numeric_bit:
You can use the SRA function without any loops or case statements:
Note that you need to make sel an unsigned, not a std_logic_vector:
In case you don't want that, you can still cast sel into an unsigned. You also need to us numeric_bit:
使用索引?
这个版本更容易参数化为泛型。
请记住,VHDl 是行为描述,它不指定多路复用器。编译器可以生成不同的设计,具体取决于您是否针对大小、速度进行优化、是否允许流水线操作等。
请注意,5 个 2:1 多路复用器可以在比单个 32:1 多路复用器小得多的区域中实现此目的。如果这不是限制您的时钟速率的块,那么这可能是更好的选择。
另请注意,您的
sel
输入太宽,它只需要 5 位。Use indexing?
This version is much easier to parameterize into a generic.
Remember that VHDl is a behavioral description, it doesn't specify a mux. The compiler can generate different designs depending on whether you optimize for size, speed, allow pipelining, etc.
Note that 5 2:1 muxes can implement this in a far smaller area than a single 32:1 mux. If this isn't the block that limits your clock rate, that might be preferable.
Also note that your
sel
input is far too wide, it only needs to be 5 bits.从硬件的角度来看,为了在单个时钟中右移可变数量的位置,每个位都是一个触发器,具有基于选择的 32 个可能值之一。所以从这个角度来看,这就是你要做的事情。
不过,我会让 sel == 0 成为一个案例并使其成为直通。从逻辑上讲,这比将所有内容都设置为零更有意义。
Well from a hardware point of view, to shift right in a single clock a variable number of positions, each bit is a single flip-flop with one of 32 possible values based on a selection. So from that point of view, this is how you do it.
I would make sel == 0 a case and make it a passthrough, though. Logically, that makes more sense than setting everything to zeros.