From a979554f1f69b1a3a28e0fa38a26736fbab082da Mon Sep 17 00:00:00 2001 From: Alejandro Soto Date: Fri, 10 Nov 2023 21:32:15 -0600 Subject: rtl/gfx: implement fragment address linearization --- rtl/gfx/gfx_frag_addr.sv | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 rtl/gfx/gfx_frag_addr.sv (limited to 'rtl/gfx/gfx_frag_addr.sv') diff --git a/rtl/gfx/gfx_frag_addr.sv b/rtl/gfx/gfx_frag_addr.sv new file mode 100644 index 0000000..9b817cb --- /dev/null +++ b/rtl/gfx/gfx_frag_addr.sv @@ -0,0 +1,59 @@ +`include "gfx/gfx_defs.sv" + +module gfx_frag_addr +( + input logic clk, + + input frag_xy frag, + input logic stall, + + output linear_coord linear +); + + /* frag está expresado en un rango normalizado con igual distribución + * entre positivos y negativos. Para obtener la dirección lineal que le + * corresponde, debemos corregir esto para que el mínimo sea cero en + * cada coordenada. Luego de eso, + * + * linear = y_corregido * `GFX_X_RES + x_corregido + * + * Afortunadamente, esto es trivializable y no necesita FMA: + * + * y * `GFX_X_RES + x + * = y * 640 + x + * = y * 128 * 5 + x + * = ((y * 5) << 7) + x + * = ((y * (4 + 1)) << 7) + x + * = (((y << 2) + y) << 7) + x + * = (y << 9) + (y << 7) + x + * + * Para corregir x ([-320, 319]) se le suma `GFX_RES_X / 2. + * + * Para corregir y ([-240, 239]) se debe tomar en cuenta que las + * direcciones lineales incrementan hacia abajo, así que: + * y_corregido = `GFX_RES_Y / 2 - 1 - y + */ + + localparam ZERO_PAD = $bits(linear_coord) - $bits(xy_coord); + + // Estas constantes asumen `GFX_X_RES == 640 + localparam Y_SHIFT0 = 9, Y_SHIFT1 = 7; + + xy_coord bias_x, bias_y; + linear_coord row_start, x_biased, x_hold, y_biased; + + assign bias_x = `GFX_X_RES / 2; + assign bias_y = `GFX_Y_RES / 2 - 1; + + always_ff @(posedge clk) + if (!stall) begin + x_biased <= {{ZERO_PAD{1'b0}}, frag.x + bias_x}; + y_biased <= {{ZERO_PAD{1'b0}}, bias_y - frag.y}; + + x_hold <= x_biased; + row_start <= (y_biased << Y_SHIFT0) + (y_biased << Y_SHIFT1); + + linear <= row_start + x_hold; + end + +endmodule -- cgit v1.2.3