2025-09-01 2025-10-04 Course AssignmentMicro Processor 功能介绍(报告) 代码123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394module RaidenFighter( input clk, input reset, //Outter-Input input player_move_left, input player_move_right, //VGA-Output output reg [2:0] RGB, output h_sync, output v_sync //LED-Output ); // --------------------------------------------------------- // Clock Divide // (1)slow clk: for objects movement 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 // (2)clk_div: for VGA Output reg clk_div; always @( posedge clk ) begin clk_div <= ~clk_div; end // VGA output regs reg [9:0] h_count = 0; reg [9:0] v_count = 0; reg h_sync_reg = 1; reg v_sync_reg = 1; // VGA constants localparam H_ACTIVE = 640; // H <=> x localparam V_ACTIVE = 480; // V <=> y 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); // Horizontal counter 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 // Vertical counter 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 // Horizontal sync pulse 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 // Vertical sync pulse 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; // --------------------------------------------------------- // Player Control parameter INIT_X_P = H_ACTIVE/2; parameter INIT_Y_P = V_ACTIVE-30; // At the botton of the screen. [CANNOT BE CHANGED.] parameter STRIDE = 10; // STRIDE: How many pixels does player move when the button is pressed. 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 // Bullet Control 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 // Enemy Emerge 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; //player score // == Player BULLET HIT DETECTION == 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; // Fake Ramdom 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 // v2: Enemy Danmoku 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 // Fire Height 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 // v2: Player HP 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 // == Game Over Judge == always @(posedge slow_clk) begin if (reset == 0) game_over <= 0; else if (hp == 0) game_over <= 1; end // --------------------------------------------------------- // Display parameter PLAYER_COLOR = 3'b110; // YELLOW parameter BACKGROUND_COLOR = 3'b000; // BLACK parameter BULLET_COLOR = 3'b010; // GREEN parameter ENEMY_COLOR = 3'b001; // RED parameter DANMOKU_COLOR = 3'b001; // RED 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( // Drawing Raiden-Fighter (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 ( // Drawing an enemy ((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 演示视频 Newer 基于Ubuntu22.4的Xilinx-FINN环境部署 Older 「によって」「に関して」「に対して」的总结和区分