clock divider RTL 如何描述?

//function: randome divider clk_out= clk/k1
//note
//    k1:  must bigger than 1 (>=2)
//    div_change_en: enable signal for change the k1
module clock_div(
    //input
      clk,       //input clk
      rstn,
      k1,         //counter value
     //output
      k1_clk,
      change_clk_en
    );

parameter DIV_WIDTH=5;

//***********************************************************************
//input output
input [DIV_WIDTH-1:0] k1;  //input divider value
input                 clk;
input                 rstn;
output                k1_clk;
output                change_clk_en;

wire  [DIV_WIDTH-1:0]   k1_sync;
wire  [DIV_WIDTH-1:0]   k1_sync_dec;
reg   [DIV_WIDTH-1:0]   k1_cnt;
reg                     k1_clk;

// -----------------------------------------------------------------------------
//  k1,k1_cnt,k1_clk random divider
// -----------------------------------------------------------------------------
assign k1_sync = k1;
assign k1_sync_dec = k1 -1'b1;

// -----------------------------------------------------------------------------
// k1_cnt
// -----------------------------------------------------------------------------
always @(posedge clk or negedge rstn)  
 begin
  if(~rstn)
     k1_cnt <= #0 0;
  else if((k1_cnt < k1_sync_dec ))
     k1_cnt <= #0 k1_cnt + 1;
  else
     k1_cnt <= #0 0;
 end

// -----------------------------------------------------------------------------
// k1_clk
// -----------------------------------------------------------------------------
//*****Special Notes ********
//***Use "=" in the always is only for simulation
//** the "clk" will generate the clock signal "k1_clk" and data signal
//** it is only that the generated data signal can be adopted right by "k1_clk"
//** it has not effect on Synthesis implementation
//***********
always @(posedge clk or negedge rstn)
 begin
  if(~rstn)
     k1_clk =  1'b1;
  else if(k1_cnt == (k1[DIV_WIDTH-1:1] -  1))
     k1_clk =  1'b0;
  else if(k1_cnt == (k1_sync_dec))
     k1_clk =  1'b1;
  else
     k1_clk =  k1_clk;
 end

//change enable signal for divider change valule
//clk_out enable:
//******Special Note: need add #1 delay when the before register add delay
//for simulation
//assign #2 change_clk_en = (k1_cnt == (k1_sync_dec));
assign  change_clk_en = (k1_cnt == (k1_sync_dec));

endmodule

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值