I have a Verilog line like this:

if (i2_0&!(i2_1)) (posedge i0_0 => (o:1'b1))=(0, 0);

How can I convert it to a line like:

if (i2_0 == 1'b1 & i2_1 == 1'b0) (posedge i0_0 => (o:1'b1))=(0, 0);

Basically I want to search the alphanumeric strings after the first bracket of the if statement and convert each section in between the brackets or the "&"s to their equivalent logic statement.

(i2_0& becomes (i2_0 == 1'b1 &


!(i2_1)) becomes i2_1 == 1'b0)

in the above example.

  • 1
    And why is i2_0& equivalent to i2_0 == 1'b1 &? How did b1 get into this? How does !(i2_1) become i2_1 == 1? That seems like exactly the inverse of what it was. Shouldn't that be i2_1==0? – terdon May 19 '15 at 11:30
  • The second line is a logical equivalent of the first one. 1'b1 means, it's a 1 bit binary variable and takes value 1. Similarly 1'b0 means, it's a binary value with value 0. Hence logical equivalent statement of !(a) is "a == 1'b0" – Pratap May 19 '15 at 17:10
  • Um, in what language? I don't know that syntax. You might want to clarify what language logic this should follow. – terdon May 19 '15 at 17:19
  • The language mentioned is verilog. – Pratap May 19 '15 at 18:31
  • Ah, thanks, when I had first read the question, Verilog was not a link so I assumed it was some kind of logging software. I guess your question is understandable if you're familiar with Verilog, but you might want to define your requirements in a way that people unfamiliar with Verlilog would understand. A lot of text-processing experts won't know what that is. – terdon May 19 '15 at 18:45

According to explanation in comments:

sed "
# for lines which starts with if
    /==/! {
        # add logic statement to first alphanums after ( 
        s/\((\w\+\)/\1 == 1b'1 /
        # add logic statement to second alphanums after & if it is present 
        s/\(&\!\?(\?\w\+\)/\1 == 1b'1 /
        # if ! sign in section replace 1 by 0 at the end of statement 
        s/\!(\([^']*'\)1 )/ \10 /g
" file

Other variant - remove everithing except part which will be modified, change it, than construct full line back:

sed "/^if\b/{
            s/if (.*) //
            s/if (\(.*\)) (.*/\1/
            s/\w\+/& == 1'b1 /g
            s/!(\(.*\)1 )/ \10/g
            s/.*/if (&)/
            s/\n/ /
            }" file
  • Can you please explain the components of the above piece of code? I tried the full one, but unfortunately it's not working for me as such. 1'b1 is the value assigned if the variable is without a "!" (means negation). Similarly '1'b0' is assigned to a variable within "!". E.g. !(i2_1) takes logical equivalent statement as i2_1 == 1'b0... – Pratap May 19 '15 at 17:18
  • @Pratap I have change code up to your comment. Please check – Costas May 19 '15 at 22:45
  • Thanks for the comments with explanation! When I use the full code I am getting error like this. lc-sj1-4894{pratap}470: sed '/^if\b/{s/(\(\w\+\))\?/(\1 == 1b'\''1 / s/\(&!\?\)(\?\(\w\+\))\?/\1\2 == 1b'\''1/ s/!\([^'\'']*'\''\)1/ \10/g}' file > mod_file Error: /bin/sed: -e expression #1, char 38: unknown option to s' ` I could get some output from the first part though: sed '/^if\b/{s/(\(\w\+\))\?/(\1 == 1b'\''1 /g}' source_file > mod_file_p1 But the result is not desired one: I wanted the == 1'b1 conversions to happen only in the first bracket enclosure of if and not other part. – Pratap May 20 '15 at 5:33
  • @Pratap I made ! escaped and exchage single to double quotes – Costas May 20 '15 at 9:03
  • The error still persises as soon as the second and third sed sections are added. lc-sj1-4894{pratap}513: sed "/^if\b/{s/(\(\w\+\))\?/(\1 == 1b'1 / s/\(&\!\?\)(\?\(\w\+\))\?/\1\2 == 1b'1/ s/\!\([^']*'\)1/ \10/g}" source_file > ! ver_file Error: /bin/sed: -e expression #1, char 38: unknown option to s'` and the other problem (tested with the first section only) it modifies the contents outside the bracket of the first if which is error. Eg it should be if (...) (posedge i0_0 => (o:1'b1))=(0, 0); But code gives if (...) (posedge i0_0 == 1b'1 => (o:1'b1))=(0, 0); – Pratap May 20 '15 at 9:34

Maybe something like:

perl -0777 -pe 's{\bif\s*\K\((([^()]++|\((?1)\))*)(?=\))}{
  $& =~ s{([&(])\s*(!?)\s*\(?(\w+\b)\)?(?!\s*(==|'\''))}{
   "$1$3 == 1'\''b".(0+!!$2)}gers}ges'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.