From 1e8f321ad389c4b45f8a7dc64a72d28a76a0b047 Mon Sep 17 00:00:00 2001 From: Michael Nolan Date: Thu, 16 Apr 2020 14:57:56 -0400 Subject: [PATCH] Working (ish) fpsin iterative cordic --- src/ieee754/cordic/fpsin_cos.py | 3 +-- src/ieee754/cordic/test/test_fpsin_cos.py | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/ieee754/cordic/fpsin_cos.py b/src/ieee754/cordic/fpsin_cos.py index fa7ed8c1..382082be 100644 --- a/src/ieee754/cordic/fpsin_cos.py +++ b/src/ieee754/cordic/fpsin_cos.py @@ -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) diff --git a/src/ieee754/cordic/test/test_fpsin_cos.py b/src/ieee754/cordic/test/test_fpsin_cos.py index 1f050960..091b7ecc 100644 --- a/src/ieee754/cordic/test/test_fpsin_cos.py +++ b/src/ieee754/cordic/test/test_fpsin_cos.py @@ -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): -- 2.30.2