题解 | #时钟切换#

时钟切换

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
全部评论
兄弟 你这代码好像也过不了
点赞 回复 分享
发布于 2023-02-21 21:14 北京

相关推荐

点赞 评论 收藏
分享
评论
6
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务