目录
Exams/ece241 2013 q4
题目见网页图片。
module top_module (
input clk,
input reset,
input [3:1] s,
output fr3,
output fr2,
output fr1,
output dfr
);
parameter nf0=0, nf1=1, nf2=2, nf3=3; //四个状态,根据表格从上到下
reg [1:0] state, next; //现在的状态state和下一个状态next
always @(*) begin
case (s) //根据输入确定next
3'b000: next=nf3; //全零意味着一个都没有起作用,水位低于S1
3'b001: next=nf2;
3'b011: next=nf1;
3'b111: next=nf0;
endcase
end
always @(posedge clk) begin
if(reset) begin
state<=nf3;
dfr<=1;
end
else begin //在3个fr状态改变的时候,dfr的值也要改变,但下一个dfr的值取决于state和next
if (state < next) begin
dfr<=1;
state<=next; end
else if (state > next) begin
dfr<=0;
state<=next; end
else begin
dfr<=dfr;
state<=next; end
end
end
always @(*) begin
case(state)
nf0: {fr3,fr2,fr1}=3'b000;
nf1: {fr3,fr2,fr1}=3'b001;
nf2: {fr3,fr2,fr1}=3'b011;
nf3: {fr3,fr2,fr1}=3'b111;
endcase
end
endmodule
Lemmings1
The game Lemmings involves critters with fairly simple brains. So simple that we are going to model it using a finite state machine.
In the Lemmings' 2D world, Lemmings can be in one of two states: walking left or walking right. It will switch directions if it hits an obstacle. In particular, if a Lemming is bumped on the left, it will walk right. If it's bumped on the right, it will walk left. If it's bumped on both sides at the same time, it will still switch directions.
Implement a Moore state machine with two states, two inputs, and one output that models this behaviour.
思路:在