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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
| module RaidenFighter( input clk, input reset, input player_move_left, input player_move_right, output reg [2:0] RGB, output h_sync, output v_sync ); reg [31:0] slow_clk_counter; reg slow_clk; always @(posedge clk) begin if (reset == 0) begin slow_clk_counter <= 0; slow_clk <= 0; end else begin if (slow_clk_counter == 1200000) begin slow_clk_counter <= 0; slow_clk <= ~slow_clk; end else begin slow_clk_counter <= slow_clk_counter + 1; end end end reg clk_div; always @( posedge clk ) begin clk_div <= ~clk_div; end reg [9:0] h_count = 0; reg [9:0] v_count = 0; reg h_sync_reg = 1; reg v_sync_reg = 1;
localparam H_ACTIVE = 640; localparam V_ACTIVE = 480; localparam H_FRONT_PORCH = 16; localparam H_SYNC_PULSE = 96; localparam H_BACK_PORCH = 48; localparam V_FRONT_PORCH = 10; localparam V_SYNC_PULSE = 2; localparam V_BACK_PORCH = 33; wire h_end = (h_count == H_ACTIVE + H_FRONT_PORCH + H_SYNC_PULSE + H_BACK_PORCH - 1); wire v_end = (v_count == V_ACTIVE + V_FRONT_PORCH + V_SYNC_PULSE + V_BACK_PORCH - 1); always @(posedge clk_div) begin if (reset == 0) begin h_count <= 0; end else if (h_end) begin h_count <= 0; end else begin h_count <= h_count + 1; end end always @(posedge clk_div) begin if (reset == 0) begin v_count <= 0; end else if (h_end) begin if (v_end) begin v_count <= 0; end else begin v_count <= v_count + 1; end end end always @(posedge clk_div) begin if (reset == 0) begin h_sync_reg <= 1; end else if (h_count >= H_ACTIVE + H_FRONT_PORCH && h_count < H_ACTIVE + H_FRONT_PORCH + H_SYNC_PULSE) begin h_sync_reg <= 0; end else begin h_sync_reg <= 1; end end always @(posedge clk_div) begin if (reset == 0) begin v_sync_reg <= 1; end else if (v_count >= V_ACTIVE + V_FRONT_PORCH && v_count < V_ACTIVE + V_FRONT_PORCH + V_SYNC_PULSE) begin v_sync_reg <= 0; end else begin v_sync_reg <= 1; end end assign h_sync = h_sync_reg; assign v_sync = v_sync_reg; parameter INIT_X_P = H_ACTIVE/2; parameter INIT_Y_P = V_ACTIVE-30; parameter STRIDE = 10; parameter SIZE_X_P = 13; parameter SIZE_Y_P = 15; reg [15:0] x_p; reg [15:0] y_p; always @(posedge slow_clk) begin if (reset == 0) x_p <= INIT_X_P; else if (player_move_left && x_p > SIZE_X_P) x_p <= x_p - STRIDE; else if (player_move_right && x_p < H_ACTIVE - SIZE_X_P) x_p <= x_p + STRIDE; else x_p <= x_p; end always @(posedge slow_clk) begin if (reset == 0) y_p <= INIT_Y_P; else y_p <= y_p; end parameter SIZE_BULLET_X = 4; parameter SIZE_BULLET_Y = 20; parameter BULLET_SPEED = 40; reg [15:0] bullet_x; reg [15:0] bullet_y; reg bullet_active;
always @(posedge slow_clk) begin if (reset == 0) begin bullet_x <= x_p + SIZE_X_P/2 - SIZE_BULLET_X/2 - 10; bullet_y <= y_p + SIZE_Y_P + 10; bullet_active <= 1; end else if (bullet_active) begin if (bullet_y > BULLET_SPEED) begin bullet_y <= bullet_y - BULLET_SPEED; end else begin bullet_active <= 0; end end else begin bullet_x <= x_p + SIZE_X_P/2 - SIZE_BULLET_X/2; bullet_y <= y_p + SIZE_Y_P; bullet_active <= 1; end end parameter ENEMY_SIZE_X = 13; parameter ENEMY_SIZE_Y = 11; parameter ENEMY_SPEED = 5; reg [15:0] enemy_x; reg [15:0] enemy_y; reg [7:0] score_p; wire bullet_hit_enemy = bullet_active && (bullet_x + SIZE_BULLET_X > enemy_x) && (bullet_x < enemy_x + ENEMY_SIZE_X) && (bullet_y + SIZE_BULLET_Y > enemy_y) && (bullet_y < enemy_y + ENEMY_SIZE_Y); always @(posedge slow_clk) begin if (reset == 0) begin enemy_x <= 300; enemy_y <= 0; score_p <= 0; end else if (bullet_hit_enemy) begin score_p <= score_p + 1; enemy_y <= 0; enemy_x <= 100 + (enemy_x + 114) % 512; end else begin if (enemy_y < V_ACTIVE - ENEMY_SPEED) enemy_y <= enemy_y + ENEMY_SPEED; else begin enemy_y <= 0; enemy_x <= 100 + (enemy_x + 114) % 512; end end end parameter ENEMY_BULLET_NUM = 8; parameter ENEMY_BULLET_SPEED = 20; parameter ENEMY_BULLET_SIZE = 10; reg [15:0] eb_x[0:ENEMY_BULLET_NUM-1]; reg [15:0] eb_y[0:ENEMY_BULLET_NUM-1]; reg eb_active[0:ENEMY_BULLET_NUM-1]; integer i; always @(posedge slow_clk) begin if (reset == 0) begin for (i = 0; i < ENEMY_BULLET_NUM; i = i + 1) begin eb_active[i] <= 0; end end else begin if (enemy_y == 150) begin eb_x[0] <= enemy_x; eb_y[0] <= enemy_y; eb_active[0] <= 1; eb_x[1] <= enemy_x + 6; eb_y[1] <= enemy_y; eb_active[1] <= 1; eb_x[2] <= enemy_x + 6; eb_y[2] <= enemy_y + 6; eb_active[2] <= 1; eb_x[3] <= enemy_x + 6; eb_y[3] <= enemy_y + 12; eb_active[3] <= 1; eb_x[4] <= enemy_x; eb_y[4] <= enemy_y + 12; eb_active[4] <= 1; eb_x[5] <= enemy_x - 6; eb_y[5] <= enemy_y + 12; eb_active[5] <= 1; eb_x[6] <= enemy_x - 6; eb_y[6] <= enemy_y + 6; eb_active[6] <= 1; eb_x[7] <= enemy_x - 6; eb_y[7] <= enemy_y; eb_active[7] <= 1; end
for (i = 0; i < ENEMY_BULLET_NUM; i = i + 1) begin if (eb_active[i]) begin case(i) 0: eb_y[i] <= eb_y[i] - ENEMY_BULLET_SPEED; 1: begin eb_x[i] <= eb_x[i] + ENEMY_BULLET_SPEED; eb_y[i] <= eb_y[i] - ENEMY_BULLET_SPEED; end 2: eb_x[i] <= eb_x[i] + ENEMY_BULLET_SPEED; 3: begin eb_x[i] <= eb_x[i] + ENEMY_BULLET_SPEED; eb_y[i] <= eb_y[i] + ENEMY_BULLET_SPEED; end 4: eb_y[i] <= eb_y[i] + ENEMY_BULLET_SPEED; 5: begin eb_x[i] <= eb_x[i] - ENEMY_BULLET_SPEED; eb_y[i] <= eb_y[i] + ENEMY_BULLET_SPEED; end 6: eb_x[i] <= eb_x[i] - ENEMY_BULLET_SPEED; 7: begin eb_x[i] <= eb_x[i] - ENEMY_BULLET_SPEED; eb_y[i] <= eb_y[i] - ENEMY_BULLET_SPEED; end endcase
if (eb_x[i] < 5 || eb_x[i] > H_ACTIVE - 5 || eb_y[i] < 5 || eb_y[i] > V_ACTIVE - 5) eb_active[i] <= 0; end end end end reg [1:0] hp; reg game_over; always @(posedge slow_clk) begin if (reset == 0) hp <= 3; else if (!game_over) begin if ( (eb_active[0] && eb_x[0] >= x_p - SIZE_X_P && eb_x[0] <= x_p + SIZE_X_P && eb_y[0] >= y_p - SIZE_Y_P && eb_y[0] <= y_p) || (eb_active[1] && eb_x[1] >= x_p - SIZE_X_P && eb_x[1] <= x_p + SIZE_X_P && eb_y[1] >= y_p - SIZE_Y_P && eb_y[1] <= y_p) || (eb_active[2] && eb_x[2] >= x_p - SIZE_X_P && eb_x[2] <= x_p + SIZE_X_P && eb_y[2] >= y_p - SIZE_Y_P && eb_y[2] <= y_p) || (eb_active[3] && eb_x[3] >= x_p - SIZE_X_P && eb_x[3] <= x_p + SIZE_X_P && eb_y[3] >= y_p - SIZE_Y_P && eb_y[3] <= y_p) || (eb_active[4] && eb_x[4] >= x_p - SIZE_X_P && eb_x[4] <= x_p + SIZE_X_P && eb_y[4] >= y_p - SIZE_Y_P && eb_y[4] <= y_p) || (eb_active[5] && eb_x[5] >= x_p - SIZE_X_P && eb_x[5] <= x_p + SIZE_X_P && eb_y[5] >= y_p - SIZE_Y_P && eb_y[5] <= y_p) || (eb_active[6] && eb_x[6] >= x_p - SIZE_X_P && eb_x[6] <= x_p + SIZE_X_P && eb_y[6] >= y_p - SIZE_Y_P && eb_y[6] <= y_p) || (eb_active[7] && eb_x[7] >= x_p - SIZE_X_P && eb_x[7] <= x_p + SIZE_X_P && eb_y[7] >= y_p - SIZE_Y_P && eb_y[7] <= y_p) ) begin if (hp > 0) hp <= hp - 1; end end end always @(posedge slow_clk) begin if (reset == 0) game_over <= 0; else if (hp == 0) game_over <= 1; end
parameter PLAYER_COLOR = 3'b110; parameter BACKGROUND_COLOR = 3'b000; parameter BULLET_COLOR = 3'b010; parameter ENEMY_COLOR = 3'b001; parameter DANMOKU_COLOR = 3'b001; parameter GAMEOVER = 3'b101; parameter HP_COLOR = 3'b001;
always @(posedge clk_div) begin if (reset == 0) RGB <= 3'b000; else if (game_over) begin RGB <= GAMEOVER; end else if (h_count < H_ACTIVE && v_count < V_ACTIVE) begin if( (h_count == x_p && v_count == y_p - 18) || ((h_count >= x_p - 1 && h_count <= x_p + 1) && v_count == y_p - 17) || ((h_count >= x_p - 2 && h_count <= x_p + 2) && v_count == y_p - 16) || ((h_count >= x_p - 3 && h_count <= x_p + 3) && v_count == y_p - 15) || ((h_count >= x_p - 4 && h_count <= x_p + 4) && v_count == y_p - 14) || ((h_count >= x_p - 5 && h_count <= x_p + 5) && v_count == y_p - 13) || ((h_count >= x_p - 6 && h_count <= x_p + 6) && v_count == y_p - 12) || ((h_count >= x_p - 7 && h_count <= x_p + 7) && v_count == y_p - 11) || ((h_count >= x_p - 8 && h_count <= x_p + 8) && v_count == y_p - 10) || ((h_count >= x_p - 9 && h_count <= x_p + 9) && v_count == y_p - 9) || ((h_count >= x_p -10 && h_count <= x_p +10) && v_count == y_p - 8) || ((h_count >= x_p -11 && h_count <= x_p +11) && v_count == y_p - 7) || ((h_count >= x_p -12 && h_count <= x_p +12) && v_count == y_p - 6) || ((h_count >= x_p - 9 && h_count <= x_p - 7) && (v_count == y_p - 5 || v_count == y_p - 4)) || ((h_count >= x_p + 7 && h_count <= x_p + 9) && (v_count == y_p - 5 || v_count == y_p - 4)) ) RGB <= PLAYER_COLOR; else if(bullet_active && h_count >= bullet_x && h_count < bullet_x + SIZE_BULLET_X && v_count >= bullet_y && v_count < bullet_y + SIZE_BULLET_Y) RGB <= BULLET_COLOR; else if ( ((h_count == enemy_x - 3 || h_count == enemy_x + 3) && v_count == enemy_y) || ((h_count >= enemy_x - 5 && h_count <= enemy_x - 1) || (h_count >= enemy_x + 1 && h_count <= enemy_x + 5)) && v_count == enemy_y + 1 || (h_count >= enemy_x - 6 && h_count <= enemy_x + 6 && v_count == enemy_y + 2) || ((h_count >= enemy_x - 6 && h_count <= enemy_x - 1) || (h_count >= enemy_x + 1 && h_count <= enemy_x + 6)) && v_count == enemy_y + 3 || (h_count >= enemy_x - 6 && h_count <= enemy_x + 6 && v_count == enemy_y + 4) || ((h_count >= enemy_x - 6 && h_count <= enemy_x - 4) || (h_count >= enemy_x + 4 && h_count <= enemy_x + 6)) && v_count == enemy_y + 5 || (h_count >= enemy_x - 3 && h_count <= enemy_x + 3 && v_count == enemy_y + 6) || ((h_count == enemy_x - 3 || h_count == enemy_x - 1 || h_count == enemy_x + 1 || h_count == enemy_x + 3) && v_count == enemy_y + 7) || ((h_count >= enemy_x - 4 && h_count <= enemy_x + 4) && v_count == enemy_y + 8) || ((h_count >= enemy_x - 5 && h_count <= enemy_x + 5) && v_count == enemy_y + 9) || ((h_count >= enemy_x - 6 && h_count <= enemy_x + 6) && v_count == enemy_y + 10) ) RGB <= ENEMY_COLOR; else if ( (eb_active[0] && h_count >= eb_x[0] && h_count < eb_x[0] + ENEMY_BULLET_SIZE && v_count >= eb_y[0] && v_count < eb_y[0] + ENEMY_BULLET_SIZE) || (eb_active[1] && h_count >= eb_x[1] && h_count < eb_x[1] + ENEMY_BULLET_SIZE && v_count >= eb_y[1] && v_count < eb_y[1] + ENEMY_BULLET_SIZE) || (eb_active[2] && h_count >= eb_x[2] && h_count < eb_x[2] + ENEMY_BULLET_SIZE && v_count >= eb_y[2] && v_count < eb_y[2] + ENEMY_BULLET_SIZE) || (eb_active[3] && h_count >= eb_x[3] && h_count < eb_x[3] + ENEMY_BULLET_SIZE && v_count >= eb_y[3] && v_count < eb_y[3] + ENEMY_BULLET_SIZE) || (eb_active[4] && h_count >= eb_x[4] && h_count < eb_x[4] + ENEMY_BULLET_SIZE && v_count >= eb_y[4] && v_count < eb_y[4] + ENEMY_BULLET_SIZE) || (eb_active[5] && h_count >= eb_x[5] && h_count < eb_x[5] + ENEMY_BULLET_SIZE && v_count >= eb_y[5] && v_count < eb_y[5] + ENEMY_BULLET_SIZE) || (eb_active[6] && h_count >= eb_x[6] && h_count < eb_x[6] + ENEMY_BULLET_SIZE && v_count >= eb_y[6] && v_count < eb_y[6] + ENEMY_BULLET_SIZE) || (eb_active[7] && h_count >= eb_x[7] && h_count < eb_x[7] + ENEMY_BULLET_SIZE && v_count >= eb_y[7] && v_count < eb_y[7] + ENEMY_BULLET_SIZE) ) RGB <= DANMOKU_COLOR; else if ( (hp >= 1 && ( (h_count == 20 && v_count == V_ACTIVE - 15) || (h_count == 21 && v_count == V_ACTIVE - 14) || (h_count == 22 && v_count == V_ACTIVE - 13) || (h_count == 23 && v_count == V_ACTIVE - 14) || (h_count == 24 && v_count == V_ACTIVE - 15) )) || (hp >= 2 && ( (h_count == 30 && v_count == V_ACTIVE - 15) || (h_count == 31 && v_count == V_ACTIVE - 14) || (h_count == 32 && v_count == V_ACTIVE - 13) || (h_count == 33 && v_count == V_ACTIVE - 14) || (h_count == 34 && v_count == V_ACTIVE - 15) )) || (hp == 3 && ( (h_count == 40 && v_count == V_ACTIVE - 15) || (h_count == 41 && v_count == V_ACTIVE - 14) || (h_count == 42 && v_count == V_ACTIVE - 13) || (h_count == 43 && v_count == V_ACTIVE - 14) || (h_count == 44 && v_count == V_ACTIVE - 15) )) ) RGB <= HP_COLOR; else RGB <= BACKGROUND_COLOR; end else begin RGB <= 3'b000; end end
endmodule
|