summaryrefslogtreecommitdiffstats
path: root/GPU/hdl/GPU_IF.v
blob: f32de1ef10e1c11758d6fa9c1661e0fa31a50b7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// gpuv2.v

// This file was auto-generated as a prototype implementation of a module
// created in component editor.  It ties off all outputs to ground and
// ignores all inputs.  It needs to be edited to make it do something
// useful.
// 
// This file will not be automatically regenerated.  You should check it in
// to your version control system if you want to keep it.

`timescale 1 ps / 1 ps
module gpuv2 #(
		parameter AUTO_CLOCK_CLOCK_RATE = "-1"
	) (
		input  wire       clk,       //        clock.clk
		input  wire       rst_n,     //        reset.reset_n
		input  wire       vga_clk,   //      vga_out.export
		output reg [2:0] vga_red,    //             .export
		output reg [2:0] vga_green,  //             .export
		output reg [2:0] vga_blue,   //             .export
		output reg        vga_hs,    //             .export
		output reg        vga_vs,    //             .export
		input  wire [31:0] data,     // avalon_slave.writedata
		input  wire       wr_n,      //             .write_n
		input  wire       cs_n,      //             .chipselect_n
		input  wire [6:0] address    //             .address
	);


  reg [9:0] ball_x;
  reg [8:0] ball_y;
  reg [8:0] bat0_y;
  reg [8:0] bat1_y;

  reg [15:0] sprite[0:15];
  
  wire [4:0] reg_addr;
 
  assign reg_addr = address[6:2];
  
  
  reg [2:0] sprite_red;
  reg [2:0] sprite_green;
  reg [2:0] sprite_blue;
  
  always @(posedge clk or negedge rst_n)
  begin
	if (rst_n == 0) begin
			ball_x <= 10'd127;
			ball_y <= 10'd127;
			bat0_y <= 10'd100;
			bat1_y <= 10'd200;
			
			sprite[0]=16'b1111111111111111;
			sprite[1]=16'b1000000000000001;
			sprite[2]=16'b1000000000000001;
			sprite[3]=16'b1000000000000001;
			sprite[4]=16'b1000000000000001;
			sprite[5]=16'b1000000000000001;
			sprite[6]=16'b1000000000000001;
			sprite[7]=16'b1000000000000001;
			sprite[8]=16'b1000000000000001;
			sprite[9]=16'b1000000000000001;
			sprite[10]=16'b1000000000000001;
			sprite[11]=16'b1000000000000001;
			sprite[12]=16'b1000000000000001;
			sprite[13]=16'b1000000000000001;
			sprite[14]=16'b1000000000000001;
			sprite[15]=16'b1111111111111111;
			
			sprite_red[2:0]=3'b111;
			sprite_green[2:0]=3'b000;
			sprite_blue[2:0]=3'b111;
			
	end else if (~cs_n && ~wr_n) begin
		if (reg_addr[4]) begin
			sprite[reg_addr[3:0]]<=data[15:0];
		end else begin
			case (reg_addr[2:0]) 
				3'b000:
					ball_x <= data[9:0];
				3'b001:
					ball_y <= data[8:0];
				3'b010:
					bat0_y <= data[8:0];
				3'b011:
					bat1_y <= data[8:0];
				3'b100:
					begin
						sprite_red <= data[8:6];
						sprite_green <= data[5:3];
						sprite_blue <= data[2:0];
					end
			endcase
		end
	end
  end


  reg trig_25M;
  always @ (posedge vga_clk)
  begin
    if(!rst_n)
      trig_25M <= 1'b0;
    else
      trig_25M <= ~trig_25M;
  end 
  
  reg [9:0] vector_x;
  always @ (posedge vga_clk or negedge rst_n)
  begin
    if(!rst_n)
      vector_x <= 10'd0;
    else if(trig_25M)
      begin
        if(vector_x != 10'd799)
          vector_x <= vector_x + 1'b1;
        else
          vector_x <= 10'd0;
      end
  end 
  
  reg [9:0] vector_y;
  always @ (posedge vga_clk or negedge rst_n)
  begin
    if(!rst_n)
      vector_y <= 10'd0;
    else if(trig_25M)
      begin
        if(vector_x == 10'd799)
          begin
            if(vector_y != 10'd524)
              vector_y <= vector_y + 1'b1;
            else
              vector_y <= 10'd0;
          end
      end
  end
  
  always @ (posedge vga_clk or negedge rst_n)
  begin
    if(!rst_n)
      vga_hs <= 1'b0;
    else if(trig_25M)
      begin
        if(vector_x >= 10'd656 && vector_x < 10'd752)
          vga_hs <= 1'b0;
        else
          vga_hs <= 1'b1;
      end
  end
  
  always @ (posedge vga_clk or negedge rst_n)
  begin
    if(!rst_n)
      vga_vs <= 1'b0;
    else if(trig_25M)
      begin
        if(vector_y >= 10'd490 && vector_y < 10'd492)
          vga_vs <= 1'b0;
        else
          vga_vs <= 1'b1;
      end
  end
  
  
  
  reg [2:0] index;
  always @ (posedge vga_clk or negedge rst_n)
  begin
    if(!rst_n)
      index <= 3'b000;
    else if(trig_25M)
      begin
        if(vector_x < 10'd640 && vector_y < 10'd480) 
		  if(vector_x >= (ball_x - 8 )&& vector_x <=( ball_x + 7 )
				&& vector_y >= (ball_y - 8) && vector_y <= (ball_y + 7)) begin 
				if (sprite[(vector_y - (ball_y - 8)) & 15 ][(vector_x - (ball_x -8))& 15 ])
					index <= 3'b001;
				else
					index <= 3'b000;
		  end else if (vector_x  < 10'd4) 
				if (vector_y >=( bat0_y - 20 ) && vector_y <= (bat0_y + 20) )
					index <= 3'b111;
				else
					index <= 3'b000;
		  else if (vector_x >= 10'd318 && vector_x < 10'd322)
			  index <= {3{vector_y[3]}};
		  else if (vector_x >= 10'd636 && vector_x < 10'd640)
				if (vector_y >=( bat1_y - 20 ) && vector_y <= (bat1_y + 20) )
					index <= 3'b111;
				else
					index <= 3'b000;
		  else
          		index <= 3'b000;
	else
          index <= 3'b000;
     end
  end
  
  always begin
	if (index == 3'b000) begin
		vga_red = 3'b000;
		vga_green = 3'b000;
		vga_blue = 3'b000;
	end else if (index == 3'b001) begin
		vga_red = sprite_red;
		vga_green= sprite_green;
		vga_blue = sprite_blue;
	end else begin
		vga_red= 3'b111;
		vga_green=3'b111;
		vga_blue=3'b111;
	end
		
  
  end
  
endmodule