Math2mat
|
00001 package m2m.backend.vhdl; 00002 00003 import java.io.File; 00004 import java.io.FileWriter; 00005 import java.io.IOException; 00006 import java.util.Vector; 00007 00008 00009 import m2m.backend.project.ExternalToolsProperties; 00010 import m2m.backend.project.M2MProject; 00011 import m2m.backend.structure.Assignment; 00012 import m2m.backend.structure.Element; 00013 import m2m.backend.structure.Function; 00014 import m2m.backend.structure.IfThenElse; 00015 import m2m.backend.structure.LoopFor; 00016 import m2m.backend.structure.Operation; 00017 import m2m.backend.utils.FileUtils; 00018 00019 import m2m.backend.buildingblocks.BuildingBlock; 00020 00021 00031 public class CompileScriptCreator { 00032 00033 private Function top; 00034 private boolean loopFor; 00035 00036 public CompileScriptCreator(Function top) { 00037 this.top = top; 00038 this.loopFor = false; 00039 } 00040 00047 public boolean createCompileScript(M2MProject project,String projectPath, String outFile) { 00048 String fileName = project.getSimPath()+"/simulation.do"; 00049 File compileFile = new File(fileName); 00050 FileWriter fw; 00051 try { 00052 fw = new FileWriter(compileFile); 00053 String script = new String(); 00054 script = createScript(project,projectPath, outFile); 00055 fw.write(script); 00056 fw.close(); 00057 } catch (IOException e) { 00058 e.printStackTrace(); 00059 return false; 00060 } catch (VHDLException e) { 00061 e.printStackTrace(); 00062 return false; 00063 } 00064 00065 return true; 00066 } 00067 00075 private String createScript(M2MProject project,String projectPath, String outFile) throws VHDLException { 00076 String script = new String(); 00077 00078 //get the type of fifo the user wants (Standard/Altera/Xilinx) 00079 String fifoType = project.getOptimisationProperties().getFifoType(); 00080 00081 // String libDir = System.getProperty("user.dir").replace("\\", "/") + "/lib/VHDL/"; 00082 String libDir = M2MProject.getLibPath()+"/VHDL/"; 00083 String vhdlDir = project.getVHDLPath()+"/"; 00084 00085 // script += "set Path \"" + projectPath.substring(0, projectPath.length() - 1) + "\"\n"; 00086 script += "set Path \"..\"\n"; 00087 script += "set compPath \"$Path/"+M2MProject.getCompDirName()+"\"\n"; 00088 script += "set srcSVPath \"$Path/"+M2MProject.getSVDirName()+"\"\n"; 00089 script += "set srcVHDLPath \"$Path/"+M2MProject.getVHDLDirName()+"\"\n"; 00090 script += "set workPath \"$Path/"+M2MProject.getCompDirName()+"/work\"\n"; 00091 script += "vlib $workPath\n\n"; 00092 script += "vmap work $workPath\n\n"; 00093 00094 00095 if (ExternalToolsProperties.getReference().getSimulationFiles()==ExternalToolsProperties.SIMULATIONFILES_UVM) { 00096 // For independent UVM 00097 script += "## compile uvm\n"; 00098 script += "set uvmPath \"$Path/"+M2MProject.getCompDirName()+"/m2mUvm\"\n"; 00099 script += "vlib $uvmPath\n"; 00100 script += "vmap m2mUvm $uvmPath\n"; 00101 } 00102 else { 00103 // For independent OVM 00104 script += "## compile ovm\n"; 00105 script += "set ovmPath \"$Path/"+M2MProject.getCompDirName()+"/m2mOvm\"\n"; 00106 script += "vlib $ovmPath\n"; 00107 script += "vmap m2mOvm $ovmPath\n"; 00108 } 00109 00110 if (ExternalToolsProperties.getReference().getSimulationFiles()==ExternalToolsProperties.SIMULATIONFILES_UVM) 00111 script += "vlog -work m2mUvm +incdir+$srcSVPath/uvm/src $srcSVPath/uvm/src/uvm_pkg.sv\n\n"; 00112 else if (ExternalToolsProperties.getReference().getSimulationFiles()==ExternalToolsProperties.SIMULATIONFILES_M2MOVM) 00113 script += "vlog -work m2mOvm +define+M2M_USE_OVM +incdir+$srcSVPath/ovm/src $srcSVPath/ovm/src/ovm_pkg.sv\n\n"; 00114 else 00115 script += "vlog -work m2mOvm +define+M2M_USE_OVM +incdir+$srcSVPath/ovm_original/src $srcSVPath/ovm_original/src/ovm_pkg.sv\n\n"; 00116 00117 00118 //copy standard VHDL files and add the line in the script to compile them 00119 FileUtils.copyFile(libDir + "pkg_definition.vhd", vhdlDir + "pkg_definition.vhd"); 00120 FileUtils.copyFile(libDir + "pkg_cellule.vhd", vhdlDir + "pkg_cellule.vhd"); 00121 FileUtils.copyFile(libDir + "misc.vhd", vhdlDir + "misc.vhd"); 00122 FileUtils.copyFile(libDir + "fifo_std.vhd", vhdlDir + "fifo_std.vhd"); 00123 script += "vcom -work \"$workPath\" \"$srcVHDLPath/pkg_definition.vhd\" \"$srcVHDLPath/fifo_std.vhd\" \"$srcVHDLPath/pkg_cellule.vhd\" \"$srcVHDLPath/misc.vhd\"\n"; 00124 //compile other VHDL files 00125 Vector<Element> body = this.top.getBody(); 00126 00127 @SuppressWarnings("unchecked") 00128 Vector<Element> bodyclone = (Vector<Element>) body.clone(); 00129 Vector<Element> operations = findOperations(bodyclone); 00130 //delete same elements to keep just one, it avoids to compile n-times the same file 00131 for (int i = 0; i < operations.size() - 1; i++) { 00132 for (int j = i + 1; j < operations.size(); j++) { 00133 if ((operations.elementAt(i) instanceof Operation) && (operations.elementAt(j) instanceof Operation) && 00134 !(operations.elementAt(i) instanceof Assignment) && !(operations.elementAt(j) instanceof Assignment)) { 00135 Operation op1 = (Operation)operations.elementAt(i); 00136 Operation op2 = (Operation)operations.elementAt(j); 00137 if (op1.getBlock().entityName().equalsIgnoreCase(op2.getBlock().entityName())) { 00138 operations.remove(j); 00139 j--; 00140 } 00141 } 00142 } 00143 } 00144 00145 //find each operation and add it in the compile script 00146 for (Element el : operations) { 00147 if (el instanceof Operation && !(el instanceof Assignment)) { 00148 Operation op = (Operation) el; 00149 BuildingBlock block = op.getBlock(); 00150 script += "vcom -work \"$workPath\""; 00151 //copy and add dependent files 00152 if (block.dependentFiles()!=null) 00153 for (int i = 0; i < block.dependentFiles().size(); i++) { 00154 FileUtils.copyFile(libDir + block.dependentFiles().get(i), vhdlDir + block.dependentFiles().get(i)); 00155 script += " \"$srcVHDLPath/" + block.dependentFiles().get(i) + "\""; 00156 } 00157 //copy and add the operation block file 00158 FileUtils.copyFile(libDir + block.vhdlFileName(), vhdlDir + block.vhdlFileName()); 00159 script += " \"$srcVHDLPath/" + block.vhdlFileName() + "\""; 00160 //add the wrapper file 00161 script += " \"$srcVHDLPath/wrapper_" + block.entityName() + ".vhd\"\n"; 00162 } 00163 } 00164 00165 //find loop and add it to the compile script 00166 for (Element el : this.top.getBody()) { 00167 if (el instanceof LoopFor) { 00168 LoopFor loop = (LoopFor) el; 00169 script += "vcom -work \"$workPath\" \"$srcVHDLPath/log_pkg.vhd\""; 00170 script += " \"$srcVHDLPath/loop_" + loop.getName() + ".vhd\"\n"; 00171 //copy necessary files 00172 FileUtils.copyFile(libDir + "log_pkg.vhd", vhdlDir + "log_pkg.vhd"); 00173 } 00174 } 00175 00176 //copy the necessary files for fifo, if there are used 00177 if (project.getUseFifo()) { 00178 if (fifoType.equalsIgnoreCase("Altera")) { 00179 File alteraDir = new File(vhdlDir+"altera_mf"); 00180 alteraDir.mkdir(); 00181 FileUtils.copyFile(libDir + "log_pkg.vhd", vhdlDir + "log_pkg.vhd"); 00182 FileUtils.copyFile(libDir + "fifo_Altera.vhd", vhdlDir + "fifo_Altera.vhd"); 00183 FileUtils.copyFile(libDir + "altera_mf/altera_mf.vhd", vhdlDir + "altera_mf/altera_mf.vhd"); 00184 FileUtils.copyFile(libDir + "altera_mf/altera_mf_components.vhd", vhdlDir + "altera_mf/altera_mf_components.vhd"); 00185 script += "vmap altera_mf work\n"; 00186 script += "vcom -work \"$workPath\" \"$srcVHDLPath/log_pkg.vhd\"\n"; 00187 script += "vcom -work altera_mf \"$srcVHDLPath/altera_mf/altera_mf_components.vhd\" \"$srcVHDLPath/altera_mf/altera_mf.vhd\"\n"; 00188 script += "vcom -work \"$workPath\" \"$srcVHDLPath/fifo_Altera.vhd\"\n"; 00189 } else if (fifoType.equalsIgnoreCase("Xilinx")) { 00190 File xilinxDir = new File(vhdlDir+"XilinxCoreLib"); 00191 xilinxDir.mkdir(); 00192 FileUtils.copyFile(libDir + "XilinxCoreLib/fifo_generator_v4_3.vhd", vhdlDir + "XilinxCoreLib/fifo_generator_v4_3.vhd"); 00193 FileUtils.copyFile(libDir + "XilinxCoreLib/iputils_conv.vhd", vhdlDir + "XilinxCoreLib/iputils_conv.vhd"); 00194 FileUtils.copyFile(libDir + "XilinxCoreLib/iputils_misc.vhd", vhdlDir + "XilinxCoreLib/iputils_misc.vhd"); 00195 FileUtils.copyFile(libDir + "XilinxCoreLib/iputils_std_logic_arith.vhd", vhdlDir + "XilinxCoreLib/iputils_std_logic_arith.vhd"); 00196 FileUtils.copyFile(libDir + "XilinxCoreLib/iputils_std_logic_unsigned.vhd", vhdlDir + "XilinxCoreLib/iputils_std_logic_unsigned.vhd"); 00197 FileUtils.copyFile(libDir + "fifo_Xilinx.vhd", vhdlDir + "fifo_Xilinx.vhd"); 00198 script += "vmap XilinxCoreLib work\n"; 00199 script += "vcom -work XilinxCoreLib \"$srcVHDLPath/XilinxCoreLib/iputils_conv.vhd\"" + 00200 " \"$srcVHDLPath/XilinxCoreLib/iputils_misc.vhd\"" + 00201 " \"$srcVHDLPath/XilinxCoreLib/iputils_std_logic_arith.vhd\"" + 00202 " \"$srcVHDLPath/XilinxCoreLib/iputils_std_logic_unsigned.vhd\"" + 00203 " \"$srcVHDLPath/XilinxCoreLib/fifo_generator_v4_3.vhd\"\n"; 00204 script += "vcom -work \"$workPath\" \"$srcVHDLPath/fifo_Xilinx.vhd\"\n"; 00205 } else { 00206 FileUtils.copyFile(libDir + "fifo_std.vhd", vhdlDir + "fifo_std.vhd"); 00207 script += "vcom -work \"$workPath\" \"$srcVHDLPath/fifo_std.vhd\"\n"; 00208 } 00209 } 00210 00211 //if there is a loop for in the design 00212 if (loopFor) { 00213 FileUtils.copyFile(libDir + "Init_Input.vhd", vhdlDir + "Init_Input.vhd"); 00214 FileUtils.copyFile(libDir + "Counter_Wr.vhd", vhdlDir + "Counter_Wr.vhd"); 00215 FileUtils.copyFile(libDir + "Counter_Rd.vhd", vhdlDir + "Counter_Rd.vhd"); 00216 FileUtils.copyFile(libDir + "Read_Result.vhd", vhdlDir + "Read_Result.vhd"); 00217 FileUtils.copyFile(libDir + "Memory.vhd", vhdlDir + "Memory.vhd"); 00218 script += "vcom -work \"$workPath\" \"$srcVHDLPath/Init_Input.vhd\" \"$srcVHDLPath/Counter_Rd.vhd\" \"$srcVHDLPath/Read_Result.vhd\" \"$srcVHDLPath/Memory.vhd\" \"$srcVHDLPath/Counter_Wr.vhd\"\n"; 00219 if (fifoType.equalsIgnoreCase("Altera")) { 00220 File alteraDir = new File(vhdlDir+"altera_mf"); 00221 alteraDir.mkdir(); 00222 FileUtils.copyFile(libDir + "log_pkg.vhd", vhdlDir + "log_pkg.vhd"); 00223 FileUtils.copyFile(libDir + "fifo_Altera.vhd", vhdlDir + "fifo_Altera.vhd"); 00224 FileUtils.copyFile(libDir + "altera_mf/altera_mf.vhd", vhdlDir + "altera_mf/altera_mf.vhd"); 00225 FileUtils.copyFile(libDir + "altera_mf/altera_mf_components.vhd", vhdlDir + "altera_mf/altera_mf_components.vhd"); 00226 script += "vmap altera_mf work\n"; 00227 script += "vcom -work \"$workPath\" \"$srcVHDLPath/log_pkg.vhd\"\n"; 00228 script += "vcom -work altera_mf \"$srcVHDLPath/altera_mf/altera_mf_components.vhd\" \"$srcVHDLPath/altera_mf/altera_mf.vhd\"\n"; 00229 script += "vcom -work \"$workPath\" \"$srcVHDLPath/fifo_Altera.vhd\"\n"; 00230 } else if (fifoType.equalsIgnoreCase("Xilinx")) { 00231 File xilinxDir = new File(vhdlDir+"XilinxCoreLib"); 00232 xilinxDir.mkdir(); 00233 FileUtils.copyFile(libDir + "XilinxCoreLib/fifo_generator_v4_3.vhd", vhdlDir + "XilinxCoreLib/fifo_generator_v4_3.vhd"); 00234 FileUtils.copyFile(libDir + "XilinxCoreLib/iputils_conv.vhd", vhdlDir + "XilinxCoreLib/iputils_conv.vhd"); 00235 FileUtils.copyFile(libDir + "XilinxCoreLib/iputils_misc.vhd", vhdlDir + "XilinxCoreLib/iputils_misc.vhd"); 00236 FileUtils.copyFile(libDir + "XilinxCoreLib/iputils_std_logic_arith.vhd", vhdlDir + "XilinxCoreLib/iputils_std_logic_arith.vhd"); 00237 FileUtils.copyFile(libDir + "XilinxCoreLib/iputils_std_logic_unsigned.vhd", vhdlDir + "XilinxCoreLib/iputils_std_logic_unsigned.vhd"); 00238 FileUtils.copyFile(libDir + "fifo_Xilinx.vhd", vhdlDir + "fifo_Xilinx.vhd"); 00239 script += "vmap XilinxCoreLib work\n"; 00240 script += "vcom -work XilinxCoreLib \"$srcVHDLPath/XilinxCoreLib/iputils_conv.vhd\"" + 00241 " \"$srcVHDLPath/XilinxCoreLib/iputils_misc.vhd\"" + 00242 " \"$srcVHDLPath/XilinxCoreLib/iputils_std_logic_arith.vhd\"" + 00243 " \"$srcVHDLPath/XilinxCoreLib/iputils_std_logic_unsigned.vhd\"" + 00244 " \"$srcVHDLPath/XilinxCoreLib/fifo_generator_v4_3.vhd\"\n"; 00245 script += "vcom -work \"$workPath\" \"$srcVHDLPath/fifo_Xilinx.vhd\"\n"; 00246 } else { 00247 FileUtils.copyFile(libDir + "fifo_std.vhd", vhdlDir + "fifo_std.vhd"); 00248 script += "vcom -work \"$workPath\" \"$srcVHDLPath/fifo_std.vhd\"\n"; 00249 } 00250 } 00251 00252 //script += "vcom -work \"$workPath\" \"$Math2Mat_srcVHDL/fifo_std.vhd\"\n"; 00253 00254 script += "vcom -work \"$workPath\" \"$srcVHDLPath/" + this.top.getName()+".vhd\"\n\n"; 00255 // script += "vcom -work \"$workPath\" \"" + outFile.replace("\\", "/") + "\"\n\n"; 00256 //compile System verilog files 00257 // script += "vlog -work \"$workPath\" \"$srcSVPath/M2M_harness.sv\" \"$srcSVPath/M2M_tb.sv\"\n"; 00258 00259 00260 if (ExternalToolsProperties.getReference().getSimulationFiles()==ExternalToolsProperties.SIMULATIONFILES_UVM) { 00261 script += "vlog -work \"$workPath\" +incdir+$srcSVPath -L m2mUvm \"$srcSVPath/M2M_harness.sv\" \"$srcSVPath/M2M_tb.sv\"\n"; 00262 //launch simulation 00263 script += "vsim -vopt +incdir+\"$srcSVPath\" -L m2mUvm work.M2M_TB work.M2M_harness\n"; 00264 } 00265 else { 00266 script += "vlog -work \"$workPath\" +incdir+$srcSVPath +define+M2M_USE_OVM -L m2mOvm \"$srcSVPath/M2M_harness.sv\" \"$srcSVPath/M2M_tb.sv\"\n"; 00267 //launch simulation 00268 script += "vsim -vopt +incdir+\"$srcSVPath\" -L m2mOvm work.M2M_TB work.M2M_harness\n"; 00269 } 00270 00271 script += "run -all"; 00272 return script; 00273 } 00274 00275 private Vector<Element> findOperations(Vector<Element> elements) throws VHDLException { 00276 Vector<Element> operations = new Vector<Element>(); 00277 00278 for (Element el : elements) { 00279 if (el instanceof Function) { 00280 if (el instanceof IfThenElse) { 00281 operations.addAll(findOperations(((IfThenElse)el).getBodyFalse())); 00282 operations.addAll(findOperations(((IfThenElse)el).getBodyTrue())); 00283 operations.addAll(findOperations(((IfThenElse)el).getCond())); 00284 } else if (el instanceof LoopFor) { 00285 operations.addAll(findOperations(((LoopFor)el).getBody())); 00286 operations.add(((LoopFor)el).getIterOperation()); 00287 loopFor = true; 00288 } 00289 } else if (el instanceof Operation && !(el instanceof Assignment)) { 00290 Operation op = (Operation) el; 00291 BuildingBlock blockOp = op.getBlock(); 00292 if (blockOp == null) { 00293 throw new VHDLException("No material description for the operation : " + 00294 op.getName()); 00295 } else { 00296 operations.add(op); 00297 for (int j = 0; j < blockOp.nbInputs(); j++) { 00298 if (op.getInputAt(j) instanceof Operation) { 00299 Vector<Element> item = new Vector<Element>(); 00300 item.add(op.getInputAt(j)); 00301 operations.addAll(findOperations(item)); 00302 } 00303 } 00304 } 00305 } 00306 } 00307 return operations; 00308 } 00309 00310 }