1 |
11 |
acapola |
/*
|
2 |
|
|
Author: Sebastien Riou (acapola)
|
3 |
|
|
Creation date: 18:05:27 01/09/2011
|
4 |
2 |
acapola |
|
5 |
11 |
acapola |
$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/ClkDivider.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 |
2 |
acapola |
/*
|
34 |
|
|
Basic clock divider
|
35 |
|
|
|
36 |
|
|
if divider=0
|
37 |
|
|
dividedClk=clk
|
38 |
|
|
else
|
39 |
|
|
F(dividedClk)=F(clk)/(divider*2)
|
40 |
|
|
dividedClk has a duty cycle of 50%
|
41 |
|
|
|
42 |
|
|
WARNING:
|
43 |
|
|
To change divider on the fly:
|
44 |
|
|
1. set it to 0 at least for one cycle
|
45 |
|
|
2. set it to the new value.
|
46 |
|
|
*/
|
47 |
11 |
acapola |
module ClkDivider
|
48 |
|
|
#(//parameters to override
|
49 |
|
|
parameter DIVIDER_WIDTH = 16
|
50 |
|
|
)
|
51 |
|
|
(
|
52 |
|
|
input wire nReset,
|
53 |
|
|
input wire clk, // input clock
|
54 |
|
|
input wire [DIVIDER_WIDTH-1:0] divider, // divide factor
|
55 |
|
|
output wire dividedClk, // divided clock
|
56 |
|
|
output wire divideBy1,
|
57 |
|
|
output wire match,
|
58 |
|
|
output wire risingMatch,
|
59 |
|
|
output wire fallingMatch
|
60 |
2 |
acapola |
);
|
61 |
11 |
acapola |
|
62 |
2 |
acapola |
|
63 |
|
|
reg out;//internal divided clock
|
64 |
|
|
reg [DIVIDER_WIDTH-1:0] cnt;
|
65 |
|
|
|
66 |
|
|
// if divider=0, dividedClk = clk.
|
67 |
|
|
assign divideBy1 = |divider ? 1'b0 : 1'b1;
|
68 |
|
|
assign dividedClk = divideBy1 ? clk : out;
|
69 |
|
|
|
70 |
|
|
assign match = (cnt==(divider-1));
|
71 |
|
|
assign risingMatch = match & ~out;
|
72 |
|
|
assign fallingMatch = match & out;
|
73 |
|
|
|
74 |
|
|
always @(posedge clk, negedge nReset)
|
75 |
|
|
begin
|
76 |
|
|
if(~nReset | divideBy1) begin
|
77 |
|
|
cnt <= 0;
|
78 |
|
|
out <= 1'b0;
|
79 |
|
|
end else if(~divideBy1) begin
|
80 |
|
|
if(match) begin
|
81 |
|
|
cnt <= 0;
|
82 |
|
|
out <= ~out;
|
83 |
|
|
end else begin
|
84 |
|
|
cnt <= cnt + 1'b1;
|
85 |
|
|
end
|
86 |
|
|
end
|
87 |
|
|
end
|
88 |
|
|
|
89 |
|
|
endmodule
|
90 |
11 |
acapola |
`default_nettype wire
|