-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsmtb.sv
175 lines (152 loc) · 6.52 KB
/
fsmtb.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
`timescale 1s / 1ms
`include "defs.sv"
module fsmtb();
//default to 1Hz
parameter CLOCK_CYCLE = 2;
parameter CLOCK_WIDTH = CLOCK_CYCLE/2;
parameter IDLE_CLOCKS = 4;
bit Clock, Reset, S1, S2, S3;
logic [1:0] L1, L2, L3;
integer log;
int err_count;
int log_count;
color c;
fsm controller(Clock, Reset, S1, S2, S3, L1, L2, L3);
initial
begin
//open log file
log = $fopen("traffic.log");
$display(">>>>>Begin fsm testbench");
//debugging
`ifdef DEBUG_MON
$fdisplay(log, "\t\tS1\tS2\tS3\tL1\tL2\tL3\tClock\tReset");
$fmonitor(log, "\t\t%b\t%b\t%b\t%b\t%b\t%b\t%b\t%b\t", S1, S2, S3, L1, L2, L3, Clock, Reset);
`endif
end
//free running clock
initial
begin
Clock = TRUE;
forever #CLOCK_WIDTH Clock = ~Clock;
end
initial
begin
Reset = FALSE;
repeat (IDLE_CLOCKS) @(negedge Clock);
Reset = TRUE;
end
//----------------------------------------------------
// Main Tests
//
// ===TESTS===
// 1. ALL SENSORS ON THEN ALL SENSORS OFF
// 2. RESET HIGH THEN LOW
// 3. CONFIRM 4TH STAYS GREEN WHEN NO HARRISON SENSOR
// 4. CONFIRM HARRISON TURNS RED EVEN IF NO 4TH SENSOR
//----------------------------------------------------
initial
begin
repeat (1) @(negedge Clock); // Reset false, sensors all 0
repeat (1) @(negedge Clock); {S1,S2,S3} = 3'b111; // Reset true, turn on sensors
//----------------------------------------
// ALL SENSORS
//----------------------------------------
//1 - 4th
repeat (4) @(negedge Clock); log_err(RED, RED, RED); $fdisplay(log, "ALL SENSORS HIGH");
repeat (45) @(negedge Clock); log_err(GREEN, RED, RED);
repeat (1) @(negedge Clock); log_err(YELLOW, RED, RED);
repeat (4) @(negedge Clock); log_err(YELLOW, RED, RED);
repeat (1) @(negedge Clock); log_err(RED, RED, RED);
//5 - Harrison
repeat (1) @(negedge Clock); log_err(RED, GREEN, GREEN);
repeat (14) @(negedge Clock); log_err(RED, GREEN, GREEN);
repeat (1) @(negedge Clock); log_err(RED, YELLOW, YELLOW);
repeat (4) @(negedge Clock); log_err(RED, YELLOW, YELLOW);
repeat (1) @(negedge Clock); log_err(RED, RED, RED);
//10 - 4th
repeat (1) @(negedge Clock); log_err(GREEN, RED, RED);
repeat (44) @(negedge Clock); log_err(GREEN, RED, RED);
repeat (1) @(negedge Clock); log_err(YELLOW, RED, RED);
repeat (4) @(negedge Clock); log_err(YELLOW, RED, RED);
repeat (1) @(negedge Clock); log_err(RED, RED, RED);
//15 - Harrison
repeat (1) @(negedge Clock); log_err(RED, GREEN, GREEN);
repeat (14) @(negedge Clock); log_err(RED, GREEN, GREEN);
repeat (1) @(negedge Clock); log_err(RED, YELLOW, YELLOW);{S1,S2,S3} = 3'b000; //sensors off
repeat (4) @(negedge Clock); log_err(RED, YELLOW, YELLOW);
repeat (1) @(negedge Clock); log_err(RED, RED, RED);
//20 - 4th
repeat (1) @(negedge Clock); log_err(GREEN, RED, RED); $fdisplay(log, "ALL SENSORS LOW");
repeat (44) @(negedge Clock); log_err(GREEN, RED, RED);
repeat (1) @(negedge Clock); log_err(YELLOW, RED, RED);
repeat (4) @(negedge Clock); log_err(YELLOW, RED, RED);
repeat (1) @(negedge Clock); log_err(RED, RED, RED);
//25 - Harrison
repeat (1) @(negedge Clock); log_err(RED, GREEN, GREEN);
repeat (14) @(negedge Clock); log_err(RED, GREEN, GREEN);
repeat (1) @(negedge Clock); log_err(RED, YELLOW, YELLOW);
repeat (4) @(negedge Clock); log_err(RED, YELLOW, YELLOW);
repeat (1) @(negedge Clock); log_err(RED, RED, RED);
//30 - 4th
repeat (1) @(negedge Clock); log_err(GREEN, RED, RED);
repeat (44) @(negedge Clock); log_err(GREEN, RED, RED);
repeat (1) @(negedge Clock); log_err(YELLOW, RED, RED);
repeat (4) @(negedge Clock); log_err(YELLOW, RED, RED);
repeat (1) @(negedge Clock); log_err(RED, RED, RED);
//35 - Harrison
repeat (1) @(negedge Clock); log_err(RED, GREEN, GREEN);
repeat (14) @(negedge Clock); log_err(RED, GREEN, GREEN);
repeat (1) @(negedge Clock); log_err(RED, YELLOW, YELLOW);
repeat (4) @(negedge Clock); log_err(RED, YELLOW, YELLOW);
repeat (1) @(negedge Clock); log_err(RED, RED, RED);
//----------------------------------------
// RESET - LOW
//----------------------------------------
repeat (1) @(negedge Clock); Reset = FALSE; $fdisplay(log, "RESET LOW");
repeat (1) @(negedge Clock); log_err(FLASH, FLASH, FLASH);
repeat (10) @(negedge Clock); log_err(FLASH, FLASH, FLASH);
//----------------------------------------
// RESET - HIGH
//----------------------------------------
repeat (1) @(negedge Clock); Reset = TRUE; $fdisplay(log, "RESET HIGH");
repeat (2) @(negedge Clock); log_err(RED, RED, RED);
//----------------------------------------
// 4TH STAYS GREEN WHEN NO HARRISON SENSOR
//----------------------------------------
repeat (1) @(negedge Clock); log_err(GREEN, RED, RED); {S1,S2,S3} = 3'b100; $fdisplay(log, "4th MUST STAY GREEN");
repeat (46) @(negedge Clock); log_err(GREEN, RED, RED);
repeat (20) @(negedge Clock); log_err(GREEN, RED, RED);
repeat (20) @(negedge Clock); log_err(GREEN, RED, RED);
repeat (20) @(negedge Clock); log_err(GREEN, RED, RED);
repeat (20) @(negedge Clock); log_err(GREEN, RED, RED);
repeat (20) @(negedge Clock); log_err(GREEN, RED, RED);
//----------------------------------------
// HARRISON TURNS RED EVEN IF NO 4TH SENSOR
//----------------------------------------
repeat (1) @(negedge Clock); log_err(GREEN, RED, RED); {S1,S2,S3} = 3'b011; $fdisplay(log, "HARRISON MUST TURN RED");
repeat (5) @(negedge Clock); log_err(YELLOW, RED, RED);
repeat (1) @(negedge Clock); log_err(RED, RED, RED);
repeat (1) @(negedge Clock); log_err(RED, GREEN, GREEN);
repeat (14) @(negedge Clock); log_err(RED, GREEN, GREEN);
repeat (1) @(negedge Clock); log_err(RED, YELLOW, YELLOW);
repeat (4) @(negedge Clock); log_err(RED, YELLOW, YELLOW);
repeat (1) @(negedge Clock); log_err(RED, RED, RED);$fdisplay(log, "END");
$fclose(log);
$display(">>>>>There were %d errors.", err_count);
$display(">>>>>End fsm testbench");
$stop;
end
function automatic void log_err(
input bit [1:0] EL1,
input bit [1:0] EL2,
input bit [1:0] EL3);
log_count++;
if(L1 !== EL1 || L2 !== EL2 || L3 !== EL3)
begin
err_count++;
$fdisplay(log, "ERR%d: expected %b %b %b got %b %b %b", err_count, EL1, EL2, EL3, L1, L2, L3);
end
else
$fdisplay(log, "check count: %d", log_count);
endfunction
endmodule