-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrigger_delay.sv
85 lines (69 loc) · 1.76 KB
/
trigger_delay.sv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* Delay module: basic
* Delays a trigger signal for a set number of target clock cycles
*/
module trigger_delay(
input rst,
input clk,
input trigger,
input clean_target_clock,
input [31:0] delay,
input set_delay,
output reg delayed_trigger
);
parameter TRIG_CYCLES=1;
// IDLE is waiting for the input trigger
// WAIT is triggered, counting down the delay time
// ACTIVE is holding the output trigger high
// FINISHED is waiting for input trigger to go low again
enum reg [2:0] { IDLE, WAIT, ACTIVE, FINISHED} state;
reg [31:0] delay_cycles;
reg [31:0] elapsed_cycles;
reg [4:0] triggered_cycles;
always @(posedge clk) begin
if (rst) begin
state <= IDLE;
delay_cycles <= 0;
end
else begin
if (set_delay) delay_cycles <= delay;
if (state == IDLE) begin
if (trigger) begin
state <= WAIT;
end
end
else if (state == WAIT) begin
if (elapsed_cycles >= delay_cycles) state <= ACTIVE;
end
else if (state == ACTIVE) begin
if (triggered_cycles >= TRIG_CYCLES) state <= FINISHED;
end
else if (state == FINISHED) begin
if (!trigger) state <= IDLE;
end
end
end
// Our state changes synchronize with the target clock
// This way we don't confuse the target device prematurely
always @(posedge clean_target_clock) begin
if (rst) begin
triggered_cycles <= 0;
delayed_trigger <= 0;
end
else begin
if (state == IDLE) begin
elapsed_cycles <= 0;
triggered_cycles <= 0;
end
else if (state == WAIT) begin
delayed_trigger <= 0;
elapsed_cycles <= elapsed_cycles + 1;
end
else if (state == ACTIVE) begin
delayed_trigger <= 1;
triggered_cycles <= triggered_cycles + 1;
end
else delayed_trigger <= 0;
end
end
endmodule