Advertisement
Wistaro

VHDL - TestBench Generator

Jan 27th, 2020
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.92 KB | None | 0 0
  1. <?php
  2. //VHDL testbench autogenerator
  3. // Developped by William Romiguieres
  4.  
  5. $regexPortDetect = '/port\([0-9a-zA-Z :_; \r \t \n\(]+/m';;
  6. $regexLibrary = '/Library ([a-zA-Z0-9]+)/m';
  7. $regexEntityName = '/entity ([a-zA-Z0-9_]+)/m';
  8. $regexEachPort = '/port\(';
  9.  
  10. session_start();
  11. $content = htmlspecialchars($_POST['code_input']);
  12.  
  13. //fix issue of multiply detection
  14. $count = null;
  15. $content = preg_replace('/([0-9]+)[ ]+downto[ ]+([0-9 ]+)\\)/m', '$1 downto $2', $content, -1, $count);
  16.  
  17. preg_match_all($regexPortDetect, $content, $matchPort, PREG_SET_ORDER, 0);
  18. $firstPortDeclaration = $matchPort[0][0].');';
  19.  
  20. $firstPortDeclaration = str_replace("\n","",$firstPortDeclaration);
  21. $firstPortDeclaration = str_replace("\r","",$firstPortDeclaration);
  22. $firstPortDeclaration = str_replace("\t","",$firstPortDeclaration);
  23. $firstPortDeclaration = trim($firstPortDeclaration);
  24.  
  25. $nb_io = substr_count($firstPortDeclaration, ';');
  26.  
  27. for ($i=0; $i < $nb_io; $i++) {
  28.     $regexEachPort.='(([\w ]+)[: ]+(out|in|buffer)([ a-zA-Z0-9_\(\)]+));';
  29. }
  30. $regexEachPort = substr($regexEachPort, 0, -1);
  31. $regexEachPort.='/m';
  32.  
  33. preg_match_all($regexLibrary, $content, $matchLib, PREG_SET_ORDER, 0);
  34. preg_match_all($regexEntityName, $content, $matchEntityName, PREG_SET_ORDER, 0);
  35. preg_match($regexEachPort, $firstPortDeclaration, $matchEachPort, PREG_OFFSET_CAPTURE, 0);
  36.  
  37. $library = $matchLib[0][1];
  38. $entityName = $matchEntityName[0][1];
  39.  
  40. /* Extact top ports and store them in an array */
  41. /*Construct port list*/
  42.  
  43. $portList = array();
  44. $cpt_port = 0;
  45.  
  46. for ($i=1; $i <= 4*$nb_io; $i = $i+4) {
  47.    $cpt_port++;
  48.    $dataPortExtracted = explode(':', trim($matchEachPort[$i][0]));
  49.    $dataPortExtracted2 = explode(' ',trim($dataPortExtracted[1]));
  50.  
  51.    $dataPortExtracted2 = cleanTab($dataPortExtracted2);
  52.    $portList[$cpt_port- 1]['name'] = $dataPortExtracted[0];
  53.    $portList[$cpt_port - 1]['direction'] = $dataPortExtracted2[0];
  54.  
  55.    if(strstr($dataPortExtracted2[1], 'std_logic_vector')){
  56.       $portList[$cpt_port - 1]['type'] = $dataPortExtracted2[1].' '.$dataPortExtracted2[2].' '.$dataPortExtracted2[3];
  57.    }else{
  58.       $portList[$cpt_port - 1]['type'] = $dataPortExtracted2[1];
  59.    }
  60.  
  61.    if(substr($portList[$cpt_port - 1]['type'], -1, 1) != ')'){
  62.       $portList[$cpt_port - 1]['type'].=')';
  63.    }
  64.    
  65.    
  66. }
  67. $cpt = 0;
  68.  
  69. $output = "-------------------------------------------------------------\n";
  70. $output .= "-- Auto-generated Test Bench\n";
  71. $output.="-- A tool by William Romiguieres\n";
  72. $output.="-- Thanks you for using my tool!\n";
  73. $output.="---------------------------------------------------------------";
  74. $output.="\n\n";
  75. $output.="--Extacted infos from your file:\n";
  76. $output.="--> Used Library: ".$library."\n";
  77. $output.="--> Entity name: ".$entityName."\n";
  78. $output.="--> Number of IO detected: ".$nb_io."\n\n\n";
  79. $output.="Library ".$library."\n\n";
  80. $output.="entity tb_".$entityName." is\n";
  81. $output.="end tb_".$entityName.";\n\n";
  82. $output.="architecture v1 of tb_".$entityName." is\n\n";
  83. $output.="-- component declaration for ".$entityName."\n";
  84. $output.="component ".$entityName."\n port(\n";
  85. foreach ($portList as $key => $value) {
  86.  
  87.    if(!strstr($value['type'], 'std_logic_vector') && $cpt != count($portList) - 1 && substr($value['type'], -1) != ';'){
  88.       $value['type'] = substr_replace($value['type'], ';', -1);
  89.    }
  90.    $output.= $value['name']." : ".$value['direction']." ".$value['type']."\n";  
  91.    $cpt++;
  92. }
  93. $cpt = 0;
  94. $output.=");\n\n";
  95. $output.="-- Signal Generation\n";
  96. foreach ($portList as $key => $value) {
  97.  
  98.    if(!strstr($value['type'], 'std_logic_vector') && substr($value['type'], -1) != ';'){
  99.       $value['type'] = substr_replace($value['type'], ' ', -1);
  100.    }
  101.  
  102.  
  103.    $output.= "signal int_".$value['name']." : ".$value['type'];
  104.  
  105.    if($value['direction'] == 'in'){
  106.       if(strstr($value['type'], 'std_logic_vector')){
  107.          $output.=" := (others => '0')";
  108.       }else{
  109.          $output.=" := '0'";
  110.       }
  111.    }
  112.    $output.=";\n";
  113.    $cpt++;
  114. }
  115.  
  116. $output.="\n--Clock signal definition\n";
  117. $output.="constant clk_period : time  := 1us;\n\n";
  118.  
  119. $output.="begin\n";
  120. $cpt = 0;
  121. $output.="UUT_".$entityName." : ".$entityName." Port map(\n";
  122. foreach ($portList as $key => $value) {
  123.    $output.="\t".$value['name']." => int_".$value['name'];
  124.  
  125.    if($cpt != count($portList) - 1){
  126.       $output.=",\n";
  127.    }else{
  128.       $output.="\n";
  129.    }
  130.    $cpt++;
  131. }
  132. $output.=");\n";
  133.  
  134. $output.="end v1;\n";
  135.  
  136. $_SESSION['result'] = $output;
  137.  
  138. header('location:result.php');
  139.  
  140. function debug($string){
  141.    echo '<pre>';
  142.    print_r($string);
  143.    echo '</pre>';
  144. }
  145.  
  146. function cleanTab($array){
  147.    foreach ($array as $k => $v) {
  148.       if (checkEmptyStr($v)){
  149.          unset($array[$k]);
  150.          $array = array_slice($array,0);
  151.       }
  152.          
  153.    }
  154.    return $array;
  155. }
  156. function checkEmptyStr($string){
  157.    if(strlen(str_replace(' ', '',$string)) == 0){
  158.       return true;
  159.    }else{
  160.       return false;
  161.    }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement