Working (ish) fpsin iterative cordic
authorMichael Nolan <mtnolan2640@gmail.com>
Thu, 16 Apr 2020 18:57:56 +0000 (14:57 -0400)
committerMichael Nolan <mtnolan2640@gmail.com>
Thu, 16 Apr 2020 18:57:56 +0000 (14:57 -0400)
src/ieee754/cordic/fpsin_cos.py
src/ieee754/cordic/test/test_fpsin_cos.py

index fa7ed8c1c16217fdbc187d1604d420d6aa0adde2..382082be0d5831b0a6753756d0c2c2b51443df97 100644 (file)
@@ -25,7 +25,7 @@ class CordicROM(Elaboratable):
         self.addr = Signal(range(iterations))
         self.data = Signal(range(-M, M-1))
 
-        angles = [int(round(M*math.atan(2**(-i))))
+        angles = [int(round(M*math.atan(2**(-i))/(math.pi/2)))
                   for i in range(self.iterations)]
 
         self.mem = Memory(width=self.data.width,
@@ -121,7 +121,6 @@ class CORDIC(Elaboratable):
             sync += i.eq(0)
             sync += state.eq(CordicState.RUNNING)
             sync += anglerom.addr.eq(1)
-            sync += self.ready.eq(1)  # debug
         with m.If(state == CordicState.RUNNING):
             with m.If(z >= 0):
                 sync += x.eq(x - dx)
index 1f050960c9bd094a688ed38f98667cd8f3fb1be1..091b7ecc259cbf48e8128cd0da76cacb079c599d 100644 (file)
@@ -50,6 +50,13 @@ class SinCosTestCase(FHDLTestCase):
                     print(f"{zo:x} {frac}")
                     self.assertEqual(str(frac), zin.__str__())
                     asserted = True
+
+                    real_sin = yield dut.sin
+                    real_sin = self.get_frac(real_sin, dut.sin.width - 2)
+                    diff = abs(real_sin - expected_sin)
+                    print(f"{real_sin} {expected_sin} {diff}")
+                    self.assertTrue(diff < 0.001)
+                    
                 yield
 
         sim.add_sync_process(process)
@@ -58,15 +65,21 @@ class SinCosTestCase(FHDLTestCase):
             sim.run()
 
     def run_test_assert(self, z, fracbits=8):
-        self.run_test(zin=z, fracbits=fracbits)
+        zpi = z * Float16(math.pi/2)
+        e_sin = math.sin(zpi)
+        e_cos = math.cos(zpi)
+        self.run_test(zin=z, fracbits=fracbits, expected_sin=e_sin,
+                      expected_cos=e_cos)
 
     def test_1(self):
-        x = Float16(.31212)
+        x = Float16(1.0)
         print(x)
         self.run_test_assert(x)
 
-    # def test_neg(self):
-    #     self.run_test_assert(-6)
+    def test_pi_4(self):
+        x = Float16(1/3)
+        print(x)
+        self.run_test_assert(x)
 
     def test_rand(self):
         for i in range(500):