前言

百战小旅鼠1991年由DMA Design(今rockstar north,蓝R)开发。

旅鼠会左右走,还会挖洞。如果下落太久了还会摔得四分五裂坠机了

思路

七个状态:左右、下落左右、向下挖时原本朝向左右,以及四分五裂。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
module top_module (
input clk,
input areset, // Freshly brainwashed Lemmings walk left.
input bump_left,
input bump_right,
input ground,
input dig,
output walk_left,
output walk_right,
output aaah,
output digging
);

parameter LEFT = 0, RIGHT = 1, FALLING_LEFT = 2, FALLING_RIGHT = 3, DIGGING_LEFT = 4,
DIGGING_RIGHT = 5, SPLAT = 6;
reg [2:0] state, next_state;
reg [7:0] count, next_count;

always @(*) begin

next_count = count;

case (state)

LEFT: begin
if (!ground) begin
next_state = FALLING_LEFT;
next_count = 0;
end
else if (dig) next_state = DIGGING_LEFT;
else if (bump_left) next_state = RIGHT;
else next_state = LEFT;
end

RIGHT: begin
if (!ground) begin
next_state = FALLING_RIGHT;
next_count = 0;
end
else if (dig) next_state = DIGGING_RIGHT;
else if (bump_right) next_state = LEFT;
else next_state = RIGHT;
end

FALLING_LEFT: begin
if (ground) begin
if (count >= 20) begin
next_state = SPLAT;
next_count = count;
end
else begin
next_state = LEFT;
next_count = 0;
end
end
else begin
next_state = FALLING_LEFT;
next_count = count + 1;
end
end

FALLING_RIGHT: begin
if (ground) begin
if (count >= 20) begin
next_state = SPLAT;
next_count = count;
end
else begin
next_state = RIGHT;
next_count = 0;
end
end
else begin
next_state = FALLING_RIGHT;
next_count = count + 1;
end
end

DIGGING_LEFT: begin
if (ground) next_state = DIGGING_LEFT;
else begin
next_state = FALLING_LEFT;
next_count = 0;
end
end

DIGGING_RIGHT: begin
if (ground) next_state = DIGGING_RIGHT;
else begin
next_state = FALLING_RIGHT;
next_count = 0;
end
end

SPLAT: next_state = SPLAT;

default: next_state = LEFT;

endcase
end

always @(posedge clk, posedge areset) begin
if (areset) begin
state <= LEFT;
count <= 0;
end
else begin
state <= next_state;
count <= next_count;
end
end

// assign aaah = (state == FALLING_LEFT) || (state == FALLING_RIGHT);
// assign digging = (state == DIGGING_LEFT) || (state == DIGGING_RIGHT);
// assign walk_left = (state == LEFT);
// assign walk_right = (state == RIGHT);

assign {aaah, digging, walk_left, walk_right} = (state == SPLAT) ?
4'b0000 : {{(state == FALLING_LEFT) || (state == FALLING_RIGHT)},
{(state == DIGGING_LEFT) || (state == DIGGING_RIGHT)},
{(state == LEFT)}, {(state == RIGHT)}};

endmodule

排查错误

一开始未设置next_count,直接给count赋值,会报错:Can't resolve multiple constant drivers for net "count[4]"。因此,需要像状态切换那样,设置一个新的next_count