tb_interp.h 4.92 KB
Newer Older
Anirudh Kaushik's avatar
Anirudh Kaushik committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236

//========================================================================================
// 
// File Name    : tb_interp.h
// Description  : Header for the testbench
// Release Date : 23/07/2013
// Author       : PolyU DARC Lab
//                Benjamin Carrion Schafer, Anushree Mahapatra  
//
// Revision History
//---------------------------------------------------------------------------------------
// Date           Version   Author     Description
//----------------------------------------------------------------------------------------
// 23/07/2013        1.0    PolyU      TB header file
//=======================================================================================
#ifndef TB_INTERP_
#define TB_INTERP_


#define SC_INCLUDE_FX

#include "define.h"


SC_MODULE(tb_interp){

 public:


  //inputs  
  sc_in_clk clk;
  sc_in<bool> rst;


  sc_in<bool> odata_en;
  sc_in<sc_fixed <16,1,SC_RND,SC_SAT> > odata;

  //outputs

  sc_out<sc_fixed <16,1,SC_TRN,SC_WRAP> > indata;
  sc_out<sc_ufixed<9,0,SC_TRN,SC_WRAP> > infactor;


  // Variables declaration
  FILE *fptr_data;
  FILE *fptr_en;
  FILE *in_file_data, *in_file_factor;


  //function prototypes


void send(void){

  // Variables declarations
  float indata_var;
  float  infactor_var;
  int k;


   //Reset initialization
   in_file_data = fopen(IN_DATA, "r");
   in_file_factor = fopen( IN_FACTOR_DATA, "r");

   if(!in_file_data){
     cout<< endl << IN_DATA << " data file could not be opened " << endl;
     sc_stop();}

   if(!in_file_factor){
     cout<< endl << IN_FACTOR_DATA << " file could not be opened" << endl;
     sc_stop();

   }


  wait();

  while(true){

    while(fscanf(in_file_data,"%f",&indata_var) !=EOF &&  fscanf(in_file_factor,"%f", &infactor_var) != EOF){
     
      indata.write(indata_var);
      infactor.write(infactor_var);

      wait(); 
    }


    fclose(in_file_data);
    fclose(in_file_factor);
   
    cout << endl << "Start comparing results" << endl;
    compare_results();
    sc_stop();

    wait();
  }// end of while loop

 

}


//---------------------------------------
// Receive data from Interpolation Filter
//--------------------------------------
void recv(void){

  bool odata_en_rcv;
  float  odata_rcv;


  fptr_data = fopen(OUT_FILE , "wt");
  fptr_en =   fopen(OUT_FILE_ENABLE, "wt");
 
  if(!fptr_data){
    cout << endl << OUT_FILE << " could not be opened" << endl;
    sc_stop();
  }

  if(!fptr_en){
    cout << endl << OUT_FILE_ENABLE <<  " could not be opened" << endl;
    sc_stop();
  }

  wait();

  while(true){

    odata_en_rcv = odata_en.read();
    odata_rcv = odata.read();
    fprintf(fptr_en,"%c\n",odata_en_rcv?'1':'0');
    fprintf(fptr_data,"%f\n",odata_rcv);
  
  wait();
  }//end of while


}

//--------------------------------
// Compare simulation results vs. golden output
//--------------------------------

void compare_results(void){

  // Variables declaration
  float outfilter_golden,outfilter,golden_en, outfilter_en;
  int errors1, errors2, line1, line2;
  FILE *out_fil_golden;
  FILE *out_fil_golden_en; 
 

  fclose(fptr_en);
  fclose(fptr_data);

  fptr_en = fopen(OUT_FILE_ENABLE, "rt");
  fptr_data = fopen(OUT_FILE, "rt");
 
  line1 =0;
  errors1 = 0;
  line2 = 0;
  errors2=0;


  out_fil_golden = fopen(GOLDEN_OUTPUT , "rt");
  out_fil_golden_en = fopen(GOLDEN_OUTPUT_ENABLE, "rt");

  if(!out_fil_golden){
    cout << "Could not open " << GOLDEN_OUTPUT << endl;
    sc_stop();
  }
  

  if(!out_fil_golden_en){
    cout << "Could not open " << GOLDEN_OUTPUT_ENABLE << endl;
    sc_stop();
  }
 
    cout << "Verfying Ouptut data " <<  endl; 

  while(fscanf(out_fil_golden, "%f", &outfilter_golden)!= EOF && fscanf(fptr_data, "%f", &outfilter) != EOF ){
 
    cout << endl << "Cycle["<< line1 << "]:" << outfilter_golden << " -- " << outfilter;

    if(outfilter!= outfilter_golden){
      cout<< "\nOutput mismatch [line:" << line1 << "] Golden:" << outfilter_golden << "--output:"<<outfilter;
      errors1++;
    }

    line1++;

  }

    cout << endl<< endl << "Verfying Ouptut enable signal " <<  endl; 

  while(fscanf(out_fil_golden_en, "%f", &golden_en)!= EOF &&  fscanf(fptr_en, "%f",&outfilter_en) != EOF){
   
    cout<< endl <<  "Cycle["<< line2 << "]:" << golden_en << "--"<< outfilter_en;

    if(golden_en != outfilter_en){
      cout<<"\n Output enable mismatch [line:" << line1 << "] Golden: " << golden_en << " -- output: "<<outfilter_en;
      errors2 ++;
    }

    line2 ++;
  }


  if(errors1 == 0 && errors2 == 0)
    cout<< endl << endl << "Finished simulation successfully" << endl;
  else
    cout<< endl<< "Finished simulation with "<< errors1 << " and " << errors2<< " mismatches between Golden and simulation"<<endl;

  fclose(out_fil_golden_en);
  fclose(out_fil_golden);
  fclose(fptr_en);
  fclose(fptr_data);
}

 SC_CTOR(tb_interp){

    SC_CTHREAD(send,clk.pos());
    reset_signal_is(rst, false);

   SC_CTHREAD(recv,clk.pos());
   reset_signal_is(rst, false);

 }

 ~tb_interp(){}


};

#endif  // TB_INTERP