1 | // The crossroads |
---|
2 | // Originally written by R. P. Kurshan |
---|
3 | // Translated by Rajeev K. Ranjan |
---|
4 | |
---|
5 | typedef enum {no_cars, car_waiting, cars_passing} traffic_status; |
---|
6 | typedef enum {stop, go, slow} traffic_signal; |
---|
7 | typedef enum {go_slow, go_A, go_B} police_signal; |
---|
8 | typedef enum {STOPPED_init, STOPPED, GO_init, GO} car_status; |
---|
9 | typedef enum {go_A_init, go_A_state, go_B_init, go_B_state} police_state; |
---|
10 | |
---|
11 | |
---|
12 | module environment(clk, status_A, test); |
---|
13 | input clk; |
---|
14 | output status_A; |
---|
15 | output test; |
---|
16 | |
---|
17 | traffic_signal wire signal_A, signal_B; |
---|
18 | traffic_status wire status_A, status_B; |
---|
19 | police_signal wire signal; |
---|
20 | wire test; |
---|
21 | |
---|
22 | POLICEMAN police (clk, status_A, status_B, signal); |
---|
23 | |
---|
24 | assign signal_A = (signal == go_A) ? go : (signal == go_slow) ? slow : stop; |
---|
25 | assign signal_B = (signal == go_B) ? go : (signal == go_slow) ? slow : stop; |
---|
26 | |
---|
27 | |
---|
28 | ROAD road_A (clk, signal_A, status_A); |
---|
29 | ROAD road_B (clk, signal_B, status_B); |
---|
30 | assign test = ((status_A == cars_passing) && (status_B == cars_passing)) ? 0 :1; |
---|
31 | |
---|
32 | collision col(clk, status_A, status_B); |
---|
33 | starvation starv(clk,status_A); |
---|
34 | |
---|
35 | endmodule |
---|
36 | |
---|
37 | |
---|
38 | module POLICEMAN(clk, status_A, status_B, signal); |
---|
39 | input clk; |
---|
40 | input status_A, status_B; |
---|
41 | output signal; |
---|
42 | traffic_status wire status_A, status_B; |
---|
43 | police_signal wire signal; |
---|
44 | police_state reg state; |
---|
45 | wire r_state, ri_state; |
---|
46 | |
---|
47 | assign ri_state = $ND(0,1); |
---|
48 | initial |
---|
49 | begin |
---|
50 | case(ri_state) |
---|
51 | 0:state = go_A_init; |
---|
52 | 1:state = go_B_init; |
---|
53 | endcase |
---|
54 | end |
---|
55 | |
---|
56 | assign signal = ((state == go_A_init) || (state == go_B_init)) ? go_slow : |
---|
57 | (state == go_A_state) ? go_A : go_B; |
---|
58 | assign r_state = $ND(0,1); |
---|
59 | always @(posedge clk) begin |
---|
60 | case(state) |
---|
61 | go_A_init: begin |
---|
62 | case(r_state) |
---|
63 | 0:state = go_A_init; |
---|
64 | 1:state = go_A_state; |
---|
65 | endcase |
---|
66 | end |
---|
67 | go_B_init: begin |
---|
68 | case(r_state) |
---|
69 | 0:state = go_B_init; |
---|
70 | 1:state = go_B_state; |
---|
71 | endcase |
---|
72 | end |
---|
73 | // go_A_state: |
---|
74 | // if (status_B == car_waiting) |
---|
75 | // state = go_B_init; |
---|
76 | // go_B_state: |
---|
77 | // if (status_A == car_waiting) |
---|
78 | // state = go_A_init; |
---|
79 | |
---|
80 | default: |
---|
81 | begin |
---|
82 | if ((signal == go_A) && (status_B == car_waiting)) |
---|
83 | state = go_B_init; |
---|
84 | else |
---|
85 | if ((signal == go_B) && (status_A == car_waiting)) |
---|
86 | state = go_A_init; |
---|
87 | end |
---|
88 | endcase |
---|
89 | end |
---|
90 | endmodule |
---|
91 | |
---|
92 | |
---|
93 | module ROAD(clk, signal, status); |
---|
94 | input clk; |
---|
95 | input signal; |
---|
96 | output status; |
---|
97 | traffic_signal wire signal; |
---|
98 | traffic_status wire status; |
---|
99 | car_status reg state; |
---|
100 | wire r_state; |
---|
101 | |
---|
102 | initial state = STOPPED_init; |
---|
103 | |
---|
104 | assign status = (state == STOPPED_init) ? no_cars: |
---|
105 | (state == STOPPED) ? car_waiting : |
---|
106 | (state == GO_init) ? cars_passing: |
---|
107 | (state == GO) ? no_cars: no_cars; |
---|
108 | assign r_state = $ND(0,1); |
---|
109 | |
---|
110 | always @(posedge clk) begin |
---|
111 | case(state) |
---|
112 | STOPPED_init: |
---|
113 | begin |
---|
114 | case(r_state) |
---|
115 | 0:state = STOPPED_init; |
---|
116 | 1:state = STOPPED; |
---|
117 | endcase |
---|
118 | end |
---|
119 | |
---|
120 | STOPPED: |
---|
121 | begin |
---|
122 | if (signal == go) |
---|
123 | state = GO_init; |
---|
124 | end |
---|
125 | GO_init: |
---|
126 | begin |
---|
127 | if (signal == stop) |
---|
128 | state = STOPPED_init; |
---|
129 | else |
---|
130 | begin |
---|
131 | case(r_state) |
---|
132 | 0:state = GO_init; |
---|
133 | 1:state = GO; |
---|
134 | endcase |
---|
135 | end |
---|
136 | end |
---|
137 | GO: |
---|
138 | state = STOPPED_init; |
---|
139 | default: ; |
---|
140 | endcase |
---|
141 | end |
---|
142 | endmodule |
---|
143 | |
---|
144 | typedef enum {GOOD, BAD} status; |
---|
145 | |
---|
146 | module collision(clk, status_A, status_B); |
---|
147 | input clk, status_A, status_B; |
---|
148 | traffic_status wire status_A, status_B; |
---|
149 | status reg state; |
---|
150 | |
---|
151 | initial state = GOOD; |
---|
152 | always @(posedge clk) begin |
---|
153 | case(state) |
---|
154 | GOOD: |
---|
155 | if ((status_A == cars_passing) && (status_B == cars_passing)) |
---|
156 | state = BAD; |
---|
157 | endcase |
---|
158 | end |
---|
159 | endmodule |
---|
160 | |
---|
161 | |
---|
162 | typedef enum {OK, NOT_OK} prop1_status; |
---|
163 | |
---|
164 | module starvation(clk, stat); |
---|
165 | input clk, stat; |
---|
166 | traffic_status wire stat; |
---|
167 | prop1_status reg state; |
---|
168 | |
---|
169 | initial state = OK; |
---|
170 | always @(posedge clk) begin |
---|
171 | case(state) |
---|
172 | OK: |
---|
173 | begin |
---|
174 | if (stat == car_waiting) |
---|
175 | state = NOT_OK; |
---|
176 | end |
---|
177 | NOT_OK: |
---|
178 | begin |
---|
179 | if (stat == cars_passing) |
---|
180 | state = OK; |
---|
181 | end |
---|
182 | default:; |
---|
183 | |
---|
184 | endcase |
---|
185 | end |
---|
186 | endmodule |
---|
187 | |
---|