VHDL 计数和移位

发布于 2025-01-17 18:47:59 字数 1956 浏览 2 评论 0 原文

8位输入值将从数据传递到A时,当负载为1时,当s变为1时,如果a(0)为1,则B值将B的值增加1,A为右移动。当a的值变为00000000时,我们完成了1并逃脱。因此,输入值中的1数在4位B中显示。我该怎么办? 我尝试编码,但直到1周才能进行编码。

library ieee;
use ieee.std_logic_1164.all;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
 
 entity one_counter is
     port (
         Output :out std_logic_vector (3 downto 0);
            Done : out std_logic;
            Input : in std_logic_vector(7 downto 0);
        load :in  std_logic;  
            S: in std_logic;
        clk,reset_n  :in  std_logic                           
    );
end entity;

 architecture behav of one_counter is 
    signal A :std_logic_vector (7 downto 0); 
    signal B :std_logic_vector (3 downto 0); 
    signal D : std_logic;
    signal state_out:std_logic_vector ( 1 downto 0);
    begin 
    process (reset_n, clk) is begin
            if (reset_n = '0') then
                state_out <= "00";             
            elsif rising_edge(clk) then  
                case (state_out) is
                    when "00" => if  (load = '1') then
                            state_out <="01"; end if;
                     when "01" => A <= Input;
                                    if (S = '1') then
                                    state_out <="10";  end if;
                     when "10" =>  
                        WHILE (A /= 0)  loop
                            if A(0) = '1' then
                                B <=  B+1;
                                Output <= B;
                            end if;
                            A <= '0' & A(7 downto 1);

                        END loop;
                        D <= '1';
                        Done <= D;
                        state_out <= "11";
                    when "11" => if( D = '0') then state_out <= "00"; end if;                           
             end case;
          end if; 
    end process;
end architecture;

8-bit input value is transferred from Data to A when load is 1. And when S becomes 1, if A(0) is 1, the value of B is increased by 1, and A is shift right. When the value of A becomes 00000000, we make done 1 and escape. Therefore, the number of 1's in the input value is displayed in 4-bit B. What should I do?
I tried to code, but I can't until 1 week.

library ieee;
use ieee.std_logic_1164.all;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
 
 entity one_counter is
     port (
         Output :out std_logic_vector (3 downto 0);
            Done : out std_logic;
            Input : in std_logic_vector(7 downto 0);
        load :in  std_logic;  
            S: in std_logic;
        clk,reset_n  :in  std_logic                           
    );
end entity;

 architecture behav of one_counter is 
    signal A :std_logic_vector (7 downto 0); 
    signal B :std_logic_vector (3 downto 0); 
    signal D : std_logic;
    signal state_out:std_logic_vector ( 1 downto 0);
    begin 
    process (reset_n, clk) is begin
            if (reset_n = '0') then
                state_out <= "00";             
            elsif rising_edge(clk) then  
                case (state_out) is
                    when "00" => if  (load = '1') then
                            state_out <="01"; end if;
                     when "01" => A <= Input;
                                    if (S = '1') then
                                    state_out <="10";  end if;
                     when "10" =>  
                        WHILE (A /= 0)  loop
                            if A(0) = '1' then
                                B <=  B+1;
                                Output <= B;
                            end if;
                            A <= '0' & A(7 downto 1);

                        END loop;
                        D <= '1';
                        Done <= D;
                        state_out <= "11";
                    when "11" => if( D = '0') then state_out <= "00"; end if;                           
             end case;
          end if; 
    end process;
end architecture;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

审判长 2025-01-24 18:48:00

根据您的规格,您应仅处理每个时钟周期的一点点(如果 s 表示高),而您的代码试图一次处理所有位。此外,您尚不理解VHDL语义:在执行时循环期间,信号 a 不更改,您总是在处理相同的位,如果 a 循环是无限的。您可能应该阅读有关VHDL的书,以更好地了解如何以及何时更新信号。

无论如何,您的规格转换为VHDL看起来很简单,就像条件这样做的形式一样,否则会这样做。将 a 是所有零(即使在重置后)时,将转换为VHDL 2008的示例应为高度声明:

use ieee.std_logic_1164.all;
use ieee.numeric_std_unsigned.all;

entity one_counter is
  port(
    clk, reset_n, load, s:  in std_ulogic;
    input:                  in std_ulogic_vector(7 downto 0);
    output:                out std_ulogic_vector(3 downto 0);
    done:                  out std_ulogic
    );
end entity one_counter;

architecture behav of one_counter is 
  signal a: std_ulogic_vector(7 downto 0);
begin
  process(reset_n, clk) is
  begin
    if reset_n = '0' then
      a      <= (others => '0');
      output <= (others => '0');
    elsif rising_edge(clk) then
      if load = '1' then
        a      <= input;
        output <= (others => '0');
      elsif s = '1' then
        a      <= a srl 1;
        output <= output + a(0);
      end if;
    end if;
  end process;

  done <= nor a;
end architecture behav;

注意:最好不要使用已解决的 std_logic std_logic_vector 类型如果您不想建模多个驱动器情况(Tri-State Buffers ...)。优先解决未解决的 std_ulogic std_ulogic_vector 类型。有关详细信息,请参见例如

注意:避免 ieee.std_logic_arith ieee.std_logic_unsigned ,它们不是标准配置。优先 ieee.numeric_std_unsigned

注意:如果完成的行为应不同(例如重置后不声称高或仅在一个时钟期间声明高),或者两者兼而有之),您将需要更多的存储时间来记住a <代码>加载发生。适应您的确切规格。

注意:输出也将重置 load 被认为高;这是您自己的尝试中缺少的,但似乎是合乎逻辑的。

According your specifications you should process one bit only per clock cycle (if S is asserted high), while your code tries to process all bits at once. Moreover you did not understand yet the VHDL semantics: during the execution of your while loop the value of signal A does not change, you are always processing the same bit and if A is not null the loop is infinite. You should probably read a book about VHDL to better understand how and when signals are updated.

Anyway, the translation of your specifications into VHDL looks quite straightforward, as they are in the form if condition do that, else do this. Example of translation into VHDL 2008, assuming done shall be asserted high when a is all zeros (even after reset):

use ieee.std_logic_1164.all;
use ieee.numeric_std_unsigned.all;

entity one_counter is
  port(
    clk, reset_n, load, s:  in std_ulogic;
    input:                  in std_ulogic_vector(7 downto 0);
    output:                out std_ulogic_vector(3 downto 0);
    done:                  out std_ulogic
    );
end entity one_counter;

architecture behav of one_counter is 
  signal a: std_ulogic_vector(7 downto 0);
begin
  process(reset_n, clk) is
  begin
    if reset_n = '0' then
      a      <= (others => '0');
      output <= (others => '0');
    elsif rising_edge(clk) then
      if load = '1' then
        a      <= input;
        output <= (others => '0');
      elsif s = '1' then
        a      <= a srl 1;
        output <= output + a(0);
      end if;
    end if;
  end process;

  done <= nor a;
end architecture behav;

Note: it is better to not use the resolved std_logic and std_logic_vector types if you do not want to model a multiple drive situation (tri-state buffers...). Prefer the unresolved std_ulogic and std_ulogic_vector types. For the details see, for instance, this Q&A.

Note: avoid IEEE.STD_LOGIC_ARITH and IEEE.STD_LOGIC_UNSIGNED, they are not standard. Prefer ieee.numeric_std_unsigned.

Note: if the behaviour of done shall be different (e.g. not asserted high after reset, or asserted high during only one clock period, or both) you will need one more bit of storage to remember that a load occurred. Adapt to your exact specifications.

Note: output is also reset when load is asserted high; this was missing in your own attempt but seems logical.

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