1 |
11 |
acapola |
/*
|
2 |
|
|
Author: Sebastien Riou (acapola)
|
3 |
|
|
Creation date: 23:57:02 08/31/2010
|
4 |
|
|
|
5 |
|
|
$LastChangedDate: 2011-01-29 13:16:17 +0100 (Sat, 29 Jan 2011) $
|
6 |
|
|
$LastChangedBy: acapola $
|
7 |
|
|
$LastChangedRevision: 11 $
|
8 |
|
|
$HeadURL: file:///svn/iso7816_3_master/iso7816_3_master/trunk/sources/Counter.v $
|
9 |
|
|
|
10 |
|
|
This file is under the BSD licence:
|
11 |
|
|
Copyright (c) 2011, Sebastien Riou
|
12 |
|
|
|
13 |
|
|
All rights reserved.
|
14 |
|
|
|
15 |
|
|
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
16 |
|
|
|
17 |
|
|
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
18 |
|
|
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
19 |
|
|
The names of contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
|
20 |
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
21 |
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
22 |
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
23 |
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
24 |
|
|
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
25 |
|
|
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
26 |
|
|
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
27 |
|
|
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
28 |
|
|
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
29 |
|
|
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
30 |
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
31 |
|
|
*/
|
32 |
|
|
`default_nettype none
|
33 |
|
|
/*
|
34 |
|
|
A counter with increment and clear operation
|
35 |
|
|
*/
|
36 |
|
|
module Counter
|
37 |
|
|
#(//parameters to override
|
38 |
|
|
parameter DIVIDER_WIDTH = 16,
|
39 |
|
|
parameter WIDTH = 8,
|
40 |
|
|
parameter WIDTH_INIT = 1
|
41 |
|
|
)
|
42 |
|
|
(
|
43 |
2 |
acapola |
output reg [WIDTH-1:0] counter,
|
44 |
4 |
acapola |
output wire earlyMatch,
|
45 |
2 |
acapola |
output reg match,
|
46 |
4 |
acapola |
output wire dividedClk,
|
47 |
|
|
input wire [DIVIDER_WIDTH-1:0] divider, // clock divide factor
|
48 |
|
|
input wire [WIDTH-1:0] compare,
|
49 |
|
|
input wire inc,
|
50 |
|
|
input wire clear,
|
51 |
|
|
input wire [WIDTH_INIT-1:0] initVal,
|
52 |
|
|
input wire clk,
|
53 |
|
|
input wire nReset
|
54 |
2 |
acapola |
);
|
55 |
|
|
|
56 |
|
|
wire divideBy1;
|
57 |
|
|
wire divMatch;
|
58 |
|
|
wire divRisingMatch;
|
59 |
|
|
wire divFallingMatch;
|
60 |
|
|
|
61 |
|
|
ClkDivider #(.DIVIDER_WIDTH(DIVIDER_WIDTH))
|
62 |
|
|
clkDivider(
|
63 |
|
|
.nReset(nReset),
|
64 |
|
|
.clk(clk),
|
65 |
|
|
.divider(divider),
|
66 |
|
|
.dividedClk(dividedClk),
|
67 |
|
|
.divideBy1(divideBy1),
|
68 |
|
|
.match(divMatch),
|
69 |
|
|
.risingMatch(divRisingMatch),
|
70 |
|
|
.fallingMatch(divFallingMatch)
|
71 |
|
|
);
|
72 |
|
|
|
73 |
|
|
wire [WIDTH-1:0] nextCounter = counter+1'b1;
|
74 |
|
|
|
75 |
|
|
wire doInc = divideBy1 ? inc :inc & divRisingMatch;
|
76 |
|
|
wire doEarlyMatch = divideBy1 ? (compare == nextCounter) : (compare == counter) & divRisingMatch;
|
77 |
|
|
|
78 |
|
|
reg earlyMatchReg;
|
79 |
|
|
assign earlyMatch = divideBy1 ? earlyMatchReg : doEarlyMatch;
|
80 |
|
|
|
81 |
|
|
always @(posedge clk, negedge nReset) begin
|
82 |
|
|
if(~nReset) begin
|
83 |
|
|
counter <= 0;//initVal;
|
84 |
|
|
earlyMatchReg <= 0;
|
85 |
|
|
match <= 0;
|
86 |
|
|
end else begin
|
87 |
|
|
if(clear) begin
|
88 |
|
|
counter <= initVal;
|
89 |
|
|
end else if(doInc) begin
|
90 |
|
|
if(compare == counter)
|
91 |
|
|
counter <= initVal;
|
92 |
|
|
else
|
93 |
|
|
counter <= nextCounter;
|
94 |
|
|
end
|
95 |
|
|
if(doEarlyMatch)
|
96 |
|
|
earlyMatchReg <= 1;
|
97 |
|
|
else begin
|
98 |
|
|
earlyMatchReg <= 0;
|
99 |
|
|
end
|
100 |
|
|
match <= divideBy1 ? earlyMatchReg : doEarlyMatch;
|
101 |
|
|
end
|
102 |
|
|
end
|
103 |
|
|
|
104 |
|
|
endmodule
|
105 |
11 |
acapola |
`default_nettype wire
|