I learn "Do not mix negedge posedge for the same clock in one module (synthesis is possible, but analysis is difficult)"
"So chat gpt
always @(posedge clk)
...
always @(negedge clk)
I was wondering how to change this shape.
always @(posedge clk) begin
// Code that executes on the positive edge of the clock
if (/* condition */) begin
// Code that executes on the negative edge of the clock
end
end
It is recommended to change to this form. So, when I changed the code below to the second code, the waveform is different. How do I change it when negedge and posedge are used together in one module?"
first code wave form enter image description here
second code wave form enter image description here
first code
`timescale 1ns / 1ps
module DEFF (
input clk, reset, in,
output out
);
reg trig1 = 0, trig2 = 0;
assign out = trig1 ^ trig2;
always @(posedge clk, posedge reset) begin
if (reset) trig1 <= 0;
trig1 <= in^trig2;
end
always @(negedge clk, posedge reset) begin
if (reset) trig2 <= 0;
trig2 <= in^trig1;
end
endmodule
second code
module DEFF (
input clk, reset, in,
output out
);
reg clk_prev = 1'b0;
reg trig1 = 0, trig2 = 0;
reg pos_edge_detected = 0;
reg neg_edge_detected = 0;
assign out = trig1 ^ trig2;
always @(posedge clk, posedge reset) begin
if (reset) begin
trig1 <= 0;
trig2 <= 0;
end else begin
trig1 <= in ^ trig2;
if (!trig1) begin
trig2 <= in ^ trig1;
end
end
end
endmodule
testbench
`timescale 1ns / 1ps
module TB_DEFF;
// Inputs
reg clk;
reg reset = 0;
reg in;
// Outputs
wire out;
// Instantiate the Unit Under Test (UUT)
DEFF DUT (
.clk(clk),
.reset(reset),
.in(in),
.out(out)
);
always #5 clk = ~clk;
// Toggle the clock and input
initial begin
clk = 0;
reset = 1;
in = 0;
#10 reset = 0;
#10 in = 1;
#20 in = 0;
#20 in = 1;
#20 in = 0;
#20 $finish;
end
endmodule