from operator import or_
from functools import reduce
+from pipeline import ObjectProxy
+
+
class MultiShiftR:
def __init__(self, width):
self.s = Signal(reset_less=True) # Sign bit
self.mzero = Const(0, (m_width, False))
+ m_msb = 1<<(self.m_width-2)
+ self.msb1 = Const(m_msb, (m_width, False))
self.m1s = Const(-1, (m_width, False))
self.P128 = Const(e_max, (e_width, True))
self.P127 = Const(e_max-1, (e_width, True))
def zero(self, s):
return self.create(s, self.N127, 0)
+ def create2(self, s, e, m):
+ """ creates a value from sign / exponent / mantissa
+
+ bias is added here, to the exponent
+ """
+ e = e + self.P127 # exp (add on bias)
+ return Cat(m[0:self.e_start],
+ e[0:self.e_end-self.e_start],
+ s)
+
+ def nan2(self, s):
+ return self.create2(s, self.P128, self.msb1)
+
+ def inf2(self, s):
+ return self.create2(s, self.P128, self.mzero)
+
+ def zero2(self, s):
+ return self.create2(s, self.N127, self.mzero)
+
class MultiShiftRMerge:
""" shifts down (right) and merges lower bits into m[0].
self.m.eq(sm.lshift(self.m, maxslen))
]
-class FPNumIn(FPNumBase):
+
+class FPNumDecode(FPNumBase):
""" Floating-point Number Class
Contains signals for an incoming copy of the value, decoded into
"""
def __init__(self, op, width, m_extra=True):
FPNumBase.__init__(self, width, m_extra)
- self.latch_in = Signal()
self.op = op
def elaborate(self, platform):
m = FPNumBase.elaborate(self, platform)
- #m.d.comb += self.latch_in.eq(self.op.ack & self.op.stb)
- #with m.If(self.latch_in):
- # m.d.sync += self.decode(self.v)
+ m.d.comb += self.decode(self.v)
return m
self.s.eq(v[-1]), # sign
]
+class FPNumIn(FPNumBase):
+ """ Floating-point Number Class
+
+ Contains signals for an incoming copy of the value, decoded into
+ sign / exponent / mantissa.
+ Also contains encoding functions, creation and recognition of
+ zero, NaN and inf (all signed)
+
+ Four extra bits are included in the mantissa: the top bit
+ (m[-1]) is effectively a carry-overflow. The other three are
+ guard (m[2]), round (m[1]), and sticky (m[0])
+ """
+ def __init__(self, op, width, m_extra=True):
+ FPNumBase.__init__(self, width, m_extra)
+ self.latch_in = Signal()
+ self.op = op
+
+ def decode2(self, m):
+ """ decodes a latched value into sign / exponent / mantissa
+
+ bias is subtracted here, from the exponent. exponent
+ is extended to 10 bits so that subtract 127 is done on
+ a 10-bit number
+ """
+ v = self.v
+ args = [0] * self.m_extra + [v[0:self.e_start]] # pad with extra zeros
+ #print ("decode", self.e_end)
+ res = ObjectProxy(m, pipemode=False)
+ res.m = Cat(*args) # mantissa
+ res.e = v[self.e_start:self.e_end] - self.P127 # exp
+ res.s = v[-1] # sign
+ return res
+
+ def decode(self, v):
+ """ decodes a latched value into sign / exponent / mantissa
+
+ bias is subtracted here, from the exponent. exponent
+ is extended to 10 bits so that subtract 127 is done on
+ a 10-bit number
+ """
+ args = [0] * self.m_extra + [v[0:self.e_start]] # pad with extra zeros
+ #print ("decode", self.e_end)
+ return [self.m.eq(Cat(*args)), # mantissa
+ self.e.eq(v[self.e_start:self.e_end] - self.P127), # exp
+ self.s.eq(v[-1]), # sign
+ ]
+
def shift_down(self, inp):
""" shifts a mantissa down by one. exponent is increased to compensate
when both stb and ack are 1.
acknowledgement is sent by setting ack to ZERO.
"""
+ res = v.decode2(m)
+ ack = Signal()
with m.If((op.ack) & (op.stb)):
m.next = next_state
- m.d.sync += [
- # op is latched in from FPNumIn class on same ack/stb
- v.decode(op.v),
- op.ack.eq(0)
- ]
+ # op is latched in from FPNumIn class on same ack/stb
+ m.d.comb += ack.eq(0)
with m.Else():
- m.d.sync += op.ack.eq(1)
+ m.d.comb += ack.eq(1)
+ return [res, ack]
def denormalise(self, m, a):
""" denormalises a number. this is probably the wrong name for
m.d.sync += out_z.stb.eq(1)
+class FPState(FPBase):
+ def __init__(self, state_from):
+ self.state_from = state_from
+
+ def set_inputs(self, inputs):
+ self.inputs = inputs
+ for k,v in inputs.items():
+ setattr(self, k, v)
+
+ def set_outputs(self, outputs):
+ self.outputs = outputs
+ for k,v in outputs.items():
+ setattr(self, k, v)
+
+
+class FPID:
+ def __init__(self, id_wid):
+ self.id_wid = id_wid
+ if self.id_wid:
+ self.in_mid = Signal(id_wid, reset_less=True)
+ self.out_mid = Signal(id_wid, reset_less=True)
+ else:
+ self.in_mid = None
+ self.out_mid = None
+
+ def idsync(self, m):
+ if self.id_wid is not None:
+ m.d.sync += self.out_mid.eq(self.in_mid)
+
+