Math2mat
|
00001 00020 package m2m.backend.processing; 00021 00022 import java.io.File; 00023 import java.io.FileNotFoundException; 00024 import java.io.FileReader; 00025 import java.io.FileWriter; 00026 import java.io.IOException; 00027 00028 00029 import m2m.backend.buildingblocks.BuildingBlock.NumType; 00030 import m2m.backend.project.ExternalToolsProperties; 00031 import m2m.backend.project.M2MProject; 00032 import m2m.backend.structure.Element; 00033 import m2m.backend.structure.Function; 00034 import m2m.backend.utils.FileUtils; 00035 00036 public class SystemVerilogCreator { 00037 00038 private Function top; 00039 00040 public SystemVerilogCreator (Function top) { 00041 this.top = top; 00042 } 00043 00044 public boolean createSystemVerilog(M2MProject project) { 00045 00046 FileUtils.copyFile(new File(M2MProject.getLibPath()+"/SV/").getAbsolutePath(), project.getSVPath(),".svn"); 00047 00048 String harnessName = project.getSVPath()+"/M2M_harness.sv"; 00049 String commonName = project.getSVPath()+"/common.sv"; 00050 File harnessFile = new File(harnessName); 00051 File commonFile = new File(commonName); 00052 int instanceIndex = 0; 00053 //instance the DUT in the harness file 00054 if ((instanceIndex = findString("<instance DUT>", harnessFile)) == -1) 00055 return false; 00056 else { 00057 String instance = instanceDUT(); 00058 writeFile(instanceIndex, instance, harnessFile); 00059 } 00060 //create the common file with all the necessary constants 00061 String constants = createConstants(project); 00062 FileWriter fw; 00063 try { 00064 fw = new FileWriter(commonFile); 00065 fw.write(constants); 00066 fw.close(); 00067 } catch (IOException e) { 00068 e.printStackTrace(); 00069 } 00070 return true; 00071 } 00072 00073 private String createConstants(M2MProject project) { 00074 00075 00076 // double period = 0.0000001f; 00077 // NumberFormat formatter = new DecimalFormat("#0.00000000000000000000000000000000000000000000000000000000000#"); 00078 // if (this.simProps.getSimulationStimFrequency() != 0) 00079 // period = 1/this.simProps.getSimulationStimFrequency(); 00080 00081 String sConstants = new String(); 00082 sConstants += "`define NUMBER_BIT_PRECISION " + project.getProperties().getSimulationProperties().getSimulationCalcPrecision() + "\n"; 00083 sConstants += "`define INACTIVITY_TIMEOUT " + project.getProperties().getSimulationProperties().getInactivityTimeout() + "\n"; 00084 sConstants += "`define SYSTEM_FREQ " + project.getProperties().getSimulationProperties().getSystemFrequency() + "\n"; 00085 sConstants += "`define INPUT_FREQ " + project.getProperties().getSimulationProperties().getInputFrequency() + "\n"; 00086 sConstants += "`define OUTPUT_FREQ " + project.getProperties().getSimulationProperties().getOutputFrequency() + "\n"; 00087 sConstants += "`define M2M_BUS_SIZE " + NumType.getDataSize(project.getOptimisationProperties().getOptimisationDataType()) + "\n\n"; 00088 sConstants += "`define CLOCK_TICKS 10\n\n"; 00089 sConstants += "`define NUMBER_OF_INPUTS " + this.top.getInput().size() + "\n"; 00090 sConstants += "`define NUMBER_OF_DUT_INPUTS " + this.top.getInput().size() + "\n"; 00091 sConstants += "`define READ_INPUT_WAY 1\n"; 00092 sConstants += "bit INPUT_PARALLEL [1:`NUMBER_OF_INPUTS] = '{"; 00093 for (int i = 0; i < this.top.getInput().size(); i++) { 00094 if ( i == 0) 00095 sConstants += "0"; 00096 else 00097 sConstants += ", 0"; 00098 } 00099 sConstants += "};\n\n"; 00100 sConstants += "`define NUMBER_OF_OUTPUTS " + this.top.getOutput().size() + "\n"; 00101 sConstants += "`define NUMBER_OF_DUT_OUTPUTS " + this.top.getOutput().size() + "\n"; 00102 sConstants += "`define READ_OUTPUT_WAY 1\n"; 00103 sConstants += "bit OUTPUT_PARALLEL [1:`NUMBER_OF_OUTPUTS] = '{"; 00104 for (int i = 0; i < this.top.getOutput().size(); i++) { 00105 if ( i == 0) 00106 sConstants += "0"; 00107 else 00108 sConstants += ", 0"; 00109 } 00110 sConstants += "};\n\n"; 00111 sConstants += "`define NUMBER_OF_INTERNALS " + this.top.getMonitoringElement().size() + "\n"; 00112 sConstants += "`define NUMBER_OF_DUT_INTERNALS " + this.top.getMonitoringElement().size() + "\n"; 00113 sConstants += "`define READ_INTERNAL_WAY 1\n"; 00114 sConstants += "bit INTERNAL_PARALLEL [1:`NUMBER_OF_INTERNALS]"; 00115 if(this.top.getMonitoringElement().size() != 0) 00116 { 00117 sConstants += " = '{"; 00118 for (int i = 0; i < this.top.getMonitoringElement().size(); i++) { 00119 if ( i == 0) 00120 sConstants += "0"; 00121 else 00122 sConstants += ", 0"; 00123 } 00124 sConstants += "}"; 00125 } 00126 sConstants += ";\n"; 00127 00128 return sConstants; 00129 } 00130 00131 private String instanceDUT() { 00132 String sInstance = new String(); 00133 sInstance += this.top.getName() + " DUT(\n"; 00134 sInstance += tab(1) + ".clock_i" + tab(1) + "(CLK),\n"; 00135 sInstance += tab(1) + ".reset_i" + tab(1) + "(reset),\n"; 00136 for (int i = 0; i < this.top.getInput().size(); i++) { 00137 Element el = this.top.getInput().get(i); 00138 if (el.getName().isEmpty()) { 00139 System.err.println("No name found for the DUT input n�" + (i + 1)); 00140 } 00141 sInstance += tab(1) + "." + el.getName() + "_i" + tab(1) + "(M2M_in_intf.Input_Value[" + (i + 1) + "]),\n"; 00142 sInstance += tab(1) + "." + el.getName() + "_Valid_i" + tab(1) + "(M2M_in_intf.Input_Valid[" + (i + 1) + "]),\n"; 00143 sInstance += tab(1) + "." + el.getName() + "_Ready_o" + tab(1) + "(M2M_in_intf.Input_Ready[" + (i + 1) + "]),\n"; 00144 } 00145 for (int i = 0; i < this.top.getOutput().size(); i++) { 00146 sInstance += tab(1) + "." + this.top.getOutput().get(i).getName() + "_o" + tab(1) + "(M2M_out_intf.Result[" + (i + 1) + "]),\n"; 00147 sInstance += tab(1) + "." + this.top.getOutput().get(i).getName() + "_Valid_o" + tab(1) + "(M2M_out_intf.Result_Valid[" + (i + 1) + "]),\n"; 00148 if (i == this.top.getOutput().size() - 1) 00149 sInstance += tab(1) + "." + this.top.getOutput().get(i).getName() + "_Ready_i" + tab(1) + "(M2M_in_intf.Result_Ready["+(i+1)+"])\n"; 00150 else 00151 sInstance += tab(1) + "." + this.top.getOutput().get(i).getName() + "_Ready_i" + tab(1) + "(M2M_in_intf.Result_Ready["+(i+1)+"]),\n"; 00152 } 00153 sInstance += ");\n"; 00154 00155 /* Instance internal Dut monitoring signal */ 00156 00157 if (ExternalToolsProperties.getReference().getSimulator()<2) { 00158 for (int i = 0; i < this.top.getMonitoringElement().size(); i++) 00159 { 00160 Element el = this.top.getMonitoringElement().get(i); 00161 00162 sInstance += tab(1) + "initial begin\n"; 00163 sInstance += tab(2) + "$init_signal_spy(\"DUT/" + el.getName() + "_s\"\t\t\t\t, \"" + "Internal_Data[" + (i+1) + "]\" ,1,1);\n"; 00164 sInstance += tab(2) + "$init_signal_spy(\"DUT/" + el.getName() + "_Ready_s\"\t\t\t, \"" + "Internal_Data_Ready[" + (i+1) + "]\" ,1,1);\n"; 00165 sInstance += tab(2) + "$init_signal_spy(\"DUT/" + el.getName() + "_Valid_s\"\t\t\t, \"" + "Internal_Data_Valid[" + (i+1) + "]\",1,1);\n"; 00166 00167 sInstance += tab(1) + "end\n\n"; 00168 } 00169 00170 sInstance += tab(1) + "assign M2M_int_intf.Data = Internal_Data;\n"; 00171 sInstance += tab(1) + "assign M2M_int_intf.Data_Ready = Internal_Data_Ready;\n"; 00172 sInstance += tab(1) + "assign M2M_int_intf.Data_Valid = Internal_Data_Valid;\n"; 00173 00174 } 00175 else { 00176 for (int i = 0; i < this.top.getMonitoringElement().size(); i++) 00177 { 00178 Element el = this.top.getMonitoringElement().get(i); 00179 00180 sInstance += tab(1) + "assign " + "M2M_int_intf.mon_mp.Data[" + (i+1) + "]\t\t\t = " + "DUT." + el.getName() + ";\n"; 00181 sInstance += tab(1) + "assign " + "M2M_int_intf.mon_mp.Data_Ready[" + (i+1) + "]\t = " + "DUT." + el.getName() + "_Ready" + ";\n"; 00182 sInstance += tab(1) + "assign " + "M2M_int_intf.mon_mp.Data_Valid[" + (i+1) + "]\t = " + "DUT." + el.getName() + "_Valid" + ";\n"; 00183 } 00184 } 00185 00186 return sInstance; 00187 } 00188 00189 private void writeFile(int instanceIndex, String instance, File file) { 00190 try { 00191 FileReader fr = new FileReader(file); 00192 String beginOfFile = new String(); 00193 String endOfFile = new String(); 00194 int readChar; 00195 for (int i = 0; i <= instanceIndex; i++) { 00196 beginOfFile += (char)fr.read(); 00197 } 00198 while ((readChar = fr.read()) != -1) { 00199 endOfFile += (char)readChar; 00200 } 00201 FileWriter fw = new FileWriter(file); 00202 fw.write(beginOfFile); 00203 fw.write(instance); 00204 fw.write(endOfFile); 00205 fw.close(); 00206 } catch (IOException e) { 00207 e.printStackTrace(); 00208 System.out.println(e.getMessage()); 00209 } 00210 } 00211 00212 private int findString (String toFind, File file) { 00213 int readChar; 00214 String readText = new String(); 00215 FileReader fr; 00216 try { 00217 fr = new FileReader(file); 00218 while ((readChar = fr.read()) != -1) { 00219 readText += (char)readChar; 00220 } 00221 fr.close(); 00222 } catch (FileNotFoundException e) { 00223 e.printStackTrace(); 00224 return -1; 00225 } catch (IOException e) { 00226 e.printStackTrace(); 00227 return -1; 00228 } 00229 00230 return readText.indexOf(toFind) + toFind.length(); 00231 } 00232 00233 private String tab(int nb) { 00234 String s = new String(); 00235 for (int i = 0; i < nb; i++) { 00236 s += "\t"; 00237 } 00238 return s; 00239 } 00240 00241 }