% ////////////////////////////////////////////////////////////////////
% FILE,     HBF2LGenHdlStim.m
% PROJECT,  HBF2L
% AUTHOR,   weitao.du
% DATE,     2007.July.19th
% VERSION,  0.1
% ////////////////////////////////////////////////////////////////////
% DECRIPTION
% 2 Level Half Band Fir Filter Design and Simulation, HDL stimulaiton 
% generation
% ////////////////////////////////////////////////////////////////////

function HBF2LGenHdlStim(cfg, simData);
  fprintf(1, 'HBF2LGenHdlStim(), run\n');

  stimDefFileName = 'stim_def.v'; 
  stimDefFileDir  = '../../HDL/src/';
  stimDefFileDirName = strcat(stimDefFileDir, stimDefFileName);

  stimDataFileName = 'HBF2LStimData.dat';
  stimDataFileDir  = '../../HDL/sim/';
  stimDataFileDirName = strcat(stimDataFileDir, stimDataFileName);
  
  resultDataFileName = 'HBF2LResultData.dat';
  resultDataFileDir  = '../../HDL/sim/';
  resultDataFileDirName = strcat(resultDataFileDir, resultDataFileName);

  inputDataFxp  = simData.dataFxp.dbg.HBFLv1.inputDataFxp;
  outLv2Fxp     = simData.dataFxp.dbg.HBFLv2.outLv2Fxp;

  inputWL       = cfg.WLCfg.inputWL;
  outputWL      = cfg.WLCfg.outputWL;
  coeffWL       = cfg.WLCfg.coeffWL;
  stimLen       = cfg.stimLen;
  resultLen     = stimLen*4;
  
  fidStimDef    = fopen(stimDefFileDirName, 'w');
  if(fidStimDef == -1)
    fprintf(1, 'ERROR: create file "%s" failed\n', stimDefFileDirName);
    return;
  end
  fprintf(1, 'HBF2LGenHdlStim(), open file %s write stim defination\n', stimDefFileDirName); 

  fprintf(fidStimDef, '//############################################\n');
  fprintf(fidStimDef, '// HBF2L stim define file      \n');
  fprintf(fidStimDef, '// In verilog define syntax\n');
  fprintf(fidStimDef, '// Generated by HBF2LGenHdlStim.m \n');
  fprintf(fidStimDef, '// @ Date %s \n',  datestr(now));
  fprintf(fidStimDef, '// -------------------------------------------\n');
  fprintf(fidStimDef, '`define STIMLEN        %d\n', stimLen);
  fprintf(fidStimDef, '`define INPUTWL        %d\n', inputWL);
  fprintf(fidStimDef, '`define OUTPUTWL       %d\n', outputWL);
  fprintf(fidStimDef, '`define COEFWL         %d\n', coeffWL);

  fclose(fidStimDef);

  % ================================================================================================== 
  % WRITE STIM DATA FILE

  fidStimData    = fopen(stimDataFileDirName, 'w');
  if(fidStimData == -1)
    fprintf(1, 'ERROR: create file "%s" failed\n', stimDataFileDirName);
    return;
  end
  fprintf(1, 'HBF2LGenHdlStim(), open file %s write stim data\n', stimDataFileDirName); 

  fprintf(fidStimData, '//#########################################################################\n');
  fprintf(fidStimData, '// HBF2L stim data file      \n');
  fprintf(fidStimData, '// In verilog binary mem format\n');
  fprintf(fidStimData, '// Generated by HBF2LGenHdlStim.m \n');
  fprintf(fidStimData, '// @ Date %s \n',  datestr(now));
  fprintf(fidStimData, '//#########################################################################\n');
  
  fprintf(fidStimData, '// %dBIT-INPUTDATAFXP \n', inputWL);
  
  dataBinStrIn  = cell(stimLen,1);
  dataHexStrIn  = cell(stimLen,1);
  dataIn    = zeros(stimLen, 1);
  for(idx = 1:stimLen)
    dataBinStrIn{idx}   = bin(inputDataFxp(idx));
    dataHexStrIn{idx}   = hex(inputDataFxp(idx));
    dataIn(idx)     = double(inputDataFxp(idx));
    
  end % for(idx = 1:stimLen)

  for(idx = 1:stimLen)
    fprintf(fidStimData, '%s',dataBinStrIn{idx});
    fprintf(fidStimData, '  //');
    
    fprintf(fidStimData, '  %-4d', idx);

    fprintf(fidStimData, '  %-10f',double(dataIn(idx)));

    fprintf(fidStimData, '  %s',dataHexStrIn{idx});
    fprintf(fidStimData, '\n');
  end % for(idx = 1:stimLen)
  
  fclose(fidStimData);

  % ================================================================================================== 
  % WRITE RESULT DATA FILE

  fidResultData    = fopen(resultDataFileDirName, 'w');
  if(fidResultData== -1)
    fprintf(1, 'ERROR: create file "%s" failed\n', resultDataFileDirName);
    return;
  end
  fprintf(1, 'HBF2LGenHdlStim(), open file %s write stim data\n', resultDataFileDirName); 

  fprintf(fidStimData, '//#########################################################################\n');
  fprintf(fidStimData, '// HBF2L result data file      \n');
  fprintf(fidStimData, '// In verilog binary mem format\n');
  fprintf(fidStimData, '// Generated by HBF2LGenHdlStim.m \n');
  fprintf(fidStimData, '// @ Date %s \n',  datestr(now));
  fprintf(fidStimData, '//#########################################################################\n');
  
  fprintf(fidStimData, '// %dBIT-OUTPUDATAFXP \n', outputWL);
  
  dataBinStrOut = cell(resultLen,1);
  dataHexStrOut = cell(resultLen,1);
  dataOut       = zeros(resultLen, 1);

  for(idx = 1:resultLen)
    dataHexStrOut{idx}  = hex(outLv2Fxp(idx));
    dataBinStrOut{idx}  = bin(outLv2Fxp(idx));
    dataOut(idx)    = double(outLv2Fxp(idx));
  end % for(idx = 1:resultLen)

  for(idx = 1:resultLen)
    fprintf(fidStimData, '%s',dataBinStrOut{idx});
    fprintf(fidStimData, '  //');
    
    fprintf(fidStimData, '  %-6d', idx);

    fprintf(fidStimData, '%-10f ',double(dataOut(idx)));

    fprintf(fidStimData, '%s',dataHexStrOut{idx});
    fprintf(fidStimData, '\n');
  end % for(idx = 1:resultLen)
  
  fclose(fidResultData);

end  % function HBF2LGenHdlStim();
