bl.v 10.9 KB
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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
////////////////////////////////////////////////////////////////////////
//
// Project Reality
//
// module:	bl
// description:	Top level for blend unit.
//
// designer:	Phil Gossett
// date:	6/22/95
//
////////////////////////////////////////////////////////////////////////

module bl (clk, start_gclk, st_span, ncyc,
	z_source_select, antialias_enable,
	z_compare_enable, z_update_enable, z_mode,
	cvg_dest, color_on_cvg, force_blend, mask15b,
	bl_p_sel_0_r, bl_m_sel_0_r, bl_a_sel_0_r, bl_b_sel_0_r,
	bl_p_sel_1_r, bl_m_sel_1_r, bl_a_sel_1_r, bl_b_sel_1_r,
	blend_r, blend_g, blend_b, fog_r, fog_g, fog_b, fog_a,
	pixel_r, pixel_g, pixel_b, pixel_a, pixel_cvg,
	shade_a, st_z, dzdx, dzdy, prim_z, prim_delta_z,
	mem_r, mem_g, mem_b, mem_a, mem_z,
	span_r, span_g, span_b, span_a, span_z,
	span_color_we, span_depth_we);

input clk;
input start_gclk;
input st_span;
input ncyc;
input z_source_select;
input antialias_enable;
input z_compare_enable;
input z_update_enable;
input [1:0] z_mode;
input [1:0] cvg_dest;
input color_on_cvg;
input force_blend;
input mask15b;
input [1:0] bl_p_sel_0_r;
input [1:0] bl_m_sel_0_r;
input [1:0] bl_a_sel_0_r;
input [1:0] bl_b_sel_0_r;
input [1:0] bl_p_sel_1_r;
input [1:0] bl_m_sel_1_r;
input [1:0] bl_a_sel_1_r;
input [1:0] bl_b_sel_1_r;
input [7:0] blend_r;
input [7:0] blend_g;
input [7:0] blend_b;
input [7:0] fog_r;
input [7:0] fog_g;
input [7:0] fog_b;
input [7:0] fog_a;
input [7:0] pixel_r;
input [7:0] pixel_g;
input [7:0] pixel_b;
input [8:0] pixel_a;	// 1.8
input [3:0] pixel_cvg;	// 1.3
input [7:0] shade_a;	// piped thru CC
input [17:0] st_z;	// stepped integer z (0,15.3)
input [15:0] dzdx;	// S.15 attributes
input [15:0] dzdy;
input [15:0] prim_z;	// 16.0
input [15:0] prim_delta_z;
input [7:0] mem_r;	// from mem_span unit
input [7:0] mem_g;
input [7:0] mem_b;
input [2:0] mem_a;
input [17:0] mem_z;	// z and delta from memory (fp format)

output [7:0] span_r;	// to mem_span unit
output [7:0] span_g;
output [7:0] span_b;
output [2:0] span_a;
output [17:0] span_z;	// z and delta to memory (fp format)
output span_color_we;
output span_depth_we;

reg force_cycle;
reg pre_cycle;

wire [1:0] min_exp_delta_z;
wire [3:0] max_old_delta_z;
wire [4:0] inc_old_delta_z;
wire [3:0] adj_old_delta_z;
wire [15:0] pp_new_delta_z; // 16.0
wire [15:0] pp_old_delta_z; // 16.0
wire [17:0] pp_old_z;	// 0,15.3
wire [3:0] norm_delta_z; // log2
wire [2:0] p_norm_a;
wire [2:0] p_norm_b;

reg blend_cycle;
reg reg_z_source_select;
reg delta_z_overflow;
reg [15:0] p_new_delta_z; // 16.0
reg [15:0] p_old_delta_z; // 16.0
reg [17:0] p_old_z;	// 0,15.3
reg [1:0] bl_p_sel_reg;
reg [1:0] bl_a_sel_reg;
reg [1:0] bl_m_sel_reg;
reg [1:0] bl_b_sel_reg;
reg [2:0] norm_a;
reg [2:0] norm_b;
reg [8:0] pixel_a_reg;	// 1.8
reg [3:0] pixel_c_reg;	// 1.3

wire [8:0] pixel_alpha;	// 1.8
wire [15:0] pp_delta_z;	// 16.0
wire [3:0] p_delta_z;	// log2
wire [17:0] p_new_z;	// 0,15.3
wire p_farther;
wire p_nearer;
wire p_infront;
wire [7:0] x_r;
wire [7:0] x_g;
wire [7:0] x_b;
wire [7:0] p_r;
wire [7:0] p_g;
wire [7:0] p_b;
wire [7:0] m_r;
wire [7:0] m_g;
wire [7:0] m_b;
wire [5:0] a;		// 1.5
wire [4:0] b;		// 0.5
wire [5:0] a_norm;
wire [4:0] b_norm;
wire lsen;
wire [10:0] mp_r;	// 0.11
wire [10:0] mp_g;
wire [10:0] mp_b;
wire [3:0] mp_c;	// 1.3

reg [15:0] new_delta_z;	// 16.0
reg [17:0] new_z;	// 0,15.3
reg [17:0] old_z;	// 0,15.3
reg [3:0] delta_z;	// log2
reg max_z;
reg farther;
reg nearer;
reg infront;
reg [3:0] pixel_cvg_reg;
reg [2:0] mem_a_reg;
reg [7:0] p_r_reg;
reg [7:0] p_g_reg;
reg [7:0] p_b_reg;
reg [7:0] m_r_reg;
reg [7:0] m_g_reg;
reg [7:0] m_b_reg;
reg [10:0] bl_r;	// 0.11
reg [10:0] bl_g;
reg [10:0] bl_b;
reg [3:0] bl_c;		// 1.3
reg p_alpha_one;

wire [13:0] p_span_z;	// fp: 11 mant, 3 exp
wire [3:0] p_span_delta_z; //log2
wire [7:0] dv_r;
wire [7:0] dv_g;
wire [7:0] dv_b;
wire [3:0] pixel_cvg_pre2;
wire pre_cvg_wrap;
wire we_cvg;

reg force_blend_reg;
reg blend_en;
reg trans;
reg decal;
reg opaque;
reg pp_span_color_we;
reg pre_span_color_we;
reg [3:0] pixel_cvg_rg2;
reg [2:0] mem_a_rg2;
reg cvg_wrap;
reg [7:0] p_r_rg2;
reg [7:0] p_g_rg2;
reg [7:0] p_b_rg2;
reg [7:0] m_r_rg2;
reg [7:0] m_g_rg2;
reg [7:0] m_b_rg2;
reg [7:0] bl_r_rg2;
reg [7:0] bl_g_rg2;
reg [7:0] bl_b_rg2;
reg [7:0] dv_r_rg2;
reg [7:0] dv_g_rg2;
reg [7:0] dv_b_rg2;
reg span_depth_we;
reg [17:0] span_z;
reg alpha_one;

wire [2:0] pre_span_a;

reg span_color_we;
reg [7:0] span_r;
reg [7:0] span_g;
reg [7:0] span_b;
reg [2:0] span_a;

// pipeline stage -1
always @(posedge clk)
  if (start_gclk) begin
	force_cycle = st_span || pre_cycle;
	pre_cycle <= !(force_cycle || !ncyc);
  end

assign min_exp_delta_z = {2{!mem_z[17]}} & ~mem_z[16:15];
assign max_old_delta_z = (mem_z[3] ||
			  mem_z[2] ||
			 (mem_z[1] && !min_exp_delta_z[1]) ||
			((mem_z[1] ^~  min_exp_delta_z[1]) &&
                         (mem_z[0] && !min_exp_delta_z[0]))) ?
			mem_z[3:0] : {2'b0,min_exp_delta_z};
assign inc_old_delta_z = (!mem_z[17] && !(mem_z[16] && mem_z[15])) ?
			(max_old_delta_z + 1) : max_old_delta_z;
assign adj_old_delta_z = {4{inc_old_delta_z[4]}} | inc_old_delta_z[3:0];

// don't optimize the following instanced stuff:
bl_deltaz bldz (.sel(z_source_select), .prim(prim_delta_z),
		            .dzdx(dzdx), .dzdy(dzdy),
		            .deltaz(pp_new_delta_z));
bl_exp    fexp (.i(adj_old_delta_z), .z(pp_old_delta_z));
bl_dec    fdec (.i(mem_z[17:4]), .z(pp_old_z));
bl_log    flgn (.i(pp_new_delta_z), .z(norm_delta_z));
bl_norm   blnm (.new(norm_delta_z), .old(mem_z[3:0]),
		.norma(p_norm_a), .normb(p_norm_b));

// pipeline stage 0
always @(posedge clk)
  if (start_gclk) begin
	blend_cycle  <= pre_cycle;
	reg_z_source_select <= z_source_select;
	delta_z_overflow <= inc_old_delta_z[4];
	p_new_delta_z <= pp_new_delta_z;
	p_old_delta_z <= pp_old_delta_z;
	p_old_z <= pp_old_z;
	norm_a	<= p_norm_a;
	norm_b	<= p_norm_b;
	bl_p_sel_reg <= pre_cycle ? bl_p_sel_1_r : bl_p_sel_0_r;
	bl_m_sel_reg <= pre_cycle ? bl_m_sel_1_r : bl_m_sel_0_r;
	bl_a_sel_reg <= pre_cycle ? bl_a_sel_1_r : bl_a_sel_0_r;
	bl_b_sel_reg <= pre_cycle ? bl_b_sel_1_r : bl_b_sel_0_r;
	pixel_a_reg <= pixel_a;
	pixel_c_reg <= pixel_cvg;
  end

assign	pixel_alpha = blend_cycle ? pixel_a_reg : pixel_a;

assign	p_new_z = reg_z_source_select ? {prim_z[14:0],3'b000} : st_z;

// don't optimize the following instanced stuff:
bl_max maxz (	.a(p_new_delta_z), .b(p_old_delta_z), .z(pp_delta_z));
bl_log flgd (	.i(pp_delta_z), .z(p_delta_z));
bl_zcomp zcmp (	.old(p_old_z), .new(p_new_z), .delta(pp_delta_z),
		.farther(p_farther), .nearer(p_nearer), .infront(p_infront));

bl_mux8 mxxr (.s(blend_cycle), .i0i(pixel_r), .i1i(bl_r[10:3]), .z(x_r));
bl_mux8 mxxg (.s(blend_cycle), .i0i(pixel_g), .i1i(bl_g[10:3]), .z(x_g));
bl_mux8 mxxb (.s(blend_cycle), .i0i(pixel_b), .i1i(bl_b[10:3]), .z(x_b));

bl_mxpm mxpr (.s(bl_p_sel_reg),	.i0i(x_r),
				.i1i(mem_r),
				.i2i(blend_r),
				.i3i(fog_r),		.z(p_r));
bl_mxpm mxpg (.s(bl_p_sel_reg),	.i0i(x_g),
				.i1i(mem_g),
				.i2i(blend_g),
				.i3i(fog_g),		.z(p_g));
bl_mxpm mxpb (.s(bl_p_sel_reg),	.i0i(x_b),
				.i1i(mem_b),
				.i2i(blend_b),
				.i3i(fog_b),		.z(p_b));

bl_mxpm mxmr (.s(bl_m_sel_reg),	.i0i(x_r),
				.i1i(mem_r),
				.i2i(blend_r),
				.i3i(fog_r),		.z(m_r));
bl_mxpm mxmg (.s(bl_m_sel_reg),	.i0i(x_g),
				.i1i(mem_g),
				.i2i(blend_g),
				.i3i(fog_g),		.z(m_g));
bl_mxpm mxmb (.s(bl_m_sel_reg),	.i0i(x_b),
				.i1i(mem_b),
				.i2i(blend_b),
				.i3i(fog_b),		.z(m_b));

bl_muxa muxa (.s(bl_a_sel_reg),	.i0i(pixel_alpha[8:3]),
				.i1i({1'b0,fog_a[7:3]}),
				.i2i({1'b0,shade_a[7:3]}),
				.i3i(6'b000000),	.z(a));
bl_muxb muxb (.s(bl_b_sel_reg),	.i0n(a[4:0]),	// note this input negated
				.i1i({mem_a,2'b0}),
				.i2i(5'b11111),
				.i3i(5'b00000),		.z(b));

// if mem cvg, ls 2 bits of alphas not enabled, else norms ignored
assign lsen = (bl_b_sel_reg == 2'b01);
bl_nrma nrma (.lsen(lsen), .sh(norm_a), .i(a),		.z(a_norm));
bl_nrmb nrmb (.lsen(lsen), .sh(norm_b), .i(b),		.z(b_norm));

bl_lerp bllerpr (.p(p_r), .m(m_r), .a(a_norm), .b(b_norm), .mp(mp_r));
bl_lerp bllerpg (.p(p_g), .m(m_g), .a(a_norm), .b(b_norm), .mp(mp_g));
bl_lerp bllerpb (.p(p_b), .m(m_b), .a(a_norm), .b(b_norm), .mp(mp_b));

bl_add  bladd   (.a(a_norm[5:2]), .b(b_norm[4:2]),          .s(mp_c));

// pipeline stage 1
always @(posedge clk)
  if (start_gclk) begin
	new_delta_z <= p_new_delta_z;
	delta_z <= p_delta_z;
	old_z <= p_old_z;
	new_z <= p_new_z;
	max_z   <= &p_old_z;
	farther <= p_farther || delta_z_overflow;
	nearer  <= p_nearer  || delta_z_overflow;
	infront <= p_infront;

	pixel_cvg_reg <= blend_cycle ? pixel_c_reg : pixel_cvg;
	mem_a_reg <= mem_a;

	p_r_reg <= p_r;
	p_g_reg <= p_g;
	p_b_reg <= p_b;

	m_r_reg <= m_r;
	m_g_reg <= m_g;
	m_b_reg <= m_b;

	bl_r    <= mp_r;
	bl_g    <= mp_g;
	bl_b    <= mp_b;

	bl_c    <= mp_c;

	p_alpha_one <= (bl_a_sel_reg == 2'b0) && (bl_b_sel_reg == 2'b0) &&
		(pixel_alpha[8] || (pixel_alpha[7:0] == 8'b11111111));
  end

// don't optimize the following instanced stuff:
bl_log  flgs (.i(new_delta_z), .z(p_span_delta_z));
bl_enc  fenc (.i(new_z), .z(p_span_z));

bl_cvg2 blcvg2  (.pcvg(pixel_cvg_reg), .mcvg(mem_a_reg),
		 .newz(new_z), .oldz(old_z), .deltaz(delta_z),
		 .zmode(z_mode), .infront(infront), .farther(farther),
		 .pout(pixel_cvg_pre2), .wrap(pre_cvg_wrap), .we(we_cvg));

bl_div bldivr (.n(bl_r), .d(bl_c), .q(dv_r));
bl_div bldivg (.n(bl_g), .d(bl_c), .q(dv_g));
bl_div bldivb (.n(bl_b), .d(bl_c), .q(dv_b));

// pipeline stage 2
always @(posedge clk)
  if (start_gclk) begin
	force_blend_reg <= force_blend;
	blend_en <= force_blend ||
			(farther && !pre_cvg_wrap && antialias_enable);
	trans  = !z_mode[0] && z_mode[1] && (max_z | infront);
	decal  =  z_mode[0] && z_mode[1] && !max_z && nearer && farther;
	opaque =              !z_mode[1] && (max_z |
					(pre_cvg_wrap ? infront : nearer));
	pp_span_color_we = (antialias_enable ? we_cvg : mask15b) &&
			(!z_compare_enable || trans || decal || opaque);
	pre_span_color_we <= pp_span_color_we;

	pixel_cvg_rg2 <= pixel_cvg_pre2;
	mem_a_rg2 <= mem_a_reg;
	cvg_wrap <= pre_cvg_wrap;

	p_r_rg2  <= p_r_reg;
	p_g_rg2  <= p_g_reg;
	p_b_rg2  <= p_b_reg;

	m_r_rg2  <= m_r_reg;
	m_g_rg2  <= m_g_reg;
	m_b_rg2  <= m_b_reg;

	bl_r_rg2 <= bl_r[10:3];
	bl_g_rg2 <= bl_g[10:3];
	bl_b_rg2 <= bl_b[10:3];

	dv_r_rg2 <= dv_r;
	dv_g_rg2 <= dv_g;
	dv_b_rg2 <= dv_b;

	span_z <= {p_span_z,p_span_delta_z};
	span_depth_we <= pp_span_color_we && z_update_enable;

	alpha_one <= p_alpha_one;
  end

// don't optimize the following instanced stuff:
bl_cvg3 blcvg3  (.pcvg(pixel_cvg_rg2), .mcvg(mem_a_rg2),
		 .dest(cvg_dest), .en(blend_en),
		 .alpha(pre_span_a));

// pipeline stage 3
always @(posedge clk)
  if (start_gclk) begin
	span_r <= (color_on_cvg && !cvg_wrap) ?
			m_r_rg2 :
			((blend_en && !alpha_one) ?
				(force_blend_reg ? bl_r_rg2 : dv_r_rg2) :
				p_r_rg2);
	span_g <= (color_on_cvg && !cvg_wrap) ?
			m_g_rg2 :
			((blend_en && !alpha_one) ?
				(force_blend_reg ? bl_g_rg2 : dv_g_rg2) :
				p_g_rg2);
	span_b <= (color_on_cvg && !cvg_wrap) ?
			m_b_rg2 :
			((blend_en && !alpha_one) ?
				(force_blend_reg ? bl_b_rg2 : dv_b_rg2) :
				p_b_rg2);
	span_a <= pre_span_a;
	span_color_we <= pre_span_color_we;
  end

endmodule // bl