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 8

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

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

powered by: WebSVN 2.1.0

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