JulienOury | 7f85918 | 2022-03-17 08:25:03 +0100 | [diff] [blame] | 1 | //////////////////////////////////////////////////////////////////////////// |
| 2 | // SPDX-FileCopyrightText: 2022 , Julien OURY |
| 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 Julien OURY <julien.oury@outlook.fr> |
| 17 | // |
| 18 | //////////////////////////////////////////////////////////////////////////// |
| 19 | |
| 20 | module prescaler #( |
| 21 | parameter BITS = 32 |
| 22 | )( |
| 23 | input wire rst_n , // Asynchronous reset (active low) |
| 24 | input wire clk , // Clock (rising edge) |
osboxes.org | 23d9f85 | 2022-03-17 20:33:04 -0400 | [diff] [blame^] | 25 | input wire clear_n , // Synchronous reset (active low) |
JulienOury | 7f85918 | 2022-03-17 08:25:03 +0100 | [diff] [blame] | 26 | |
| 27 | input wire [BITS-1:0] multiplier , // frequency multiplier |
| 28 | input wire [BITS-1:0] divider , // frequency divider |
| 29 | |
| 30 | output reg tick // output clock [Ftick=Fclk*(multiplier/divider)] with multiplier <= divider |
| 31 | |
| 32 | ); |
| 33 | |
| 34 | wire [BITS-1:0] next_counter; |
| 35 | reg [BITS-1:0] counter; |
| 36 | |
| 37 | assign next_counter = counter + multiplier; |
| 38 | |
| 39 | always @(negedge rst_n or posedge clk) begin |
| 40 | if (rst_n == 1'b0) begin |
| 41 | counter <= 1'b0; |
| 42 | tick <= 1'b0; |
| 43 | end else begin |
| 44 | if (clear_n == 1'b0) begin |
| 45 | counter <= 1'b0; |
| 46 | tick <= 1'b0; |
| 47 | end else begin |
| 48 | if (next_counter > divider) begin |
| 49 | counter <= next_counter - divider; |
| 50 | tick <= 1'b1; |
| 51 | end else begin |
| 52 | counter <= next_counter; |
| 53 | tick <= 1'b0; |
| 54 | end |
| 55 | end |
| 56 | end |
| 57 | end |
| 58 | |
| 59 | endmodule |