题解 | #任意奇数倍时钟分频#
https://www.nowcoder.com/practice/b058395d003344e0a74dd67e44a33fae
`timescale 1ns/1ns
module clk_divider
#(parameter dividor = 5)
( input clk_in,
input rst_n,
output clk_out
);
reg [$clog2(dividor)-1'b1:0] cnt;
reg flag1,flag2;
always @ (posedge clk_in or negedge rst_n) begin
if (!rst_n)
cnt<=0;
else
cnt<=(cnt==dividor-1)?0:cnt+1;
end
always @ (posedge clk_in or negedge rst_n) begin
if (!rst_n)
flag1<=0;
else
flag1<=(cnt<dividor-1&&cnt>=(dividor-1)>>1)?1:0;
always @ (negedge clk_in or negedge rst_n) begin
if (!rst_n)
flag2<=0;
else
flag2<=(cnt<dividor-1&&cnt>=(dividor-1)>>1)?1:0;
end
assign clk_out=flag1||flag2;
endmodule
module clk_divider
#(parameter dividor = 5)
( input clk_in,
input rst_n,
output clk_out
);
reg [$clog2(dividor)-1'b1:0] cnt;
reg flag1,flag2;
always @ (posedge clk_in or negedge rst_n) begin
if (!rst_n)
cnt<=0;
else
cnt<=(cnt==dividor-1)?0:cnt+1;
end
always @ (posedge clk_in or negedge rst_n) begin
if (!rst_n)
flag1<=0;
else
flag1<=(cnt<dividor-1&&cnt>=(dividor-1)>>1)?1:0;
end
if (!rst_n)
flag2<=0;
else
flag2<=(cnt<dividor-1&&cnt>=(dividor-1)>>1)?1:0;
end
assign clk_out=flag1||flag2;
endmodule