///////////////////////////////////////////////////////////////
//module which makes game logic
///////////////////////////////////////////////////////////////
module game(
// inputs:
input wire char_clock,
input wire [3:0]key,
// outputs:
input wire [7:0]char_count,
input wire [1:0]pixel_state,
input wire [11:0]line_count,
input wire [1:0]line_state,
output reg video,
output reg [7:0]goals
);
reg [7:0]x;
reg dx;
reg [7:0]y;
reg dy;
reg [20:0]counter;
reg tm;
//synchronous process
always @(posedge char_clock)
begin
if(counter==500000/2)
begin counter<=0; tm<=1'b1; end
else
begin counter <= counter+1'b1; tm<=1'b0; end
end
reg border;
always @*
border = (char_count==0)|(line_count[11:3]==0)|(line_count[11:3]==74);
reg visible;
always @*
visible = (pixel_state==0)&(line_state==0);
reg [7:0]ry;
reg raket;
always @*
raket = (char_count==95)&(ry<line_count[11:3])&((ry+8)>line_count[11:3]);
always @(posedge tm)
begin
if((key[0]==0)&(ry>0))
ry<=ry-1'b1;
else
if((key[1]==0)&(ry<64))
ry<=ry+1'b1;
end
always @(posedge tm or negedge key[3])
begin
if(!key[3])
begin
//reset
goals <= 0;
x<=0;
y<=0;
end
else
begin
if(goals!=8'b11111111)
begin
if(dx==1'b0)
begin
x <= x+1'b1;
if( (x>92)&&(y>ry)&&(y<ry+8) )
begin
//raket
dx<=1'b1;
end
else
if(x>95)
begin
//goal
goals<={goals[6:0],1'b1};
dx<=1'b1;
end
end
else
begin
x <= x-1'b1;
if(x==2)
dx<=1'b0;
end
if(dy==1'b0)
begin
y <= y+1'b1;
if(y>72)
dy<=1'b1;
end
else
begin
y <= y-1'b1;
if(y==2)
dy<=1'b0;
end
end
end
end
always @(posedge char_clock)
video <= (((char_count==x)&(line_count[11:3]==y))|border|raket)&visible;
endmodule
Подробнее...