1
+ module main (N_SENSE,S_SENSE,E_SENSE,N_GO,S_GO,E_GO);
2
+
3
+ /*
4
+ This program implements a traffic light controller.
5
+
6
+ Input signals:
7
+ N_SENSE -
8
+ When this goes high, a car from the north is waiting to cross the
9
+ intersection. (This signal does not have to remain high until
10
+ the request is granted.) Once the request has been granted, this
11
+ signal goes low to indicate that the car has finished crossing
12
+ the intersection.
13
+ S_SENSE - Same as N, except for traffic from the south.
14
+ E_SENSE - Same as N, except for traffic from the east.
15
+
16
+ Output signals:
17
+ N_GO - If this signal is high, traffic from the north is permitted to
18
+ enter the intersection. If it is low, traffic must wait.
19
+ S_GO - Same as N_GO, except for traffic from the south.
20
+ E_GO - Same as N_GO, except for traffic from the east.
21
+
22
+ Internal signals:
23
+ N_REQ - Since N is not required to remain high to indicate that a
24
+ car from the north wants to cross the intersection, this signal
25
+ is used to latch requests from the north.
26
+ S_REQ - Same as N_REQ, except for traffic from the south.
27
+ E_REQ - Same as N_REQ, except for traffic from the east.
28
+ NS_LOCK - This signal is set high by north/south traffic in order to
29
+ lock out traffic from the east.
30
+ EW_LOCK - This signal is set high by east traffic in order to
31
+ lock out traffic from the north/south.
32
+ */
33
+
34
+ input N_SENSE, S_SENSE, E_SENSE;
35
+ output N_GO, S_GO, E_GO;
36
+
37
+ wire N_SENSE, S_SENSE, E_SENSE;
38
+ reg N_GO, S_GO, E_GO;
39
+ reg NS_LOCK, EW_LOCK, N_REQ, S_REQ, E_REQ;
40
+
41
+ initial begin
42
+ N_REQ = 0 ; S_REQ = 0 ; E_REQ = 0 ;
43
+ N_GO = 0 ; S_GO = 0 ; E_GO = 0 ;
44
+ NS_LOCK = 0 ; EW_LOCK = 0 ;
45
+ end
46
+
47
+
48
+ /* latch traffic sensor inputs */
49
+
50
+ always begin if (! N_REQ & N_SENSE) N_REQ = 1 ; end
51
+ always begin if (! S_REQ & S_SENSE) S_REQ = 1 ; end
52
+ always begin if (! E_REQ & E_SENSE) E_REQ = 1 ; end
53
+
54
+ /* North traffic controller */
55
+
56
+ always begin
57
+ if (N_REQ)
58
+ begin
59
+ wait (! EW_LOCK & ! (S_GO & ! S_SENSE));
60
+ NS_LOCK = 1 ; N_GO = 1 ;
61
+ wait (! N_SENSE);
62
+ if (! S_GO | ! S_SENSE) NS_LOCK = 0 ;
63
+ N_GO = 0 ; N_REQ = 0 ;
64
+ end
65
+ end
66
+
67
+ /* South traffic controller */
68
+
69
+ always begin
70
+ if (S_REQ)
71
+ begin
72
+ wait (! EW_LOCK & ! (N_GO & ! N_SENSE));
73
+ NS_LOCK = 1 ; S_GO = 1 ;
74
+ wait (! S_SENSE);
75
+ if (! N_GO | ! N_SENSE) NS_LOCK = 0 ;
76
+ S_GO = 0 ; S_REQ = 0 ;
77
+ end
78
+ end
79
+
80
+ /* East traffic controller */
81
+
82
+ always begin
83
+ if (E_REQ)
84
+ begin
85
+ EW_LOCK = 1 ;
86
+ wait (! NS_LOCK);
87
+ E_GO = 1 ;
88
+ wait (! E_SENSE);
89
+ EW_LOCK = 0 ; E_GO = 0 ; E_REQ = 0 ;
90
+ end
91
+ end
92
+
93
+ /* specifications */
94
+
95
+ always begin
96
+ assert mutex: ! (E_GO & (S_GO | N_GO));
97
+ if (E_SENSE) assert E_live: eventually E_GO;
98
+ if (S_SENSE) assert S_live: eventually S_GO;
99
+ if (N_SENSE) assert N_live: eventually N_GO;
100
+ end
101
+
102
+ /* assumptions */
103
+
104
+ always begin
105
+ assert E_fair: eventually ! (E_GO & E_SENSE);
106
+ assert S_fair: eventually ! (S_GO & S_SENSE);
107
+ assert N_fair: eventually ! (N_GO & N_SENSE);
108
+ end
109
+
110
+ using E_fair, S_fair, N_fair prove E_live, S_live, N_live;
111
+ assume E_fair, S_fair, N_fair;
112
+
113
+
114
+ endmodule
0 commit comments