OpenCores
URL https://opencores.org/ocsvn/iso7816_3_master/iso7816_3_master/trunk

Subversion Repositories iso7816_3_master

[/] [iso7816_3_master/] [trunk/] [sources/] [Counter.v] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 acapola
`timescale 1ns / 1ps
2
//////////////////////////////////////////////////////////////////////////////////
3
// Company: 
4
// Engineer: Sebastien Riou
5
// 
6
// Create Date:    23:57:02 08/31/2010 
7
// Design Name: 
8
// Module Name:    Counter 
9
// Project Name: 
10
// Target Devices: 
11
// Tool versions: 
12
// Description: A counter with increment and clear operation
13
//
14
// Dependencies: 
15
//
16
// Revision: 
17
// Revision 0.01 - File Created
18
// Additional Comments: 
19
//
20
//////////////////////////////////////////////////////////////////////////////////
21
module Counter(
22
    output reg [WIDTH-1:0] counter,
23
    output earlyMatch,
24
         output reg match,
25
         output dividedClk,
26
         input [DIVIDER_WIDTH-1:0] divider,      // clock divide factor
27
         input [WIDTH-1:0] compare,
28
         input inc,
29
         input clear,
30
         input [WIDTH_INIT-1:0] initVal,
31
         input clk,
32
    input nReset
33
    );
34
 
35
//parameters to override
36
parameter DIVIDER_WIDTH = 16;
37
parameter WIDTH = 8;
38
parameter WIDTH_INIT = 1;
39
 
40
wire divideBy1;
41
wire divMatch;
42
wire divRisingMatch;
43
wire divFallingMatch;
44
 
45
ClkDivider #(.DIVIDER_WIDTH(DIVIDER_WIDTH))
46
        clkDivider(
47
                .nReset(nReset),
48
                .clk(clk),
49
                .divider(divider),
50
                .dividedClk(dividedClk),
51
                .divideBy1(divideBy1),
52
                .match(divMatch),
53
                .risingMatch(divRisingMatch),
54
                .fallingMatch(divFallingMatch)
55
                );
56
 
57
wire [WIDTH-1:0] nextCounter = counter+1'b1;
58
 
59
wire doInc = divideBy1 ? inc :inc & divRisingMatch;
60
wire doEarlyMatch = divideBy1 ? (compare == nextCounter) : (compare == counter) & divRisingMatch;
61
 
62
reg earlyMatchReg;
63
assign earlyMatch = divideBy1 ? earlyMatchReg : doEarlyMatch;
64
 
65
always @(posedge clk, negedge nReset) begin
66
        if(~nReset) begin
67
                counter <= 0;//initVal;
68
      earlyMatchReg <= 0;
69
                match <= 0;
70
        end else begin
71
                if(clear) begin
72
                        counter <= initVal;
73
                end else if(doInc) begin
74
                        if(compare == counter)
75
                                counter <= initVal;
76
                        else
77
                                counter <= nextCounter;
78
                end
79
                if(doEarlyMatch)
80
                        earlyMatchReg <= 1;
81
                else begin
82
                        earlyMatchReg <= 0;
83
                end
84
      match <= divideBy1 ? earlyMatchReg : doEarlyMatch;
85
        end
86
end
87
 
88
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.