我正在構建一個計數器,用於統計輸入通道的上升沿。我簡化了我的設計,以包括兩個狀態,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
啊......愚蠢的錯誤。謝謝;修復它! –