⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 genmathvhdl.cpp

📁 產生你所需要的FIR濾波器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		// Get Variables parameters	aName  = aIn->GetVarName();	aSize  = aIn->GetVarBitSize();    aShift = aIn->GetShift();    aSign  = aIn->GetSign();    // Set default values for B=NULL    if (bIn!=NULL) {	   bName  = bIn->GetVarName();	   bSize  = bIn->GetVarBitSize();       bShift = bIn->GetShift();       bSign  = bIn->GetSign();    } else {       bName  = "";       bSize  = 0;       bShift = 0;       bSign  = 1;    }    // Calculate Total Size (VarSize+Shift)        aTotalSize = aSize + aShift;    bTotalSize = bSize + bShift;        // Chose Destination Total Size    if (aTotalSize>bTotalSize) {    	destTotalSize = aTotalSize + 1;    } else {    	destTotalSize = bTotalSize + 1;    }        // Chouse destination Shift    if (aShift < bShift) {       destShift = aShift;    } else {       destShift = bShift;    }        ///////////////////////////////////////    if (cmd==CMD_A_pm_B) {        cout << "@@##@@##@@ CMD_A_pm_B  ##@@##@@##@@##\r\n";    	        /////////////////////////////////////////////////        // Create Variable        destSize = destTotalSize - destShift;                        /*        switch(a->GetMathVarType()) {      	   case MatVHDL_var_DIN  :       	        destType = MatVHDL_var_TEMP;      	        break;       	   case MatVHDL_var_TERM :        	   case MatVHDL_var_TEMP :       	   default :       	        destType = MatVHDL_var_TERM;        }        */                if (DestIn!=NULL) {           destVar = DestIn;        } else {           destType = MatVHDL_var_TERM;           destName = GetNextTempName();           termNum  = GetCurTemp();                   destVar = new MathVHDL_var(destName,VHDL_var_std_logic_vector,destSize-1,0,                                      destType,destShift,1,termNum,                                       0,aIn->GetLabel(),aIn->GetAttachment());        }            	/////////////////////////////////////////////////        // Generate +/- Command        // -A-B = -(A+B);        // -A+B = B-A;    	if ((aIn->GetSign()>0) && (bIn->GetSign()>0)) {    	   cmd = CMD_pA_pB;    	   //cout << "CMD_pA_pB\r\n";    	}     	if ((aIn->GetSign()<0) && (bIn->GetSign()<0)) {    	   cmd = CMD_pA_pB;    	   destVar->MultMinus1();    	   //cout << "CMD_mA_mB ---------------------- \r\n";    	}    	if ((aIn->GetSign()>0) && (bIn->GetSign()<0)) {    	   cmd = CMD_pA_mB;    	   //cout << "CMD_pA_mB\r\n";    	}    	if ((aIn->GetSign()<0) && (bIn->GetSign()>0)) {    	   cmd = CMD_pA_mB;    	   destVar->MultMinus1();    	   //cout << "CMD_pA_mB\r\n";    	}            	////////////////////////////////////////////////    	// Execute Code                // Sort A <-> B        if (aTotalSize > bTotalSize) {           a = new MathVHDL_var(aIn);           b = new MathVHDL_var(bIn);        } else {           // a+b = b+a	           a = new MathVHDL_var(bIn);           b = new MathVHDL_var(aIn);           // a-b = (-1)*(b-a)           if (cmd == CMD_pA_mB) destVar->MultMinus1();         }               /*         // Extract parameters	    aName  = a->GetVarName();        aShift = a->GetShift();	    aSize  = a->GetVarBitSize();        aTotalSize = aSize + aShift;	    	    bName  = b->GetVarName();        bShift = b->GetShift();	    bSize  = b->GetVarBitSize();	            bTotalSize = bSize + bShift;*/                code = "";        if ( (cmd == CMD_pA_mB) &&             ( (a->GetVarName()) == (b->GetVarName()) ) &&             ( (a->GetShift()-1) == (b->GetShift())   ) ) {           cout << "CMD_pA_mB\r\n";                      if (DestIn==NULL) {              destVar->SetVarRange(a->GetRangeFrom(),a->GetRangeTo()); // update variable range (decrement)              m_varList.push_back(destVar);              AddBaseVariable(destVar);           }                     code = destVar->GetVarName()+" <= "+a->GetVarName()+";\r\n";           cout << "Shift distance=1 :" << code;           cout << "DestVarBitSize:" << destVar->GetVarBitSize() << "\r\n";                   } else {           //tempVar = AddVariable(destVar,0);           if (DestIn==NULL) {               m_varList.push_back(destVar);               AddBaseVariable(destVar);           }        	           //destVar = AddVariable(destVar,0);           if (cmd == CMD_pA_pB) {              //cout << "CMD_pA_pB\r\n";           	  code = codeGen_A_pm_B(a,b,destVar,1);           	  //cout << code;           } else {              //cout << "CMD_pA_mB\r\n";           	  code = codeGen_A_pm_B(a,b,destVar,(-1));           	  //cout << code;           }         /*           if (a->GetSign()>0) cout << "a+ ";                          else cout << "a- ";           if (b->GetSign()>0) cout << "b+ ";                          else cout << "b- ";                                     if (destVar->GetSign()>0) cout << "dest+ ";                                else cout << "dest- ";                                     */                       }            }    if (cmd==CMD_GEN_OUT) {        cout << "@@##@@##@@ CMD_GEN_OUT  ##@@##@@##@@##\r\n";        /////////////////////////////////////////////////        // Create Variable        destSize = aTotalSize;            // Set Output Size to maximum        if (aIn->GetSign()<0) destSize++; // increment to avoid overlap                termNum  = GetCurTemp();        destType = MatVHDL_var_TEMP;        destName = GetNextTempName();        destVar = AddVariable(destName,VHDL_var_std_logic_vector,destSize-1,0,                              destType,destShift,1,termNum,                              0,aIn->GetLabel());                                      code = codeGen_finalizeTerm(aIn,destVar);                          }    if (cmd==CMD_EQUAL) {        cout << "@@##@@##@@ CMD_EQUAL  ##@@##@@##@@##\r\n";    	destVar = DestIn;    	if (destVar==NULL) {    		destSize = aSize;//aTotalSize - aShift;            termNum  = GetCurTemp();            destType = MatVHDL_var_TEMP;            destName = GetNextTempName();            destShift = aShift;            destSign  = aSign;            destVar = AddVariable(destName,VHDL_var_std_logic_vector,destSize-1,0,                                  destType,destShift,destSign,termNum,                                  0,aIn->GetLabel());    	}    	code = destVar->GetVarName() + " <= " + aIn->GetVarName() + ";";    }    if (cmd==CMD_BLANK) {        cout << "@@##@@##@@ CMD_BLANK  ##@@##@@##@@##\r\n";            	code = "-- Optimized:";    	if (DestIn!=NULL) code+=DestIn->GetVarName();    	destVar = DestIn;    }    if (destVar->GetSign()>0) code = code + "-- Sign + \r\n";    if (destVar->GetSign()<0) code = code + "-- Sign - \r\n";    ret->dest = destVar;    ret->code = code;        return(ret);}//////////////////////////////////////////////////////////////////////////////////////////////////////                                   Finilize Output                                              //// this function get the finnal result and executes final sign conversion and Shift operations    //////////////////////////////////////////////////////////////////////////////////////////////////////MathVHDL_var* GenMathVHDL::Gen_FinalOut(MathVHDL_var* varIn,string outName="",int termNumIn = (-1),int mult_num=1,int inDataWidth=1) {  MathVHDL_var* destVar;  string code;  char myChar[200],ch;  int i;  int termNum;  int destType;  string destName;  int destBitSize;    int CoefBitSize,TotalBitSize;  int temp;  int shift;  int UpLimit;    ostringstream str_s;      ch = '"';  code.clear();    destBitSize = varIn->GetVarBitSize();  shift = varIn->GetShift();  destBitSize += shift;  // Calculate target bit size  temp = mult_num;  CoefBitSize = 0;    while (temp!=0) {  	temp = temp/2;  	CoefBitSize++;  }    cout << "Multiplier:" << mult_num << " CoefBitSize:" << CoefBitSize << "\r\n";  TotalBitSize = CoefBitSize + inDataWidth + 1;    if (destBitSize<TotalBitSize) TotalBitSize = destBitSize; // in case of 0,1 or special    //////////////////////////////////  // SHIFT  UpLimit = TotalBitSize - shift;  str_s.clear();  str_s << "(" << varIn->GetVarName() + "(" << (UpLimit-1) << " downto 0)";  if (shift>0) {    str_s << " & \"";    code += ch;    for (i=0;i<shift;i++) {      str_s << "0";    }    str_s << "\"";  }  str_s << ")";  code = str_s.str();  ////////////////////////////////  // Negate  if ((varIn->GetSign())<0) {    printf("negative\r\n");    code = "( (not("+code+")) + 1)";    //destBitSize++;  }  ///////////////////////////////  // Generate Variable Name  if (!outName.compare("")) {    termNum  = GetCurTemp();    destType = MatVHDL_var_TEMP;    destName = GetNextTempName();  } else {    destType = MatVHDL_var_TERM;    termNum  = termNumIn;    //sprintf(myChar,"%s%d",outName.c_str(),termNum);    //destName = myChar;    destName = outName;  }  destVar = new MathVHDL_var(destName,destType,TotalBitSize-1,0,			     varIn->GetMathVarType(),0,1,termNum,			     varIn->GetPortType(),varIn->GetLabel(),varIn->GetAttachment());    destVar = AddVariable(destVar,0);  code = destName + " <= " + code + ";";  if ((varIn->GetSign())>0) {    sprintf(myChar,"Gen_pA_mB : %s = %s<<%d",destName.c_str(),(varIn->GetVarName()).c_str(),varIn->GetShift());  } else {    sprintf(myChar,"Gen_pA_mB : %s = -%s<<%d",destName.c_str(),(varIn->GetVarName()).c_str(),varIn->GetShift());  }  AddCodeComments(myChar);  AddVHDLCode(code);  return(destVar);}/////////////////////////////////////////////////////////////////////////////////////                               Resample Array                                  /////////////////////////////////////////////////////////////////////////////////////vector<MathVHDL_var*> GenMathVHDL::ResampleArray(vector<MathVHDL_var*> varIn,int clk_en) {  vector<string> code;  vector<MathVHDL_var*> varOut;  MathVHDL_var* tempVar;  int i;  char myChar[200];  code.clear();  varOut.clear();  for (i=0;i<varIn.size();i++) {    tempVar = new MathVHDL_var(varIn[i],1);    tempVar = AddVariable(tempVar,0);    varOut.push_back(tempVar);    sprintf(myChar,"%s <= %s;",(varOut[i]->GetVarName()).c_str(),(varIn[i]->GetVarName()).c_str() );    code.push_back(myChar);  }  AddVHDLCodeClk(code,clk_en);  return(varOut);}///////////////////////////////////////////////////////////////////                     Copy to new Array                       ///////////////////////////////////////////////////////////////////// this function creates new array by specidied name           //// and copies existing array to new one                        //// return new array                                            ///////////////////////////////////////////////////////////////////vector<MathVHDL_var*> GenMathVHDL::CopyToNewArray(vector<MathVHDL_var*> varIn,string newName,int pipeStage=0) {  vector<string> code;  vector<MathVHDL_var*> varOut;  MathVHDL_var* tempVar;  int i,j;  char myChar[200];  string varName;  code.clear();  varOut.clear();  for (i=0;i<varIn.size();i++) {        sprintf(myChar,"%s%d",newName.c_str(),i);     varName = myChar;    for (j=0;j<pipeStage;j++) varName += "_d";        tempVar = new MathVHDL_var(varName,varIn[i]->GetVarType(),varIn[i]->GetRangeFrom(),varIn[i]->GetRangeTo(),			       varIn[i]->GetMathVarType(),varIn[i]->GetShift(),varIn[i]->GetSign(),varIn[i]->GetTermNum(),			       varIn[i]->GetPortType(),varIn[i]->GetLabel(),varIn[i]->GetAttachment());     tempVar = AddVariable(tempVar,0);    varOut.push_back(tempVar);    sprintf(myChar,"%s <= %s;",(varOut[i]->GetVarName()).c_str(),(varIn[i]->GetVarName()).c_str() );    code.push_back(myChar);  }  AddVHDLCode(code);  return(varOut);}///////////////////////////////////////////////////////////////////                  Generate Variable List                     ///////////////////////////////////////////////////////////////////vector<string> GenMathVHDL::VHDL_GenVar(vector<MathVHDL_var*> varList) {  vector<string> str;  string s;  int i;  str.clear();  printf("GenMathVar varSize=%d\r\n",varList.size());  for (i=0;i<varList.size();i++) {    //printf(".");    s =  "  Signal ";    s += varList[i]->GetVarName();    s += " : ";    s += varList[i]->GetVarTypeStr();    s += ";";    str.push_back(s);  }   return(str);}/*GenMathVHDL::~GenMathVHDL() {  vector<string> str;  str = VHDL_GenVar(m_varList);  AddVHDLCode(str);  }*/  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -