// ------------------------------------------------------------------------
// ON Semiconductor Proprietary and Confidential Information.
// ------------------------------------------------------------------------

// ------------------------------------------------------------------------
// Company           : ON Semiconductor
// ------------------------------------------------------------------------
// File              : $RCSfile: tb_pkg.sv,v $
// ------------------------------------------------------------------------
// Last Modified     : $Date: 2016/04/01 08:03:44 $
// Last Modified By  : $Author: fg7g4t $
// Revision          : $Revision: 1.1 $
// ------------------------------------------------------------------------
// Description       : Testbench package
// ------------------------------------------------------------------------
// Revision history  :
//
//   $Log: tb_pkg.sv,v $
//   Revision 1.1  2016/04/01 08:03:44  fg7g4t
//   Initial revision (directory structure & standard scripts revision 255).
//

package tb_pkg;

  // import libmath functions
  import "DPI-C" context get_real_c = function real get_real_v ();
  import "DPI-C" context put_real_c = function void put_real_v (real x);
  import "DPI-C" context get_pi     = function real get_pi ();
  import "DPI-C" context sv_sin     = function real sv_sin (real x);
  import "DPI-C" context sv_cos     = function real sv_cos (real x);
  import "DPI-C" context sv_tan     = function real sv_tan (real x);
  import "DPI-C" context sv_asin    = function real sv_asin (real x);
  import "DPI-C" context sv_acos    = function real sv_acos (real x);
  import "DPI-C" context sv_atan    = function real sv_atan (real x);
  import "DPI-C" context sv_log     = function real sv_log (real x);
  import "DPI-C" context sv_log10   = function real sv_log10 (real x);
  import "DPI-C" context sv_exp     = function real sv_exp (real x);
  import "DPI-C" context sv_sqrt    = function real sv_sqrt (real x);
  import "DPI-C" context sv_pow     = function real sv_pow (real base, power);

//   // import generic communication functions (for Matlab communication)
//   import "DPI-C" context function int sv2c(input  int  handle,
//                                            input  int  to_matlab_int[],
//                                            input  real to_matlab_real[40],
//                                            input  int  to_matlab_r_width,
//                                            output int  from_matlab_int[],
//                                            output real from_matlab_real[16],
//                                            input  int  from_matlab_r_width);
//   import "DPI-C" context function int init_sv2c(input string server,
//                                                 input int    port,
//                                                 input string workdir,
//                                                 input string logfile,
//                                                 input string init,
//                                                 input string func);
//   import "DPI-C" context function void close_sv2c(input int handle);

  // ********************************
  // ***** GLOBALLY USED PARAMS *****
  // ********************************

  // verbosity level
  localparam c_ERR_ONLY             = 0;
  localparam c_INFO                 = 1;
  localparam c_VERBOSE              = 2;
  localparam c_DEBUG                = 3;

  // checks enable / disable
  localparam c_LOGIC_CHECKS         = 1'b1;
  localparam c_X_CHECKS             = 1'b1;

  // timeout before simulation is stopped after error has been encountered
  localparam c_SIM_ERROR_TIMEOUT    = 10us;

  // main clock frequency definition
  localparam c_CLK_PER_MAX          = 125.0ns;  // 8 MHz -> 10 * 0.8 = 8 MHz
  localparam c_CLK_PER_TYP          = 100.0ns;  // 10 MHz
  localparam c_CLK_PER_MIN          = 83.0ns;   // 12.05 MHz -> 10 * 1.2 = 12 MHz
  localparam c_CLK_PER_NON_TRIM     = 131.0ns;  // 7.63 MHz

  localparam c_DEL                  = 10ns;
  localparam c_DEL2                 = 20ns;
  localparam c_DELS                 = 40ns;   // static checker delay (gate delay)
  localparam c_DELST                = 500ns;  // startup delay

  localparam real c_GLITCH_THR      = 0.7;    // ratio of clk_per

  // A/D interface bus params

  // OTP constants

  // *******************************
  // ***** GLOBALLY USED TYPES *****
  // *******************************

  // clock edge definition
  typedef enum {
    e_NOEDGE    = 0,
    e_POSEDGE   = 1,
    e_NEGEDGE   = 2,
    e_ANYEDGE   = 3
  } t_EDGE;

  // A/D interface types

  // *******************************************
  // ***** GLOBALLY USED TASKS & FUNCTIONS *****
  // *******************************************

//   // function to initialize generic communication interface ---------------
//   function automatic int init_sv2c_wrapper(input string serverName,
//                                            input int serverPort,
//                                            input string workdir,
//                                            input string logfile,
//                                            input string mPaths[],
//                                            input real samplePeriod,
//                                            input string functionName);
//     var string cmd;
//     var string fun;
//     var string sp;
//     var int i;
//
//
//     if(mPaths.size() > 0) begin
//       cmd = "addpath(";
//       for(i = 0; i < (mPaths.size() - 1); ++i) begin
//         cmd = {cmd, "'", mPaths[i], "', "};
//       end
//       if(mPaths.size() > 0) begin
//         cmd = {cmd, "'", mPaths[mPaths.size()-1], "'"};
//       end
//       cmd = {cmd, "); "};
//     end
//     sp.realtoa(samplePeriod);
//     fun = {functionName, "(", sp, ", fd);"};
//     return init_sv2c(serverName, serverPort, workdir, logfile, cmd, fun);
//   endfunction

  /* ***************************************************************************
    return absolute value of argument
  *************************************************************************** */
  function real f_abs (input real val);
    return ((val < 0.0) ? -val : val);
  endfunction

  /* ***************************************************************************
    return value within min/max limits
  *************************************************************************** */
  function real f_limit (input real val, min, max);
    if      (val < min) return (min);
    else if (val > max) return (max);
    else return (val);
  endfunction

  /* ***************************************************************************
    return min of two numbers
  *************************************************************************** */
  function real f_min (input real a, b);
    return ((a < b) ? a : b);
  endfunction

  /* ***************************************************************************
    return max of two numbers
  *************************************************************************** */
  function real f_max (input real a, b);
    return ((a > b) ? a : b);
  endfunction

endpackage

