----------------------------------------------------------------------------------
-- Design Name: dma_s2mm
-- Engineer: Jon Bean
-- Date: 05/07/21
-- Additional Comments:
-- DMA to transfer streaming data to memory.
----------------------------------------------------------------------------------

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity dma_s2mm is
  generic(
    M_AXI_ADDR_WIDTH  : integer	:= 32;
    M_AXI_DATA_WIDTH	: integer	:= 32);
  port(     
    dma_strt        : in  std_logic;
    dma_done        : out std_logic;
    dma_addr        : in  std_logic_vector(M_AXI_ADDR_WIDTH-1 downto 0);     
    dma_len         : in  std_logic_vector(31 downto 0);     

    m_axi_aclk      : in  std_logic;   
    m_axi_aresetn   : in  std_logic;      
    m_axi_awaddr    : out std_logic_vector(M_AXI_ADDR_WIDTH-1 downto 0);     
    m_axi_awlen     : out std_logic_vector(7 downto 0);  
    m_axi_awsize    : out std_logic_vector(2 downto 0);   
    m_axi_awburst   : out std_logic_vector(1 downto 0);   
    m_axi_awprot    : out std_logic_vector(2 downto 0);  
    m_axi_awcache   : out std_logic_vector(3 downto 0);  
    m_axi_awvalid   : out std_logic; 
    m_axi_awready   : in  std_logic;                         
    m_axi_wdata     : out std_logic_vector(M_AXI_DATA_WIDTH-1 downto 0); 
    m_axi_wstrb     : out std_logic_vector((M_AXI_DATA_WIDTH/8)-1 downto 0); 
    m_axi_wlast     : out std_logic;
    m_axi_wvalid    : out std_logic;
    m_axi_wready    : in  std_logic;                        
    m_axi_bresp     : in  std_logic_vector(1 downto 0);   
    m_axi_bvalid    : in  std_logic; 
    m_axi_bready    : out std_logic; 

    s_axis_aclk     : in  std_logic;   
    s_axis_tdata    : in  std_logic_vector(M_AXI_DATA_WIDTH-1 downto 0);    
    s_axis_tvalid   : in  std_logic;     
    s_axis_tready   : out std_logic); 
end dma_s2mm;

architecture Behavioral of dma_s2mm is
------------------------------------------------------------
-- Typedef
------------------------------------------------------------
type state is (IDLE,
               FIFO_RDY,
               CALC_XFER_SIZE,
               WAIT_FIFO,
               SEND_ADDR,
               SEND_DATA,
               SEND_RESP);

constant BUS_BYTES        : integer := M_AXI_DATA_WIDTH/8;
constant MAX_BURST_LEN    : integer := 16;
constant MAX_BURST_BYTES  : integer := MAX_BURST_LEN*BUS_BYTES;      
      
------------------------------------------------------------
-- Signals
------------------------------------------------------------

signal state_cs         : state;                         
signal state_ns         : state;    
signal req_len          : unsigned(31 downto 0);     
signal xfer_cnt         : unsigned(7 downto 0);
signal xfer_max         : unsigned(7 downto 0);

signal m_axi_awaddr_i   : std_logic_vector(M_AXI_ADDR_WIDTH-1 downto 0);     
signal m_axi_awvalid_i  : std_logic;    
signal m_axi_wvalid_i   : std_logic; 
signal m_axi_wlast_i    : std_logic;
signal m_axi_bready_i   : std_logic;

signal fifo_rst         : std_logic;      
signal fifo_rst_cnt     : unsigned(7 downto 0);
signal fifo_wr_data     : std_logic_vector(M_AXI_DATA_WIDTH-1 downto 0);  
signal fifo_wr_we       : std_logic;    
signal fifo_full        : std_logic;   
signal fifo_rd_data     : std_logic_vector(M_AXI_DATA_WIDTH-1 downto 0);  
signal fifo_rd_re       : std_logic;    
signal fifo_cnt         : std_logic_vector(9 downto 0);  
    
component fifo_32w_512d
  port(
    rst           : in std_logic;
    wr_clk        : in std_logic;
    rd_clk        : in std_logic;
    din           : in std_logic_vector(31 downto 0);
    wr_en         : in std_logic;
    rd_en         : in std_logic;
    dout          : out std_logic_vector(31 downto 0);
    full          : out std_logic;
    empty         : out std_logic;
    rd_data_count : out std_logic_vector(9 downto 0);
    wr_rst_busy   : out std_logic;
    rd_rst_busy   : out std_logic);
end component;
    
begin

m_axi_awaddr  <= m_axi_awaddr_i;
m_axi_awsize  <= "010";
m_axi_awlen   <= std_logic_vector(xfer_max-1);
m_axi_awburst <= "01";
m_axi_awprot  <= "000";
m_axi_awcache <= "0011";
m_axi_wdata   <= fifo_rd_data;
m_axi_wstrb   <= x"f";
m_axi_awvalid <= m_axi_awvalid_i;
m_axi_wvalid  <= m_axi_wvalid_i;
m_axi_wlast   <= m_axi_wlast_i;
m_axi_bready  <= m_axi_bready_i;

s_axis_tready <= not fifo_full;

U_fifo : fifo_32w_512d
  port map(
    wr_clk         => s_axis_aclk,
    rd_clk         => m_axi_aclk,    
    rst            => fifo_rst,
    wr_rst_busy    => open,
    rd_rst_busy    => open,
    din            => fifo_wr_data,
    wr_en          => fifo_wr_we,
    full           => fifo_full,
    dout           => fifo_rd_data,
    rd_en          => fifo_rd_re,
    empty          => open,
    rd_data_count  => fifo_cnt);
 
 fifo_wr_data <= s_axis_tdata;
 fifo_wr_we   <= s_axis_tvalid and not fifo_full;
 fifo_rd_re   <= m_axi_wvalid_i and m_axi_wready;
  
------------------------------------------------------------
-- Name : State Machine Seq Logic
--  
------------------------------------------------------------
process (m_axi_aclk, m_axi_aresetn)
begin
  if (m_axi_aresetn = '0') then
    state_cs <= IDLE;
  elsif rising_edge (m_axi_aclk) then
    state_cs <= state_ns;
  end if;
end process;  

-----------------------------------------------------------
-- Name : State Machine Comb Logic
-- 
------------------------------------------------------------
process (state_cs, dma_strt, fifo_rst_cnt, fifo_cnt, 
         xfer_max, m_axi_awvalid_i, m_axi_awready,
         m_axi_wvalid_i, m_axi_wready, m_axi_wlast_i,
         m_axi_bvalid, m_axi_bready_i, req_len)
begin
  case state_cs is
    -- IDLE - Wait for a new write request
    when IDLE =>
      if (dma_strt = '1') then
        state_ns <= FIFO_RDY;
      else 
        state_ns <= IDLE;      
      end if;  
    
    -- FIFO_RDY - Wait for the FIFO to come out of reset 
    when FIFO_RDY =>
      if (fifo_rst_cnt = 255) then
        state_ns <= CALC_XFER_SIZE;
      else 
        state_ns <= FIFO_RDY;      
      end if;            

    -- CALC_XFER_SIZE - Calculate the transfer size of the burst
    when CALC_XFER_SIZE =>       
      state_ns <= WAIT_FIFO;        

    -- WAIT_FIFO - Wait until the data FIFO has the data
    when WAIT_FIFO =>
      if (unsigned(fifo_cnt) >= xfer_max) then
        state_ns <= SEND_ADDR;        
      else
        state_ns <= WAIT_FIFO;        
      end if;
   
    -- SEND_ADDR - Send the addr   
    when SEND_ADDR =>
       if (m_axi_awvalid_i = '1' and m_axi_awready = '1') then
        state_ns <= SEND_DATA;        
      else
        state_ns <= SEND_ADDR;        
      end if;
          
    -- SEND_DATA - Send the data   
    when SEND_DATA =>
       if (m_axi_wvalid_i = '1' and m_axi_wready = '1' and m_axi_wlast_i = '1') then
        state_ns <= SEND_RESP;        
      else
        state_ns <= SEND_DATA;        
      end if;
        
    -- SEND_RESP - Send the bus response
    when SEND_RESP =>          
      if (m_axi_bvalid = '1' and m_axi_bready_i = '1') then
        if (req_len = 0) then
          state_ns <= IDLE;         
        else
          state_ns <= CALC_XFER_SIZE;        
        end if;         
      else
        state_ns <= SEND_RESP;        
      end if;                                                              
  end case;
end process;

------------------------------------------------------------
-- Name : DMA Done
--  
------------------------------------------------------------
process (m_axi_aclk)
begin
  if rising_edge (m_axi_aclk) then
    case state_cs is
    -- IDLE - Wait for a new write request
    when IDLE =>
      if (dma_strt = '1') then
        dma_done <= '0';    
      end if;       

      -- SEND_RESP - Send the bus response
      when SEND_RESP =>          
        if (m_axi_bvalid = '1' and m_axi_bready_i = '1') then
          if (req_len = 0) then
            dma_done <= '1';      
          end if;            
        end if;        
        
       when others => 
    end case;  
  end if;
end process; 

------------------------------------------------------------
-- Name : Master AWLEN
--  
------------------------------------------------------------
process (m_axi_aclk)
begin
  if rising_edge (m_axi_aclk) then
    case state_cs is
      -- IDLE - Wait for a new write request
      when IDLE =>
        m_axi_awaddr_i <= dma_addr;
    
      -- SEND_ADDR - Send the addr   
      when SEND_ADDR =>
        if (m_axi_awvalid_i = '1' and m_axi_awready = '1') then
          m_axi_awaddr_i <= std_logic_vector(to_unsigned(to_integer(unsigned(m_axi_awaddr_i)) + MAX_BURST_BYTES, m_axi_awaddr_i'length));
        end if;    
        
       when others => 
    end case;  
  end if;
end process;   

------------------------------------------------------------
-- Name : Master AWVALID
--  
------------------------------------------------------------
process (m_axi_aclk)
begin
  if rising_edge (m_axi_aclk) then
    case state_cs is
      -- SEND_ADDR - Send the addr   
      when SEND_ADDR =>
        if (m_axi_awvalid_i = '1' and m_axi_awready = '1') then
          m_axi_awvalid_i <= '0';      
        else
          m_axi_awvalid_i <= '1';     
        end if; 
        
      when others =>
        m_axi_awvalid_i <= '0';
    end case;  
  end if;
end process;  

------------------------------------------------------------
-- Name : Master WVALID
--  
------------------------------------------------------------
process (m_axi_aclk)
begin
  if rising_edge (m_axi_aclk) then
    case state_cs is
      -- SEND_DATA - Send the data   
      when SEND_DATA =>
        if (m_axi_wvalid_i = '1' and m_axi_wready = '1' and m_axi_wlast_i = '1') then
          m_axi_wvalid_i <= '0';    
        else
          m_axi_wvalid_i <= '1';     
        end if;
        
      when others =>
        m_axi_wvalid_i <= '0';
    end case;  
  end if;
end process;  

------------------------------------------------------------
-- Name : Master WLAST
--  
------------------------------------------------------------
process (m_axi_aclk)
begin
  if rising_edge (m_axi_aclk) then
    case state_cs is     
      -- SEND_ADDR - Send the addr   
      when SEND_ADDR =>
        if (xfer_max = to_unsigned(1, xfer_max'length)) then
          m_axi_wlast_i <= '1';           
        else
          m_axi_wlast_i <= '0';         
        end if;
          
      -- SEND_DATA - Send the data   
      when SEND_DATA =>
        if (m_axi_wvalid_i = '1' and m_axi_wready = '1' and m_axi_wlast_i = '1') then
          m_axi_wlast_i <= '0';    
        else
          if (xfer_cnt = 2) then
            m_axi_wlast_i <= '1';         
          end if;         
        end if;
        
      when others =>
        m_axi_wlast_i <= '0';
    end case;  
  end if;
end process;  

------------------------------------------------------------
-- Name : Master BREADY
--  
------------------------------------------------------------
process (m_axi_aclk)
begin
  if rising_edge (m_axi_aclk) then
    case state_cs is
      -- SEND_RESP - Send the bus response
      when SEND_RESP =>  
        if (m_axi_bvalid = '1' and m_axi_bready_i = '1') then
          m_axi_bready_i <= '0';    
        else
          m_axi_bready_i <= '1';     
        end if;
        
      when others =>
        m_axi_bready_i <= '0';
    end case;  
  end if;
end process; 

------------------------------------------------------------
-- Name : Transfer Max Length
--  
------------------------------------------------------------
process (m_axi_aclk)
begin
  if rising_edge (m_axi_aclk) then
    case state_cs is
      -- CALC_XFER_SIZE - Calculate the transfer size of the burst
      when CALC_XFER_SIZE =>       
        if (req_len >= MAX_BURST_BYTES) then
          xfer_max <= to_unsigned(MAX_BURST_LEN, m_axi_awlen'length);      
        else
          xfer_max <= req_len(7 downto 0) / BUS_BYTES;     
        end if;  
        
       when others => 
    end case;  
  end if;
end process; 

------------------------------------------------------------
-- Name : Request Length
-- Size of the DMA request in bytes. 
------------------------------------------------------------
process (m_axi_aclk)
begin
  if rising_edge (m_axi_aclk) then
    case state_cs is
      -- IDLE - Wait for a new write request
      when IDLE =>
        if (dma_strt = '1') then
          req_len <= unsigned(dma_len);  
        end if;    
        
      -- CALC_XFER_SIZE - Calculate the transfer size of the burst
      when CALC_XFER_SIZE =>       
        if (req_len >= MAX_BURST_BYTES) then
          req_len <= req_len - to_unsigned(MAX_BURST_BYTES, req_len'length);     
        else
          req_len <= to_unsigned(0, req_len'length);     
        end if;  
        
      when others =>                
    end case;       
  end if;
end process; 

------------------------------------------------------------
-- Name : Request Length
-- Size of the DMA request in bytes. 
------------------------------------------------------------
process (m_axi_aclk)
begin
  if rising_edge (m_axi_aclk) then
    case state_cs is
      -- CALC_XFER_SIZE - Calculate the transfer size of the burst
      when CALC_XFER_SIZE =>       
        if (req_len >= MAX_BURST_BYTES) then
          xfer_cnt <= to_unsigned(MAX_BURST_LEN, xfer_cnt'length);      
        else
          xfer_cnt <= req_len(7 downto 0) / BUS_BYTES;     
        end if;  

      -- SEND_DATA - Send the data   
      when SEND_DATA =>
        if (m_axi_wvalid_i = '1' and m_axi_wready = '1') then
          xfer_cnt <= xfer_cnt - to_unsigned(1, xfer_cnt'length);  
        end if;
                
      when others =>                
    end case;       
  end if;
end process; 

------------------------------------------------------------
-- Name : FIFO Reset
-- 
------------------------------------------------------------
process (m_axi_aclk)
begin
  if rising_edge (m_axi_aclk) then
    case state_cs is
      -- IDLE - Wait for a new write request
      when IDLE =>
        fifo_rst <= '1';
       
      when others =>              
        fifo_rst <= '0';  
    end case;       
  end if;
end process; 

------------------------------------------------------------
-- Name : FIFO Reset Count
-- 
------------------------------------------------------------
process (m_axi_aclk)
begin
  if rising_edge (m_axi_aclk) then
    case state_cs is
      -- IDLE - Wait for a new write request
      when IDLE =>
        fifo_rst_cnt <= to_unsigned(0, fifo_rst_cnt'length);
        
      when FIFO_RDY =>
        fifo_rst_cnt <= fifo_rst_cnt + to_unsigned(1, fifo_rst_cnt'length);
        
      when others =>                
    end case;       
  end if;
end process; 

end Behavioral;