-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameFSM.sv
208 lines (193 loc) · 5.28 KB
/
gameFSM.sv
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
module gameFSM( input logic Clk, VGACLK, VGA_VS, Reset,
input logic [7:0] keycode,
input logic [9:0] DRAWX, DRAWY,
input logic charIsRunning,
output logic charIsMoving, PLAY,
output logic [1:0] direction, charMoveFrame,
output logic [3:0] state_num
);
/*
direction map:
0: down
1: up
2: left
3: right
charMoveFrame:
0, 1, 2 for movement.
state map:
0: start_screen
1: flash_press_enter
2: fade
3: draw_main_game
4: hold
*/
logic [1:0] next_direction;
logic [4:0] tilecounter, next_tilecounter;
logic [5:0] charFrameCounter, next_charFrameCounter;
logic [5:0] fade_counter, fade_counter_next;
logic [7:0] W = 8'h1A;
logic [7:0] A = 8'h04;
logic [7:0] S = 8'h16;
logic [7:0] D = 8'h07;
enum logic [3:0] {start_screen, flash_press_enter, fade, draw_main_game, hold} state, next_state;
always_ff @ (posedge VGA_VS) begin
if(Reset) begin
//state <= start_screen;
state <= start_screen;
fade_counter <= 6'd0;
direction <= 2'd1;
charFrameCounter<= 6'd0;
tilecounter <= 5'd31;
end
else begin
state <= next_state;
fade_counter <= fade_counter_next;
direction <= next_direction;
charFrameCounter<= next_charFrameCounter;
tilecounter <= next_tilecounter;
end
end
always_comb begin
next_state = state;
unique case (state)
start_screen :
if(keycode == 8'h28) begin
next_state = fade;
end
else begin
next_state = start_screen;
end
flash_press_enter :
if(keycode == 8'h28) begin
next_state = fade;
end
else begin
next_state = start_screen;
end
fade :
if(fade_counter == 6'd50) begin
next_state = draw_main_game;
end
draw_main_game :
if(keycode == 8'h29) begin
next_state = start_screen;
end
hold : ;
endcase // state
end
always_comb begin
fade_counter_next = fade_counter;
next_charFrameCounter = charFrameCounter;
next_direction = direction;
charIsMoving = 1'b0;
charMoveFrame = 2'b00;
state_num = 4'b0000;
next_tilecounter = tilecounter;
PLAY = 1'b1;
case (state)
start_screen : ;
flash_press_enter :
begin
state_num = 4'd1;
end
fade :
begin
if(fade_counter == 6'd50) begin
fade_counter_next = 6'd0;
end
else begin
fade_counter_next = fade_counter + 6'd1;
end
state_num = 4'd2;
end
draw_main_game : //NEED TO IMPLEMENT RUNNING
begin
state_num = 4'd3;
if (tilecounter != 5'd31) begin //doing this for the character to walk in blocks of 16
next_direction = direction;
charIsMoving = 1'b1;
next_charFrameCounter = charFrameCounter + 6'd1;
next_tilecounter = tilecounter + 5'd1;
end
else begin
next_tilecounter = 5'd31;
unique case (keycode)
S : begin
if(direction == 2'd0) begin
next_tilecounter = 5'd0;
next_direction = direction;
charIsMoving = 1'b1;
next_charFrameCounter = charFrameCounter + 6'd1;
end
else begin
next_direction = 2'd0;
charIsMoving = 1'b0;
next_charFrameCounter = 6'd0;
next_tilecounter = 5'd31;
end
end
W : begin
if(direction == 2'd1) begin
next_tilecounter = 5'd0;
next_direction = direction;
charIsMoving = 1'b1;
next_charFrameCounter = charFrameCounter + 6'd1;
end
else begin
next_direction = 2'd1;
charIsMoving = 1'b0;
next_charFrameCounter = 6'd0;
next_tilecounter = 5'd31;
end
end
A : begin
if(direction == 2'd2) begin
next_tilecounter = 5'd0;
next_direction = direction;
charIsMoving = 1'b1;
next_charFrameCounter = charFrameCounter + 6'd1;
end
else begin
next_direction = 2'd2;
charIsMoving = 1'b0;
next_charFrameCounter = 6'd0;
next_tilecounter = 5'd31;
end
end
D : begin
if(direction == 2'd3) begin
next_tilecounter = 5'd0;
next_direction = direction;
charIsMoving = 1'b1;
next_charFrameCounter = charFrameCounter + 6'd1;
end
else begin
next_direction = 2'd3;
charIsMoving = 1'b0;
next_charFrameCounter = 6'd0;
next_tilecounter = 5'd31;
end
end
default : ;
endcase // keycode
end
if(charFrameCounter < 6'd10) begin
charMoveFrame = 2'd0;
end
else if(charFrameCounter < 6'd20) begin
charMoveFrame = 2'd1;
end
else if(charFrameCounter < 6'd30) begin
charMoveFrame = 2'd2;
end
else if(charFrameCounter < 6'd40) begin
charMoveFrame = 2'd1;
end
if(charFrameCounter == 6'd39) begin
next_charFrameCounter = 6'd0;
end
end
hold : ;
endcase
end
endmodule