-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq2.v
78 lines (78 loc) · 1.74 KB
/
q2.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
module tes_fsm (speed_limit,car_speed,leading_distance,clk,rst,unlock_doors,accelerate_car);
parameter MIN_DISTANCE=7'd40;
parameter STOP=2'b00;
parameter DECELERATE=2'b01;
parameter ACCELERATE=2'b10;
input [7:0] speed_limit,car_speed;
input [6:0] leading_distance;
input clk,rst;
output unlock_doors,accelerate_car;
reg unlock_doors_tmp,accelerate_car_tmp;
reg [1:0] cs,ns;
always @(posedge clk or posedge rst) begin
if(rst) begin
cs<=STOP;
end
else begin
cs<=ns;
end
end
always @(cs or speed_limit or car_speed or leading_distance) begin
case(cs)
STOP:begin
if(leading_distance<MIN_DISTANCE) begin
ns=STOP;
end
else begin
ns=ACCELERATE;
end
end
DECELERATE:begin
if((leading_distance<MIN_DISTANCE)||(car_speed>speed_limit)) begin
ns=DECELERATE;
end
else if(car_speed==0) begin
ns=STOP;
end
else
begin
ns=ACCELERATE;
end
end
ACCELERATE:begin
if((leading_distance>=MIN_DISTANCE)&&(car_speed<speed_limit)) begin
ns=ACCELERATE;
end
else
begin
ns=DECELERATE;
end
end
default:begin
ns=STOP;
end
endcase
end
always@(*) begin
case(cs)
STOP:begin
unlock_doors_tmp=1;
accelerate_car_tmp=0;
end
DECELERATE:begin
unlock_doors_tmp=0;
accelerate_car_tmp=0;
end
ACCELERATE:begin
unlock_doors_tmp=0;
accelerate_car_tmp=1;
end
default:begin
unlock_doors_tmp=1;
accelerate_car_tmp=0;
end
endcase
end
assign unlock_doors=unlock_doors_tmp;
assign accelerate_car=accelerate_car_tmp;
endmodule