feat: 可丟出球並反彈回原位
This commit is contained in:
parent
2a60d64e9f
commit
effc9bad25
114
FPGA_FINAL.v
114
FPGA_FINAL.v
@ -1,15 +1,19 @@
|
|||||||
|
|
||||||
module FPGA_FINAL(
|
module FPGA_FINAL(
|
||||||
input CLK,
|
input CLK,
|
||||||
output reg [0:27] led,
|
output reg [0:27] led, // 8x8(RGB s2 s1 s0 COM)
|
||||||
input left, right
|
input left, right,
|
||||||
|
input throw, // 丟球
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
reg [2:0]plat_position; // 板子位置
|
reg [2:0]plat_position; // 板子位置
|
||||||
reg [2:0]ball_position; // 球 位置
|
reg [2:0]ball_position; // 球 位置
|
||||||
|
reg [2:0]ball_y_position; // 球 y 座標
|
||||||
|
|
||||||
reg upPosition;
|
reg upPosition; // 垂直方向
|
||||||
integer horizonPosition;
|
integer horizonPosition; // 水平方向(-1左 0中 1右)
|
||||||
|
|
||||||
reg handsOn; // bool,紀錄球現在丟出去了沒
|
reg handsOn; // bool,紀錄球現在丟出去了沒
|
||||||
|
|
||||||
@ -24,25 +28,79 @@ module FPGA_FINAL(
|
|||||||
|
|
||||||
plat_position = 3'b010; // 預設在 x=2 的位置
|
plat_position = 3'b010; // 預設在 x=2 的位置
|
||||||
ball_position = 3'b011; // 預設在 x=3 的位置
|
ball_position = 3'b011; // 預設在 x=3 的位置
|
||||||
|
ball_y_position = 3'b010; // 預設在 y=1 的位置
|
||||||
handsOn = 1; // 預設為 為丟出狀態
|
handsOn = 1; // 預設為 為丟出狀態
|
||||||
|
|
||||||
upPosition = 1; // 預設為 向上
|
upPosition = 1; // 預設為 向上
|
||||||
horizonPosition = 0; // 預設為 正中間方向
|
horizonPosition = 0; // 預設為 正中間方向
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
divfreq F(CLK, divclk);
|
|
||||||
buttondivfreq B(CLK, buttonclk);
|
|
||||||
|
|
||||||
|
|
||||||
|
// 開始所有除頻器
|
||||||
|
divfreq F(CLK, divclk); // 顯示用除頻器
|
||||||
|
buttondivfreq BT(CLK, buttonclk);
|
||||||
|
balldivfreq BA(CLK, ballclk);
|
||||||
|
|
||||||
|
|
||||||
|
// 判斷 球 移動
|
||||||
|
always @(posedge ballclk)
|
||||||
|
begin
|
||||||
|
if(handsOn==0) // 如果是丟出去的狀態才移動
|
||||||
|
begin
|
||||||
|
// 先判斷垂直方向
|
||||||
|
// 方向向上
|
||||||
|
if(upPosition)
|
||||||
|
if(ball_y_position<7) // 還沒到頂端
|
||||||
|
ball_y_position <= ball_y_position+1;
|
||||||
|
else
|
||||||
|
begin
|
||||||
|
ball_y_position <= ball_y_position-1; // 到頂端就開始往下
|
||||||
|
upPosition = 0;
|
||||||
|
end
|
||||||
|
// 方向向下
|
||||||
|
else
|
||||||
|
if(ball_y_position>1)
|
||||||
|
ball_y_position <= ball_y_position-1;
|
||||||
|
|
||||||
|
|
||||||
|
// 判斷水平方向
|
||||||
|
// 向右移動
|
||||||
|
if(horizonPosition==1)
|
||||||
|
if(ball_position<7)
|
||||||
|
ball_position <= ball_position+1; // 範圍內右移
|
||||||
|
else
|
||||||
|
horizonPosition = -1; // 超過範圍就轉向左邊
|
||||||
|
// 向左移動
|
||||||
|
else if(horizonPosition==-1)
|
||||||
|
// 範圍內
|
||||||
|
if(ball_position>0)
|
||||||
|
ball_position <= ball_position-1; // 範圍內左移
|
||||||
|
else
|
||||||
|
horizonPosition = 1; // 超過範圍就轉向右邊
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
// 判斷 所有按鈕
|
||||||
always @(posedge buttonclk)
|
always @(posedge buttonclk)
|
||||||
begin
|
begin
|
||||||
|
// 判斷 向左
|
||||||
if(left)
|
if(left)
|
||||||
if(plat_position>0)
|
if(plat_position>0)
|
||||||
plat_position <= plat_position-1;
|
plat_position <= plat_position-1;
|
||||||
|
|
||||||
|
// 判斷 向右
|
||||||
if(right)
|
if(right)
|
||||||
if(plat_position<5)
|
if(plat_position<5)
|
||||||
plat_position <= plat_position+1;
|
plat_position <= plat_position+1;
|
||||||
|
|
||||||
|
// 判斷 丟出球
|
||||||
|
if(throw)
|
||||||
|
if(handsOn)
|
||||||
|
begin
|
||||||
|
handsOn = 0;
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@ -68,11 +126,34 @@ module FPGA_FINAL(
|
|||||||
|
|
||||||
|
|
||||||
// 開始畫球 ( G )
|
// 開始畫球 ( G )
|
||||||
|
|
||||||
|
// 在板子上的狀況
|
||||||
if(handsOn)
|
if(handsOn)
|
||||||
if(row==plat_position+1) // 放在正中間
|
if(row==plat_position+1) // 放在正中間
|
||||||
led[8:15] = 8'b11111101;
|
led[8:15] = 8'b11111101;
|
||||||
else
|
else
|
||||||
led[8:15] = 8'b11111111;
|
led[8:15] = 8'b11111111;
|
||||||
|
|
||||||
|
// 丟出球後的狀況
|
||||||
|
else
|
||||||
|
if(row==ball_position)
|
||||||
|
begin
|
||||||
|
reg [7:0] map;
|
||||||
|
// 轉成 one-hot encode
|
||||||
|
case(ball_y_position)
|
||||||
|
3'b000: map = 8'b11111110 ;
|
||||||
|
3'b001: map = 8'b11111101 ;
|
||||||
|
3'b010: map = 8'b11111011 ;
|
||||||
|
3'b011: map = 8'b11110111 ;
|
||||||
|
3'b100: map = 8'b11101111 ;
|
||||||
|
3'b101: map = 8'b11011111 ;
|
||||||
|
3'b110: map = 8'b10111111 ;
|
||||||
|
3'b111: map = 8'b01111111 ;
|
||||||
|
endcase
|
||||||
|
led[8:15] = map;
|
||||||
|
end
|
||||||
|
else
|
||||||
|
led[8:15] = 8'b11111111;
|
||||||
end
|
end
|
||||||
endmodule
|
endmodule
|
||||||
|
|
||||||
@ -106,4 +187,21 @@ module buttondivfreq(input CLK, output reg CLK_div);
|
|||||||
else
|
else
|
||||||
Count <= Count + 1'b1;
|
Count <= Count + 1'b1;
|
||||||
end
|
end
|
||||||
endmodule
|
endmodule
|
||||||
|
|
||||||
|
|
||||||
|
// 球 飛行用的除頻器
|
||||||
|
module balldivfreq(input CLK, output reg CLK_div);
|
||||||
|
reg[26:0] Count;
|
||||||
|
always @(posedge CLK)
|
||||||
|
begin
|
||||||
|
if(Count>10000000)
|
||||||
|
begin
|
||||||
|
Count <= 27'b0;
|
||||||
|
CLK_div <= ~CLK_div;
|
||||||
|
end
|
||||||
|
else
|
||||||
|
Count <= Count + 1'b1;
|
||||||
|
end
|
||||||
|
endmodule
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user