text: /*******************************************************************************
   * Copyright 2016 -- 2022 IBM Corporation
   *
   * 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.
  *******************************************************************************/
  
  /*****************************************************************************
   * @file       commom.hpp
   * @brief      Common functions for testbenches - headers.
   *
   * @date       October 2020
   * @author     FAB, WEI, NGL, DID
   * 
   * @ingroup VitisQuantitativeFinance 
   * @addtogroup VitisQuantitativeFinance 
   * \{
   *****************************************************************************/
  
  
  #ifndef _TEST_COMMON_H_
  #define _TEST_COMMON_H_
  
  
  #include <stdio.h>
  #include <string>
  #include <hls_stream.h>
  #include <sys/stat.h>
  #include "../../../../../HOST/quantitative_finance/mceuropeanengine/languages/cplusplus/include/config.h"
  #include "../../mceuropeanengine/include/mceuropeanengine.hpp"
  
  
  //---------------------------------------------------------
  // HELPERS FOR THE DEBUGGING TRACES
  //  .e.g: DEBUG_LEVEL = (MDL_TRACE | IPS_TRACE)
  //---------------------------------------------------------
  #define THIS_NAME "TB"
  
  #define TRACE_OFF     0x0000
  #define TRACE_URIF   1 <<  1
  #define TRACE_UAF    1 <<  2
  #define TRACE_MMIO   1 <<  3
  #define TRACE_ALL     0xFFFF
  
  #define DEBUG_LEVEL (TRACE_ALL)
  
  
  //------------------------------------------------------
  //-- TESTBENCH DEFINES
  //------------------------------------------------------
  #define OK          true
  #define KO          false
  #define VALID       true
  #define UNVALID     false
  #define DEBUG_TRACE true
  
  #define ENABLED     (ap_uint<1>)1
  #define DISABLED    (ap_uint<1>)0
  
  
  
  
  /*****************************************************************************
   * @brief Initialize an input data stream from a file.
   *
   * @param[in] sDataStream the input data stream to set.
   * @param[in] dataStreamName the name of the data stream.
   * @param[in] inpFileName the name of the input file to read from.
   * @return OK if successful otherwise KO.
   ******************************************************************************/
  bool setInputDataStream(stream<UdpWord> &sDataStream, const std::string dataStreamName, 
  			const std::string inpFileName, int simCnt);
  
  
  /*****************************************************************************
   * @brief Read data from a stream.
   *
   * @param[in]  sDataStream,    the output data stream to read.
   * @param[in]  dataStreamName, the name of the data stream.
   * @param[out] udpWord,        a pointer to the storage location of the data
   *                              to read.
   * @return VALID if a data was read, otherwise UNVALID.
   ******************************************************************************/
  bool readDataStream(stream <UdpWord> &sDataStream, UdpWord *udpWord);
  
  
  /*****************************************************************************
   * @brief Pack an array of 8 x ap_uint<8> into a ap_uint<64> word.
   *
   * @param[in]  buffer     A pointer to an array of 8 x ap_uint<8>
   * @return An ap_uint<64> word.
   ******************************************************************************/
  ap_uint<64> pack_ap_uint_64_ (ap_uint<8> *buffer);
  
  
  /*****************************************************************************
   * @brief Dump a data word to a file.
   *
   * @param[in] udpWord,      a pointer to the data word to dump.
   * @param[in] outFileStream,the output file stream to write to.
   * @return OK if successful, otherwise KO.
   ******************************************************************************/
  bool dumpDataToFile(UdpWord *udpWord, std::ofstream &outFileStream);
  
  
  /*****************************************************************************
   * @brief Initialize an input data stream from a file.
   *
   * @param[in]  inpFileName the name of the input file to read from.
   * @param[out] strOutput the output string to set.
   * @return OK if successful otherwise KO.
   ******************************************************************************/
  bool dumpFileToString(const std::string inpFileName, std::string strOutput, int simCnt);
  
  
  /*****************************************************************************
   * @brief Fill an output file with data from an output stream.
   *
   * @param[in] sDataStream     the output data stream to set.
   * @param[in] dataStreamName  the name of the data stream.
   * @param[in] outFileName     the name of the output file to write to.
   * @return                    OK if successful, otherwise KO.
   ******************************************************************************/
  bool getOutputDataStream(stream<UdpWord> &sDataStream,
                           const std::string  dataStreamName, const std::string outFileName, int simCnt);
  
  /*****************************************************************************
   * @brief Fill an output file with data from an output stream.
   *
   * @param[in] s            the struct with configuration parameters for MCEuropeanEngine.
   * @param[in] outFileName  the name of the output file to write to.
   * @return                 OK if successful, otherwise KO.
   ******************************************************************************/
  bool dumpStructToFile(varin *s, const std::string outFileName, int simCnt);
  
  static inline unsigned int 
  readFileConfigToStruct(const char *fname, varin *instruct);
  
  #endif
  
  /*! \} */
  