1 | `define elev 2 |
---|
2 | `define floor 3 |
---|
3 | `define width 2 |
---|
4 | typedef enum {UP,DOWN} dir; |
---|
5 | typedef enum {STOPPED,MOVING} mov; |
---|
6 | typedef enum {OPEN,OPENING,CLOSED,CLOSING} dr; |
---|
7 | typedef enum {ON,OFF} onoff; |
---|
8 | |
---|
9 | //*********************************************** |
---|
10 | module main(clk); |
---|
11 | input clk; |
---|
12 | wire[1:`elev] stop_next; |
---|
13 | wire[1:`elev] inc; |
---|
14 | wire[1:`elev] dec; |
---|
15 | wire[1:`elev] continue; |
---|
16 | wire[0:`width-1] init1, init2, init11, init22; |
---|
17 | |
---|
18 | assign init11=$ND(0,1,2,3); |
---|
19 | assign init22=$ND(0,1,2,3); |
---|
20 | assign init1 = init11==3?2:init11; |
---|
21 | assign init2 = init22==3?2:init22; |
---|
22 | |
---|
23 | elevator e1(clk,stop_next[1],inc[1],dec[1],continue[1],init1); |
---|
24 | elevator e2(clk,stop_next[2],inc[2],dec[2],continue[2],init2); |
---|
25 | main_control main_control(clk,inc,dec,stop_next,continue,init1,init2); |
---|
26 | endmodule |
---|
27 | //*********************************************** |
---|
28 | |
---|
29 | //************************************************ |
---|
30 | module main_control(clk,inc,dec,stop_next,continue,init1,init2); |
---|
31 | input clk,inc,dec,init1,init2; |
---|
32 | output stop_next,continue; |
---|
33 | wire[1:`elev] inc; |
---|
34 | wire[1:`elev] continue; |
---|
35 | wire[1:`elev] dec; |
---|
36 | wire[1:`elev] stop_next; |
---|
37 | reg [0:`width-1] locations[1:`elev]; |
---|
38 | onoff reg up_floor_buttons[0:`floor-1]; |
---|
39 | onoff reg down_floor_buttons[0:`floor-1]; |
---|
40 | wire[0:`floor-1] random_up; |
---|
41 | wire[0:`width-1] init1,init2; |
---|
42 | wire[0:`floor-1] random_down; |
---|
43 | wire[0:`floor-1] buttons; |
---|
44 | wire[1:`elev] button_above,button_below; |
---|
45 | dir reg direction[1:`elev]; |
---|
46 | initial begin |
---|
47 | locations[1]=init1; |
---|
48 | locations[2]=init2; |
---|
49 | up_floor_buttons[0]=OFF; |
---|
50 | up_floor_buttons[1]=OFF; |
---|
51 | up_floor_buttons[2]=OFF; |
---|
52 | down_floor_buttons[0]=OFF; |
---|
53 | down_floor_buttons[1]=OFF; |
---|
54 | down_floor_buttons[2]=OFF; |
---|
55 | direction[1]=UP; |
---|
56 | direction[2]=UP; |
---|
57 | end |
---|
58 | |
---|
59 | //compute if elevator should continue in same direction. We skip the next floor assuming |
---|
60 | // that the stop_next computation will take care of this. |
---|
61 | assign buttons[0] = up_floor_buttons[0]==ON || down_floor_buttons[0]==ON; |
---|
62 | assign buttons[1] = up_floor_buttons[1]==ON || down_floor_buttons[1]==ON; |
---|
63 | assign buttons[2] = up_floor_buttons[2]==ON || down_floor_buttons[2]==ON; |
---|
64 | assign button_below[1] = |
---|
65 | ((locations[1]==2)&&(buttons[0]||buttons[1])) |
---|
66 | || (locations[1]==1 && buttons[0]); |
---|
67 | assign button_above[1] = ((locations[1]==0)&&(buttons[2]||buttons[1])) |
---|
68 | || ((locations[1]==1)&&(buttons[2])); |
---|
69 | assign button_below[2] = |
---|
70 | ((locations[2]==2)&&(buttons[0]||buttons[1])) |
---|
71 | || (locations[2]==1 && buttons[0]); |
---|
72 | assign button_above[2] = ((locations[2]==0)&&(buttons[2]||buttons[1])) |
---|
73 | || ((locations[2]==1)&&(buttons[2])); |
---|
74 | assign continue[1] = button_above[1] && direction[1]==UP || button_below[1] && direction[1]==DOWN; |
---|
75 | assign continue[2] = button_above[2] && direction[2]==UP || button_below[2] && direction[2]==DOWN; |
---|
76 | |
---|
77 | //schedule the next pickup |
---|
78 | assign stop_next[1]=((locations[1] != `floor-1)&&(direction[1]==UP))? |
---|
79 | ((up_floor_buttons[locations[1]+1]==ON)?1:0): |
---|
80 | (((locations[1] != 0)&&(direction[1]==DOWN))? |
---|
81 | ((down_floor_buttons[locations[1]-1]==ON)?1:0):0); |
---|
82 | assign stop_next[2]=((locations[2] != `floor-1)&&(direction[2]==UP))? |
---|
83 | ((up_floor_buttons[locations[2]+1]==ON)?1:0): |
---|
84 | (((locations[2] != 0)&&(direction[2]==DOWN))? |
---|
85 | ((down_floor_buttons[locations[2]-1]==ON)?1:0):0); |
---|
86 | assign random_up[0] = $ND(0,1); |
---|
87 | assign random_down[0] = $ND(0,1); |
---|
88 | assign random_up[1] = $ND(0,1); |
---|
89 | assign random_down[1] = $ND(0,1); |
---|
90 | assign random_up[2] = $ND(0,1); |
---|
91 | assign random_down[2] = $ND(0,1); |
---|
92 | |
---|
93 | |
---|
94 | always@(posedge clk) begin |
---|
95 | // randomly push floor buttons |
---|
96 | for (i=0;i<=`floor-1;i=i+1)begin |
---|
97 | if (random_up[i]) up_floor_buttons[i]=ON; |
---|
98 | if (random_down[i]) down_floor_buttons[i]=ON; |
---|
99 | end |
---|
100 | |
---|
101 | //turn off scheduled floor buttons. |
---|
102 | // it is important to turn these off after the random pushes, since we |
---|
103 | // want the scheduled buttons to be OFF even though they may have been |
---|
104 | // randomly pushed. |
---|
105 | for (i=1;i<=`elev;i=i+1) begin |
---|
106 | if ((locations[i] != `floor-1)&& (direction[i] == UP)) begin |
---|
107 | if (up_floor_buttons[locations[i]+1]==ON) begin |
---|
108 | up_floor_buttons[locations[i]+1] = OFF; |
---|
109 | end |
---|
110 | end |
---|
111 | if ((locations[i] != 0)&& (direction[i] == DOWN)) begin |
---|
112 | if (down_floor_buttons[locations[i]-1]==ON) begin |
---|
113 | down_floor_buttons[locations[i]-1] = OFF; |
---|
114 | end |
---|
115 | end |
---|
116 | end |
---|
117 | end |
---|
118 | |
---|
119 | //keep track of locations and directions |
---|
120 | always@(posedge clk) begin |
---|
121 | for (i=1;i<=`elev;i=i+1) begin |
---|
122 | if (locations[i]==`floor-1) direction[i] = DOWN; |
---|
123 | if (locations[i]==0) direction[i]=UP; |
---|
124 | if(inc[i]) begin |
---|
125 | locations[i]=locations[i]+1; |
---|
126 | direction[i]=UP; |
---|
127 | end |
---|
128 | if(dec[i]) begin |
---|
129 | locations[i]=locations[i]-1; |
---|
130 | direction[i]=DOWN; |
---|
131 | end |
---|
132 | end |
---|
133 | end |
---|
134 | |
---|
135 | endmodule |
---|
136 | //*********************************************** |
---|
137 | |
---|
138 | //*********************************************** |
---|
139 | module elevator(clk,stop_next,inc,dec,continue,init); |
---|
140 | input clk,stop_next,continue,init; |
---|
141 | output inc,dec; |
---|
142 | onoff reg buttons[0:`floor-1]; |
---|
143 | wire [0:`width-1] init; |
---|
144 | reg[0:`width-1] location; |
---|
145 | dir reg direction; |
---|
146 | mov reg movement; |
---|
147 | dr reg door; |
---|
148 | reg open_next; |
---|
149 | wire button_above, button_below; |
---|
150 | |
---|
151 | //initial begin |
---|
152 | initial open_next = 0; |
---|
153 | initial location = init; |
---|
154 | initial direction = UP; |
---|
155 | initial door = OPEN; |
---|
156 | initial movement = STOPPED; |
---|
157 | initial buttons[0]=OFF; |
---|
158 | initial buttons[1]=OFF; |
---|
159 | initial buttons[2]=OFF; |
---|
160 | //end |
---|
161 | |
---|
162 | wire[0:`floor-1] random_push; |
---|
163 | wire button_above,button_below; |
---|
164 | assign random_push = $ND(0,1,2,3,4,5,6,7); |
---|
165 | assign button_below = |
---|
166 | ((location==2)&&(buttons[1]==ON||buttons[0]==ON)) || |
---|
167 | ((location==1)&&buttons[0]==ON); |
---|
168 | assign button_above = ((location==0)&&(buttons[2]==ON|| |
---|
169 | buttons[1]==ON)) || |
---|
170 | ((location==1)&&(buttons[2]==ON)); |
---|
171 | |
---|
172 | //****************************** |
---|
173 | always@(posedge clk) begin |
---|
174 | // randomly push buttons. |
---|
175 | // But when door is open turn button off for that floor. |
---|
176 | for (i=0;i<=`floor-1;i=i+1) begin |
---|
177 | if (i == location) buttons[i]=OFF; |
---|
178 | else if (random_push[i]) buttons[i]=ON; |
---|
179 | end |
---|
180 | |
---|
181 | // record a request to stop at the next floor |
---|
182 | // it is important that this happens last since we want to |
---|
183 | // insure that the stop_next request is always recorded by |
---|
184 | // pushing the button. |
---|
185 | if(stop_next) begin |
---|
186 | if (direction==UP) buttons[location+1]=ON; |
---|
187 | else buttons[location-1]=ON; |
---|
188 | end |
---|
189 | end |
---|
190 | //******************************* |
---|
191 | |
---|
192 | //******************************* |
---|
193 | always@(posedge clk) begin |
---|
194 | //schedule the door to open at the next floor |
---|
195 | if(door != CLOSED) open_next=0; |
---|
196 | else if (movement==MOVING&&(stop_next||(direction == UP&&buttons[location+1]==ON)|| |
---|
197 | (direction == DOWN&&buttons[location-1]==ON))) |
---|
198 | open_next=1; |
---|
199 | end |
---|
200 | //******************************* |
---|
201 | |
---|
202 | wire random; |
---|
203 | assign random = $ND(0,1); |
---|
204 | |
---|
205 | //******************************* |
---|
206 | always@(posedge clk) begin |
---|
207 | //Door operation: open the door if button[location] is on. |
---|
208 | //Random pause between different states. |
---|
209 | case (door) |
---|
210 | CLOSED: if (open_next&&movement==STOPPED) |
---|
211 | door=OPENING; |
---|
212 | OPENING: if (random) door = OPEN; |
---|
213 | OPEN: if (random) door = CLOSING; |
---|
214 | CLOSING: if (random) door = CLOSED; |
---|
215 | endcase |
---|
216 | end |
---|
217 | //******************************* |
---|
218 | |
---|
219 | // Move to next floor. Increase or decrease location when arrived. |
---|
220 | // Signal to main control (through inc or dec) that have arrived at next floor. |
---|
221 | wire stop_moving; |
---|
222 | wire start_moving; |
---|
223 | wire r_stop; |
---|
224 | assign start_moving = (continue || button_above&&direction==UP) || |
---|
225 | (button_below && direction == DOWN); |
---|
226 | assign r_stop = $ND(0,1); |
---|
227 | assign stop_moving = r_stop&&(movement == MOVING); |
---|
228 | assign inc = (stop_moving)&&(direction == UP); |
---|
229 | assign dec = (stop_moving)&&(direction == DOWN); |
---|
230 | //******************************* |
---|
231 | always@(posedge clk) begin |
---|
232 | if (door == CLOSED) begin |
---|
233 | case (movement) |
---|
234 | STOPPED: if (door==CLOSED&&start_moving&&!open_next) |
---|
235 | movement=MOVING; |
---|
236 | MOVING: if (stop_moving) begin |
---|
237 | movement=STOPPED; |
---|
238 | if (direction == UP) location = location+1; |
---|
239 | if (direction == DOWN) location = location-1; |
---|
240 | end |
---|
241 | endcase |
---|
242 | end |
---|
243 | end |
---|
244 | //******************************* |
---|
245 | |
---|
246 | // Determine direction of movement |
---|
247 | |
---|
248 | //******************************* |
---|
249 | always@(posedge clk) begin |
---|
250 | case (direction) |
---|
251 | UP: if((!button_above)&&!continue) |
---|
252 | direction = DOWN; |
---|
253 | DOWN: if((!button_below)&&!continue) |
---|
254 | direction = UP; |
---|
255 | endcase |
---|
256 | if(location==`floor-1) direction=DOWN; |
---|
257 | if(location==0) direction=UP; |
---|
258 | end |
---|
259 | //******************************* |
---|
260 | |
---|
261 | endmodule |
---|
262 | //*********************************************** |
---|