我正在构建一个计数器,用于统计输入通道的上升沿。我简化了我的设计,以包括两个状态,one
和two
,其中计数完成。出于某种原因,无论何时我尝试将counter_reg
加1,或尝试将任何数字都指定给它,ModelSim中的信号都会变成红色并带有X.下面提供了信号发生情况的代码和图片。带计数器的无符号加法不起作用
我已经包含了IEEE.NUMERIC_STD.ALL,所以我应该可以做无符号的加法。我不确定counter_reg
有什么问题。有没有什么我在柜台做错了?
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
entity photon_counter is
Port (clk,reset : in STD_LOGIC;
arm,shifter,channel : in STD_LOGIC;
start : in STD_LOGIC);
end photon_counter;
architecture fsm_arch of photon_counter is
type state_type is (idle,zero,one);
type array_type is array (1 downto 0) of UNSIGNED (15 downto 0);
signal state_reg,state_next : state_type;
signal arm_prev,shifter_prev,channel_prev : STD_LOGIC;
signal counter : array_type;
signal counter_reg,counter_next : UNSIGNED (15 downto 0);
begin
--------------------------------------
--State Register
--------------------------------------
process(clk,reset)
begin
if reset='1' then
state_reg <= zero;
counter_reg <= (others => '0');
counter <= (others => (others => '0'));
elsif rising_edge(clk) then
state_reg <= state_next;
counter_reg <= counter_next;
arm_prev <= arm;
shifter_prev <= shifter;
channel_prev <= channel;
end if;
end process;
--------------------------------------
--Next-State Logic/Output Logic
--------------------------------------
process(clk,reset,state_reg,start,counter_reg,shifter_prev,shifter,arm,channel_prev,channel)
begin
--default actions
state_next <= state_reg;
counter_next <= counter_reg;
counter_reg <= counter_reg;
case state_reg is
when idle =>
counter_reg <= (others => '0');
counter <= (others => (others => '0'));
if start = '1' then
state_next <= zero;
end if;
when zero =>
if (shifter = '1') and (shifter_prev = '0') then
state_next <= one;
counter(0) <= counter_reg;
end if;
if (channel = '1') and (channel_prev = '0') then
counter_next <= counter_reg + 1;
end if;
when one =>
if arm = '1' then
state_next <= zero;
counter(1) <= counter_reg;
end if;
if (channel = '1') and (channel_prev = '0') then
counter_reg <= counter_reg + 1;
end if;
end case;
end process;
end fsm_arch;
如下所示,counter_reg
和counter_next
开始为0的值,直到我尝试加1 counter_next。当channel_prev
上升时,counter_reg
和counter_next
都变为X(错误)并变为红色。
检查您在哪里分配问题的寄存器。如果您在多个流程中分配它,请不要!相反,在一个过程中保留所有的分配。 –
'X'表示错误,不是未知('U')。你的信号'计数器'有2个驱动过程。第一个重置'counter',第二个过程为'counter'生成一个锁存器。 – Paebbels
啊......愚蠢的错误。谢谢;修复它! –