-------------------------------------------------------------------------------
--
-- Copyright 2022
-- ASTRON (Netherlands Institute for Radio Astronomy) <http://www.astron.nl/>
-- P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-------------------------------------------------------------------------------
-- Author: Job van Wee
-- Purpose: controller for ddrctrl, decides when to write when to read or when to stop writing or reading.
--
-- Description:
--
-- Remark:
--  Use VHDL coding template from:
--  https://support.astron.nl/confluence/display/SBe/VHDL+design+patterns+for+RTL+coding
--  

LIBRARY IEEE, dp_lib, common_lib, tech_ddr_lib;
USE IEEE.std_logic_1164.ALL;
USE IEEE.numeric_std.ALL;
USE dp_lib.dp_stream_pkg.ALL;
USE common_lib.common_mem_pkg.ALL;
USE common_lib.common_pkg.ALL;
USE tech_ddr_lib.tech_ddr_pkg.ALL;


ENTITY ddrctrl_controller IS
  GENERIC (
    g_tech_ddr                : t_c_tech_ddr;
    g_stop_percentage         : NATURAL     := 50
  );
  PORT (
    clk	     	                : IN  STD_LOGIC;
    rst                       : IN  STD_LOGIC;

    -- ddrctrl_input
    out_of                    : IN NATURAL;
    out_sosi                  : IN t_dp_sosi;
    out_adr                   : IN NATURAL;

    -- io_ddr
    dvr_mosi                  : OUT t_mem_ctlr_mosi;
    wr_sosi                   : OUT t_dp_sosi;
    rd_siso                   : OUT t_dp_siso;

    -- ddrctrl
    stop_in	                  : IN  STD_LOGIC;
    stop_out                  : OUT STD_LOGIC
  );
END ddrctrl_controller;

ARCHITECTURE rtl OF ddrctrl_controller IS

  CONSTANT  c_burstsize       : NATURAL                                     := 64;                                            -- max burstsize for max troughput
  CONSTANT  c_bitshift_adr    : NATURAL                                     := ceil_log2(c_burstsize);                        -- bitshift to make sure there is only a burst start at a interval of c_burstsize.
  CONSTANT  c_adr_w           : NATURAL                                     := func_tech_ddr_ctlr_address_w( g_tech_ddr );    -- the lengt of the address vector, for simulation this is smaller, otherwise the simulation would take to long, 27
  CONSTANT  c_max_adr         : NATURAL                                     := 2**(c_adr_w)-1;                                -- the maximal address that is possible within the vector length of the address
  CONSTANT  c_pof_ma          : NATURAL                                     := (c_max_adr*(100-g_stop_percentage))/100;
  CONSTANT  c_zeros           : STD_LOGIC_VECTOR(c_bitshift_adr-1 DOWNTO 0) := (OTHERS => '0');

  -- type for statemachine
  TYPE t_state IS (RESET, WRITING, SET_STOP, STOP_WRITING, READING, STOP_READING, IDLE);

  -- record for readability
  TYPE t_reg IS RECORD
  -- state of program
  state                       : t_state;

  -- signals
  stop_adr                    : STD_LOGIC_VECTOR(c_adr_w-1 DOWNTO 0);
  stopped                     : STD_LOGIC;

  -- output
  dvr_mosi                    : t_mem_ctlr_mosi;
  wr_sosi                     : t_dp_sosi;
  rd_siso                     : t_dp_siso;
  END RECORD;

  CONSTANT c_t_reg_init       : t_reg         := (RESET, TO_UVEC(c_max_adr, c_adr_w), '0', c_mem_ctlr_mosi_rst, c_dp_sosi_init, c_dp_siso_rst);


  -- signals for readability
  SIGNAL d_reg                : t_reg         := c_t_reg_init;
  SIGNAL q_reg                : t_reg         := c_t_reg_init;

BEGIN

  q_reg <= d_reg WHEN rising_edge(clk);

  -- put the input data into c_v and fill the output vector from c_v
  p_state : PROCESS(q_reg, rst, out_of, out_sosi, out_adr)

    VARIABLE v                : t_reg         := c_t_reg_init;

  BEGIN

    v := q_reg;
    CASE q_reg.state IS



    WHEN RESET =>
      v := c_t_reg_init;


    WHEN WRITING =>
      IF TO_UVEC(out_adr, c_adr_w)(c_bitshift_adr-1 DOWNTO 0) = c_zeros THEN                        -- if adr mod c_burstsize = 0
        v.dvr_mosi.burstbegin   := '1';
        IF out_adr = 0 THEN
          v.dvr_mosi.address    := TO_UVEC(c_max_adr-c_burstsize, dvr_mosi.address'length);
        ELSE
          v.dvr_mosi.address    := TO_UVEC(out_adr-c_burstsize, dvr_mosi.address'length);
        END IF;
      ELSE
        v.dvr_mosi.burstbegin   := '0';
      END IF;
      v.dvr_mosi.burstsize      := TO_UVEC(c_burstsize, dvr_mosi.burstsize'length);
      v.dvr_mosi.wr             := '1';
      v.dvr_mosi.rd             := '0';
      v.wr_sosi                 := out_sosi;



    WHEN SET_STOP =>
      --setting a stop address dependend on the g_stop_percentage
      IF out_adr+c_pof_ma >= c_max_adr THEN
        v.stop_adr(c_adr_w-1 DOWNTO c_bitshift_adr) := TO_UVEC(out_adr-c_pof_ma, c_adr_w)(c_adr_w-1 DOWNTO c_bitshift_adr);
      ELSE
        v.stop_adr(c_adr_w-1 DOWNTO c_bitshift_adr) := TO_UVEC(out_adr+c_pof_ma, c_adr_w)(c_adr_w-1 DOWNTO c_bitshift_adr);
      END IF;
      v.stop_adr(c_bitshift_adr-1 DOWNTO 0)         := c_zeros;

      -- still a write cyle
      IF TO_UVEC(out_adr, c_adr_w)(c_bitshift_adr-1 DOWNTO 0) = c_zeros THEN                        -- adr mod 64 = 0
        v.dvr_mosi.burstbegin                   := '1';
        IF out_adr = 0 THEN
          v.dvr_mosi.address                    := TO_UVEC(c_max_adr-c_burstsize, dvr_mosi.address'length);
        ELSE
          v.dvr_mosi.address                    := TO_UVEC(out_adr-c_burstsize, dvr_mosi.address'length);
        END IF;
      ELSE
        v.dvr_mosi.burstbegin                   := '0';
      END IF;
      v.dvr_mosi.burstsize                      := TO_UVEC(c_burstsize, dvr_mosi.burstsize'length);
      v.dvr_mosi.wr                             := '1';
      v.dvr_mosi.rd                             := '0';
      v.wr_sosi                                 := out_sosi;



    WHEN STOP_WRITING =>
      v.stopped                                 := '1';
      v.wr_sosi.valid                           := '0';
      v.dvr_mosi.flush                          := '1';

    WHEN IDLE =>
    


    WHEN OTHERS =>
      v := c_t_reg_init;



    END CASE;

    IF rst = '1' THEN
      v.state := RESET;
    ELSIF stop_in = '1' THEN
      v.state := SET_STOP;
    ELSIF v.stop_adr = TO_UVEC(out_adr, c_adr_w) AND v.stop_adr(c_bitshift_adr-1 DOWNTO 0) = c_zeros(c_bitshift_adr-1 DOWNTO 0) AND q_reg.stopped = '0' THEN
      v.state := STOP_WRITING;
    ELSIF v.stopped = '1' THEN
      v.state := IDLE;
    ELSE
      v.state := WRITING;
    END IF;
    d_reg     <= v;
  END PROCESS;

  -- fill outputs
  dvr_mosi  <= q_reg.dvr_mosi;
  wr_sosi   <= q_reg.wr_sosi;
  rd_siso   <= q_reg.rd_siso;
  stop_out  <= q_reg.stopped;

END rtl;