fix: 修復球只能在原位丟出的問題
- 嘗試修復時遇到同一資料流不能被兩個 always 同步修改,故改成待在同一執行序列
This commit is contained in:
parent
effc9bad25
commit
3f368c63aa
110
FPGA_FINAL.v
110
FPGA_FINAL.v
@ -1,9 +1,10 @@
|
||||
|
||||
module FPGA_FINAL(
|
||||
input CLK,
|
||||
output reg [0:27] led, // 8x8(RGB s2 s1 s0 COM)
|
||||
output reg [0:27] led,
|
||||
input left, right,
|
||||
input throw, // 丟球
|
||||
input throw,
|
||||
output testLED
|
||||
);
|
||||
|
||||
|
||||
@ -12,10 +13,11 @@ module FPGA_FINAL(
|
||||
reg [2:0]ball_position; // 球 位置
|
||||
reg [2:0]ball_y_position; // 球 y 座標
|
||||
|
||||
reg upPosition; // 垂直方向
|
||||
integer horizonPosition; // 水平方向(-1左 0中 1右)
|
||||
reg upPosition;
|
||||
integer horizonPosition;
|
||||
|
||||
reg handsOn; // bool,紀錄球現在丟出去了沒
|
||||
reg throwFlag; // 判斷丟球,避免兩 always 修改同一 reg 發生衝突
|
||||
|
||||
|
||||
|
||||
@ -38,62 +40,30 @@ module FPGA_FINAL(
|
||||
|
||||
|
||||
// 開始所有除頻器
|
||||
divfreq F(CLK, divclk); // 顯示用除頻器
|
||||
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
|
||||
|
||||
|
||||
// 判斷 所有按鈕
|
||||
integer ballTime;
|
||||
// 判斷 所有操作
|
||||
always @(posedge buttonclk)
|
||||
begin
|
||||
// 判斷 向左
|
||||
if(left)
|
||||
if(plat_position>0)
|
||||
begin
|
||||
plat_position <= plat_position-1;
|
||||
if(handsOn==1)ball_position <= ball_position-1;
|
||||
end
|
||||
|
||||
// 判斷 向右
|
||||
if(right)
|
||||
if(plat_position<5)
|
||||
begin
|
||||
plat_position <= plat_position+1;
|
||||
if(handsOn==1)ball_position <= ball_position+1;
|
||||
end
|
||||
|
||||
// 判斷 丟出球
|
||||
if(throw)
|
||||
@ -101,6 +71,48 @@ module FPGA_FINAL(
|
||||
begin
|
||||
handsOn = 0;
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 下方操作球的運行
|
||||
// 除頻用
|
||||
if(ballTime<5)
|
||||
ballTime <= ballTime+1;
|
||||
else
|
||||
//開始判斷球的行進
|
||||
begin
|
||||
ballTime <= 0;
|
||||
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
|
||||
end
|
||||
|
||||
|
||||
@ -126,20 +138,15 @@ module FPGA_FINAL(
|
||||
|
||||
|
||||
// 開始畫球 ( G )
|
||||
|
||||
// 在板子上的狀況
|
||||
if(handsOn)
|
||||
if(row==plat_position+1) // 放在正中間
|
||||
led[8:15] = 8'b11111101;
|
||||
else
|
||||
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 ;
|
||||
@ -179,7 +186,7 @@ module buttondivfreq(input CLK, output reg CLK_div);
|
||||
reg[24:0] Count;
|
||||
always @(posedge CLK)
|
||||
begin
|
||||
if(Count>2500000)
|
||||
if(Count>2500000) // 20 Hz
|
||||
begin
|
||||
Count <= 25'b0;
|
||||
CLK_div <= ~CLK_div;
|
||||
@ -204,4 +211,3 @@ module balldivfreq(input CLK, output reg CLK_div);
|
||||
Count <= Count + 1'b1;
|
||||
end
|
||||
endmodule
|
||||
|
||||
Loading…
Reference in New Issue
Block a user