dineshannayya | 52e8a34 | 2022-02-15 14:19:56 +0530 | [diff] [blame] | 1 | ////////////////////////////////////////////////////////////////////////////// |
| 2 | // SPDX-FileCopyrightText: 2021 , Dinesh Annayya |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // SPDX-License-Identifier: Apache-2.0 |
| 16 | // SPDX-FileContributor: Created by Dinesh Annayya <dinesha@opencores.org> |
| 17 | //////////////////////////////////////////////////////////////////////// |
| 18 | //// //// |
| 19 | //// Tubo 8051 cores common library Module //// |
| 20 | //// //// |
| 21 | //// This file is part of the Turbo 8051 cores project //// |
| 22 | //// https://github.com/dineshannayya/yifive_r0.git //// |
| 23 | //// http://www.opencores.org/cores/turbo8051/ //// |
| 24 | //// //// |
| 25 | //// Description //// |
| 26 | //// Turbo 8051 definitions. //// |
| 27 | //// //// |
| 28 | //// To Do: //// |
| 29 | //// nothing //// |
| 30 | //// //// |
| 31 | //// Author(s): //// |
| 32 | //// - Dinesh Annayya, dinesha@opencores.org //// |
| 33 | //// //// |
| 34 | //// Revision : //// |
| 35 | //// 1.0 Mar 2, 2011,Dinesh.A //// |
| 36 | //// Initial Version //// |
| 37 | //// 1.1 Nov 15,2021,Dinesh A //// |
| 38 | //// Bug fix in High and Low count width //// |
| 39 | //// //// |
| 40 | ////////////////////////////////////////////////////////////////////// |
| 41 | //// //// |
| 42 | //// Copyright (C) 2000 Authors and OPENCORES.ORG //// |
| 43 | //// //// |
| 44 | //// This source file may be used and distributed without //// |
| 45 | //// restriction provided that this copyright statement is not //// |
| 46 | //// removed from the file and that any derivative work contains //// |
| 47 | //// the original copyright notice and the associated disclaimer. //// |
| 48 | //// //// |
| 49 | //// This source file is free software; you can redistribute it //// |
| 50 | //// and/or modify it under the terms of the GNU Lesser General //// |
| 51 | //// Public License as published by the Free Software Foundation; //// |
| 52 | //// either version 2.1 of the License, or (at your option) any //// |
| 53 | //// later version. //// |
| 54 | //// //// |
| 55 | //// This source is distributed in the hope that it will be //// |
| 56 | //// useful, but WITHOUT ANY WARRANTY; without even the implied //// |
| 57 | //// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //// |
| 58 | //// PURPOSE. See the GNU Lesser General Public License for more //// |
| 59 | //// details. //// |
| 60 | //// //// |
| 61 | //// You should have received a copy of the GNU Lesser General //// |
| 62 | //// Public License along with this source; if not, download it //// |
| 63 | //// from http://www.opencores.org/lgpl.shtml //// |
| 64 | //// //// |
| 65 | ////////////////////////////////////////////////////////////////////// |
| 66 | |
| 67 | // ################################################################# |
| 68 | // Module: clk_ctl |
| 69 | // |
| 70 | // Description: Generic clock control logic , clk-out = mclk/(2+clk_div_ratio) |
| 71 | // |
| 72 | // |
| 73 | // ################################################################# |
| 74 | |
| 75 | |
| 76 | module clk_ctl ( |
| 77 | // Outputs |
| 78 | clk_o, |
| 79 | // Inputs |
| 80 | mclk, |
| 81 | reset_n, |
| 82 | clk_div_ratio |
| 83 | ); |
| 84 | |
| 85 | //--------------------------------- |
| 86 | // CLOCK Default Divider value. |
| 87 | // This value will be change from outside |
| 88 | //--------------------------------- |
| 89 | parameter WD = 'h1; |
| 90 | |
| 91 | //--------------------------------------------- |
| 92 | // All the input to this block are declared here |
| 93 | // -------------------------------------------- |
| 94 | input mclk ;// |
| 95 | input reset_n ;// primary reset signal |
| 96 | input [WD:0] clk_div_ratio ;// primary clock divide ratio |
| 97 | // output clock = selected clock / (div_ratio+1) |
| 98 | |
| 99 | //--------------------------------------------- |
| 100 | // All the output to this block are declared here |
| 101 | // -------------------------------------------- |
| 102 | output clk_o ; // clock out |
| 103 | |
| 104 | |
| 105 | |
| 106 | //------------------------------------ |
| 107 | // Clock Divide func is done here |
| 108 | //------------------------------------ |
| 109 | reg [WD:0] high_count ; // high level counter |
| 110 | reg [WD:0] low_count ; // low level counter |
| 111 | reg mclk_div ; // divided clock |
| 112 | |
| 113 | |
| 114 | assign clk_o = mclk_div; |
| 115 | |
| 116 | always @ (posedge mclk or negedge reset_n) |
| 117 | begin // { |
| 118 | if(reset_n == 1'b0) |
| 119 | begin |
| 120 | high_count <= 'h0; |
| 121 | low_count <= 'h0; |
| 122 | mclk_div <= 'b0; |
| 123 | end |
| 124 | else |
| 125 | begin |
| 126 | if(high_count != 0) |
| 127 | begin // { |
| 128 | high_count <= high_count - 1; |
| 129 | mclk_div <= 1'b1; |
| 130 | end // } |
| 131 | else if(low_count != 0) |
| 132 | begin // { |
| 133 | low_count <= low_count - 1; |
| 134 | mclk_div <= 1'b0; |
| 135 | end // } |
| 136 | else |
| 137 | begin // { |
| 138 | high_count <= clk_div_ratio[WD:1] + clk_div_ratio[0]; |
| 139 | low_count <= clk_div_ratio[WD:1] + 1; |
| 140 | mclk_div <= ~mclk_div; |
| 141 | end // } |
| 142 | end // } |
| 143 | end // } |
| 144 | |
| 145 | |
| 146 | endmodule |
| 147 | |