summaryrefslogtreecommitdiff
path: root/digital/asserv/src/hdlcounter/test_updown_counter.v
blob: ebe5d115c6568ac071d8daebb1b76ebe3ba4f41d (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
// test_updown_counter.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_updown_counter();
    parameter debug = 0;
    reg clk;
    reg rst;
    reg [1:0] updown;
    wire [7:0] count8;
    wire [11:0] count12;
    
    `include "common.v"

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

    // Instantiation.
    updown_counter uut_size8 (clk, rst, updown, count8);
    updown_counter #(12) uut_size12 (clk, rst, updown, count12);

    integer n, count, diff;

    initial begin
	$dumpfile ("test_updown_counter.vcd");
	$dumpvars;
	clk <= 1;
	rst <= 0;
	updown <= 0;
	count = 0;
	n = 0;
	#2 rst <= 1;
	repeat (1000) begin
	    @(negedge clk)
	    if (n == 0) begin
		n = $random;
		n = n > 0 ? n : -n;
		n = n % 16 + 1;
		diff = $random % 2;
		if (debug)
		    $display ("%t: n %1d diff %1d", $time, n, diff);
	    end
	    n = n - 1;
	    count = count + diff;
	    if (diff > 0)
		updown <= 2'b10;
	    else if (diff < 0)
		updown <= 2'b01;
	    else
		updown <= 2'b00;
	    @(posedge clk) #1
	    assertv8 (count[7:0], count8);
	    assertv12 (count[11:0], count12);
	end
	if (debug)
	    $display ("%t: count %1d", $time, count);
	$finish;
    end

endmodule