6cf5ab7ec93e5de0752cdfd2cf00f56c4edf7d8c
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_less
=True)
37 self
.r
= Signal(reset_less
=True)
38 self
.q
= Signal(reset_less
=True)
39 self
.qn
= Signal(reset_less
=True)
41 def elaborate(self
, platform
):
43 q_int
= Signal(reset_less
=True)
47 m
.d
.sync
+= q_int
.eq(1)
49 m
.d
.sync
+= q_int
.eq(0)
51 m
.d
.sync
+= q_int
.eq(q_int
)
52 m
.d
.comb
+= self
.q
.eq(q_int
)
55 m
.d
.sync
+= q_int
.eq(1)
56 m
.d
.comb
+= self
.q
.eq(1)
58 m
.d
.sync
+= q_int
.eq(0)
59 m
.d
.comb
+= self
.q
.eq(0)
61 m
.d
.sync
+= q_int
.eq(q_int
)
62 m
.d
.comb
+= self
.q
.eq(q_int
)
63 m
.d
.comb
+= self
.qn
.eq(~self
.q
)
68 return self
.s
, self
.r
, self
.q
, self
.qn
96 vl
= rtlil
.convert(dut
, ports
=dut
.ports())
97 with
open("test_srlatch.il", "w") as f
:
100 run_simulation(dut
, sr_sim(dut
), vcd_name
='test_srlatch.vcd')
102 dut
= SRLatch(sync
=False)
103 vl
= rtlil
.convert(dut
, ports
=dut
.ports())
104 with
open("test_srlatch_async.il", "w") as f
:
107 run_simulation(dut
, sr_sim(dut
), vcd_name
='test_srlatch_async.vcd')
109 if __name__
== '__main__':