Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Reading block

by ravi030 (Initiate)
on Jul 31, 2008 at 06:13 UTC ( [id://701343]=perlquestion: print w/replies, xml ) Need Help??

ravi030 has asked for the wisdom of the Perl Monks concerning the following question:

HI everyone,This is the block which i have to read
input [31:0] ucast_mem_wdata; input [31:0] ucast_mem_wren; input ucast_mem_wr; input ucast_mem_rd; output [31:0] ucast_mem_rdata; wire [BMU_MEM_ADDR_BITS-1:0] mem_addr; always @ (posedge sys_clk +or negedge sys_reset_n) begin if (!sys_reset_n) ucast_int_mem_rd_r <= 0; else ucast_int_mem_rd_r <= ucast_int_mem_rd & !ucast_access; end endmodule
This is the file i have to read From the above file.I have to read inputs and output and always blcoks into differant files I succeed in reading inputs and outputs but i didnt get the logic how to read the entire always block into the file

Replies are listed 'Best First'.
Re: Reading block
by shoness (Friar) on Jul 31, 2008 at 08:28 UTC
    You will suffer greatly if you try to write your own general Verilog parser.

    There are modules on CPAN that can do it for you. Maybe Verilog::Parser or Verilog::SigParser?

    If you need to parse some auto-generated file or a restricted subset of the language, you stand a better chance. Is your example data auto-generated? Who but a machine would omit the carriage return following the ";" before the always block?

    I don't know what you want to DO with the always block (and the rest of that Verilog code). What are you building in Perl and what data do you want to collect from the code to make it happen?

    For your example code block, it is straightforward to pull out the clock, the reset (and it's polarity) and the expression this DFF uses. The next always block might be a latch, or a flop with synchronous reset, or no reset, or have multiple signals, or use SystemVerilog always_ff, or be an entire state-machine, or call a task, etc. In short, you need a language parser.

    My advice is to use, or build on, what exists if possible. If not, then restrict what you are doing and start small.

Re: Block reading
by dHarry (Abbot) on Jul 31, 2008 at 06:37 UTC

    Use the diamond operator to read the file/data. When a line starts with "//always" it triggers the begin of the block. You can use a Regular Expression to test this. Keep reading until you encounter an "//end" it's the end of the block. This should be straightforward.

    Update

    Something you can play with. You'll get the idea.

    use strict; use warnings; while (my $line=<DATA>){ if ($line =~ /\/\/always/){ print "Start of block\n"; } if ($line =~ /end\/\//) { print "End of block\n"; } } __DATA__ input [31:0] ucast_mem_wdata; input [31:0] ucast_mem_wren; input ucast_mem_wr; input ucast_mem_rd; output [31:0] ucast_mem_rdata; wire [BMU_MEM_ADDR_BITS-1:0] mem_addr; //always @ (posedge sys_clk or negedge sys_reset_n) begin if (!sys_reset_n) ucast_int_mem_rd_r <= 0; else ucast_int_mem_rd_r <= ucast_int_mem_rd & !ucast_access; end// this is the always block which i have to read into another +file
Re: Reading block: Verilog parse
by toolic (Bishop) on Jul 31, 2008 at 14:40 UTC
    Assuming you missed my comments in the Chatterbox yesterday (or maybe you chose to ignore them:), I will re-state them here. Since Verilog employs a comlpex syntax, a complex parser is required. Obviously, if you can control the format of the Verilog source code you are trying to parse, you may be able to roll your own simple parser. Since that is unlikely, I whole-heartedly agree with shoness' advice on trying to use the CPAN modules.

    Here are some of the common pitfalls you are facing (and there are many, many more):

    • What if there are ports or always blocks inside comments, both single-line (//) and multi-line (/**/)?
    • What if someone used `include or `define compiler directives to declare ports?
    • What if there are functions declared within a module, which have their own ports?
    • How do you reliably determine the end of an always block? There is no "always_end" construct.
    • How do you deal with IEEE Std 2001 syntax for ports?

    Here are some techniques I have used to overcome some of these issues. You could pre-process the Verilog file to remove all comments using the regex in perlfaq6:

    How do I use a regular expression to strip C style comments from a file?

    This will work in most (but not all) cases.

    If you have access to the Cadence ncverilog simulation tools, you could first compile the Verilog files, then de-compile the files using ncdc. This tool allows for some control over the resulting de-compiled files so that parsing may be simpler. I am not sure if other simulation tools have the same capability.

    If you can edit the Verilog source files, you could embed pragmas in comments such as // always_end -- but that will only help you for future development.

    That being said, here is some EXTREMELY BRITTLE code which works for the Verilog code you provided:

    use strict; use warnings; my @ins; my @outs; my @blocks; my $flag = 0; while (<DATA>) { if (/\binput\b/) { push @ins , $_ } if (/\boutput\b/) { push @outs, $_ } if (/\balways\b/) { s/^.*\b(always)\b/$1/; push @blocks, $_; $flag = 1; next; } if ($flag) { push @blocks, $_; $flag = 0 if (/^\s*$/); # blank line ends always block } } create_file('inputs.v' , @ins); create_file('outputs.v', @outs); create_file('always.v' , @blocks); sub create_file { my $file = shift; open my $fileHandle, '>', $file or die "Unable to create $file: $! +\n"; print $fileHandle $_ for (@_); close $fileHandle or die "Unable to close file $file: $!\n"; } __DATA__ input [31:0] ucast_mem_wdata; input [31:0] ucast_mem_wren; input ucast_mem_wr; input ucast_mem_rd; output [31:0] ucast_mem_rdata; wire [BMU_MEM_ADDR_BITS-1:0] mem_addr; always @ (posedge sys_clk +or negedge sys_reset_n) begin if (!sys_reset_n) ucast_int_mem_rd_r <= 0; else ucast_int_mem_rd_r <= ucast_int_mem_rd & !ucast_access; end endmodule

    Here is the output:

Re: Reading block
by dHarry (Abbot) on Jul 31, 2008 at 06:29 UTC

    I have difficulty understanding this. Could you use some formatting to make the text more readable? See Perl Monks Approved HTML tags for the possibilities.

      Now i have formatted it.Please dont understanding me wrong,here for me all features are looking bit difficult in using them.Now you can have a look at that.Thank you
Re: Reading block
by Anonymous Monk on Jul 31, 2008 at 06:32 UTC
    sample input file
    input [31:0] ucast_mem_wdata; input [31:0] ucast_mem_wren; input ucast_mem_wr; input ucast_mem_rd; output [31:0] ucast_mem_rdata; wire [BMU_MEM_ADDR_BITS-1:0] mem_addr; always @ (posedge sys_clk +or negedge sys_reset_n) begin if (!sys_reset_n) ucast_int_mem_rd_r <= 0; else ucast_int_mem_rd_r <= ucast_int_mem_rd &amp; !ucast_access; end endmodule
    From this what i have to do is,i have to read input,output,always,and initial blocks and have to send them seperate files ie input to input_in,output_out,always to alway_all;

    I am having the pblm at reading always block as it is so many internal blocks

    could u anybody tell me how to read entire always block and write it to the socalled named file THis is the code I tried for input and out put reading

    but i couldnt get any idea regarding the reading of whole always blcok for the above

    your attempt from ravi030's scratchpad

    #!usr/bin/perl use strict; use warnings; open( my $ip_fileh, '<', "D:\\PacketProcessor\\rtl\\common\\gen_sync_fifo.v" ) || die ( "unable to open the file D:\\PacketProcessor\\rtl\\common\\gen_sync_fifo.v\n" ); #open the file while (<$ip_fileh>) { #read the file line by line my $str = $_; #assign to a variable chomp $str; #remove the new line if ( $str =~ m/(Input|output) ((\w+(,\s*\w+)*)|([^\n]*))/i ) { my $type = $1; #get whether input or output my @inp_vals = split /\,\s*/, $2; #split by comma followed +by space if more than one values local $" = "\n"; #to print the array values line by line print "$type\n@inp_vals \n"; #print the output } } __END__

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://701343]
Approved by wfsp
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-25 09:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found