sim: support eval of slice, cat and mux

This commit is contained in:
Sebastien Bourdeauducq 2015-09-17 14:39:36 +08:00
parent 9dd3200ba2
commit 6e08df75ee
1 changed files with 18 additions and 2 deletions

View File

@ -2,7 +2,8 @@ import operator
from collections import defaultdict from collections import defaultdict
from migen.fhdl.structure import * from migen.fhdl.structure import *
from migen.fhdl.structure import _Operator, _Assign, _Fragment from migen.fhdl.structure import _Operator, _Slice, _Assign, _Fragment
from migen.fhdl.bitcontainer import flen
from migen.fhdl.tools import list_inputs from migen.fhdl.tools import list_inputs
@ -87,10 +88,25 @@ class Evaluator:
return -operands[0] return -operands[0]
else: else:
return operands[0] - operands[1] return operands[0] - operands[1]
elif node.op == "m":
return operands[1] if operands[0] else operands[2]
else: else:
return str2op[node.op](*operands) return str2op[node.op](*operands)
elif isinstance(node, _Slice):
v = self.eval(node.value)
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:
nbits = flen(element)
# make value always positive
r |= (self.eval(element) & (2**nbits-1)) << shift
shift += nbits
return r
else: else:
# TODO: Cat, Slice, Array, ClockSignal, ResetSignal, Memory # TODO: Array, ClockSignal, ResetSignal, Memory
raise NotImplementedError raise NotImplementedError
def assign(self, signal, value): def assign(self, signal, value):