|
1 | 1 | // ****************************************************************************************************** |
2 | 2 | // |
3 | | -// BHG_FP_clk_divider.v V1.1, August 2022. |
| 3 | +// BHG_FP_clk_divider.v V1.2, August 2022. |
4 | 4 | // Floating point clock divider/synthesizer. |
5 | 5 | // 24.16 (m.n) bit floating point clock divider. (Actually it is a fixed point fractional divider.) |
6 | 6 | // |
| 7 | +// 1.2 - Added a protection for when the integer divider has less than 2 bits. |
| 8 | +// - Added a compilation $error and $stop with instructions if the user supplies inoperable CLK_HZ parameters. |
| 9 | +// 1.1 - Fixed a bug with some Modelsim versions where its 'Compile / Compile Options / Language Syntax' is set to 'Use Verilog 2001' instead of 'Default'. |
| 10 | +// 1.0 - Initial release. |
| 11 | +// |
7 | 12 | // Written by Brian Guralnick. |
8 | 13 | // https://github.com/BrianHGinc / or / https://www.eevblog.com/forum/fpga/ User BrianHG. |
9 | 14 | // |
@@ -37,6 +42,37 @@ parameter INPUT_CLK_HZ = 100000000 ; // Source clk_in frequency in Hz. |
37 | 42 | parameter OUTPUT_CLK_HZ = 3579545 ; // Desired clk_out frequency in Hz. |
38 | 43 |
|
39 | 44 |
|
| 45 | +// **************************************************************************** |
| 46 | +// Inoperable Parameter Error |
| 47 | +// **************************************************************************** |
| 48 | +generate |
| 49 | +if ( (OUTPUT_CLK_HZ*2)>INPUT_CLK_HZ ) initial begin |
| 50 | +$display(""); |
| 51 | +$display(" XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); |
| 52 | +$display(" XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); |
| 53 | +$display(" XXXXX XXXXX"); |
| 54 | +$display(" XXXXX BHG_FP_clk_divider.v inoperable parameter error. XXXXX"); |
| 55 | +$display(" XXXXX https://github.com/BrianHGinc XXXXX"); |
| 56 | +$display(" XXXXX XXXXX"); |
| 57 | +$display(" XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); |
| 58 | +$display(" XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); |
| 59 | +$display(" XXXXX XXXXX"); |
| 60 | +$display(" XXXXX BHG_FP_clk_divider.v can only generate an output clock at least 1/2 the source clock frequency. XXXXX"); |
| 61 | +$display(" XXXXX XXXXX"); |
| 62 | +$display(" XXXXX Your current set parameters: XXXXX"); |
| 63 | +$display(" XXXXX XXXXX"); |
| 64 | +$display(" XXXXX .INPUT_CLK_HZ = %d Hz. XXXXX",31'(INPUT_CLK_HZ)); |
| 65 | +$display(" XXXXX .OUTPUT_CLK_HZ = %d Hz. XXXXX",31'(OUTPUT_CLK_HZ)); |
| 66 | +$display(" XXXXX XXXXX"); |
| 67 | +$display(" XXXXX .OUTPUT_CLK_HZ must be less than or equal to .INPUT_CLK_HZ/2 for BHG_FP_clk_divider.v to function. XXXXX"); |
| 68 | +$display(" XXXXX XXXXX"); |
| 69 | +$display(" XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); |
| 70 | +$warning(" XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); |
| 71 | +$error; |
| 72 | +$stop; |
| 73 | +end |
| 74 | +endgenerate |
| 75 | + |
40 | 76 | // ************************************************************************************************************************************************************* |
41 | 77 | // Forcing 64 bits is required due to a ModelSim internal bug where if 'PLL1_OUT_TRUE_HZ*655360' exceeds 31 bits, it's considered negative and cropped |
42 | 78 | // when computing the localparam. So, never use 'localparam int' when going above 2 billion anywhere inside, or you will get bogus results. |
@@ -90,7 +126,8 @@ endgenerate |
90 | 126 | // Floating point clock divider logic. |
91 | 127 | // **************************************************************************** |
92 | 128 |
|
93 | | -localparam mb = $clog2(clk_per_int); |
| 129 | +localparam pmb = $clog2(clk_per_int); |
| 130 | +localparam mb = (pmb<2) ? 2 : pmb ; // Protection for when the integer divider has less than 2 bits. |
94 | 131 |
|
95 | 132 | reg [mb-1:0] clk_cnt_m = (mb)'(0) ; // This integer counts up 1 by 1 requiring manual bit reduction 'mb' for minimum logic cells. |
96 | 133 | reg [16:0] clk_cnt_n = 17'd0 ; // This fractional int adds upward by a fixed parameter, the FPGA compiler is capable of automatically pruning unused bits. |
|
0 commit comments