Skip to content

Commit 2f23a21

Browse files
Alex YazdaniAlex Yazdani
authored andcommitted
frequency_divider.v
1 parent a93bda4 commit 2f23a21

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

frequency_divider.v

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Alexander Yazdani
3+
2 December 2024
4+
Frequency Divider (supports multiples of 0.5)
5+
*/
6+
7+
module frequency_divider #(parameter MULTIPLIER = 2)(
8+
input clk_in,
9+
input reset,
10+
output reg clk_out
11+
);
12+
13+
integer counter;
14+
15+
always @(posedge clk_in or negedge clk_in) begin
16+
if (reset) begin
17+
counter <= 0;
18+
clk_out <= 0;
19+
end else begin
20+
if (counter >= (MULTIPLIER*2)-1) begin
21+
counter <= 0;
22+
clk_out <= ~ clk_out;
23+
end else counter <= counter + 1;
24+
end
25+
end
26+
27+
endmodule
28+
29+
// Testbench:
30+
module tb_frequency_divider;
31+
32+
reg clk_in;
33+
reg reset;
34+
wire clk_out;
35+
36+
// Instantiate the frequency divider with a fractional multiplier
37+
frequency_divider #(3.5) uut (
38+
.clk_in(clk_in),
39+
.reset(reset),
40+
.clk_out(clk_out)
41+
);
42+
43+
// Generate the input clock (50 MHz -> 20ns period)
44+
initial begin
45+
clk_in = 0;
46+
forever #10 clk_in = ~clk_in; // Toggle every 10ns
47+
end
48+
49+
// Test sequence
50+
initial begin
51+
// Initialize
52+
reset = 1;
53+
#25;
54+
reset = 0;
55+
56+
// Run simulation for a few cycles
57+
#500;
58+
59+
// End simulation
60+
$finish;
61+
end
62+
63+
// Monitor output
64+
initial begin
65+
$monitor("Time: %0t | clk_in: %b | clk_out: %b | reset: %b",
66+
$time, clk_in, clk_out, reset);
67+
end
68+
69+
endmodule

0 commit comments

Comments
 (0)