1 from nmigen
.compat
.sim
import run_simulation
2 from nmigen
.cli
import verilog
, rtlil
3 from nmigen
import Signal
, Module
, Elaboratable
11 initial begin q=1'b0; q1=1'b1; end
15 {1'b0,1'b0}:begin q=q; q1=q1; end
16 {1'b0,1'b1}: begin q=1'b0; q1=1'b1; end
17 {1'b1,1'b0}:begin q=1'b1; q1=1'b0; end
18 {1'b1,1'b1}: begin q=~q; q1=~q1; end
24 def latchregister(m
, incoming
, outgoing
, settrue
):
25 reg
= Signal
.like(incoming
) # make register same as input. reset is OK.
27 m
.d
.sync
+= reg
.eq(incoming
) # latch input into register
28 m
.d
.comb
+= outgoing
.eq(incoming
) # return input (combinatorial)
30 m
.d
.comb
+= outgoing
.eq(reg
) # return input (combinatorial)
33 class SRLatch(Elaboratable
):
34 def __init__(self
, sync
=True):
36 self
.s
= Signal(reset
=0)
37 self
.r
= Signal(reset
=1) # defaults to off
38 self
.q
= Signal(reset_less
=True)
39 self
.qn
= Signal(reset_less
=True)
40 self
.qlq
= Signal(reset_less
=True)
42 def elaborate(self
, platform
):
48 m
.d
.sync
+= q_int
.eq(1)
50 m
.d
.sync
+= q_int
.eq(0)
52 m
.d
.sync
+= q_int
.eq(q_int
)
53 m
.d
.comb
+= self
.q
.eq(q_int
)
56 m
.d
.sync
+= q_int
.eq(1)
57 m
.d
.comb
+= self
.q
.eq(1)
59 m
.d
.sync
+= q_int
.eq(0)
60 m
.d
.comb
+= self
.q
.eq(0)
62 m
.d
.sync
+= q_int
.eq(q_int
)
63 m
.d
.comb
+= self
.q
.eq(q_int
)
64 m
.d
.comb
+= self
.qn
.eq(~self
.q
)
65 m
.d
.comb
+= self
.qlq
.eq(self
.q | q_int
) # useful output
70 return self
.s
, self
.r
, self
.q
, self
.qn
98 vl
= rtlil
.convert(dut
, ports
=dut
.ports())
99 with
open("test_srlatch.il", "w") as f
:
102 run_simulation(dut
, sr_sim(dut
), vcd_name
='test_srlatch.vcd')
104 dut
= SRLatch(sync
=False)
105 vl
= rtlil
.convert(dut
, ports
=dut
.ports())
106 with
open("test_srlatch_async.il", "w") as f
:
109 run_simulation(dut
, sr_sim(dut
), vcd_name
='test_srlatch_async.vcd')
111 if __name__
== '__main__':