2015-04-15 11:54:43 -04:00
|
|
|
// ###############################################################
|
|
|
|
// # FUNCTION: Synchronous clock divider that divides by integer
|
|
|
|
// ###############################################################
|
|
|
|
module clock_divider(/*AUTOARG*/
|
|
|
|
// Outputs
|
2015-05-06 12:26:07 -04:00
|
|
|
clkout,
|
2015-04-15 11:54:43 -04:00
|
|
|
// Inputs
|
|
|
|
clkin, divcfg, reset
|
|
|
|
);
|
|
|
|
|
|
|
|
input clkin; // Input clock
|
2015-05-06 12:26:07 -04:00
|
|
|
input [3:0] divcfg; // Divide factor (1-128)
|
2015-04-15 11:54:43 -04:00
|
|
|
input reset; // Counter init
|
|
|
|
output clkout; // Divided clock phase aligned with clkin
|
|
|
|
|
|
|
|
reg clkout_reg;
|
|
|
|
reg [7:0] counter;
|
|
|
|
reg [7:0] divcfg_dec;
|
2015-04-18 16:12:43 -04:00
|
|
|
reg [3:0] divcfg_reg;
|
|
|
|
|
2015-05-06 12:26:07 -04:00
|
|
|
wire div_bp;
|
2015-04-15 11:54:43 -04:00
|
|
|
wire posedge_match;
|
|
|
|
wire negedge_match;
|
2015-04-18 16:12:43 -04:00
|
|
|
|
2015-04-15 11:54:43 -04:00
|
|
|
// ###################
|
|
|
|
// # Decode divcfg
|
|
|
|
// ###################
|
|
|
|
|
|
|
|
always @ (divcfg[3:0])
|
|
|
|
casez (divcfg[3:0])
|
2015-11-03 19:56:27 -05:00
|
|
|
4'b0001 : divcfg_dec[7:0] = 8'b00000010; // Divide by 2
|
|
|
|
4'b0010 : divcfg_dec[7:0] = 8'b00000100; // Divide by 4
|
|
|
|
4'b0011 : divcfg_dec[7:0] = 8'b00001000; // Divide by 8
|
|
|
|
4'b0100 : divcfg_dec[7:0] = 8'b00010000; // Divide by 16
|
|
|
|
4'b0101 : divcfg_dec[7:0] = 8'b00100000; // Divide by 32
|
|
|
|
4'b0110 : divcfg_dec[7:0] = 8'b01000000; // Divide by 64
|
|
|
|
4'b0111 : divcfg_dec[7:0] = 8'b10000000; // Divide by 128
|
|
|
|
default : divcfg_dec[7:0] = 8'b00000000; // others
|
|
|
|
endcase
|
2015-04-15 11:54:43 -04:00
|
|
|
|
|
|
|
always @ (posedge clkin or posedge reset)
|
|
|
|
if(reset)
|
2015-05-06 12:26:07 -04:00
|
|
|
counter[7:0] <= 8'b00000001;
|
|
|
|
else if(posedge_match)
|
|
|
|
counter[7:0] <= 8'b00000001;// Self resetting
|
|
|
|
else
|
|
|
|
counter[7:0] <= (counter[7:0] + 8'b00000001);
|
2015-04-15 11:54:43 -04:00
|
|
|
|
|
|
|
assign posedge_match = (counter[7:0]==divcfg_dec[7:0]);
|
|
|
|
assign negedge_match = (counter[7:0]=={1'b0,divcfg_dec[7:1]});
|
2015-05-06 12:26:07 -04:00
|
|
|
|
2015-04-15 16:33:20 -04:00
|
|
|
always @ (posedge clkin or posedge reset)
|
|
|
|
if(reset)
|
|
|
|
clkout_reg <= 1'b0;
|
|
|
|
else if(posedge_match)
|
2015-04-15 11:54:43 -04:00
|
|
|
clkout_reg <= 1'b1;
|
|
|
|
else if(negedge_match)
|
|
|
|
clkout_reg <= 1'b0;
|
|
|
|
|
2015-05-06 12:26:07 -04:00
|
|
|
//Divide by one bypass
|
|
|
|
assign div_bp = (divcfg[3:0]==4'b0000);
|
|
|
|
assign clkout = div_bp ? clkin : clkout_reg;
|
|
|
|
|
2015-04-15 11:54:43 -04:00
|
|
|
endmodule // clock_divider
|
|
|
|
|