create FPDecode module
[ieee754fpu.git] / src / add / fpbase.py
index 30c3a5c47e288c8224f3dc0acd8da7fd8a765fd1..dc2c9020dd5fc3644da3a9f7f36e2532ea896619 100644 (file)
@@ -298,7 +298,8 @@ class FPNumShift(FPNumBase):
                 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
@@ -312,15 +313,12 @@ class FPNumIn(FPNumBase):
     """
     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
 
@@ -338,6 +336,37 @@ class FPNumIn(FPNumBase):
                 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 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