VHDL 计数和移位
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的规格,您应仅处理每个时钟周期的一点点(如果
s
表示高),而您的代码试图一次处理所有位。此外,您尚不理解VHDL语义:在执行时循环期间,信号a
不更改,您总是在处理相同的位,如果a
循环是无限的。您可能应该阅读有关VHDL的书,以更好地了解如何以及何时更新信号。无论如何,您的规格转换为VHDL看起来很简单,就像条件这样做的形式一样,否则会这样做。将 a 是所有零(即使在重置后)时,将转换为VHDL 2008的示例应为高度声明:
注意:最好不要使用已解决的
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 signalA
does not change, you are always processing the same bit and ifA
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 whena
is all zeros (even after reset):Note: it is better to not use the resolved
std_logic
andstd_logic_vector
types if you do not want to model a multiple drive situation (tri-state buffers...). Prefer the unresolvedstd_ulogic
andstd_ulogic_vector
types. For the details see, for instance, this Q&A.Note: avoid
IEEE.STD_LOGIC_ARITH
andIEEE.STD_LOGIC_UNSIGNED
, they are not standard. Preferieee.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 aload
occurred. Adapt to your exact specifications.Note:
output
is also reset whenload
is asserted high; this was missing in your own attempt but seems logical.