2015-09-10 23:33:45 -04:00
|
|
|
import operator
|
2015-10-19 07:17:26 -04:00
|
|
|
import collections
|
2015-10-20 04:37:54 -04:00
|
|
|
import inspect
|
2015-09-10 23:33:45 -04:00
|
|
|
|
2015-09-12 07:34:07 -04:00
|
|
|
from migen.fhdl.structure import *
|
2015-09-20 03:04:15 -04:00
|
|
|
from migen.fhdl.structure import (_Value, _Statement,
|
|
|
|
_Operator, _Slice, _ArrayProxy,
|
2015-09-19 02:56:26 -04:00
|
|
|
_Assign, _Fragment)
|
2015-10-19 08:08:46 -04:00
|
|
|
from migen.fhdl.bitcontainer import value_bits_sign
|
2015-09-21 10:13:36 -04:00
|
|
|
from migen.fhdl.tools import list_signals, list_targets, insert_resets
|
2015-09-20 02:52:26 -04:00
|
|
|
from migen.fhdl.simplify import MemoryToArray
|
|
|
|
from migen.fhdl.specials import _MemoryLocation
|
2015-09-21 09:20:31 -04:00
|
|
|
from migen.sim.vcd import VCDWriter, DummyVCDWriter
|
2015-09-12 07:34:07 -04:00
|
|
|
|
|
|
|
|
2015-09-10 23:33:45 -04:00
|
|
|
class ClockState:
|
2015-09-21 09:52:13 -04:00
|
|
|
def __init__(self, high, half_period, time_before_trans):
|
|
|
|
self.high = high
|
|
|
|
self.half_period = half_period
|
|
|
|
self.time_before_trans = time_before_trans
|
2015-09-10 23:33:45 -04:00
|
|
|
|
|
|
|
|
|
|
|
class TimeManager:
|
|
|
|
def __init__(self, description):
|
|
|
|
self.clocks = dict()
|
|
|
|
|
2015-09-21 09:52:13 -04:00
|
|
|
for k, period_phase in description.items():
|
|
|
|
if isinstance(period_phase, tuple):
|
|
|
|
period, phase = period_phase
|
|
|
|
else:
|
|
|
|
period = period_phase
|
|
|
|
phase = 0
|
|
|
|
half_period = period//2
|
|
|
|
if phase >= half_period:
|
|
|
|
phase -= half_period
|
|
|
|
high = True
|
|
|
|
else:
|
|
|
|
high = False
|
|
|
|
self.clocks[k] = ClockState(high, half_period, half_period - phase)
|
2015-09-10 23:33:45 -04:00
|
|
|
|
|
|
|
def tick(self):
|
2015-09-21 09:52:13 -04:00
|
|
|
rising = set()
|
|
|
|
falling = set()
|
|
|
|
dt = min(cs.time_before_trans for cs in self.clocks.values())
|
2015-09-10 23:33:45 -04:00
|
|
|
for k, cs in self.clocks.items():
|
2015-09-21 09:52:13 -04:00
|
|
|
if cs.time_before_trans == dt:
|
|
|
|
cs.high = not cs.high
|
|
|
|
if cs.high:
|
|
|
|
rising.add(k)
|
|
|
|
else:
|
|
|
|
falling.add(k)
|
|
|
|
cs.time_before_trans -= dt
|
|
|
|
if not cs.time_before_trans:
|
|
|
|
cs.time_before_trans += cs.half_period
|
|
|
|
return dt, rising, falling
|
2015-09-10 23:33:45 -04:00
|
|
|
|
|
|
|
|
|
|
|
str2op = {
|
|
|
|
"~": operator.invert,
|
|
|
|
"+": operator.add,
|
|
|
|
"-": operator.sub,
|
|
|
|
"*": operator.mul,
|
|
|
|
|
|
|
|
">>>": operator.rshift,
|
|
|
|
"<<<": operator.lshift,
|
|
|
|
|
|
|
|
"&": operator.and_,
|
|
|
|
"^": operator.xor,
|
|
|
|
"|": operator.or_,
|
|
|
|
|
|
|
|
"<": operator.lt,
|
|
|
|
"<=": operator.le,
|
|
|
|
"==": operator.eq,
|
|
|
|
"!=": operator.ne,
|
|
|
|
">": operator.gt,
|
|
|
|
">=": operator.ge,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-19 08:08:46 -04:00
|
|
|
def _truncate(value, nbits, signed):
|
|
|
|
value = value & (2**nbits - 1)
|
|
|
|
if signed and (value & 2**(nbits - 1)):
|
|
|
|
value -= 2**nbits
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
2015-09-10 23:33:45 -04:00
|
|
|
class Evaluator:
|
2015-09-21 10:13:36 -04:00
|
|
|
def __init__(self, clock_domains, replaced_memories):
|
|
|
|
self.clock_domains = clock_domains
|
2015-09-20 02:52:26 -04:00
|
|
|
self.replaced_memories = replaced_memories
|
2015-09-10 23:33:45 -04:00
|
|
|
self.signal_values = dict()
|
|
|
|
self.modifications = dict()
|
|
|
|
|
|
|
|
def commit(self):
|
|
|
|
r = set()
|
|
|
|
for k, v in self.modifications.items():
|
|
|
|
if k not in self.signal_values or self.signal_values[k] != v:
|
|
|
|
self.signal_values[k] = v
|
|
|
|
r.add(k)
|
|
|
|
self.modifications.clear()
|
|
|
|
return r
|
|
|
|
|
2015-09-19 02:56:26 -04:00
|
|
|
def eval(self, node, postcommit=False):
|
2015-09-15 00:38:02 -04:00
|
|
|
if isinstance(node, Constant):
|
|
|
|
return node.value
|
2015-09-10 23:33:45 -04:00
|
|
|
elif isinstance(node, Signal):
|
2015-09-19 02:56:26 -04:00
|
|
|
if postcommit:
|
|
|
|
try:
|
|
|
|
return self.modifications[node]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2015-09-10 23:33:45 -04:00
|
|
|
try:
|
|
|
|
return self.signal_values[node]
|
|
|
|
except KeyError:
|
2015-09-17 03:20:27 -04:00
|
|
|
return node.reset.value
|
2015-09-10 23:33:45 -04:00
|
|
|
elif isinstance(node, _Operator):
|
2015-09-19 02:56:26 -04:00
|
|
|
operands = [self.eval(o, postcommit) for o in node.operands]
|
2015-09-10 23:33:45 -04:00
|
|
|
if node.op == "-":
|
|
|
|
if len(operands) == 1:
|
|
|
|
return -operands[0]
|
|
|
|
else:
|
|
|
|
return operands[0] - operands[1]
|
2015-09-17 02:39:36 -04:00
|
|
|
elif node.op == "m":
|
|
|
|
return operands[1] if operands[0] else operands[2]
|
2015-09-10 23:33:45 -04:00
|
|
|
else:
|
|
|
|
return str2op[node.op](*operands)
|
2015-09-17 02:39:36 -04:00
|
|
|
elif isinstance(node, _Slice):
|
2015-09-19 02:56:26 -04:00
|
|
|
v = self.eval(node.value, postcommit)
|
2015-09-17 02:39:36 -04:00
|
|
|
idx = range(node.start, node.stop)
|
|
|
|
return sum(((v >> i) & 1) << j for j, i in enumerate(idx))
|
|
|
|
elif isinstance(node, Cat):
|
|
|
|
shift = 0
|
|
|
|
r = 0
|
|
|
|
for element in node.l:
|
2015-09-26 06:45:10 -04:00
|
|
|
nbits = len(element)
|
2015-09-17 02:39:36 -04:00
|
|
|
# make value always positive
|
2015-09-19 02:56:26 -04:00
|
|
|
r |= (self.eval(element, postcommit) & (2**nbits-1)) << shift
|
2015-09-17 02:39:36 -04:00
|
|
|
shift += nbits
|
|
|
|
return r
|
2015-09-19 02:56:26 -04:00
|
|
|
elif isinstance(node, _ArrayProxy):
|
|
|
|
return self.eval(node.choices[self.eval(node.key, postcommit)],
|
|
|
|
postcommit)
|
2015-09-20 02:52:26 -04:00
|
|
|
elif isinstance(node, _MemoryLocation):
|
|
|
|
array = self.replaced_memories[node.memory]
|
|
|
|
return self.eval(array[self.eval(node.index, postcommit)], postcommit)
|
2015-09-21 10:13:36 -04:00
|
|
|
elif isinstance(node, ClockSignal):
|
|
|
|
return self.eval(self.clock_domains[node.cd].clk, postcommit)
|
|
|
|
elif isinstance(node, ResetSignal):
|
|
|
|
rst = self.clock_domains[node.cd].rst
|
|
|
|
if rst is None:
|
|
|
|
if node.allow_reset_less:
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
raise ValueError("Attempted to get reset signal of resetless"
|
|
|
|
" domain '{}'".format(node.cd))
|
|
|
|
else:
|
|
|
|
return self.eval(rst, postcommit)
|
2015-09-10 23:33:45 -04:00
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2015-09-19 02:56:26 -04:00
|
|
|
def assign(self, node, value):
|
|
|
|
if isinstance(node, Signal):
|
|
|
|
assert not node.variable
|
2015-10-19 08:08:46 -04:00
|
|
|
self.modifications[node] = _truncate(value,
|
|
|
|
node.nbits, node.signed)
|
2015-09-19 02:56:26 -04:00
|
|
|
elif isinstance(node, Cat):
|
|
|
|
for element in node.l:
|
2015-09-26 06:45:10 -04:00
|
|
|
nbits = len(element)
|
2015-09-19 02:56:26 -04:00
|
|
|
self.assign(element, value & (2**nbits-1))
|
2015-10-19 04:08:42 -04:00
|
|
|
value >>= nbits
|
2015-09-19 11:21:46 -04:00
|
|
|
elif isinstance(node, _Slice):
|
2015-09-22 08:33:44 -04:00
|
|
|
full_value = self.eval(node.value, True)
|
2015-09-19 02:56:26 -04:00
|
|
|
# clear bits assigned to by the slice
|
|
|
|
full_value &= ~((2**node.stop-1) - (2**node.start-1))
|
|
|
|
# set them to the new value
|
|
|
|
value &= 2**(node.stop - node.start)-1
|
|
|
|
full_value |= value << node.start
|
2015-09-22 08:33:44 -04:00
|
|
|
self.assign(node.value, full_value)
|
2015-09-19 02:56:26 -04:00
|
|
|
elif isinstance(node, _ArrayProxy):
|
|
|
|
self.assign(node.choices[self.eval(node.key)], value)
|
2015-09-20 02:52:26 -04:00
|
|
|
elif isinstance(node, _MemoryLocation):
|
|
|
|
array = self.replaced_memories[node.memory]
|
|
|
|
self.assign(array[self.eval(node.index)], value)
|
2015-09-19 02:56:26 -04:00
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
2015-09-11 00:44:14 -04:00
|
|
|
|
2015-09-10 23:33:45 -04:00
|
|
|
def execute(self, statements):
|
|
|
|
for s in statements:
|
|
|
|
if isinstance(s, _Assign):
|
2015-09-19 02:56:26 -04:00
|
|
|
self.assign(s.l, self.eval(s.r))
|
2015-09-10 23:33:45 -04:00
|
|
|
elif isinstance(s, If):
|
2015-10-19 03:58:21 -04:00
|
|
|
if self.eval(s.cond) & (2**len(s.cond) - 1):
|
2015-09-10 23:33:45 -04:00
|
|
|
self.execute(s.t)
|
|
|
|
else:
|
|
|
|
self.execute(s.f)
|
2015-09-17 05:25:06 -04:00
|
|
|
elif isinstance(s, Case):
|
2015-10-19 08:08:46 -04:00
|
|
|
nbits, signed = value_bits_sign(s.test)
|
|
|
|
test = _truncate(self.eval(s.test), nbits, signed)
|
2015-10-20 05:18:33 -04:00
|
|
|
found = False
|
2015-09-17 05:25:06 -04:00
|
|
|
for k, v in s.cases.items():
|
|
|
|
if isinstance(k, Constant) and k.value == test:
|
|
|
|
self.execute(v)
|
2015-10-20 05:18:33 -04:00
|
|
|
found = True
|
|
|
|
break
|
|
|
|
if not found and "default" in s.cases:
|
2015-09-17 05:25:06 -04:00
|
|
|
self.execute(s.cases["default"])
|
2015-10-19 07:17:26 -04:00
|
|
|
elif isinstance(s, collections.Iterable):
|
2015-10-15 01:53:04 -04:00
|
|
|
self.execute(s)
|
2015-09-10 23:33:45 -04:00
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: instances via Iverilog/VPI
|
|
|
|
class Simulator:
|
2015-09-21 09:20:31 -04:00
|
|
|
def __init__(self, fragment_or_module, generators, clocks={"sys": 10}, vcd_name=None):
|
2015-09-10 23:33:45 -04:00
|
|
|
if isinstance(fragment_or_module, _Fragment):
|
|
|
|
self.fragment = fragment_or_module
|
|
|
|
else:
|
|
|
|
self.fragment = fragment_or_module.get_fragment()
|
|
|
|
if not isinstance(generators, dict):
|
|
|
|
generators = {"sys": generators}
|
|
|
|
self.generators = dict()
|
|
|
|
for k, v in generators.items():
|
2015-10-19 07:21:20 -04:00
|
|
|
if (isinstance(v, collections.Iterable)
|
2015-10-20 04:37:54 -04:00
|
|
|
and not inspect.isgenerator(v)):
|
2015-10-19 07:18:17 -04:00
|
|
|
self.generators[k] = list(v)
|
2015-09-10 23:33:45 -04:00
|
|
|
else:
|
|
|
|
self.generators[k] = [v]
|
|
|
|
|
2015-09-21 09:52:13 -04:00
|
|
|
self.time = TimeManager(clocks)
|
|
|
|
for clock in clocks.keys():
|
|
|
|
if clock not in self.fragment.clock_domains:
|
|
|
|
cd = ClockDomain(name=clock, reset_less=True)
|
|
|
|
cd.clk.reset = C(self.time.clocks[clock].high)
|
|
|
|
self.fragment.clock_domains.append(cd)
|
|
|
|
|
2015-09-20 02:52:26 -04:00
|
|
|
mta = MemoryToArray()
|
|
|
|
mta.transform_fragment(None, self.fragment)
|
2015-09-21 10:13:36 -04:00
|
|
|
insert_resets(self.fragment)
|
2015-09-19 02:56:26 -04:00
|
|
|
# comb signals return to their reset value if nothing assigns them
|
2015-09-17 05:24:20 -04:00
|
|
|
self.fragment.comb[0:0] = [s.eq(s.reset)
|
|
|
|
for s in list_targets(self.fragment.comb)]
|
2015-09-21 10:13:36 -04:00
|
|
|
self.evaluator = Evaluator(self.fragment.clock_domains,
|
|
|
|
mta.replacements)
|
2015-09-17 05:24:20 -04:00
|
|
|
|
2015-09-21 09:20:31 -04:00
|
|
|
if vcd_name is None:
|
|
|
|
self.vcd = DummyVCDWriter()
|
|
|
|
else:
|
2015-09-21 09:52:13 -04:00
|
|
|
signals = list_signals(self.fragment)
|
|
|
|
for cd in self.fragment.clock_domains:
|
|
|
|
signals.add(cd.clk)
|
|
|
|
if cd.rst is not None:
|
|
|
|
signals.add(cd.rst)
|
2015-10-05 00:24:32 -04:00
|
|
|
for memory_array in mta.replacements.values():
|
|
|
|
signals |= set(memory_array)
|
2015-09-21 09:52:13 -04:00
|
|
|
signals = sorted(signals, key=lambda x: x.duid)
|
2015-09-21 09:20:31 -04:00
|
|
|
self.vcd = VCDWriter(vcd_name, signals)
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, type, value, traceback):
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.vcd.close()
|
|
|
|
|
2015-09-12 04:27:59 -04:00
|
|
|
def _commit_and_comb_propagate(self):
|
2015-09-17 05:24:20 -04:00
|
|
|
# TODO: optimize
|
2015-09-21 09:20:31 -04:00
|
|
|
all_modified = set()
|
2015-09-12 04:27:59 -04:00
|
|
|
modified = self.evaluator.commit()
|
2015-09-21 09:20:31 -04:00
|
|
|
all_modified |= modified
|
2015-09-10 23:33:45 -04:00
|
|
|
while modified:
|
2015-09-17 05:24:20 -04:00
|
|
|
self.evaluator.execute(self.fragment.comb)
|
2015-09-10 23:33:45 -04:00
|
|
|
modified = self.evaluator.commit()
|
2015-09-21 09:20:31 -04:00
|
|
|
all_modified |= modified
|
|
|
|
for signal in all_modified:
|
|
|
|
self.vcd.set(signal, self.evaluator.signal_values[signal])
|
2015-09-10 23:33:45 -04:00
|
|
|
|
2015-09-20 03:04:15 -04:00
|
|
|
def _evalexec_nested_lists(self, x):
|
2015-09-12 04:01:53 -04:00
|
|
|
if isinstance(x, list):
|
2015-09-20 03:04:15 -04:00
|
|
|
return [self._evalexec_nested_lists(e) for e in x]
|
2015-09-20 02:52:26 -04:00
|
|
|
elif isinstance(x, _Value):
|
2015-09-12 04:01:53 -04:00
|
|
|
return self.evaluator.eval(x)
|
2015-09-20 03:04:15 -04:00
|
|
|
elif isinstance(x, _Statement):
|
|
|
|
self.evaluator.execute([x])
|
|
|
|
return None
|
2015-09-12 04:01:53 -04:00
|
|
|
else:
|
|
|
|
raise ValueError
|
|
|
|
|
2015-09-11 00:44:14 -04:00
|
|
|
def _process_generators(self, cd):
|
2015-09-12 03:12:57 -04:00
|
|
|
exhausted = []
|
|
|
|
for generator in self.generators[cd]:
|
|
|
|
reply = None
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
request = generator.send(reply)
|
|
|
|
if request is None:
|
|
|
|
break # next cycle
|
|
|
|
else:
|
2015-09-20 03:04:15 -04:00
|
|
|
reply = self._evalexec_nested_lists(request)
|
2015-09-12 03:12:57 -04:00
|
|
|
except StopIteration:
|
|
|
|
exhausted.append(generator)
|
|
|
|
break
|
|
|
|
for generator in exhausted:
|
|
|
|
self.generators[cd].remove(generator)
|
2015-09-11 00:44:14 -04:00
|
|
|
|
2015-09-10 23:33:45 -04:00
|
|
|
def _continue_simulation(self):
|
|
|
|
# TODO: passive generators
|
|
|
|
return any(self.generators.values())
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
self.evaluator.execute(self.fragment.comb)
|
2015-09-12 04:27:59 -04:00
|
|
|
self._commit_and_comb_propagate()
|
2015-09-10 23:33:45 -04:00
|
|
|
|
|
|
|
while True:
|
2015-09-21 09:52:13 -04:00
|
|
|
dt, rising, falling = self.time.tick()
|
2015-09-21 09:20:31 -04:00
|
|
|
self.vcd.delay(dt)
|
2015-09-21 09:52:13 -04:00
|
|
|
for cd in rising:
|
|
|
|
self.evaluator.assign(self.fragment.clock_domains[cd].clk, 1)
|
2015-09-12 03:12:57 -04:00
|
|
|
if cd in self.fragment.sync:
|
|
|
|
self.evaluator.execute(self.fragment.sync[cd])
|
|
|
|
if cd in self.generators:
|
|
|
|
self._process_generators(cd)
|
2015-09-21 09:52:13 -04:00
|
|
|
for cd in falling:
|
|
|
|
self.evaluator.assign(self.fragment.clock_domains[cd].clk, 0)
|
2015-09-12 04:27:59 -04:00
|
|
|
self._commit_and_comb_propagate()
|
2015-09-11 00:44:14 -04:00
|
|
|
|
2015-09-10 23:33:45 -04:00
|
|
|
if not self._continue_simulation():
|
|
|
|
break
|
2015-09-21 09:20:31 -04:00
|
|
|
|
|
|
|
|
|
|
|
def run_simulation(*args, **kwargs):
|
|
|
|
with Simulator(*args, **kwargs) as s:
|
|
|
|
s.run()
|