5 from contextlib
import contextmanager
7 from vcd
import VCDWriter
8 from vcd
.gtkw
import GTKWSave
10 from .._utils
import deprecated
11 from ..hdl
.ast
import *
12 from ..hdl
.cd
import *
13 from ..hdl
.ir
import *
14 from ..hdl
.xfrm
import ValueVisitor
, StatementVisitor
, LHSGroupFilter
21 class Settle(Command
):
27 def __init__(self
, interval
=None):
28 self
.interval
= None if interval
is None else float(interval
)
31 if self
.interval
is None:
34 return "(delay {:.3}us)".format(self
.interval
* 1e6
)
38 def __init__(self
, domain
="sync"):
39 if not isinstance(domain
, (str, ClockDomain
)):
40 raise TypeError("Domain must be a string or a ClockDomain instance, not {!r}"
42 assert domain
!= "comb"
46 return "(tick {})".format(self
.domain
)
49 class Passive(Command
):
54 class Active(Command
):
59 class _WaveformWriter
:
60 def update(self
, timestamp
, signal
, value
):
61 raise NotImplementedError # :nocov:
63 def close(self
, timestamp
):
64 raise NotImplementedError # :nocov:
67 class _VCDWaveformWriter(_WaveformWriter
):
69 def timestamp_to_vcd(timestamp
):
70 return timestamp
* (10 ** 10) # 1/(100 ps)
73 def decode_to_vcd(signal
, value
):
74 return signal
.decoder(value
).expandtabs().replace(" ", "_")
76 def __init__(self
, signal_names
, *, vcd_file
, gtkw_file
=None, traces
=()):
77 if isinstance(vcd_file
, str):
78 vcd_file
= open(vcd_file
, "wt")
79 if isinstance(gtkw_file
, str):
80 gtkw_file
= open(gtkw_file
, "wt")
82 self
.vcd_vars
= SignalDict()
83 self
.vcd_file
= vcd_file
84 self
.vcd_writer
= vcd_file
and VCDWriter(self
.vcd_file
,
85 timescale
="100 ps", comment
="Generated by nMigen")
87 self
.gtkw_names
= SignalDict()
88 self
.gtkw_file
= gtkw_file
89 self
.gtkw_save
= gtkw_file
and GTKWSave(self
.gtkw_file
)
93 trace_names
= SignalDict()
95 if trace
not in signal_names
:
96 trace_names
[trace
] = {("top", trace
.name
)}
97 self
.traces
.append(trace
)
99 if self
.vcd_writer
is None:
102 for signal
, names
in itertools
.chain(signal_names
.items(), trace_names
.items()):
106 var_init
= self
.decode_to_vcd(signal
, signal
.reset
)
109 var_size
= signal
.width
110 var_init
= signal
.reset
112 for (*var_scope
, var_name
) in names
:
117 var_name_suffix
= var_name
119 var_name_suffix
= "{}${}".format(var_name
, suffix
)
120 vcd_var
= self
.vcd_writer
.register_var(
121 scope
=var_scope
, name
=var_name_suffix
,
122 var_type
=var_type
, size
=var_size
, init
=var_init
)
125 suffix
= (suffix
or 0) + 1
127 if signal
not in self
.vcd_vars
:
128 self
.vcd_vars
[signal
] = set()
129 self
.vcd_vars
[signal
].add(vcd_var
)
131 if signal
not in self
.gtkw_names
:
132 self
.gtkw_names
[signal
] = (*var_scope
, var_name_suffix
)
134 def update(self
, timestamp
, signal
, value
):
135 vcd_vars
= self
.vcd_vars
.get(signal
)
139 vcd_timestamp
= self
.timestamp_to_vcd(timestamp
)
141 var_value
= self
.decode_to_vcd(signal
, value
)
144 for vcd_var
in vcd_vars
:
145 self
.vcd_writer
.change(vcd_var
, vcd_timestamp
, var_value
)
147 def close(self
, timestamp
):
148 if self
.vcd_writer
is not None:
149 self
.vcd_writer
.close(self
.timestamp_to_vcd(timestamp
))
151 if self
.gtkw_save
is not None:
152 self
.gtkw_save
.dumpfile(self
.vcd_file
.name
)
153 self
.gtkw_save
.dumpfile_size(self
.vcd_file
.tell())
155 self
.gtkw_save
.treeopen("top")
156 for signal
in self
.traces
:
157 if len(signal
) > 1 and not signal
.decoder
:
158 suffix
= "[{}:0]".format(len(signal
) - 1)
161 self
.gtkw_save
.trace(".".join(self
.gtkw_names
[signal
]) + suffix
)
163 if self
.vcd_file
is not None:
164 self
.vcd_file
.close()
165 if self
.gtkw_file
is not None:
166 self
.gtkw_file
.close()
170 __slots__
= ("runnable", "passive")
173 raise NotImplementedError # :nocov:
176 raise NotImplementedError # :nocov:
180 __slots__
= ("signal", "curr", "next", "waiters", "pending")
182 def __init__(self
, signal
, pending
):
184 self
.pending
= pending
185 self
.waiters
= dict()
186 self
.curr
= self
.next
= signal
.reset
188 def set(self
, value
):
189 if self
.next
== value
:
192 self
.pending
.add(self
)
194 def wait(self
, task
, *, trigger
=None):
195 assert task
not in self
.waiters
196 self
.waiters
[task
] = trigger
199 if self
.curr
== self
.next
:
201 self
.curr
= self
.next
204 for process
, trigger
in self
.waiters
.items():
205 if trigger
is None or trigger
== self
.curr
:
206 process
.runnable
= awoken_any
= True
210 class _SimulatorState
:
212 self
.signals
= SignalDict()
217 self
.deadlines
= dict()
220 for signal
, index
in self
.signals
.items():
221 self
.slots
[index
].curr
= self
.slots
[index
].next
= signal
.reset
225 self
.deadlines
.clear()
227 def get_signal(self
, signal
):
229 return self
.signals
[signal
]
231 index
= len(self
.slots
)
232 self
.slots
.append(_SignalState(signal
, self
.pending
))
233 self
.signals
[signal
] = index
236 def get_in_signal(self
, signal
, *, trigger
=None):
237 index
= self
.get_signal(signal
)
238 self
.slots
[index
].waiters
[self
] = trigger
241 def get_out_signal(self
, signal
):
242 return self
.get_signal(signal
)
244 def for_signal(self
, signal
):
245 return self
.slots
[self
.get_signal(signal
)]
249 for signal_state
in self
.pending
:
250 if signal_state
.commit():
256 nearest_processes
= set()
257 nearest_deadline
= None
258 for process
, deadline
in self
.deadlines
.items():
260 if nearest_deadline
is not None:
261 nearest_processes
.clear()
262 nearest_processes
.add(process
)
263 nearest_deadline
= self
.timestamp
265 elif nearest_deadline
is None or deadline
<= nearest_deadline
:
266 assert deadline
>= self
.timestamp
267 if nearest_deadline
is not None and deadline
< nearest_deadline
:
268 nearest_processes
.clear()
269 nearest_processes
.add(process
)
270 nearest_deadline
= deadline
272 if not nearest_processes
:
275 for process
in nearest_processes
:
276 process
.runnable
= True
277 del self
.deadlines
[process
]
278 self
.timestamp
= nearest_deadline
289 def append(self
, code
):
290 self
._buffer
.append(" " * self
._level
)
291 self
._buffer
.append(code
)
292 self
._buffer
.append("\n")
300 def flush(self
, indent
=""):
301 code
= "".join(self
._buffer
)
305 def gen_var(self
, prefix
):
306 name
= f
"{prefix}_{self._suffix}"
310 def def_var(self
, prefix
, value
):
311 name
= self
.gen_var(prefix
)
312 self
.append(f
"{name} = {value}")
317 def __init__(self
, state
, emitter
):
319 self
.emitter
= emitter
322 class _ValueCompiler(ValueVisitor
, _Compiler
):
324 "sign": lambda value
, sign
: value | sign
if value
& sign
else value
,
325 "zdiv": lambda lhs
, rhs
: 0 if rhs
== 0 else lhs
// rhs
,
326 "zmod": lambda lhs
, rhs
: 0 if rhs
== 0 else lhs
% rhs
,
329 def on_ClockSignal(self
, value
):
330 raise NotImplementedError # :nocov:
332 def on_ResetSignal(self
, value
):
333 raise NotImplementedError # :nocov:
335 def on_AnyConst(self
, value
):
336 raise NotImplementedError # :nocov:
338 def on_AnySeq(self
, value
):
339 raise NotImplementedError # :nocov:
341 def on_Sample(self
, value
):
342 raise NotImplementedError # :nocov:
344 def on_Initial(self
, value
):
345 raise NotImplementedError # :nocov:
348 class _RHSValueCompiler(_ValueCompiler
):
349 def __init__(self
, state
, emitter
, *, mode
, inputs
=None):
350 super().__init
__(state
, emitter
)
351 assert mode
in ("curr", "next")
353 # If not None, `inputs` gets populated with RHS signals.
356 def on_Const(self
, value
):
357 return f
"{value.value}"
359 def on_Signal(self
, value
):
360 if self
.inputs
is not None:
361 self
.inputs
.add(value
)
363 if self
.mode
== "curr":
364 return f
"slots[{self.state.get_signal(value)}].{self.mode}"
366 return f
"next_{self.state.get_signal(value)}"
368 def on_Operator(self
, value
):
370 value_mask
= (1 << len(value
)) - 1
371 return f
"({self(value)} & {value_mask})"
374 if value
.shape().signed
:
375 return f
"sign({mask(value)}, {-1 << (len(value) - 1)})"
379 if len(value
.operands
) == 1:
380 arg
, = value
.operands
381 if value
.operator
== "~":
382 return f
"(~{self(arg)})"
383 if value
.operator
== "-":
384 return f
"(-{self(arg)})"
385 if value
.operator
== "b":
386 return f
"bool({mask(arg)})"
387 if value
.operator
== "r|":
388 return f
"({mask(arg)} != 0)"
389 if value
.operator
== "r&":
390 return f
"({mask(arg)} == {(1 << len(arg)) - 1})"
391 if value
.operator
== "r^":
392 # Believe it or not, this is the fastest way to compute a sideways XOR in Python.
393 return f
"(format({mask(arg)}, 'b').count('1') % 2)"
394 if value
.operator
in ("u", "s"):
395 # These operators don't change the bit pattern, only its interpretation.
397 elif len(value
.operands
) == 2:
398 lhs
, rhs
= value
.operands
399 lhs_mask
= (1 << len(lhs
)) - 1
400 rhs_mask
= (1 << len(rhs
)) - 1
401 if value
.operator
== "+":
402 return f
"({sign(lhs)} + {sign(rhs)})"
403 if value
.operator
== "-":
404 return f
"({sign(lhs)} - {sign(rhs)})"
405 if value
.operator
== "*":
406 return f
"({sign(lhs)} * {sign(rhs)})"
407 if value
.operator
== "//":
408 return f
"zdiv({sign(lhs)}, {sign(rhs)})"
409 if value
.operator
== "%":
410 return f
"zmod({sign(lhs)}, {sign(rhs)})"
411 if value
.operator
== "&":
412 return f
"({self(lhs)} & {self(rhs)})"
413 if value
.operator
== "|":
414 return f
"({self(lhs)} | {self(rhs)})"
415 if value
.operator
== "^":
416 return f
"({self(lhs)} ^ {self(rhs)})"
417 if value
.operator
== "<<":
418 return f
"({sign(lhs)} << {sign(rhs)})"
419 if value
.operator
== ">>":
420 return f
"({sign(lhs)} >> {sign(rhs)})"
421 if value
.operator
== "==":
422 return f
"({sign(lhs)} == {sign(rhs)})"
423 if value
.operator
== "!=":
424 return f
"({sign(lhs)} != {sign(rhs)})"
425 if value
.operator
== "<":
426 return f
"({sign(lhs)} < {sign(rhs)})"
427 if value
.operator
== "<=":
428 return f
"({sign(lhs)} <= {sign(rhs)})"
429 if value
.operator
== ">":
430 return f
"({sign(lhs)} > {sign(rhs)})"
431 if value
.operator
== ">=":
432 return f
"({sign(lhs)} >= {sign(rhs)})"
433 elif len(value
.operands
) == 3:
434 if value
.operator
== "m":
435 sel
, val1
, val0
= value
.operands
436 return f
"({self(val1)} if {self(sel)} else {self(val0)})"
437 raise NotImplementedError("Operator '{}' not implemented".format(value
.operator
)) # :nocov:
439 def on_Slice(self
, value
):
440 return f
"(({self(value.value)} >> {value.start}) & {(1 << len(value)) - 1})"
442 def on_Part(self
, value
):
443 offset_mask
= (1 << len(value
.offset
)) - 1
444 offset
= f
"(({self(value.offset)} & {offset_mask}) * {value.stride})"
445 return f
"({self(value.value)} >> {offset} & " \
446 f
"{(1 << value.width) - 1})"
448 def on_Cat(self
, value
):
451 for part
in value
.parts
:
452 part_mask
= (1 << len(part
)) - 1
453 gen_parts
.append(f
"(({self(part)} & {part_mask}) << {offset})")
456 return f
"({' | '.join(gen_parts)})"
459 def on_Repl(self
, value
):
460 part_mask
= (1 << len(value
.value
)) - 1
461 gen_part
= self
.emitter
.def_var("repl", f
"{self(value.value)} & {part_mask}")
464 for _
in range(value
.count
):
465 gen_parts
.append(f
"({gen_part} << {offset})")
466 offset
+= len(value
.value
)
468 return f
"({' | '.join(gen_parts)})"
471 def on_ArrayProxy(self
, value
):
472 index_mask
= (1 << len(value
.index
)) - 1
473 gen_index
= self
.emitter
.def_var("rhs_index", f
"{self(value.index)} & {index_mask}")
474 gen_value
= self
.emitter
.gen_var("rhs_proxy")
477 for index
, elem
in enumerate(value
.elems
):
479 self
.emitter
.append(f
"if {gen_index} == {index}:")
481 self
.emitter
.append(f
"elif {gen_index} == {index}:")
482 with self
.emitter
.indent():
483 self
.emitter
.append(f
"{gen_value} = {self(elem)}")
484 self
.emitter
.append(f
"else:")
485 with self
.emitter
.indent():
486 self
.emitter
.append(f
"{gen_value} = {self(value.elems[-1])}")
492 def compile(cls
, state
, value
, *, mode
, inputs
=None):
494 compiler
= cls(state
, emitter
, mode
=mode
, inputs
=inputs
)
495 emitter
.append(f
"result = {compiler(value)}")
496 return emitter
.flush()
499 class _LHSValueCompiler(_ValueCompiler
):
500 def __init__(self
, state
, emitter
, *, rhs
, outputs
=None):
501 super().__init
__(state
, emitter
)
502 # `rrhs` is used to translate rvalues that are syntactically a part of an lvalue, e.g.
503 # the offset of a Part.
505 # `lrhs` is used to translate the read part of a read-modify-write cycle during partial
506 # update of an lvalue.
507 self
.lrhs
= _RHSValueCompiler(state
, emitter
, mode
="next", inputs
=None)
508 # If not None, `outputs` gets populated with signals on LHS.
509 self
.outputs
= outputs
511 def on_Const(self
, value
):
512 raise TypeError # :nocov:
514 def on_Signal(self
, value
):
515 if self
.outputs
is not None:
516 self
.outputs
.add(value
)
519 value_mask
= (1 << len(value
)) - 1
520 if value
.shape().signed
:
521 value_sign
= f
"sign({arg} & {value_mask}, {-1 << (len(value) - 1)})"
523 value_sign
= f
"{arg} & {value_mask}"
524 self
.emitter
.append(f
"next_{self.state.get_out_signal(value)} = {value_sign}")
527 def on_Operator(self
, value
):
528 raise TypeError # :nocov:
530 def on_Slice(self
, value
):
532 width_mask
= (1 << (value
.stop
- value
.start
)) - 1
533 self(value
.value
)(f
"({self.lrhs(value.value)} & " \
534 f
"{~(width_mask << value.start)} | " \
535 f
"(({arg} & {width_mask}) << {value.start}))")
538 def on_Part(self
, value
):
540 width_mask
= (1 << value
.width
) - 1
541 offset_mask
= (1 << len(value
.offset
)) - 1
542 offset
= f
"(({self.rrhs(value.offset)} & {offset_mask}) * {value.stride})"
543 self(value
.value
)(f
"({self.lrhs(value.value)} & " \
544 f
"~({width_mask} << {offset}) | " \
545 f
"(({arg} & {width_mask}) << {offset}))")
548 def on_Cat(self
, value
):
550 gen_arg
= self
.emitter
.def_var("cat", arg
)
553 for part
in value
.parts
:
554 part_mask
= (1 << len(part
)) - 1
555 self(part
)(f
"(({gen_arg} >> {offset}) & {part_mask})")
559 def on_Repl(self
, value
):
560 raise TypeError # :nocov:
562 def on_ArrayProxy(self
, value
):
564 index_mask
= (1 << len(value
.index
)) - 1
565 gen_index
= self
.emitter
.def_var("index", f
"{self.rrhs(value.index)} & {index_mask}")
568 for index
, elem
in enumerate(value
.elems
):
570 self
.emitter
.append(f
"if {gen_index} == {index}:")
572 self
.emitter
.append(f
"elif {gen_index} == {index}:")
573 with self
.emitter
.indent():
575 self
.emitter
.append(f
"else:")
576 with self
.emitter
.indent():
577 self(value
.elems
[-1])(arg
)
579 self
.emitter
.append(f
"pass")
583 def compile(cls
, state
, stmt
, *, inputs
=None, outputs
=None):
585 compiler
= cls(state
, emitter
, inputs
=inputs
, outputs
=outputs
)
587 return emitter
.flush()
590 class _StatementCompiler(StatementVisitor
, _Compiler
):
591 def __init__(self
, state
, emitter
, *, inputs
=None, outputs
=None):
592 super().__init
__(state
, emitter
)
593 self
.rhs
= _RHSValueCompiler(state
, emitter
, mode
="curr", inputs
=inputs
)
594 self
.lhs
= _LHSValueCompiler(state
, emitter
, rhs
=self
.rhs
, outputs
=outputs
)
596 def on_statements(self
, stmts
):
600 self
.emitter
.append("pass")
602 def on_Assign(self
, stmt
):
603 return self
.lhs(stmt
.lhs
)(self
.rhs(stmt
.rhs
))
605 def on_Switch(self
, stmt
):
606 gen_test
= self
.emitter
.def_var("test",
607 f
"{self.rhs(stmt.test)} & {(1 << len(stmt.test)) - 1}")
608 for index
, (patterns
, stmts
) in enumerate(stmt
.cases
.items()):
611 gen_checks
.append(f
"True")
613 for pattern
in patterns
:
615 mask
= int("".join("0" if b
== "-" else "1" for b
in pattern
), 2)
616 value
= int("".join("0" if b
== "-" else b
for b
in pattern
), 2)
617 gen_checks
.append(f
"({gen_test} & {mask}) == {value}")
619 value
= int(pattern
, 2)
620 gen_checks
.append(f
"{gen_test} == {value}")
622 self
.emitter
.append(f
"if {' or '.join(gen_checks)}:")
624 self
.emitter
.append(f
"elif {' or '.join(gen_checks)}:")
625 with self
.emitter
.indent():
628 def on_Assert(self
, stmt
):
629 raise NotImplementedError # :nocov:
631 def on_Assume(self
, stmt
):
632 raise NotImplementedError # :nocov:
634 def on_Cover(self
, stmt
):
635 raise NotImplementedError # :nocov:
638 def compile(cls
, state
, stmt
, *, inputs
=None, outputs
=None):
639 output_indexes
= [state
.get_signal(signal
) for signal
in stmt
._lhs
_signals
()]
641 for signal_index
in output_indexes
:
642 emitter
.append(f
"next_{signal_index} = slots[{signal_index}].next")
643 compiler
= cls(state
, emitter
, inputs
=inputs
, outputs
=outputs
)
645 for signal_index
in output_indexes
:
646 emitter
.append(f
"slots[{signal_index}].set(next_{signal_index})")
647 return emitter
.flush()
650 class _CompiledProcess(_Process
):
651 __slots__
= ("state", "comb", "run")
653 def __init__(self
, state
, *, comb
):
656 self
.run
= None # set by _FragmentCompiler
660 self
.runnable
= self
.comb
664 class _FragmentCompiler
:
665 def __init__(self
, state
, signal_names
):
667 self
.signal_names
= signal_names
669 def __call__(self
, fragment
, *, hierarchy
=("top",)):
672 def add_signal_name(signal
):
673 hierarchical_signal_name
= (*hierarchy
, signal
.name
)
674 if signal
not in self
.signal_names
:
675 self
.signal_names
[signal
] = {hierarchical_signal_name}
677 self
.signal_names
[signal
].add(hierarchical_signal_name
)
679 for domain_name
, domain_signals
in fragment
.drivers
.items():
680 domain_stmts
= LHSGroupFilter(domain_signals
)(fragment
.statements
)
681 domain_process
= _CompiledProcess(self
.state
, comb
=domain_name
is None)
684 emitter
.append(f
"def run():")
687 if domain_name
is None:
688 for signal
in domain_signals
:
689 signal_index
= domain_process
.state
.get_signal(signal
)
690 emitter
.append(f
"next_{signal_index} = {signal.reset}")
693 _StatementCompiler(domain_process
.state
, emitter
, inputs
=inputs
)(domain_stmts
)
696 self
.state
.for_signal(input).wait(domain_process
)
699 domain
= fragment
.domains
[domain_name
]
700 add_signal_name(domain
.clk
)
701 if domain
.rst
is not None:
702 add_signal_name(domain
.rst
)
704 clk_trigger
= 1 if domain
.clk_edge
== "pos" else 0
705 self
.state
.for_signal(domain
.clk
).wait(domain_process
, trigger
=clk_trigger
)
706 if domain
.rst
is not None and domain
.async_reset
:
708 self
.state
.for_signal(domain
.rst
).wait(domain_process
, trigger
=rst_trigger
)
711 clk_index
= domain_process
.state
.get_signal(domain
.clk
)
712 gen_asserts
.append(f
"slots[{clk_index}].curr == {clk_trigger}")
713 if domain
.rst
is not None and domain
.async_reset
:
714 rst_index
= domain_process
.state
.get_signal(domain
.rst
)
715 gen_asserts
.append(f
"slots[{rst_index}].curr == {rst_trigger}")
716 emitter
.append(f
"assert {' or '.join(gen_asserts)}")
718 for signal
in domain_signals
:
719 signal_index
= domain_process
.state
.get_signal(signal
)
720 emitter
.append(f
"next_{signal_index} = slots[{signal_index}].next")
722 _StatementCompiler(domain_process
.state
, emitter
)(domain_stmts
)
724 for signal
in domain_signals
:
725 signal_index
= domain_process
.state
.get_signal(signal
)
726 emitter
.append(f
"slots[{signal_index}].set(next_{signal_index})")
728 # There shouldn't be any exceptions raised by the generated code, but if there are
729 # (almost certainly due to a bug in the code generator), use this environment variable
730 # to make backtraces useful.
731 code
= emitter
.flush()
732 if os
.getenv("NMIGEN_pysim_dump"):
733 file = tempfile
.NamedTemporaryFile("w", prefix
="nmigen_pysim_", delete
=False)
737 filename
= "<string>"
739 exec_locals
= {"slots": domain_process
.state
.slots
, **_ValueCompiler
.helpers
}
740 exec(compile(code
, filename
, "exec"), exec_locals
)
741 domain_process
.run
= exec_locals
["run"]
743 processes
.add(domain_process
)
745 for used_signal
in domain_process
.state
.signals
:
746 add_signal_name(used_signal
)
748 for subfragment_index
, (subfragment
, subfragment_name
) in enumerate(fragment
.subfragments
):
749 if subfragment_name
is None:
750 subfragment_name
= "U${}".format(subfragment_index
)
751 processes
.update(self(subfragment
, hierarchy
=(*hierarchy
, subfragment_name
)))
756 class _CoroutineProcess(_Process
):
757 def __init__(self
, state
, domains
, constructor
, *, default_cmd
=None):
759 self
.domains
= domains
760 self
.constructor
= constructor
761 self
.default_cmd
= default_cmd
767 self
.coroutine
= self
.constructor()
769 "slots": self
.state
.slots
,
771 **_ValueCompiler
.helpers
773 self
.waits_on
= set()
776 coroutine
= self
.coroutine
777 while coroutine
.gi_yieldfrom
is not None:
778 coroutine
= coroutine
.gi_yieldfrom
779 if inspect
.isgenerator(coroutine
):
780 frame
= coroutine
.gi_frame
781 if inspect
.iscoroutine(coroutine
):
782 frame
= coroutine
.cr_frame
783 return "{}:{}".format(inspect
.getfile(frame
), inspect
.getlineno(frame
))
785 def get_in_signal(self
, signal
, *, trigger
=None):
786 signal_state
= self
.state
.for_signal(signal
)
787 assert self
not in signal_state
.waiters
788 signal_state
.waiters
[self
] = trigger
789 self
.waits_on
.add(signal_state
)
793 if self
.coroutine
is None:
797 for signal_state
in self
.waits_on
:
798 del signal_state
.waiters
[self
]
799 self
.waits_on
.clear()
804 command
= self
.coroutine
.send(response
)
806 command
= self
.default_cmd
809 if isinstance(command
, Value
):
810 exec(_RHSValueCompiler
.compile(self
.state
, command
, mode
="curr"),
812 response
= Const
.normalize(self
.exec_locals
["result"], command
.shape())
814 elif isinstance(command
, Statement
):
815 exec(_StatementCompiler
.compile(self
.state
, command
),
818 elif type(command
) is Tick
:
819 domain
= command
.domain
820 if isinstance(domain
, ClockDomain
):
822 elif domain
in self
.domains
:
823 domain
= self
.domains
[domain
]
825 raise NameError("Received command {!r} that refers to a nonexistent "
826 "domain {!r} from process {!r}"
827 .format(command
, command
.domain
, self
.src_loc()))
828 self
.get_in_signal(domain
.clk
, trigger
=1 if domain
.clk_edge
== "pos" else 0)
829 if domain
.rst
is not None and domain
.async_reset
:
830 self
.get_in_signal(domain
.rst
, trigger
=1)
833 elif type(command
) is Settle
:
834 self
.state
.deadlines
[self
] = None
837 elif type(command
) is Delay
:
838 if command
.interval
is None:
839 self
.state
.deadlines
[self
] = None
841 self
.state
.deadlines
[self
] = self
.state
.timestamp
+ command
.interval
844 elif type(command
) is Passive
:
847 elif type(command
) is Active
:
850 elif command
is None: # only possible if self.default_cmd is None
851 raise TypeError("Received default command from process {!r} that was added "
852 "with add_process(); did you mean to add this process with "
853 "add_sync_process() instead?"
854 .format(self
.src_loc()))
857 raise TypeError("Received unsupported command {!r} from process {!r}"
858 .format(command
, self
.src_loc()))
860 except StopIteration:
862 self
.coroutine
= None
865 except Exception as exn
:
866 self
.coroutine
.throw(exn
)
870 def __init__(self
, fragment
):
871 self
._state
= _SimulatorState()
872 self
._signal
_names
= SignalDict()
873 self
._fragment
= Fragment
.get(fragment
, platform
=None).prepare()
874 self
._processes
= _FragmentCompiler(self
._state
, self
._signal
_names
)(self
._fragment
)
875 self
._clocked
= set()
876 self
._waveform
_writers
= []
878 def _check_process(self
, process
):
879 if not (inspect
.isgeneratorfunction(process
) or inspect
.iscoroutinefunction(process
)):
880 raise TypeError("Cannot add a process {!r} because it is not a generator function"
884 def _add_coroutine_process(self
, process
, *, default_cmd
):
885 self
._processes
.add(_CoroutineProcess(self
._state
, self
._fragment
.domains
, process
,
886 default_cmd
=default_cmd
))
888 def add_process(self
, process
):
889 process
= self
._check
_process
(process
)
891 # Only start a bench process after comb settling, so that the reset values are correct.
894 self
._add
_coroutine
_process
(wrapper
, default_cmd
=None)
896 def add_sync_process(self
, process
, *, domain
="sync"):
897 process
= self
._check
_process
(process
)
899 # Only start a sync process after the first clock edge (or reset edge, if the domain
900 # uses an asynchronous reset). This matches the behavior of synchronous FFs.
903 return self
._add
_coroutine
_process
(wrapper
, default_cmd
=Tick(domain
))
905 def add_clock(self
, period
, *, phase
=None, domain
="sync", if_exists
=False):
906 """Add a clock process.
908 Adds a process that drives the clock signal of ``domain`` at a 50% duty cycle.
913 Clock period. The process will toggle the ``domain`` clock signal every ``period / 2``
915 phase : None or float
916 Clock phase. The process will wait ``phase`` seconds before the first clock transition.
917 If not specified, defaults to ``period / 2``.
918 domain : str or ClockDomain
919 Driven clock domain. If specified as a string, the domain with that name is looked up
920 in the root fragment of the simulation.
922 If ``False`` (the default), raise an error if the driven domain is specified as
923 a string and the root fragment does not have such a domain. If ``True``, do nothing
926 if isinstance(domain
, ClockDomain
):
928 elif domain
in self
._fragment
.domains
:
929 domain
= self
._fragment
.domains
[domain
]
933 raise ValueError("Domain {!r} is not present in simulation"
935 if domain
in self
._clocked
:
936 raise ValueError("Domain {!r} already has a clock driving it"
937 .format(domain
.name
))
939 half_period
= period
/ 2
941 # By default, delay the first edge by half period. This causes any synchronous activity
942 # to happen at a non-zero time, distinguishing it from the reset values in the waveform
948 # Behave correctly if the process is added after the clock signal is manipulated, or if
949 # its reset state is high.
950 initial
= (yield domain
.clk
)
952 domain
.clk
.eq(~initial
),
954 domain
.clk
.eq(initial
),
958 yield from iter(steps
)
959 self
._add
_coroutine
_process
(clk_process
, default_cmd
=None)
960 self
._clocked
.add(domain
)
963 """Reset the simulation.
965 Assign the reset value to every signal in the simulation, and restart every user process.
968 for process
in self
._processes
:
971 def _real_step(self
):
972 """Step the simulation.
974 Run every process and commit changes until a fixed point is reached. If there is
975 an unstable combinatorial loop, this function will never return.
977 # Performs the two phases of a delta cycle in a loop:
980 # 1. eval: run and suspend every non-waiting process once, queueing signal changes
981 for process
in self
._processes
:
983 process
.runnable
= False
986 for waveform_writer
in self
._waveform
_writers
:
987 for signal_state
in self
._state
.pending
:
988 waveform_writer
.update(self
._state
.timestamp
,
989 signal_state
.signal
, signal_state
.curr
)
991 # 2. commit: apply every queued signal change, waking up any waiting processes
992 converged
= self
._state
.commit()
994 # TODO(nmigen-0.4): replace with _real_step
995 @deprecated("instead of `sim.step()`, use `sim.advance()`")
997 return self
.advance()
1000 """Advance the simulation.
1002 Run every process and commit changes until a fixed point is reached, then advance time
1003 to the closest deadline (if any). If there is an unstable combinatorial loop,
1004 this function will never return.
1006 Returns ``True`` if there are any active processes, ``False`` otherwise.
1009 self
._state
.advance()
1010 return any(not process
.passive
for process
in self
._processes
)
1013 """Run the simulation while any processes are active.
1015 Processes added with :meth:`add_process` and :meth:`add_sync_process` are initially active,
1016 and may change their status using the ``yield Passive()`` and ``yield Active()`` commands.
1017 Processes compiled from HDL and added with :meth:`add_clock` are always passive.
1019 while self
.advance():
1022 def run_until(self
, deadline
, *, run_passive
=False):
1023 """Run the simulation until it advances to ``deadline``.
1025 If ``run_passive`` is ``False``, the simulation also stops when there are no active
1026 processes, similar to :meth:`run`. Otherwise, the simulation will stop only after it
1027 advances to or past ``deadline``.
1029 If the simulation stops advancing, this function will never return.
1031 assert self
._state
.timestamp
<= deadline
1032 while (self
.advance() or run_passive
) and self
._state
.timestamp
< deadline
:
1036 def write_vcd(self
, vcd_file
, gtkw_file
=None, *, traces
=()):
1037 """Write waveforms to a Value Change Dump file, optionally populating a GTKWave save file.
1039 This method returns a context manager. It can be used as: ::
1041 sim = Simulator(frag)
1043 with sim.write_vcd("dump.vcd", "dump.gtkw"):
1048 vcd_file : str or file-like object
1049 Verilog Value Change Dump file or filename.
1050 gtkw_file : str or file-like object
1051 GTKWave save file or filename.
1052 traces : iterable of Signal
1053 Signals to display traces for.
1055 if self
._state
.timestamp
!= 0.0:
1056 raise ValueError("Cannot start writing waveforms after advancing simulation time")
1057 waveform_writer
= _VCDWaveformWriter(self
._signal
_names
,
1058 vcd_file
=vcd_file
, gtkw_file
=gtkw_file
, traces
=traces
)
1059 self
._waveform
_writers
.append(waveform_writer
)
1061 waveform_writer
.close(self
._state
.timestamp
)
1062 self
._waveform
_writers
.remove(waveform_writer
)