新手请教verilog实现VGA显示移动小球的问题:使用ALTER公司的QuartusII软件开发,小弟现在想要实现一个用字模实现的小球,在屏幕上斜线移动的问题,但球会逐行显示又逐行消失,若只横向移动,则能一直显示,希望大家不吝赐教,部分代码如下:
module vga(
clk,
rst_n,
vga_hs,
vga_vs,
vga_r0,
vga_g0,
vga_b0,
vga_r1,
vga_g1,
vga_b1,
vga_r2,
vga_g2,
vga_b2
);
input clk;
input rst_n ;// synthesis attribute clock_buffer of rst_n is ibufg;
output vga_hs;
output vga_vs; // sync signals for monitor
output vga_r0, vga_g0, vga_b0;
output vga_r1, vga_g1, vga_b1;
output vga_r2, vga_g2, vga_b2;
wire hsync;
wire vsync;
wire valid;
wire [9:0] x_cnt;
wire [9:0] y_cnt;
reg [9:0] char_sel;
reg [9:0] char_sel2;
wire [15:0] char;
wire [31:0] char2;
reg [28:0]i;
reg [8:0]j;
reg [8:0]gx;
reg [8:0]gy;
//设定屏幕分辨率:800x 525
wire [9:0] xpos;//[0...799]
wire [9:0] ypos;//[0...524]
reg color;
reg color2;
reg word0;
reg globe0;
//reg [8:0]adr_g;
assign xpos = x_cnt - 10'd180; //确定X轴像素位置
assign ypos = y_cnt - 10'd35; //确定Y轴像素位置
sync_gen_50m sync_gen_50m_int (
.clk ( clk ),
.rst_n ( rst_n ),
.hsync ( hsync ),
.vsync ( vsync ),
.valid ( valid ),
.x_cnt ( x_cnt ),
.y_cnt ( y_cnt )
);
wire table0 = (( ypos > 10'd140 && ypos < 10'd460 )&&( xpos > 10'd160 && xpos < 10'd640 )); //显示一个方框
wire line0 = (( ypos == 10'd140 || ypos == 10'd460 ) && ( xpos > 10'd160 && xpos < 10'd640 )||
( xpos == 10'd160 || xpos == 10'd640 ) && ( ypos > 10'd140 && ypos < 10'd460 )); //方框边线
wire word = word0 && color;
wire globe=globe0 && color2;
assign vga_r0 = valid ? (globe?1:(word ? 1'b1 : (line0 ? 1'b1 : (table0? 1'b0 : 1'b1 )))) : 1'b0;
assign vga_g0 = valid ? (globe?0:(word ? 1'b1 : (line0 ? 1'b1 : (table0? 1'b0 : 1'b0 )))) : 1'b0;
assign vga_b0 = valid ? (globe?0:(word ? 1'b0 : (line0 ? 1'b1 : (table0? 1'b0 : 1'b1 )))) : 1'b0;
assign vga_r1 = valid ? (globe?1:(word ? 1'b1 : (line0 ? 1'b1 : (table0? 1'b0 : 1'b1 )))) : 1'b0;
assign vga_g1 = valid ? (globe?0:(word ? 1'b1 : (line0 ? 1'b1 : (table0? 1'b0 : 1'b0 )))) : 1'b0;
assign vga_b1 = valid ? (globe?0:(word ? 1'b0 : (line0 ? 1'b1 : (table0? 1'b0 : 1'b1 )))) : 1'b0;
assign vga_r2 = valid ? (globe?1:(word ? 1'b1 : (line0 ? 1'b1 : (table0? 1'b0 : 1'b0 )))) : 1'b0;
assign vga_g2 = valid ? (globe?0:(word ? 1'b1 : (line0 ? 1'b1 : (table0? 1'b0 : 1'b0 )))) : 1'b0;
assign vga_b2 = valid ? (globe?0:(word ? 1'b0 : (line0 ? 1'b1 : (table0? 1'b0 : 1'b1 )))) : 1'b0;
assign vga_hs = hsync;
assign vga_vs = vsync;
char_rom char_rom_VibesIC_inst(
.addr ({char_sel[9:5],ypos[4:1]}), //字符的Y轴由16 pixel变为32 pixel
.data (char)
);
always @(posedge clk)
begin
word0 = ((xpos > (10'd460-j)) && (xpos < (10'd780-j)) &&
(ypos > 10'd96) && (ypos < 10'd128));
char_sel = xpos -10'd460+j;
globe0=((xpos>10'd384+gx)&&(xpos<10'd416+gx)&& //小球位置
(ypos>10'd140-gy)&&(ypos<172-gy));
char_sel2=xpos-10'd384; //小球X轴初始位置
if(i==29'h01Ef480)
begin
i=0;
j=j+1;
gx=gx-1;//小球X轴改变量
gy=gy+1;//小球Y轴改变量
if(j>280)j=0;
if(gx==160)begin gx=384;gy=140;end //小球初始位置
if(gx==0)begin gx=384;gy=140;end
end
else i=i+1;
end
always @(char_sel[4:1] or char)
begin
case (char_sel[4:1])
4'h0 : color = char[15]; //将X轴对应点赋给color
4'h1 : color = char[14];
4'h2 : color = char[13];
4'h3 : color = char[12];
4'h4 : color = char[11];
4'h5 : color = char[10];
4'h6 : color = char[9];
4'h7 : color = char[8];
4'h8 : color = char[7];
4'h9 : color = char[6];
4'hA : color = char[5];
4'hB : color = char[4];
4'hC : color = char[3];
4'hD : color = char[2];
4'hE : color = char[1];
4'hF : color = char[0];
endcase
end
char_rom_2 char_rom_VibesIC_inst2(
.addr ({char_sel2[8:5],ypos[4:0]}), //32 pixel
.data (char2)
);
always @(char_sel2[4:0] or char2)
begin
case (char_sel2[4:0])
5'h0 : color2 = char2[31]; //将X轴对应点赋给color
5'h1 : color2 = char2[30];
5'h2 : color2 = char2[29];
5'h3 : color2 = char2[28];
5'h4 : color2 = char2[27];
5'h5 : color2 = char2[26];
5'h6 : color2 = char2[25];
5'h7 : color2 = char2[24];
5'h8 : color2 = char2[23];
5'h9 : color2 = char2[22];
5'hA : color2 = char2[21];
5'hB : color2 = char2[20];
5'hC : color2 = char2[19];
5'hD : color2 = char2[18];
5'hE : color2 = char2[17];
5'hF : color2 = char2[16];
5'h10 : color2 = char2[15];
5'h11 : color2 = char2[14];
5'h12 : color2 = char2[13];
5'h13 : color2 = char2[12];
5'h14 : color2 = char2[11];
5'h15 : color2 = char2[10];
5'h16 : color2 = char2[9];
5'h17 : color2 = char2[8];
5'h18 : color2 = char2[7];
5'h19 : color2 = char2[6];
5'h1A : color2 = char2[5];
5'h1B : color2 = char2[4];
5'h1C : color2 = char2[3];
5'h1D : color2 = char2[2];
5'h1E : color2 = char2[1];
5'h1F : color2 = char2[0];
endcase
end
endmodule
谢谢!