summaryrefslogtreecommitdiff
path: root/digital/asserv/src/hdlcounter/test_noise_filter.v
blob: 057378451e7973093b5ef9ddfcec84991eb423a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// test_noise_filter.v
// hdlcounter - Incremental encoder counter on programmable logic. {{{
//
// Copyright (C) 2007 Nicolas Schodet
//
// Robot APB Team 2008.
//        Web: http://apbteam.org/
//      Email: team AT apbteam DOT org
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// }}}
`timescale 1ns / 1ps

module test_noise_filter();
    parameter debug = 0;
    reg clk;
    reg rst;
    reg q;
    wire ql2, ql3, ql5;
    reg zql2, zql3, zql5;

    `include "common.v"

    // Clock generator.
    always #5 clk <= !clk;

    // Instantiation.
    noise_filter #(2) uut_size2 (clk, rst, q, ql2);
    noise_filter uut_size3 (clk, rst, q, ql3);
    noise_filter #(5) uut_size5 (clk, rst, q, ql5);

    integer i;
    time r, to;

    initial begin
	$dumpfile ("test_noise_filter.vcd");
	$dumpvars;
	clk <= 1;
	rst <= 0;
	q <= 0;
	zql2 <= 0; zql3 <= 0; zql5 <= 0;
	#2 rst <= 1;
	#6 // 2 ns before clock edge.
	// Toggle q, then draw a random delay.  Check that filtered output
	// switch at the right time.
	q <= ~q;
	repeat (1000) begin
	    #1 r = $random;
	    r = r % 8 + 1;
	    to = (r * 10) - 1;
	    if (debug)
		$display ("%t: rand %1d", $time, r);
	    fork
		// This checks the filtered output is unchanged before the
		// right clock edge, and changed after, or not changed at all
		// when the delay is too small.
		// Did not found an easy way to factorise this (tasks sample
		// input at invocation):
		begin
		    if (r > 2) begin
			#(2 * 10) assert (ql2, zql2);
			#2 assert (ql2, q);
		    end
		    else
			#(to) assert (ql2, zql2);
		    if (r >= 2)
			zql2 <= q;
		end
		begin
		    if (r > 3) begin
			#(3 * 10) assert (ql3, zql3);
			#2 assert (ql3, q);
		    end
		    else
			#(to) assert (ql3, zql3);
		    if (r >= 3)
			zql3 <= q;
		end
		begin
		    if (r > 5) begin
			#(5 * 10) assert (ql5, zql5);
			#2 assert (ql5, q);
		    end
		    else
			#(to) assert (ql5, zql5);
		    if (r >= 5)
			zql5 <= q;
		end
		begin
		    #(to) q <= ~q;
		end
	    join
	    r = 0;
	end
	$finish;
    end

endmodule