Skip to content

Commit 1efbd7c

Browse files
committed
Added soft_latch module and testbench
1 parent d262582 commit 1efbd7c

File tree

2 files changed

+236
-0
lines changed

2 files changed

+236
-0
lines changed

soft_latch.sv

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//------------------------------------------------------------------------------
2+
// soft_latch.sv
3+
// Konstantin Pavlov, [email protected]
4+
//------------------------------------------------------------------------------
5+
6+
// INFO ------------------------------------------------------------------------
7+
// "Software" latch, aka combinational data hold circuit
8+
//
9+
// Features combinational data latching and combinational resetting
10+
// Zero latency for setting and resetting data
11+
// No hardware latches inferred by means of this circuit
12+
//
13+
14+
// | | +---+ | | | | | | latch, this module input
15+
// | | | | | | | | | |
16+
// +------------+ +--------------------------------+
17+
// | | | | | | | | | |
18+
// +----------------------------+ +----------------+
19+
// | | | | | | | | | |
20+
// | | | | | | +---+ | | nrst, this module input
21+
// | | | | | | | | | |
22+
// +-------------------------------------------------+
23+
// }{A }{B }{C }{D }{E }{F }{G }{H }{J }{ in, this module data input
24+
// +-------------------------------------------------+
25+
// | | | | | | | | | |
26+
// | | | +---------------+ | | standard unblocking assignment
27+
// | | | { C | C | C | C } | |
28+
// +----------------+ | | | +----------------+
29+
// | | | | | | | | | |
30+
// | | +---------------+ | | | out, this module data output
31+
// | | { C | C | C | C } | | |
32+
// +------------+ | | | +--------------------+
33+
// | | | | | | | | | |
34+
// | | | | | | | | | |
35+
36+
37+
/* --- INSTANTIATION TEMPLATE BEGIN ---
38+
39+
soft_latch #(
40+
.WIDTH( 16 )
41+
) SL1 (
42+
.clk( clk ),
43+
.nrst( 1'b1 ),
44+
.latch( ),
45+
.in( ),
46+
.out( )
47+
);
48+
49+
--- INSTANTIATION TEMPLATE END ---*/
50+
51+
52+
module soft_latch #( parameter
53+
WIDTH = 1 // data width
54+
)(
55+
input clk, // clock
56+
input nrst, // inverted reset
57+
58+
input latch, // latch strobe
59+
input [WIDTH-1:0] in, // data in
60+
output logic [WIDTH-1:0] out // data out
61+
);
62+
63+
logic [WIDTH-1:0] in_buf = '0;
64+
65+
// buffering input data
66+
always_ff @(posedge clk) begin
67+
if( ~nrst ) begin
68+
in_buf[WIDTH-1:0] <= '0;
69+
end else if( latch ) begin
70+
in_buf[WIDTH-1:0] <= in[WIDTH-1:0];
71+
end
72+
end
73+
74+
// mixing combinational and buffered data to the output
75+
always_comb begin
76+
if( ~nrst ) begin
77+
out[WIDTH-1:0] <= '0;
78+
end else if( latch ) begin
79+
out[WIDTH-1:0] <= in[WIDTH-1:0];
80+
end else begin
81+
out[WIDTH-1:0] <= in_buf[WIDTH-1:0];
82+
end
83+
84+
85+
end
86+
87+
endmodule
88+

soft_latch_tb.sv

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
//------------------------------------------------------------------------------
2+
// soft_latch_tb.sv
3+
// Konstantin Pavlov, [email protected]
4+
//------------------------------------------------------------------------------
5+
6+
// INFO ------------------------------------------------------------------------
7+
// testbench for soft_latch.sv module
8+
//
9+
10+
`timescale 1ns / 1ps
11+
12+
module soft_latch_tb();
13+
14+
logic clk200;
15+
initial begin
16+
#0 clk200 = 1'b0;
17+
forever
18+
#2.5 clk200 = ~clk200;
19+
end
20+
21+
// external device "asynchronous" clock
22+
logic clk33;
23+
initial begin
24+
#0 clk33 = 1'b0;
25+
forever
26+
#15.151 clk33 = ~clk33;
27+
end
28+
29+
logic rst;
30+
initial begin
31+
#0 rst = 1'b0;
32+
#10.2 rst = 1'b1;
33+
#5 rst = 1'b0;
34+
//#10000;
35+
forever begin
36+
#9985 rst = ~rst;
37+
#5 rst = ~rst;
38+
end
39+
end
40+
41+
logic nrst;
42+
assign nrst = ~rst;
43+
44+
logic rst_once;
45+
initial begin
46+
#0 rst_once = 1'b0;
47+
#10.2 rst_once = 1'b1;
48+
#5 rst_once = 1'b0;
49+
end
50+
51+
logic nrst_once;
52+
assign nrst_once = ~rst_once;
53+
54+
logic [31:0] DerivedClocks;
55+
clk_divider #(
56+
.WIDTH( 32 )
57+
) cd1 (
58+
.clk( clk200 ),
59+
.nrst( nrst_once ),
60+
.ena( 1'b1 ),
61+
.out( DerivedClocks[31:0] )
62+
);
63+
64+
logic [31:0] E_DerivedClocks;
65+
edge_detect ed1[31:0] (
66+
.clk( {32{clk200}} ),
67+
.nrst( {32{nrst_once}} ),
68+
.in( DerivedClocks[31:0] ),
69+
.rising( E_DerivedClocks[31:0] ),
70+
.falling( ),
71+
.both( )
72+
);
73+
74+
logic [15:0] RandomNumber1;
75+
c_rand rng1 (
76+
.clk(clk200),
77+
.rst(rst_once),
78+
.reseed(1'b0),
79+
.seed_val(DerivedClocks[31:0]),
80+
.out( RandomNumber1[15:0] )
81+
);
82+
83+
logic start;
84+
initial begin
85+
#0 start = 1'b0;
86+
#100 start = 1'b1;
87+
#20 start = 1'b0;
88+
end
89+
90+
// Module under test ==========================================================
91+
92+
logic set;
93+
assign set = &RandomNumber1[14:12];
94+
95+
logic ret;
96+
assign ret = &RandomNumber1[11:9];
97+
98+
// verilog hardvare latch
99+
logic [15:0] data1;
100+
always_latch begin
101+
if( ret ) begin
102+
data1[15:0] <= '0;
103+
end else if( set ) begin
104+
data1[15:0] <= RandomNumber1[15:0];
105+
end
106+
end
107+
108+
// soft_latch prototype
109+
logic [15:0] data2;
110+
set_reset_comb SR [15:0] (
111+
.clk( {16{clk200}} ),
112+
.nrst( {16{1'b1}} ),
113+
.s( {16{set}} & RandomNumber1[15:0] ), //set
114+
.r( ({16{set}} & ~RandomNumber1[15:0]) | {16{ret}} ), //rst
115+
.q( data2[15:0] ),
116+
.nq( )
117+
);
118+
119+
// genuine soft_latch instance
120+
logic [15:0] data3;
121+
soft_latch #(
122+
.WIDTH( 16 )
123+
) SL1 (
124+
.clk( clk200 ),
125+
.nrst( ~ret ),
126+
.latch( set ),
127+
.in( RandomNumber1[15:0] ),
128+
.out( data3[15:0] )
129+
);
130+
131+
//==============================================================================
132+
133+
logic outputs_equal;
134+
assign outputs_equal = ( data1[15:0] == data2[15:0] ) &&
135+
( data1[15:0] == data3[15:0] );
136+
137+
logic success = 1'b1;
138+
always_ff @(posedge clk200) begin
139+
if( ~nrst ) begin
140+
success <= 1'b1;
141+
end else begin
142+
if( ~outputs_equal ) begin
143+
success <= 1'b0;
144+
end
145+
end
146+
end
147+
148+
endmodule

0 commit comments

Comments
 (0)