Math2mat

/home/ythoma/docs/math2mat/svn/wp1/framework/m2mGUI/src/m2m/backend/vhdl/VHDLCreator.java

Go to the documentation of this file.
00001 /******************************************************************************
00002  *                                                                                      VHDLCreator
00003  ******************************************************************************
00004  * Auteur               : Daniel Molla
00005  * Date                 : 11 avr. 2011
00006  * Description :
00007  ******************************************************************************/
00008 package m2m.backend.vhdl;
00009 
00010 import m2m.backend.buildingblocks.BuildingBlock;
00011 import m2m.backend.buildingblocks.BuildingBlock.NumType;
00012 
00013 import java.io.File;
00014 import java.io.FileWriter;
00015 import java.io.IOException;
00016 import java.util.HashMap;
00017 import java.util.Vector;
00018 import java.util.ArrayList;
00019 import java.util.Date;
00020 
00021 import m2m.backend.project.M2MProject;
00022 import m2m.backend.project.ProjectRef;
00023 import m2m.backend.structure.*;
00024 
00032 public class VHDLCreator extends ProjectRef 
00033 {               
00036         private class MultiUseSignals 
00037         {               
00041                 private Vector<SimpleVariable> variables;
00045                 private Vector<Vector<Integer>> cost;
00046 
00047                 
00053                 public MultiUseSignals(Vector<SimpleVariable> vectorVar, Vector<Vector<Integer>> vectorCost) 
00054                 {
00055                         this.variables = vectorVar;
00056                         this.cost = vectorCost;
00057                 }
00058                 
00059                 
00064                 public void setNbrMultSimpleVariable(Function f, SimpleVariable e) 
00065                 {
00066                         Vector<Integer> costs = cost.elementAt(variables.indexOf(e));
00067                         SimpleVariable var;
00068                         
00069                         for(int i = 0; i < costs.size(); i++) {         
00070                                 var = new SimpleVariable(e.getName() + "_" + (i));
00071                                 var.setCost(costs.elementAt(i));
00072                                 var.setType("mult");
00073                                 e.getVariableMap().get(f).getMultSimpleVar().add(var);  
00074                                 
00075                                 if(f instanceof LoopFor && costs.elementAt(i) == f.getMaxCost()) {
00076                                         var.setSimpleFifo(true);
00077                                         var.setSimpleFifoCost(f.getMaxCost()- getMinCost(variables.indexOf(e)));
00078                                         
00079                                         SimpleVariable loopVar = ((LoopFor)f).getLoopVariable(e);
00080                                         if(loopVar != null)
00081                                                 var.setOutputLoopVariable(loopVar);
00082                                         else
00083                                                 var.setInputLoopVariable(e);
00084                                 }
00085                         }
00086                         
00087                         e.setSimpleFifo(false);
00088                 }
00089                 
00094                 public int getMinCost(int index)
00095                 {
00096                         int minCost = cost.elementAt(index).elementAt(0);
00097                         for(Integer val : cost.elementAt(index))
00098                                 if(minCost > val)
00099                                         minCost = val;
00100                         
00101                         return minCost;
00102                 }
00103                 
00108                 public int getMinCost(SimpleVariable var) 
00109                 {
00110                         return getMinCost(variables.indexOf(var));
00111                 }
00112         }
00113         
00114         
00118         private Function top;
00122         private HashMap<Function, MultiUseSignals> multUseSignal;
00126         private NumType numType;
00127         
00128 
00129 //      /**
00130 //       * Significant size
00131 //       */
00132 //      private int SIGNSIZE;
00133 //      /**
00134 //       * Exponent size
00135 //       */
00136 //      private int EXPSIZE;
00137         
00138         
00146         public VHDLCreator(Function top, M2MProject project) 
00147         {
00148                 this.top = top;
00149                 setProject(project);
00150                 multUseSignal = new HashMap<Function, MultiUseSignals>();
00151                 findMultiUseSignals(top);
00152                 numType = project.getOptimisationProperties().getOptimisationDataType();
00153 //              SIGNSIZE = 23;
00154 //              EXPSIZE = 8;
00155 //              this.vectorTypeList = new ArrayList<Integer>();
00156         }
00157 
00158         
00166         public VHDLCreator(Function top, M2MProject project ,int significand, int exp) {
00167                 this.top = top;
00168                 setProject(project);
00169                 multUseSignal = new HashMap<Function, MultiUseSignals>();
00170                 findMultiUseSignals(top);
00171                 numType = project.getOptimisationProperties().getOptimisationDataType();
00172 //              SIGNSIZE = significand;
00173 //              EXPSIZE = exp;
00174 //              this.vectorTypeList = new ArrayList<Integer>();
00175         }
00176         
00177         
00178         
00182         private void parse(Function f)
00183         {
00184                 /* Go throught all internal SimpleVariable */
00185                 for(Element e : f.getInternalVars()) {
00186                         if(e instanceof SimpleVariable && multUseSignal.get(f).variables.contains((SimpleVariable) e)) {
00187                                 ((SimpleVariable) e).getVariableMap().put(f, ((SimpleVariable) e).new MapVHDLSimpleVariable());
00188                                 multUseSignal.get(f).setNbrMultSimpleVariable(f, (SimpleVariable) e);   
00189                         }
00190                 }
00191                 
00192                 parse(f.getBody(), f);
00193         }
00194 
00195         
00201         private void parse(Vector<Element> elements, Function parentFunction)
00202         {
00203                 for(Element e : elements) {
00204                         if(e instanceof Operation)
00205                                 parse((Operation)e, parentFunction);
00206                         else if(e instanceof IfThenElse)
00207                                 parse((IfThenElse)e, parentFunction);
00208                         else if (e instanceof Function)
00209                                 parse((Function)e);
00210                 }                       
00211         }
00212         
00213         
00219         private void parse(Operation op, Function parentFunction)
00220         {
00221                 int cost;
00222                 int minCost = 0;
00223                 Vector<SimpleVariable> inputs = new Vector<SimpleVariable>();
00224                 Vector<Boolean> dependFlag = new Vector<Boolean>();
00225                 if(op instanceof Comparison)
00226                         ((SimpleVariable) op.getOutputAt(0)).setType("resultComp");
00227                                 
00228                 /* Initialisation of inputs */
00229                 for(int i = 0; i < op.getInput().size(); i++)
00230                         inputs.add((SimpleVariable) op.getInputAt(i));
00231                 if(op instanceof Multiplexer)
00232                         inputs.add((SimpleVariable)((Multiplexer)op).getSel());
00233                 
00234                 /* Check if there is common variables */
00235                 for(int i = 0; i < inputs.size(); i++) {
00236                         dependFlag.add(false);
00237                         for(int j = 0; j < inputs.size(); j++) {
00238                                 if(j != i) {
00239                                         for(Element e : inputs.elementAt(j).getVariableDependencies()) {
00240                                                 if(inputs.elementAt(i).getVariableDependencies().contains(e))
00241                                                         dependFlag.setElementAt(true, i);
00242                                         }
00243                                 }
00244                         }
00245                 }
00246                 
00247                 MultiUseSignals mus = multUseSignal.get(parentFunction);
00248                 if(op.getBlock() != null) {
00249                         cost = ((SimpleVariable)op.getOutputAt(0)).getCost()-op.getBlock().latencyTime()-1;
00250                         for(int i = 0 ; i < inputs.size(); i++) {
00251                                 SimpleVariable var = inputs.elementAt(i);
00252                                 if(mus.variables.contains(inputs.elementAt(i))) {
00253                                         var = inputs.elementAt(i).getVariableMap().get(parentFunction).getMapVHDLSimpleVariable(cost);
00254                                         op.setInputAt(op.getInput().indexOf(inputs.elementAt(i)), var);
00255                                         minCost = mus.getMinCost(mus.variables.indexOf(inputs.elementAt(i)));
00256                                 }
00257                                 for(int j = 0 ; j < inputs.size(); j++) {
00258                                         if(i != j && inputs.elementAt(i).getCost() < inputs.elementAt(j).getCost()) {
00259                                                 if(project.getProperties().getOptimisationProperties().getFifoCompensation() ||  parentFunction instanceof LoopFor || dependFlag.elementAt(i) || (mus.variables.contains(inputs.elementAt(i)) && cost-inputs.elementAt(i).getCost() != minCost))
00260                                                         var.setSimpleFifoCost(cost-inputs.elementAt(i).getCost());      
00261                                         }
00262                                 }
00263                         }
00264                 }
00265                 else if(op instanceof Assignment) {
00266                         cost = ((SimpleVariable)op.getOutputAt(0)).getCost();
00267                         if(op.getInputAt(0) instanceof SimpleVariable && mus.variables.contains((SimpleVariable) op.getInputAt(0))) {
00268                                 SimpleVariable var = ((SimpleVariable)op.getInputAt(0)).getVariableMap().get(parentFunction).getMapVHDLSimpleVariable(cost);
00269                                 op.setInputAt(op.getInput().indexOf(op.getInputAt(0)), var);
00270                         }
00271                 }
00272         }
00273         
00274         
00280         private void parse(IfThenElse ite, Function parentFunction)
00281         {
00282                 if(ite.getName().contains("loopfor")) {
00283                         if(parentFunction.getBody().firstElement().equals(ite)) 
00284                                 parse(ite.getCond(), parentFunction);
00285                 }
00286                 else
00287                         parse(ite.getCond(), parentFunction);
00288                 
00289                 parse(ite.getBodyTrue(), parentFunction);
00290                 parse(ite.getBodyFalse(), parentFunction);
00291         }
00292         
00293         
00299         private void findMultiUseSignals(Function f)
00300         {       
00301                 Vector<SimpleVariable> multSignals = new Vector<SimpleVariable>();
00302                 Vector<Vector<Integer>> costMultSignals = new Vector<Vector<Integer>>();        
00303                 MultiUseSignals multiUseSignals = new MultiUseSignals(multSignals, costMultSignals);
00304                 multUseSignal.put(f, multiUseSignals);
00305                 
00306                 /* Get all variables and operations who are contained into the structure */
00307                 Vector<Operation> operations = findOperations(f.getBody());
00308                 Vector<SimpleVariable> variables = findVariables(operations);                   
00309                 
00310                 /* Set init loop variables as multiUseSignal */
00311                 SimpleVariable var;     
00312                 Vector<SimpleVariable> loopVariables = new Vector<SimpleVariable>();
00313                 if(f instanceof LoopFor) {
00314                         for(Element e : f.getInput()) {
00315                                 var = (SimpleVariable) e;
00316                                 if(!var.getType().equalsIgnoreCase("const")) {                          
00317                                         var.getMultiCost().add(f.getMaxCost());
00318                                         loopVariables.add(var);
00319                                 }
00320                         }
00321                         
00322                         var = (SimpleVariable)f.getInternalVars().elementAt(0);
00323                         if(var.getType().equalsIgnoreCase("iter")) {
00324                                 SimpleVariable startVar =((LoopFor)f).getStart();
00325                                 var.getMultiCost().add(startVar.getType().equalsIgnoreCase("const") ? 0 : startVar.getCost());
00326                                 loopVariables.add(var);
00327                         }
00328                         
00329                         for(Element e : f.getBody()) {
00330                                 if(e instanceof IfThenElse && e.getName().contains("loopfor")) {
00331                                         var = (SimpleVariable) ((IfThenElse)e).getOutput().firstElement();
00332                                         ((SimpleVariable)((Assignment)((IfThenElse)e).getBodyFalse().firstElement()).getInputAt(0)).setInputLoopVariable(var);
00333                                         var.getMultiCost().add(f.getMaxCost());
00334                                         loopVariables.add(var);
00335                                 }
00336                         }
00337                         
00338                         var = (SimpleVariable)((IfThenElse)f.getBody().elementAt(0)).getInternalVars().firstElement();
00339                         var.setSimpleFifo(true);
00340                         var.setSimpleFifoCost(f.getMaxCost()-var.getCost()+1);
00341                         
00342                         variables.addAll(loopVariables);
00343                         ((LoopFor)f).setLoopVariables(loopVariables);
00344                 }
00345                 
00346                 /* Set causal multiUseSignal */
00347                 for(int i = 0; i < variables.size()-1; i++) {
00348                         for(int j = i + 1; j < variables.size(); j++) {
00349                                 if(variables.elementAt(i).equals(variables.elementAt(j)) && !variables.elementAt(i).getType().equalsIgnoreCase("const")) {
00350                                         if(!multSignals.contains(variables.elementAt(i))) {                                     
00351                                                 multSignals.add(variables.elementAt(i));
00352                                                 costMultSignals.add(new Vector<Integer>());
00353                                                 costMultSignals.elementAt(multSignals.indexOf(variables.elementAt(i))).addAll(variables.elementAt(i).getMultiCost());
00354                                                 ((SimpleVariable)variables.elementAt(i)).getMultiCost().clear();
00355                                         }
00356                                         variables.remove(j--);
00357                                 }
00358                         }
00359                 }
00360         }
00361         
00362         
00369         private Vector<Element> findComponent(Vector<Element> elements) throws VHDLException
00370         {
00371                 // The vector of operations to return
00372                 Vector<Element> components = new Vector<Element>();
00373                 
00374                 /* Get all Components */
00375                 for (Element e : elements) {
00376                         if (e instanceof IfThenElse) {
00377                                 components.addAll(findComponent(((IfThenElse)e).getBodyFalse()));
00378                                 components.addAll(findComponent(((IfThenElse)e).getBodyTrue()));
00379                                 components.addAll(findComponent(((IfThenElse)e).getCond()));
00380                         }                       
00381                         else {
00382                                 components.add(e);
00383                         }
00384                 }
00385 
00386                 /* Remove operations that are used more than once */
00387                 for (int i = 0; i < components.size() - 1; i++) {
00388                         for (int j = i + 1; j < components.size(); j++) {
00389                                 if ( (components.elementAt(i) instanceof Operation) && (components.elementAt(j) instanceof Operation) &&
00390                                     !(components.elementAt(i) instanceof Assignment) && !(components.elementAt(j) instanceof Assignment)) 
00391                                 {
00392                                         Element op1 = components.elementAt(i);
00393                                         Element op2 = components.elementAt(j);
00394                                         BuildingBlock blockOp1 = ((Operation)op1).getBlock();
00395                                         BuildingBlock blockOp2 = ((Operation)op2).getBlock();
00396                                         
00397                                         if (blockOp1 == null || blockOp2 == null) {
00398                                                 if (blockOp1 == null && !(op1 instanceof Function))
00399                                                         throw new VHDLException("No material description for the operation : " + op1.getName());
00400                                                 if (blockOp2 == null && !(op2 instanceof Function))
00401                                                         throw new VHDLException("No material description for the operation : " + op2.getName());
00402                                         } else if (((Operation)op1).getBlock().entityName().equalsIgnoreCase(((Operation)op2).getBlock().entityName()))
00403                                                         components.remove(j--);
00404                                 }
00405                         }
00406                 }
00407                 
00408                 return components;
00409         }
00410         
00411 
00417         private Vector<Operation> findOperations(Vector<Element> elements)
00418         {
00419                 // The vector of operations to return
00420                 Vector<Operation> operations = new Vector<Operation>();
00421                 
00422                 for (Element e : elements) {
00423                         if (e instanceof Function) {
00424                                 /* Recursive call for ifThenElse element */
00425                                 if (e instanceof IfThenElse) {
00426                                         operations.addAll(findOperations(((IfThenElse)e).getBodyFalse()));
00427                                         operations.addAll(findOperations(((IfThenElse)e).getBodyTrue()));
00428                                         operations.addAll(findOperations(((IfThenElse)e).getCond()));
00429                                 }                       
00430                                 /* Recursive call for LoopFor element */
00431                                 else if (e instanceof LoopFor) {
00432                                         findMultiUseSignals((Function)e);
00433                                         for(Element var : ((LoopFor)e).getInput()) {
00434                                                 Assignment ass = new Assignment();
00435                                                 ass.addInput(var); 
00436                                                 ass.addOutput(var);
00437                                                 operations.add(ass);
00438                                         }
00439                                 }
00440                         } 
00441                         else if (e instanceof Operation) {
00442                                 operations.add((Operation)e);
00443                         }
00444                 }
00445                 return operations;
00446         }
00447         
00448         
00454         private Vector<SimpleVariable> findVariables(Vector<Operation> operations)
00455         {               
00456                 // The vector of variables to return
00457                 Vector<SimpleVariable> variables = new Vector<SimpleVariable>();
00458                 
00459                 for (Operation op : operations) {
00460                         /* Get the SimpleVariable selection of a Multiplexer */
00461                         if (op instanceof Multiplexer) {
00462                                 if (((Multiplexer)op).getSel() instanceof SimpleVariable) {
00463                                         variables.add((SimpleVariable)((Multiplexer)op).getSel());
00464                                 }
00465                         }
00466                         
00467                         /* Get all SimpleVariable inputs of the operation */
00468                         for (int i = 0; i < op.getNbInput(); i++) {
00469                                 if (op.getInputAt(i) instanceof SimpleVariable) {
00470                                         if(!(((SimpleVariable)op.getInputAt(i)).getType().equals("iter") && variables.contains(((SimpleVariable)op.getInputAt(i))))){
00471                                                 variables.add((SimpleVariable)op.getInputAt(i));
00472                                                 if(op.getBlock() != null)
00473                                                         ((SimpleVariable)op.getInputAt(i)).getMultiCost().add((((SimpleVariable)op.getOutputAt(0)).getCost()-op.getBlock().latencyTime())-1);   
00474                                                 else if(op instanceof Assignment)
00475                                                         ((SimpleVariable)op.getInputAt(i)).getMultiCost().add(((SimpleVariable)op.getOutputAt(0)).getCost());
00476 
00477                                         }
00478                                 }       
00479                         }
00480                 }
00481                 
00482                 return variables;
00483         }
00484 
00485         
00486         public void wrapTop(String vhdlPath) throws VHDLException {
00487                 //create new folder into VHDL source folder for the wrappers
00488                 String wrapperPath = vhdlPath+"wrappers/";
00489                 File wrapDir = new File(wrapperPath);
00490                 wrapDir.mkdir();
00491                 
00492                 String common = new String();
00493                 String sWrapVhdl = new String();
00494                 String sPackVhdl = new String();
00495                 String sGenericWrapVhdl = new String();
00496                 String sGenericPackVhdl = new String();
00497                 String sRecomsVhdl = new String();
00498 
00499                 String description;
00500                 String wrapFileName="wrapper_"+this.top.getName()+".vhd";
00501                 String packFileName="wrapper_"+this.top.getName()+"_pkg.vhd";
00502                 String genericWrapFileName="m2m_generic_wrapper.vhd";
00503                 String genericPackFileName="m2m_generic_wrapper_pkg.vhd";
00504                 File wrapFile = new File(wrapperPath+wrapFileName);
00505                 FileWriter wrapFw;
00506                 File packFile = new File(wrapperPath+packFileName);
00507                 FileWriter packFw;
00508                 File genericWrapFile = new File(wrapperPath+genericWrapFileName);
00509                 FileWriter genericWrapFw;
00510                 File genericPackFile = new File(wrapperPath+genericPackFileName);
00511                 FileWriter genericPackFw;
00512                 File recomsFile = new File(wrapperPath+"wrapper_Recoms_"+this.top.getName()+".vhd");
00513                 FileWriter recomsFw;
00514 
00515                 //create package top
00516                 description="";
00517                 description+="-- Description: This file contains the declaration of 2 constants:\n";
00518                 description+="--                - The number of inputs of the generated core.\n";
00519                 description+="--                - The number of outputs of the generated core.\n";
00520                 description+="--              It also defines generic types that are used by the\n";
00521                 description+="--              specific wrapper.\n";
00522                 sPackVhdl += this.createHeader(packFileName,description);
00523                 
00524                 //create generic package top
00525                 description="";
00526                 description+="-- Description: This file contains the declaration of 2 constants:\n";
00527                 description+="--                - The number of inputs of the generated core.\n";
00528                 description+="--                - The number of outputs of the generated core.\n";
00529                 description+="--              It also defines generic types that are used by the\n";
00530                 description+="--              generic wrapper.\n";
00531                 sGenericPackVhdl += this.createHeader(genericPackFileName,description);
00532                 
00533                 common = "library ieee;\n";
00534                 common += "\tuse ieee.std_logic_1164.all;\n";
00535                 
00536                 sPackVhdl += common;
00537                 sGenericPackVhdl += common;
00538 
00539                 sPackVhdl += "package m2m_"+this.top.getName()+"_inout_definition_pkg is\n\n";
00540                 sGenericPackVhdl += "package m2m_inout_definition_pkg is\n\n";
00541 
00542                 common = "\tconstant M2M_Nb_Inputs_c  : positive := " + this.top.getInput().size() + ";\n";
00543                 common += "\tconstant M2M_Nb_Outputs_c : positive := " + this.top.getOutput().size() + ";\n";
00544                 common += "\ttype M2M_Data is array(natural range <>) of std_logic_vector(" + NumType.getVHDLType(numType) + ";\n";
00545                 common += "\ttype M2M_Control is array(natural range <>) of std_logic;\n";
00546                 common += "\tconstant M2M_Function_Name_c : String := \""+this.top.getName()+"\";\n";
00547                 
00548                 sPackVhdl += common;
00549                 sGenericPackVhdl += common;
00550                 
00551                 sPackVhdl += "\nend package m2m_"+this.top.getName()+"_inout_definition_pkg;";
00552                 sGenericPackVhdl += "\nend package m2m_inout_definition_pkg;";
00553                 
00554                 //create wrapper top
00555                 description="";
00556                 description+="-- Description: This file contains a wrapper for the generated core.\n";
00557                 description+="--              This wrapper can be used if better suited for a\n";
00558                 description+="--              particular application.\n";
00559                 sWrapVhdl += this.createHeader(wrapFileName,description);
00560                 
00561                 //create generic wrapper top
00562                 description="";
00563                 description+="-- Description: This file contains a wrapper for the generated core.\n";
00564                 description+="--              This wrapper is generic, its name being always the same.\n";
00565                 description+="--              It can be used if only one instance of a Math2mat core\n";
00566                 description+="--              is used in a design.\n";
00567                 sGenericWrapVhdl += this.createHeader(genericWrapFileName,description);
00568                 
00569                 //create Recoms wrapper top
00570                 description="";
00571                 description+="-- Description: This file contains a wrapper for the generated core.\n";
00572                 description+="--              This wrapper is specific for Recoms applications,\n";
00573                 description+="--              it names the clock specifically and adds a clock enable signal.\n";
00574                 sRecomsVhdl += this.createHeader(genericWrapFileName,description);
00575                 
00576                 //load libraries
00577                 common = "library ieee;\n";
00578                 common += "\tuse ieee.std_logic_1164.all;\n";
00579 
00580                 sWrapVhdl += common;
00581                 sGenericWrapVhdl += common;
00582                 sRecomsVhdl += common;
00583                 
00584                 sWrapVhdl += "\tuse work.m2m_"+this.top.getName()+"_inout_definition_pkg.all;\n";
00585                 sGenericWrapVhdl += "\tuse work.m2m_inout_definition_pkg.all;\n";
00586                 //sRecomsVhdl += "\tuse work.m2m_"+this.top.getName()+"_inout_definition_pkg.all;\n";
00587                 
00588                 //create entity
00589                 sWrapVhdl += "\nentity wrapper_" + top.getName() + " is\n";
00590                 sGenericWrapVhdl += "\nentity m2m_generic_wrapper is\n";
00591                 sRecomsVhdl += "\nentity wrapper_Recoms_" + top.getName() + " is\n";
00592                 
00593                 common = "\tport( Clock_i          : in std_logic;\n";
00594                 common += "\t      Reset_i        : in std_logic;\n";
00595                 common += "\t      Input_i        : in M2M_Data(M2M_Nb_Inputs_c-1 downto 0);\n";
00596                 common += "\t      Input_Valid_i  : in M2M_Control(M2M_Nb_Inputs_c-1 downto 0);\n";
00597                 common += "\t      Input_Ready_o  : out M2M_Control(M2M_Nb_Inputs_c-1 downto 0);\n";
00598                 common += "\t      Result_o       : out M2M_Data(M2M_Nb_Outputs_c-1 downto 0);\n";
00599                 common += "\t      Result_Valid_o : out M2M_Control(M2M_Nb_Outputs_c-1 downto 0);\n";
00600                 common += "\t      Result_Ready_i : in M2M_Control(M2M_Nb_Outputs_c-1 downto 0)\n";
00601                 common += "\t     );\n";
00602                 
00603                 sWrapVhdl += common;
00604                 sGenericWrapVhdl += common;
00605                 
00606                 sRecomsVhdl += "\tport( m2m_clk         : in std_logic;\n";
00607                 sRecomsVhdl += "\t      Reset_i         : in std_logic;\n";
00608                 for (int i = 0; i < this.top.getInput().size(); i++) {
00609                         sRecomsVhdl += "\t      Input" + (i+1) + "_i        : in std_logic_vector(" + NumType.getVHDLType(numType) + ";\n";
00610                         sRecomsVhdl += "\t      Input" + (i+1) + "_Valid_i  : in std_logic;\n";
00611                         sRecomsVhdl += "\t      Input" + (i+1) + "_Ready_o  : out std_logic;\n";
00612                 }
00613                 for (int i = 0; i < this.top.getOutput().size(); i++) {
00614                         sRecomsVhdl += "\t      Result" + (i+1) + "_o       : out std_logic_vector(" + NumType.getVHDLType(numType) + ";\n";
00615                         sRecomsVhdl += "\t      Result" + (i+1) + "_Valid_o : out std_logic;\n";
00616                         sRecomsVhdl += "\t      Result" + (i+1) + "_Ready_i : in std_logic";
00617                         sRecomsVhdl += i == this.top.getOutput().size()-1 ? ";\n" : "\n";
00618                 }
00619                 sRecomsVhdl += "\t      m2m_ce          : in std_logic\n";
00620                 sRecomsVhdl += "\t     );\n";
00621                 
00622                 sWrapVhdl += "end wrapper_" + top.getName() + ";\n";
00623                 sGenericWrapVhdl += "end m2m_generic_wrapper;\n";
00624                 sRecomsVhdl += "end wrapper_Recoms_" + top.getName() + ";\n";
00625                 
00626                 //create architecture
00627                 sWrapVhdl += "\narchitecture mapping of wrapper_" + top.getName() + " is\n";
00628                 sGenericWrapVhdl += "\narchitecture mapping of m2m_generic_wrapper is\n";
00629                 sRecomsVhdl += "\narchitecture mapping of wrapper_Recoms_" + top.getName() + " is\n";
00630                 
00631                 common = "\ncomponent " + top.getName() + " is\n";
00632                 common += "\tport(\n";
00633                 common += "\t\tClock_i:\tin std_logic;\n";
00634                 common += "\t\tReset_i:\tin std_logic;\n";
00635                 for (Element el : this.top.getInput()) {
00636                         if (el.getName().isEmpty()) {
00637                                 throw new NoNameException("input");
00638                         }
00639                         common += "\t\t" + el.getName() + "_i :\tin " + NumType.getVHDLType(numType) + ";\n";
00640                         common += "\t\t" + el.getName() + "_Valid_i : \tin std_logic;\n";
00641                         common += "\t\t" + el.getName() + "_Ready_o : \tout std_logic;\n";
00642                 }
00643                 for (int i=0;i<this.top.getOutput().size();i++) {
00644                         Element el=this.top.getOutput().elementAt(i);
00645                         if (el.getName().isEmpty()) {
00646                                 throw new NoNameException("output");
00647                         }
00648                         common += "\t\t" + el.getName() + "_o :\tout " + NumType.getVHDLType(numType) + ";\n";
00649                         common += "\t\t" + el.getName() + "_Ready_i : \tin std_logic;\n";
00650                         common += "\t\t" + el.getName() + "_Valid_o : \tout std_logic";
00651                         if (i!=this.top.getOutput().size()-1)
00652                                 common += ";";
00653                         common += "\n";
00654                 //      common += this.top.getOutput().lastElement().equals(el) ? ";\n" : "\n";
00655                 }
00656                 common += "\t\t" + ");\n" + "\t" + "end component;\n";
00657                 
00658                 common += "\nbegin\n\n";
00659                 
00660                 //map the top
00661                 common += "top : " + top.getName() + "\n";
00662                 
00663                 sWrapVhdl += common;
00664                 sGenericWrapVhdl += common;
00665                 sRecomsVhdl += common;
00666                 
00667                 sWrapVhdl += "\tport map( Clock_i => Clock_i,\n";
00668                 sGenericWrapVhdl += "\tport map( Clock_i => Clock_i,\n";
00669                 sRecomsVhdl += "\tport map( Clock_i => m2m_clk,\n";
00670                 
00671                 common = "\t          Reset_i => Reset_i,\n";
00672                 for (int i = 0; i < this.top.getInput().size(); i++) {
00673                         Element el = this.top.getInput().elementAt(i);
00674                         if (el.getName().isEmpty()) {
00675                                 throw new NoNameException("input");
00676                         }
00677                         common += "\t\t" + el.getName() + "_i => Input_i(" + i + "),\n";
00678                         common += "\t\t" + el.getName() + "_Valid_i => Input_Valid_i(" + i + "),\n";
00679                         common += "\t\t" + el.getName() + "_Ready_o => Input_Ready_o(" + i + "),\n";
00680                 }
00681                 for (int i = 0; i < this.top.getOutput().size(); i++) {
00682                         Element el = this.top.getOutput().elementAt(i);
00683                         if (el.getName().isEmpty()) {
00684                                 throw new NoNameException("output");
00685                         }
00686                         common += "\t\t" + el.getName() + "_o => Result_o(" + i + "),\n";
00687                         common += "\t\t" + el.getName() + "_Valid_o => Result_Valid_o(" + i + "),\n";
00688                         if (i == this.top.getOutput().size() - 1) {
00689                                 common += "\t\t" + el.getName() + "_Ready_i => Result_Ready_i(" + i + ")\n";
00690                         } else {
00691                                 common += "\t\t" + el.getName() + "_Ready_i => Result_Ready_i(" + i + "),\n";
00692                         }
00693                 }
00694                 sWrapVhdl += common;
00695                 sGenericWrapVhdl += common;
00696 
00697                 common = "\t          Reset_i => Reset_i,\n";
00698                 for (int i = 0; i < this.top.getInput().size(); i++) {
00699                         Element el = this.top.getInput().elementAt(i);
00700                         if (el.getName().isEmpty()) {
00701                                 throw new NoNameException("input");
00702                         }
00703                         common += "\t\t" + el.getName() + " => Input" + (i+1) + "_i,\n";
00704                         common += "\t\t" + el.getName() + "_Valid => Input" + (i+1) + "_Valid_i,\n";
00705                         common += "\t\t" + el.getName() + "_Ready => Input" + (i+1) + "_Ready_o,\n";
00706                 }
00707                 for (int i = 0; i < this.top.getOutput().size(); i++) {
00708                         Element el = this.top.getOutput().elementAt(i);
00709                         if (el.getName().isEmpty()) {
00710                                 throw new NoNameException("output");
00711                         }
00712                         common += "\t\t" + el.getName() + " => Result" + (i+1) + "_o,\n";
00713                         common += "\t\t" + el.getName() + "_Valid => Result" + (i+1) + "_Valid_o,\n";
00714                         if (i == this.top.getOutput().size() - 1) {
00715                                 common += "\t\t" + el.getName() + "_Ready => Result" + (i+1) + "_Ready_i\n";
00716                         } else {
00717                                 common += "\t\t" + el.getName() + "_Ready => Result" + (i+1) + "_Ready_i,\n";
00718                         }
00719                 }
00720                 sRecomsVhdl += common;
00721                 
00722                 common = "\t        );\n\n";
00723 
00724                 common += "end mapping;\n";
00725                 
00726                 sWrapVhdl += common;
00727                 sGenericWrapVhdl += common;
00728                 sRecomsVhdl += common;
00729 
00730                 try {
00731                         wrapFw = new FileWriter(wrapFile);
00732                         packFw = new FileWriter(packFile);
00733                         genericWrapFw = new FileWriter(genericWrapFile);
00734                         genericPackFw = new FileWriter(genericPackFile);
00735                         recomsFw = new FileWriter(recomsFile);
00736                         wrapFw.write(sWrapVhdl);
00737                         packFw.write(sPackVhdl);
00738                         genericWrapFw.write(sGenericWrapVhdl);
00739                         genericPackFw.write(sGenericPackVhdl);
00740                         recomsFw.write(sRecomsVhdl);
00741                         wrapFw.close();
00742                         packFw.close();
00743                         genericWrapFw.close();
00744                         genericPackFw.close();
00745                         recomsFw.close();
00746                 } catch (IOException e) {
00747                         e.printStackTrace();
00748                 }
00749         }
00750 
00755         public String createVHDL(String fileName) throws VHDLException {
00756                 parse(top);
00757                 wrapComponents(top.getBody());
00758                 String description="";
00759                 description+="-- Description: This file contains the top level block generated by the\n";
00760                 description+="--              Math2mat tool. The architecture is composed of instantiations of\n";
00761                 description+="--              the different operatores, as well as of the control logic.\n";
00762                 String sVhdl = new String();
00763                 sVhdl += createHeader(fileName,description);
00764                 sVhdl += createFunctionEntity(top, 0);
00765                 sVhdl += "\n\n";
00766                 sVhdl += createFunctionArchitecture(top, 0);
00767                 return sVhdl;
00768         }
00769         
00770 
00781         private String createHeader(String fileName,String description) {
00782                 String sHeader = new String();
00783                 Date curDate= new Date();
00784                 sHeader+="--------------------------------------------------------------------------------\n";
00785                 sHeader+="-- This file was automatically generated by Math2mat\n";
00786                 sHeader+="-- File:   "+fileName+"\n";
00787                 sHeader+="-- Author: Math2mat consortium\n";
00788                 sHeader+="-- Date:   "+curDate.toString()+"\n--\n";
00789                 if (!description.isEmpty())
00790                         sHeader+=description;
00791                 sHeader+="--------------------------------------------------------------------------------\n";
00792                 sHeader+="\n";
00793                 return sHeader;
00794         }
00795 
00796         
00801         public void wrapComponents(Vector<Element> body) throws VHDLException {
00802                 String workFolder = project.getProperties().getProjectPath();
00803                 
00804                 /* Go throught all elements contained into the body */
00805                 for (Element el : body) {
00806                         /* Create a loopFor vhdl file */
00807                         if (el instanceof IfThenElse) {
00808                                 wrapComponents(((IfThenElse)el).getBodyFalse());
00809                                 wrapComponents(((IfThenElse)el).getBodyTrue());
00810                                 wrapComponents(((IfThenElse)el).getCond());
00811                         } else if (el instanceof LoopFor) {
00812                                 LoopFor loop = (LoopFor) el;
00813                                 String fileName = workFolder + project.getVHDLSubFolder()+"/loop_" + loop.getName() + ".vhd";
00814                                 File file = new File(fileName);
00815                                 FileWriter fw;
00816                                 try {
00817                                         fw = new FileWriter(file);
00818                                         String sLoop = new String();
00819                                         sLoop = createLoop(loop, 0);
00820                                         fw.write(sLoop);
00821                                         fw.close();
00822                                 } catch (IOException e) {
00823                                         e.printStackTrace();
00824                                 }
00825                                 wrapComponents(loop.getBody());
00826                                 Vector<Element> addLoopFor = new Vector<Element>();
00827                                 addLoopFor.add(loop.getIterOperation());
00828                                 wrapComponents(addLoopFor);
00829                         } 
00830                         /* Create a wrapper operation vhdl file */
00831                         else if (el instanceof Operation && !(el instanceof Assignment)) {
00832                                 Operation op = (Operation) el;
00833                                 BuildingBlock block = op.getBlock();
00834                                 if (block == null) {
00835                                         throw new VHDLException("No material description for the operation : " +
00836                                           op.getName());
00837                                 }
00838                                 String fileName = workFolder + project.getVHDLSubFolder() + "/wrapper_" + block.entityName() + ".vhd";
00839                                 File file = new File(fileName);
00840                                 FileWriter fw;
00841                                 try {
00842                                         fw = new FileWriter(file);
00843                                         String sVhdl = new String();
00844                                         try {
00845                                                 sVhdl = createWrapper(op);
00846                                         } catch (NoNameException e) {
00847                                                 e.printStackTrace();
00848                                         }
00849                                         fw.write(sVhdl);
00850                                         fw.close();
00851                                 } catch (IOException e) {
00852                                         e.printStackTrace();
00853                                 }
00854                         }
00855                 }
00856         }
00857 
00858         
00865         private String createWrapper(Operation op) throws NoNameException {
00866                 String sOperator = new String();                
00867                 BuildingBlock blockOp = op.getBlock();
00868                 
00869                 /* Generate entity */
00870                 sOperator += "library ieee;\n";
00871                 sOperator += "use ieee.std_logic_1164.all;\n";
00872                 if (blockOp.entityName().isEmpty())
00873                         throw new NoNameException("Operator");
00874                 sOperator += "entity wrapper_" + blockOp.entityName() + " is\n";
00875                 sOperator += "generic (\n";
00876                 if (op instanceof Multiplexer)
00877                         sOperator += "\tD_Sel_Fifo_Size : integer := 2;\n";
00878                 for(int i = 0; i < op.getNbInput(); i++) {
00879                         sOperator += "\tD" + (i+1) + "_Fifo_Size : integer := 2";
00880                         sOperator += i == op.getNbInput()-1 ? "\n);\n" : ";\n" ;
00881                 }
00882                 sOperator += "port(\n";
00883                 sOperator += "\tClock_i:\tin std_logic;\n";
00884                 sOperator += "\tReset_i:\tin std_logic;\n";
00885                 
00886                 /* Generate inputs of the entity */
00887                 for (int i = 0; i < blockOp.nbInputs(); i++) {
00888                         if(op instanceof LogicOperation)
00889                                 sOperator += tab(1) + "D" + (i + 1) + "_i\t: in Std_Logic;\n";
00890                         else
00891                                 sOperator += tab(1) + "D" + (i + 1) + "_i\t: in " + NumType.getVHDLType(numType) + ";\n";
00892                         sOperator += tab(1) + "D" + (i + 1) + "_Valid_i\t: in Std_Logic;\n";
00893                         sOperator += tab(1) + "D" + (i + 1) + "_Ready_o\t: out Std_Logic;\n";
00894                 }
00895                 if (op instanceof Multiplexer) {
00896                         sOperator += tab(1) + "D_Sel_i\t: in Std_Logic;\n";
00897                         sOperator += tab(1) + "D_Sel_Valid_i\t: in Std_Logic;\n";
00898                         sOperator += tab(1) + "D_Sel_Ready_o\t: out Std_Logic;\n";
00899                 }
00900                 
00901                 /* Generate outputs of the entity */
00902                 sOperator += tab(1) + "Ready_i\t: in Std_Logic;\n";
00903                 if (op instanceof Comparison) {
00904                         sOperator += tab(1) + "Smaller_o\t: out Std_Logic;\n";
00905                         sOperator += tab(1) + "Bigger_o\t : out Std_Logic;\n";
00906                         sOperator += tab(1) + "Equal_o\t  : out Std_Logic;\n";
00907                 } else{
00908                         if(op instanceof LogicOperation)
00909                                 sOperator += tab(1) + "Result_o\t: out Std_Logic;\n";
00910                         else
00911                                 sOperator += tab(1) + "Result_o\t: out " + NumType.getVHDLType(numType) + ";\n";
00912                 }
00913                         
00914                 sOperator += tab(1) + "Valid_o\t: out Std_Logic\n";
00915                 sOperator += ");\n" + "end " + "wrapper_" + blockOp.entityName() + ";\n\n";
00916 
00917                 /* Generate Internal signals */
00918                 sOperator += "architecture comp of " + "wrapper_" + blockOp.entityName() + " is\n\n";
00919                 sOperator += tab(1) + "signal Stall_s : std_logic;\n";
00920                 sOperator += tab(1) + "signal Ready_s : std_logic;\n";
00921                 sOperator += tab(1) + "signal Valid_in_s : std_logic;\n";
00922                 sOperator += tab(1) + "signal Op_Ready_s : std_logic;\n";
00923                 sOperator += tab(1) + "signal Valid_s : std_logic;\n";
00924                 sOperator += tab(1) + "signal Valid_Mem_s : std_logic;\n";
00925                 if (op instanceof Comparison) {
00926                         sOperator += tab(1) + "signal Smaller_s : std_logic;\n";
00927                         sOperator += tab(1) + "signal Smaller_Mem_s : std_logic;\n";
00928                         sOperator += tab(1) + "signal Bigger_s : std_logic;\n";
00929                         sOperator += tab(1) + "signal Bigger_Mem_s : std_logic;\n";
00930                         sOperator += tab(1) + "signal Equal_s : std_logic;\n";
00931                         sOperator += tab(1) + "signal Equal_Mem_s : std_logic;\n\n";
00932                 } else {
00933                         if(op instanceof LogicOperation) {
00934                                 sOperator += tab(1) + "signal Result_s : std_logic;\n";
00935                                 sOperator += tab(1) + "signal Result_Mem_s : std_logic;\n\n";
00936                         } else {
00937                                 sOperator += tab(1) + "signal Result_s : " + NumType.getVHDLType(numType) + ";\n";
00938                                 sOperator += tab(1) + "signal Result_Mem_s : " + NumType.getVHDLType(numType) + ";\n\n";
00939                         }
00940                 }
00941                 for (int i = 0; i < blockOp.nbInputs(); i++) {
00942                         if(op instanceof LogicOperation) 
00943                                 sOperator += tab(1) + "signal D"+ (i + 1) + "_s             : std_logic;\n";
00944                         else 
00945                                 sOperator += tab(1) + "signal D"+ (i + 1) + "_s             : " + NumType.getVHDLType(numType) + ";\n";
00946                         sOperator += tab(1) + "signal D"+ (i + 1) + "_fifo_full_s   : std_logic;\n";
00947                         sOperator += tab(1) + "signal D"+ (i + 1) + "_fifo_empty_s  : std_logic;\n";
00948                         sOperator += tab(1) + "signal D"+ (i + 1) + "_Ready_s       : std_logic;\n";
00949                         sOperator += tab(1) + "signal D"+ (i + 1) + "_Valid_s       : std_logic;\n";
00950                 }
00951                 if(op instanceof Multiplexer) {
00952                         sOperator += tab(1) + "signal Sel_s            : std_logic;\n";
00953                         sOperator += tab(1) + "signal Sel_fifo_full_s  : std_logic;\n";
00954                         sOperator += tab(1) + "signal Sel_fifo_empty_s : std_logic;\n";
00955                         sOperator += tab(1) + "signal Sel_Ready_s      : std_logic;\n";
00956                         sOperator += tab(1) + "signal Sel_Valid_s      : std_logic;\n"; 
00957                 }
00958                 sOperator += "\n";
00959                 
00960                 /* Generate component to wrap */
00961                 sOperator += tab(1) + "component " + blockOp.entityName() + " is\n";
00962                 sOperator += tab(1) + "generic (\n";
00963                 sOperator += tab(2) + "wValid_g : integer := 1\n";
00964                 sOperator += tab(1) + ");\n";
00965                 sOperator += tab(1) + "port (\n";
00966                 sOperator += tab(2) + "clk_i   : in std_logic;\n";
00967                 sOperator += tab(2) + "reset_i : in std_logic;\n";
00968                 sOperator += tab(2) + "valid_i : in std_logic_vector(wValid_g-1 downto 0);\n";
00969                 sOperator += tab(2) + "stall_i : in std_logic;\n";
00970                 
00971                 for (int i = 0; i < blockOp.nbInputs(); i++) {
00972                         if(op instanceof LogicOperation) 
00973                                 sOperator += tab(2) + "D" + (i + 1) + "_i\t: in Std_Logic;\n";
00974                         else 
00975                                 sOperator += tab(2) + "D" + (i + 1) + "_i\t: in " + NumType.getVHDLType(numType) + ";\n";       
00976                 }
00977                 if (op instanceof Multiplexer) {
00978                         sOperator += tab(2) + "D_Sel_i\t: in Std_Logic;\n";
00979                 }
00980                 if (op instanceof Comparison) {
00981                         sOperator += tab(2) + "smaller_o : out Std_Logic;\n";
00982                         sOperator += tab(2) + "bigger_o  : out Std_Logic;\n";
00983                         sOperator += tab(2) + "equal_o   : out Std_Logic;\n";
00984                 } else {
00985                         if(op instanceof LogicOperation) 
00986                                 sOperator += tab(2) + "m_o     : out Std_Logic;\n";
00987                         else 
00988                                 sOperator += tab(2) + "m_o     : out " + NumType.getVHDLType(numType) + ";\n";  
00989                 }
00990                 sOperator += tab(2) + "valid_o : out std_logic_vector(wValid_g-1 downto 0);\n";
00991                 sOperator += tab(2) + "ready_o : out std_logic\n";
00992                 sOperator += tab(1) + ");\n";
00993                 sOperator += tab(1) + "end component;\n\n";
00994                 
00995                 /* Declare fifo component */
00996                 sOperator += tab(1) + "component fifo_Std is\n";
00997                 sOperator += tab(1) + "generic (\n";
00998                 sOperator += tab(2) + "FIFOSIZE  : integer := " + NumType.getDataSize(numType) + ";\n";
00999                 sOperator += tab(2) + "DATAWIDTH : integer := " + NumType.getDataSize(numType) + "\n";
01000                 sOperator += tab(1) + ");\n";
01001                 sOperator += tab(2) + "port (\n";
01002                 sOperator += tab(2) + "Clock_i      : in Std_Logic;\n";
01003                 sOperator += tab(2) + "Reset_i      : in Std_Logic;\n";
01004                 sOperator += tab(2) + "Wr_Req_i     : in Std_Logic;\n";
01005                 sOperator += tab(2) + "Rd_Req_i     : in Std_Logic;\n";
01006                 sOperator += tab(2) + "Data_i       : in Std_Logic_Vector(DATAWIDTH-1 downto 0);\n";
01007                 sOperator += tab(2) + "Data_o       : out Std_Logic_Vector(DATAWIDTH-1 downto 0);\n";
01008                 sOperator += tab(2) + "Fifo_Full_o  : out Std_Logic;\n";
01009                 sOperator += tab(2) + "Fifo_Empty_o : out Std_Logic\n";
01010                 sOperator += tab(1) + ");\n";
01011                 sOperator += tab(1) + "end component;\n\n";
01012                 sOperator += "begin\n\n";
01013                 
01014                 /* Map fifos */
01015                 for(int i = 0; i < blockOp.nbInputs(); i++) {
01016                         sOperator += tab(1) + "Fifo_D" + (i + 1) + " : fifo_Std\n";
01017                         sOperator += tab(1) + "generic map(\n"; 
01018                         sOperator += tab(2) + "FIFOSIZE => D" + (i+1) + "_Fifo_Size,\n";
01019                         if(op instanceof LogicOperation) 
01020                                 sOperator += tab(2) + "DATAWIDTH => 1\n" + tab(1) + ")\n";
01021                         else 
01022                                 sOperator += tab(2) + "DATAWIDTH => " + NumType.getDataSize(numType) + "\n" + tab(1) + ")\n";   
01023                         sOperator += tab(1) + "port map(\n";
01024                         sOperator += tab(2) + "Clock_i      => Clock_i,\n";
01025                         sOperator += tab(2) + "Reset_i      => Reset_i,\n";
01026                         sOperator += tab(2) + "Wr_Req_i     => D" + (i + 1) + "_Valid_i,\n";
01027                         sOperator += tab(2) + "Rd_Req_i     => D" + (i + 1) + "_Ready_s,\n";
01028                         if(op instanceof LogicOperation) {
01029                                 sOperator += tab(2) + "Data_i(0)    => D" + (i + 1) + "_i,\n";
01030                                 sOperator += tab(2) + "Data_o(0)    => D" + (i + 1) + "_s,\n";
01031                         }
01032                         else { 
01033                                 sOperator += tab(2) + "Data_i       => D" + (i + 1) + "_i,\n";
01034                                 sOperator += tab(2) + "Data_o       => D" + (i + 1) + "_s,\n";
01035                         }   
01036                         sOperator += tab(2) + "Fifo_Full_o  => D" + (i + 1) + "_fifo_full_s,\n";
01037                         sOperator += tab(2) + "Fifo_Empty_o => D" + (i + 1) + "_fifo_empty_s\n";
01038                         sOperator += tab(1) + ");\n\n";
01039                 }
01040                 if(op instanceof Multiplexer) {
01041                         sOperator += tab(1) + "Fifo_Sel : fifo_Std\n";
01042                         sOperator += tab(1) + "generic map(\n";
01043                         sOperator += tab(2) + "FIFOSIZE => D_Sel_Fifo_Size,\n";
01044                         sOperator += tab(2) + "DATAWIDTH => 1\n" + tab(1) + ")\n";
01045                         sOperator += tab(1) + "port map(\n";
01046                         sOperator += tab(1) + "Clock_i      => Clock_i,\n";
01047                         sOperator += tab(2) + "Reset_i      => Reset_i,\n";
01048                         sOperator += tab(2) + "Wr_Req_i     => D_Sel_Valid_i,\n";
01049                         sOperator += tab(2) + "Rd_Req_i     => Sel_Ready_s,\n";
01050                         sOperator += tab(2) + "Data_i(0)    => D_Sel_i,\n";
01051                         sOperator += tab(2) + "Data_o(0)    => Sel_s,\n";
01052                         sOperator += tab(2) + "Fifo_Full_o  => Sel_fifo_full_s,\n";
01053                         sOperator += tab(2) + "Fifo_Empty_o => Sel_fifo_empty_s\n";
01054                         sOperator += tab(1) + ");\n\n";
01055                 }
01056                 
01057                 /* Map operator */
01058                 sOperator += tab(1) + "Op : " + blockOp.entityName() + "\n";
01059                 sOperator += tab(1) + "port map (\n";
01060                 sOperator += tab(2) + "clk_i    => Clock_i,\n";
01061                 sOperator += tab(2) + "reset_i    => Reset_i,\n";
01062                 sOperator += tab(2) + "valid_i(0) => Valid_in_s,\n";
01063                 sOperator += tab(2) +       "stall_i    => Stall_s,\n";
01064                 for (int i = 0; i < blockOp.nbInputs(); i++) {
01065                         sOperator += tab(2) + "D" + (i + 1) + "_i       => D" + (i + 1) + "_s,\n";
01066                 }
01067                 if (op instanceof Multiplexer) {
01068                         sOperator += tab(2  ) + "D_Sel_i    => Sel_s,\n";
01069                 }
01070                 /* Generate as many signals as we have outputs */
01071                 if (op instanceof Comparison) {
01072                         sOperator += tab(2) + "smaller_o  => Smaller_s,\n";
01073                         sOperator += tab(2) + "bigger_o   => Bigger_s,\n";
01074                         sOperator += tab(2) + "equal_o    => Equal_s,\n";
01075                 } else
01076                         sOperator += tab(2) + "m_o        => Result_s,\n";
01077                 sOperator += tab(2) + "valid_o(0) => Valid_s,\n";
01078                 sOperator += tab(2) + "ready_o    => Op_Ready_s\n";
01079                 sOperator += tab(1) + ");\n\n";
01080                 
01081                 /* Generate combinatorial logic */
01082                 for(int i = 0; i < blockOp.nbInputs(); i++) {
01083                         sOperator += tab(1) + "D" + (i + 1) + "_Ready_o <= not D" + (i + 1) + "_fifo_full_s;\n";
01084                         sOperator += tab(1) + "D" + (i + 1) + "_Valid_s <= not D" + (i + 1) + "_fifo_empty_s;\n\n";
01085                 }
01086                 if(op instanceof Multiplexer) {
01087                         sOperator += tab(1) + "D_Sel_Ready_o <= not Sel_fifo_full_s;\n";
01088                         sOperator += tab(1) + "Sel_Valid_s <= not Sel_fifo_empty_s;\n\n";
01089                 }
01090                 
01091                 sOperator += tab(1) + "Stall_s <= not Ready_s;\n";
01092                 sOperator += tab(1) + "Valid_in_s <= ";
01093                 if (op instanceof Multiplexer) {
01094                         sOperator += "Sel_Valid_s and ";
01095                 }
01096                 for (int i = 0; i < blockOp.nbInputs(); i++) {
01097                         if (i == 0) {
01098                                 sOperator += "D" + (i + 1) + "_Valid_s";
01099                         } else {
01100                                 sOperator += " and D" + (i + 1) + "_Valid_s";
01101                         }
01102                 }
01103                 sOperator += " and Op_Ready_s;\n";
01104                 for (int i = 0; i < blockOp.nbInputs(); i++) {
01105                         sOperator += tab(1) + "D" + (i + 1) + "_Ready_s <= not D" + (i + 1) + "_Valid_s or (";
01106                         for (int j = 0; j < blockOp.nbInputs(); j++) {
01107                                 if (j == 0) {
01108                                         sOperator += "(D" + (j + 1) + "_Valid_s";
01109                                 } else {
01110                                         sOperator += " and D" + (j + 1) + "_Valid_s";
01111                                 }
01112                         }
01113                         if(op instanceof Multiplexer)
01114                                 sOperator += " and Sel_Valid_s";
01115                         sOperator += ") and Op_Ready_s and (Ready_s or not Valid_s));\n";
01116                 }
01117                 if (op instanceof Multiplexer) {
01118                         sOperator += tab(1) + "Sel_Ready_s <= not Sel_Valid_s or (";
01119                         for (int j = 0; j < blockOp.nbInputs(); j++) {
01120                                 if (j == 0) 
01121                                         sOperator += "(D" + (j + 1) + "_Valid_s";
01122                              else 
01123                                         sOperator += " and D" + (j + 1) + "_Valid_s";
01124                         }
01125                         sOperator += " and Sel_Valid_s) and Op_Ready_s and (Ready_s or not Valid_s));\n";
01126                 }
01127                 
01128                 /* Generate assignement of outputs */
01129                 sOperator += "\n";
01130                 if (op instanceof Comparison) {
01131                         sOperator += tab(1) + "Smaller_o <= Smaller_s when Ready_s = '1' else Smaller_Mem_s;\n";
01132                         sOperator += tab(1) + "Bigger_o  <= Bigger_s when Ready_s = '1' else Bigger_Mem_s;\n";
01133                         sOperator += tab(1) + "Equal_o   <= Equal_s when Ready_s = '1' else Equal_Mem_s;\n";
01134                 } else
01135                         sOperator += tab(1) + "Result_o <= Result_s when Ready_s = '1' else Result_Mem_s;\n";
01136                 sOperator += tab(1) + "Valid_o <= Valid_s when Ready_s = '1' else Valid_Mem_s;\n";
01137                 sOperator += "\n";
01138                 
01139                 /* Generate process */
01140                 sOperator += tab(1) + "process (Clock_i, Reset_i)\n";
01141                 sOperator += tab(1) + "begin\n";
01142                 sOperator += tab(2) + "if (Reset_i = '1') then\n";
01143                 if (op instanceof Comparison) {
01144                         sOperator += tab(3) + "Smaller_Mem_s <= '0';\n";
01145                         sOperator += tab(3) + "Bigger_Mem_s  <= '0';\n";
01146                         sOperator += tab(3) + "Equal_Mem_s   <= '0';\n";
01147                 } else {
01148                         if(op instanceof LogicOperation) 
01149                                 sOperator += tab(3) + "Result_Mem_s <= '0';\n";
01150                         else 
01151                                 sOperator += tab(3) + "Result_Mem_s <= (others => '0');\n";
01152                 }
01153                 sOperator += tab(3) + "Valid_Mem_s <= '0';\n";
01154                 sOperator += tab(3) + "Ready_s <= '1';\n";
01155                 sOperator += tab(2) + "elsif Rising_Edge(Clock_i) then\n";
01156                 sOperator += tab(3) + "if (Ready_s = '1') then\n";
01157                 if (op instanceof Comparison) {
01158                         sOperator += tab(4) + "Smaller_Mem_s <= Smaller_s;\n";
01159                         sOperator += tab(4) + "Bigger_Mem_s  <= Bigger_s;\n";
01160                         sOperator += tab(4) + "Equal_Mem_s   <= Equal_s;\n";
01161                 } else
01162                         sOperator += tab(4) + "Result_Mem_s <= Result_s;\n";
01163                 sOperator += tab(4) + "Valid_Mem_s <= Valid_s;\n";
01164                 sOperator += tab(3) + "end if;\n";
01165                 sOperator += tab(3) + "Ready_s <= Ready_i;\n";
01166                 sOperator += tab(2) + "end if;\n";
01167                 sOperator += tab(1) + "end process;\n\n";
01168                 
01169                 /* Assertion for debug 
01170                 sOperator += tab(1) + "assert ";
01171                 if(op instanceof Multiplexer)
01172                         sOperator += "Sel_fifo_full_s = '1' or ";
01173                 for(int i = 0; i < op.getNbInput(); i++) {
01174                         sOperator += "D" + (i+1) + "_fifo_full_s = '1' ";
01175                         sOperator += i == op.getNbInput()-1 ? "report \"Fifo full\";\n\n" : "or ";
01176                 }
01177                 */
01178                 
01179                 sOperator += "end comp;";
01180 
01181                 return sOperator;
01182         }
01183         
01184         private String createFunctionEntity(Function f, int level) throws VHDLException {
01185                 String sEntity = new String();
01186                 sEntity += tab(level) + "library ieee;\n";
01187                 sEntity += tab(level) + "use ieee.std_logic_1164.all;\n";
01188                 sEntity += tab(level) + "use ieee.std_logic_arith.all;\n";
01189                 sEntity += tab(level) + "use ieee.std_logic_unsigned.all;\n";
01190                 sEntity += tab(level) + "use work.pkg_cell.all;\n";
01191                 if (f.getName().isEmpty()) {
01192                         throw new NoNameException("top function");
01193                 }
01194                 sEntity += tab(level) + "entity " + f.getName() + " is\n";
01195                 sEntity += tab(level) + "port(\n";
01196                 sEntity += tab(level) + "\tClock_i:\tin std_logic;\n";
01197                 sEntity += tab(level) + "\tReset_i:\tin std_logic;\n";
01198                 for (Element el : f.getInput()) {
01199                         if (el.getName().isEmpty()) {
01200                                 throw new NoNameException("input");
01201                         }
01202                         sEntity += tab(level + 1) + el.getName() + "_i :\tin " + NumType.getVHDLType(numType) + ";\n";
01203                         sEntity += tab(level + 1) + el.getName() + "_Valid_i : \tin std_logic;\n";
01204                         sEntity += tab(level + 1) + el.getName() + "_Ready_o : \tout std_logic;\n";
01205                 }
01206                 for (int i = 0; i < f.getOutput().size(); i++) {
01207                         Element el = f.getOutput().elementAt(i);
01208                         if (el.getName().isEmpty()) {
01209                                 throw new NoNameException("output");
01210                         }
01211                         sEntity += tab(level + 1) + el.getName() + "_o :\tout " + NumType.getVHDLType(numType) + ";\n";
01212                         sEntity += tab(level + 1) + el.getName() + "_Ready_i : \tin std_logic;\n";
01213                         sEntity += tab(level + 1) + el.getName() + "_Valid_o : \tout std_logic";
01214                         if (i != f.getOutput().size()-1)
01215                                 sEntity += ";";
01216                         sEntity += "\n";
01217                 }
01218                 sEntity += tab(level + 1) + ");\n" + tab(level) + "end " + f.getName() + ";";
01219                 return sEntity;
01220         }
01221         
01222         
01230         private String createLoopForEntity(LoopFor loop, int level) throws VHDLException {
01231                 String sLoopForEntity = new String();
01232                 sLoopForEntity += "library ieee;\n";
01233                 sLoopForEntity += "\tuse ieee.std_logic_1164.all;\n";
01234                 sLoopForEntity += "\tuse work.log_pkg.all;\n";          
01235                 sLoopForEntity += "\n" + tab(level) + "entity loop_" + loop.getName() + " is\n";
01236                 sLoopForEntity += tab(level) + "port (\n";
01237                 sLoopForEntity += tab(level + 1) + "Clock_i\t: in Std_Logic;\n";
01238                 sLoopForEntity += tab(level + 1) + "Reset_i\t: in Std_Logic;\n";
01239                 
01240                 /* Instanciation of all inputs */
01241                 for (int i = 0; i < loop.getInput().size(); i++) {
01242                         sLoopForEntity += tab(level + 1) + loop.getInput().elementAt(i).getName() + "_i\t: in " + NumType.getVHDLType(numType) + ";\n";
01243                         sLoopForEntity += tab(level + 1) + loop.getInput().elementAt(i).getName() + "_Valid_i\t: in Std_Logic;\n";
01244                         sLoopForEntity += tab(level + 1) + loop.getInput().elementAt(i).getName() + "_Ready_o\t: out Std_Logic;\n";
01245                 }
01246                 
01247                 /* Instanciation of start signal if it's not a constant */
01248                 if (!loop.getStart().getType().equalsIgnoreCase("const") && !loop.getInput().contains(loop.getStart())) {
01249                         sLoopForEntity += tab(level + 1) + loop.getStart().getName() + "_i\t: in " + NumType.getVHDLType(numType) + ";\n";
01250                         sLoopForEntity += tab(level + 1) + loop.getStart().getName() + "_Valid_i\t: in Std_Logic;\n";
01251                         sLoopForEntity += tab(level + 1) + loop.getStart().getName() + "_Ready_o\t: out Std_Logic;\n";
01252                 }
01253                 
01254                 /* Instanciation of increment signal if it's not a constant */
01255                 if (!loop.getIncr().getType().equalsIgnoreCase("const") && !loop.getInput().contains(loop.getIncr())) {
01256                         sLoopForEntity += tab(level + 1) + loop.getIncr().getName() + "_i\t: in " + NumType.getVHDLType(numType) + ";\n";
01257                         sLoopForEntity += tab(level + 1) + loop.getIncr().getName() + "_Valid_i\t: in Std_Logic;\n";
01258                         sLoopForEntity += tab(level + 1) + loop.getIncr().getName() + "_Ready_o\t: out Std_Logic;\n";
01259                 }
01260                 
01261                 /* Instanciation of end signal if it's not a constant */
01262                 if (!loop.getEnd().getType().equalsIgnoreCase("const") && !loop.getInput().contains(loop.getEnd())) {
01263                         sLoopForEntity += tab(level + 1) + loop.getEnd().getName() + "_i\t: in " + NumType.getVHDLType(numType) + ";\n";
01264                         sLoopForEntity += tab(level + 1) + loop.getEnd().getName() + "_Valid_i\t: in Std_Logic;\n";
01265                         sLoopForEntity += tab(level + 1) + loop.getEnd().getName() + "_Ready_o\t: out Std_Logic;\n";
01266                 }
01267                 
01268                 /* Instanciation of all outputs */
01269                 for (int i = 0; i < loop.getOutput().size(); i++) {
01270                         sLoopForEntity += tab(level + 1) + loop.getOutput().elementAt(i).getName() + "_o\t: out " + NumType.getVHDLType(numType) + ";\n";
01271                         sLoopForEntity += tab(level + 1) + loop.getOutput().elementAt(i).getName() + "_Valid_o\t: out Std_Logic;\n";
01272                         sLoopForEntity += tab(level + 1) + loop.getOutput().elementAt(i).getName() + "_Ready_i\t: in Std_Logic";
01273                         sLoopForEntity += i == loop.getOutput().size()-1 ? "\n" : ";\n";
01274                 }
01275                 sLoopForEntity += tab(level) + ");\n";
01276                 sLoopForEntity += tab(level) + "end loop_" + loop.getName() + ";\n\n";
01277                 
01278                 return sLoopForEntity;
01279         }
01280 
01281         
01282 
01283         private String createFunctionArchitecture(Function f, int level) throws VHDLException {
01284                 String sArchitecture = new String();
01285                 sArchitecture += tab(level) + "architecture comp of " + f.getName() + " is\n";
01286                 sArchitecture += "\n" + this.createComponent(f, level + 1);
01287                 sArchitecture += "\n" + this.createInternals(f, level+1);
01288                 sArchitecture += "\n" + tab(level) + "begin\n";
01289                 sArchitecture += this.createBody(level + 1, f.getBody(), f);
01290                 sArchitecture += "\n" + tab(level) + "end comp;";
01291                 return sArchitecture;
01292         }
01293 
01301         private String createLoopForComponent(LoopFor loop, int level) throws VHDLException {
01302                 String sLoopForComponent = new String();        
01303                 /* Add the fifo component */
01304                 String fifoType = project.getOptimisationProperties().getFifoType();
01305                 if (fifoType.equalsIgnoreCase("Altera"))
01306                         sLoopForComponent += tab(level) + "component fifo_altera is\n";
01307                 else if (fifoType.equalsIgnoreCase("Xilinx"))
01308                         sLoopForComponent += tab(level) + "component fifo_Xilinx is\n";
01309                 else
01310                         sLoopForComponent += tab(level) + "component fifo_Std is\n";
01311                 sLoopForComponent += tab(level) + "generic (\n";
01312                 sLoopForComponent += tab(level + 1) + "FIFOSIZE  : integer := " + NumType.getDataSize(numType) + ";\n";
01313                 sLoopForComponent += tab(level + 1) + "DATAWIDTH : integer := " + NumType.getDataSize(numType) + "\n";
01314                 sLoopForComponent += tab(level) + ");\n";
01315                 sLoopForComponent += tab(level) + "port (\n";
01316                 sLoopForComponent += tab(level + 1) + "Clock_i      : in Std_Logic;\n";
01317                 sLoopForComponent += tab(level + 1) + "Reset_i      : in Std_Logic;\n";
01318                 sLoopForComponent += tab(level + 1) + "Wr_Req_i     : in Std_Logic;\n";
01319                 sLoopForComponent += tab(level + 1) + "Rd_Req_i     : in Std_Logic;\n";
01320                 sLoopForComponent += tab(level + 1) + "Data_i       : in Std_Logic_Vector(DATAWIDTH-1 downto 0);\n";
01321                 sLoopForComponent += tab(level + 1) + "Data_o       : out Std_Logic_Vector(DATAWIDTH-1 downto 0);\n";
01322                 sLoopForComponent += tab(level + 1) + "Fifo_Full_o  : out Std_Logic;\n";
01323                 sLoopForComponent += tab(level + 1) + "Fifo_Empty_o : out Std_Logic\n";
01324                 sLoopForComponent += tab(level) + ");\n";
01325                 sLoopForComponent += tab(level) + "end component;\n\n";
01326         
01327                 /* Instanciation of init omponent */
01328                 sLoopForComponent += tab(level) + "component Init_Input is\n";
01329                 sLoopForComponent += tab(level) + "generic(\n";
01330                 sLoopForComponent += tab(level) + "\tData_Size_g : integer := " + NumType.getDataSize(numType) + ");\n";
01331                 sLoopForComponent += tab(level) + "port(\n";
01332                 sLoopForComponent += tab(level) + "\tClock_i             : in  std_logic;\n";
01333                 sLoopForComponent += tab(level) + "\tReset_i           : in  std_logic;\n";
01334                 sLoopForComponent += tab(level) + "\tData_i            : in  std_logic_vector(data_Size_g-1 downto 0);\n";
01335                 sLoopForComponent += tab(level) + "\tData_Valid_i      : in  std_logic;\n";
01336                 sLoopForComponent += tab(level) + "\tData_Ready_o      : out std_logic;\n";
01337                 sLoopForComponent += tab(level) + "\tData_Loop_i       : in  std_logic_vector(data_Size_g-1 downto 0);\n";
01338                 sLoopForComponent += tab(level) + "\tData_Loop_Valid_i : in  std_logic;\n";
01339                 sLoopForComponent += tab(level) + "\tData_Loop_Ready_o : out std_logic;\n";
01340                 sLoopForComponent += tab(level) + "\tCond_Loop_i       : in  std_logic;\n";
01341                 sLoopForComponent += tab(level) + "\tData_o            : out std_logic_vector(Data_Size_g-1 downto 0);\n";
01342                 sLoopForComponent += tab(level) + "\tData_Valid_o      : out std_logic;\n";
01343                 sLoopForComponent += tab(level) + "\tData_Ready_i      : in  std_logic\n";      
01344                 sLoopForComponent += tab(level) + ");\n";
01345                 sLoopForComponent += tab(level) + "end component;\n\n";
01346                 
01347                 /* Instanciation of init counter component */
01348                 sLoopForComponent += tab(level) + "component Counter_Wr is\n";
01349                 sLoopForComponent += tab(level) + "generic(\n";
01350                 sLoopForComponent += tab(level) + "\tData_Size_g : integer := " + NumType.getDataSize(numType) + ";\n";
01351                 sLoopForComponent += tab(level) + "\tMem_Size_g  : integer := 1024);\n";
01352                 sLoopForComponent += tab(level) + "port(\n";
01353                 sLoopForComponent += tab(level) + "\tClock_i      : in  std_logic;\n";
01354                 sLoopForComponent += tab(level) + "\tReset_i      : in  std_logic;\n";
01355             sLoopForComponent += tab(level) + "\tData_i       : in  std_logic_vector(ilogup(Mem_Size_g)-1 downto 0);\n";
01356             sLoopForComponent += tab(level) + "\tData_Valid_i : in  std_logic;\n";
01357             sLoopForComponent += tab(level) + "\tReady_i      : in  std_logic;\n";
01358             sLoopForComponent += tab(level) + "\tCond_i       : in  std_logic;\n";
01359             sLoopForComponent += tab(level) + "\tRead_i       : in  std_logic;\n";
01360             sLoopForComponent += tab(level) + "\tAdr_Rd_i     : in  std_logic_vector(ilogup(Mem_Size_g)-1 downto 0);\n";
01361             sLoopForComponent += tab(level) + "\tFifo_Ready_i : in  std_logic;\n";
01362             sLoopForComponent += tab(level) + "\tData_o       : out std_logic_vector(ilogup(Mem_Size_g)-1 downto 0);\n";
01363             sLoopForComponent += tab(level) + "\tData_Valid_o : out std_logic;\n";
01364             sLoopForComponent += tab(level) + "\tReady_o     : out std_logic\n";
01365                 sLoopForComponent += tab(level) + ");\n";
01366                 sLoopForComponent += tab(level) + "end component;\n\n";
01367                 
01368                 /* Instanciation of loop memory omponent */
01369                 sLoopForComponent += tab(level) + "component Memory is\n";
01370                 sLoopForComponent += tab(level) + "generic(\n";
01371                 sLoopForComponent += tab(level) + "\tData_Width_g : integer := " + NumType.getDataSize(numType) + ";\n";
01372                 sLoopForComponent += tab(level) + "\tMem_Size_g   : integer := 128);\n";
01373                 sLoopForComponent += tab(level) + "port(\n";
01374                 sLoopForComponent += tab(level) + "\tClock_i      : in std_logic;\n";
01375                 sLoopForComponent += tab(level) + "\tReset_i      : in std_logic;\n";
01376                 sLoopForComponent += tab(level) + "\tWr_i         : in std_logic;\n";
01377                 sLoopForComponent += tab(level) + "\tRd_i         : in std_logic;\n";
01378                 sLoopForComponent += tab(level) + "\tAdr_Wr_i     : in std_logic_vector(ilogup(Mem_Size_g)-1 downto 0);\n";
01379                 sLoopForComponent += tab(level) + "\tAdr_Rd_i     : in std_logic_vector(ilogup(Mem_Size_g)-1 downto 0);\n";
01380                 sLoopForComponent += tab(level) + "\tAdr_Valid_i  : in std_logic;\n";
01381                 sLoopForComponent += tab(level) + "\tData_i       : in std_logic_vector(Data_Width_g-1 downto 0);\n";
01382                 sLoopForComponent += tab(level) + "\tData_Valid_o : out std_logic;\n";
01383                 sLoopForComponent += tab(level) + "\tData_o       : out std_logic_vector(Data_Width_g-1 downto 0)\n";
01384                 sLoopForComponent += tab(level) + ");\n";
01385                 sLoopForComponent += tab(level) + "end component;\n\n";
01386                 
01387                 /* Instanciation of counter omponent */
01388                 sLoopForComponent += tab(level) + "component Counter_Rd is\n";
01389                 sLoopForComponent += tab(level) + "generic(\n";
01390                 sLoopForComponent += tab(level) + "\tSize_g : integer := " + NumType.getDataSize(numType) + ");\n";
01391                 sLoopForComponent += tab(level) + "port(\n";
01392                 sLoopForComponent += tab(level) + "\tClock_i   : in std_logic;\n";
01393                 sLoopForComponent += tab(level) + "\tReset_i : in std_logic;\n";
01394                 sLoopForComponent += tab(level) + "\tEn_i    : in std_logic;\n";
01395                 sLoopForComponent += tab(level) + "\tReady_i : in std_logic;\n";
01396                 sLoopForComponent += tab(level) + "\tCount_o : out std_logic_vector(ilogup(Size_g)-1 downto 0)\n";
01397                 sLoopForComponent += tab(level) + ");\n";
01398                 sLoopForComponent += tab(level) + "end component;\n\n";
01399                 
01400                 /* Instanciation of read result component */
01401                 sLoopForComponent += tab(level) + "component Read_Result is\n";
01402                 sLoopForComponent += tab(level) + "port(\n";
01403                 sLoopForComponent += tab(level) + "\tClock_i   : in std_logic;\n";
01404                 sLoopForComponent += tab(level) + "\tReset_i : in std_logic;\n";
01405                 sLoopForComponent += tab(level) + "\tData_i  : in " + NumType.getVHDLType(numType) + ";\n";
01406                 sLoopForComponent += tab(level) + "\tValid_i : in std_logic;\n";
01407                 sLoopForComponent += tab(level) + "\tReady_i : in std_logic;\n";
01408                 sLoopForComponent += tab(level) + "\tData_o  : out " + NumType.getVHDLType(numType) + ";\n";
01409                 sLoopForComponent += tab(level) + "\tValid_o : out std_logic;\n";
01410                 sLoopForComponent += tab(level) + "\tReady_o : out std_logic\n";
01411                 sLoopForComponent += tab(level) + ");\n";
01412                 sLoopForComponent += tab(level) + "end component;\n\n";
01413                 
01414                 sLoopForComponent += createComponent(loop, level);      
01415                 
01416                 return sLoopForComponent;
01417         }
01418         
01419         
01420         private String createComponent(Function f, int level) throws VHDLException {
01421                 String sComponent = new String();
01422         
01423                 @SuppressWarnings("unchecked")
01424                 Vector<Element> body = (Vector<Element>)f.getBody().clone();
01425                 if(f instanceof LoopFor)
01426                         body.add(((LoopFor)f).getIterOperation());
01427                 Vector<Element> components = findComponent(body);
01428                 
01429                 for (Element el : components) {
01430                         if (el instanceof Operation && !(el instanceof Assignment)) {
01431                                 Operation op = (Operation) el;
01432                                 BuildingBlock blockOp = op.getBlock();
01433                                 if (blockOp == null) {
01434                                         throw new VHDLException("No material description for the operation : " +
01435                                                           op.getName());
01436                                 } else {
01437                                         sComponent += tab(level) + "component wrapper_" + blockOp.entityName() + " is\n";
01438                                         sComponent += tab(level) + "generic (\n";
01439                                         if (op instanceof Multiplexer)
01440                                                 sComponent += tab(level+1) + "D_Sel_Fifo_Size : integer := 2;\n";
01441                                         for(int i = 0; i < op.getNbInput(); i++) {
01442                                                 sComponent += tab(level+1) +  "D" + (i+1) + "_Fifo_Size : integer := 2";
01443                                                 sComponent += i == op.getNbInput()-1 ? "\n" + tab(level) + ");\n" : ";\n" ;
01444                                         }
01445                                         sComponent += tab(level) + "port (\n";
01446                                         sComponent += tab(level + 1) + "Clock_i    : in Std_Logic;\n";
01447                                         sComponent += tab(level + 1) + "Reset_i    : in Std_Logic;\n";
01448                                         for (int i = 0; i < blockOp.nbInputs(); i++) {
01449                                                 if(op instanceof LogicOperation)
01450                                                         sComponent += tab(level + 1) + "D" + (i + 1) + "_i       : in Std_Logic;\n";
01451                                                 else
01452                                                         sComponent += tab(level + 1) + "D" + (i + 1) + "_i       : in " + NumType.getVHDLType(numType) + ";\n";                         
01453                                                 sComponent += tab(level + 1) + "D" + (i + 1) + "_Valid_i : in Std_Logic;\n";
01454                                                 sComponent += tab(level + 1) + "D" + (i + 1) + "_Ready_o : out Std_Logic;\n";
01455                                         }
01456                                         if(op instanceof Multiplexer){
01457                                                 sComponent += tab(level + 1) + "D_Sel_i       : in Std_Logic;\n";
01458                                                 sComponent += tab(level + 1) + "D_Sel_Valid_i : in Std_Logic;\n";
01459                                                 sComponent += tab(level + 1) + "D_Sel_Ready_o : out Std_Logic;\n";
01460                                         }
01461                                         sComponent += tab(level + 1) + "Ready_i    : in Std_Logic;\n";
01462                                         if (op instanceof Comparison) {
01463                                                 sComponent += tab(level + 1) + "Smaller_o  : out Std_Logic;\n";
01464                                                 sComponent += tab(level + 1) + "Bigger_o   : out Std_Logic;\n";
01465                                                 sComponent += tab(level + 1) + "Equal_o    : out Std_Logic;\n";
01466                                         } else {
01467                                                 if(op instanceof LogicOperation)
01468                                                         sComponent += tab(level + 1) + "Result_o   : out Std_Logic;\n";
01469                                                 else
01470                                                         sComponent += tab(level + 1) + "Result_o   : out " + NumType.getVHDLType(numType) + ";\n";
01471                                         }
01472                                                 
01473                                         sComponent += tab(level + 1) + "Valid_o    : out Std_Logic\n";
01474                                         sComponent += tab(level) + ");\n";
01475                                         sComponent += tab(level) + "end component;\n\n";
01476                                 }
01477                         } 
01478                         else if (el instanceof LoopFor) {
01479                                 LoopFor loop = (LoopFor)el;
01480                                 sComponent += tab(level) + "component loop_" + loop.getName() + " is\n";
01481                                 sComponent += tab(level) + "port (\n";
01482                                 sComponent += tab(level + 1) + "Clock_i\t: in Std_Logic;\n";
01483                                 sComponent += tab(level + 1) + "Reset_i\t: in Std_Logic;\n";
01484                                 for (int i = 0; i < loop.getInput().size(); i++) {
01485                                         if (loop.getInput().elementAt(i) instanceof SimpleVariable) {
01486                                                 sComponent += tab(level + 1) + loop.getInput().elementAt(i).getName() + "_i\t: in " + NumType.getVHDLType(numType) + ";\n";
01487                                                 sComponent += tab(level + 1) + loop.getInput().elementAt(i).getName() + "_Valid_i\t: in Std_Logic;\n";
01488                                                 sComponent += tab(level + 1) + loop.getInput().elementAt(i).getName() + "_Ready_o\t: out Std_Logic;\n";
01489                                         }
01490                                 }
01491                                 if (!loop.getStart().getType().equalsIgnoreCase("const") && !loop.getInput().contains(loop.getStart())) {
01492                                         sComponent += tab(level + 1) + loop.getStart().getName() + "_i\t: in " + NumType.getVHDLType(numType) + ";\n";
01493                                         sComponent += tab(level + 1) + loop.getStart().getName() + "_Valid_i\t: in Std_Logic;\n";
01494                                         sComponent += tab(level + 1) + loop.getStart().getName() + "_Ready_o\t: out Std_Logic;\n";
01495                                 }
01496                                 if (!loop.getIncr().getType().equalsIgnoreCase("const") && !loop.getInput().contains(loop.getIncr())) {
01497                                         sComponent += tab(level + 1) + loop.getIncr().getName() + "_i\t: in " + NumType.getVHDLType(numType) + ";\n";
01498                                         sComponent += tab(level + 1) + loop.getIncr().getName() + "_Valid_i\t: in Std_Logic;\n";
01499                                         sComponent += tab(level + 1) + loop.getIncr().getName() + "_Ready_o\t: out Std_Logic;\n";
01500                                 }
01501                                 if (!loop.getEnd().getType().equalsIgnoreCase("const") && !loop.getInput().contains(loop.getEnd())) {
01502                                         sComponent += tab(level + 1) + loop.getEnd().getName() + "_i\t: in " + NumType.getVHDLType(numType) + ";\n";
01503                                         sComponent += tab(level + 1) + loop.getEnd().getName() + "_Valid_i\t: in Std_Logic;\n";
01504                                         sComponent += tab(level + 1) + loop.getEnd().getName() + "_Ready_o\t: out Std_Logic;\n";
01505                                 }
01506                                 for (int i = 0; i < loop.getOutput().size(); i++) {
01507                                         sComponent += tab(level + 1) + loop.getOutput().elementAt(i).getName() + "_o\t: out " + NumType.getVHDLType(numType) + ";\n";
01508                                         sComponent += tab(level + 1) + loop.getOutput().elementAt(i).getName() + "_Valid_o\t: out Std_Logic;\n";
01509                                         sComponent += tab(level + 1) + loop.getOutput().elementAt(i).getName() + "_Ready_i\t: in Std_Logic";
01510                                         sComponent += i == loop.getOutput().size()-1 ? "\n" : ";\n";
01511                                 }
01512                                 sComponent += tab(level) + ");\n";
01513                                 sComponent += tab(level) + "end component;\n\n";
01514                         }
01515                 }
01516 
01517                 return sComponent;
01518         }
01519         
01520 
01528         private String createInternals(Function f, int level) throws VHDLException {
01529                 String sInternals = new String();
01530 //              sInternals += tab(level) + "type vec_array is array (natural RANGE <>)" + " of " + NumType.getVHDLType(numType) + ";\n";
01531         
01532                 /* Go throught all internal elements of the function */
01533                 for (Element var : f.getInternalVars()) 
01534                 {
01535                         if (var instanceof SimpleVariable) 
01536                         {
01537                                 if (var.getName().isEmpty())
01538                                         throw new NoNameException("Internals");
01539                                 
01540                                 if (((SimpleVariable)var).getType().equals("const"))
01541                                         continue;
01542                                 
01543                                 sInternals += "\n" + tab(level) + "signal " + var.getName() + "_s            : ";
01544                                 if (((SimpleVariable)var).getType().equalsIgnoreCase("cond")) {
01545                                         sInternals += "std_logic;\n";
01546                                 } else if (((SimpleVariable)var).getType().equalsIgnoreCase("resultComp")) {
01547                                         sInternals += "std_logic;\n";
01548                                         sInternals += tab(level) + "signal " + var.getName() + "_smaller_s    : std_logic;\n";          
01549                                         sInternals += tab(level) + "signal " + var.getName() + "_bigger_s     : std_logic;\n";
01550                                         sInternals += tab(level) + "signal " + var.getName() + "_equal_s      : std_logic;\n";
01551                                 } else {
01552                                         sInternals += NumType.getVHDLType(numType) + ";\n";
01553                                 }
01554                                 sInternals += tab(level) + "signal " + var.getName() + "_Valid_s      : std_logic;\n";          
01555                                 sInternals += tab(level) + "signal " + var.getName() + "_Ready_s      : std_logic := '1';\n";
01556                                 
01557                                 if(((SimpleVariable)var).getSimpleFifo())
01558                                         sInternals += createFifoSignals((SimpleVariable)var, level);                            
01559                                 
01560                                 /* Create internals variables of init blocs */
01561                                 if(f instanceof LoopFor && ((LoopFor)f).getLoopVariables().contains(var)) {                             
01562                                         sInternals += tab(level) + "signal " + var.getName() + "_Init_s       : " + NumType.getVHDLType(numType) + ";\n";                               
01563                                         sInternals += tab(level) + "signal " + var.getName() + "_Init_Ready_s : std_logic;\n";
01564                                         sInternals += tab(level) + "signal " + var.getName() + "_Init_Valid_s : std_logic;\n";  
01565                                         sInternals += tab(level) + "signal " + var.getName() + "_Loop_s       : " + NumType.getVHDLType(numType) + ";\n";                               
01566                                         sInternals += tab(level) + "signal " + var.getName() + "_Loop_Ready_s : std_logic;\n";
01567                                         sInternals += tab(level) + "signal " + var.getName() + "_Loop_Valid_s : std_logic;\n";  
01568                                         
01569                                         if(((SimpleVariable)var).getType().equalsIgnoreCase("iter")) {
01570                                                 sInternals += tab(level) + "signal " + var.getName() + "_Add_s        : " + NumType.getVHDLType(numType) + ";\n";                               
01571                                                 sInternals += tab(level) + "signal " + var.getName() + "_Add_Ready_s  : std_logic;\n";
01572                                                 sInternals += tab(level) + "signal " + var.getName() + "_Add_Valid_s  : std_logic;\n";  
01573                                                 
01574                                                 if(f.getMaxCost() > ((SimpleVariable)((LoopFor)f).getIterOperation().getOutputAt(0)).getCost()) {
01575                                                         sInternals += createFifoSignals((SimpleVariable)((LoopFor)f).getIterOperation().getOutputAt(0), level);
01576                                                 }
01577                                         }
01578                                 }
01579                                 
01580                                 /* Create mutli-use temporary variable */
01581                                 if(((SimpleVariable) var).getVariableMap().get(f) != null) {
01582                                         for(SimpleVariable sv : ((SimpleVariable) var).getVariableMap().get(f).getMultSimpleVar())      {
01583                                                 if(multUseSignal.get(f).variables.contains(var)) {
01584                                                         sInternals += tab(level) + "signal " + sv.getName() + "_s       : " + NumType.getVHDLType(numType) + ";\n";                             
01585                                                         sInternals += tab(level) + "signal " + sv.getName() + "_Ready_s : std_logic;\n";
01586                                                         sInternals += tab(level) + "signal " + sv.getName() + "_Valid_s : std_logic;\n";        
01587                                                         
01588                                                         if(sv.getSimpleFifo())
01589                                                                 sInternals += createFifoSignals(sv, level);
01590                                                 }
01591                                         }       
01592                                 }
01593                         } 
01594                         else {
01595                                 throw new BadElementException(var.getName(), "Variable", var.getClass().getName());
01596                         }
01597                 }
01598                 
01599                 if(f instanceof LoopFor) { 
01600                         sInternals += "\n" + tab(level) + "signal Data_Valid_s        : std_logic;\n";                          
01601                         sInternals += tab(level) + "signal Data_Ready_s        : std_logic;\n";
01602                         sInternals += tab(level) + "signal Data_Init_Valid_s   : std_logic;\n";
01603                         sInternals += tab(level) + "signal Data_Loop_Valid_s   : std_logic;\n";
01604                         sInternals += tab(level) + "signal Cond_Loop_s         : std_logic;\n";
01605                         sInternals += tab(level) + "signal Fifo_Ready_s        : std_logic;\n";
01606                         sInternals += tab(level) + "signal n_Fifo_Ready_s        : std_logic;\n";
01607                         sInternals += tab(level) + "signal Counter_Ready_s     : std_logic;\n\n";
01608                         
01609                         sInternals += tab(level) + "signal Loop_Memory_Ready_s      : std_logic;\n";
01610                         sInternals += tab(level) + "signal Loop_Memory_Valid_s      : std_logic;\n";
01611                         sInternals += tab(level) + "signal Loop_Memory_Write_s      : std_logic;\n";
01612                         sInternals += tab(level) + "signal Loop_Memory_Read_s       : std_logic;\n";
01613                         sInternals += tab(level) + "signal Loop_Memory_Adr_Write_s  : std_logic_vector(ilogup(" + f.getRelativeMaxCost() + ")-1 downto 0);\n";
01614                         sInternals += tab(level) + "signal Loop_Memory_Adr_Read_s   : std_logic_vector(ilogup(" + f.getRelativeMaxCost() + ")-1 downto 0);\n\n";
01615                         
01616                         for(Element e : f.getOutput()) {
01617                                 sInternals += tab(level) + "signal Loop_Memory_" + e.getName() + "_Ready_s     : std_logic;\n";
01618                                 sInternals += tab(level) + "signal Loop_Memory_" + e.getName() + "_Valid_s     : std_logic;\n";
01619                                 sInternals += tab(level) + "signal Loop_Memory_" + e.getName() + "_s           : " + NumType.getVHDLType(numType) + ";\n";      
01620                                 sInternals += tab(level) + "signal " + e.getName() + "_Delayed_s               : " + NumType.getVHDLType(numType) + ";\n\n";    
01621                         }
01622                         
01623                         sInternals += tab(level) + "signal Adr_Wr_s              : std_logic_vector(ilogup(" + f.getRelativeMaxCost() + ") -1 downto 0);\n";
01624                         sInternals += tab(level) + "signal Adr_Wr_Valid_s        : std_logic;\n";
01625                         sInternals += tab(level) + "signal Adr_Wr_Ready_s        : std_logic;\n";
01626                         sInternals += tab(level) + "signal Adr_Wr_fifo_s         : std_logic_vector(ilogup(" + f.getRelativeMaxCost() + ") -1 downto 0);\n";
01627                         sInternals += tab(level) + "signal Adr_Wr_fifo_full_s    : std_logic;\n";
01628                         sInternals += tab(level) + "signal Adr_Wr_fifo_empty_s   : std_logic;\n";
01629                         sInternals += tab(level) + "signal Adr_Wr_fifo_write_s   : std_logic;\n";
01630                         sInternals += tab(level) + "signal Adr_Wr_fifo_read_s    : std_logic;\n";
01631                         sInternals += tab(level) + "signal n_Adr_Wr_fifo_empty_s : std_logic;\n\n";
01632                         
01633                 sInternals += tab(level) + "signal Loop_Memory_Adr_Wr_Valid_s              : std_logic_vector(0 downto 0);\n";
01634                     sInternals += tab(level) + "signal Loop_Memory_Adr_Wr_Valid_Valid_s        : std_logic;\n";
01635                     sInternals += tab(level) + "signal Loop_Memory_Adr_Wr_Valid_Ready_s        : std_logic;\n";
01636                     sInternals += tab(level) + "signal Loop_Memory_Adr_Wr_Valid_fifo_s         : std_logic_vector(0 downto 0);\n";
01637                     sInternals += tab(level) + "signal Loop_Memory_Adr_Wr_Valid_fifo_full_s    : std_logic;\n";
01638                     sInternals += tab(level) + "signal Loop_Memory_Adr_Wr_Valid_fifo_empty_s   : std_logic;\n";
01639                     sInternals += tab(level) + "signal Loop_Memory_Adr_Wr_Valid_fifo_write_s   : std_logic;\n";
01640                     sInternals += tab(level) + "signal Loop_Memory_Adr_Wr_Valid_fifo_read_s    : std_logic;\n";
01641                     sInternals += tab(level) + "signal n_Loop_Memory_Adr_Wr_Valid_fifo_empty_s : std_logic;\n\n";
01642                 }
01643                 
01644         
01645                 return sInternals;
01646         }
01647 
01648         
01655         private String createFifoSignals(SimpleVariable sv, int level)
01656         {
01657                 String sFifoSignals = new String();
01658                 
01659                 String type = ((SimpleVariable)sv).getType();
01660                 if (type.equalsIgnoreCase("cond") || type.equalsIgnoreCase("resultComp") )
01661                         sFifoSignals += tab(level) + "signal " + sv.getName() + "_fifo_s         : std_logic;\n";
01662                 else
01663                         sFifoSignals += tab(level) + "signal " + sv.getName() + "_fifo_s         : " + NumType.getVHDLType(numType) + ";\n";
01664         
01665                 sFifoSignals += tab(level) + "signal " + sv.getName() + "_fifo_read_s    : std_logic;\n";
01666                 sFifoSignals += tab(level) + "signal " + sv.getName() + "_fifo_write_s   : std_logic;\n";
01667                 sFifoSignals += tab(level) + "signal " + sv.getName() + "_fifo_empty_s   : std_logic;\n";
01668                 sFifoSignals += tab(level) + "signal " + sv.getName() + "_fifo_full_s    : std_logic;\n";
01669         sFifoSignals += tab(level) + "signal n_" + sv.getName() + "_fifo_empty_s : std_logic;\n";
01670                 
01671                 return sFifoSignals;
01672         }
01673         
01674         
01683         private String createBody(int level, Vector<Element> body, Function parentFunction) throws VHDLException {
01684                 String sBody = new String();
01685                 for (Element el : body) {
01686                         if (el instanceof Assignment) {
01687                                 sBody += this.createAssignment((Assignment) el, level, parentFunction);
01688                         } else if (el instanceof Operation) {
01689                                 sBody += this.createOp((Operation) el, level, parentFunction);
01690                         } else if (el instanceof LoopFor) {
01691                                 sBody += this.createFunction((LoopFor) el, level);
01692                         } else if (el instanceof IfThenElse && !el.getName().contains("loopfor")) {
01693                                 sBody += this.createBody(level, ((IfThenElse)el).getBodyFalse(), parentFunction);
01694                                 sBody += this.createBody(level, ((IfThenElse)el).getBodyTrue(), parentFunction);
01695                                 sBody += this.createBody(level, ((IfThenElse)el).getCond(), parentFunction);
01696                         }
01697                 }
01698                 return sBody;
01699         }
01700         
01701         
01708         private String createFunction(Function el, int level) throws VHDLException 
01709         {
01710                 String sFunc = new String();
01711                 SimpleVariable var;
01712                 
01713                 if (el instanceof LoopFor)
01714                 {
01715                         /* Instance of classic loopFor */
01716                         LoopFor loop = (LoopFor)el;
01717                         sFunc += "\n" + tab(level) + loop.getName() + "_inst : loop_" + loop.getName() + "\n";
01718                         sFunc += tab(level) + "port map(\n";
01719                         sFunc += tab(level + 1) + "Clock_i\t=> Clock_i,\n";
01720                         sFunc += tab(level + 1) + "Reset_i\t=> Reset_i,\n";
01721                         
01722                         for (int i = 0; i < loop.getInput().size(); i++) {                              
01723                                 /* Check the validity of the input */
01724                                 if (!(loop.getInput().elementAt(i) instanceof SimpleVariable))
01725                                         throw new VHDLException("Loop for inputs must be variables.");
01726                                 
01727                                 var = (SimpleVariable)loop.getInput().elementAt(i);
01728                                 
01729                                 /* Check if the input is a constant variable */
01730                                 if (var.getType().equalsIgnoreCase("const")) {
01731                                         sFunc += tab(level + 1) + var.getName() + "_i\t => "  + this.createValues(var.getValues()) + "," + "--=" + var.getValues() + "\n";
01732                                         sFunc += tab(level + 1) + var.getName() + "_Valid_i\t=> '1',\n";
01733                                         sFunc += tab(level + 1) + var.getName() + "_Ready_o\t=> open,\n";
01734                                 }
01735                                 else {
01736                                         sFunc += tab(level + 1) + var.getName() + "_i\t => " + var.getName() + "_s,\n";
01737                                         sFunc += tab(level + 1) + var.getName() + "_Valid_i\t=> " + var.getName() + "_Valid_s,\n";
01738                                         sFunc += tab(level + 1) + var.getName() + "_Ready_o\t=> " + var.getName() + "_Ready_s,\n";              
01739                                 }
01740                         }
01741                         
01742                         /* Check if the start variable is a constant */
01743                         if (!loop.getStart().getType().equalsIgnoreCase("const") && !loop.getInput().contains(loop.getStart())) {
01744                                 var = loop.getStart();
01745                                 sFunc += tab(level + 1) + var.getName() + "_i\t => " + var.getName() + "_s,\n";
01746                                 sFunc += tab(level + 1) + var.getName() + "_Valid_i\t=> " + var.getName() + "_Valid_s,\n";
01747                                 sFunc += tab(level + 1) + var.getName() + "_Ready_o\t=> " + var.getName() + "_Ready_s,\n";              
01748                         }
01749                         
01750                         /* Check if the increment variable is a constant */
01751                         if (!loop.getIncr().getType().equalsIgnoreCase("const") && !loop.getInput().contains(loop.getIncr())) {
01752                                 var = loop.getIncr();
01753                                 sFunc += tab(level + 1) + var.getName() + "_i\t => " + var.getName() + "_s,\n";
01754                                 sFunc += tab(level + 1) + var.getName() + "_Valid_i\t=> " + var.getName() + "_Valid_s,\n";
01755                                 sFunc += tab(level + 1) + var.getName() + "_Ready_o\t=> " + var.getName() + "_Ready_s,\n";              
01756                         }
01757                         
01758                         /* Check if the end variable is a constant */
01759                         if (!loop.getEnd().getType().equalsIgnoreCase("const") && !loop.getInput().contains(loop.getEnd())) {
01760                                 var = loop.getEnd();
01761                                 sFunc += tab(level + 1) + var.getName() + "_i\t => " + var.getName() + "_s,\n";
01762                                 sFunc += tab(level + 1) + var.getName() + "_Valid_i\t=> " + var.getName() + "_Valid_s,\n";
01763                                 sFunc += tab(level + 1) + var.getName() + "_Ready_o\t=> " + var.getName() + "_Ready_s,\n";              
01764                         }
01765                         
01766                         for (int i = 0; i < loop.getOutput().size(); i++) {             
01767                                 /* Check the validity of the output */
01768                                 if (!(loop.getOutput().elementAt(i) instanceof SimpleVariable))
01769                                         throw new VHDLException("Loop for inputs must be variables.");
01770                                 
01771                                 var = (SimpleVariable)loop.getOutput().elementAt(i);                    
01772                                 sFunc += tab(level + 1) + this.createVar(var, "_o")       + "\t=> " + this.createVar(var, "_s,") + "\n";
01773                                 sFunc += tab(level + 1) + this.createVar(var, "_Valid_o") + "\t=> " + this.createVar(var, "_Valid_s,") + "\n";
01774                                 sFunc += tab(level + 1) + this.createVar(var, "_Ready_i") + "\t=> " + this.createVar(var, "_Ready_s");
01775                                 sFunc += i == loop.getOutput().size()-1 ? "\n" : ",\n";
01776                         }
01777                         sFunc += tab(level) + ");\n";
01778                 }
01779                         
01780                 return sFunc;
01781         }
01782 
01790         private String createLoop(LoopFor loop, int level) throws VHDLException 
01791         {       
01792                 String sLoopFor = new String();
01793                 sLoopFor += createLoopForEntity(loop, level);
01794                 sLoopFor += "architecture comp of " + "loop_" + loop.getName() + " is\n\n";
01795                 sLoopFor += createLoopForComponent(loop, level+1);
01796                 sLoopFor += createInternals(loop, level+1);
01797                 sLoopFor += "begin\n\n";
01798                 
01799                 /* Generate initial assignment */
01800                 SimpleVariable initVar;
01801                 for(SimpleVariable var : loop.getLoopVariables()) {                     
01802                         if(var.getType().equalsIgnoreCase("iter")) {
01803                                 if(!loop.getStart().getType().equalsIgnoreCase("const")) {
01804                                         sLoopFor += tab(level+1) + var.getName() + "_Init_s <= " + loop.getStart().getName() + "_i;\n";
01805                                         sLoopFor += tab(level+1) + loop.getStart().getName() + "_o <= " + var.getName() + "_Init_Valid_s or (Data_Init_Valid_s and " + var.getName() + "_Init_Ready_s);\n";
01806                                         sLoopFor += tab(level+1) + var.getName() + "_Init_Valid_s <= " + loop.getStart().getName() + "_Valid__i;\n";
01807                                 }
01808                                 else {
01809                                         sLoopFor += tab(level+1) + var.getName() + "_Init_s <= " + createValues(loop.getStart().getValues()) + ";\n";
01810                                         sLoopFor += tab(level+1) + var.getName() + "_Init_Valid_s <= '1';\n";
01811                                 }
01812                         }
01813                         else if((initVar = loop.getInitVariable(var)) != null) {
01814                                 sLoopFor += tab(level+1) + var.getName() + "_Init_s <= " + initVar.getName() + "_i;\n";
01815                                 sLoopFor += tab(level+1) + initVar.getName() + "_Ready_o <= not " + var.getName() + "_Init_Valid_s or (Data_Init_Valid_s and " + var.getName() + "_Init_Ready_s);\n";
01816                                 sLoopFor += tab(level+1) + var.getName() + "_Init_Valid_s <= " + initVar.getName() + "_Valid_i;\n";
01817                         }       
01818                         else {
01819                                 sLoopFor += tab(level+1) + var.getName() + "_Init_s <= " + var.getName() + "_i;\n";
01820                                 sLoopFor += tab(level+1) + var.getName() + "_Ready_o <= not " + var.getName() + "_Init_Valid_s or (Data_Init_Valid_s and " + var.getName() + "_Init_Ready_s);\n";
01821                                 sLoopFor += tab(level+1) + var.getName() + "_Init_Valid_s <= " + var.getName() + "_Valid_i;\n";
01822                         }       
01823                         
01824                         sLoopFor += tab(level+1) + var.getName() + "_Ready_s <= ";
01825                         for(SimpleVariable sv : var.getVariableMap().get(loop).getMultSimpleVar()) {
01826                                 sLoopFor += sv.getName() + "_Ready_s";
01827                                 sLoopFor += var.getVariableMap().get(loop).getMultSimpleVar().lastElement().equals(sv) ? ";\n" : " and ";
01828                         }
01829                         
01830                         for(SimpleVariable sv : var.getVariableMap().get(loop).getMultSimpleVar()) {
01831                                 sLoopFor += tab(level+1) + sv.getName() + "_s <= " + var.getName() + "_s;\n";
01832                                 sLoopFor += tab(level+1) + sv.getName() + "_Valid_s <= Data_Ready_s;\n";
01833                         }
01834                         sLoopFor += "\n";
01835                 }
01836                 
01837                 /* Generate global assignment */
01838                 String sDataInitValid = new String();
01839                 String sDataReady = new String();
01840                 String sDataValid = new String();
01841                 String sDataLoopValid = new String();   
01842                 sDataInitValid += tab(level+1) + "Data_Init_Valid_s   <= ";
01843                 sDataValid     += tab(level+1) + "Data_Valid_s        <= ";
01844                 sDataReady     += tab(level+1) + "Data_Ready_s        <= Data_Valid_s and ";
01845                 sDataLoopValid += tab(level+1) + "Data_Loop_Valid_s   <= ";     
01846                 for(SimpleVariable var : loop.getLoopVariables()) {             
01847                         if(!loop.getLoopVariables().lastElement().equals(var)) {
01848                                 sDataInitValid += var.getName() + "_Init_Valid_s and ";
01849                                 sDataValid     += var.getName() + "_Valid_s and ";
01850                                 sDataReady     += var.getName() + "_Ready_s and ";
01851                                 sDataLoopValid += var.getName() + "_Loop_Valid_s and ";
01852                         }
01853                         else {
01854                                 sDataInitValid += var.getName() + "_Init_Valid_s;\n";
01855                                 sDataValid     += var.getName() + "_Valid_s;\n";
01856                                 sDataReady     += var.getName() + "_Ready_s;\n";
01857                                 sDataLoopValid += var.getName() + "_Loop_Valid_s;\n";
01858                         }
01859                 }
01860                 sLoopFor += sDataInitValid + sDataReady + sDataValid + sDataLoopValid;
01861                 
01862                 // TODO Adapter au cas particulier
01863                 SimpleVariable cond = (SimpleVariable)((IfThenElse)loop.getBody().elementAt(0)).getInternalVars().elementAt(0);
01864                 sLoopFor += tab(level+1) + "Fifo_Ready_s        <= n_" + cond.getName() + "_fifo_empty_s and Data_Loop_Valid_s;\n";
01865                 sLoopFor += tab(level+1) + "n_Fifo_Ready_s      <= not Fifo_Ready_s;\n";
01866                 sLoopFor += tab(level+1) + "Cond_Loop_s         <= not ("+ cond.getName() + "_fifo_s and Fifo_Ready_s);\n\n";
01867         
01868                 sLoopFor += tab(level+1) + "Loop_Memory_Write_s <= not " + cond.getName() + "_fifo_s and Fifo_Ready_s;\n";
01869                 
01870                 /* Generate components for reading results */
01871                 String sLoopMemoryRead  = new String();
01872                 String sLoopMemoryValid = new String();
01873                 String sLoopMemoryReady = new String();
01874                 for(Element e : loop.getOutput()) {
01875                         if(!loop.getOutput().lastElement().equals(e)) {
01876                                 sLoopMemoryRead  += "Loop_Memory_" + e.getName() + "_Ready_s and ";
01877                                 sLoopMemoryValid += "Loop_Memory_" + e.getName() + "_Valid_s and ";
01878                                 sLoopMemoryReady += e.getName() + "_Ready_i and ";
01879                         }
01880                         else {
01881                                 sLoopMemoryRead  += "Loop_Memory_" + e.getName() + "_Ready_s;\n";
01882                                 sLoopMemoryValid += "Loop_Memory_" + e.getName() + "_Valid_s;\n";
01883                                 sLoopMemoryReady += e.getName() + "_Ready_i;\n\n";
01884                         }
01885                 }
01886                 sLoopFor += tab(level+1) + "Loop_Memory_Read_s  <= " + sLoopMemoryRead;
01887                 sLoopFor += tab(level+1) + "Loop_Memory_Valid_s <= " + sLoopMemoryValid;
01888                 sLoopFor += tab(level+1) + "Loop_Memory_Ready_s <= " + sLoopMemoryReady;
01889                 
01890                 /* Generate init Blocs */
01891                 for(SimpleVariable var : loop.getLoopVariables()) {             
01892                         sLoopFor += tab(level+1) + "Init_" + var.getName() + " : Init_Input\n";
01893                         sLoopFor += tab(level+1) + "port map(\n";
01894                         sLoopFor += tab(level+2) + "Clock_i           => Clock_i,\n";
01895                         sLoopFor += tab(level+2) + "Reset_i           => Reset_i,\n";
01896                         sLoopFor += tab(level+2) + "Data_i            => " + var.getName() + "_Init_s,\n";
01897                         sLoopFor += tab(level+2) + "Data_Valid_i      => Data_Init_Valid_s,\n";
01898                         sLoopFor += tab(level+2) + "Data_Ready_o      => " + var.getName() + "_Init_Ready_s,\n";
01899                         sLoopFor += tab(level+2) + "Data_Loop_i       => " + var.getName() + "_Loop_s,\n";
01900                         sLoopFor += tab(level+2) + "Data_Loop_Valid_i => Data_Loop_Valid_s,\n";
01901                         sLoopFor += tab(level+2) + "Data_Loop_Ready_o => " + var.getName() + "_Loop_Ready_s,\n";
01902                         sLoopFor += tab(level+2) + "Cond_Loop_i       => Counter_Ready_s,\n";
01903                         sLoopFor += tab(level+2) + "Data_o            => " + var.getName() + "_s,\n";
01904                         sLoopFor += tab(level+2) + "Data_Valid_o      => " + var.getName() + "_Valid_s,\n";
01905                         sLoopFor += tab(level+2) + "Data_Ready_i      => Data_Ready_s\n";
01906                         sLoopFor += tab(level+1) + ");\n\n";
01907                 }
01908                 
01909                 // Create fifo of loop variable
01910                 sLoopFor += createFifo(loop);
01911                 
01912                 
01913                 /* Generate the loop iterator operation and condition */
01914                 Addition add = new Addition();
01915                 SimpleVariable iter = (SimpleVariable)loop.getInternalVars().elementAt(0);
01916                 int minCost = multUseSignal.get(loop).getMinCost(iter);
01917                 add.setName("add_" + iter.getName());
01918                 add.addInput(loop.getStart());
01919                 add.addInput(iter.getVariableMap().get(loop).getMapVHDLSimpleVariable(minCost));
01920                 add.addOutput(new SimpleVariable(iter.getName() + "_Add"));
01921                 sLoopFor += createOp(add, level+1, loop);
01922                 if(iter.getCost() + loop.getIterOperation().getBlock().latencyTime()+1 < loop.getMaxCost()) {
01923                         SimpleVariable sv = (SimpleVariable)loop.getIterOperation().getOutputAt(0);
01924                         sLoopFor += createFifoInstanciation(sv, loop.getRelativeMaxCost()-add.getBlock().latencyTime(), NumType.getDataSize(numType), false);
01925                         sLoopFor += tab(1) + iter.getName() + "_Loop_s <= " +  iter.getName() + "_Add_fifo_s;\n";
01926                         sLoopFor += tab(1) + iter.getName() + "_Loop_Valid_s <= " + "n_" + iter.getName() + "_Add_fifo_empty_s;\n";
01927                 }
01928                 else {
01929                         sLoopFor += tab(1) + iter.getName() + "_Add_Ready_s <= " +  iter.getName() + "_Loop_Ready_s;\n";
01930                         sLoopFor += tab(1) + iter.getName() + "_Loop_s <= " +  iter.getName() + "_Add_s;\n";
01931                         sLoopFor += tab(1) + iter.getName() + "_Loop_Valid_s <= " + iter.getName() + "_Add_Valid_s;\n";
01932                 }
01933                 sLoopFor += createBody(level+1, ((IfThenElse)loop.getBody().firstElement()).getCond(), loop);
01934                 
01935                 // Generate the body of the loop
01936                 sLoopFor += createBody(level+1, loop.getBody(), loop);
01937                 
01938                 /* Generate init counter */
01939                 sLoopFor += "\n" + tab(level+1) + "Inst_Counter_Wr : Counter_Wr\n";
01940                 sLoopFor += tab(level+1) + "generic map(\n";
01941                 sLoopFor += tab(level+2) + "\tData_Size_g => " + NumType.getDataSize(numType) + ",\n";
01942                 sLoopFor += tab(level+2) + "\tMem_Size_g  => " + loop.getRelativeMaxCost() + "\n";
01943                 sLoopFor += tab(level+1) + ")\n";
01944                 sLoopFor += tab(level+1) + "port map(\n";
01945                 sLoopFor += tab(level+2) + "Clock_i      => Clock_i,\n";
01946                 sLoopFor += tab(level+2) + "Reset_i      => Reset_i,\n";
01947                 sLoopFor += tab(level+2) + "Data_i       => Adr_Wr_fifo_s,\n";
01948                 sLoopFor += tab(level+2) + "Data_Valid_i => Loop_Memory_Adr_Wr_Valid_fifo_s(0),\n";
01949                 sLoopFor += tab(level+2) + "Read_i       => Loop_Memory_Valid_s,\n";
01950                 sLoopFor += tab(level+2) + "Adr_Rd_i     => Loop_Memory_Adr_Read_s,\n";
01951                 sLoopFor += tab(level+2) + "Ready_i      => Data_Init_Valid_s,\n";
01952                 sLoopFor += tab(level+2) + "Fifo_Ready_i => n_Fifo_Ready_s,\n";
01953                 sLoopFor += tab(level+2) + "Cond_i       => Cond_Loop_s,\n";
01954                 sLoopFor += tab(level+2) + "Data_o       => Adr_Wr_s,\n";
01955                 sLoopFor += tab(level+2) + "Data_Valid_o => Loop_Memory_Adr_Wr_Valid_s(0),\n";
01956                 sLoopFor += tab(level+2) + "Ready_o      => Counter_Ready_s\n";
01957                 sLoopFor += tab(level+1) + ");\n";
01958                 
01959                 sLoopFor += createFifoInstanciation(new SimpleVariable("Adr_Wr"), loop.getRelativeMaxCost(), loop.getRelativeMaxCost(), true);
01960                 sLoopFor += tab(level+1) + "Adr_Wr_Valid_s <= Data_Ready_s;\n";
01961                 sLoopFor += tab(level+1) + "Loop_Memory_Adr_Write_s <= Adr_Wr_fifo_s;\n\n";
01962                 sLoopFor += createFifoInstanciation(new SimpleVariable("Loop_Memory_Adr_Wr_Valid"), loop.getRelativeMaxCost(), 1, false);
01963                 sLoopFor += tab(level+1) + "Loop_Memory_Adr_Wr_Valid_Valid_s <= Data_Ready_s;\n\n";
01964                 
01965                 
01966                 /* Generate read counter component */
01967                 sLoopFor += tab(level+1) + "Inst_Counter_Rd : Counter_Rd\n";
01968                 sLoopFor += tab(level+1) + "generic map(\n";
01969                 sLoopFor += tab(level+2) + "Size_g => " + loop.getRelativeMaxCost() + "\n";
01970                 sLoopFor += tab(level+1) + ")\n";
01971                 sLoopFor += tab(level+1) + "port map(\n";
01972                 sLoopFor += tab(level+2) + "Clock_i => Clock_i,\n";
01973                 sLoopFor += tab(level+2) + "Reset_i => Reset_i,\n";
01974                 sLoopFor += tab(level+2) + "En_i    => Loop_Memory_Valid_s,\n";
01975                 sLoopFor += tab(level+2) + "Ready_i => Loop_Memory_Ready_s,\n";
01976                 sLoopFor += tab(level+2) + "Count_o => Loop_Memory_Adr_Read_s\n";
01977                 sLoopFor += tab(level+1) + ");\n\n";
01978                 
01979                 /* Generate components for reading results */
01980                 for(Element e : loop.getOutput()) {
01981                         String name = e.getName();
01982                         
01983                         /* Generate loop memory component */
01984                         sLoopFor += tab(level+1) + name + "_Memory : Memory\n";
01985                         sLoopFor += tab(level+1) + "generic map(\n";
01986                         sLoopFor += tab(level+2) + "Data_Width_g => " + NumType.getDataSize(numType) + ",\n";
01987                         sLoopFor += tab(level+2) + "Mem_Size_g   => " + loop.getRelativeMaxCost() + "\n";
01988                         sLoopFor += tab(level+1) + ")\n";
01989                         sLoopFor += tab(level+1) + "port map(\n";
01990                         sLoopFor += tab(level+2) + "Clock_i      => Clock_i,\n";
01991                         sLoopFor += tab(level+2) + "Reset_i      => Reset_i,\n";
01992                         sLoopFor += tab(level+2) + "Wr_i         => Loop_Memory_Write_s,\n";
01993                         sLoopFor += tab(level+2) + "Rd_i         => Loop_Memory_Read_s,\n";
01994                         sLoopFor += tab(level+2) + "Adr_Wr_i     => Loop_Memory_Adr_Write_s,\n";
01995                         sLoopFor += tab(level+2) + "Adr_Rd_i     => Loop_Memory_Adr_Read_s,\n";
01996                         sLoopFor += tab(level+2) + "Adr_Valid_i  => Loop_Memory_Adr_Wr_Valid_fifo_s(0),\n";
01997                         sLoopFor += tab(level+2) + "Data_i       => " + name + "_Delayed_s,\n";
01998                         sLoopFor += tab(level+2) + "Data_Valid_o => Loop_Memory_" + name + "_Valid_s,\n";
01999                         sLoopFor += tab(level+2) + "Data_o       => Loop_Memory_" + name + "_s\n";
02000                         sLoopFor += tab(level+1) + ");\n\n";
02001                         
02002                         /* Generate read result component */
02003                         sLoopFor += tab(level+1) + "Read_" + name + "_Mem_Result : Read_Result\n";
02004                         sLoopFor += tab(level+1) + "port map(\n";
02005                         sLoopFor += tab(level+2) + "Clock_i => Clock_i,\n";
02006                         sLoopFor += tab(level+2) + "Reset_i => Reset_i,\n";
02007                         sLoopFor += tab(level+2) + "Data_i  => Loop_Memory_" + name + "_s,\n";
02008                         sLoopFor += tab(level+2) + "Valid_i => Loop_Memory_" + name + "_Valid_s,\n";
02009                         sLoopFor += tab(level+2) + "Ready_o => Loop_Memory_" + name + "_Ready_s,\n";
02010                         sLoopFor += tab(level+2) + "Data_o  => " + name + "_o,\n";
02011                         sLoopFor += tab(level+2) + "Valid_o => " + name + "_Valid_o,\n";
02012                         sLoopFor += tab(level+2) + "Ready_i => " + name + "_Ready_i\n";
02013                         sLoopFor += tab(level+1) + ");\n\n";
02014                 }       
02015                 
02016                 sLoopFor += "end comp;";
02017                 
02018                 return sLoopFor;
02019         }
02020 
02021 
02022         
02027         private String createFifo(Function f)
02028         {
02029                 String sFifo = new String();    
02030                 project.setUseFifo(false);
02031                 
02032                 for (Element var : f.getInternalVars()) {
02033                         if (var instanceof SimpleVariable && !((SimpleVariable)var).getType().equals("const")) {        
02034                                 /* Instanciation of fifo simple variable */
02035                                 if(((SimpleVariable)var).getSimpleFifo())
02036                                         sFifo += createFifoInstanciation((SimpleVariable)var, f.getRelativeMaxCost(),  NumType.getDataSize(numType), false);            
02037                                 
02038                                 /* Instanciation of fifo multi-use variable */
02039                                 if(((SimpleVariable) var).getVariableMap().get(f) != null) {                    
02040                                         for(SimpleVariable sv : ((SimpleVariable) var).getVariableMap().get(f).getMultSimpleVar())
02041                                                 if(sv.getSimpleFifo())
02042                                                         sFifo += createFifoInstanciation(sv, f.getRelativeMaxCost(),  NumType.getDataSize(numType), false);
02043                                 }
02044                         } 
02045                 }
02046                 
02047                 return sFifo;
02048         }
02049                 
02050         
02051         private String createFifoInstanciation(SimpleVariable sv, int fifoSize, int dataSize, boolean ilogup) 
02052         {       
02053                 String sFifoInstanciation = new String();
02054 
02055                 // Get the type of fifo the user wants (Standard/Altera/Xilinx)
02056                 String fifoType = project.getOptimisationProperties().getFifoType();    
02057                 
02058                 // Get the name of the output signal that is used at multiples inputs
02059                 String sigNameOut = sv.getName();
02060 
02061                 /* Set the type of fifo */
02062                 sFifoInstanciation += "\n" + tab(1) + "fifo" + "_" + sigNameOut + ": ";
02063                 if (fifoType.equalsIgnoreCase("Altera")) {
02064                         sFifoInstanciation += "fifo_Altera\n";
02065                 } else if (fifoType.equalsIgnoreCase("Xilinx")) {
02066                         sFifoInstanciation += "fifo_Xilinx\n";
02067                 } else {
02068                         sFifoInstanciation += "fifo_Std\n";
02069                 }
02070                 
02071                 /* Set the size of the fifo */
02072                 if (sv.getType().equalsIgnoreCase("cond") || sv.getType().equalsIgnoreCase("resultComp") ) {
02073                         sFifoInstanciation += tab(1) + "generic map(\n";
02074                         sFifoInstanciation += tab(2) + "FIFOSIZE => " +  (fifoSize) + ",\n";
02075                         sFifoInstanciation += tab(2) + "DATAWIDTH => 1)\n";
02076                 } else {
02077                         sFifoInstanciation += tab(1) + "generic map(\n";
02078                         sFifoInstanciation += tab(2) + "FIFOSIZE => " +  (fifoSize) + ",\n";
02079                         if(!ilogup)
02080                                 sFifoInstanciation += tab(2) + "DATAWIDTH => " +  dataSize + ")\n";
02081                         else
02082                                 sFifoInstanciation += tab(2) + "DATAWIDTH => ilogup(" +  dataSize + "))\n";
02083                 }
02084                 
02085                 /* Mapping of the fifo */
02086                 sFifoInstanciation += tab(1) + "port map(\n";
02087                 sFifoInstanciation += tab(2) + "Clock_i      => Clock_i,\n";
02088                 sFifoInstanciation += tab(2) + "Reset_i      => Reset_i,\n";
02089                 sFifoInstanciation += tab(2) + "Wr_Req_i     => " + sigNameOut + "_fifo_write_s,\n";
02090                 sFifoInstanciation += tab(2) + "Rd_Req_i     => " + sigNameOut + "_fifo_read_s,\n";
02091                 if (sv.getType().equalsIgnoreCase("cond") || sv.getType().equalsIgnoreCase("resultComp")) {
02092                         sFifoInstanciation += tab(2) + "Data_i(0)    => " + sigNameOut + "_s,\n";
02093                         sFifoInstanciation += tab(2) + "Data_o(0)    => " + sigNameOut + "_fifo_s,\n";
02094                 } else {
02095                         sFifoInstanciation += tab(2) + "Data_i       => " + sigNameOut + "_s,\n";
02096                         sFifoInstanciation += tab(2) + "Data_o       => " + sigNameOut + "_fifo_s,\n";
02097                 }                               
02098                 sFifoInstanciation += tab(2) + "Fifo_Full_o  => " + sigNameOut  + "_fifo_full_s,\n";
02099                 sFifoInstanciation += tab(2) + "Fifo_Empty_o => " + sigNameOut + "_fifo_empty_s\n";
02100                 sFifoInstanciation += tab(1) + ");\n";
02101                 sFifoInstanciation += tab(1) + "n_" + sigNameOut + "_fifo_empty_s <= " + "not " + sigNameOut + "_fifo_empty_s;\n";
02102                 sFifoInstanciation += tab(1) + sigNameOut + "_fifo_write_s   <= " + sigNameOut + "_Valid_s;\n";
02103                 sFifoInstanciation += tab(1) + sigNameOut + "_fifo_read_s    <= Fifo_Ready_s;\n";
02104                 sFifoInstanciation += tab(1) + sigNameOut + "_Ready_s        <= " + "not " + sigNameOut + "_fifo_full_s;\n";
02105                 
02106                 if(sv.getInputLoopVariable() != null) {
02107                         sFifoInstanciation += tab(1) + sv.getInputLoopVariable().getName()  + "_Loop_s <= " +  sigNameOut + "_fifo_s;\n";
02108                         sFifoInstanciation += tab(1) + sv.getInputLoopVariable().getName()  + "_Loop_Valid_s <= " + "n_" + sigNameOut + "_fifo_empty_s;\n";
02109                 }
02110                 else if(sv.getOutputLoopVariable() != null) {
02111                         sFifoInstanciation += tab(1) + sv.getOutputLoopVariable().getName()  + "_Delayed_s <= " +  sigNameOut + "_fifo_s;\n";                   
02112                 }
02113                         
02114                 return sFifoInstanciation;
02115         }
02116 
02117         
02126         private String createAssignment(Assignment aff, int level, Function parentFunction) 
02127         {
02128                 String sAssign = new String();
02129                         
02130                 if (aff.getOutputAt(0) instanceof SimpleVariable) 
02131                 {
02132                         /* Get input and output varaible of the assignment */
02133                         SimpleVariable varIn  = (SimpleVariable)aff.getInputAt(0);
02134                         SimpleVariable varOut = (SimpleVariable)aff.getOutputAt(0);
02135                         
02136                         if (!varIn.getType().equals("const")) 
02137                         {
02138                                 /* Mapping of usual assignment */
02139                                 if(top.getInput().contains(varIn)) {
02140                                         sAssign += "\n" + tab(level) + this.createVar(varOut, "_s") + " <= " + this.createVar(varIn, "_i") + ";\n";                     
02141                                         sAssign += tab(level) + this.createVar(varIn, "_Ready_o") + " <= " + this.createVar(varOut, "_Ready_s") + ";\n";        
02142                                         sAssign += tab(level) + this.createVar(varOut, "_Valid_s") + " <= " + this.createVar(varIn, "_Valid_i") + ";\n";
02143                                 }
02144                                 else if(top.getOutput().contains(varOut)) {
02145                                         sAssign += "\n" + tab(level) + this.createVar(varOut, "_o") + " <= " + this.createVar(varIn, "_s") + ";\n";                     
02146                                         sAssign += tab(level) + this.createVar(varIn, "_Ready_s") + " <= " + this.createVar(varOut, "_Ready_i") + ";\n";        
02147                                         sAssign += tab(level) + this.createVar(varOut, "_Valid_o") + " <= " + this.createVar(varIn, "_Valid_s") + ";\n";                
02148                                 }
02149                                 else {
02150                                         sAssign += "\n" + tab(level) + this.createVar(varOut, "_s") + " <= " + this.createVar(varIn, "_s") + ";\n";                     
02151                                         sAssign += tab(level) + this.createVar(varIn, "_Ready_s") + " <= " + this.createVar(varOut, "_Ready_s") + ";\n";        
02152                                         sAssign += tab(level) + this.createVar(varOut, "_Valid_s") + " <= " + this.createVar(varIn, "_Valid_s") + ";\n";
02153                                 }                               
02154                                                                 
02155                                 if(multUseSignal.get(parentFunction).variables.contains(varOut)) {
02156                                         sAssign += createTemporaryAssignment(parentFunction, varOut, level);                    
02157                                 }
02158                         }
02159                 }
02160                         
02161                 return sAssign;
02162         }
02163 
02164         
02172         private String createTemporaryAssignment(Function parentFunction, SimpleVariable var, int level) 
02173         {
02174                 String sTempAssign = new String();
02175                         
02176                 /* Mapping of the "ready" output variable for mult variables */
02177                 sTempAssign += tab(level) + this.createVar(var, "_Ready_s") + " <= ";   
02178                 for(SimpleVariable sv : var.getVariableMap().get(parentFunction).getMultSimpleVar()) {
02179                         if(sv.equals(var.getVariableMap().get(parentFunction).getMultSimpleVar().lastElement()))
02180                                 sTempAssign += this.createVar(sv, "_Ready_s") + ";\n";
02181                         else
02182                                 sTempAssign += this.createVar(sv, "_Ready_s") + " and ";
02183                 }                                       
02184                                         
02185                 /* Mapping of mult variables */
02186                 for(SimpleVariable sv : var.getVariableMap().get(parentFunction).getMultSimpleVar()) {
02187                         sTempAssign += tab(level) + this.createVar(sv, "_s") + " <= " + this.createVar(var, "_s") + ";\n";                      
02188                         sTempAssign += tab(level) + this.createVar(sv, "_Valid_s") + " <= " + this.createVar(var, "_Valid_s");
02189                         for(SimpleVariable sv2 : var.getVariableMap().get(parentFunction).getMultSimpleVar())
02190                                 if(!sv2.equals(sv))
02191                                         sTempAssign += " and " + this.createVar(sv2, "_Ready_s");
02192                         sTempAssign += ";\n";   
02193                 }                                       
02194 
02195                 return sTempAssign;
02196         }
02197         
02198         
02207         private String createOp(Operation op, int level, Function parentFunction) throws VHDLException 
02208         {
02209                 String sOp = new String();
02210 
02211                 /* Get the builfing bloc of the operation */
02212                 BuildingBlock blockOp = op.getBlock();
02213                 if (blockOp == null)
02214                         throw new VHDLException("No material description for the operation : " + op.getName());
02215                 
02216                 /* Assignement of temporary signal if the variable is a temporary variable */
02217                 if(multUseSignal.get(parentFunction).variables.contains(op.getOutputAt(0))) {
02218                         sOp += "\n" + createTemporaryAssignment(parentFunction, (SimpleVariable) op.getOutputAt(0), level);
02219                 }       
02220                 
02221                 /* Instance of classic operation */
02222                 sOp += "\n" + tab(level) + op.getName() + ": wrapper_" + blockOp.entityName() + "\n";
02223                 sOp += tab(level) + "generic map(\n";
02224                 if(op instanceof Multiplexer)
02225                         sOp += tab(level+1) + "D_Sel_Fifo_Size => " + (2+((SimpleVariable)((Multiplexer)op).getSel()).getSimpleFifoCost()) + ",\n";
02226                 for(int i = 0; i < op.getNbInput(); i++) {
02227                         SimpleVariable var = (SimpleVariable)op.getInputAt(i);
02228                         sOp += tab(level+1) + "D" + (i+1) + "_Fifo_Size => " + (2+var.getSimpleFifoCost());
02229                         sOp += i == op.getNbInput()-1 ? "\n" + tab(level) + ")\n" : ",\n" ;
02230                 }
02231                 
02232                 sOp += tab(level) + "port map(\n";
02233                 sOp += tab(level + 1) + "Clock_i      => Clock_i,\n";
02234                 sOp += tab(level + 1) + "Reset_i      => Reset_i,\n";
02235                 
02236                 /* Mapping of input signals of the operation */
02237                 for (int i = 0; i < blockOp.nbInputs(); i++) 
02238                 {
02239                         if (op.getInputAt(i) instanceof Operation) 
02240                                 throw new VHDLException("All operational entries must be variables.");
02241                         
02242                         SimpleVariable var = (SimpleVariable)op.getInputAt(i);
02243                         
02244                         /* If the variable is a constant, just write the value of the constant, not the name */
02245                         if (var.getType().equals("const")) {            
02246                                 sOp += tab(level + 1) + "D" + (i + 1) + "_i         => "  + this.createValues(var.getValues()) + "," + "--=" + var.getValues() + "\n";
02247                                 sOp += tab(level + 1) + "D" + (i + 1) + "_Valid_i   => '1',\n";
02248                                 sOp += tab(level + 1) + "D" + (i + 1) + "_Ready_o   => open,\n";
02249                         }
02250                         else {
02251                                 sOp += tab(level + 1) + "D" + (i + 1) + "_i         => " + this.createVar(var, "_s") + ",\n";
02252                                 sOp += tab(level + 1) + "D" + (i + 1) + "_Valid_i   => " + this.createVar(var, "_Valid_s") + ",\n";
02253                                 sOp += tab(level + 1) + "D" + (i + 1) + "_Ready_o   => " + this.createVar(var, "_Ready_s") + ",\n";
02254                         }
02255                 }       
02256 
02257                 /* Mapping of selection signal in case of multiplexer operation */
02258                 if (op instanceof Multiplexer) {
02259                         SimpleVariable var = (SimpleVariable)((Multiplexer)op).getSel();        
02260                         
02261                         /* If the variable is a constant, just write the value of the constant, not the name */
02262                         if (var.getType().equals("const")) {            
02263                                 sOp += tab(level + 1) + "D_Sel_i       => '1',\n";
02264                                 sOp += tab(level + 1) + "D_Sel_Valid_i => '1',\n";
02265                                 sOp += tab(level + 1) + "D_Sel_Ready_o => open,\n";
02266                         }
02267                         else {
02268                                 sOp += tab(level + 1) + "D_Sel_i       => " + this.createVar(var, "_s") + ",\n";
02269                                 sOp += tab(level + 1) + "D_Sel_Valid_i => "  + this.createVar(var, "_Valid_s") + ",\n";
02270                                 sOp += tab(level + 1) + "D_Sel_Ready_o => " + this.createVar(var, "_Ready_s") + ",\n";
02271                         }
02272                 }
02273                 
02274                 /* Mapping of result signals */
02275                 SimpleVariable varOutput = (SimpleVariable) op.getOutputAt(0);
02276                 sOp += tab(level + 1) + "Ready_i      => " + this.createVar(varOutput, "_Ready_s,") + "\n";
02277                 if (op instanceof Comparison) {
02278                         sOp += tab(level + 1) + "Smaller_o    => " + this.createVar(varOutput, "_smaller_s,") + "\n";
02279                         sOp += tab(level + 1) + "Bigger_o     => " + this.createVar(varOutput, "_bigger_s,") + "\n";
02280                         sOp += tab(level + 1) + "Equal_o      => " + this.createVar(varOutput, "_equal_s,") + "\n";
02281                 } else {
02282                         sOp += tab(level + 1) + "Result_o     => "+ this.createVar(varOutput, "_s,") + "\n";
02283                 }               
02284                 sOp += tab(level + 1) + "Valid_o      => " + this.createVar(varOutput, "_Valid_s") + "\n";
02285                 sOp += tab(level) + ");\n";
02286                 
02287                 if (op instanceof Comparison) {
02288                         if(op instanceof Less)
02289                                 sOp += tab(level) + this.createVar(varOutput, "_s") + " <= " + createVar(varOutput, "_smaller_s") + ";\n";
02290                         else if(op instanceof LessEqual)
02291                                 sOp += tab(level) + this.createVar(varOutput, "_s") + " <= " + createVar(varOutput, "_smaller_s") + " or " + this.createVar(varOutput, "_equal_s") + ";\n";
02292                         else if(op instanceof Greater)
02293                                 sOp += tab(level) + this.createVar(varOutput, "_s") + " <= " + createVar(varOutput, "_bigger_s") + ";\n";
02294                         else if(op instanceof GreaterEqual)
02295                                 sOp += tab(level) + this.createVar(varOutput, "_s") + " <= " + createVar(varOutput, "_bigger_s") + " or " + this.createVar(varOutput, "_equal_s") + ";\n";
02296                         else if(op instanceof Equal)
02297                                 sOp += tab(level) + this.createVar(varOutput, "_s") + " <= " + createVar(varOutput, "_equal_s")  + ";\n";               
02298                 }
02299                 
02300                 if(varOutput.getInputLoopVariable() != null) {
02301                         sOp += tab(level) + varOutput.getName()  + "_Ready_s <= " + varOutput.getInputLoopVariable().getName() + "_Loop_Ready_s;\n";
02302                         sOp += tab(level) + varOutput.getInputLoopVariable().getName()  + "_Loop_s <= " +  varOutput.getName() + "_s;\n";
02303                         sOp += tab(level) + varOutput.getInputLoopVariable().getName()  + "_Loop_Valid_s <= " + varOutput.getName()  + "_Valid_s;\n";
02304                 }
02305                 
02306                 return sOp;
02307         }
02308 
02309         
02316         private String createVar(SimpleVariable var, String endLineChar) {
02317                 String sVar = new String();
02318                 if (var.getName().isEmpty()) {
02319                         sVar += this.createValues(var.getValues()) + endLineChar + "--=" + var.getValues();
02320                 } else {
02321                         sVar += var.getName() + endLineChar;
02322                 } 
02323                 return sVar;
02324         }
02325 
02326         
02332         private String createValues(ArrayList<Double> val) {
02333                 String sVal = new String();
02334                 BinaryFloatingPoint bin = BinaryFloatingPoint.getInstance();
02335                 if (val.size() == 1) {
02336                         sVal += "\"" + bin.getBinary(val.get(0)) + "\"";
02337                 } else {
02338                         sVal += "(";
02339                         sVal += "\"" + bin.getBinary(val.get(0)) + "\"";
02340                         for (int i = 1; i < val.size(); i++) {
02341                                 sVal += ",\"" + bin.getBinary(val.get(i)) + "\"";
02342                         }
02343                         sVal += ")";
02344                 }
02345                 return sVal;
02346         }
02347 
02348 
02349 //      private String convertType(SimpleVariable var) {
02350 //              String type = NumType.getVHDLType(numType);
02351 //              if (var.getType().equals("vector")) {
02352 //                      type = "vec_array(" + var.getSize() + " downto 0)";
02353 //              }
02354 //              return type;
02355 //      }
02356 
02357         
02363         private String tab(int nb) {
02364                 String s = new String();
02365                 for (int i = 0; i < nb; i++) {
02366                         s += "\t";
02367                 }
02368                 return s;
02369         }
02370 }
 All Classes Namespaces Files Functions Variables Enumerations