Refer to field and assign some logic to that field

Viewed 78

I have input file with data as

cell   input     out    type      fun            level
AI20   A1,A2,A3    Z    comb    ((A1A2))          2
INV    I1         ZN    comb    (!I1)              1  
BUF  A1,A2,A3,B1  Z     comb    (!(((A1A2)A3)B1))  4
CLK    C          Z     seq      Cq                1
XOR    A1,A2,B1   Z     comb    (((A1A2)B1)        3
IAD    A1,A2,A3   Z     comb    (!((A1A2)A3))      3
INV    I1         ZN    comb    (!I1)              1

I want output as below

cell   ins    input                             out
AI20   i1    .A1(A),.A2(B),.A3(C)               .Z(n1)    
INV    i2    .I1(n1)                            .ZN(n2)    
BUF    i3    .A1(n2),.A2(1),.A3(0),.B1(0)       .Z(n3)    
CLK    i4    .C(n3)                             .Z(n4)    
XOR    i5    .A1(n4),.A2(0),.B1(0)              .Z(n5)    
IAD    i6    .A1(1),.A2(n5),.A3(0)              .Z(n6)    
INV    i7    .I1(n6)                            .ZN(X)                     

The logic is

if it is first line of input file I.e AI20 assign its inputs A1,A2 to user defined A and B and assign it output to n1. For next line in input file assign previous net n1 to one of input and 0/1 to other inputs of that line and assign its output to next net n2 and so on. n1, n2,n3 so on are nets and can be assigned to an array which has no. Of rows equal to no of rows in (input file)-1(no. of rows exclude header row of input file). If it is last line or row of input file then assign that line or row output to X. i1,i2,i3... are instance name which has number of rows equal to no of rows of (input file).

A,B,X are user defined. We can directly use them in code. The term inside bracket for input is basically the one present in the previous line in output(). Exception will be for first line input and last line output whose () terms are directly defined.

I used code

awk ' { print $1 ;print "."$2"()"; print "."$3"()" file }'

This gives me, but how can I build logic for inside().

AI20   .A1(), .A2(),.A3()    .Z()    
1 Answers

You may use this awk script:

cat remap.awk

NR == 1 {
   print "cell   ins    input     out"
   next
}
id != "" {
   ++r
   print id, "i" r, s, "." out "(n" r ")"
}
{
   n = split($2, a, /,/)
   s = ""
   if (NR == 2) {
      ch =  65
      for (i=1; i<=n; ++i)
         s = (s == "" ? "" : s ",") sprintf(".%s(%c)", a[i], ch++)
   } else {
      s = "." a[1] "(n" r ")"
      for (i=2; i<=n; ++i)
         s = s "," sprintf(".%s(%d)", a[i], i%2)
   }

   id = $1
   out = $3
}
END {
   if (r)
      print id, "i" r+1, s, "." out "(X)"
}

Then use it as:

awk -f remap.awk file.txt | column -t

cell  ins  input                         out
AI20  i1   .A1(A),.A2(B),.A3(C)          .Z(n1)
INV   i2   .I1(n1)                       .ZN(n2)
BUF   i3   .A1(n2),.A2(0),.A3(1),.B1(0)  .Z(n3)
CLK   i4   .C(n3)                        .Z(n4)
XOR   i5   .A1(n4),.A2(0),.B1(1)         .Z(n5)
IAD   i6   .A1(n5),.A2(0),.A3(1)         .Z(n6)
INV   i7   .I1(n6)                       .ZN(X)
Related