信号 "counter & stretcher" 不匹配计数

Signal "counter & stretcher" mismatch count

我的代码 vhdl 代码中有这个小的不匹配。我正在使用时钟计数器做信号展宽器。 基本上我将外部信号接收为 "start" ,在我将其与 2 ff 同步器同步并将此 "syncrhro-start" 用于我的 fsm 之后。它读取寄存器中的值并让 "gate" 打开我设置给寄存器的计数。我的时钟周期为 25 ns(40MHz 时钟)。 所以当我在寄存器上设置值“0”时,我没有任何信号(如预期的那样)。如果我设置“1”我有一个 50 ns 的信号而不是 25ns。 如果我设置“2”,我有一个 75ns 的信号输出。从概念上讲它是有效的(2,二进制是 01,所以它从 0 开始计数,所以 0-1-2 是 3 个时钟run/75ns)。

但我需要“1”一个 25ns 信号,2 个 50ns 等等。

这是我的代码:

                 library IEEE;
               use IEEE.STD_LOGIC_1164.ALL;
               use IEEE.numeric_std.ALL;

entity gate is
Port (
    start : in STD_LOGIC := '0'; --segnale di start
    clk : in  STD_LOGIC; --clock di ingresso
    reset  : in  STD_LOGIC; --ff reset
    gate: out STD_LOGIC; --gate in uscita 
    lenght: in std_logic_vector (7 downto 0)        
);
end gate;

architecture behavioral of gate is
--type state_type is (idle, gate_up, final);
type state_type is (idle, stretching);
signal state : state_type;


begin


process (reset, clk, start)
variable index : integer :=0;
variable gate_v: std_logic;
variable gatelen : integer;

begin
gatelen := to_integer(unsigned(lenght));

if reset = '1' then  
  gate_v := '0';
  index := 0;
else
    if rising_edge(clk) then
    case state is
      when idle =>
        index := 0;
        gate_v :='0';

        if(start = '1') then
          state <= stretching;
        else
          state <= idle;
        end if;

        when stretching =>
        if index = gatelen then
        state <=idle;
       else
        gate_v := '1';
        index := index + 1;
        state <= stretching;
        end if;            

      when others => null;
    end case;

 end if;
  end if;     
   gate <= gate_v;
   end process; 


  end Behavioral;

我可以用这个 mod

部分解决问题

gatelen := to_integer(无符号(长度))-1;

但在这种情况下,如果我在注册表上输入“1”,我将没有任何信号,如果我输入“2”,我有一个 50ns 的输出信号。 25ns 输出信号仍然缺失。

任何帮助将不胜感激!

编辑:

感谢您的回复,按要求,我也会添加我的测试平台的代码。

library ieee;
use ieee.std_logic_1164.all;

entity gate_tb is
end gate_tb;

architecture behavioral of gate_tb is

signal master_clk : std_logic := '0';
signal global_rst : std_logic := '0';
signal gate_on : std_logic := '0';
signal gate_out : std_logic := '0';
constant clk_period : time := 25 ns; --40ns
signal lenght_tb : std_logic_vector (7 downto 0);

component gate is 
    port( 
        clk : in std_logic;  
        reset : in std_logic;         
        start : in std_logic;
        gate: out std_logic;
        lenght: in std_logic_vector ( 7 downto 0)
    );
  end component gate;


begin
 uut : gate port map (
        clk => master_clk,
        reset => global_rst,
        start => gate_on,
        gate => gate_out,
        lenght => lenght_tb

        );

       gate_process : process
       begin

         master_clk <= '0';
         wait for clk_period/2;  --for 10 ns signal is '0'.
         master_clk <= '1';
         wait for clk_period/2;  --for next 10 ns signal is '1'.


end process;  

--stimulus

  stim : process 
  begin
  lenght_tb <= "00000001";
  wait for 50 ns;
  global_rst <= '1';
wait for 30 ns;
global_rst <= '0';
wait for 50 ns;
gate_on <= '1';
wait for 35 ns;
gate_on <= '0';
wait for 150 ns;
gate_on <= '1';
wait for 35 ns;
gate_on <= '0';
wait for 150 ns;
gate_on <= '1';
wait for 35 ns;
gate_on <= '0';
wait for 150 ns;
gate_on <= '1';
wait for 35 ns;
gate_on <= '0';
wait for 150 ns;





end process;

end behavioral;

所以如果我在这个 TB 中设置 lenght_tb <= "00000000";输出为 0 我设置了吗 lenght_tb <= "00000001";输出是 50 ns 而不是 25 ns 的信号。

问题出在 stretching 状态,例如 reg=1:

  1. 开始=1然后进入拉伸状态
  2. 在拉伸中你增加索引,所以现在索引=1 和门=0
  3. 下一个上升沿(已通过 1 个时钟)index= gatelen 所以进入空闲状态
  4. 下一个上升沿(另一个时钟所以通过了 2 个时钟)gate=0

检查index是否为gatelen时必须设置gate=0:

when stretching =>
        if index = gatelen then
            gate_v :=  '0';
        state <=idle;
       else
        gate_v := '1';
        index := index + 1;
        state <= stretching;
        end if;