Wistaro

VHDL - Parser by Wistaro

Feb 8th, 2020 (edited)
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.35 KB | None | 0 0
  1. <?php
  2. session_start();
  3. set_error_handler(function($niveau, $message, $fichier, $ligne){
  4.    displayError($message.'\nLine: '.$ligne.'\n Type erreur : [PARSING]');
  5. });
  6.  
  7. $regexPortDetect = '/port[ ]?\([0-9a-zA-Z :_; \r \t \n\(]+/m';;
  8. $regexLibrary = '/(L|l)ibrary ([a-zA-Z0-9]+)/m';
  9. $regexEntityName = '/entity ([a-zA-Z0-9_]+)/m';
  10. $regexEachPort = '/port[ ]?\(';
  11. $inputFreq = (int)$_POST['freq'];
  12. if($inputFreq > 0){
  13.    $timingSimu = round(1 / $inputFreq, 2).'us';
  14. }else{
  15.    $timingSimu = '1us';
  16. }
  17.  
  18. $content = htmlspecialchars($_POST['code_input']);
  19. $count = null;
  20. $content = preg_replace('/([0-9]+)[ ]+downto[ ]+([0-9 ]+)\\)/m', '$1 downto $2', $content, -1, $count);
  21. $content = preg_replace('/\-\-[a-zA-Z0-9 ]+/m', '', $content);
  22.  
  23. preg_match_all($regexPortDetect, $content, $matchPort, PREG_SET_ORDER, 0);
  24.  
  25. if(count($matchPort) == 0){
  26.    displayError("Erreur de lors de lecture de l'entité. Celle-ci est peut-être vide.");
  27. }
  28.  
  29. $firstPortDeclaration = $matchPort[0][0].');';
  30.  
  31. $firstPortDeclaration = str_replace("\n","",$firstPortDeclaration);
  32. $firstPortDeclaration = str_replace("\r","",$firstPortDeclaration);
  33. $firstPortDeclaration = str_replace("\t","",$firstPortDeclaration);
  34. $firstPortDeclaration = trim($firstPortDeclaration);
  35.  
  36. $nb_io = substr_count($firstPortDeclaration, ';');
  37.  
  38. for ($i=0; $i < $nb_io; $i++) {
  39.     $regexEachPort.='(([\w ]+)[: ]+(out|in|buffer)([ a-zA-Z0-9_\(\)]+));';
  40. }
  41. $regexEachPort = substr($regexEachPort, 0, -1);
  42. $regexEachPort.='/m';
  43.  
  44. preg_match_all($regexLibrary, $content, $matchLib, PREG_SET_ORDER, 0);
  45.  
  46. if(count($matchLib) == 0){
  47.    displayError("Impossible d'extraire la librairie utilisée!");
  48. }
  49.  
  50. preg_match_all($regexEntityName, $content, $matchEntityName, PREG_SET_ORDER, 0);
  51.  
  52. if(count($matchEntityName) == 0){
  53.    displayError("Impossible d'extraire le nom de l'entité!");
  54. }
  55.  
  56. preg_match($regexEachPort, $firstPortDeclaration, $matchEachPort, PREG_OFFSET_CAPTURE, 0);
  57.  
  58. if(count($matchEachPort) == 0){
  59.    displayError("Impossible d'extraire la liste des ports d'entrée-sortie! Vérifiez l'absence de commentaire dans la définition des ports de l'entité!");
  60. }
  61.  
  62.  
  63. $library = $matchLib[0][2];
  64. $entityName = $matchEntityName[0][1];
  65.  
  66. /* Extact top ports and store them in an array */
  67. /*Construct port list*/
  68.  
  69. $portList = array();
  70. $cpt_port = 0;
  71.  
  72. for ($i=1; $i <= 4*$nb_io; $i = $i+4) {
  73.    $cpt_port++;
  74.    $dataPortExtracted = explode(':', trim($matchEachPort[$i][0]));
  75.    $dataPortExtracted2 = explode(' ',trim($dataPortExtracted[1]));
  76.  
  77.    $dataPortExtracted2 = cleanTab($dataPortExtracted2);
  78.    $portList[$cpt_port- 1]['name'] = $dataPortExtracted[0];
  79.    $portList[$cpt_port - 1]['direction'] = $dataPortExtracted2[0];
  80.  
  81.    if(strstr($dataPortExtracted2[1], 'std_logic_vector')){
  82.       $portList[$cpt_port - 1]['type'] = $dataPortExtracted2[1].' '.$dataPortExtracted2[2].' '.$dataPortExtracted2[3];
  83.    }else{
  84.       $portList[$cpt_port - 1]['type'] = $dataPortExtracted2[1];
  85.    }
  86.  
  87.    if(substr($portList[$cpt_port - 1]['type'], -1, 1) != ')'){
  88.       $portList[$cpt_port - 1]['type'].=')';
  89.    }
  90. }
  91. $cpt = 0;
  92.  
  93. $output = "-------------------------------------------------------------\n";
  94. $output .= "-- Auto-generated Test Bench\n";
  95. $output.="-- A tool by Wistaro\n";
  96. $output.="-- Thanks you for using my tool!\n";
  97. $output.="---------------------------------------------------------------";
  98. $output.="\n\n";
  99. $output.="--Extacted infos from your file:\n";
  100. $output.="--> Used Library: ".$library."\n";
  101. $output.="--> Entity name: ".$entityName."\n";
  102. $output.="--> Number of IO detected: ".$nb_io."\n\n\n";
  103. $output.="Library ".$library.";\n\n";
  104. $output.="entity tb_".$entityName." is\n";
  105. $output.="end tb_".$entityName.";\n\n";
  106. $output.="architecture v1 of tb_".$entityName." is\n\n";
  107. $output.="-- component declaration for ".$entityName."\n";
  108. $output.="component ".$entityName."\n port(\n";
  109. foreach ($portList as $key => $value) {
  110.  
  111.    if(!strstr($value['type'], 'std_logic_vector') && $cpt != count($portList) - 1 && substr($value['type'], -1) != ';'){
  112.       $value['type'] = substr_replace($value['type'], ';', -1);
  113.    }
  114.    $output.= $value['name']." : ".$value['direction']." ".$value['type']."\n";  
  115.    $cpt++;
  116. }
  117. $cpt = 0;
  118. $clk_name = '';
  119. $output.=");\n\n";
  120. $output.="-- Signal Generation\n";
  121. foreach ($portList as $key => $value) {
  122.  
  123.    if(!strstr($value['type'], 'std_logic_vector') && substr($value['type'], -1) != ';'){
  124.       $value['type'] = substr_replace($value['type'], ' ', -1);
  125.    }
  126.  
  127.    if( strstr(strtolower($value['name']), 'clk') || strstr(strtolower($value['name']), 'clock')) {
  128.       $clk_name = $value['name'];
  129.    }
  130.  
  131.    $output.= "signal int_".$value['name']." : ".$value['type'];
  132.  
  133.    if($value['direction'] == 'in'){
  134.       if(strstr($value['type'], 'std_logic_vector')){
  135.          $output.=" := (others => '0')";
  136.       }else{
  137.          $output.=" := '0'";
  138.       }
  139.    }
  140.    $output.=";\n";
  141.    $cpt++;
  142. }
  143. $output.= "signal int_generated_clock : std_logic := '0';\n";
  144. $output.="\n--Clock signal definition\n";
  145. $output.="constant clk_period : time  := ".$timingSimu.";\n\n";
  146.  
  147. $output.="begin\n";
  148. $cpt = 0;
  149. $output.="UUT_".$entityName." : ".$entityName." Port map(\n";
  150. foreach ($portList as $key => $value) {
  151.  
  152.    if($value['name'] == $clk_name){
  153.       $output.="\t".$value['name']." => int_generated_clock";
  154.    }else{
  155.       $output.="\t".$value['name']." => int_".$value['name'];
  156.    }
  157.    
  158.  
  159.    if($cpt != count($portList) - 1){
  160.       $output.=",\n";
  161.    }else{
  162.       $output.="\n";
  163.    }
  164.    $cpt++;
  165. }
  166. $output.=");\n\n";
  167.  
  168. $output.="int_generated_clock <= NOT int_generated_clock after clk_period/2; \n";
  169.  
  170. $output.="end v1;\n";
  171.  
  172. $_SESSION['result'] = $output;
  173.  
  174. header('location:result.php');
  175.  
  176. function debug($string){
  177.    echo '<pre>';
  178.    print_r($string);
  179.    echo '</pre>';
  180. }
  181.  
  182. function cleanTab($array){
  183.    foreach ($array as $k => $v) {
  184.       if (checkEmptyStr($v)){
  185.          unset($array[$k]);
  186.          $array = array_slice($array,0);
  187.       }
  188.          
  189.    }
  190.    return $array;
  191. }
  192. function checkEmptyStr($string){
  193.    if(strlen(str_replace(' ', '',$string)) == 0){
  194.       return true;
  195.    }else{
  196.       return false;
  197.    }
  198. }
  199. function displayError($message){
  200.    $_SESSION['bkp_code'] = $_POST['code_input'];
  201.    echo '<script>alert("Echec! Le fichier VHDL comporte des erreurs!\nDétails: '.$message.'");';
  202.    echo "document.location.href='index.php'</script>";
  203.    die();
  204. }
Add Comment
Please, Sign In to add comment