1 from nmigen
import Module
, Signal
2 from nmigen
.back
.pysim
import Simulator
, Passive
3 from nmigen
.test
.utils
import FHDLTestCase
4 from nmigen
.cli
import rtlil
5 from sfpy
import Float32
7 from ieee754
.cordic
.fp_pipeline
import FPCordicBasePipe
8 from ieee754
.cordic
.fp_pipe_data
import FPCordicPipeSpec
14 class SinCosTestCase(FHDLTestCase
):
15 def run_test(self
, inputs
, outputs
=iter([])):
17 pspec
= FPCordicPipeSpec(width
=32, rounds_per_stage
=4, num_rows
=1)
18 m
.submodules
.dut
= dut
= FPCordicBasePipe(pspec
)
20 # write out module (useful for seeing what's going on)
21 vl
= rtlil
.convert(dut
, ports
=dut
.ports())
22 with
open("test_cordic_pipe_sin_cos.il", "w") as f
:
25 z
= Signal(dut
.p
.data_i
.a
.shape())
31 dut
.p
.valid_i
.eq(z_valid
),
32 dut
.n
.ready_i
.eq(ready
),
49 vld
= yield dut
.n
.valid_o
52 sin
, cos
= outputs
.__next
__()
53 result
= yield dut
.n
.data_o
.x
54 result
= Float32(result
)
55 msg
= f
"cos: expected {cos} got {result}"
56 self
.assertLess(abs(result
- Float32(cos
)),
57 Float32(2e-7), msg
=msg
)
58 result
= yield dut
.n
.data_o
.y
59 result
= Float32(result
)
60 msg
= f
"sin: expected {sin} got {result}"
61 self
.assertLess(abs(result
- Float32(sin
)),
62 Float32(2e-7), msg
=msg
)
66 sim
.add_sync_process(writer_process
)
67 sim
.add_sync_process(reader_process
)
68 with sim
.write_vcd("fp_pipeline.vcd", "fp_pipeline.gtkw", traces
=[
74 for i
in range(20000):
75 x
= random
.uniform(-1, 1)
76 inputs
.append(Float32(x
))
77 sines
= [math
.sin(x
* Float32(math
.pi
/2)) for x
in inputs
]
78 cosines
= [math
.cos(x
* Float32(math
.pi
/2)) for x
in inputs
]
79 outputs
= zip(sines
, cosines
)
80 self
.run_test(iter(inputs
), outputs
=iter(outputs
))
83 inputs
= [Float32(0.5), Float32(1/3), Float32(2/3),
84 Float32(-.5), Float32(0.001)]
85 sines
= [math
.sin(x
* Float32(math
.pi
/2)) for x
in inputs
]
86 cosines
= [math
.cos(x
* Float32(math
.pi
/2)) for x
in inputs
]
87 outputs
= zip(sines
, cosines
)
88 self
.run_test(iter(inputs
), outputs
=iter(outputs
))
91 if __name__
== "__main__":