-
Notifications
You must be signed in to change notification settings - Fork 1
/
Freq_ratio.sv
206 lines (179 loc) · 4.12 KB
/
Freq_ratio.sv
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//Author: Jisu
//1/7/2023
// This unit measure the frequency of the input and return as a positive integer.
//reference clock(refClk) is the input frequency, which is slower than the ring oscillator generated clock(clk)
//use average to get more accurate difference.
`timescale 1ps/1ps
module Freq_ratio(En,Reset,F_clk,F_ring,C_freq);
input F_clk,F_ring;//F_clk: frequency of input clock; F_ring: frequency of a ring oscillator.
input En, Reset; // Enable signal, Reset signal.
logic combReset;//high reset
output logic [31:0]C_freq=32'b0; //this is the output connected to the logic module
logic [6:0]clkCounter=7'b0; // this is connected to an output for debug purpose. We will change this to a internal reg later.
logic [31:0]ringCounter=32'b0; //this is connected to an output for debug purpose. We will change this to a internal reg later.
parameter phaseAvg=7'd100; // this is controlling condition to measure certein numbers of slower clock. For other modules' design it should be 100.
//if clkCounter = phaseAvg; C_freq = ringcounter;
always_ff @(posedge F_clk or posedge combReset or negedge En) begin
//(neg) hold when enable goes 0
if(!En) begin
clkCounter <= clkCounter;
end
//(pos) reset the counter
else if(combReset) begin
clkCounter <= 7'b0;
end
//add clkCounter 1'b1.
else begin
if(clkCounter == 7'b1111111) begin
clkCounter <= 7'b0;
end
else begin
clkCounter <= clkCounter +7'b1;
end
end
end
always_ff @(posedge F_ring or posedge combReset or negedge En) begin
if(!En) begin
ringCounter <= ringCounter;
end
else if(combReset) begin
ringCounter <= 9'b0;
end
else begin
/*if(ringCounter == 32'hFFFFFFFF) begin
ringCounter <= 32'b0;
end
else begin
*/ringCounter <= ringCounter +9'b1;/*
end
*/
end
end
always @(posedge F_clk or posedge Reset or negedge En) begin
if(!En) begin
C_freq <= C_freq;
combReset <=combReset;
end
else if(Reset) begin
C_freq <= 1'b0;
combReset <=1'b1;
end
else begin
if ( clkCounter == phaseAvg-1) begin
C_freq <= ringCounter; //save ringCounter to C_freq now
combReset <= 1'b1; //set reset to 1 now
end
else begin
C_freq <= C_freq;
combReset <= 1'b0;
end
end
end
endmodule
/*
`timescale 1ps/1ps
module Freq_Ratio_tb();
logic clk, refClk,En,Reset; // speed: clk < refClk
logic [31:0] freqNum;
//logic [15:0]counter;
//module Freq_ratio(En,Reset,F_clk,F_ring,C_freq);
Freq_Ratio DUT (En,Reset,clk, refClk,freqNum);
initial begin
refClk=1;#10;
refClk=0;#10;
refClk=1;#10;
refClk=0;#10;
clk =1;
refClk=1;#10;
refClk=0;#10;
refClk=1;#10;
clk =0;
refClk=0;#10;
refClk=1;#10;
refClk=0;#10;
clk =1;
refClk=1;#10;
refClk=0;#10;
refClk=1;#10;
clk =0;
refClk=0;#10;
refClk=1;#10;
refClk=0;#10;
clk =1;
refClk=1;#10;
refClk=0;#10;
refClk=1;#10;
clk =0;
refClk=0;#10;
refClk=1;#10;
refClk=0;#10;
clk =1;
refClk=0;#10;
refClk=1;#10;
refClk=0;#10;
refClk=1;#10;
clk =0;
refClk=0;#10;
refClk=1;#10;
refClk=0;#10;
refClk=1;#10;
clk =1;
refClk=0;#10;
refClk=1;#10;
refClk=0;#10;
refClk=1;#10;
clk =0;
refClk=0;#10;
refClk=1;#10;
refClk=0;#10;
refClk=1;#10;
clk =1;
refClk=0;#10;
refClk=1;#10;
refClk=0;#10;
refClk=1;#10;
clk =0;
refClk=0;#10;
refClk=1;#10;
refClk=0;#10;
refClk=1;#10;
clk =1;
refClk=0;#10;
refClk=1;#10;
refClk=0;#10;
refClk=1;#10;
clk =0;
refClk=0;#10;
refClk=1;#10;
refClk=0;#10;
refClk=1;#10;
clk =1;
refClk=0;#10;
refClk=1;#10;
refClk=0;#10;
refClk=1;#10;
clk =0;
refClk=0;#10;
refClk=1;#10;
refClk=0;#10;
refClk=1;#10;
clk =1;
refClk=0;#10;
refClk=1;#10;
refClk=0;#10;
refClk=1;#10;
clk =0;
refClk=0;#10;
refClk=1;#10;
refClk=0;#10;
refClk=1;#10;
clk =1;
refClk=0;#10;
refClk=1;#10;
refClk=0;#10;
refClk=0;#10;
refClk=1;#10;
refClk=0;#10;
end
endmodule
*/