File tree Expand file tree Collapse file tree 6 files changed +167
-29
lines changed Expand file tree Collapse file tree 6 files changed +167
-29
lines changed File renamed without changes.
Original file line number Diff line number Diff line change
1
+ // ------------------------------------------------------------------------------
2
+ // EdgeDetect.sv
3
+ // Konstantin Pavlov, [email protected]
4
+ // ------------------------------------------------------------------------------
5
+
6
+ // INFO ------------------------------------------------------------------------
7
+ // Variable width edge detector
8
+ // Features one tick propagation time
9
+
10
+
11
+ /* --- INSTANTIATION TEMPLATE BEGIN ---
12
+
13
+ EdgeDetect #(
14
+ .WIDTH( 32 )
15
+ ) ED1 (
16
+ .clk( clk ),
17
+ .nrst( 1'b1 ),
18
+ .in( ),
19
+ .rising( ),
20
+ .falling( ),
21
+ .both( )
22
+ );
23
+
24
+ --- INSTANTIATION TEMPLATE END ---*/
25
+
26
+
27
+ module EdgeDetect # (
28
+ WIDTH = 1
29
+ )(
30
+ input clk,
31
+ input nrst,
32
+
33
+ input [(WIDTH - 1 ): 0 ] in,
34
+ output logic [(WIDTH - 1 ): 0 ] rising = 0 ,
35
+ output logic [(WIDTH - 1 ): 0 ] falling = 0 ,
36
+ output [(WIDTH - 1 ): 0 ] both
37
+ );
38
+
39
+
40
+ logic [(WIDTH - 1 ): 0 ] in_prev = 0 ;
41
+
42
+ always_ff @ (posedge clk) begin
43
+ if ( ~ nrst ) begin
44
+ in_prev <= 0 ;
45
+ rising <= 0 ;
46
+ falling <= 0 ;
47
+ end
48
+ else begin
49
+ in_prev <= in;
50
+ rising[(WIDTH - 1 ): 0 ] <= in[(WIDTH - 1 ): 0 ] & ~ in_prev[(WIDTH - 1 ): 0 ];
51
+ falling[(WIDTH - 1 ): 0 ] <= ~ in[(WIDTH - 1 ): 0 ] & in_prev[(WIDTH - 1 ): 0 ];
52
+ end
53
+ end
54
+
55
+ assign both[(WIDTH - 1 ): 0 ] = rising[(WIDTH - 1 ): 0 ] | falling[(WIDTH - 1 ): 0 ];
56
+
57
+ endmodule
File renamed without changes.
Original file line number Diff line number Diff line change 4
4
// ------------------------------------------------------------------------------
5
5
6
6
// INFO ------------------------------------------------------------------------
7
- // Variable width edge detector
8
- // Features one tick propagation time
7
+ // Edge detector, ver.2
8
+ // Combinational implementation (zero ticks delay)
9
+ //
10
+ // In case when "in" port has toggle rate 100% (changes every clock period)
11
+ // "rising" and "falling" outputs will completely replicate input
12
+ // "both" output will be always active in this case
9
13
10
14
11
15
/* --- INSTANTIATION TEMPLATE BEGIN ---
12
16
13
- EdgeDetect #(
14
- .WIDTH( 32 )
15
- ) ED1 (
16
- .clk( clk ),
17
- .nrst( 1'b1 ),
18
- .in( ),
19
- .rising( ),
17
+ EdgeDetect ED1[31:0] (
18
+ .clk( {32{clk}} ),
19
+ .nrst( {32{1'b1}} ),
20
+ .in( in[31:0] ),
21
+ .rising( out[31:0] ),
20
22
.falling( ),
21
23
.both( )
22
24
);
23
25
24
26
--- INSTANTIATION TEMPLATE END ---*/
25
27
26
28
27
- module EdgeDetect # (
28
- WIDTH = 1
29
- )(
29
+ module EdgeDetect (
30
30
input clk,
31
31
input nrst,
32
32
33
- input [( WIDTH - 1 ) : 0 ] in,
34
- output logic [( WIDTH - 1 ) : 0 ] rising = 0 ,
35
- output logic [( WIDTH - 1 ) : 0 ] falling = 0 ,
36
- output [( WIDTH - 1 ) : 0 ] both
33
+ input in,
34
+ output logic rising,
35
+ output logic falling,
36
+ output logic both
37
37
);
38
38
39
-
40
- logic [(WIDTH - 1 ): 0 ] in_prev = 0 ;
41
-
39
+ logic in_d = 0 ;
42
40
always_ff @ (posedge clk) begin
43
41
if ( ~ nrst ) begin
44
- in_prev <= 0 ;
45
- rising <= 0 ;
46
- falling <= 0 ;
47
- end
48
- else begin
49
- in_prev <= in;
50
- rising[(WIDTH - 1 ): 0 ] <= in[(WIDTH - 1 ): 0 ] & ~ in_prev[(WIDTH - 1 ): 0 ];
51
- falling[(WIDTH - 1 ): 0 ] <= ~ in[(WIDTH - 1 ): 0 ] & in_prev[(WIDTH - 1 ): 0 ];
42
+ in_d <= 0 ;
43
+ end else begin
44
+ in_d <= in;
52
45
end
53
46
end
54
47
55
- assign both[(WIDTH - 1 ): 0 ] = rising[(WIDTH - 1 ): 0 ] | falling[(WIDTH - 1 ): 0 ];
48
+ always_comb begin
49
+ rising = nrst && (in && ~ in_d);
50
+ falling = nrst && (~ in && in_d);
51
+ both = nrst && (rising || falling);
52
+ end
56
53
57
54
endmodule
Original file line number Diff line number Diff line change
1
+ // ------------------------------------------------------------------------------
2
+ // EdgeDetect_tb.sv
3
+ // Konstantin Pavlov, [email protected]
4
+ // ------------------------------------------------------------------------------
5
+
6
+ // INFO ------------------------------------------------------------------------
7
+ //
8
+
9
+ `timescale 1ns / 1ps
10
+
11
+ module EdgeDetect_tb ();
12
+
13
+ logic clk200;
14
+ initial begin
15
+ # 0 clk200 = 1 ;
16
+ forever
17
+ # 2 .5 clk200 = ~ clk200;
18
+ end
19
+
20
+ logic rst;
21
+ initial begin
22
+ # 10 .2 rst = 1 ;
23
+ # 5 rst = 0 ;
24
+ // #10000;
25
+ forever begin
26
+ # 9985 rst = ~ rst;
27
+ # 5 rst = ~ rst;
28
+ end
29
+ end
30
+
31
+ logic nrst;
32
+ assign nrst = ~ rst;
33
+
34
+ logic rst_once;
35
+ initial begin // initializing non-X data before PLL starts
36
+ # 10 .2 rst_once = 1 ;
37
+ # 5 rst_once = 0 ;
38
+ end
39
+ initial begin
40
+ # 510 .2 rst_once = 1 ; // PLL starts at 500ns, clock appears, so doing the reset for modules
41
+ # 5 rst_once = 0 ;
42
+ end
43
+
44
+ logic nrst_once;
45
+ assign nrst_once = ~ rst_once;
46
+
47
+ logic [31 : 0 ] DerivedClocks;
48
+ ClkDivider # (
49
+ .WIDTH ( 32 )
50
+ ) CD1 (
51
+ .clk ( clk200 ),
52
+ .nrst ( nrst_once ),
53
+ .out ( DerivedClocks[31 : 0 ] )
54
+ );
55
+
56
+ logic [15 : 0 ] RandomNumber1;
57
+ c_rand RNG1 (
58
+ .clk ( clk200 ),
59
+ .rst ( rst_once ),
60
+ .reseed ( 1'b0 ),
61
+ .seed_val ( DerivedClocks[31 : 0 ] ),
62
+ .out ( RandomNumber1[15 : 0 ] )
63
+ );
64
+
65
+ logic start;
66
+ initial begin
67
+ # 0 start = 1'b0 ;
68
+ # 100 .2 start = 1'b1 ;
69
+ # 5 start = 1'b0 ;
70
+ end
71
+
72
+ // Module under test ==========================================================
73
+
74
+ EdgeDetect ED1 [15 :0 ] (
75
+ .clk ( { 16 { clk200}} ),
76
+ .nrst ( { 16 { nrst_once}} ),
77
+ .in ( RandomNumber1[15 : 0 ] ),
78
+ .rising ( ),
79
+ .falling ( ),
80
+ .both ( )
81
+ );
82
+
83
+
84
+ endmodule
Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ reg clk200;
15
15
initial begin
16
16
#0 clk200 = 1 ;
17
17
forever
18
-
18
+ #2 . 5 clk200 = ~ clk200;
19
19
end
20
20
21
21
reg rst;
You can’t perform that action at this time.
0 commit comments