----------------------------------------------------------------------------------
-- Design Name: max2771_if
-- Engineer: Jon Bean
-- Date: 05/07/21
-- Additional Comments:
-- Interfaces a MAX2771 GPS receiver.
----------------------------------------------------------------------------------

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

library UNISIM;
use UNISIM.vcomponents.all;

entity max2771_if is
  generic(
    S_AXI_ADDR_WIDTH	  : integer	:= 12;
    M_AXI_ADDR_WIDTH	  : integer	:= 32;
    M_AXI_DATA_WIDTH	  : integer	:= 32;    
    M_AXIS_DATA_WIDTH	  : integer	:= 32);    
  port(
    max_clk             : in  std_logic;
    max_din             : in  std_logic_vector(3 downto 0);  
    
    aclk                : in  std_logic;
    aresetn             : in  std_logic;
   
    s_axi_lite_awaddr   : in  std_logic_vector(S_AXI_ADDR_WIDTH-1 downto 0);
    s_axi_lite_awprot   : in  std_logic_vector(2 downto 0);
    s_axi_lite_awvalid  : in  std_logic;
    s_axi_lite_awready  : out std_logic;
    s_axi_lite_wdata    : in  std_logic_vector(31 downto 0);
    s_axi_lite_wstrb    : in  std_logic_vector(3 downto 0);
    s_axi_lite_wvalid   : in  std_logic;
    s_axi_lite_wready   : out std_logic;
    s_axi_lite_bresp    : out std_logic_vector(1 downto 0);
    s_axi_lite_bvalid   : out std_logic;
    s_axi_lite_bready   : in  std_logic;
    s_axi_lite_araddr   : in  std_logic_vector(S_AXI_ADDR_WIDTH-1 downto 0);
    s_axi_lite_arprot   : in  std_logic_vector(2 downto 0);
    s_axi_lite_arvalid  : in  std_logic;
    s_axi_lite_arready  : out std_logic;
    s_axi_lite_rdata    : out std_logic_vector(31 downto 0);
    s_axi_lite_rresp    : out std_logic_vector(1 downto 0);
    s_axi_lite_rvalid   : out std_logic;
    s_axi_lite_rready   : 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;   
    
    m_axis_tdata        : out std_logic_vector(M_AXIS_DATA_WIDTH-1 downto 0);          
    m_axis_tvalid       : out std_logic;       
    m_axis_tready       : in  std_logic);                                       
end max2771_if;

architecture Behavioral of max2771_if is

-- Address map
constant CTRL_ADDR      : integer := 0;   -- Control register
constant STS_ADDR       : integer := 4;   -- Status register
constant DMA_ADDR_ADDR  : integer := 8;   -- DMA address
constant DMA_LEN_ADDR   : integer := 12;  -- DMA length

-- Control register bits
constant CTRL_DMA_STRT  : integer := 0;   -- [0] - DMA start
constant CTRL_MODE_NORM : integer := 1;   -- [1] - Normal mode
constant CTRL_MODE_TEST : integer := 2;   -- [2] - Test mode

-- Status register bits
constant STS_DMA_DONE   : integer := 0;   -- [0] - DMA done

------------------------------------------------------------
-- Signals
-----------------------------------------------------------

signal reg_ctrl_en          : std_logic;
signal reg_ctrl_we          : std_logic_vector(3 downto 0);
signal reg_ctrl_addr        : std_logic_vector(S_AXI_ADDR_WIDTH-1 downto 0);
signal reg_ctrl_wr_data     : std_logic_vector(31 downto 0);
signal reg_ctrl_rd_data     : std_logic_vector(31 downto 0);
signal reg_ctrl_wr_we       : std_logic;
signal reg_ctrl_rd_re       : std_logic;

signal ctrl_reg             : std_logic_vector(31 downto 0);
signal sts_reg              : std_logic_vector(31 downto 0);
signal dma_strt             : std_logic;
signal dma_done             : std_logic;
signal dma_addr             : std_logic_vector(31 downto 0);
signal dma_len              : std_logic_vector(31 downto 0);

signal din                  : std_logic_vector(3 downto 0);
signal din_cnt              : unsigned(2 downto 0);
signal test_cnt             : unsigned(31 downto 0);
signal mode                 : std_logic;   

signal s_axis_tdata         : std_logic_vector(M_AXI_DATA_WIDTH-1 downto 0);    
signal s_axis_tvalid        : std_logic;     
signal s_axis_tready        : std_logic; 
    
signal fifo_rst             : std_logic;      
signal fifo_wr_data         : std_logic_vector(M_AXIS_DATA_WIDTH-1 downto 0);  
signal fifo_wr_we           : std_logic;    
signal fifo_full            : std_logic;   
signal fifo_rd_data         : std_logic_vector(M_AXIS_DATA_WIDTH-1 downto 0);  
signal fifo_rd_re           : std_logic;    
signal fifo_empty           : std_logic;  
    
component reg_ctrl
  port (
    s_axi_aclk    : in std_logic;
    s_axi_aresetn : in std_logic;
    s_axi_awaddr  : in std_logic_vector(11 downto 0);
    s_axi_awprot  : in std_logic_vector(2 downto 0);
    s_axi_awvalid : in std_logic;
    s_axi_awready : out std_logic;
    s_axi_wdata   : in std_logic_vector(31 downto 0);
    s_axi_wstrb   : in std_logic_vector(3 downto 0);
    s_axi_wvalid  : in std_logic;
    s_axi_wready  : out std_logic;
    s_axi_bresp   : out std_logic_vector(1 downto 0);
    s_axi_bvalid  : out std_logic;
    s_axi_bready  : in std_logic;
    s_axi_araddr  : in std_logic_vector(11 downto 0);
    s_axi_arprot  : in std_logic_vector(2 downto 0);
    s_axi_arvalid : in std_logic;
    s_axi_arready : out std_logic;
    s_axi_rdata   : out std_logic_vector(31 downto 0);
    s_axi_rresp   : out std_logic_vector(1 downto 0);
    s_axi_rvalid  : out std_logic;
    s_axi_rready  : in std_logic;
    bram_rst_a    : out std_logic;
    bram_clk_a    : out std_logic;
    bram_en_a     : out std_logic;
    bram_we_a     : out std_logic_vector(3 downto 0);
    bram_addr_a   : out std_logic_vector(11 downto 0);
    bram_wrdata_a : out std_logic_vector(31 downto 0);
    bram_rddata_a : in std_logic_vector(31 downto 0));
end component;

component 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 component dma_s2mm;

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

sts_reg <= x"0000000" & "000" & dma_done;

m_axis_tdata  <= fifo_rd_data;
m_axis_tvalid <= not fifo_empty;

U_reg_ctrl : reg_ctrl
  port map(
    s_axi_aclk    => aclk,
    s_axi_aresetn => aresetn,
    s_axi_awaddr  => s_axi_lite_awaddr,
    s_axi_awprot  => s_axi_lite_awprot,
    s_axi_awvalid => s_axi_lite_awvalid,
    s_axi_awready => s_axi_lite_awready,
    s_axi_wdata   => s_axi_lite_wdata,
    s_axi_wstrb   => s_axi_lite_wstrb,
    s_axi_wvalid  => s_axi_lite_wvalid,
    s_axi_wready  => s_axi_lite_wready,
    s_axi_bresp   => s_axi_lite_bresp,
    s_axi_bvalid  => s_axi_lite_bvalid,
    s_axi_bready  => s_axi_lite_bready,
    s_axi_araddr  => s_axi_lite_araddr,
    s_axi_arprot  => s_axi_lite_arprot,
    s_axi_arvalid => s_axi_lite_arvalid,
    s_axi_arready => s_axi_lite_arready,
    s_axi_rdata   => s_axi_lite_rdata,
    s_axi_rresp   => s_axi_lite_rresp,
    s_axi_rvalid  => s_axi_lite_rvalid,
    s_axi_rready  => s_axi_lite_rready,
    bram_rst_a    => open,
    bram_clk_a    => open,
    bram_en_a     => reg_ctrl_en,
    bram_we_a     => reg_ctrl_we,
    bram_addr_a   => reg_ctrl_addr,
    bram_wrdata_a => reg_ctrl_wr_data,
    bram_rddata_a => reg_ctrl_rd_data);
    
reg_ctrl_wr_we <= '1' when reg_ctrl_en = '1' and (reg_ctrl_we = x"f") else '0';     
reg_ctrl_rd_re <= '1' when reg_ctrl_en = '1' and (reg_ctrl_we = x"0") else '0'; 

U_dma : dma_s2mm
  port map(                 
    dma_strt      => dma_strt,   
    dma_done      => dma_done,  
    dma_addr      => dma_addr,
    dma_len       => dma_len,     
    m_axi_aclk    => aclk,   
    m_axi_aresetn => aresetn,                
    m_axi_awaddr  => m_axi_awaddr, 
    m_axi_awlen   => m_axi_awlen,
    m_axi_awsize  => m_axi_awsize,
    m_axi_awburst => m_axi_awburst,
    m_axi_awprot  => m_axi_awprot, 
    m_axi_awcache => m_axi_awcache,
    m_axi_awvalid => m_axi_awvalid, 
    m_axi_awready => m_axi_awready, 
    m_axi_wdata   => m_axi_wdata,   
    m_axi_wstrb   => m_axi_wstrb,  
    m_axi_wlast   => m_axi_wlast,  
    m_axi_wvalid  => m_axi_wvalid, 
    m_axi_wready  => m_axi_wready,
    m_axi_bresp   => m_axi_bresp, 
    m_axi_bvalid  => m_axi_bvalid,  
    m_axi_bready  => m_axi_bready,   
    s_axis_aclk   => max_clk,               
    s_axis_tdata  => s_axis_tdata,  
    s_axis_tvalid => s_axis_tvalid, 
    s_axis_tready => s_axis_tready);

dma_strt <= ctrl_reg(CTRL_DMA_STRT);

U_strm_fifo : fifo_32w_512d
  port map(
    wr_clk         => max_clk,
    rd_clk         => 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          => fifo_empty,
    rd_data_count  => open);

fifo_rst    <= not aresetn;
fifo_rd_re  <= m_axis_tready and not fifo_empty;
    
gen_din: for i in 0 to 3 generate 
  U_iddr_i0 : IDDR
    generic map(
      DDR_CLK_EDGE  =>"SAME_EDGE",
      INIT_Q1       =>'0',
      INIT_Q2       =>'0',
      SRTYPE        =>"SYNC")
    port map(
      Q1            => open,
      Q2            => din(i),
      C             => max_clk,
      CE            => '1',
      D             => max_din(i),
      R             => '0',
      S             => '0');
end generate;
             
------------------------------------------------------------
-- Name : Control Register
-- 
------------------------------------------------------------
process (aclk, aresetn)
begin
  if aresetn = '0' then                
    ctrl_reg <= (others => '0');
  elsif rising_edge(aclk) then        
    if reg_ctrl_wr_we = '1' and reg_ctrl_addr = std_logic_vector(to_unsigned(CTRL_ADDR, reg_ctrl_addr'length)) then     
      ctrl_reg <= reg_ctrl_wr_data;
    else
      if (ctrl_reg(CTRL_DMA_STRT) = '1') then
        ctrl_reg(CTRL_DMA_STRT) <= '0';
      end if;    
      
      if (ctrl_reg(CTRL_MODE_NORM) = '1') then
        ctrl_reg(CTRL_MODE_NORM) <= '0';
      end if;   
      
      if (ctrl_reg(CTRL_MODE_TEST) = '1') then
        ctrl_reg(CTRL_MODE_TEST) <= '0';
      end if;                 
    end if;
  end if;
end process;  

------------------------------------------------------------
-- Name : DMA Address
-- 
------------------------------------------------------------
process (aclk)
begin
  if rising_edge(aclk) then        
    if reg_ctrl_wr_we = '1' and reg_ctrl_addr = std_logic_vector(to_unsigned(DMA_ADDR_ADDR, reg_ctrl_addr'length)) then     
      dma_addr <= reg_ctrl_wr_data;
    end if;
  end if;    
end process;  

------------------------------------------------------------
-- Name : DMA Length
-- 
------------------------------------------------------------
process (aclk)
begin
  if rising_edge(aclk) then        
    if reg_ctrl_wr_we = '1' and reg_ctrl_addr = std_logic_vector(to_unsigned(DMA_LEN_ADDR, reg_ctrl_addr'length)) then     
      dma_len <= reg_ctrl_wr_data;
    end if;
  end if;    
end process;  

------------------------------------------------------------
-- Name : Register Read Data
-- 
------------------------------------------------------------
process (aclk)
begin
  if rising_edge (aclk) then
    case to_integer(unsigned(reg_ctrl_addr(S_AXI_ADDR_WIDTH-1 downto 0)))is
      when STS_ADDR       => reg_ctrl_rd_data <= sts_reg;
      when DMA_ADDR_ADDR  => reg_ctrl_rd_data <= dma_addr;      
      when DMA_LEN_ADDR   => reg_ctrl_rd_data <= dma_len;                             
      when others         => reg_ctrl_rd_data <= (others => '0');
    end case;   
  end if;
end process;  

------------------------------------------------------------
-- Name : 
-- 
------------------------------------------------------------
process (max_clk, aresetn)
begin
  if aresetn = '0' then                
    din_cnt <= to_unsigned(0, din_cnt'length);
  elsif rising_edge(max_clk) then        
    din_cnt <= din_cnt + to_unsigned(1, din_cnt'length); 
  end if;    
end process;  

------------------------------------------------------------
-- Name : Mode
-- Ses the data mode of the DMA
------------------------------------------------------------
process (aclk, aresetn)
begin
  if aresetn = '0' then                
    mode <= '0';
  elsif rising_edge(aclk) then        
    if (ctrl_reg(CTRL_MODE_NORM) = '1') then     
      mode <= '0';
    else
      if (ctrl_reg(CTRL_MODE_TEST) = '1') then
        mode <= '1';
      end if;      
    end if;
  end if;
end process;  

------------------------------------------------------------
-- Name : Slave TDATA
-- 
------------------------------------------------------------
process (max_clk)
begin
  if rising_edge(max_clk) then     
    if (mode = '0') then   
      case din_cnt is
        when "000" => s_axis_tdata(3 downto 0) <= din;
        when "001" => s_axis_tdata(7 downto 4) <= din;
        when "010" => s_axis_tdata(11 downto 8) <= din;
        when "011" => s_axis_tdata(15 downto 12) <= din;
        when "100" => s_axis_tdata(19 downto 16) <= din;
        when "101" => s_axis_tdata(23 downto 20) <= din;
        when "110" => s_axis_tdata(27 downto 24) <= din;
        when "111" => s_axis_tdata(31 downto 28) <= din;    
        when others =>              
      end case;
    else
      s_axis_tdata <= std_logic_vector(test_cnt);
    end if;
  end if;    
end process;  

------------------------------------------------------------
-- Name : Slave TVALID
-- 
------------------------------------------------------------
process (max_clk)
begin
  if rising_edge(max_clk) then        
    if (din_cnt = "111" and s_axis_tready = '1') then
      s_axis_tvalid <= '1';
    else
      s_axis_tvalid <= '0';
    end if;
  end if;    
end process;  

------------------------------------------------------------
-- Name : FIFO Write Data
-- 
------------------------------------------------------------
process (max_clk)
begin
  if rising_edge(max_clk) then      
    case din_cnt is
      when "000" => fifo_wr_data(3 downto 0) <= din;
      when "001" => fifo_wr_data(7 downto 4) <= din;
      when "010" => fifo_wr_data(11 downto 8) <= din;
      when "011" => fifo_wr_data(15 downto 12) <= din;
      when "100" => fifo_wr_data(19 downto 16) <= din;
      when "101" => fifo_wr_data(23 downto 20) <= din;
      when "110" => fifo_wr_data(27 downto 24) <= din;
      when "111" => fifo_wr_data(31 downto 28) <= din;    
      when others =>              
    end case;
  end if;    
end process;  

------------------------------------------------------------
-- Name : FIFO Write Enable
-- 
------------------------------------------------------------
process (max_clk)
begin
  if rising_edge(max_clk) then        
    if (din_cnt = "111" and fifo_full = '0') then
      fifo_wr_we <= '1';
    else
      fifo_wr_we <= '0';
    end if;
  end if;    
end process;  

------------------------------------------------------------
-- Name : Test Counter
-- Counter to use when testing the DMA
------------------------------------------------------------
process (max_clk, aresetn)
begin
  if aresetn = '0' then                
    test_cnt <= to_unsigned(0, test_cnt'length);
  elsif rising_edge(max_clk) then        
    if (din_cnt = "111" and s_axis_tready = '1') then
      test_cnt <= test_cnt + to_unsigned(1, test_cnt'length);
    end if;
  end if;    
end process;  

end Behavioral;