-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdummy_target_clk.sv
116 lines (95 loc) · 2.57 KB
/
dummy_target_clk.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* Simulated target device, vulnerable to a single clock upset
* This is never meant to be synthesized, only used in test benches.
*
* Contains a trap door, where a clock glitch within a certain time frame
* will make the soft reset not work - DESYNK should detect this as a timeout
* and perform a hard reset instead
*/
module dummy_target_clk (
input clk,
input rst, // Start of simulation, not really target reset
input soft_reset,
input power,
input throttle,
output reg ready,
output reg success,
/* Diagnostics about the trap door */
output reg trap_is_active,
output reg test_passed_trap,
output reg crashed
);
parameter READY_CYCLES = 15;
parameter READY_LEN = 5;
parameter VULN_CYCLES = 80; // Glitch at this time to win
parameter TRAP_CYCLES = 40; // Glitch at this time to spring the trap
int current_cycle;
realtime clk_high;
realtime regular_half_period;
realtime current_half_period;
always begin
@(posedge clk) if (!rst) clk_high = $realtime;
@(negedge clk) begin
current_half_period = $realtime - clk_high;
if (current_cycle == 2) begin
// This is what we'll base our measurements off of
if (regular_half_period == 0) regular_half_period = current_half_period;
end
end
end
always @(posedge clk) begin
if (rst) begin
clk_high = 0;
regular_half_period = 0;
current_half_period = 0;
current_cycle = 0;
ready <= 0;
success <= 0;
crashed <= 0;
trap_is_active <= 0;
test_passed_trap <= 0;
end
else begin
if (!crashed) begin
current_cycle = current_cycle + 1;
end
if (soft_reset && !trap_is_active) begin
current_cycle = 0;
crashed <= 0;
end
else if (!power && throttle) begin
current_cycle = 0;
crashed <= 0;
if (trap_is_active) begin
trap_is_active <= 0;
test_passed_trap <= 1;
end
end
else if (success) begin
/* Nothing left to do */
end
else begin
if (current_half_period < (0.8 * regular_half_period)) begin
// The glitch has come in
if (current_cycle == VULN_CYCLES) begin
success <= 1;
end
else if (current_cycle == TRAP_CYCLES) begin
trap_is_active = 1;
crashed <= 1;
end
else begin
crashed <= 1;
end
end
else begin
if (current_cycle >= READY_CYCLES && current_cycle < (READY_CYCLES+READY_LEN)) begin
ready <= 1;
end
else if (current_cycle >= READY_CYCLES+READY_LEN)
ready <= 0;
end
end
end
end
endmodule