status == 'b001) begin//换发球
status = serviceSide == 'b0 ? 'b010 : 'b001;
getScoreA = 'b0;
getScoreB = 'b0;
end
if(status == 'b010) begin //A发球
accurateBallLocation = 'd2000;
if(hitATrigger == 'b0 && hitA == 'b1) begin
status = 'b101;
if(speedA == 'd00) speed = 'd2;
else speed = 'd4;
end
hitATrigger = hitA;
end
else if(status == 'b001) begin //B发球
accurateBallLocation = 'd10000;
if(hitBTrigger == 'b0 && hitB == 'b1) begin
status = 'b110;
if(speedB == 'd00) speed = 'd2;
else speed = 'd4;
end
hitBTrigger = hitB;
end
else if(status == 'b110) begin //A接球
if(hitATrigger == 'b0 && hitA == 'b1) begin
if(accurateBallLocation >= 'd1000 && accurateBallLocation <= 'd3000) begin
status = 'b101;
if(speedA == 'd00) speed = 'd2;
else speed = 'd4;
end
end
hitATrigger = hitA;
if(accurateBallLocation < 'd500) begin
getScoreB = 'b1;
status = serviceSide == 'b0 ? 'b010 : 'b001;
end
accurateBallLocation -= speed * 'd3;
end
else if(status == 'b101) begin //B接球
if(hitBTrigger == 'b0 && hitB == 'b1) begin
if(accurateBallLocation >= 'd9000 && accurateBallLocation <= 'd11000) begin
status = 'b110;
if(speedB == 'd00) speed = 'd2;
else speed = 'd4;
end
end
hitBTrigger = hitB;
if(accurateBallLocation >'d11500) begin
getScoreA = 'b1;
status = serviceSide == 'b0 ? 'b010 : 'b001;
end
accurateBallLocation += speed * 'd3;
end
end
resetTrigger = reset;
if(accurateBallLocation >= 'd2000 && accurateBallLocation < 'd3000) ballLocation = 'b10000000;//球的位置显示
if(accurateBallLocation >= 'd3000 && accurateBallLocation < 'd4000) ballLocation = 'b01000000;
if(accurateBallLocation >= 'd4000 && accurateBallLocation < 'd5000) ballLocation = 'b00100000;
if(accurateBallLocation >= 'd5000 && accurateBallLocation < 'd6000) ballLocation = 'b00010000;
if(accurateBallLocation >= 'd6000 && accurateBallLocation < 'd7000) ballLocation = 'b00001000;
if(accurateBallLocation >= 'd7000 && accurateBallLocation < 'd8000) ballLocation = 'b00000100;
if(accurateBallLocation >= 'd8000 && accurateBallLocation < 'd9000) ballLocation = 'b00000010;
if(accurateBallLocation >= 'd9000 && accurateBallLocation <= 'd10000) ballLocation = 'b00000001;
end
endmodule
玩家控制模块
-
模块功能:控制玩家输入与接发球操作;
-
设计思路:在设计电路中规定了使能端EN,玩家只有在轮到自己发/击球时才有效;并规定了击球的间隔,模拟了击空的情况。除此之外还设计实现了玩家击球速度的选择。
-
代码:
点击查看代码
`timescale 1ns / 1ps
module Player(CLK, EN, hit, speed, hitOut, speedOut);
input CLK, EN, hit, speed;
output reg hitOut;
output reg [1: 0] speedOut;
reg [31: 0] activeInterval = 'd1000; //一个下降沿到下一个上升沿直接最小时间间隔
reg [31: 0] interval;
reg hitTrigger;
initial begin
interval = 'd0;
hitTrigger = 'b0;
hitOut = 'b0;
speedOut = 'b1;
end
always @(posedge CLK) begin
if(EN == 'b1) begin
if(hitTrigger =='b0 && hit == 'b1) begin
if(interval >= activeInterval) begin
hitOut = hit;
end
end
else if(hitTrigger == 'b1 && hit == 'b0) begin
interval = 'd0;
hitOut = hit;
end
hitTrigger = hit;
interval += 1;
if(speed == 'b0) begin
speedOut = 'd00;
end
else begin
speedOut = 'd01;
end
end
end
endmodule
时钟分频模块
-
模块功能:对时钟分频;
-
设计思路:将EG01的100MHZ的时钟分频为1000HZ。
-
代码:
点击查看代码
`timescale 1ns / 1ps
module ClockDivider(originCLK, dividedCLK);
input originCLK;
output div