星期一, 3月 19, 2007
星期一, 3月 12, 2007
3月12日 紅綠燈挑戰
今日實做程式碼
module top;
reg clk,reset;
wire [1:0]state,speed;
wire [2:0]count;
wire stop;
GYR G1(clk,reset,state,stop,speed,count);
initial
begin
#0 begin reset=1;clk=0; end
#10 reset=0;
#10 clk=1;
repeat(30) #5 clk=~clk;
end
initial
#400 $finish;
endmodule
module GYR(clk,reset,state,stop,speed,count);
parameter GREEN=2'b00,YELLOW=2'b01,RED=2'b10;
input clk,reset;
output [1:0]state,speed;
output [2:0]count;
output stop;
reg [1:0]state,speed,states;
reg [2:0]count;
reg stop;
always@(posedge clk or reset)
begin
if(reset)
begin
states=GREEN;
count=3'b000;
state=0;
stop=0;
speed=2'b11;
end
else
begin
if(states==GREEN && count==3'b000)
begin
state=GREEN;
stop=0;
speed=2'b11;
count=3'b000;
states=YELLOW;
end
else if(states==YELLOW && count==3'b000)
begin
state=YELLOW;
stop=1;
speed=2'b01;
count=3'b000;
states=RED;
end
else if(states==RED && count==3'b000)
begin
state=RED;
stop=1;
speed=2'b00;
count=3'b001;
states=GREEN;
end
else if(states==GREEN && count==3'b001)
begin
state=GREEN;
stop=0;
speed=2'b11;
count=3'b011;
states=YELLOW;
end
else if(states==YELLOW && count==3'b011)
begin
state=YELLOW;
stop=1;
speed=2'b01;
count=3'b011;
states=RED;
end
else if(states==RED && count==3'b011)
begin
state=RED;
stop=1;
speed=2'b00;
count=3'b100;
states=GREEN;
end
else if(states==GREEN && count==3'b100)
begin
state=GREEN;
stop=0;
speed=2'b11;
count=3'b000;
states=YELLOW;
end
end
end
endmodule
模擬結果圖
結果正確成功嚕^^
module top;
reg clk,reset;
wire [1:0]state,speed;
wire [2:0]count;
wire stop;
GYR G1(clk,reset,state,stop,speed,count);
initial
begin
#0 begin reset=1;clk=0; end
#10 reset=0;
#10 clk=1;
repeat(30) #5 clk=~clk;
end
initial
#400 $finish;
endmodule
module GYR(clk,reset,state,stop,speed,count);
parameter GREEN=2'b00,YELLOW=2'b01,RED=2'b10;
input clk,reset;
output [1:0]state,speed;
output [2:0]count;
output stop;
reg [1:0]state,speed,states;
reg [2:0]count;
reg stop;
always@(posedge clk or reset)
begin
if(reset)
begin
states=GREEN;
count=3'b000;
state=0;
stop=0;
speed=2'b11;
end
else
begin
if(states==GREEN && count==3'b000)
begin
state=GREEN;
stop=0;
speed=2'b11;
count=3'b000;
states=YELLOW;
end
else if(states==YELLOW && count==3'b000)
begin
state=YELLOW;
stop=1;
speed=2'b01;
count=3'b000;
states=RED;
end
else if(states==RED && count==3'b000)
begin
state=RED;
stop=1;
speed=2'b00;
count=3'b001;
states=GREEN;
end
else if(states==GREEN && count==3'b001)
begin
state=GREEN;
stop=0;
speed=2'b11;
count=3'b011;
states=YELLOW;
end
else if(states==YELLOW && count==3'b011)
begin
state=YELLOW;
stop=1;
speed=2'b01;
count=3'b011;
states=RED;
end
else if(states==RED && count==3'b011)
begin
state=RED;
stop=1;
speed=2'b00;
count=3'b100;
states=GREEN;
end
else if(states==GREEN && count==3'b100)
begin
state=GREEN;
stop=0;
speed=2'b11;
count=3'b000;
states=YELLOW;
end
end
end
endmodule
模擬結果圖
結果正確成功嚕^^