【问题标题】:Shifting a logic vector to a bit将逻辑向量移位一位
【发布时间】:2014-12-04 03:31:18
【问题描述】:

我有一个 8 位逻辑向量,应该转移到输出。

constant CR:std_logic_vector:(7 downto 0):="11000000";

我正在尝试为 CR 使用索引以及属于指定索引的每个值...

Q<=CR(i);

我通过 if 语句将 i 计数为 0 到 7 以实现移位。但我对此有些怀疑。我走对了吗?或者有没有更好的方法来做到这一点,比如转移功能。 (当然这个操作应该与时钟同步才能同步。)

我是 VHDL 的新手,所以感谢您的回复。

【问题讨论】:

  • 您的案例似乎证明了在堆栈交换网站上的声誉价值。您尚未接受对您之前的任何问题的单一答案。您可以从CR 的声明中导出循环的迭代范围,如果需要反转顺序,请使用属性“REVERSE_RANGE”。否则请参阅我对您的问题的回答How can i generate a pulse train to give output in common way?
  • 对不起。我不知道因为我的低代表我可以接受一个答案。我喜欢头脑风暴,一个问题可能会以不同的方式提出,问题的解决方案也可能不同。我想这与堆栈交换站点无关,这是一种很好的理解方式。感谢您的所有回复。
  • 还要注意 CR 声明中 std_logic_vector 后面的虚假冒号。

标签: if-statement vhdl clock fpga bit-shift


【解决方案1】:

不,真的,我对你的问题的评论并不是在征求你接受答案,老实说,我对声誉积分并不那么苛刻。

我的意思是,潜在的问题回答者很难为您的问题付出任何努力,而且您的声誉也不会提高。

将时钟条件放在循环语句中似乎有点尴尬(与生成语句相反,所以让我们尝试一个单独的计数器:

library ieee;
use ieee.std_logic_1164.all;

entity shft_log_vec is
    port (
        rst:    in  std_logic;
        clk:    in  std_logic;
        Q:      out std_logic
    );
end entity;

architecture foo of shft_log_vec is
    constant CR:       std_logic_vector (7 downto 0) := "11000000";
    signal bit_ctr:  natural range CR'REVERSE_RANGE;  

begin

    Q <= CR(bit_ctr);

INDEX_CTR:
    process (clk, rst)

                           -- uninitialized default value is 0
    begin
        if rst = '1' then  -- will only run after a rst after the first time, use reset
           bit_ctr <= 0;
        elsif bit_ctr < CR'HIGH and rising_edge(clk) then  -- you could add an enable
           bit_ctr <= bit_ctr + 1;  -- integer arithmetic and comparison.
        else
        end if;
        -- because you need clock evaluation  for sequential operation in there, no loop 
        -- statement.  Reverse the bit order by using 'RANGE instead.

    end process;

end architecture;

library ieee;
use ieee.std_logic_1164.all;

entity tb_shft_log_vec is
end entity;

architecture foo of tb_shft_log_vec is
    signal rst: std_logic := '0';
    signal clk: std_logic := '0';
    signal Q:   std_logic;
begin

DUT:
    entity work.shft_log_vec 
        port map (
            rst => rst,
            clk => clk,
            Q  => Q
        );

CLOCK:
    process 
    begin
        wait for 20 ns;
        clk <= not clk;
        if Now > 360 ns then
            wait;
        end if;
    end process;

STIMULUS:
    process
    begin
        wait for 1 ns;
        rst <= '1';
        wait for 20 ns;
        rst <= '0';
        wait;
    end process;

end architecture;

注意CR 的指针是bit_ctr,它是一个从0 计数到7 然后停止的受限整数。

如果您希望 CR 以相反的顺序出现(第 7 个),请使用 'RANGE'LOW 而不是 'REVERSE_RANGE'HIGH.

在模拟测试台时,我们看到 Q 的值在每个时钟上升沿出现 CR(0)CR(7),并停止等待复位。

(图片是一个完整尺寸的链接)

使用复位,意味着生成器和接收器都知道要在哪些时钟边沿对Q 进行采样。在生成器和接收器之间切换到同步复位共享可能是谨慎的做法,具体取决于信号 Q 的路由距离(如跨越时钟树边界)。

请注意评论链接中您上一个问题的答案的异同。这种使用属性的方式可以让您轻松更改 CR 的大小。

【讨论】:

    猜你喜欢
    • 2011-02-09
    • 1970-01-01
    • 2011-04-11
    • 2023-01-09
    • 1970-01-01
    • 1970-01-01
    • 2012-02-07
    • 1970-01-01
    • 2012-01-05
    相关资源
    最近更新 更多