|
案例I 六分频
i:div_6.v
module div_6(
input i_clk,
input i_rst_n,
output reg o_clk
);
// log2(6) = 2.5850 <= 3
reg [2:0] cnt;
// 6 bit counter: 0 ~ 5
// 5 = 6 - 1
always @ (posedge i_clk, negedge i_rst_n)
begin
if (!i_rst_n)
cnt <= 0;
else
begin
if (cnt == 5)
cnt <= 0;
else
cnt <= cnt + 1'b1;
end
end
// 0 ~ 2 -> 1
// 2 ~ 5 -> 0
// 2 = 6>>1 - 1
// 5 = 6 - 1
always @ (posedge i_clk, negedge i_rst_n)
begin
if (!i_rst_n)
o_clk <= 0;
else
begin
if (cnt <= 2)
o_clk <= 1;
else
o_clk <= 0;
end
end
endmodule
案例II 十分频
i:div_10.v
module div_10(
input i_clk,
input i_rst_n,
output reg o_clk
);
// log2(10) = 3.3219 <= 4
reg [3:0] cnt;
// 10 bit counter: 0 ~ 9
// 9 = 10 - 1
always @ (posedge i_clk, negedge i_rst_n)
begin
if (!i_rst_n)
cnt <= 0;
else
begin
if (cnt == 9)
cnt <= 0;
else
cnt <= cnt + 1'b1;
end
end
// 0 ~ 4 -> 1
// 4 ~ 9 -> 0
// 4 = 10>>1 - 1
// 9 = 10 - 1
always @ (posedge i_clk, negedge i_rst_n)
begin
if (!i_rst_n)
o_clk <= 0;
else
begin
if (cnt <= 4)
o_clk <= 1;
else
o_clk <= 0;
end
end
endmodule |
|