Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- set_error_handler(function($niveau, $message, $fichier, $ligne){
- displayError($message.'\nLine: '.$ligne.'\n Type erreur : [PARSING]');
- });
- $regexPortDetect = '/port[ ]?\([0-9a-zA-Z :_; \r \t \n\(]+/m';;
- $regexLibrary = '/(L|l)ibrary ([a-zA-Z0-9]+)/m';
- $regexEntityName = '/entity ([a-zA-Z0-9_]+)/m';
- $regexEachPort = '/port[ ]?\(';
- $inputFreq = (int)$_POST['freq'];
- if($inputFreq > 0){
- $timingSimu = round(1 / $inputFreq, 2).'us';
- }else{
- $timingSimu = '1us';
- }
- $content = htmlspecialchars($_POST['code_input']);
- $count = null;
- $content = preg_replace('/([0-9]+)[ ]+downto[ ]+([0-9 ]+)\\)/m', '$1 downto $2', $content, -1, $count);
- $content = preg_replace('/\-\-[a-zA-Z0-9 ]+/m', '', $content);
- preg_match_all($regexPortDetect, $content, $matchPort, PREG_SET_ORDER, 0);
- if(count($matchPort) == 0){
- displayError("Erreur de lors de lecture de l'entité. Celle-ci est peut-être vide.");
- }
- $firstPortDeclaration = $matchPort[0][0].');';
- $firstPortDeclaration = str_replace("\n","",$firstPortDeclaration);
- $firstPortDeclaration = str_replace("\r","",$firstPortDeclaration);
- $firstPortDeclaration = str_replace("\t","",$firstPortDeclaration);
- $firstPortDeclaration = trim($firstPortDeclaration);
- $nb_io = substr_count($firstPortDeclaration, ';');
- for ($i=0; $i < $nb_io; $i++) {
- $regexEachPort.='(([\w ]+)[: ]+(out|in|buffer)([ a-zA-Z0-9_\(\)]+));';
- }
- $regexEachPort = substr($regexEachPort, 0, -1);
- $regexEachPort.='/m';
- preg_match_all($regexLibrary, $content, $matchLib, PREG_SET_ORDER, 0);
- if(count($matchLib) == 0){
- displayError("Impossible d'extraire la librairie utilisée!");
- }
- preg_match_all($regexEntityName, $content, $matchEntityName, PREG_SET_ORDER, 0);
- if(count($matchEntityName) == 0){
- displayError("Impossible d'extraire le nom de l'entité!");
- }
- preg_match($regexEachPort, $firstPortDeclaration, $matchEachPort, PREG_OFFSET_CAPTURE, 0);
- if(count($matchEachPort) == 0){
- 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é!");
- }
- $library = $matchLib[0][2];
- $entityName = $matchEntityName[0][1];
- /* Extact top ports and store them in an array */
- /*Construct port list*/
- $portList = array();
- $cpt_port = 0;
- for ($i=1; $i <= 4*$nb_io; $i = $i+4) {
- $cpt_port++;
- $dataPortExtracted = explode(':', trim($matchEachPort[$i][0]));
- $dataPortExtracted2 = explode(' ',trim($dataPortExtracted[1]));
- $dataPortExtracted2 = cleanTab($dataPortExtracted2);
- $portList[$cpt_port- 1]['name'] = $dataPortExtracted[0];
- $portList[$cpt_port - 1]['direction'] = $dataPortExtracted2[0];
- if(strstr($dataPortExtracted2[1], 'std_logic_vector')){
- $portList[$cpt_port - 1]['type'] = $dataPortExtracted2[1].' '.$dataPortExtracted2[2].' '.$dataPortExtracted2[3];
- }else{
- $portList[$cpt_port - 1]['type'] = $dataPortExtracted2[1];
- }
- if(substr($portList[$cpt_port - 1]['type'], -1, 1) != ')'){
- $portList[$cpt_port - 1]['type'].=')';
- }
- }
- $cpt = 0;
- $output = "-------------------------------------------------------------\n";
- $output .= "-- Auto-generated Test Bench\n";
- $output.="-- A tool by Wistaro\n";
- $output.="-- Thanks you for using my tool!\n";
- $output.="---------------------------------------------------------------";
- $output.="\n\n";
- $output.="--Extacted infos from your file:\n";
- $output.="--> Used Library: ".$library."\n";
- $output.="--> Entity name: ".$entityName."\n";
- $output.="--> Number of IO detected: ".$nb_io."\n\n\n";
- $output.="Library ".$library.";\n\n";
- $output.="entity tb_".$entityName." is\n";
- $output.="end tb_".$entityName.";\n\n";
- $output.="architecture v1 of tb_".$entityName." is\n\n";
- $output.="-- component declaration for ".$entityName."\n";
- $output.="component ".$entityName."\n port(\n";
- foreach ($portList as $key => $value) {
- if(!strstr($value['type'], 'std_logic_vector') && $cpt != count($portList) - 1 && substr($value['type'], -1) != ';'){
- $value['type'] = substr_replace($value['type'], ';', -1);
- }
- $output.= $value['name']." : ".$value['direction']." ".$value['type']."\n";
- $cpt++;
- }
- $cpt = 0;
- $clk_name = '';
- $output.=");\n\n";
- $output.="-- Signal Generation\n";
- foreach ($portList as $key => $value) {
- if(!strstr($value['type'], 'std_logic_vector') && substr($value['type'], -1) != ';'){
- $value['type'] = substr_replace($value['type'], ' ', -1);
- }
- if( strstr(strtolower($value['name']), 'clk') || strstr(strtolower($value['name']), 'clock')) {
- $clk_name = $value['name'];
- }
- $output.= "signal int_".$value['name']." : ".$value['type'];
- if($value['direction'] == 'in'){
- if(strstr($value['type'], 'std_logic_vector')){
- $output.=" := (others => '0')";
- }else{
- $output.=" := '0'";
- }
- }
- $output.=";\n";
- $cpt++;
- }
- $output.= "signal int_generated_clock : std_logic := '0';\n";
- $output.="\n--Clock signal definition\n";
- $output.="constant clk_period : time := ".$timingSimu.";\n\n";
- $output.="begin\n";
- $cpt = 0;
- $output.="UUT_".$entityName." : ".$entityName." Port map(\n";
- foreach ($portList as $key => $value) {
- if($value['name'] == $clk_name){
- $output.="\t".$value['name']." => int_generated_clock";
- }else{
- $output.="\t".$value['name']." => int_".$value['name'];
- }
- if($cpt != count($portList) - 1){
- $output.=",\n";
- }else{
- $output.="\n";
- }
- $cpt++;
- }
- $output.=");\n\n";
- $output.="int_generated_clock <= NOT int_generated_clock after clk_period/2; \n";
- $output.="end v1;\n";
- $_SESSION['result'] = $output;
- header('location:result.php');
- function debug($string){
- echo '<pre>';
- print_r($string);
- echo '</pre>';
- }
- function cleanTab($array){
- foreach ($array as $k => $v) {
- if (checkEmptyStr($v)){
- unset($array[$k]);
- $array = array_slice($array,0);
- }
- }
- return $array;
- }
- function checkEmptyStr($string){
- if(strlen(str_replace(' ', '',$string)) == 0){
- return true;
- }else{
- return false;
- }
- }
- function displayError($message){
- $_SESSION['bkp_code'] = $_POST['code_input'];
- echo '<script>alert("Echec! Le fichier VHDL comporte des erreurs!\nDétails: '.$message.'");';
- echo "document.location.href='index.php'</script>";
- die();
- }
Add Comment
Please, Sign In to add comment