A systemverilog module that can be instantiated to detect a positive edge, negedge, or both. Aim for clarity and function. Think about systemverilog coding practices before responding. Also give an example of an instantiation of your module.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
I'll create a SystemVerilog module for edge detection that is clear, reusable, and follows good coding practices. The module will be parameterizable to detect positive edges, negative edges, or both, and will include proper documentation and error checking.
Here's the implementation:
systemverilog
// edge_detector.sv
// Description: Configurable edge detector module
// Features:
// - Parameterizable edge detection (posedge, negedge, or both)
// - Synchronous reset
// - Clear output signals
// - Input validation
module edge_detector #(
parameter DETECT_TYPE = "BOTH" // Options: "POSEDGE", "NEGEDGE", "BOTH"
) (
input logic clk, // System clock
input logic rst_n, // Active-low synchronous reset
input logic signal_in, // Input signal to detect edges on
output logic posedge_detected, // Pulse on positive edge
output logic negedge_detected // Pulse on negative edge
);
// Local parameters for edge detection types
localparam string POSEDGE = "POSEDGE";
localparam string NEGEDGE = "NEGEDGE";
localparam string BOTH = "BOTH";
// Internal signals
logic signal_in_d; // Delayed version of input signal
// Input parameter validation
initial begin
if (DETECT_TYPE != POSEDGE && DETECT_TYPE != NEGEDGE && DETECT_TYPE != BOTH) begin
$error("Invalid DETECT_TYPE parameter. Must be 'POSEDGE', 'NEGEDGE', or 'BOTH'");
end
end
// Register input signal to detect edges
always_ff @(posedge clk) begin
if (!rst_n) begin
signal_in_d <= 1'b0;
end else begin
signal_in_d <= signal_in;
end
end
// Edge detection logic
always_comb begin
// Default outputs to 0
posedge_detected = 1'b0;
negedge_detected = 1'b0;
// Detect edges based on DETECT_TYPE parameter
case (DETECT_TYPE)
POSEDGE: begin
posedge_detected = signal_in & ~signal_in_d;
end
NEGEDGE: begin
negedge_detected = ~signal_in & signal_in_d;
end
BOTH: begin
posedge_detected = signal_in & ~signal_in_d;
negedge_detected = ~signal_in & signal_in_d;
end
default: begin
posedge_detected = 1'b0;
negedge_detected = 1'b0;
end
endcase
end
endmodule : edge_detector
Here's an example of how to instantiate and use this module:
systemverilog
// top_module.sv
// Example instantiation of edge_detector module
module top_module (
input logic clk,
input logic rst_n,
input logic input_signal,
output logic pos_edge_out,
output logic neg_edge_out
);
// Instantiate edge detector for both edges
edge_detector #(
.DETECT_TYPE("BOTH")
) edge_detector_both (
.clk(clk),
.rst_n(rst_n),
.signal_in(input_signal),
.posedge_detected(pos_edge_out),
.negedge_detected(neg_edge_out)
);
// Example of instantiating for posedge only
/*
edge_detector #(
.DETECT_TYPE("POSEDGE")
) edge_detector_pos (
.clk(clk),
.rst_n(rst_n),
.signal_in(input_signal),
.posedge_detected(pos_edge_out),
.negedge_detected() // Leave unconnected if not needed
);
*/
endmodule : top_module
Key features and good practices implemented in this code:
Documentation: Clear comments explaining the module's purpose, features, and parameters.
Parameterization: Uses a string parameter to configure the detection type, making it easy to understand and use.
Input Validation: Includes an initial block to check for valid parameter values and generate an error if invalid.
Naming Conventions: Clear and consistent naming (e.g., _n suffix for active-low signals, _d for delayed signals).
Reset Strategy: Implements synchronous active-low reset, which is a common practice in digital design.
Coding Style:
Uses always_ff for sequential logic and always_comb for combinational logic
Proper indentation and organization
Default values in combinational logic to prevent latches
Explicit port connections in instantiations
Modularity: The module is self-contained and can be easily reused in different designs.
Output Signals: Provides separate outputs for posedge and negedge detection, making it flexible for different use cases.
To use this module in a testbench or larger design, you would:
Include the file in your compilation
Instantiate it with the desired DETECT_TYPE parameter
Connect the appropriate signals
Use the output pulses as needed in your design
The output pulses (posedge_detected and negedge_detected) will be active for one clock cycle when the corresponding edge is detected, making them suitable for triggering other logic in your design.