-
Notifications
You must be signed in to change notification settings - Fork 0
/
traffic_light.v
220 lines (202 loc) · 3.73 KB
/
traffic_light.v
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
module traffic_light (TA,TB,A,B,R,Clk,A1,A2,B1,B2,A_light,B_light);
input TA,TB,A,B,R,Clk;
output [0:3] A1;
output [0:3] A2;
output [0:3] B1;
output [0:3] B2;
wire [0:3] Q1;
wire [0:3] Q2;
wire [0:6] F;
wire [0:6] F5A;
wire [0:6] F5B;
output A_light,B_light;
reg [0:2] x;
reg clear1;
reg clear2;
reg cnt1;
reg cnt2;
reg T1,T2;
reg clearF;
reg clear5FA;
reg clear5FB;
//main counter
counter Acount1 (clear1,cnt1,Q1,Clk);
counter Acount2 (clear2,cnt2&Q1[0]&Q1[3],Q2,Clk);
//counter 125s
Counter125 count125 ((F[0]&F[1]&F[2]&F[3]&F[4]&F[6])|clearF,1,F,Clk);
Counter125 count5FA ((F5A[4]&F5A[6])|clear5FA,1,F5A,Clk);
Counter125 count5FB ((F5B[4]&F5B[6])|clear5FB,1,F5B,Clk);
//light of A and B
//T_FF tffA (T1,1,Clk);
//T_FF tffB (T2,1,Clk);
mux4x1_bh muxA (1,0,0,0,T1,x,A_light);
mux4x1_bh muxB (0,0,1,0,T2,x,B_light);
Mux4_2 light_A1 (0,0,0,0,Q1[0],Q1[1],Q1[2],Q1[3],A_light&(~x[0]),A1[0],A1[1],A1[2],A1[3]);
Mux4_2 light_A2 (0,0,0,0,Q2[0],Q2[1],Q2[2],Q2[3],A_light&(~x[0]),A2[0],A2[1],A2[2],A2[3]);
Mux4_2 light_B1 (0,0,0,0,Q1[0],Q1[1],Q1[2],Q1[3],B_light&(~x[0]),B1[0],B1[1],B1[2],B1[3]);
Mux4_2 light_B2 (0,0,0,0,Q2[0],Q2[1],Q2[2],Q2[3],B_light&(~x[0]),B2[0],B2[1],B2[2],B2[3]);
//for police
always @ (*)
begin
if(A)
begin
clear1=1;
clear2=1;
cnt1=0;
cnt2=0;
x[1]=0;
x[0]=0;
x[2]=0;
end
end
always @ (*)
begin
if(B)
begin
clear1=1;
clear2=1;
cnt1=0;
cnt2=0;
x[0]=0;
x[1]=1;
x[2]=0;
end
end
always @ (*)
begin
if(R&(A|B))
begin
x[0]=0;
x[1]=0;
x[2]=0;
cnt1=1;
cnt2=1;
clear1=0;
clear2=0;
end
end
always @ (*)
begin
if((~A)&(~B))
begin
cnt1=1;
cnt2=1;
clear1=0;
clear2=0;
end
end
//reset counter125
always @ (*)
begin
if(TA|TB) clearF=1;
else clearF=0;
end
// go to flasher
always @ (*)
begin
if(F[0]&F[1]&F[2]&F[3]&F[4]&F[6])
begin
x[0]=1;
x[1]=0;
x[2]=0;
clearF=1;
cnt1=0;
cnt2=0;
end
end
//reset counter5
always @ (*)
begin
if(x[0]&(~TA)) clear5FA = 1;
else clear5FA=0;
end
always @ (*)
begin
if(x[0]&(~TB)) clear5FB = 1;
else clear5FB=0;
end
//Exit from counter5
always @ (*)
begin
if(x[0]&F5A[6]&F5A[4])
begin
x[0]=0;
x[1]=0;
x[2]=0;
cnt1=1;
cnt2=1;
clear5FA=1;
end
if(x[0]&F5B[6]&F5B[4])
begin
x[0]=0;
x[1]=0;
x[2]=0;
cnt1=1;
cnt2=1;
clear5FB=1;
end
end
//for change color of light
//000 light A is green
//001 both are red
//010 B is green
//011 both are red
//100 flasher
always @(*)
case(x)
3'b000 : if(Q2[0]& Q2[3])
begin
x[2] = 1;
clear1 = 1;
clear2 = 1;
end
3'b001 : if(Q1[1]& Q1[3])
begin
x[1] = 1;
x[2] = 0;
clear1 = 1;
clear2 = 1;
end
3'b010 : if(Q2[2]& Q2[3])
begin
x[1] = 1;
x[2] = 1;
clear1 = 1;
clear2 = 1;
end
3'b011 : if(Q1[1]& Q1[3])
begin
x[1] = 0;
x[2] = 0;
x[0] = 0;
clear1 = 1;
clear2 = 1;
end
endcase
//this is for if we are not in flasher mode and output of first counter is 0 change clear to 0
always @(*)
if(!Q1[0]&!Q1[1]&!Q1[2]&!Q1[3]& ~x[0])
begin
clear1 = 0;
clear2 = 0;
end
//this is for if counter1 is 9 reset that
always @(*)
if(Q1[0]&Q1[3])
clear1=1;
//this is for if we are not in flasher mode make t1 and t2 0 and else toggle it for flasher time it is my mux input for alight and b light
always @(*)
begin
if(~x[0])
begin
T1=0;
T2=0;
end
end
always @(posedge Clk)
begin
T1=~T1;
T2=~T2;
end
endmodule