@@ -132,7 +132,7 @@ struct AigerWriter
132132 return a;
133133 }
134134
135- AigerWriter (Module *module , bool zinit_mode, bool imode, bool omode, bool bmode, bool lmode) : module (module ), zinit_mode(zinit_mode), sigmap(module )
135+ AigerWriter (Module *module , bool no_sort, bool zinit_mode, bool imode, bool omode, bool bmode, bool lmode) : module (module ), zinit_mode(zinit_mode), sigmap(module )
136136 {
137137 pool<SigBit> undriven_bits;
138138 pool<SigBit> unused_bits;
@@ -152,16 +152,12 @@ struct AigerWriter
152152 if (wire->port_input )
153153 sigmap.add (wire);
154154
155- for (auto wire : module ->wires ())
156- {
157- if (wire->attributes .count (ID::init)) {
158- SigSpec initsig = sigmap (wire);
159- Const initval = wire->attributes .at (ID::init);
160- for (int i = 0 ; i < GetSize (wire) && i < GetSize (initval); i++)
161- if (initval[i] == State::S0 || initval[i] == State::S1)
162- init_map[initsig[i]] = initval[i] == State::S1;
163- }
164-
155+ // handle ports
156+ // provided the input_bits and output_bits don't get sorted they
157+ // will be returned in reverse order, so add them in reverse to
158+ // match
159+ for (auto riter = module ->ports .rbegin (); riter != module ->ports .rend (); ++riter) {
160+ auto *wire = module ->wire (*riter);
165161 for (int i = 0 ; i < GetSize (wire); i++)
166162 {
167163 SigBit wirebit (wire, i);
@@ -175,9 +171,6 @@ struct AigerWriter
175171 continue ;
176172 }
177173
178- undriven_bits.insert (bit);
179- unused_bits.insert (bit);
180-
181174 if (wire->port_input )
182175 input_bits.insert (bit);
183176
@@ -187,6 +180,32 @@ struct AigerWriter
187180 output_bits.insert (wirebit);
188181 }
189182 }
183+ }
184+
185+ // handle wires
186+ for (auto wire : module ->wires ())
187+ {
188+ if (wire->attributes .count (ID::init)) {
189+ SigSpec initsig = sigmap (wire);
190+ Const initval = wire->attributes .at (ID::init);
191+ for (int i = 0 ; i < GetSize (wire) && i < GetSize (initval); i++)
192+ if (initval[i] == State::S0 || initval[i] == State::S1)
193+ init_map[initsig[i]] = initval[i] == State::S1;
194+ }
195+
196+ for (int i = 0 ; i < GetSize (wire); i++)
197+ {
198+ SigBit wirebit (wire, i);
199+ SigBit bit = sigmap (wirebit);
200+
201+ if (bit.wire == nullptr )
202+ continue ;
203+ if (wire->port_input || wire->port_output )
204+ continue ;
205+
206+ undriven_bits.insert (bit);
207+ unused_bits.insert (bit);
208+ }
190209
191210 if (wire->width == 1 ) {
192211 auto gclk_attr = wire->attributes .find (ID::replaced_by_gclk);
@@ -200,12 +219,6 @@ struct AigerWriter
200219 }
201220 }
202221
203- for (auto bit : input_bits)
204- undriven_bits.erase (bit);
205-
206- for (auto bit : output_bits)
207- unused_bits.erase (bit);
208-
209222 for (auto cell : module ->cells ())
210223 {
211224 if (cell->type == ID ($_NOT_))
@@ -343,8 +356,11 @@ struct AigerWriter
343356 }
344357
345358 init_map.sort ();
346- input_bits.sort ();
347- output_bits.sort ();
359+ // we are relying here on unsorted pools iterating last-in-first-out
360+ if (!no_sort) {
361+ input_bits.sort ();
362+ output_bits.sort ();
363+ }
348364 not_map.sort ();
349365 ff_map.sort ();
350366 and_map.sort ();
@@ -901,6 +917,9 @@ struct AigerBackend : public Backend {
901917 log (" -symbols\n " );
902918 log (" include a symbol table in the generated AIGER file\n " );
903919 log (" \n " );
920+ log (" -no-sort\n " );
921+ log (" don't sort input/output ports\n " );
922+ log (" \n " );
904923 log (" -map <filename>\n " );
905924 log (" write an extra file with port and latch symbols\n " );
906925 log (" \n " );
@@ -925,6 +944,7 @@ struct AigerBackend : public Backend {
925944 bool zinit_mode = false ;
926945 bool miter_mode = false ;
927946 bool symbols_mode = false ;
947+ bool no_sort = false ;
928948 bool verbose_map = false ;
929949 bool imode = false ;
930950 bool omode = false ;
@@ -955,6 +975,10 @@ struct AigerBackend : public Backend {
955975 symbols_mode = true ;
956976 continue ;
957977 }
978+ if (args[argidx] == " -no-sort" ) {
979+ no_sort = true ;
980+ continue ;
981+ }
958982 if (map_filename.empty () && args[argidx] == " -map" && argidx+1 < args.size ()) {
959983 map_filename = args[++argidx];
960984 continue ;
@@ -1008,7 +1032,7 @@ struct AigerBackend : public Backend {
10081032 if (!top_module->memories .empty ())
10091033 log_error (" Found unmapped memories in module %s: unmapped memories are not supported in AIGER backend!\n " , log_id (top_module));
10101034
1011- AigerWriter writer (top_module, zinit_mode, imode, omode, bmode, lmode);
1035+ AigerWriter writer (top_module, no_sort, zinit_mode, imode, omode, bmode, lmode);
10121036 writer.write_aiger (*f, ascii_mode, miter_mode, symbols_mode);
10131037
10141038 if (!map_filename.empty ()) {
0 commit comments