• 校招Verilog——glitch free时钟切换电路


    要求:

      用 Verilog 实现 glitch free 时钟切换电路。输入 sel,clka,clkb,sel 为 1 输出 clka,sel 为 0 输出 clkb。

    module Change_Clk_Source
    (
        input   rst_n,
        input   clk_a,
        input   clk_b,
        input   sel,
        output  clk_o
    );

    一、有毛刺写法

    assign clk_o = sel ? clk_a : clk_b;

    二、两个时钟是倍数关系

    reg clk_a_vld;
    reg clk_b_vld;
    
    always @(negedge clk_a or negedge rst_n)begin
        if(!rst_n)begin
            clk_a_vld <= 0;
        end
        else begin
            clk_a_vld <= sel & ~clk_b_vld;
        end
    end
    
    always @(negedge clk_b or negedge rst_n)begin
        if(!rst_n)begin
            clk_b_vld <= 0;
        end
        else begin
            clk_b_vld <= ~sel & ~clk_a_vld;
        end
    end
    
    assign clk_o = (clk_a_vld & clk_a) | (clk_b_vld & clk_b);

    三、两个时钟是异步关系

    reg clk_a_vld;
    reg clk_b_vld;
    reg clk_a_vld_r;
    reg clk_b_vld_r;
    
    always @(posedge clk_a or negedge rst_n)begin
        if(!rst_n)begin
            clk_a_vld   <= 0;
            clk_a_vld_r <= 0;
        end
        else begin
            clk_a_vld   <= sel & ~clk_b_vld_r;
            clk_a_vld_r <= clk_a_vld;
        end
    end
    
    always @(posedge clk_b or negedge rst_n)begin
        if(rst_n == 1'b0)begin
            clk_b_vld   <= 0;
            clk_b_vld_r <= 0;
        end
        else begin
            clk_b_vld   <= ~sel & ~clk_a_vld_r;
            clk_b_vld_r <= clk_b_vld;
        end
    end
    
    assign clk_o = (clk_a_vld_r & clk_a) | (clk_b_vld_r & clk_b);
    
    endmodule
    --------------------------------------------------------------------------------------
    作者:咸鱼FPGA
    本文版权归作者所有,如需转载请保留此段声明。
  • 相关阅读:
    [BZOJ4755][JSOI2016]扭动的回文串(manacher+Hash)
    十二省联考2019部分题解
    [BZOJ2959]长跑(LCT+并查集)
    [BZOJ4541][HNOI2016]矿区(平面图转对偶图)
    笛卡尔树
    [CF896C]Willem, Chtholly and Seniorious(珂朵莉树)
    [BZOJ4349]最小树形图
    [BZOJ1858][SCOI2010]序列操作(线段树)
    [PA2014]Parking
    [PA2014]Budowa
  • 原文地址:https://www.cnblogs.com/xianyufpga/p/13641598.html
Copyright © 2020-2023  润新知