You are on page 1of 14

1.

module lift2(clk,

//1KHz

2.

reset,

//reset signal,low(0) active

3.

//maintain,

4.

up1,

5.

up2, //2nd floor ,request for up,high active

6.

up3,

//3rd floor,request for up,high active

7.

//up4,

//4th floor ,request for up,high active

8.

//up5,

//5th floor ,request for up,high active

9.

down2,

//2th floor ,request for down,high active

10.

down3,

//3th floor ,request for down,high active

11.

down4,

//4th floor ,request for down,high active

//maintain signal,low(0) active


//1st floor ,request for up,high active

12.
13.

inter_button1, //inside of the lift,request for 1st floor,high active

14.

inter_button2, //inside of the lift,request for 2nd floor,high active

15.

inter_button3, //inside of the lift,request for 3rd floor,high active

16.

inter_button4, //inside of the lift,request for 4th floor,high active

17.
18.

close_door,

//the signal for close the lift'door,high active

19.

open_door,

//the signal for open the lift'door,high active

20.

floor,

//the signal for the lift arriving at the floor,high active

21.
22.

led_up1,

//output

23.

led_up2,

//output

24.

led_up3,

//output

25.
26.

led_down2,

//output

27.

led_down3,

//output

28.

led_down4,

//output

29.

inter1,

//output

30.

inter2,

//output

31.

inter3,

//output

32.

inter4,

//output

33.

status,

34.

door,

35.

number,

36.

r_up1,r_up2,r_up3,r_down2,r_down3,r_down4,

37.
38.

//output 1--operate 0--stop


//output 0--close,1--open
//output

r_inter1,r_inter2,r_inter3,r_inter4
);

39. input clk,reset/*,maintain*/;


40. input up1,up2,up3,down2,down3,down4;
41. input inter_button1,inter_button2,inter_button3,inter_button4;
42. input close_door,open_door;
43. input floor;
44.
45. output led_up1,led_up2,led_up3;
46. output led_down2, led_down3, led_down4;
47. output inter1,inter2,inter3,inter4;
48. output door;

//0--close,1--open

49. output [1:0]number;


50. output status;
51. output [1:0]r_up1,r_up2,r_up3,r_down2,r_down3,r_down4, r_inter1,r_inter2,r_inter3,r_inter4;
52.
53. reg led_up1,led_up2,led_up3;
54. reg led_down2, led_down3, led_down4;
55. reg inter1,inter2,inter3,inter4;
56. reg door;
57.
58. reg [1:0]direction;

//11--up,00--down

59. reg [1:0]cnt;

//count for the floor

60. reg [2:0]cnt_time; //count the time for the door


61. reg status;

//operate--1,stop--0

62.
63. reg [1:0]r_up1,r_up2,r_up3,r_down2,r_down3,r_down4;
64. reg [1:0]r_inter1,r_inter2,r_inter3,r_inter4;
65.
66. parameter f1=2'd0,//1st floor
67.

f2=2'd1,//2nd floor

68.

f3=2'd2,//3rd floor

69.

f4=2'd3;//4th floor

70.
71. parameter s1=2'd0,//1st floor
72.

s2=2'd1,//2nd floor

73.

s3=2'd2,//3rd floor

74.

s4=2'd3;//4th floor

75. wire t1=( r_up1!=f1 | r_inter1!=f1 );


76.
77. //-----------------judge the lift's storey----------------//
78. always @(posedge clk or negedge reset)
79. begin
80.
81.

if (!reset )
cnt<=f1;

82.

else if (status==0)

83.

cnt<=cnt;

84.

else if (status==1'b1 &&floor==1'b1)

85.

begin

86.

if (direction==2'b11)

87.

cnt<=cnt+1;

88.

else if (direction==0)

89.

cnt<=cnt-1;

90.

else cnt<=cnt;

91.
92.

end
else cnt<=cnt;

93. end
94.
95. assign number=cnt;
96.
97. //-----------------the lift's door------------------------//
98. always @(posedge clk or negedge reset )
99. begin
100.

if(!reset )

101.

door<=1'b0;

102.

else if(status==1'b1) //the lift is operating.

103.

door<=1'b0;

104.

else if(close_door && (status==0 &&floor==1) )

105.

door<=1'b0;

106.

else if(cnt_time==3'd5 && (status==0 &&floor==1) )

107.

door<=1'b0;

108.

else if( cnt==f1 && s1==0)

109.

door<=1'b0;

110.

else if (status==0 && floor==1 /*&& cnt_time==3'd0*/) //the lift is stopping.

111.
112.

door<=1'b1;
else if (open_door&&(status==1'b0 &&floor==1'b1))

113.

door<=1'b1;

114.

else door<=door;

115. end
116.

117. always @(posedge clk or negedge reset /*or posedge door*/)


118. begin
119.

if(!reset)

120.

cnt_time<=3'd0;

121.

else if (door==1'b1)

122.
123.

cnt_time<=cnt_time+1;
else cnt_time<=3'd0;

124. end
125.
126. //------------------the lift control---------------------//
127. always @(posedge clk or negedge reset)
128. begin
129.

if(!reset )

130.

begin

131.

led_up1<=1'b0;

132.

r_up1<=2'b11;

133.

end

134.

else if(up1)

135.

begin

136.

r_up1<=f1;

137.

led_up1<=1'b1;

138.

end

139.

else if(cnt==s1)

140.

begin

141. led_up1<=1'b0;
142. r_up1<=2'b11;
143. end
144. end
145.

//the lift's request ---- the 1st floor

146. always @(posedge clk or negedge reset)

//the lift's request ----the 2nd floor

147. begin
148.

if(!reset )

149.

begin

150.

led_up2<=1'b0;

151.

r_up2<=2'b00;

152.

end

153.

else if(up2)

154.

begin

155.

r_up2<=f2;

156.

led_up2<=1'b1;

157.

end

158.

else if(cnt==s2)

159.

begin

160. led_up2<=1'b0;
161. r_up2<=2'b00;
162. end
163.
164. end
165.
166. always @(posedge clk or negedge reset)
167. begin
168.

if(!reset )

169.

begin

170.

led_up3<=1'b0;

171.

r_up3<=2'b00;

172.

end

173.

else if (up3)

174.

begin

//the lift's request

175.

r_up3<=f3;

176.

led_up3<=1'b1;

177.

end

178.

else if (cnt==s3)

179.

begin

180. led_up3<=1'b0;
181. r_up3<=2'b00;
182. end
183. end
184.
185. always @(posedge clk or negedge reset)
186. begin
187.

if(!reset )

188.

begin

189.

led_down2<=1'b0;

190.

r_down2<=2'b00;

191.
192.

end
else if(down2)

193.

begin

194.

r_down2<=f2;

195.

led_down2<=1'b1;

196.

end

197.

else if(cnt==s2)

198.

begin

199. led_down2<=1'b0;
200. r_down2<=2'b00;
201. end
202. end
203.

//the lift's request

204. always @(posedge clk or negedge reset)

//the lift's request

205. begin
206.

if(!reset )

207.

begin

208.

led_down3<=1'b0;

209.

r_down3<=2'b00;

210.
211.

end
else if(down3)

212.

begin

213.

r_down3<=f3;

214.

led_down3<=1'b1;

215.

end

216.

else if(cnt==s3)

217.

begin

218. led_down3<=1'b0;
219. r_down3<=2'b00;
220. end
221. end
222.
223. always @(posedge clk or negedge reset)
224. begin
225.

if(!reset )

226.

begin

227.

led_down4<=1'b0;

228.

r_down4<=2'b00;

229.
230.
231.
232.

end
else if(down4)
begin
r_down4<=f4;

//the lift's request

233.

led_down4<=1'b1;

234.

end

235.

else if(cnt==s4)

236.

begin

237. led_down4<=1'b0;
238. r_down4<=2'b00;
239. end
240. end
241.
242. always @(posedge clk or negedge reset)

//the lift's request

243. begin
244.

if(!reset )

245.

begin

246.

inter1<=1'b0;

247.

r_inter1<=2'b11;

248.
249.

end
else if(inter_button1)

250.

begin

251.

inter1<=1'b1;

252.

r_inter1<=f1;

253.

end

254.

else if(cnt==s1)

255.

begin

256.

inter1<=1'b0;

257.

r_inter1<=2'b11;

258. end
259. end
260.
261. always @(posedge clk or negedge reset)

//the lift's request

262. begin
263.

if(!reset )

264.

begin

265.

inter2<=1'b0;

266.

r_inter2<=2'b00;

267.
268.

end
else if(inter_button2)

269.

begin

270.

inter2<=1'b1;

271.

r_inter2<=f2;

272.

end

273.

else if(cnt==s2)

274.

begin

275.

inter2<=1'b0;

276.

r_inter2<=2'b00;

277. end
278. end
279.
280. always @(posedge clk or negedge reset)
281. begin
282.

if(!reset )

283.

begin

284.

inter3<=1'b0;

285.

r_inter3<=2'b00;

286.
287.

end
else if(inter_button3)

288.

begin

289.

inter3<=1'b1;

290.

r_inter3<=f3;

//the lift's request

291.

end

292.

else if(cnt==s3)

293.

begin

294.

inter3<=1'b0;

295.

r_inter3<=2'b00;

296. end
297. end
298.
299. always @(posedge clk or negedge reset)

//the lift's request

300. begin
301.

if(!reset )

302.

begin

303.

inter4<=1'b0;

304.

r_inter4<=2'b00;

305.

end

306.

else if(inter_button4)

307.

begin

308.

inter4<=1'b1;

309.

r_inter4<=f4;

310.

end

311.

else if(cnt==s4)

312.

begin

313.

inter4<=1'b0;

314.

r_inter4<=2'b00;

315. end
316. end
317.
318. wire tt = (r_up1==f1|r_up2==f2|r_up3==f3|r_down2==f2|r_down3==f3|r_down4==f4|r_inter2==f2|r_inter3==f3|r_inter4==f4)
;

319.
320. always @ (posedge clk or negedge reset)
321. begin
322. if (!reset)
323. direction<=2'b10;
324. else
325. begin
326. case(cnt)
327. s1:begin
328.

if (r_up1==f1|r_up2==f2|r_up3==f3|r_down2==f2|r_down3==f3|r_down4==f4|r_inter2==f2|r_inter3==f3|r_inter4==f4)

329. direction<=2'b11;
330. else direction<=2'b11;
331.

end

332. s2:begin
333.

if(direction==2'b11)

334.

begin

335.

if ( r_up2==f2 | r_up3==f3 | r_down4==f4 | r_down3==f3 | r_inter3==f3 | r_inter4==f4 )

336.

direction<=2'b11;

337.

else if (r_up1==f1 | r_down2==f2 | r_inter1==f1 )

338.

direction<=2'b00;

339.

end

340.

else if(direction==2'b00)

341.

begin

342.
343.
344.

if(r_up1==f1|r_down2==f2|r_inter1==f1)
direction<=2'b00;
else if (r_up2==f2 | r_up3==f3 | r_down4==f4 | r_down3==f3 | r_inter3==f3 | r_inter4==f4 )

345.

direction<=2'b11;

346.

end

347.

else direction<=2'b10;

348.

end

349. s3:begin
350.

if(direction==2'b11)

351.

begin

352.

if( r_inter4==f4 | r_up3==f3 | r_down4==f4 )

353.

direction<=2'b11 ;

354.

else if(r_inter1==f1 | r_inter2==f2)

355.

direction<=2'b00 ;

356.

else if(r_down2==f2|r_up2==f2)

357.

direction<=2'b00 ;

358.

else if(r_up1==f1)

359.

direction<=2'b00 ;

360. end
361.
362.

else if(direction==2'b00)

363.

begin

364.

if ( r_up1==f1 | r_up2==f2 | r_down2==f2 | r_down3==f3 | r_inter1==f1 | r_inter2==f2 )

365.

direction<=0;

366.

else if ( r_up3==f3 | r_down4==f4 | r_inter4==f4 )

367.

direction<=2'b11;

368. end
369.

else direction<=2'b10;

370.

end

371. s4:begin
372.
if( r_up1==f1 | r_up2==f2 | r_up3==f3 | r_down2==f2 | r_down3==f3 | r_down4==f4 | r_inter1==f1 | r_inter2==f2 | r_inter3==f
3)
373.

direction<=2'b00;

374.

else direction<=2'b10;

375.

end

376. default:direction<=2'b10;
377. endcase
378. end
379. end
380.
381. always @( posedge clk or negedge reset)
382.
383.

begin
if (!reset)

384.
385.

status<=1'b0;
else if ( (floor==0) && (r_up1==f1|r_up2==f2|r_up3==f3|r_down2==f2|r_down3==f3|r_down4==f4|r_inter1==f1|r_inter2==
f2|r_inter3==f3|r_inter4==f4))

386.

status<=1'b1;

387.

else if (floor &&(r_up1!=f1|r_up2!=f2|r_up3!=f3|r_down2!=f2|r_down3!=f3|r_down4!=f4|r_inter1!=f1|r_inter2!=f2|r_inter3!=f


3|r_inter4!=f4))

388.

status<=1'b0;

389.

else status<=status;

390.

end

391.
392. endmodule