summaryrefslogtreecommitdiff
path: root/rtl/gfx/gfx_fix_vertex.sv
blob: 728f3b675642300aaabb5d46ce07874dcc6dcb27 (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
`include "gfx/gfx_defs.sv"

module gfx_fix_vertex
(
	input  logic       clk,

	input  vec4        in_vertex,
	input  logic       stall,

	output raster_xyzw out_vertex
);

	fixed x, y;
	raster_xyzw fixed_vertex, corrected;
	fixed[`FLOATS_PER_VEC - 1:0] fixed_vals, corrected_vals, skid_vals;

	assign out_vertex = skid_vals;
	assign fixed_vertex = fixed_vals;
	assign corrected_vals = corrected;

	assign x = fixed_vertex.xy.x;
	assign y = fixed_vertex.xy.y;

	genvar i;
	generate
		for (i = 0; i < `FLOATS_PER_VEC; ++i) begin: components
			gfx_fp_fix fix
			(
				.in(in_vertex[i]),
				.out(fixed_vals[i]),
				.*
			);

			gfx_skid_buf #(.WIDTH($bits(fixed))) skid
			(
				.in(corrected_vals[i]),
				.out(skid_vals[i]),
				.*
			);
		end
	endgenerate

	always_ff @(posedge clk)
		if (!stall) begin
			/*   x * `GFX_X_RES / 2
			 * = x * 320
			 * = x * 64 * 5
			 * = (x * 5) << 6
			 * = (x * (4 + 1)) << 6
			 * = ((x << 2) + x) << 6
			 *
			 *   y * `GFX_Y_RES / 2
			 * = y * 240
			 * = y * 16 * 15
			 * = (y * 15) << 4
			 * = (y * (16 - 1)) << 4
			 * = ((y << 4) - y) << 4
			 */
			corrected.zw <= fixed_vertex.zw;
			corrected.xy.x <= ((x << 2) + x) << 6;
			corrected.xy.y <= ((y << 4) - y) << 4;
		end

endmodule