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

function HBF2LGenRTL(cfg, simData);
  fprintf(1, 'HBF2LGenRTL(), run\n');
  GenCROM(cfg, simData);
  PackRtl(cfg, simData);
end  % function HBF2LGenRTL();
% ////////////////////////////////////////////////////////////////////
function PackRtl(cfg, simData);
  RTLFileDir  = '../../HDL/src';
  topModuleName = 'HBF2L';
  % pack rtl
  rtlFileList = cell(5,1);
  rtlFileList{1} = 'HBF2L.v';
  rtlFileList{2} = 'HBFctl.v';
  rtlFileList{3} = 'crom.v';
  rtlFileList{4} = 'HBFdch.v';
  rtlFileList{5} = 'HBFdmem2bk.v'; 

  pkFileNum = length(rtlFileList);
  % add path to file name
  for(idx=1:pkFileNum)
    rtlFileList{idx} = strcat(RTLFileDir,'/',rtlFileList{idx});
  end
  outRtlFileName       = strcat(topModuleName, '_PK.v');
  outRtlFileNameDir    = strcat(RTLFileDir,'/', outRtlFileName);
  packSrc(rtlFileList, outRtlFileNameDir);

end % function PackRtl(cfg, simData);
% ////////////////////////////////////////////////////////////////////

% ////////////////////////////////////////////////////////////////////
function GenCROM(cfg, simData);
  RTLFileDir  = '../../HDL/src/';
  CROMFileName = 'crom.v';
  romFileDirName = strcat(RTLFileDir, CROMFileName);

  K1 = cfg.K1;
  K2 = cfg.K2;
  coeffWL = cfg.WLCfg.coeffWL;
  coeffNum = K1+K2;
  coeff1CdsFxp = simData.dataFxp.dbg.HBFLv1.coeff1CdsFxp;
  coeff2CdsFxp = simData.dataFxp.dbg.HBFLv2.coeff2CdsFxp;
  coeffCdsFxp = [coeff1CdsFxp; coeff2CdsFxp];
  romAddrLen = floor(log2(coeffNum) + 0.5);
  coeffStrCell = cell(coeffNum, 1);

  for(idx = 1:coeffNum)
   coeffStrCell{idx} = bin(coeffCdsFxp(idx));
  end % for(idx = 1:coeffNum)

  fidRomFile = fopen(romFileDirName, 'w');
  if(fidRomFile == -1)
    errMsg = strcat('ERROR, GenCROM(), create file',romFileDirName, ',failed');
    fprintf(1, '%s\n', errMsg);
    return;
  end
  fprintf(fidRomFile,  ...
  '// ************************************************************** //\n');
  fprintf(fidRomFile,  ...
  '// FILE    : %s \n', CROMFileName);
  fprintf(fidRomFile,  ...
  '// DSCP    : HBF2L cofficent rom file \n');
  fprintf(fidRomFile,  ...
  '// Generated by HBF2LGenRTL()\n');
  fprintf(fidRomFile,  ...
  '// Date    : %s \n',  datestr(now));
  fprintf(fidRomFile,  ...
  '// ************************************************************** //\n');
  % generate the crom module
  fprintf(fidRomFile, ...
  '// module crom(), coeff rom\n');
  fprintf(fidRomFile, ...
  'module crom(\n');
  fprintf(fidRomFile, ...
  '  CLK  ,         // clock\n');
  fprintf(fidRomFile, ...
  '  CRA,           // coeff read address\n');
  fprintf(fidRomFile, ...
  '  CRD);          // coeff read data\n');
  
  fprintf(fidRomFile, ...
  'input          CLK;\n');
  fprintf(fidRomFile, ...
  'input  [%-2d :0] CRA;\n', romAddrLen-1); 
  fprintf(fidRomFile, ...
  'output [%-2d :0] CRD;\n', coeffWL-1);
  fprintf(fidRomFile, ...
  'reg    [%-2d :0] CRD;\n', coeffWL-1);
  
  fprintf(fidRomFile, ...
  'always @ (posedge CLK)\n'); 
  fprintf(fidRomFile, ...
  '  case(CRA)\n');
  
  for addr = 0:1:coeffNum-1
    fprintf(fidRomFile, ... 
  '     %-2d''d %-6d:CRD = #2 %-2d''b %s; // %-9f %s \n', ...
      romAddrLen,addr, coeffWL, coeffStrCell{addr+1},  double(coeffCdsFxp(addr+1)), hex(coeffCdsFxp(addr+1)));
  end
  fprintf(fidRomFile, ...
  '  default : CRD = #2 0;\n');
  fprintf(fidRomFile, ...
  '  endcase\n');
  
  fprintf(fidRomFile, ...
  'endmodule \n');
  fclose(fidRomFile);

end % function GenCROM(cfg, simData);
% ////////////////////////////////////////////////////////////////////
