Finally! It worked! I didn't use the "hold" state at the end, I didn't see it neccesary and it seems to work fine, I'll continue checking it to see if its consistent in time. However I paste here the code again, in case someone has the same problem or see something wrong in it.
I don't know how to thank you for your help, you saved me. I wish one day I can return the favor. Thank you very much!
LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.NUMERIC_STD.ALL;
entity control_unit2 IS
port (reset: in std_logic;
clk: in std_logic;
trigger: in std_logic;
data : in std_logic_vector (23 downto 0);
data_out : out std_logic;
clk_out : out std_logic);
end control_unit2;
Architecture Behavioral of control_unit2 is
type state_type is (start, load, pulse, rst, shift, check);
signal pstate, nstate : state_type;
signal data_reg : std_logic_vector (23 downto 0);
signal data_next: std_logic_vector (23 downto 0);
signal i_reg : unsigned (4 downto 0);
signal i_next: unsigned (4 downto 0);
signal datout_reg, datout_next: std_logic ;
signal clkout_reg, clkout_next: std_logic;
BEGIN
clocked: PROCESS (clk,reset)
BEGIN
if (reset = '1') then
pstate <= start;
i_reg <= (others => '0');
data_reg <= (others => '0');
datout_reg <= '0';
clkout_reg <= '0';
elsif clk'event and clk = '1' then
pstate <= nstate;
i_reg <= i_next;
data_reg <= data_next;
datout_reg <= datout_next;
clkout_reg <= clkout_next;
end if;
end process clocked;
states: process(datout_reg, pstate,data,trigger,data_reg,clkout_reg,i_reg)
begin
data_next <= data_reg;
i_next <= i_reg;
clkout_next <= clkout_reg;
datout_next <= datout_reg;
case pstate is
when start =>
if trigger = '1' then
i_next <= to_unsigned(24,5);
data_next <= data;
nstate <= load;
clkout_next <= '0';
else
nstate <= start;
end if;
when load =>
datout_next <= data_reg(23);
nstate <= pulse;
clkout_next <= '0';
i_next <= i_reg;
when pulse =>
data_next <= data_reg(22 downto 0) & '0';
clkout_next <= '1';
nstate <= rst;
i_next <= i_reg;
when rst =>
i_next <= i_reg;
clkout_next <= '0';
nstate <= shift;
when shift =>
i_next <= i_reg - 1;
nstate <= check;
when check =>
if i_reg = 0 then
nstate <= start;
else
nstate <= load;
end if;
end case;
end process states;
clk_out <= clkout_reg;
data_out <= datout_reg;
END Behavioral;
Pdta: I will read one of those books! Thanks!