mirror of
https://github.com/myhdl/myhdl.git
synced 2024-12-14 07:44:38 +08:00
ops
This commit is contained in:
parent
045e66ebb8
commit
fce4c7e7e5
@ -767,6 +767,8 @@ class _AnalyzeVisitor(_ConversionMixin):
|
||||
self.visit(node.body, *args)
|
||||
self.refStack.pop()
|
||||
y = node.body.nodes[0]
|
||||
if isinstance(y, astNode.Discard):
|
||||
y = y.expr
|
||||
if node.test.obj == True and \
|
||||
isinstance(y, astNode.Yield) and \
|
||||
not self.ast.hasYield > 1 and \
|
||||
|
@ -382,44 +382,28 @@ class _ConvertVisitor(_ConversionMixin):
|
||||
pre, suf = "", ""
|
||||
prel, sufl = "", ""
|
||||
prer, sufr = "", ""
|
||||
if isinstance(node.vhd, vhd_type):
|
||||
ts = node.vhd.size
|
||||
if isinstance(node.left.vhd, vhd_int) and isinstance(node.right.vhd, vhd_int):
|
||||
if isinstance(node.vhd, vhd_unsigned):
|
||||
n, o, l, r = node.vhd, node.vhdOri, node.left.vhd, node.right.vhd
|
||||
ns, os, ls, rs = n.size, o.size, l.size, r.size
|
||||
ds = ns - os
|
||||
if isinstance(o, vhd_int):
|
||||
if isinstance(n, vhd_unsigned):
|
||||
pre, suf = "to_unsigned(", ", %s)" % node.vhd.size
|
||||
elif isinstance(node.vhd, vhd_signed):
|
||||
elif isinstance(n, vhd_signed):
|
||||
pre, suf = "to_signed(", ", %s)" % node.vhd.size
|
||||
elif isinstance(node.vhd, vhd_int):
|
||||
elif isinstance(n, vhd_int):
|
||||
pass
|
||||
else:
|
||||
raise NotImplementedError
|
||||
elif isinstance(node.left.vhd, vhd_unsigned) and isinstance(node.right.vhd, vhd_int):
|
||||
if op == '*':
|
||||
ns = 2 * node.left.vhd.size
|
||||
else:
|
||||
ns = node.left.vhd.size
|
||||
ds = ts - ns
|
||||
if isinstance(node.vhd, vhd_unsigned):
|
||||
if ds > 0:
|
||||
prel, sufl = "resize(", ", %s)" % node.vhd.size
|
||||
elif ds < 0:
|
||||
pre, suf = "resize(", ", %s)" % node.vhd.size
|
||||
else:
|
||||
assert NotImplementedError
|
||||
elif isinstance(node.left.vhd, vhd_unsigned) and isinstance(node.right.vhd, vhd_unsigned):
|
||||
if op == '*':
|
||||
ns = node.left.vhd.size + node.right.vhd.size
|
||||
else:
|
||||
ns = max(node.left.vhd.size, node.right.vhd.size)
|
||||
ds = ts - ns
|
||||
if isinstance(node.vhd, vhd_unsigned):
|
||||
if ds > 0:
|
||||
prel, sufl = "resize(", ", %s)" % ts
|
||||
elif ds < 0:
|
||||
pre, suf = "resize(", ", %s)" % ts
|
||||
else:
|
||||
assertNotImplementedError
|
||||
|
||||
self.raiseError(node, "Not implemented")
|
||||
elif isinstance(l, vhd_unsigned) and isinstance(r, (vhd_int, vhd_unsigned)):
|
||||
## if op == '+':
|
||||
## print ns
|
||||
## print os
|
||||
## print ds
|
||||
if ds > 0:
|
||||
prel, sufl = "resize(", ", %s)" % ns
|
||||
elif ds < 0:
|
||||
pre, suf = "resize(", ", %s)" % ns
|
||||
|
||||
context = None
|
||||
self.write(pre)
|
||||
self.write("(")
|
||||
@ -642,13 +626,13 @@ class _ConvertVisitor(_ConversionMixin):
|
||||
"-=" : "-",
|
||||
"*=" : "*",
|
||||
"//=" : "/",
|
||||
"%=" : "%",
|
||||
"%=" : "mod",
|
||||
"**=" : "**",
|
||||
"|=" : "|",
|
||||
"|=" : "or",
|
||||
">>=" : ">>>",
|
||||
"<<=" : "<<",
|
||||
"&=" : "&",
|
||||
"^=" : "^"
|
||||
"&=" : "and",
|
||||
"^=" : "xor"
|
||||
}
|
||||
if node.op not in opmap:
|
||||
self.raiseError(node, _error.NotSupported,
|
||||
@ -1006,7 +990,10 @@ class _ConvertVisitor(_ConversionMixin):
|
||||
elif isinstance(node.vhd, vhd_unsigned):
|
||||
if vhd.size != node.vhd.size:
|
||||
s = "resize(%s, %s)" % (n, node.vhd.size)
|
||||
elif isinstance(node.vhd, vhd_boolean):
|
||||
s = "(%s /= 0)" % n
|
||||
else:
|
||||
print node.vhd
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@ -1197,9 +1184,11 @@ class _ConvertAlwaysVisitor(_ConvertVisitor):
|
||||
|
||||
def visitFunction(self, node, *args):
|
||||
w = node.code.nodes[-1]
|
||||
assert isinstance(w.body.nodes[0], astNode.Yield)
|
||||
sl = w.body.nodes[0].value
|
||||
senslist = w.body.nodes[0].senslist
|
||||
y = w.body.nodes[0]
|
||||
if isinstance(y, astNode.Discard):
|
||||
y = y.expr
|
||||
assert isinstance(y, astNode.Yield)
|
||||
senslist = y.senslist
|
||||
senslist = self.manageEdges(w.body.nodes[1], senslist)
|
||||
singleEdge = (len(senslist) == 1) and isinstance(senslist[0], _WaiterList)
|
||||
self.write("%s: process (" % self.ast.name)
|
||||
@ -1472,15 +1461,18 @@ class vhd_boolean(vhd_type):
|
||||
self.size = 1
|
||||
def toStr(self, constr=True):
|
||||
return 'boolean'
|
||||
|
||||
class vhd_vector(vhd_type):
|
||||
pass
|
||||
|
||||
class vhd_unsigned(vhd_type):
|
||||
class vhd_unsigned(vhd_vector):
|
||||
def toStr(self, constr=True):
|
||||
if constr:
|
||||
return "unsigned(%s downto 0)" % (self.size-1)
|
||||
else:
|
||||
return "unsigned"
|
||||
|
||||
class vhd_signed(vhd_type):
|
||||
class vhd_signed(vhd_vector):
|
||||
def toStr(self, constr=True):
|
||||
if constr:
|
||||
return "signed(%s downto 0)" % (self.size-1)
|
||||
@ -1594,20 +1586,46 @@ class _AnnotateTypesVisitor(_ConversionMixin):
|
||||
def binaryOp(self, node, op=None):
|
||||
self.visit(node.left)
|
||||
self.visit(node.right)
|
||||
r = node.right.vhd
|
||||
l = node.left.vhd
|
||||
if op in ('+', '-', '%'):
|
||||
s = max(l.size, r.size)
|
||||
elif op in ('*',):
|
||||
s = l.size + r.size
|
||||
r, l = node.right.vhd, node.left.vhd
|
||||
rs, ls = r.size, l.size
|
||||
if isinstance(r, vhd_signed) and isinstance(r, vhd_unsigned):
|
||||
rs += 1
|
||||
if isinstance(r, vhd_unsigned) and isinstance(r, vhd_signed):
|
||||
ls += 1
|
||||
if isinstance(r, vhd_vector) and isinstance(l, vhd_vector):
|
||||
if op in ('+', '-'):
|
||||
s = max(ls, rs)
|
||||
elif op == '%':
|
||||
s = rs
|
||||
elif op == '/':
|
||||
s = ls
|
||||
elif op == '*':
|
||||
s = ls + rs
|
||||
else:
|
||||
self.raiseError(node, "Not implemented: %s" % op)
|
||||
elif isinstance(l, vhd_vector) and isinstance(r, vhd_int):
|
||||
if op in ('+', '-', '%', '/'):
|
||||
s = ls
|
||||
elif op == '*':
|
||||
s = 2 * ls
|
||||
else:
|
||||
self.raiseError(node, "Not implemented")
|
||||
elif isinstance(l, vhd_int) and isinstance(r, vhd_vector):
|
||||
if op in ('+', '-', '%', '/'):
|
||||
s = rs
|
||||
elif op == '*':
|
||||
s = 2 * rs
|
||||
else:
|
||||
self.raiseError(node, "Not implemented: %s" % op)
|
||||
if isinstance(r, vhd_int) and isinstance(l, vhd_int):
|
||||
node.vhd = vhd_int()
|
||||
elif isinstance(r, (vhd_signed, vhd_int)) and isinstance(l, (vhd_signed, vhd_int)):
|
||||
node.vhd = vhd_signed(max(l.size, r.size))
|
||||
node.vhd = vhd_signed(s)
|
||||
elif isinstance(r, (vhd_unsigned, vhd_int)) and isinstance(l, (vhd_unsigned, vhd_int)):
|
||||
node.vhd = vhd_unsigned(max(l.size, r.size))
|
||||
node.vhd = vhd_unsigned(s)
|
||||
else:
|
||||
node.vhd = vhd_int()
|
||||
node.vhdOri = node.vhd
|
||||
|
||||
def visitAdd(self, node):
|
||||
self.binaryOp(node, op='+')
|
||||
@ -1616,7 +1634,9 @@ class _AnnotateTypesVisitor(_ConversionMixin):
|
||||
def visitMod(self, node):
|
||||
self.binaryOp(node, op='%')
|
||||
def visitMul(self, node):
|
||||
self.binaryOp(node, op='+')
|
||||
self.binaryOp(node, op='*')
|
||||
def visitFloorDiv(self, node):
|
||||
self.binaryOp(node, op='/')
|
||||
|
||||
|
||||
def multiBitOp(self, node):
|
||||
@ -1687,5 +1707,9 @@ def _annotateTypes(genlist):
|
||||
v = _AnnotateTypesVisitor(ast)
|
||||
compiler.walk(ast, v)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -32,7 +32,7 @@ import inspect
|
||||
import compiler
|
||||
from compiler import ast as astNode
|
||||
from sets import Set
|
||||
from types import GeneratorType, FunctionType, ClassType
|
||||
from types import GeneratorType, FunctionType, ClassType, TypeType
|
||||
from cStringIO import StringIO
|
||||
import __builtin__
|
||||
import warnings
|
||||
@ -529,7 +529,7 @@ class _ConvertVisitor(_ConversionMixin):
|
||||
elif f is intbv:
|
||||
self.visit(node.args[0])
|
||||
return
|
||||
elif type(f) is ClassType and issubclass(f, Exception):
|
||||
elif type(f) in (ClassType, TypeType) and issubclass(f, Exception):
|
||||
self.write(f.__name__)
|
||||
elif f in (posedge, negedge):
|
||||
opening, closing = ' ', ''
|
||||
@ -769,7 +769,7 @@ class _ConvertVisitor(_ConversionMixin):
|
||||
s = m.name
|
||||
elif isinstance(obj, EnumItemType):
|
||||
s = obj._toVerilog()
|
||||
elif type(obj) is ClassType and issubclass(obj, Exception):
|
||||
elif type(obj) in (ClassType, TypeType) and issubclass(obj, Exception):
|
||||
s = n
|
||||
else:
|
||||
self.raiseError(node, _error.UnsupportedType, "%s, %s" % (n, type(obj)))
|
||||
@ -905,8 +905,11 @@ class _ConvertAlwaysVisitor(_ConvertVisitor):
|
||||
|
||||
def visitFunction(self, node, *args):
|
||||
w = node.code.nodes[-1]
|
||||
assert isinstance(w.body.nodes[0], astNode.Yield)
|
||||
sl = w.body.nodes[0].value
|
||||
y = w.body.nodes[0]
|
||||
if isinstance(y, astNode.Discard):
|
||||
y = y.expr
|
||||
assert isinstance(y, astNode.Yield)
|
||||
sl = y.value
|
||||
self.write("always @(")
|
||||
self.visit(sl, _context.YIELD)
|
||||
self.write(") begin: %s" % self.ast.name)
|
||||
|
@ -127,7 +127,7 @@ class _VerificationClass(object):
|
||||
print >> sys.stderr, "Conversion verification succeeded"
|
||||
else:
|
||||
print >> sys.stderr, "Conversion verification failed"
|
||||
print >> sys.stderr, s ,
|
||||
# print >> sys.stderr, s ,
|
||||
return 1
|
||||
|
||||
return 0
|
||||
|
@ -32,12 +32,13 @@ def binaryOps(
|
||||
left, right):
|
||||
while 1:
|
||||
yield left, right
|
||||
## Bitand.next = left & right
|
||||
## Bitor.next = left | right
|
||||
## Bitxor.next = left ^ right
|
||||
## if right != 0:
|
||||
## FloorDiv.next = left // right
|
||||
if left < 256 and right < 40:
|
||||
Bitand.next = left & right
|
||||
Bitor.next = left | right
|
||||
Bitxor.next = left ^ right
|
||||
if right != 0:
|
||||
FloorDiv.next = left // right
|
||||
## if left < 256 and right < 40:
|
||||
if left < 256 and right < 26: # fails in ghdl for > 26
|
||||
LeftShift.next = left << right
|
||||
## if right != 0:
|
||||
## Modulo.next = left % right
|
||||
@ -136,19 +137,16 @@ def binaryBench(m, n):
|
||||
while True:
|
||||
yield left, right
|
||||
yield delay(1)
|
||||
# print "%s %s %s %s" % (left, right, Boolor, Boolor_v)
|
||||
## self.assertEqual(Bitand, Bitand_v)
|
||||
## self.assertEqual(Bitor, Bitor_v)
|
||||
## self.assertEqual(Bitxor, Bitxor_v)
|
||||
## self.assertEqual(FloorDiv, FloorDiv_v)
|
||||
|
||||
|
||||
print Bitand
|
||||
print Bitor
|
||||
print Bitxor
|
||||
print FloorDiv
|
||||
print LeftShift
|
||||
|
||||
## print Modulo, Modulo_v)
|
||||
## # print Pow, Pow_v)
|
||||
|
||||
## self.assertEqual(Modulo, Modulo_v)
|
||||
## # self.assertEqual(Pow, Pow_v)
|
||||
|
||||
|
||||
print RightShift
|
||||
print Mul
|
||||
print Sub
|
||||
@ -164,225 +162,185 @@ def binaryBench(m, n):
|
||||
|
||||
return binops, stimulus(), check()
|
||||
|
||||
|
||||
def testBinary():
|
||||
# for m, n in ((4, 4,), (5, 3), (2, 6), (8, 7)):
|
||||
for m, n in ((4, 4,),):
|
||||
yield checkBinary, m, n
|
||||
|
||||
def checkBinary(m, n):
|
||||
assert verify(binaryBench, m, n) == 0
|
||||
|
||||
def testBinary():
|
||||
for m, n in ((4, 4,), (5, 3), (2, 6), (8, 7)):
|
||||
# for m, n in ((2, 6),):
|
||||
yield checkBinary, m, n
|
||||
|
||||
|
||||
## def multiOps(
|
||||
## Bitand,
|
||||
## Bitor,
|
||||
## Bitxor,
|
||||
## Booland,
|
||||
## Boolor,
|
||||
## argm, argn, argp):
|
||||
## while 1:
|
||||
## yield argm, argn, argp
|
||||
## Bitand.next = argm & argn & argp
|
||||
## Bitor.next = argm | argn | argp
|
||||
## Bitxor.next = argm ^ argn ^ argp
|
||||
## Booland.next = bool(argm) and bool(argn) and bool(argp)
|
||||
## Boolor.next = bool(argm) and bool(argn) and bool(argp)
|
||||
|
||||
|
||||
## def multiOps_v( name,
|
||||
## Bitand,
|
||||
## Bitor,
|
||||
## Bitxor,
|
||||
## Booland,
|
||||
## Boolor,
|
||||
## argm, argn, argp):
|
||||
def multiOps(
|
||||
Bitand,
|
||||
Bitor,
|
||||
Bitxor,
|
||||
Booland,
|
||||
Boolor,
|
||||
argm, argn, argp):
|
||||
while 1:
|
||||
yield argm, argn, argp
|
||||
Bitand.next = argm & argn & argp
|
||||
Bitor.next = argm | argn | argp
|
||||
Bitxor.next = argm ^ argn ^ argp
|
||||
Booland.next = bool(argm) and bool(argn) and bool(argp)
|
||||
Boolor.next = bool(argm) and bool(argn) and bool(argp)
|
||||
|
||||
## return setupCosimulation(**locals())
|
||||
|
||||
## class TestMultiOps(TestCase):
|
||||
|
||||
## def multiBench(self, m, n, p):
|
||||
def multiBench(m, n, p):
|
||||
|
||||
## M = 2**m
|
||||
## N = 2**n
|
||||
## P = 2**p
|
||||
M = 2**m
|
||||
N = 2**n
|
||||
P = 2**p
|
||||
|
||||
## argm = Signal(intbv(0)[m:])
|
||||
## argn = Signal(intbv(0)[n:])
|
||||
## argp = Signal(intbv(0)[p:])
|
||||
## Bitand = Signal(intbv(0)[max(m, n, p):])
|
||||
## Bitand_v = Signal(intbv(0)[max(m, n, p):])
|
||||
## Bitor = Signal(intbv(0)[max(m, n, p):])
|
||||
## Bitor_v = Signal(intbv(0)[max(m, n, p):])
|
||||
## Bitxor = Signal(intbv(0)[max(m, n, p):])
|
||||
## Bitxor_v = Signal(intbv(0)[max(m, n, p):])
|
||||
## Booland, Boolor = [Signal(bool()) for i in range(2)]
|
||||
## Booland_v, Boolor_v, = [Signal(bool()) for i in range(2)]
|
||||
Q = min(M, N, P)
|
||||
seqQ = tuple(range(1, Q))
|
||||
seqM = tuple([randrange(M) for i in range(NRTESTS)])
|
||||
seqN = tuple([randrange(N) for i in range(NRTESTS)])
|
||||
seqP = tuple([randrange(P) for i in range(NRTESTS)])
|
||||
|
||||
## multiops = toVerilog(multiOps,
|
||||
## Bitand,
|
||||
## Bitor,
|
||||
## Bitxor,
|
||||
## Booland,
|
||||
## Boolor,
|
||||
## argm, argn, argp)
|
||||
## multiops_v = multiOps_v(multiOps.func_name,
|
||||
## Bitand_v,
|
||||
## Bitor_v,
|
||||
## Bitxor_v,
|
||||
## Booland_v,
|
||||
## Boolor_v,
|
||||
## argm, argn, argp)
|
||||
argm = Signal(intbv(0)[m:])
|
||||
argn = Signal(intbv(0)[n:])
|
||||
argp = Signal(intbv(0)[p:])
|
||||
Bitand = Signal(intbv(0)[max(m, n, p):])
|
||||
Bitor = Signal(intbv(0)[max(m, n, p):])
|
||||
Bitxor = Signal(intbv(0)[max(m, n, p):])
|
||||
Booland, Boolor = [Signal(bool()) for i in range(2)]
|
||||
|
||||
## def stimulus():
|
||||
## for i in range(min(M, N, P)):
|
||||
## # print i
|
||||
## argm.next = intbv(i)
|
||||
## argn.next = intbv(i)
|
||||
## argp.next = intbv(i)
|
||||
## yield delay(10)
|
||||
## for i in range(100):
|
||||
## argm.next = randrange(M)
|
||||
## argn.next = randrange(N)
|
||||
## argp.next = randrange(P)
|
||||
## yield delay(10)
|
||||
## for j, k, l in ((0, 0, 0), (0, 0, P-1), (0, N-1, P-1),
|
||||
## (M-1, 0, 0), (M-1, 0, P-1), (M-1, N-1, 0),
|
||||
## (0, N-1, 0), (M-1, N-1, P-1)):
|
||||
## argm.next = j
|
||||
## argn.next = k
|
||||
## argp.next = l
|
||||
## yield delay(10)
|
||||
multiops = multiOps(Bitand,
|
||||
Bitor,
|
||||
Bitxor,
|
||||
Booland,
|
||||
Boolor,
|
||||
argm, argn, argp)
|
||||
|
||||
## def check():
|
||||
## while 1:
|
||||
## yield argm, argn, argp
|
||||
## yield delay(1)
|
||||
## # print "%s %s %s %s %s" % (argm, argn, argp, Bitxor, Bitxor_v)
|
||||
## self.assertEqual(Bitand, Bitand_v)
|
||||
## self.assertEqual(Bitor, Bitor_v)
|
||||
## self.assertEqual(Bitxor, Bitxor_v)
|
||||
## self.assertEqual(Booland, Booland_v)
|
||||
## self.assertEqual(Boolor, Boolor_v)
|
||||
def stimulus():
|
||||
for i in range(len(seqQ)):
|
||||
argm.next = seqQ[i]
|
||||
argn.next = seqQ[i]
|
||||
argp.next = seqQ[i]
|
||||
yield delay(10)
|
||||
for i in range(NRTESTS):
|
||||
argm.next = seqM[i]
|
||||
argn.next = seqN[i]
|
||||
argp.next = seqP[i]
|
||||
yield delay(10)
|
||||
## for j, k, l in ((0, 0, 0), (0, 0, P-1), (0, N-1, P-1),
|
||||
## (M-1, 0, 0), (M-1, 0, P-1), (M-1, N-1, 0),
|
||||
## (0, N-1, 0), (M-1, N-1, P-1)):
|
||||
## argm.next = j
|
||||
## argn.next = k
|
||||
## argp.next = l
|
||||
## yield delay(10)
|
||||
|
||||
## return multiops, multiops_v, stimulus(), check()
|
||||
def check():
|
||||
while 1:
|
||||
yield argm, argn, argp
|
||||
yield delay(1)
|
||||
|
||||
print Bitand
|
||||
print Bitor
|
||||
print Bitxor
|
||||
print int(Booland)
|
||||
print int(Boolor)
|
||||
|
||||
return multiops, stimulus(), check()
|
||||
|
||||
def checkMultiOps(m, n, p):
|
||||
assert verify(multiBench, m, n, p) == 0
|
||||
|
||||
def testMultiOps():
|
||||
for m, n, p in ((4, 4, 4,), (5, 3, 2), (3, 4, 6), (3, 7, 4)):
|
||||
yield checkMultiOps, m, n, p
|
||||
|
||||
|
||||
def unaryOps(
|
||||
Not_kw,
|
||||
Invert,
|
||||
UnaryAdd,
|
||||
UnarySub,
|
||||
arg):
|
||||
while 1:
|
||||
yield arg
|
||||
Not_kw.next = not arg
|
||||
Invert.next = ~arg
|
||||
# unary operators not supported ?
|
||||
#UnaryAdd.next = +arg
|
||||
# UnarySub.next = --arg
|
||||
|
||||
def unaryBench(m):
|
||||
|
||||
M = 2**m
|
||||
seqM = tuple([randrange(M) for i in range(NRTESTS)])
|
||||
|
||||
arg = Signal(intbv(0)[m:])
|
||||
Not_kw = Signal(bool(0))
|
||||
Invert = Signal(intbv(0)[m:])
|
||||
UnaryAdd = Signal(intbv(0)[m:])
|
||||
UnarySub = Signal(intbv(0)[m:])
|
||||
|
||||
unaryops = unaryOps(Not_kw,
|
||||
Invert,
|
||||
UnaryAdd,
|
||||
UnarySub,
|
||||
arg)
|
||||
|
||||
def stimulus():
|
||||
for i in range(NRTESTS):
|
||||
arg.next = seqM[i]
|
||||
yield delay(10)
|
||||
raise StopSimulation
|
||||
|
||||
def check():
|
||||
while 1:
|
||||
yield arg
|
||||
yield delay(1)
|
||||
print int(Not_kw)
|
||||
print Invert
|
||||
# check unary operator support in vhdl
|
||||
# print UnaryAdd
|
||||
# print UnarySub
|
||||
|
||||
return unaryops, stimulus(), check()
|
||||
|
||||
def checkUnaryOps(m):
|
||||
assert verify(unaryBench, m) == 0
|
||||
|
||||
|
||||
## def testMultiOps(self):
|
||||
## for m, n, p in ((4, 4, 4,), (5, 3, 2), (3, 4, 6), (3, 7, 4)):
|
||||
## sim = self.multiBench(m, n, p)
|
||||
## Simulation(sim).run()
|
||||
def testUnaryOps():
|
||||
for m in (4, 7):
|
||||
yield checkUnaryOps, m
|
||||
|
||||
|
||||
|
||||
## def unaryOps(
|
||||
## Not,
|
||||
## Invert,
|
||||
## UnaryAdd,
|
||||
## UnarySub,
|
||||
## arg):
|
||||
## while 1:
|
||||
## yield arg
|
||||
## Not.next = not arg
|
||||
## Invert.next = ~arg
|
||||
## UnaryAdd.next = +arg
|
||||
## UnarySub.next = --arg
|
||||
|
||||
## def unaryOps_v(name,
|
||||
## Not,
|
||||
## Invert,
|
||||
## UnaryAdd,
|
||||
## UnarySub,
|
||||
## arg):
|
||||
## return setupCosimulation(**locals())
|
||||
|
||||
|
||||
|
||||
## class TestUnaryOps(TestCase):
|
||||
|
||||
## def unaryBench(self, m):
|
||||
|
||||
## M = 2**m
|
||||
|
||||
## arg = Signal(intbv(0)[m:])
|
||||
## Not = Signal(bool(0))
|
||||
## Not_v = Signal(bool(0))
|
||||
## Invert = Signal(intbv(0)[m:])
|
||||
## Invert_v = Signal(intbv(0)[m:])
|
||||
## UnaryAdd = Signal(intbv(0)[m:])
|
||||
## UnaryAdd_v = Signal(intbv(0)[m:])
|
||||
## UnarySub = Signal(intbv(0)[m:])
|
||||
## UnarySub_v = Signal(intbv(0)[m:])
|
||||
|
||||
## unaryops = toVerilog(unaryOps,
|
||||
## Not,
|
||||
## Invert,
|
||||
## UnaryAdd,
|
||||
## UnarySub,
|
||||
## arg)
|
||||
## unaryops_v = unaryOps_v(unaryOps.func_name,
|
||||
## Not_v,
|
||||
## Invert_v,
|
||||
## UnaryAdd_v,
|
||||
## UnarySub_v,
|
||||
## arg)
|
||||
|
||||
## def stimulus():
|
||||
## for i in range(M):
|
||||
## arg.next = intbv(i)
|
||||
## yield delay(10)
|
||||
## for i in range(100):
|
||||
## arg.next = randrange(M)
|
||||
## yield delay(10)
|
||||
## raise StopSimulation
|
||||
|
||||
## def check():
|
||||
## while 1:
|
||||
## yield arg
|
||||
## yield delay(1)
|
||||
## self.assertEqual(Not, Not_v)
|
||||
## self.assertEqual(Invert, Invert_v)
|
||||
## self.assertEqual(UnaryAdd, UnaryAdd_v)
|
||||
## self.assertEqual(UnarySub, UnarySub_v)
|
||||
|
||||
## return unaryops, unaryops_v, stimulus(), check()
|
||||
|
||||
## def testUnaryOps(self):
|
||||
## for m in (4, 7):
|
||||
## sim = self.unaryBench(m)
|
||||
## Simulation(sim).run()
|
||||
|
||||
|
||||
## def augmOps(
|
||||
## Bitand,
|
||||
## Bitor,
|
||||
## Bitxor,
|
||||
## FloorDiv,
|
||||
## LeftShift,
|
||||
## Modulo,
|
||||
## Mul,
|
||||
## RightShift,
|
||||
## Sub,
|
||||
## Sum,
|
||||
## left, right):
|
||||
## var = intbv(0)[max(64, len(left) + len(right)):]
|
||||
## while 1:
|
||||
## yield left, right
|
||||
## var[:] = left
|
||||
## var &= right
|
||||
## Bitand.next = var
|
||||
## var[:] = left
|
||||
## var |= right
|
||||
## Bitor.next = var
|
||||
## var[:] = left
|
||||
## var ^= left
|
||||
## Bitxor.next = var
|
||||
## if right != 0:
|
||||
## var[:] = left
|
||||
## var //= right
|
||||
## FloorDiv.next = var
|
||||
def augmOps( Bitand,
|
||||
Bitor,
|
||||
Bitxor,
|
||||
FloorDiv,
|
||||
LeftShift,
|
||||
Modulo,
|
||||
Mul,
|
||||
RightShift,
|
||||
Sub,
|
||||
Sum,
|
||||
left, right):
|
||||
var = intbv(0)[max(64, len(left) + len(right)):]
|
||||
while True:
|
||||
yield left, right
|
||||
var[:] = left
|
||||
var &= right
|
||||
Bitand.next = var
|
||||
var[:] = left
|
||||
var |= right
|
||||
Bitor.next = var
|
||||
var[:] = left
|
||||
var ^= left
|
||||
Bitxor.next = var
|
||||
if right != 0:
|
||||
var[:] = left
|
||||
var //= right
|
||||
# FloorDiv.next = var
|
||||
## if left < 256 and right < 40:
|
||||
## var[:] = left
|
||||
## var <<= right
|
||||
@ -391,126 +349,90 @@ def checkBinary(m, n):
|
||||
## var[:] = left
|
||||
## var %= right
|
||||
## Modulo.next = var
|
||||
## var[:] = left
|
||||
## var *= right
|
||||
## Mul.next = var
|
||||
## var[:] = left
|
||||
var[:] = left
|
||||
var *= right
|
||||
Mul.next = var
|
||||
var[:] = left
|
||||
## var >>= right
|
||||
## RightShift.next = var
|
||||
## if left >= right:
|
||||
## var[:] = left
|
||||
## var -= right
|
||||
## Sub.next = var
|
||||
## var[:] = left
|
||||
## var += right
|
||||
## Sum.next = var
|
||||
if left >= right:
|
||||
var[:] = left
|
||||
var -= right
|
||||
Sub.next = var
|
||||
var[:] = left
|
||||
var += right
|
||||
Sum.next = var
|
||||
|
||||
|
||||
|
||||
|
||||
## def augmOps_v( name,
|
||||
## Bitand,
|
||||
## Bitor,
|
||||
## Bitxor,
|
||||
## FloorDiv,
|
||||
## LeftShift,
|
||||
## Modulo,
|
||||
## Mul,
|
||||
## RightShift,
|
||||
## Sub,
|
||||
## Sum,
|
||||
## left, right):
|
||||
## return setupCosimulation(**locals())
|
||||
def augmBench(m, n):
|
||||
|
||||
## class TestAugmOps(TestCase):
|
||||
|
||||
## def augmBench(self, m, n):
|
||||
|
||||
## M = 2**m
|
||||
## N = 2**n
|
||||
|
||||
## left = Signal(intbv(0)[m:])
|
||||
## right = Signal(intbv(0)[n:])
|
||||
## Bitand = Signal(intbv(0)[max(m, n):])
|
||||
## Bitand_v = Signal(intbv(0)[max(m, n):])
|
||||
## Bitor = Signal(intbv(0)[max(m, n):])
|
||||
## Bitor_v = Signal(intbv(0)[max(m, n):])
|
||||
## Bitxor = Signal(intbv(0)[max(m, n):])
|
||||
## Bitxor_v = Signal(intbv(0)[max(m, n):])
|
||||
## FloorDiv = Signal(intbv(0)[m:])
|
||||
## FloorDiv_v = Signal(intbv(0)[m:])
|
||||
## LeftShift = Signal(intbv(0)[64:])
|
||||
## LeftShift_v = Signal(intbv(0)[64:])
|
||||
## Modulo = Signal(intbv(0)[m:])
|
||||
## Modulo_v = Signal(intbv(0)[m:])
|
||||
## Mul = Signal(intbv(0)[m+n:])
|
||||
## Mul_v = Signal(intbv(0)[m+n:])
|
||||
## RightShift = Signal(intbv(0)[m:])
|
||||
## RightShift_v = Signal(intbv(0)[m:])
|
||||
## Sub = Signal(intbv(0)[max(m, n):])
|
||||
## Sub_v = Signal(intbv(0)[max(m, n):])
|
||||
## Sum = Signal(intbv(0)[max(m, n)+1:])
|
||||
## Sum_v = Signal(intbv(0)[max(m, n)+1:])
|
||||
|
||||
## augmops = toVerilog(augmOps,
|
||||
## Bitand,
|
||||
## Bitor,
|
||||
## Bitxor,
|
||||
## FloorDiv,
|
||||
## LeftShift,
|
||||
## Modulo,
|
||||
## Mul,
|
||||
## RightShift,
|
||||
## Sub,
|
||||
## Sum,
|
||||
## left, right)
|
||||
## augmops_v = augmOps_v( augmOps.func_name,
|
||||
## Bitand_v,
|
||||
## Bitor_v,
|
||||
## Bitxor_v,
|
||||
## FloorDiv_v,
|
||||
## LeftShift_v,
|
||||
## Modulo_v,
|
||||
## Mul_v,
|
||||
## RightShift_v,
|
||||
## Sub_v,
|
||||
## Sum_v,
|
||||
## left, right)
|
||||
|
||||
## def stimulus():
|
||||
## for i in range(min(M, N)):
|
||||
## # print i
|
||||
## left.next = intbv(i)
|
||||
## right.next = intbv(i)
|
||||
## yield delay(10)
|
||||
## for i in range(100):
|
||||
## left.next = randrange(M)
|
||||
## right.next = randrange(N)
|
||||
## yield delay(10)
|
||||
## for j, k in ((0, 0), (0, N-1), (M-1, 0), (M-1, N-1)):
|
||||
## left.next = j
|
||||
## right.next = k
|
||||
## yield delay(10)
|
||||
|
||||
## def check():
|
||||
## while 1:
|
||||
## yield left, right
|
||||
## yield delay(1)
|
||||
## # print "%s %s %s %s" % (left, right, Boolor, Boolor_v)
|
||||
## self.assertEqual(Bitand, Bitand_v)
|
||||
## self.assertEqual(Bitor, Bitor_v)
|
||||
## self.assertEqual(Bitxor, Bitxor_v)
|
||||
## self.assertEqual(FloorDiv, FloorDiv_v)
|
||||
## self.assertEqual(LeftShift, LeftShift_v)
|
||||
## self.assertEqual(Modulo, Modulo_v)
|
||||
## self.assertEqual(Mul, Mul_v)
|
||||
## self.assertEqual(RightShift, RightShift_v)
|
||||
## self.assertEqual(Sub, Sub_v)
|
||||
## self.assertEqual(Sum, Sum_v)
|
||||
|
||||
## return augmops, augmops_v, stimulus(), check()
|
||||
M = 2**m
|
||||
N = 2**n
|
||||
|
||||
seqM = tuple([randrange(M) for i in range(NRTESTS)])
|
||||
seqN = tuple([randrange(N) for i in range(NRTESTS)])
|
||||
|
||||
## def testAugmOps(self):
|
||||
## for m, n in ((4, 4,), (5, 3), (2, 6), (8, 7)):
|
||||
## sim = self.augmBench(m, n)
|
||||
## Simulation(sim).run()
|
||||
left = Signal(intbv(0)[m:])
|
||||
right = Signal(intbv(0)[n:])
|
||||
Bitand = Signal(intbv(0)[max(m, n):])
|
||||
Bitor = Signal(intbv(0)[max(m, n):])
|
||||
Bitxor = Signal(intbv(0)[max(m, n):])
|
||||
FloorDiv = Signal(intbv(0)[m:])
|
||||
LeftShift = Signal(intbv(0)[64:])
|
||||
Modulo = Signal(intbv(0)[m:])
|
||||
Mul = Signal(intbv(0)[m+n:])
|
||||
RightShift = Signal(intbv(0)[m:])
|
||||
Sub = Signal(intbv(0)[max(m, n):])
|
||||
Sum = Signal(intbv(0)[max(m, n)+1:])
|
||||
|
||||
augmops = augmOps( Bitand,
|
||||
Bitor,
|
||||
Bitxor,
|
||||
FloorDiv,
|
||||
LeftShift,
|
||||
Modulo,
|
||||
Mul,
|
||||
RightShift,
|
||||
Sub,
|
||||
Sum,
|
||||
left, right)
|
||||
|
||||
def stimulus():
|
||||
for i in range(NRTESTS):
|
||||
left.next = seqM[i]
|
||||
right.next = seqN[i]
|
||||
yield delay(10)
|
||||
## for j, k in ((0, 0), (0, N-1), (M-1, 0), (M-1, N-1)):
|
||||
## left.next = j
|
||||
## right.next = k
|
||||
## yield delay(10)
|
||||
|
||||
|
||||
def check():
|
||||
while True:
|
||||
yield left, right
|
||||
yield delay(1)
|
||||
print Bitand
|
||||
print Bitor
|
||||
print Bitxor
|
||||
# print FloorDiv
|
||||
## print LeftShift
|
||||
# print Modulo
|
||||
print Mul
|
||||
## print RightShift
|
||||
print Sub
|
||||
print Sum
|
||||
|
||||
return augmops, stimulus(), check()
|
||||
|
||||
|
||||
def checkAugmOps(m, n):
|
||||
assert verify(augmBench, m, n) == 0
|
||||
|
||||
## def testAugmOps():
|
||||
## for m, n in ((4, 4,), (5, 3), (2, 6), (8, 7)):
|
||||
## yield checkAugmOps, m, n
|
||||
|
||||
# Simulation(augmBench(4, 4))
|
||||
|
Loading…
x
Reference in New Issue
Block a user