VHDL 中的通用移位算术

发布于 2024-10-02 11:21:57 字数 832 浏览 5 评论 0原文

我正在设计通用移位算术运算符。 除了按照下面介绍的方式使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光倒影 2024-10-09 11:21:57

您可以在没有任何循环或 case 语句的情况下使用 SRA 函数:

res <= to_stdlogicvector(to_bitvector(di) sra to_integer(sel));

请注意,您需要将 sel 设置为无符号,而不是 std_logic_vector:

sel: in unsigned (31  downto 0);

如果您不希望这样做,您仍然可以将 sel 转换为无符号。您还需要使用 numeric_bit:

use ieee.numeric_bit.all;

You can use the SRA function without any loops or case statements:

res <= to_stdlogicvector(to_bitvector(di) sra to_integer(sel));

Note that you need to make sel an unsigned, not a std_logic_vector:

sel: in unsigned (31  downto 0);

In case you don't want that, you can still cast sel into an unsigned. You also need to us numeric_bit:

use ieee.numeric_bit.all;
彩扇题诗 2024-10-09 11:21:57

使用索引?

PROCESS
  VARIABLE shift_count : INTEGER RANGE 0 TO 31;
BEGIN
  IF rst = '1' THEN
    res <= (others => '0');
  ELSIF RISING_EDGE(clk) THEN
    shift_count := to_integer(sel);
    FOR I IN 0 TO 31 LOOP
      IF I + shift_count < 32 THEN
        res(I) <= din(I + shift_count);
      ELSE
        res(I) <= din(31); -- for logical shift right, use '0' instead
      END IF;
    END LOOP;
  END IF;
END PROCESS;

这个版本更容易参数化为泛型。

请记住,VHDl 是行为描述,它不指定多路复用器。编译器可以生成不同的设计,具体取决于您是否针对大小、速度进行优化、是否允许流水线操作等。

请注意,5 个 2:1 多路复用器可以在比单个 32:1 多路复用器小得多的区域中实现此目的。如果这不是限制您的时钟速率的块,那么这可能是更好的选择。

另请注意,您的 sel 输入太宽,它只需要 5 位。

Use indexing?

PROCESS
  VARIABLE shift_count : INTEGER RANGE 0 TO 31;
BEGIN
  IF rst = '1' THEN
    res <= (others => '0');
  ELSIF RISING_EDGE(clk) THEN
    shift_count := to_integer(sel);
    FOR I IN 0 TO 31 LOOP
      IF I + shift_count < 32 THEN
        res(I) <= din(I + shift_count);
      ELSE
        res(I) <= din(31); -- for logical shift right, use '0' instead
      END IF;
    END LOOP;
  END IF;
END PROCESS;

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.

弱骨蛰伏 2024-10-09 11:21:57

从硬件的角度来看,为了在单个时钟中右移可变数量的位置,每个位都是一个触发器,具有基于选择的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文