----------------------------------------------------------------------------------
-- Company:        ETH Zurich, Institute for Particle Physics
-- Engineer:       P. Vogler, Q. Weitzel
-- 
-- Create Date:    05/07/2010 
-- Design Name: 
-- Module Name:    FTU_test2_dac_control - Behavioral 
-- Project Name: 
-- Target Devices: 
-- Tool versions: 
-- Description:    test2 for control DAC on FTU board to set trigger thresholds 
--
-- Dependencies: 
--
-- Revision: 
-- Revision 0.01 - File Created
-- Additional Comments: 
--
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
library ftu_definitions;
USE ftu_definitions.ftu_array_types.all;

---- Uncomment the following library declaration if instantiating
---- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity FTU_test2_dac_control is
  port(
    clk      : IN     STD_LOGIC;
    reset    : IN     STD_LOGIC;
    clr      : OUT    STD_LOGIC;
    mosi     : OUT    STD_LOGIC;
    sck      : OUT    STD_LOGIC;
    cs_ld    : out    STD_LOGIC;
    enable1  : out    STD_LOGIC;
    enable2  : out    STD_LOGIC;
    enable3  : out    STD_LOGIC
  );
end FTU_test2_dac_control;

architecture Behavioral of FTU_test2_dac_control is

  component FTU_test2_spi_interface
    port(
      clk_50MHz      : IN     std_logic;
      config_start   : IN     std_logic;
      dac_array      : IN     dac_array_type;
      config_ready   : OUT    std_logic;
      config_started : OUT    std_logic;
      dac_cs         : OUT    std_logic;
      mosi           : OUT    std_logic;
      sclk           : OUT    std_logic;
      miso           : INOUT  std_logic
   );
  end component;

  signal clk_sig            : std_logic;
  signal reset_sig          : std_logic;

  signal clr_sig            : std_logic;
  signal mosi_sig           : std_logic := '0';
  signal serial_clock_sig   : std_logic;
  signal dac_cs_sig         : std_logic;
  
  signal config_start_sig   : std_logic := '0';  
  signal config_ready_sig   : std_logic;
  signal config_started_sig : std_logic := '0';
  signal dac_array_sig      : dac_array_type := (100,200,300,400,500);
 
  -- Build an enumerated type for the state machine
  type state_type is (START, WAITING, STOP);

  -- Register to hold the current state
  signal state, next_state : state_type;
  
begin

  reset_sig <= reset;
  clk_sig <= clk;
  mosi <= mosi_sig;
  sck <= serial_clock_sig;
  cs_ld <= dac_cs_sig;
  
  -- FSM for dac control: first process
  FSM_Registers: process(clk_sig, reset_sig)
  begin
    if reset_sig = '1' then
      state <= START;
    elsif Rising_edge(clk_sig) then
      state <= next_state;
    end if;
  end process;

  -- FSM for dac control: second process
  FSM_logic: process(state, config_ready_sig)
  begin
    next_state <= state;
    case state is
      when START =>
        config_start_sig <= '1';
        enable1 <= '1';
        enable2 <= '0';
        enable3 <= '0';
        next_state <= WAITING;        
      when WAITING =>
        config_start_sig <= '1';
        enable1 <= '0';
        enable2 <= '1';
        enable3 <= '0';
        if (config_ready_sig = '1') then          
          next_state <= STOP;          
        else
          next_state <= WAITING;
        end if;
      when STOP =>
        enable1 <= '0';
        enable2 <= '0';
        enable3 <= '1';
        config_start_sig <= '0';
    end case;
  end process;
    
  Inst_FTU_test2_spi_interface : FTU_test2_spi_interface
    port map(
      clk_50MHz      => clk_sig,
      config_start   => config_start_sig,
      dac_array      => dac_array_sig,
      config_ready   => config_ready_sig,
      config_started => config_started_sig,
      dac_cs         => dac_cs_sig,
      mosi           => mosi_sig,
      sclk           => serial_clock_sig,
      miso           => open
    );

end Behavioral;

