wrpll/thls: implement signed numbers

This commit is contained in:
Sebastien Bourdeauducq 2019-08-15 17:04:17 +08:00
parent efc43142a6
commit 19620948bf
1 changed files with 26 additions and 11 deletions

View File

@ -132,13 +132,28 @@ class ASTCompiler:
output = self.get_ssa_reg() output = self.get_ssa_reg()
self.program.append(cons(inputs=inputs, outputs=[output])) self.program.append(cons(inputs=inputs, outputs=[output]))
return output return output
elif isinstance(node, ast.Num): elif isinstance(node, (ast.Num, ast.UnaryOp)):
if node.n in self.constants: if isinstance(node, ast.UnaryOp):
return self.constants[node.n] if not isinstance(node.operand, ast.Num):
raise NotImplementedError
if isinstance(node.op, ast.UAdd):
transform = lambda x: x
elif isinstance(node.op, ast.USub):
transform = operator.neg
elif isinstance(node.op, ast.Invert):
transform = operator.invert
else:
raise NotImplementedError
node = node.operand
else:
transform = lambda x: x
n = transform(node.n)
if n in self.constants:
return self.constants[n]
else: else:
r = len(self.data) r = len(self.data)
self.data.append(node.n) self.data.append(n)
self.constants[node.n] = r self.constants[n] = r
return r return r
elif isinstance(node, ast.Name): elif isinstance(node, ast.Name):
return self.names[node.id] return self.names[node.id]
@ -357,10 +372,10 @@ def compile(processor, function):
class BaseUnit(Module): class BaseUnit(Module):
def __init__(self, data_width): def __init__(self, data_width):
self.stb_i = Signal() self.stb_i = Signal()
self.i0 = Signal(data_width) self.i0 = Signal((data_width, True))
self.i1 = Signal(data_width) self.i1 = Signal((data_width, True))
self.stb_o = Signal() self.stb_o = Signal()
self.o = Signal(data_width) self.o = Signal((data_width, True))
class NopUnit(BaseUnit): class NopUnit(BaseUnit):
@ -436,10 +451,10 @@ class OutputUnit(BaseUnit):
class ProcessorImpl(Module): class ProcessorImpl(Module):
def __init__(self, pd, program, data): def __init__(self, pd, program, data):
self.input_stb = Signal() self.input_stb = Signal()
self.input = Signal(pd.data_width) self.input = Signal((pd.data_width, True))
self.output_stb = Signal() self.output_stb = Signal()
self.output = Signal(pd.data_width) self.output = Signal((pd.data_width, True))
self.busy = Signal() self.busy = Signal()
@ -569,7 +584,7 @@ def foo(x):
def simple_test(x): def simple_test(x):
return min((x*2 >> 1) + 2, 10) return min((x*-2 >> 1) + 2 - 1000, 10)
if __name__ == "__main__": if __name__ == "__main__":