cc.v 12.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 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
////////////////////////////////////////////////////////////////////////
//
// Project Reality
//
// module:	cc
// description:	Top level for color combine unit.
//
// designer:	Phil Gossett
// date:	12/13/94
//
////////////////////////////////////////////////////////////////////////

module cc (gclk, st_span, ncyc,
	key_en, texture_edge_mode, cvg_times_alpha, alpha_cvg_select,
	cc_x_sel_0_r, cc_y_sel_0_r, cc_a_sel_0_r, cc_c_sel_0_r,
	cc_x_sel_0_a, cc_y_sel_0_a, cc_a_sel_0_a, cc_c_sel_0_a,
	cc_x_sel_1_r, cc_y_sel_1_r, cc_a_sel_1_r, cc_c_sel_1_r,
	cc_x_sel_1_a, cc_y_sel_1_a, cc_a_sel_1_a, cc_c_sel_1_a,
	st_r, st_g, st_b, st_a,
	tf_r, tf_g, tf_b, tf_a,
	tf_lod_frac, tf_lge1, prim_lod_frac,
	prim_r, prim_g, prim_b, prim_a,
	env_r, env_g, env_b, env_a,
	center_r, center_g, center_b,
	scale_r, scale_g, scale_b,
	width_r, width_g, width_b,
	k4_coeff, k5_coeff,
	noise, cvg, dither_en,
	shade_a, pixel_r, pixel_g, pixel_b, pixel_a, pixel_cvg);

input gclk;
input st_span;
input ncyc;
input key_en;
input texture_edge_mode;
input cvg_times_alpha;
input alpha_cvg_select;
input [3:0] cc_x_sel_0_r;
input [3:0] cc_y_sel_0_r;
input [4:0] cc_a_sel_0_r;
input [2:0] cc_c_sel_0_r;
input [2:0] cc_x_sel_0_a;
input [2:0] cc_y_sel_0_a;
input [2:0] cc_a_sel_0_a;
input [2:0] cc_c_sel_0_a;
input [3:0] cc_x_sel_1_r;
input [3:0] cc_y_sel_1_r;
input [4:0] cc_a_sel_1_r;
input [2:0] cc_c_sel_1_r;
input [2:0] cc_x_sel_1_a;
input [2:0] cc_y_sel_1_a;
input [2:0] cc_a_sel_1_a;
input [2:0] cc_c_sel_1_a;
input [7:0] st_r;
input [7:0] st_g;
input [7:0] st_b;
input [7:0] st_a;
input [8:0] tf_r;
input [8:0] tf_g;
input [8:0] tf_b;
input [8:0] tf_a;
input [8:0] tf_lod_frac;
input tf_lge1;
input [8:0] prim_lod_frac;
input [7:0] prim_r;
input [7:0] prim_g;
input [7:0] prim_b;
input [7:0] prim_a;
input [7:0] env_r;
input [7:0] env_g;
input [7:0] env_b;
input [7:0] env_a;
input [7:0] center_r;
input [7:0] center_g;
input [7:0] center_b;
input [7:0] scale_r;
input [7:0] scale_g;
input [7:0] scale_b;
input [11:0] width_r;
input [11:0] width_g;
input [11:0] width_b;
input [8:0] k4_coeff;
input [8:0] k5_coeff;
input [8:0] noise;
input [3:0] cvg;
input dither_en;

output [7:0] shade_a;
output [7:0] pixel_r;
output [7:0] pixel_g;
output [7:0] pixel_b;
output [8:0] pixel_a;	// 1.8
output [3:0] pixel_cvg;	// 1.3

reg force_cycle;
reg pre_cycle;

reg [8:0] tf_reg_r;
reg [8:0] tf_reg_g;
reg [8:0] tf_reg_b;
reg [8:0] tf_reg_a;
reg [8:0] lod_frac;
reg lge1;

reg shad_cycle;

reg [3:0] cc_x_sel_reg_rgb;
reg [3:0] cc_y_sel_reg_rgb;
reg [4:0] cc_a_sel_reg_rgb;
reg [2:0] cc_c_sel_reg_rgb;
reg [2:0] cc_x_sel_reg_alf;
reg [2:0] cc_y_sel_reg_alf;
reg [2:0] cc_a_sel_reg_alf;
reg [2:0] cc_c_sel_reg_alf;

wire [8:0]  r_x;	// positive subtract input
wire [8:0]  g_x;
wire [8:0]  b_x;
wire [8:0]  a_x;
wire [8:0]  r_y;	// negative subtract input
wire [8:0]  g_y;
wire [8:0]  b_y;
wire [8:0]  a_y;
wire [8:0]  r_a;	// multiply input
wire [8:0]  g_a;
wire [8:0]  b_a;
wire [8:0]  a_a;
wire [8:0]  r_c;	// final lerp add
wire [8:0]  g_c;
wire [8:0]  b_c;
wire [8:0]  a_c;

wire [16:0] r_mp;	// final carry-propagated sum
wire [16:0] g_mp;
wire [16:0] b_mp;
wire [16:0] a_mp;

reg key_cycle;
reg [2:0] lod_frac_reg;
reg lge1_reg;
reg [7:0] st_reg_a;	// delay for blend
reg [16:0] cc_r;	// registered color combine loopback
reg [16:0] cc_g;
reg [16:0] cc_b;
reg [8:0] cc_a;
reg [8:0]  r_x_reg;	// registered positive subtract input
reg [8:0]  g_x_reg;
reg [8:0]  b_x_reg;

wire [15:0] absn_r;
wire [15:0] absn_g;
wire [15:0] absn_b;
wire sgn_r;
wire sgn_g;
wire sgn_b;
wire inco_r;
wire inco_g;
wire inco_b;
wire [16:0] offs_r;
wire [16:0] offs_g;
wire [16:0] offs_b;
wire max1;
wire lfg;
wire [7:0] clamp_a;
wire [8:0] dither_a;
wire [8:0] dither_st_a;
wire [4:0] lfrac;
wire [4:0] p0p;
wire [4:0] p1p;
wire [3:0] p2p;
wire c2c;
wire [5:0] mp;
wire [3:0] d;
wire [3:0] cvg_alpha;
wire [7:0] clmp_r;
wire [7:0] clmp_g;
wire [7:0] clmp_b;
wire rltg;
wire gltb;
wire bltr;
wire [7:0] key_mux;
wire key_sel;
wire [7:0] key_a;
wire [8:0] alpha_a;
wire [3:0] alpha_cvg;
wire cvg_alpha_enable;

reg [8:0] key_r;
reg [8:0] key_g;
reg [8:0] key_b;
reg [7:0] clamp_r;
reg [7:0] clamp_g;
reg [7:0] clamp_b;

reg [7:0] shade_a;
reg [7:0] pixel_r;
reg [7:0] pixel_g;
reg [7:0] pixel_b;
reg [8:0] pixel_a;	// 1.8
reg [3:0] pixel_cvg;	// 1.3

always @(posedge gclk)
begin
	force_cycle = st_span || pre_cycle;
	pre_cycle <= !(force_cycle && ncyc);
end

always @(posedge gclk)
begin
	shad_cycle <= pre_cycle;

	cc_x_sel_reg_rgb <= pre_cycle ? cc_x_sel_1_r : cc_x_sel_0_r;
	cc_y_sel_reg_rgb <= pre_cycle ? cc_y_sel_1_r : cc_y_sel_0_r;
	cc_a_sel_reg_rgb <= pre_cycle ? cc_a_sel_1_r : cc_a_sel_0_r;
	cc_c_sel_reg_rgb <= pre_cycle ? cc_c_sel_1_r : cc_c_sel_0_r;
	cc_x_sel_reg_alf <= pre_cycle ? cc_x_sel_1_a : cc_x_sel_0_a;
	cc_y_sel_reg_alf <= pre_cycle ? cc_y_sel_1_a : cc_y_sel_0_a;
	cc_a_sel_reg_alf <= pre_cycle ? cc_a_sel_1_a : cc_a_sel_0_a;
	cc_c_sel_reg_alf <= pre_cycle ? cc_c_sel_1_a : cc_c_sel_0_a;

	lod_frac <= tf_lod_frac;
	lge1 <= tf_lge1;

	tf_reg_r <= tf_r;
	tf_reg_g <= tf_g;
	tf_reg_b <= tf_b;
	tf_reg_a <= tf_a;
end

// don't optimize the following instanced stuff:
cc_mxxr muxxr (.s(cc_x_sel_reg_rgb),	.i0i(cc_r[16:8]),
					.i1i(tf_reg_r),
					.i2i(tf_r),
					.i3i({1'b0,prim_r}),
					.i4i({1'b0,st_r}),
					.i5i({1'b0,env_r}),
					.i6i(9'b100000000),
					.i7i(noise),		.z(r_x));
cc_mxyr muxyr (.s(cc_y_sel_reg_rgb),	.i0i(cc_r[16:8]),
					.i1i(tf_reg_r),
					.i2i(tf_r),
					.i3i({1'b0,prim_r}),
					.i4i({1'b0,st_r}),
					.i5i({1'b0,env_r}),
					.i6i({1'b0,center_r}),
					.i7i(k4_coeff),		.z(r_y));
cc_mxar muxar (.s(cc_a_sel_reg_rgb),	.i0i(cc_r[16:8]),
					.i1i(tf_reg_r),
					.i2i(tf_r),
					.i3i({1'b0,prim_r}),
					.i4i({1'b0,st_r}),
					.i5i({1'b0,env_r}),
					.i6i({1'b0,scale_r}),
					.i7i(cc_a),
					.i8i(tf_reg_a),
					.i9i(tf_a),
					.iai({1'b0,prim_a}),
					.ibi({1'b0,st_a}),
					.ici({1'b0,env_a}),
					.idi(lod_frac),
					.iei(prim_lod_frac),
					.ifi(k5_coeff),		.z(r_a));
cc_mxcr muxcr (.s(cc_c_sel_reg_rgb),	.i0i(cc_r[16:8]),
					.i1i(tf_reg_r),
					.i2i(tf_r),
					.i3i({1'b0,prim_r}),
					.i4i({1'b0,st_r}),
					.i5i({1'b0,env_r}),
					.i6i(9'b100000000),
					.i7i(9'b000000000),	.z(r_c));

cc_mxxr muxxg (.s(cc_x_sel_reg_rgb),	.i0i(cc_g[16:8]),
					.i1i(tf_reg_g),
					.i2i(tf_g),
					.i3i({1'b0,prim_g}),
					.i4i({1'b0,st_g}),
					.i5i({1'b0,env_g}),
					.i6i(9'b100000000),
					.i7i(noise),		.z(g_x));
cc_mxyr muxyg (.s(cc_y_sel_reg_rgb),	.i0i(cc_g[16:8]),
					.i1i(tf_reg_g),
					.i2i(tf_g),
					.i3i({1'b0,prim_g}),
					.i4i({1'b0,st_g}),
					.i5i({1'b0,env_g}),
					.i6i({1'b0,center_g}),
					.i7i(k4_coeff),		.z(g_y));
cc_mxar muxag (.s(cc_a_sel_reg_rgb),	.i0i(cc_g[16:8]),
					.i1i(tf_reg_g),
					.i2i(tf_g),
					.i3i({1'b0,prim_g}),
					.i4i({1'b0,st_g}),
					.i5i({1'b0,env_g}),
					.i6i({1'b0,scale_g}),
					.i7i(cc_a),
					.i8i(tf_reg_a),
					.i9i(tf_a),
					.iai({1'b0,prim_a}),
					.ibi({1'b0,st_a}),
					.ici({1'b0,env_a}),
					.idi(lod_frac),
					.iei(prim_lod_frac),
					.ifi(k5_coeff),		.z(g_a));
cc_mxcr muxcg (.s(cc_c_sel_reg_rgb),	.i0i(cc_g[16:8]),
					.i1i(tf_reg_g),
					.i2i(tf_g),
					.i3i({1'b0,prim_g}),
					.i4i({1'b0,st_g}),
					.i5i({1'b0,env_g}),
					.i6i(9'b100000000),
					.i7i(9'b000000000),	.z(g_c));

cc_mxxr muxxb (.s(cc_x_sel_reg_rgb),	.i0i(cc_b[16:8]),
					.i1i(tf_reg_b),
					.i2i(tf_b),
					.i3i({1'b0,prim_b}),
					.i4i({1'b0,st_b}),
					.i5i({1'b0,env_b}),
					.i6i(9'b100000000),
					.i7i(noise),		.z(b_x));
cc_mxyr muxyb (.s(cc_y_sel_reg_rgb),    .i0i(cc_b[16:8]),
                                        .i1i(tf_reg_b),
                                        .i2i(tf_b),
                                        .i3i({1'b0,prim_b}),
                                        .i4i({1'b0,st_b}),
                                        .i5i({1'b0,env_b}),
                                        .i6i({1'b0,center_b}),
                                        .i7i(k4_coeff),         .z(b_y));
cc_mxar muxab (.s(cc_a_sel_reg_rgb),	.i0i(cc_b[16:8]),
					.i1i(tf_reg_b),
					.i2i(tf_b),
					.i3i({1'b0,prim_b}),
					.i4i({1'b0,st_b}),
					.i5i({1'b0,env_b}),
					.i6i({1'b0,scale_b}),
					.i7i(cc_a),
					.i8i(tf_reg_a),
					.i9i(tf_a),
					.iai({1'b0,prim_a}),
					.ibi({1'b0,st_a}),
					.ici({1'b0,env_a}),
					.idi(lod_frac),
					.iei(prim_lod_frac),
					.ifi(k5_coeff),		.z(b_a));
cc_mxcr muxcb (.s(cc_c_sel_reg_rgb),	.i0i(cc_b[16:8]),
					.i1i(tf_reg_b),
					.i2i(tf_b),
					.i3i({1'b0,prim_b}),
					.i4i({1'b0,st_b}),
					.i5i({1'b0,env_b}),
					.i6i(9'b100000000),
					.i7i(9'b000000000),	.z(b_c));

cc_mxxa muxxa (.s(cc_x_sel_reg_alf),	.i0i(cc_a),
					.i1i(tf_reg_a),
					.i2i(tf_a),
					.i3i({1'b0,prim_a}),
					.i4i({1'b0,st_a}),
					.i5i({1'b0,env_a}),
					.i6i(9'b100000000),
					.i7i(9'b000000000),	.z(a_x));
cc_mxya muxya (.s(cc_y_sel_reg_alf),	.i0i(cc_a),
					.i1i(tf_reg_a),
					.i2i(tf_a),
					.i3i({1'b0,prim_a}),
					.i4i({1'b0,st_a}),
					.i5i({1'b0,env_a}),
					.i6i(9'b100000000),
					.i7i(9'b000000000),	.z(a_y));
cc_mxaa muxaa (.s(cc_a_sel_reg_alf),	.i0i(lod_frac),
					.i1i(tf_reg_a),
					.i2i(tf_a),
					.i3i({1'b0,prim_a}),
					.i4i({1'b0,st_a}),
					.i5i({1'b0,env_a}),
					.i6i(prim_lod_frac),
					.i7i(9'b000000000),	.z(a_a));
cc_mxca muxca (.s(cc_c_sel_reg_alf),	.i0i(cc_a),
					.i1i(tf_reg_a),
					.i2i(tf_a),
					.i3i({1'b0,prim_a}),
					.i4i({1'b0,st_a}),
					.i5i({1'b0,env_a}),
					.i6i(9'b100000000),
					.i7i(9'b000000000),	.z(a_c));

cc_lerp cclerpr (.x(r_x), .y(r_y), .a(r_a), .c(r_c), .mp(r_mp));
cc_lerp cclerpg (.x(g_x), .y(g_y), .a(g_a), .c(g_c), .mp(g_mp));
cc_lerp cclerpb (.x(b_x), .y(b_y), .a(b_a), .c(b_c), .mp(b_mp));
cc_lerp cclerpa (.x(a_x), .y(a_y), .a(a_a), .c(a_c), .mp(a_mp));

always @(posedge gclk)
begin
	key_cycle <= shad_cycle;
	st_reg_a <= st_a;

	lod_frac_reg <= lod_frac[7:5];
	lge1_reg <= lge1;

	cc_r <= r_mp;
	cc_g <= g_mp;
	cc_b <= b_mp;
	cc_a <= a_mp[16:8];

	r_x_reg <= r_x;
	g_x_reg <= g_x;
	b_x_reg <= b_x;
end

// don't optimize the following instanced stuff:
cc_abs16n absr (.a(cc_r), .sn(absn_r));
cc_abs16n absg (.a(cc_g), .sn(absn_g));
cc_abs16n absb (.a(cc_b), .sn(absn_b));

assign sgn_r = ~cc_r[16];
assign sgn_g = ~cc_g[16];
assign sgn_b = ~cc_b[16];

cc_inc4 incr (.a(absn_r[3:0]), .ci(sgn_r), .co(inco_r), .s(offs_r[3:0]));
cc_inc4 incg (.a(absn_g[3:0]), .ci(sgn_g), .co(inco_g), .s(offs_g[3:0]));
cc_inc4 incb (.a(absn_b[3:0]), .ci(sgn_b), .co(inco_b), .s(offs_b[3:0]));

cc_add12 addr (	.a(absn_r[15:4]), .b(width_r), .ci(inco_r),
		.co(offs_r[16]), .s(offs_r[15:4]));
cc_add12 addg (	.a(absn_g[15:4]), .b(width_g), .ci(inco_g),
		.co(offs_g[16]), .s(offs_g[15:4]));
cc_add12 addb (	.a(absn_b[15:4]), .b(width_b), .ci(inco_b),
		.co(offs_b[16]), .s(offs_b[15:4]));

cc_edge_ctrl tectrl (	.texture_edge_mode(texture_edge_mode),
			.cvg_times_alpha(cvg_times_alpha),
			.alpha_cvg_select(alpha_cvg_select),
			.a(cc_a),
			.lod_frac({lge1_reg,~lge1_reg,lod_frac_reg}),
			.max1(max1), .lfg(lfg),
			.cvg_alpha_enable(cvg_alpha_enable),
			.clamp_a(clamp_a), .lfrac(lfrac));
cc_edge_add teadd (	.max1(max1), .lfg(lfg),
			.a(clamp_a[7:5]), .lfrac(lfrac),
			.d(d));
cc_edge_booth tebooth (	.max1(max1),
			.x(clamp_a[7:5]), .y(lfrac[3:1]),
			.a(cvg),
			.p0p(p0p), .p1p(p1p), .p2p(p2p), .c2c(c2c));
cc_edge_csa tecsa (	.p0p(p0p), .p1p(p1p), .p2p(p2p), .c2c(c2c), .mp(mp));
cc_edge_div tediv (	.max1(max1), .n(mp), .d(d), .q(cvg_alpha));

cc_key_clamp keyclp (	.r(offs_r), .g(offs_g), .b(offs_b),
			.clmp_r(clmp_r), .clmp_g(clmp_g), .clmp_b(clmp_b));
cc_key_gt    keygtr (	.a(clmp_g), .b(clmp_r), .z(rltg));
cc_key_gt    keygtg (	.a(clmp_b), .b(clmp_g), .z(gltb));
cc_key_gt    keygtb (	.a(clmp_r), .b(clmp_b), .z(bltr));
cc_key_mux   keymux (	.rltg(rltg), .gltb(gltb), .bltr(bltr),
			.r(clmp_r), .g(clmp_g), .b(clmp_b), .a(key_mux));

assign dither_a    = {1'b0,clamp_a}  + {6'b0,({3{dither_en}} & noise[8:6])};
assign dither_st_a = {1'b0,st_reg_a} + {6'b0,({3{dither_en}} & noise[8:6])};
assign key_sel     = key_cycle && key_en;
assign key_a       = key_sel ? key_mux : ({8{dither_a[8]}} | dither_a[7:0]);
assign alpha_a     = alpha_cvg_select ? {cvg_alpha,5'b00000} : {1'b0,key_a};
assign alpha_cvg   = cvg_alpha_enable ?  cvg_alpha           : cvg;

always @(posedge gclk)
begin
	key_r = key_sel ? r_x_reg : cc_r[16:8];
	key_g = key_sel ? g_x_reg : cc_g[16:8];
	key_b = key_sel ? b_x_reg : cc_b[16:8];

	case (key_r[8:7])
	2'b00: clamp_r = key_r[7:0];
	2'b01: clamp_r = key_r[7:0];
	2'b10: clamp_r = 8'b11111111;
	2'b11: clamp_r = 8'b00000000;
	default: clamp_r = 'bx;
	endcase	

	case (key_g[8:7])
	2'b00: clamp_g = key_g[7:0];
	2'b01: clamp_g = key_g[7:0];
	2'b10: clamp_g = 8'b11111111;
	2'b11: clamp_g = 8'b00000000;
	default: clamp_g = 'bx;
	endcase	

	case (key_b[8:7])
	2'b00: clamp_b = key_b[7:0];
	2'b01: clamp_b = key_b[7:0];
	2'b10: clamp_b = 8'b11111111;
	2'b11: clamp_b = 8'b00000000;
	default: clamp_b = 'bx;
	endcase	

	shade_a   <= {8{dither_st_a[8]}} | dither_st_a[7:0];
	pixel_r   <= clamp_r;
	pixel_g   <= clamp_g;
	pixel_b   <= clamp_b;
	pixel_a   <= alpha_a;
	pixel_cvg <= alpha_cvg;
end

endmodule // cc