Eunchan Kim | 13ad1c4 | 2021-08-25 17:39:41 -0700 | [diff] [blame] | 1 | // Copyright lowRISC contributors. |
| 2 | // Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | // Edge Detector |
| 6 | |
| 7 | module prim_edge_detector #( |
| 8 | parameter int unsigned Width = 1, |
| 9 | |
| 10 | parameter logic [Width-1:0] ResetValue = '0, |
| 11 | |
| 12 | // EnSync |
| 13 | // |
| 14 | // Enable Synchronizer to the input signal. |
| 15 | // It is assumed that the input signal is glitch free (registered input). |
| 16 | parameter bit EnSync = 1'b 1 |
| 17 | ) ( |
| 18 | input clk_i, |
| 19 | input rst_ni, |
| 20 | |
| 21 | input [Width-1:0] d_i, |
| 22 | output logic [Width-1:0] q_sync_o, |
| 23 | |
| 24 | output logic [Width-1:0] q_posedge_pulse_o, |
| 25 | output logic [Width-1:0] q_negedge_pulse_o |
| 26 | ); |
| 27 | |
| 28 | logic [Width-1:0] q_sync_d, q_sync_q; |
| 29 | |
| 30 | if (EnSync) begin : g_sync |
| 31 | prim_flop_2sync #( |
| 32 | .Width (Width), |
| 33 | .ResetValue (ResetValue) |
| 34 | ) u_sync ( |
| 35 | .clk_i, |
| 36 | .rst_ni, |
| 37 | .d_i, |
| 38 | .q_o (q_sync_d) |
| 39 | ); |
| 40 | end : g_sync |
| 41 | else begin : g_nosync |
| 42 | assign q_sync_d = d_i; |
| 43 | end : g_nosync |
| 44 | |
| 45 | assign q_sync_o = q_sync_d; |
| 46 | |
| 47 | always_ff @(posedge clk_i or negedge rst_ni) begin |
| 48 | if (!rst_ni) q_sync_q <= ResetValue; |
| 49 | else q_sync_q <= q_sync_d; |
| 50 | end |
| 51 | |
Eunchan Kim | 505776b | 2021-09-27 12:05:00 -0700 | [diff] [blame] | 52 | assign q_posedge_pulse_o = q_sync_d & ~q_sync_q; |
| 53 | assign q_negedge_pulse_o = ~q_sync_d & q_sync_q; |
Eunchan Kim | 13ad1c4 | 2021-08-25 17:39:41 -0700 | [diff] [blame] | 54 | |
| 55 | endmodule : prim_edge_detector |