基于FPGA的五子棋游戏设计
本文基于FPGA设计五子棋游戏,使用按键输入,使用VGA接口输出。五子棋的棋具与围棋相同,棋子分为黑白两色,棋盘为10×10,棋子放置于棋盘线交叉点上。两人对局,各执一色,轮流下一子,先将横、竖或斜线的5个或5个以上同色棋子连成不间断的一排者为胜。
VGA技术介绍:
VGA 接口就是显卡上输出模拟信号的接口,也叫 D-Sub 接口。VGA 接口是一种 D
型口,上面共有 15 针空,分成三排,每排五个。VGA 接口是目前中低端电脑配置上的主流口。
VGA显示中,FPGA需要产生5个信号:R、G、B三基色信号,行同步信号HS,场同步信号VS。以上接口的5个孔对应着我们FPGA中产生的5个重要的信号,其中R、G、B是数据信号;HS、VS是控制信号。下面是VGA 显示模块与CRT 显示器的控制框图:
像素是产生各种颜色的基本单元。根据物理学中的混色原理,三色发光的亮度比例适当,可呈现白色。适当的调整发光比例可以出现不同的颜色。三基色混色原理示意图如下图所示:
对于显示器来说,RGB三个信号其实是模拟信号,其电平的高低,可以表示颜色的深浅。利用这个原理,我们就可以产生丰富的色彩。为了控制电压的高低,我们就必须用到DA芯片。例如下图中,FPGA产生RGB三种信号,这时RGB都是多位的数字信号。DA芯片根据数字信号的值,产生不同电压的模拟信号rgb。
DE1-SoC板上有一个15针D-SUB连接器,用于VGA输出。VGA同步信号直接从Cyclone V SoC FPGA和Analog器件ADV7123三路10位高速视频DAC(仅使用较高的8位)转换从数字到模拟的信号代表三种基本颜色(红色,绿色和蓝色)。 它可以支持高达SXGA标准(1280 * 1024),信号以100MHz传输。 下图显示FPGA和VGA之间连接的信号。
显示器采用光栅扫描方式,即轰击荧光屏的电子束在 CRT 屏幕上从左到右(受水平同步信号 HSYNC 控制)、从上到下(受垂直同步信号 VSYNC 控制)做有规律的移动。电子束采用光栅扫描方式,从屏幕左上角一点开始,向右逐点进行扫描,形成一条水平线;到达最右端后,又回到下一条水平线的左端,重复上面的过程;当电子束完成右下角一点的扫描后,形成一帧。此后,电子束又回到左上方起点,开始下一帧的扫描。这种方法也就是常说的逐行扫描显示。
完成一行扫描的时间称为水平扫描时间,其倒数称为行频率;完成一帧(整屏)扫描的时间称为垂直扫描时间,其倒数称为场频率,即刷新一屏的频率,常见的有60Hz,75Hz等等。标准的VGA显示的场频60Hz。
行时序和场时序都需要同步脉冲(Sync a)、显示后沿(Back porch b)、显示时序段(Display interval c)和显示前沿(Front porch d)四部分。VGA工业标准显示模式要求:行同步,场同步都为负极性,即同步脉冲要求是负脉冲。
VGA支持的规格
我们以第一个分辨率640/480来分析,其刷新速率是60Hz,每幅图像有525行,每行有800个值。也就是说完成一幅图像约是1s/60=16.6ms,完成一行约为16.6ms/525=31.75us,完成一个像素传送约来31.75us /800=40ns。因此为了方便设计,接口的时候设为25MHz最方便,每个时钟送一个数据。
VGA时序设计
根据上节的介绍,VGA时序要求如下
VGA时序代码设计
/**********VGA驱动**********/
always@(posedge vga_clk or negedge rst_n)begin
if(!rst_n)begin
hcount <= 10'd0;
end
else if(hcount == 10'd799)begin
hcount <= 10'd0;
end
else begin
hcount <= hcount+1'b1;
end
end
always@(posedge vga_clk or negedge rst_n)begin
if(!rst_n) begin
vcount <= 10'd0;
end
else if(hcount == 10'd799)begin
if(vcount==524)
vcount <= 0;
else
vcount <= vcount+1;
end
end
always@(posedge vga_clk or negedge rst_n)begin
if(!rst_n)begin
hsync <= 1;
end
else if(hcount==10'd95)begin
hsync <= 1'b1;
end
else if(hcount==10'd799)begin
hsync <= 1'b0;
end
end
always@(posedge vga_clk or negedge rst_n)begin
if(!rst_n)begin
vsync <= 1'b1;
end
else if(vcount<10'd2)begin
vsync <= 1'b0;
end
else begin
vsync <= 1'b1;
end
end
VGA时序仿真结果
五子棋设计
为了节约FPGA资源,本文设计并编写了10x10 五子棋的专用算法。棋子半径大小20个像素。光标当前的棋子所在位置使用青色表示,因为屏幕背景色是黑色,棋盘背景色是白色,所以黑白棋子使用紫色和黄色代替。
棋盘设计:
设10x10数组reg [9:0] PLAYED [9:0]; 数组中每一位代表棋盘的一个交叉点。数组的某位为0时,代表无子状态; 为l时,代表有棋子;
设10x10数组reg [9:0] CHESS_COLOR [9:0];表示棋子的颜色。
产生棋盘格的代码如下:
//产生竖长条
always@(posedge vga_clk )
begin
if(hcount<=200||hcount>=640)begin
v_flag <= 1'b1 ;
v_dat <= 12'h000;//hei
end else if(hcount ==240||hcount ==280||hcount ==320||hcount ==360||hcount ==400||hcount ==440||hcount ==480||hcount ==520||hcount==560||hcount==600) begin
v_flag <= 1'b1 ;
v_dat <= 12'h000;//hei
end else begin
v_flag <= 1'b0 ;
v_dat <= 12'hfff;//bai
end
end
//产生横长条
always@(posedge vga_clk )
begin
if(vcount<=50||vcount>=490) begin
h_flag <= 1'b1 ;
h_dat <= 12'h000;//hei
end else if(vcount ==90||vcount ==130||vcount ==170||vcount==210 ||vcount==250||vcount ==290||vcount ==330||vcount==370 ||vcount==410||vcount==450) begin
h_flag <= 1'b1 ;
h_dat <= 12'h000;
end else begin
h_flag <= 1'b0 ;
h_dat <= 12'hfff;
end
end
2)当前棋子光标提示设计:
当前棋子位置提示显示数据为x_data,显示青色。V_dat和H_dat是横竖线的,显示为黑色,y_dat为棋子颜色。
/
/显示模块
always@(posedge vga_clk)
begin
if(dat_act)begin
if(over==0 && over2==0 && debug_flag==0 ) begin
if(curchess_flag)
disp_rgb <=x_dat; //
else if(v_flag)
disp_rgb <=v_dat; //
else if(h_flag)
disp_rgb <=h_dat; //
else if(chess_flag)
disp_rgb <=y_dat; //
else
disp_rgb <=12'hfff; //
end else
disp_rgb <=z_dat; //游戏结束则显示另外画面
end
else begin
disp_rgb = 12'h000; //VGA区域之外
end
end
3)按钮上、下、左、右、落子:
always@(posedge sysclk or negedge rst_n)begin
if(!rst_n)begin
PLAYED[0]<=10'd0;
PLAYED[1]<=10'd0;
PLAYED[2]<=10'd0;
PLAYED[3]<=10'd0;
PLAYED[4]<=10'd0;
PLAYED[5]<=10'd0;
PLAYED[6]<=10'd0;
PLAYED[7]<=10'd0;
PLAYED[8]<=10'd0;
PLAYED[9]<=10'd0;
CHESS_COLOR[0]<=10'd0;
CHESS_COLOR[1]<=10'd0;
CHESS_COLOR[2]<=10'd0;
CHESS_COLOR[3]<=10'd0;
CHESS_COLOR[4]<=10'd0;
CHESS_COLOR[5]<=10'd0;
CHESS_COLOR[6]<=10'd0;
CHESS_COLOR[7]<=10'd0;
CHESS_COLOR[8]<=10'd0;
CHESS_COLOR[9]<=10'd0;
end
else
begin
case(key_en_tem)
5'b00100:begin
if (ball_x_pos== 10'd240)
ball_x_pos<=10'd600;
else
ball_x_pos<=ball_x_pos-10'd40;
end
5'b01000:begin
if (ball_x_pos==10'd600)
ball_x_pos<=10'd240;
else
ball_x_pos<= ball_x_pos+10'd40;
end
5'b00001: begin
if (ball_y_pos== 10'd90)
ball_y_pos<=10'd450;
else
ball_y_pos<=ball_y_pos-10'd40;
end
5'b00010: begin
if(ball_y_pos== 10'd450)
ball_y_pos<=10'd90;
else
ball_y_pos<=ball_y_pos+10'd40;
end
5'b10000: begin
if(PLAYED[current_y][current_x]==0)
begin
PLAYED[current_y][current_x]<=1;
CHESS_COLOR[current_y][current_x]<=change_color;
end
end
(4)判胜:
横竖正斜反斜四个方向出现连续落子,且颜色一样,输出胜利提示。
```c
//输赢状态判断1
always@(posedge vga_clk )
if(!rst_n)begin
over<=0;
end
else
begin
if ( (scan_x<=5&&CHESS_COLOR[scan_y][scan_x]==1&&CHESS_COLOR[scan_y][scan_x+1]==1&&CHESS_COLOR[scan_y][scan_x+2]==1&&CHESS_COLOR[scan_y][scan_x+3]==1&&CHESS_COLOR[scan_y][scan_x+4]==1)//heng
&& (PLAYED[scan_y][scan_x]==1&&PLAYED[scan_y][scan_x+1]==1&&PLAYED[scan_y][scan_x+2]==1&&PLAYED[scan_y][scan_x+3]==1&&PLAYED[scan_y][scan_x+4]==1) )
begin
over<=1;
end
else if( (scan_y<=5&&CHESS_COLOR[scan_y][scan_x]==1&&CHESS_COLOR[scan_y+1][scan_x]==1&&CHESS_COLOR[scan_y+2][scan_x]==1&&CHESS_COLOR[scan_y+3][scan_x]==1&&CHESS_COLOR[scan_y+4][scan_x]==1)//shu
&& (PLAYED[scan_y][scan_x]==1&&PLAYED[scan_y+1][scan_x]==1&&PLAYED[scan_y+2][scan_x]==1&&PLAYED[scan_y+3][scan_x]==1&&PLAYED[scan_y+4][scan_x]==1) )
begin
over<=1;
end
else if( (scan_x<=5&&scan_y<=5&&CHESS_COLOR[scan_y][scan_x]==1&&CHESS_COLOR[scan_y+1][scan_x+1]==1&&CHESS_COLOR[scan_y+2][scan_x+2]==1&&CHESS_COLOR[scan_y+3][scan_x+3]==1&&CHESS_COLOR[scan_y+4][scan_x+4]==1)//chenggong
&& (PLAYED[scan_y][scan_x]==1&&PLAYED[scan_y+1][scan_x+1]==1&&PLAYED[scan_y+2][scan_x+2]==1&&PLAYED[scan_y+3][scan_x+3]==1&&PLAYED[scan_y+4][scan_x+4]==1) )
over<=1;
else if ( (scan_y<=5&&scan_x>=4&&CHESS_COLOR[scan_y][scan_x]==1&&CHESS_COLOR[scan_y+1][scan_x-1]==1&&CHESS_COLOR[scan_y+2][scan_x-2]==1&&CHESS_COLOR[scan_y+3][scan_x-3]==1&&CHESS_COLOR[scan_y+4][scan_x-4]==1)
&& (PLAYED[scan_y][scan_x]==1&&PLAYED[scan_y+1][scan_x-1]==1&&PLAYED[scan_y+2][scan_x-2]==1&&PLAYED[scan_y+3][scan_x-3]==1&&PLAYED[scan_y+4][scan_x-4]==1) )
over<=1;
end
```c
//输赢状态判断2 yellow
always@(posedge vga_clk )
if(!rst_n)begin
over2<=0;
debug_flag<=0;
end
else
begin
if( (scan_x<=5&&CHESS_COLOR[scan_y][scan_x]==0&&CHESS_COLOR[scan_y][scan_x+1]==0&&CHESS_COLOR[scan_y][scan_x+2]==0&&CHESS_COLOR[scan_y][scan_x+3]==0&&CHESS_COLOR[scan_y][scan_x+4]==0)//heng
&& (PLAYED[scan_y][scan_x]==1&&PLAYED[scan_y][scan_x+1]==1&&PLAYED[scan_y][scan_x+2]==1&&PLAYED[scan_y][scan_x+3]==1&&PLAYED[scan_y][scan_x+4]==1) )
over2<=1;
else if( (scan_y<=5&&CHESS_COLOR[scan_y][scan_x]==0&&CHESS_COLOR[scan_y+1][scan_x]==0&&CHESS_COLOR[scan_y+2][scan_x]==0&&CHESS_COLOR[scan_y+3][scan_x]==0&&CHESS_COLOR[scan_y+4][scan_x]==0)//shu
&& (PLAYED[scan_y][scan_x]==1&&PLAYED[scan_y+1][scan_x]==1&&PLAYED[scan_y+2][scan_x]==1&&PLAYED[scan_y+3][scan_x]==1&&PLAYED[scan_y+4][scan_x]==1) )
begin
over2<=1;
debug_flag<=1;
end
else if( (scan_x<=5&&scan_y<=5&&CHESS_COLOR[scan_y][scan_x]==0&&CHESS_COLOR[scan_y+1][scan_x+1]==0&&CHESS_COLOR[scan_y+2][scan_x+2]==0&&CHESS_COLOR[scan_y+3][scan_x+3]==0&&CHESS_COLOR[scan_y+4][scan_x+4]==0)//chenggong
&& (PLAYED[scan_y][scan_x]==1&&PLAYED[scan_y+1][scan_x+1]==1&&PLAYED[scan_y+2][scan_x+2]==1&&PLAYED[scan_y+3][scan_x+3]==1&&PLAYED[scan_y+4][scan_x+4]==1) )
over2<=1;
else if ( (scan_y<=5&&scan_x>=4&&CHESS_COLOR[scan_y][scan_x]==0&&CHESS_COLOR[scan_y+1][scan_x-1]==0&&CHESS_COLOR[scan_y+2][scan_x-2]==0&&CHESS_COLOR[scan_y+3][scan_x-3]==0&&CHESS_COLOR[scan_y+4][scan_x-4]==0)
&& (PLAYED[scan_y][scan_x]==1&&PLAYED[scan_y+1][scan_x-1]==1&&PLAYED[scan_y+2][scan_x-2]==1&&PLAYED[scan_y+3][scan_x-3]==1&&PLAYED[scan_y+4][scan_x-4]==1) )
over2<=1;
end
(5)win画面:在任一方胜利后VGA显示win三个字母画面输出胜利提示。根据棋子颜色win显示不同颜色以显示
//产生win画面1
always@(posedge vga_clk )
begin
if(((hcount>=240&&hcount<=260)||(hcount>=300&&hcount<=320)||(hcount>=360&&hcount<=380)||(hcount>=420&&hcount<=440)||(hcount>=480&&hcount<=500)||(hcount>=560&&hcount<=580))&&(vcount >=200&&vcount <=360))begin
z_flag <= 1'b1;
if(over)
z_dat <= 12'hf0f;
else
z_dat <= 12'hff0;
end else if(hcount>=260&&hcount<=380&&vcount>=340&&vcount <=360) begin
z_flag <= 1'b1;
if(over)
z_dat <= 12'hf0f;
else
z_dat <= 12'hff0;
end else if(hcount>=500&&hcount<=560&&vcount>=200&&vcount <=220) begin
z_flag <= 1'b1;
if(over)
z_dat <= 12'hf0f;
else
z_dat <= 12'hff0;
end else begin
z_flag <= 1'b0;
z_dat <= 12'hfff;
end
end
遇到的问题:
1 棋子颜色显示不稳定,下棋时棋子颜色随机产生不受控制。原因怀疑是时钟一开始是分频产生的25M,后面换成pll锁相环后问题果然解决。
一开始的时钟产生方法:
always@(posedge sysclk)
begin
vga_clk <= ~vga_clk;
end
优化后的:
pll pll(
.inclk0(sysclk),
.c0(vga_clk)
);