1a338f56f85dbac45e0eb15bede5bdf701500f6c
1 # IEEE Floating Point Divider (Single Precision)
2 # Copyright (C) Jonathan P Dawson 2013
5 from nmigen
import Module
, Signal
, Const
, Cat
, Elaboratable
6 from nmigen
.cli
import main
, verilog
, rtlil
7 from nmigen
.compat
.sim
import run_simulation
10 from fpbase
import FPNumIn
, FPNumOut
, FPOpIn
, FPOpOut
, FPBase
, FPState
11 from nmutil
.nmoperator
import eq
12 from nmutil
.singlepipe
import SimpleHandshake
, ControlBase
13 from test_buf_pipe
import data_chain2
, Test5
16 class FPDIV(FPBase
, Elaboratable
):
18 def __init__(self
, width
):
22 self
.p
= FPOpIn(width
)
23 self
.n
= FPOpOut(width
)
25 self
.p
.data_i
= self
.ispec()
26 self
.n
.data_o
= self
.ospec()
31 return Signal(self
.width
, name
="a")
34 return Signal(self
.width
, name
="z")
36 def setup(self
, m
, i
):
37 m
.d
.comb
+= self
.p
.v
.eq(i
) # connect input
40 return self
.n
.v
# return z output
42 def add_state(self
, state
):
43 self
.states
.append(state
)
46 def elaborate(self
, platform
=None):
47 """ creates the HDL code-fragment for FPDiv
52 a
= FPNumIn(None, self
.width
, False)
53 z
= FPNumOut(self
.width
, False)
55 m
.submodules
.p
= self
.p
56 m
.submodules
.n
= self
.n
60 m
.d
.comb
+= a
.v
.eq(self
.p
.v
)
67 with m
.State("get_a"):
68 res
= self
.get_op(m
, self
.p
, a
, "add_1")
69 m
.d
.sync
+= eq([a
, self
.p
.ready_o
], res
)
71 with m
.State("add_1"):
75 z
.e
.eq(a
.e
), # exponent
76 z
.m
.eq(a
.m
+ 1), # mantissa
83 self
.pack(m
, z
, "put_z")
88 with m
.State("put_z"):
89 self
.put_z(m
, z
, self
.n
, "get_a")
93 class FPDIVPipe(ControlBase
):
95 def __init__(self
, width
):
97 self
.fpdiv
= FPDIV(width
=width
)
98 ControlBase
.__init
__(self
, self
.fpdiv
)
100 def elaborate(self
, platform
):
101 self
.m
= m
= ControlBase
.elaborate(self
, platform
)
103 m
.submodules
.fpdiv
= self
.fpdiv
105 # see if connecting to stb/ack works
106 m
.d
.comb
+= self
.fpdiv
.p
._connect
_in
(self
.p
)
107 m
.d
.comb
+= self
.fpdiv
.n
._connect
_out
(self
.n
, do_data
=False)
108 m
.d
.comb
+= self
.n
.data_o
.eq(self
.data_r
)
112 def resultfn(data_o
, expected
, i
, o
):
114 assert data_o
== res
, \
115 "%d-%d received data %x not match expected %x\n" \
116 % (i
, o
, data_o
, res
)
119 if __name__
== "__main__":
120 dut
= FPDIVPipe(width
=16)
123 vl
= rtlil
.convert(dut
, ports
=ports
)
124 with
open("test_fsm_experiment.il", "w") as f
:
126 test
= Test5(dut
, resultfn
, data
=data
)
127 run_simulation(dut
, [test
.send
, test
.rcv
],
128 vcd_name
="test_fsm_experiment.vcd")