题解 | #时钟分频(偶数)#

时钟分频(偶数)

https://www.nowcoder.com/practice/49a7277c203a4ddd956fa385e687a72e

//解法1:


`timescale 1ns/1ns

module even_div
    (
    input     wire rst ,
    input     wire clk_in,
    output    wire clk_out2,
    output    wire clk_out4,
    output    wire clk_out8
    );
//*************code***********//
    reg [2:0] cnt;
    reg clk_out2_r, clk_out4_r, clk_out8_r;
    always @ (posedge clk_in, negedge rst) begin
        if(~rst) begin
            cnt <= 3'b0;
        end
        else  begin
            cnt <= (cnt == 3'd7) ? 3'd0 : cnt + 3'd1;
        end
    end
    
    always @ (posedge clk_in, negedge rst) begin
        if(~rst) begin
            clk_out2_r <= 1'b0;
        end
        else begin
            clk_out2_r <= ~clk_out2_r;
        end
    end
    assign clk_out2 = clk_out2_r;
    
    always @ (posedge clk_in, negedge rst) begin
        if(~rst) begin
            clk_out4_r <= 1'b0;
        end
        else if (cnt % 2 == 1'b0) begin
            clk_out4_r <= ~clk_out4_r;
        end
    end
    assign clk_out4 = clk_out4_r;
    
    always @ (posedge clk_in, negedge rst) begin
        if(~rst) begin
            clk_out8_r <= 1'b0;
        end
        else begin
            clk_out8_r <= ((cnt == 3'd4) || (cnt == 3'd0)) ? (~clk_out8_r) : clk_out8_r;
        end
    end
    assign clk_out8 = clk_out8_r;


//*************code***********//
endmodule
自己解法,偶数分频,0和n/2开始翻转,

解法2:
`timescale 1ns/1ns

module even_div
    (
    input     wire rst ,
    input     wire clk_in,
    output    wire clk_out2,
    output    wire clk_out4,
    output    wire clk_out8
    );
//*************code***********//
    reg clk_out2_r, clk_out4_r, clk_out8_r;
    
    always@(posedge clk_in or negedge rst) begin
        if(~rst)
            clk_out2_r <= 0;
        else
            clk_out2_r <= ~clk_out2_r;
    end
    
    always@(posedge clk_out2 or negedge rst) begin
        if(~rst)
            clk_out4_r <= 0;
        else
            clk_out4_r <= ~clk_out4_r;
    end
    
    always@(posedge clk_out4 or negedge rst) begin
        if(~rst)
            clk_out8_r <= 0;
        else
            clk_out8_r <= ~clk_out8_r;
    end
    
    assign clk_out2 = clk_out2_r;
    assign clk_out4 = clk_out4_r;
    assign clk_out8 = clk_out8_r;
//*************code***********//
endmodule




全部评论

相关推荐

Southyeung:我说一下我的看法(有冒犯实属抱歉):(1)简历不太美观,给我一种看都不想看的感觉,感觉字体还是排版问题;(2)numpy就一个基础包,机器学习算法是什么鬼?我感觉你把svm那些写上去都要好一点。(2)课程不要写,没人看,换成获奖经历;(3)项目太少了,至少2-3个,是在不行把网上学习的也写上去。
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务