2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Claire Xenia Wolf <claire@yosyshq.com>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "kernel/yosys.h"
21 #include "kernel/sigtools.h"
22 #include "kernel/celltypes.h"
23 #include "kernel/mem.h"
24 #include "kernel/fstdata.h"
29 PRIVATE_NAMESPACE_BEGIN
31 enum class SimulationMode
{
37 static const std::map
<std::string
, int> g_units
=
39 { "", -9 }, // default is ns
50 static double stringToTime(std::string str
)
52 if (str
=="END") return -1;
55 long value
= strtol(str
.c_str(), &endptr
, 10);
57 if (g_units
.find(endptr
)==g_units
.end())
58 log_error("Cannot parse '%s', bad unit '%s'\n", str
.c_str(), endptr
);
61 log_error("Time value '%s' must be positive\n", str
.c_str());
63 return value
* pow(10.0, g_units
.at(endptr
));
69 bool hide_internal
= true;
70 bool writeback
= false;
73 FstData
*fst
= nullptr;
74 double start_time
= 0;
75 double stop_time
= -1;
76 SimulationMode sim_mode
= SimulationMode::cmp
;
87 for (auto &bit
: v
.bits
)
100 dict
<Cell
*, SimInstance
*> children
;
103 dict
<SigBit
, State
> state_nets
;
104 dict
<SigBit
, pool
<Cell
*>> upd_cells
;
105 dict
<SigBit
, pool
<Wire
*>> upd_outports
;
107 pool
<SigBit
> dirty_bits
;
108 pool
<Cell
*> dirty_cells
;
109 pool
<IdString
> dirty_memories
;
110 pool
<SimInstance
*, hash_ptr_ops
> dirty_children
;
121 std::vector
<Const
> past_wr_clk
;
122 std::vector
<Const
> past_wr_en
;
123 std::vector
<Const
> past_wr_addr
;
124 std::vector
<Const
> past_wr_data
;
128 dict
<Cell
*, ff_state_t
> ff_database
;
129 dict
<IdString
, mem_state_t
> mem_database
;
130 pool
<Cell
*> formal_database
;
131 dict
<Cell
*, IdString
> mem_cells
;
133 std::vector
<Mem
> memories
;
135 dict
<Wire
*, pair
<int, Const
>> vcd_database
;
136 dict
<Wire
*, pair
<fstHandle
, Const
>> fst_database
;
137 dict
<Wire
*, fstHandle
> fst_handles
;
139 SimInstance(SimShared
*shared
, std::string scope
, Module
*module
, Cell
*instance
= nullptr, SimInstance
*parent
= nullptr) :
140 shared(shared
), scope(scope
), module(module
), instance(instance
), parent(parent
), sigmap(module
)
145 log_assert(parent
->children
.count(instance
) == 0);
146 parent
->children
[instance
] = this;
149 for (auto wire
: module
->wires())
151 SigSpec sig
= sigmap(wire
);
153 for (int i
= 0; i
< GetSize(sig
); i
++) {
154 if (state_nets
.count(sig
[i
]) == 0)
155 state_nets
[sig
[i
]] = State::Sx
;
156 if (wire
->port_output
) {
157 upd_outports
[sig
[i
]].insert(wire
);
158 dirty_bits
.insert(sig
[i
]);
163 fstHandle id
= shared
->fst
->getHandle(scope
+ "." + RTLIL::unescape_id(wire
->name
));
164 if (id
==0 && wire
->name
.isPublic())
165 log_warning("Unable to found wire %s in input file.\n", (scope
+ "." + RTLIL::unescape_id(wire
->name
)).c_str());
166 fst_handles
[wire
] = id
;
169 if (wire
->attributes
.count(ID::init
)) {
170 Const initval
= wire
->attributes
.at(ID::init
);
171 for (int i
= 0; i
< GetSize(sig
) && i
< GetSize(initval
); i
++)
172 if (initval
[i
] == State::S0
|| initval
[i
] == State::S1
) {
173 state_nets
[sig
[i
]] = initval
[i
];
174 dirty_bits
.insert(sig
[i
]);
179 memories
= Mem::get_all_memories(module
);
180 for (auto &mem
: memories
) {
181 auto &mdb
= mem_database
[mem
.memid
];
183 for (auto &port
: mem
.wr_ports
) {
184 mdb
.past_wr_clk
.push_back(Const(State::Sx
));
185 mdb
.past_wr_en
.push_back(Const(State::Sx
, GetSize(port
.en
)));
186 mdb
.past_wr_addr
.push_back(Const(State::Sx
, GetSize(port
.addr
)));
187 mdb
.past_wr_data
.push_back(Const(State::Sx
, GetSize(port
.data
)));
189 mdb
.data
= mem
.get_init_data();
192 for (auto cell
: module
->cells())
194 Module
*mod
= module
->design
->module(cell
->type
);
196 if (mod
!= nullptr) {
197 dirty_children
.insert(new SimInstance(shared
, scope
+ "." + RTLIL::unescape_id(module
->name
), mod
, cell
, this));
200 for (auto &port
: cell
->connections()) {
201 if (cell
->input(port
.first
))
202 for (auto bit
: sigmap(port
.second
)) {
203 upd_cells
[bit
].insert(cell
);
204 // Make sure cell inputs connected to constants are updated in the first cycle
205 if (bit
.wire
== nullptr)
206 dirty_bits
.insert(bit
);
210 if (cell
->type
.in(ID($dff
))) {
212 ff
.past_clock
= State::Sx
;
213 ff
.past_d
= Const(State::Sx
, cell
->getParam(ID::WIDTH
).as_int());
214 ff_database
[cell
] = ff
;
217 if (cell
->is_mem_cell())
219 mem_cells
[cell
] = cell
->parameters
.at(ID::MEMID
).decode_string();
221 if (cell
->type
.in(ID($
assert), ID($cover
), ID($assume
))) {
222 formal_database
.insert(cell
);
228 for (auto &it
: ff_database
)
230 Cell
*cell
= it
.first
;
231 ff_state_t
&ff
= it
.second
;
234 SigSpec qsig
= cell
->getPort(ID::Q
);
235 Const qdata
= get_state(qsig
);
237 set_state(qsig
, qdata
);
240 for (auto &it
: mem_database
) {
241 mem_state_t
&mem
= it
.second
;
242 for (auto &val
: mem
.past_wr_en
)
251 for (auto child
: children
)
255 IdString
name() const
257 if (instance
!= nullptr)
258 return instance
->name
;
262 std::string
hiername() const
264 if (instance
!= nullptr)
265 return parent
->hiername() + "." + log_id(instance
->name
);
267 return log_id(module
->name
);
270 Const
get_state(SigSpec sig
)
274 for (auto bit
: sigmap(sig
))
275 if (bit
.wire
== nullptr)
276 value
.bits
.push_back(bit
.data
);
277 else if (state_nets
.count(bit
))
278 value
.bits
.push_back(state_nets
.at(bit
));
280 value
.bits
.push_back(State::Sz
);
283 log("[%s] get %s: %s\n", hiername().c_str(), log_signal(sig
), log_signal(value
));
287 bool set_state(SigSpec sig
, Const value
)
289 bool did_something
= false;
292 log_assert(GetSize(sig
) <= GetSize(value
));
294 for (int i
= 0; i
< GetSize(sig
); i
++)
295 if (state_nets
.at(sig
[i
]) != value
[i
]) {
296 state_nets
.at(sig
[i
]) = value
[i
];
297 dirty_bits
.insert(sig
[i
]);
298 did_something
= true;
302 log("[%s] set %s: %s\n", hiername().c_str(), log_signal(sig
), log_signal(value
));
303 return did_something
;
306 void update_cell(Cell
*cell
)
308 if (ff_database
.count(cell
))
311 if (formal_database
.count(cell
))
314 if (mem_cells
.count(cell
))
316 dirty_memories
.insert(mem_cells
[cell
]);
320 if (children
.count(cell
))
322 auto child
= children
.at(cell
);
323 for (auto &conn
: cell
->connections())
324 if (cell
->input(conn
.first
) && GetSize(conn
.second
)) {
325 Const value
= get_state(conn
.second
);
326 child
->set_state(child
->module
->wire(conn
.first
), value
);
328 dirty_children
.insert(child
);
332 if (yosys_celltypes
.cell_evaluable(cell
->type
))
334 RTLIL::SigSpec sig_a
, sig_b
, sig_c
, sig_d
, sig_s
, sig_y
;
335 bool has_a
, has_b
, has_c
, has_d
, has_s
, has_y
;
337 has_a
= cell
->hasPort(ID::A
);
338 has_b
= cell
->hasPort(ID::B
);
339 has_c
= cell
->hasPort(ID::C
);
340 has_d
= cell
->hasPort(ID::D
);
341 has_s
= cell
->hasPort(ID::S
);
342 has_y
= cell
->hasPort(ID::Y
);
344 if (has_a
) sig_a
= cell
->getPort(ID::A
);
345 if (has_b
) sig_b
= cell
->getPort(ID::B
);
346 if (has_c
) sig_c
= cell
->getPort(ID::C
);
347 if (has_d
) sig_d
= cell
->getPort(ID::D
);
348 if (has_s
) sig_s
= cell
->getPort(ID::S
);
349 if (has_y
) sig_y
= cell
->getPort(ID::Y
);
352 log("[%s] eval %s (%s)\n", hiername().c_str(), log_id(cell
), log_id(cell
->type
));
354 // Simple (A -> Y) and (A,B -> Y) cells
355 if (has_a
&& !has_c
&& !has_d
&& !has_s
&& has_y
) {
356 set_state(sig_y
, CellTypes::eval(cell
, get_state(sig_a
), get_state(sig_b
)));
360 // (A,B,C -> Y) cells
361 if (has_a
&& has_b
&& has_c
&& !has_d
&& !has_s
&& has_y
) {
362 set_state(sig_y
, CellTypes::eval(cell
, get_state(sig_a
), get_state(sig_b
), get_state(sig_c
)));
366 // (A,B,S -> Y) cells
367 if (has_a
&& has_b
&& !has_c
&& !has_d
&& has_s
&& has_y
) {
368 set_state(sig_y
, CellTypes::eval(cell
, get_state(sig_a
), get_state(sig_b
), get_state(sig_s
)));
372 log_warning("Unsupported evaluable cell type: %s (%s.%s)\n", log_id(cell
->type
), log_id(module
), log_id(cell
));
376 log_error("Unsupported cell type: %s (%s.%s)\n", log_id(cell
->type
), log_id(module
), log_id(cell
));
379 void update_memory(IdString id
) {
380 auto &mdb
= mem_database
[id
];
381 auto &mem
= *mdb
.mem
;
383 for (int port_idx
= 0; port_idx
< GetSize(mem
.rd_ports
); port_idx
++)
385 auto &port
= mem
.rd_ports
[port_idx
];
386 Const addr
= get_state(port
.addr
);
387 Const data
= Const(State::Sx
, mem
.width
<< port
.wide_log2
);
390 log_error("Memory %s.%s has clocked read ports. Run 'memory' with -nordff.\n", log_id(module
), log_id(mem
.memid
));
392 if (addr
.is_fully_def()) {
393 int index
= addr
.as_int() - mem
.start_offset
;
394 if (index
>= 0 && index
< mem
.size
)
395 data
= mdb
.data
.extract(index
*mem
.width
, mem
.width
<< port
.wide_log2
);
398 set_state(port
.data
, data
);
404 pool
<Cell
*> queue_cells
;
405 pool
<Wire
*> queue_outports
;
407 queue_cells
.swap(dirty_cells
);
411 for (auto bit
: dirty_bits
)
413 if (upd_cells
.count(bit
))
414 for (auto cell
: upd_cells
.at(bit
))
415 queue_cells
.insert(cell
);
417 if (upd_outports
.count(bit
) && parent
!= nullptr)
418 for (auto wire
: upd_outports
.at(bit
))
419 queue_outports
.insert(wire
);
424 if (!queue_cells
.empty())
426 for (auto cell
: queue_cells
)
433 for (auto &memid
: dirty_memories
)
434 update_memory(memid
);
435 dirty_memories
.clear();
437 for (auto wire
: queue_outports
)
438 if (instance
->hasPort(wire
->name
)) {
439 Const value
= get_state(wire
);
440 parent
->set_state(instance
->getPort(wire
->name
), value
);
443 queue_outports
.clear();
445 for (auto child
: dirty_children
)
448 dirty_children
.clear();
450 if (dirty_bits
.empty())
457 bool did_something
= false;
459 for (auto &it
: ff_database
)
461 Cell
*cell
= it
.first
;
462 ff_state_t
&ff
= it
.second
;
464 if (cell
->type
.in(ID($dff
)))
466 bool clkpol
= cell
->getParam(ID::CLK_POLARITY
).as_bool();
467 State current_clock
= get_state(cell
->getPort(ID::CLK
))[0];
469 if (clkpol
? (ff
.past_clock
== State::S1
|| current_clock
!= State::S1
) :
470 (ff
.past_clock
== State::S0
|| current_clock
!= State::S0
))
473 if (set_state(cell
->getPort(ID::Q
), ff
.past_d
))
474 did_something
= true;
478 for (auto &it
: mem_database
)
480 mem_state_t
&mdb
= it
.second
;
481 auto &mem
= *mdb
.mem
;
483 for (int port_idx
= 0; port_idx
< GetSize(mem
.wr_ports
); port_idx
++)
485 auto &port
= mem
.wr_ports
[port_idx
];
486 Const addr
, data
, enable
;
488 if (!port
.clk_enable
)
490 addr
= get_state(port
.addr
);
491 data
= get_state(port
.data
);
492 enable
= get_state(port
.en
);
496 if (port
.clk_polarity
?
497 (mdb
.past_wr_clk
[port_idx
] == State::S1
|| get_state(port
.clk
) != State::S1
) :
498 (mdb
.past_wr_clk
[port_idx
] == State::S0
|| get_state(port
.clk
) != State::S0
))
501 addr
= mdb
.past_wr_addr
[port_idx
];
502 data
= mdb
.past_wr_data
[port_idx
];
503 enable
= mdb
.past_wr_en
[port_idx
];
506 if (addr
.is_fully_def())
508 int index
= addr
.as_int() - mem
.start_offset
;
509 if (index
>= 0 && index
< mem
.size
)
510 for (int i
= 0; i
< (mem
.width
<< port
.wide_log2
); i
++)
511 if (enable
[i
] == State::S1
&& mdb
.data
.bits
.at(index
*mem
.width
+i
) != data
[i
]) {
512 mdb
.data
.bits
.at(index
*mem
.width
+i
) = data
[i
];
513 dirty_memories
.insert(mem
.memid
);
514 did_something
= true;
520 for (auto it
: children
)
521 if (it
.second
->update_ph2()) {
522 dirty_children
.insert(it
.second
);
523 did_something
= true;
526 return did_something
;
531 for (auto &it
: ff_database
)
533 Cell
*cell
= it
.first
;
534 ff_state_t
&ff
= it
.second
;
536 if (cell
->type
.in(ID($dff
))) {
537 ff
.past_clock
= get_state(cell
->getPort(ID::CLK
))[0];
538 ff
.past_d
= get_state(cell
->getPort(ID::D
));
542 for (auto &it
: mem_database
)
544 mem_state_t
&mem
= it
.second
;
546 for (int i
= 0; i
< GetSize(mem
.mem
->wr_ports
); i
++) {
547 auto &port
= mem
.mem
->wr_ports
[i
];
548 mem
.past_wr_clk
[i
] = get_state(port
.clk
);
549 mem
.past_wr_en
[i
] = get_state(port
.en
);
550 mem
.past_wr_addr
[i
] = get_state(port
.addr
);
551 mem
.past_wr_data
[i
] = get_state(port
.data
);
555 for (auto cell
: formal_database
)
557 string label
= log_id(cell
);
558 if (cell
->attributes
.count(ID::src
))
559 label
= cell
->attributes
.at(ID::src
).decode_string();
561 State a
= get_state(cell
->getPort(ID::A
))[0];
562 State en
= get_state(cell
->getPort(ID::EN
))[0];
564 if (cell
->type
== ID($cover
) && en
== State::S1
&& a
!= State::S1
)
565 log("Cover %s.%s (%s) reached.\n", hiername().c_str(), log_id(cell
), label
.c_str());
567 if (cell
->type
== ID($assume
) && en
== State::S1
&& a
!= State::S1
)
568 log("Assumption %s.%s (%s) failed.\n", hiername().c_str(), log_id(cell
), label
.c_str());
570 if (cell
->type
== ID($
assert) && en
== State::S1
&& a
!= State::S1
)
571 log_warning("Assert %s.%s (%s) failed.\n", hiername().c_str(), log_id(cell
), label
.c_str());
574 for (auto it
: children
)
575 it
.second
->update_ph3();
578 void writeback(pool
<Module
*> &wbmods
)
580 if (wbmods
.count(module
))
581 log_error("Instance %s of module %s is not unique: Writeback not possible. (Fix by running 'uniquify'.)\n", hiername().c_str(), log_id(module
));
583 wbmods
.insert(module
);
585 for (auto wire
: module
->wires())
586 wire
->attributes
.erase(ID::init
);
588 for (auto &it
: ff_database
)
590 Cell
*cell
= it
.first
;
591 SigSpec sig_q
= cell
->getPort(ID::Q
);
592 Const initval
= get_state(sig_q
);
594 for (int i
= 0; i
< GetSize(sig_q
); i
++)
596 Wire
*w
= sig_q
[i
].wire
;
598 if (w
->attributes
.count(ID::init
) == 0)
599 w
->attributes
[ID::init
] = Const(State::Sx
, GetSize(w
));
601 w
->attributes
[ID::init
][sig_q
[i
].offset
] = initval
[i
];
605 for (auto &it
: mem_database
)
607 mem_state_t
&mem
= it
.second
;
608 mem
.mem
->clear_inits();
610 minit
.addr
= mem
.mem
->start_offset
;
611 minit
.data
= mem
.data
;
612 minit
.en
= Const(State::S1
, mem
.mem
->width
);
613 mem
.mem
->inits
.push_back(minit
);
617 for (auto it
: children
)
618 it
.second
->writeback(wbmods
);
621 void write_vcd_header(std::ofstream
&f
, int &id
)
623 f
<< stringf("$scope module %s $end\n", log_id(name()));
625 for (auto wire
: module
->wires())
627 if (shared
->hide_internal
&& wire
->name
[0] == '$')
630 f
<< stringf("$var wire %d n%d %s%s $end\n", GetSize(wire
), id
, wire
->name
[0] == '$' ? "\\" : "", log_id(wire
));
631 vcd_database
[wire
] = make_pair(id
++, Const());
634 for (auto child
: children
)
635 child
.second
->write_vcd_header(f
, id
);
637 f
<< stringf("$upscope $end\n");
640 void write_vcd_step(std::ofstream
&f
)
642 for (auto &it
: vcd_database
)
644 Wire
*wire
= it
.first
;
645 Const value
= get_state(wire
);
646 int id
= it
.second
.first
;
648 if (it
.second
.second
== value
)
651 it
.second
.second
= value
;
654 for (int i
= GetSize(value
)-1; i
>= 0; i
--) {
656 case State::S0
: f
<< "0"; break;
657 case State::S1
: f
<< "1"; break;
658 case State::Sx
: f
<< "x"; break;
663 f
<< stringf(" n%d\n", id
);
666 for (auto child
: children
)
667 child
.second
->write_vcd_step(f
);
670 void write_fst_header(struct fstContext
*f
)
672 fstWriterSetScope(f
, FST_ST_VCD_MODULE
, stringf("%s",log_id(name())).c_str(), nullptr);
673 for (auto wire
: module
->wires())
675 if (shared
->hide_internal
&& wire
->name
[0] == '$')
678 fstHandle id
= fstWriterCreateVar(f
, FST_VT_VCD_WIRE
, FST_VD_IMPLICIT
, GetSize(wire
),
679 stringf("%s%s", wire
->name
[0] == '$' ? "\\" : "", log_id(wire
)).c_str(), 0);
680 fst_database
[wire
] = make_pair(id
, Const());
683 for (auto child
: children
)
684 child
.second
->write_fst_header(f
);
686 fstWriterSetUpscope(f
);
689 void write_fst_step(struct fstContext
*f
)
691 for (auto &it
: fst_database
)
693 Wire
*wire
= it
.first
;
694 Const value
= get_state(wire
);
695 fstHandle id
= it
.second
.first
;
697 if (it
.second
.second
== value
)
700 it
.second
.second
= value
;
701 std::stringstream ss
;
702 for (int i
= GetSize(value
)-1; i
>= 0; i
--) {
704 case State::S0
: ss
<< "0"; break;
705 case State::S1
: ss
<< "1"; break;
706 case State::Sx
: ss
<< "x"; break;
710 fstWriterEmitValueChange(f
, id
, ss
.str().c_str());
713 for (auto child
: children
)
714 child
.second
->write_fst_step(f
);
717 bool checkSignals(uint64_t time
)
720 for(auto &item
: fst_handles
) {
721 if (item
.second
==0) continue; // Ignore signals not found
722 Const fst_val
= Const::from_string(shared
->fst
->valueAt(item
.second
, time
));
723 Const sim_val
= get_state(item
.first
);
724 if (shared
->sim_mode
== SimulationMode::gate
&& !fst_val
.is_fully_def()) { // FST data contains X
725 // TODO: check bit by bit
726 } else if (shared
->sim_mode
== SimulationMode::gold
&& !sim_val
.is_fully_def()) { // sim data contains X
727 // TODO: check bit by bit
729 if (fst_val
!=sim_val
) {
731 log("signal: %s fst: %s sim: %s\n", log_id(item
.first
), log_signal(fst_val
), log_signal(sim_val
));
733 //log("signal: %s fst: %s sim: %s\n", log_id(item.first), log_signal(fst_val), log_signal(sim_val));
736 for (auto child
: children
)
737 retVal
|= child
.second
->checkSignals(time
);
742 struct SimWorker
: SimShared
744 SimInstance
*top
= nullptr;
745 std::ofstream vcdfile
;
746 struct fstContext
*fstfile
= nullptr;
747 pool
<IdString
> clock
, clockn
, reset
, resetn
;
748 std::string timescale
;
749 std::string sim_filename
;
757 void write_vcd_header()
759 vcdfile
<< stringf("$version %s $end\n", yosys_version_str
);
761 std::time_t t
= std::time(nullptr);
763 if (std::strftime(mbstr
, sizeof(mbstr
), "%c", std::localtime(&t
))) {
764 vcdfile
<< stringf("$date ") << mbstr
<< stringf(" $end\n");
767 if (!timescale
.empty())
768 vcdfile
<< stringf("$timescale %s $end\n", timescale
.c_str());
771 top
->write_vcd_header(vcdfile
, id
);
773 vcdfile
<< stringf("$enddefinitions $end\n");
776 void write_vcd_step(int t
)
778 vcdfile
<< stringf("#%d\n", t
);
779 top
->write_vcd_step(vcdfile
);
782 void write_fst_header()
784 std::time_t t
= std::time(nullptr);
785 fstWriterSetDate(fstfile
, asctime(std::localtime(&t
)));
786 fstWriterSetVersion(fstfile
, yosys_version_str
);
787 if (!timescale
.empty())
788 fstWriterSetTimescaleFromString(fstfile
, timescale
.c_str());
790 fstWriterSetPackType(fstfile
, FST_WR_PT_FASTLZ
);
791 fstWriterSetRepackOnClose(fstfile
, 1);
793 top
->write_fst_header(fstfile
);
796 void write_fst_step(int t
)
798 fstWriterEmitTimeChange(fstfile
, t
);
800 top
->write_fst_step(fstfile
);
803 void write_output_header()
805 if (vcdfile
.is_open())
811 void write_output_step(int t
)
813 if (vcdfile
.is_open())
819 void write_output_end()
822 fstWriterClose(fstfile
);
830 log("\n-- ph1 --\n");
835 log("\n-- ph2 --\n");
837 if (!top
->update_ph2())
842 log("\n-- ph3 --\n");
847 void set_inports(pool
<IdString
> ports
, State value
)
849 for (auto portname
: ports
)
851 Wire
*w
= top
->module
->wire(portname
);
854 log_error("Can't find port %s on module %s.\n", log_id(portname
), log_id(top
->module
));
856 top
->set_state(w
, value
);
860 void run(Module
*topmod
, int numcycles
)
862 log_assert(top
== nullptr);
863 top
= new SimInstance(this, scope
, topmod
);
866 log("\n===== 0 =====\n");
868 log("Simulating cycle 0.\n");
870 set_inports(reset
, State::S1
);
871 set_inports(resetn
, State::S0
);
873 set_inports(clock
, State::Sx
);
874 set_inports(clockn
, State::Sx
);
878 write_output_header();
879 write_output_step(0);
881 for (int cycle
= 0; cycle
< numcycles
; cycle
++)
884 log("\n===== %d =====\n", 10*cycle
+ 5);
886 set_inports(clock
, State::S0
);
887 set_inports(clockn
, State::S1
);
890 write_output_step(10*cycle
+ 5);
893 log("\n===== %d =====\n", 10*cycle
+ 10);
895 log("Simulating cycle %d.\n", cycle
+1);
897 set_inports(clock
, State::S1
);
898 set_inports(clockn
, State::S0
);
900 if (cycle
+1 == rstlen
) {
901 set_inports(reset
, State::S0
);
902 set_inports(resetn
, State::S1
);
906 write_output_step(10*cycle
+ 10);
909 write_output_step(10*numcycles
+ 2);
914 pool
<Module
*> wbmods
;
915 top
->writeback(wbmods
);
919 void run_cosim(Module
*topmod
, int numcycles
)
921 log_assert(top
== nullptr);
922 fst
= new FstData(sim_filename
);
924 top
= new SimInstance(this, scope
, topmod
);
926 std::vector
<fstHandle
> fst_clock
;
928 for (auto portname
: clock
)
930 Wire
*w
= topmod
->wire(portname
);
932 log_error("Can't find port %s on module %s.\n", log_id(portname
), log_id(top
->module
));
934 log_error("Clock port %s on module %s is not input.\n", log_id(portname
), log_id(top
->module
));
935 fstHandle id
= fst
->getHandle(scope
+ "." + RTLIL::unescape_id(portname
));
937 log_error("Can't find port %s.%s in FST.\n", scope
.c_str(), log_id(portname
));
938 fst_clock
.push_back(id
);
940 for (auto portname
: clockn
)
942 Wire
*w
= topmod
->wire(portname
);
944 log_error("Can't find port %s on module %s.\n", log_id(portname
), log_id(top
->module
));
946 log_error("Clock port %s on module %s is not input.\n", log_id(portname
), log_id(top
->module
));
947 fstHandle id
= fst
->getHandle(scope
+ "." + RTLIL::unescape_id(portname
));
949 log_error("Can't find port %s.%s in FST.\n", scope
.c_str(), log_id(portname
));
950 fst_clock
.push_back(id
);
952 if (fst_clock
.size()==0)
953 log_error("No clock signals defined for input file\n");
955 SigMap
sigmap(topmod
);
956 std::map
<Wire
*,fstHandle
> inputs
;
958 for (auto wire
: topmod
->wires()) {
959 if (wire
->port_input
) {
960 fstHandle id
= fst
->getHandle(scope
+ "." + RTLIL::unescape_id(wire
->name
));
965 uint64_t startCount
= 0;
966 uint64_t stopCount
= 0;
968 if (start_time
< fst
->getStartTime())
969 log_warning("Start time is before simulation file start time\n");
970 startCount
= fst
->getStartTime();
971 } else if (start_time
==-1)
972 startCount
= fst
->getEndTime();
974 startCount
= start_time
/ fst
->getTimescale();
975 if (startCount
> fst
->getEndTime()) {
976 startCount
= fst
->getEndTime();
977 log_warning("Start time is after simulation file end time\n");
981 if (stop_time
< fst
->getStartTime())
982 log_warning("Stop time is before simulation file start time\n");
983 stopCount
= fst
->getStartTime();
984 } else if (stop_time
==-1)
985 stopCount
= fst
->getEndTime();
987 stopCount
= stop_time
/ fst
->getTimescale();
988 if (stopCount
> fst
->getEndTime()) {
989 stopCount
= fst
->getEndTime();
990 log_warning("Stop time is after simulation file end time\n");
993 auto edges
= fst
->getAllEdges(fst_clock
, startCount
, stopCount
);
994 fst
->reconstructAllAtTimes(edges
);
995 for(auto &time
: edges
) {
996 for(auto &item
: inputs
) {
997 std::string v
= fst
->valueAt(item
.second
, time
);
998 top
->set_state(item
.first
, Const::from_string(v
));
1002 bool status
= top
->checkSignals(time
);
1004 log_error("Signal difference at %zu\n", time
);
1009 struct SimPass
: public Pass
{
1010 SimPass() : Pass("sim", "simulate the circuit") { }
1011 void help() override
1013 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
1015 log(" sim [options] [top-level]\n");
1017 log("This command simulates the circuit using the given top-level module.\n");
1019 log(" -vcd <filename>\n");
1020 log(" write the simulation results to the given VCD file\n");
1022 log(" -fst <filename>\n");
1023 log(" write the simulation results to the given FST file\n");
1025 log(" -clock <portname>\n");
1026 log(" name of top-level clock input\n");
1028 log(" -clockn <portname>\n");
1029 log(" name of top-level clock input (inverse polarity)\n");
1031 log(" -reset <portname>\n");
1032 log(" name of top-level reset input (active high)\n");
1034 log(" -resetn <portname>\n");
1035 log(" name of top-level inverted reset input (active low)\n");
1037 log(" -rstlen <integer>\n");
1038 log(" number of cycles reset should stay active (default: 1)\n");
1041 log(" zero-initialize all uninitialized regs and memories\n");
1043 log(" -timescale <string>\n");
1044 log(" include the specified timescale declaration in the vcd\n");
1046 log(" -n <integer>\n");
1047 log(" number of cycles to simulate (default: 20)\n");
1050 log(" include all nets in VCD output, not just those with public names\n");
1053 log(" writeback mode: use final simulation state as new init state\n");
1056 log(" read simulation results file (file formats supported: FST)\n");
1059 log(" scope of simulation top model\n");
1061 log(" -start <time>\n");
1062 log(" start co-simulation in arbitary time (default 0)\n");
1064 log(" -stop <time>\n");
1065 log(" stop co-simulation in arbitary time (default END)\n");
1068 log(" co-simulation expect exact match (default)\n");
1070 log(" -sim-gold\n");
1071 log(" co-simulation, x in simulation can match any value in FST\n");
1073 log(" -sim-gate\n");
1074 log(" co-simulation, x in FST can match any value in simulation\n");
1077 log(" enable debug output\n");
1080 void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) override
1085 log_header(design
, "Executing SIM pass (simulate the circuit).\n");
1088 for (argidx
= 1; argidx
< args
.size(); argidx
++) {
1089 if (args
[argidx
] == "-vcd" && argidx
+1 < args
.size()) {
1090 std::string vcd_filename
= args
[++argidx
];
1091 rewrite_filename(vcd_filename
);
1092 worker
.vcdfile
.open(vcd_filename
.c_str());
1095 if (args
[argidx
] == "-fst" && argidx
+1 < args
.size()) {
1096 std::string fst_filename
= args
[++argidx
];
1097 rewrite_filename(fst_filename
);
1098 worker
.fstfile
= (struct fstContext
*)fstWriterCreate(fst_filename
.c_str(),1);
1101 if (args
[argidx
] == "-n" && argidx
+1 < args
.size()) {
1102 numcycles
= atoi(args
[++argidx
].c_str());
1105 if (args
[argidx
] == "-rstlen" && argidx
+1 < args
.size()) {
1106 worker
.rstlen
= atoi(args
[++argidx
].c_str());
1109 if (args
[argidx
] == "-clock" && argidx
+1 < args
.size()) {
1110 worker
.clock
.insert(RTLIL::escape_id(args
[++argidx
]));
1113 if (args
[argidx
] == "-clockn" && argidx
+1 < args
.size()) {
1114 worker
.clockn
.insert(RTLIL::escape_id(args
[++argidx
]));
1117 if (args
[argidx
] == "-reset" && argidx
+1 < args
.size()) {
1118 worker
.reset
.insert(RTLIL::escape_id(args
[++argidx
]));
1121 if (args
[argidx
] == "-resetn" && argidx
+1 < args
.size()) {
1122 worker
.resetn
.insert(RTLIL::escape_id(args
[++argidx
]));
1125 if (args
[argidx
] == "-timescale" && argidx
+1 < args
.size()) {
1126 worker
.timescale
= args
[++argidx
];
1129 if (args
[argidx
] == "-a") {
1130 worker
.hide_internal
= false;
1133 if (args
[argidx
] == "-d") {
1134 worker
.debug
= true;
1137 if (args
[argidx
] == "-w") {
1138 worker
.writeback
= true;
1141 if (args
[argidx
] == "-zinit") {
1142 worker
.zinit
= true;
1145 if (args
[argidx
] == "-r" && argidx
+1 < args
.size()) {
1146 std::string sim_filename
= args
[++argidx
];
1147 rewrite_filename(sim_filename
);
1148 worker
.sim_filename
= sim_filename
;
1151 if (args
[argidx
] == "-scope" && argidx
+1 < args
.size()) {
1152 worker
.scope
= args
[++argidx
];
1155 if (args
[argidx
] == "-start" && argidx
+1 < args
.size()) {
1156 worker
.start_time
= stringToTime(args
[++argidx
]);
1159 if (args
[argidx
] == "-stop" && argidx
+1 < args
.size()) {
1160 worker
.stop_time
= stringToTime(args
[++argidx
]);
1163 if (args
[argidx
] == "-sim-cmp") {
1164 worker
.sim_mode
= SimulationMode::cmp
;
1167 if (args
[argidx
] == "-sim-gold") {
1168 worker
.sim_mode
= SimulationMode::gold
;
1171 if (args
[argidx
] == "-sim-gate") {
1172 worker
.sim_mode
= SimulationMode::gate
;
1177 extra_args(args
, argidx
, design
);
1179 Module
*top_mod
= nullptr;
1181 if (design
->full_selection()) {
1182 top_mod
= design
->top_module();
1185 log_cmd_error("Design has no top module, use the 'hierarchy' command to specify one.\n");
1187 auto mods
= design
->selected_whole_modules();
1188 if (GetSize(mods
) != 1)
1189 log_cmd_error("Only one top module must be selected.\n");
1190 top_mod
= mods
.front();
1193 if (worker
.sim_filename
.empty())
1194 worker
.run(top_mod
, numcycles
);
1196 worker
.run_cosim(top_mod
, numcycles
);
1200 PRIVATE_NAMESPACE_END