abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cbaWriteVer.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [cba.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Verilog parser.]
8 
9  Synopsis [Parses several flavors of word-level Verilog.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - November 29, 2014.]
16 
17  Revision [$Id: cba.c,v 1.00 2014/11/29 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "cba.h"
22 #include "cbaPrs.h"
23 
25 
26 ////////////////////////////////////////////////////////////////////////
27 /// DECLARATIONS ///
28 ////////////////////////////////////////////////////////////////////////
29 /*
30 
31 // node types during parsing
32 typedef enum {
33  CBA_NODE_NONE = 0, // 0: unused
34  CBA_NODE_CONST, // 1: constant
35  CBA_NODE_BUF, // 2: buffer
36  CBA_NODE_INV, // 3: inverter
37  CBA_NODE_AND, // 4: AND
38  CBA_NODE_OR, // 5: OR
39  CBA_NODE_XOR, // 6: XOR
40  CBA_NODE_NAND, // 7: NAND
41  CBA_NODE_NOR, // 8: NOR
42  CBA_NODE_XNOR, // 9 .XNOR
43  CBA_NODE_MUX, // 10: MUX
44  CBA_NODE_MAJ, // 11: MAJ
45  CBA_NODE_KNOWN // 12: unknown
46  CBA_NODE_UNKNOWN // 13: unknown
47 } Cba_NodeType_t;
48 
49 */
50 
51 const char * s_NodeTypes[CBA_NODE_UNKNOWN+1] = {
52  NULL, // 0: unused
53  "const", // 1: constant
54  "buf", // 2: buffer
55  "not", // 3: inverter
56  "and", // 4: AND
57  "nand", // 5: OR
58  "or", // 6: XOR
59  "nor", // 7: NAND
60  "xor", // 8: NOR
61  "xnor", // 9: .XNOR
62  "mux", // 10: MUX
63  "maj", // 11: MAJ
64  "???" // 12: known
65  "???" // 13: unknown
66 };
67 
68 ////////////////////////////////////////////////////////////////////////
69 /// FUNCTION DEFINITIONS ///
70 ////////////////////////////////////////////////////////////////////////
71 
72 /**Function*************************************************************
73 
74  Synopsis [Writing parser state into a file.]
75 
76  Description []
77 
78  SideEffects []
79 
80  SeeAlso []
81 
82 ***********************************************************************/
83 void Cba_PrsWriteVerilogMux( FILE * pFile, Cba_Ntk_t * p, Vec_Int_t * vFanins )
84 {
85  int NameId, RangeId, i;
86  char * pStrs[4] = { " = ", " ? ", " : ", ";\n" };
87  assert( Vec_IntSize(vFanins) == 8 );
88  fprintf( pFile, " assign " );
89  Vec_IntForEachEntryDouble( vFanins, NameId, RangeId, i )
90  {
91  fprintf( pFile, "%s%s%s", Cba_NtkStr(p, NameId), RangeId > 0 ? Cba_NtkStr(p, RangeId) : "", pStrs[i/2] );
92  }
93 }
94 void Cba_PrsWriteVerilogConcat( FILE * pFile, Cba_Ntk_t * p, int Id )
95 {
96  extern void Cba_PrsWriteVerilogArray2( FILE * pFile, Cba_Ntk_t * p, Vec_Int_t * vFanins );
97  fprintf( pFile, "{" );
98  Cba_PrsWriteVerilogArray2( pFile, p, Cba_ObjFanins(p, Id) );
99  fprintf( pFile, "}" );
100 }
101 void Cba_PrsWriteVerilogArray2( FILE * pFile, Cba_Ntk_t * p, Vec_Int_t * vFanins )
102 {
103  int NameId, RangeId, i;
104  assert( Vec_IntSize(vFanins) % 2 == 0 );
105  Vec_IntForEachEntryDouble( vFanins, NameId, RangeId, i )
106  {
107  assert( RangeId >= -2 );
108  if ( RangeId == -2 )
109  Cba_PrsWriteVerilogConcat( pFile, p, NameId-1 );
110  else if ( RangeId == -1 )
111  fprintf( pFile, "%s", Cba_NtkStr(p, NameId) );
112  else
113  fprintf( pFile, "%s%s", Cba_NtkStr(p, NameId), RangeId ? Cba_NtkStr(p, RangeId) : "" );
114  fprintf( pFile, "%s", (i == Vec_IntSize(vFanins) - 2) ? "" : ", " );
115  }
116 }
117 void Cba_PrsWriteVerilogArray3( FILE * pFile, Cba_Ntk_t * p, Vec_Int_t * vFanins )
118 {
119  int FormId, NameId, RangeId, i;
120  assert( Vec_IntSize(vFanins) % 3 == 0 );
121  Vec_IntForEachEntryTriple( vFanins, FormId, NameId, RangeId, i )
122  {
123  fprintf( pFile, ".%s(", Cba_NtkStr(p, FormId) );
124  if ( RangeId == -2 )
125  Cba_PrsWriteVerilogConcat( pFile, p, NameId-1 );
126  else if ( RangeId == -1 )
127  fprintf( pFile, "%s", Cba_NtkStr(p, NameId) );
128  else
129  fprintf( pFile, "%s%s", Cba_NtkStr(p, NameId), RangeId ? Cba_NtkStr(p, RangeId) : "" );
130  fprintf( pFile, ")%s", (i == Vec_IntSize(vFanins) - 3) ? "" : ", " );
131  }
132 }
133 void Cba_PrsWriteVerilogNodes( FILE * pFile, Cba_Ntk_t * p )
134 {
135  int Type, Func, i;
136  Cba_NtkForEachObjType( p, Type, i )
137  if ( Type == CBA_PRS_NODE ) // .names/assign/box2 (no formal/actual binding)
138  {
139  Func = Cba_ObjFuncId(p, i);
140  if ( Func >= CBA_NODE_BUF && Func <= CBA_NODE_XNOR )
141  {
142  fprintf( pFile, " %s (", s_NodeTypes[Func] );
143  Cba_PrsWriteVerilogArray2( pFile, p, Cba_ObjFanins(p, i) );
144  fprintf( pFile, ");\n" );
145  }
146  else if ( Func == CBA_NODE_MUX )
147  Cba_PrsWriteVerilogMux( pFile, p, Cba_ObjFanins(p, i) );
148  else
149  {
150  //char * pName = Cba_NtkStr(p, Func);
151  assert( 0 );
152  }
153  }
154 }
155 void Cba_PrsWriteVerilogBoxes( FILE * pFile, Cba_Ntk_t * p )
156 {
157  int Type, i;
158  Cba_NtkForEachObjType( p, Type, i )
159  if ( Type == CBA_PRS_BOX ) // .subckt/.gate/box (formal/actual binding)
160  {
161  fprintf( pFile, " %s %s (", Cba_ObjFuncStr(p, i), Cba_ObjInstStr(p, i) );
162  Cba_PrsWriteVerilogArray3( pFile, p, Cba_ObjFanins(p, i) );
163  fprintf( pFile, ");\n" );
164  }
165 }
166 void Cba_PrsWriteVerilogSignals( FILE * pFile, Cba_Ntk_t * p, int SigType )
167 {
168  int NameId, RangeId, i;
169  char * pSigNames[4] = { "inout", "input", "output", "wire" };
170  Vec_Int_t * vSigs[4] = { &p->vInouts, &p->vInputs, &p->vOutputs, &p->vWires };
171  Vec_IntForEachEntryDouble( vSigs[SigType], NameId, RangeId, i )
172  fprintf( pFile, " %s %s%s;\n", pSigNames[SigType], RangeId ? Cba_NtkStr(p, RangeId) : "", Cba_NtkStr(p, NameId) );
173 }
174 void Cba_PrsWriteVerilogSignalList( FILE * pFile, Cba_Ntk_t * p, int SigType, int fSkipComma )
175 {
176  int NameId, RangeId, i;
177  Vec_Int_t * vSigs[4] = { &p->vInouts, &p->vInputs, &p->vOutputs, &p->vWires };
178  Vec_IntForEachEntryDouble( vSigs[SigType], NameId, RangeId, i )
179  fprintf( pFile, "%s%s", Cba_NtkStr(p, NameId), (fSkipComma && i == Vec_IntSize(vSigs[SigType]) - 2) ? "" : ", " );
180 }
181 void Cba_PrsWriteVerilogNtk( FILE * pFile, Cba_Ntk_t * p )
182 {
183  int s;
184  assert( Vec_IntSize(&p->vTypes) == Cba_NtkObjNum(p) );
185  assert( Vec_IntSize(&p->vFuncs) == Cba_NtkObjNum(p) );
187  // write header
188  fprintf( pFile, "module %s (\n", Cba_NtkName(p) );
189  for ( s = 0; s < 3; s++ )
190  {
191  if ( s == 0 && Vec_IntSize(&p->vInouts) == 0 )
192  continue;
193  fprintf( pFile, " " );
194  Cba_PrsWriteVerilogSignalList( pFile, p, s, s==2 );
195  fprintf( pFile, "\n" );
196  }
197  fprintf( pFile, " );\n" );
198  // write declarations
199  for ( s = 0; s < 4; s++ )
200  Cba_PrsWriteVerilogSignals( pFile, p, s );
201  fprintf( pFile, "\n" );
202  // write objects
203  Cba_PrsWriteVerilogNodes( pFile, p );
204  Cba_PrsWriteVerilogBoxes( pFile, p );
205  fprintf( pFile, "endmodule\n\n" );
206 }
207 void Cba_PrsWriteVerilog( char * pFileName, Cba_Man_t * pDes )
208 {
209  FILE * pFile;
210  Cba_Ntk_t * pNtk;
211  int i;
212  pFile = fopen( pFileName, "wb" );
213  if ( pFile == NULL )
214  {
215  printf( "Cannot open output file \"%s\".\n", pFileName );
216  return;
217  }
218  fprintf( pFile, "// Design \"%s\" written by ABC on %s\n\n", Cba_ManName(pDes), Extra_TimeStamp() );
219  Cba_ManForEachNtk( pDes, pNtk, i )
220  Cba_PrsWriteVerilogNtk( pFile, pNtk );
221  fclose( pFile );
222 }
223 
224 
225 /**Function*************************************************************
226 
227  Synopsis []
228 
229  Description []
230 
231  SideEffects []
232 
233  SeeAlso []
234 
235 ***********************************************************************/
236 
237 
238 ////////////////////////////////////////////////////////////////////////
239 /// END OF FILE ///
240 ////////////////////////////////////////////////////////////////////////
241 
242 
244 
Definition: cba.h:99
void Cba_PrsWriteVerilogMux(FILE *pFile, Cba_Ntk_t *p, Vec_Int_t *vFanins)
FUNCTION DEFINITIONS ///.
Definition: cbaWriteVer.c:83
static Llb_Mgr_t * p
Definition: llb3Image.c:950
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition: bblif.c:37
Vec_Int_t vWires
Definition: cba.h:107
static int Cba_ObjFuncId(Cba_Ntk_t *p, int i)
Definition: cba.h:132
void Cba_PrsWriteVerilog(char *pFileName, Cba_Man_t *pDes)
Definition: cbaWriteVer.c:207
static char * Cba_NtkName(Cba_Ntk_t *p)
Definition: cba.h:125
Vec_Int_t vInputs
Definition: cba.h:105
#define Cba_NtkForEachObjType(p, Type, i)
Definition: cba.h:158
Vec_Int_t vOutputs
Definition: cba.h:106
static Vec_Int_t * Cba_ObjFanins(Cba_Ntk_t *p, int i)
Definition: cba.h:134
void Cba_PrsWriteVerilogNtk(FILE *pFile, Cba_Ntk_t *p)
Definition: cbaWriteVer.c:181
Vec_Int_t vFuncs
Definition: cba.h:110
static int Cba_NtkObjNum(Cba_Ntk_t *p)
Definition: cba.h:126
static char * Cba_ObjFuncStr(Cba_Ntk_t *p, int i)
Definition: cba.h:140
void Cba_PrsWriteVerilogArray2(FILE *pFile, Cba_Ntk_t *p, Vec_Int_t *vFanins)
Definition: cbaWriteVer.c:101
static char * Cba_ManName(Cba_Man_t *p)
Definition: cba.h:120
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
static char * Cba_NtkStr(Cba_Ntk_t *p, int i)
Definition: cba.h:127
void Cba_PrsWriteVerilogSignalList(FILE *pFile, Cba_Ntk_t *p, int SigType, int fSkipComma)
Definition: cbaWriteVer.c:174
static char * Cba_ObjInstStr(Cba_Ntk_t *p, int i)
Definition: cba.h:141
#define Vec_IntForEachEntryDouble(vVec, Entry1, Entry2, i)
Definition: vecInt.h:66
Definition: cba.h:82
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
Vec_Int_t vInstIds
Definition: cba.h:111
void Cba_PrsWriteVerilogArray3(FILE *pFile, Cba_Ntk_t *p, Vec_Int_t *vFanins)
Definition: cbaWriteVer.c:117
ABC_NAMESPACE_IMPL_START const char * s_NodeTypes[CBA_NODE_UNKNOWN+1]
DECLARATIONS ///.
Definition: cbaWriteVer.c:51
static int Vec_IntSize(Vec_Int_t *p)
Definition: bblif.c:252
void Cba_PrsWriteVerilogSignals(FILE *pFile, Cba_Ntk_t *p, int SigType)
Definition: cbaWriteVer.c:166
char * Extra_TimeStamp()
#define assert(ex)
Definition: util_old.h:213
Vec_Int_t vInouts
Definition: cba.h:104
#define Vec_IntForEachEntryTriple(vVec, Entry1, Entry2, Entry3, i)
Definition: vecInt.h:68
void Cba_PrsWriteVerilogConcat(FILE *pFile, Cba_Ntk_t *p, int Id)
Definition: cbaWriteVer.c:94
void Cba_PrsWriteVerilogNodes(FILE *pFile, Cba_Ntk_t *p)
Definition: cbaWriteVer.c:133
Vec_Int_t vTypes
Definition: cba.h:109
void Cba_PrsWriteVerilogBoxes(FILE *pFile, Cba_Ntk_t *p)
Definition: cbaWriteVer.c:155
#define Cba_ManForEachNtk(p, pNtk, i)
MACRO DEFINITIONS ///.
Definition: cba.h:155