题解 | #时钟切换#
时钟切换
http://www.nowcoder.com/practice/1de5e9bf749244cb8e5908626cc36d36
因为牛客网标准答案中2个DFF的驱动时钟搞反了。以下能通过
`timescale 1ns/1ns
module huawei6(
input wire clk0 ,
input wire clk1 ,
input wire rst ,
input wire sel ,
output reg clk_out
);
//*************code***********//
reg sel1, sel0;
always @ (negedge clk0, negedge rst) begin
if(!rst)
sel1 <= 0;
else
sel1 <= sel & ~sel0;
end
always @ (negedge clk1, negedge rst) begin
if(!rst)
sel0 <= 0;
else
sel0 <= ~sel & ~sel1;
end
always @ (*) begin
clk_out <= (sel1 & clk1) | (sel0 & clk0);
end
//*************code***********//
endmodule
实际正确代码
`timescale 1ns/1ns
module huawei6(
input wire clk0 ,
input wire clk1 ,
input wire rst ,
input wire sel ,
output reg clk_out
);
//*************code***********//
reg sel1, sel0;
always @ (negedge clk1, negedge rst) begin
if(!rst)
sel1 <= 0;
else
sel1 <= sel & ~sel0;
end
always @ (negedge clk0, negedge rst) begin
if(!rst)
sel0 <= 0;
else
sel0 <= ~sel & ~sel1;
end
always @ (*) begin
clk_out <= (sel1 & clk1) | (sel0 & clk0);
end
//*************code***********//
endmodule