clip.s 13.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 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802

	#
	# Test program for clipping code.
	#

#include "../rsp.h"
#include "../gfx_dmem.h"
#include "../gfx_regs.h"
#include "mbi.h"

	#
	# define registers here
	#

.name	vertex1,	$1
.name	vertex2,	$2
.name	vertex3,	$3
.name	cc_clip,	$4
.name	num_planes,	$5
.name	plane,		$6
.name	one,		$7
.name	flag,		$8
.name	temp,		$9
	# $10 not used
.name	i,		$11
.name	n,		$12
.name	os,		$13
.name	on,		$14
.name	axis,		$15
.name	previous,	$16
.name	inside,		$17
.name	last_in,	$18
.name	in,		$19
.name	out,		$20
.name	wi,		$21
.name	wo,		$22
.name	wone,		$23
.name	pi,		$24
.name	po,		$25
.name	num,		$26
.name	den,		$27

	#
	# base address of code
	#

.base   0x10002000

	#
	# Setup some registers
	#

	lui	dmembase, 0x1000

	#
	# This next section does what should already have been done for me
	#

	ori	vertex1, dmembase, 0
	ori	vertex2, dmembase, 32
	ori	vertex3, dmembase, 64
	or	cc_clip, zero, zero

	addi	$1, vertex1, RSP_POINTS_OFFSET
	jal	gen_outcodes
	nop
	or	cc_clip, cc_clip, vertex1

	addi	$1, vertex2, RSP_POINTS_OFFSET
	jal	gen_outcodes
	nop
	or	cc_clip, cc_clip, $1

	addi	$1, vertex3, RSP_POINTS_OFFSET
	jal	gen_outcodes
	nop
	or	cc_clip, cc_clip, $1

	ori	vertex1, dmembase, 0

clip:
	#
	# Clip
	#
	# Parameters:
	#
	#   $1 = Vertex number 1 (address of, minus points offset)
	#   $2 = Vertex number 2
	#   $3 = Vertex number 3
	#   $4 = Logical-or of all outcodes for three vertices
	#

	# int	cc_clip, s, n, i, j, plane, flag, axis, inside, last_in;
	# int	on, os;
	# int	in, out;
	# i32	d, pi, po, wi, wo, wone;

	#
	# If the outcode is zero, then the whole triangle is inside the
	# culling box.
	#

	bne	zero, cc_clip, do_clip
	nop

	#
	# XXX Maybe here AND all of the outcodes, and if any bit is 1, then
	# the triangle is to be culled entirely.
	#

	#
	# Pass the triangle in parameters $1, $2, and $3 to the triangle
	# rasterization routine.
	#

	break

do_clip:
	# temp[0] = vert[v1];
	lw	temp, RSP_POINTS_OFFSET(vertex1)
	sw	temp, RSP_SCRATCH_OFFSET(vertex1)
	lw	temp, eval(RSP_POINTS_OFFSET+4)(vertex1)
	sw	temp, eval(RSP_SCRATCH_OFFSET+4)(vertex1)
	lw	temp, eval(RSP_POINTS_OFFSET+8)(vertex1)
	sw	temp, eval(RSP_SCRATCH_OFFSET+8)(vertex1)
	lw	temp, eval(RSP_POINTS_OFFSET+12)(vertex1)
	sw	temp, eval(RSP_SCRATCH_OFFSET+12)(vertex1)
	lw	temp, eval(RSP_POINTS_OFFSET+16)(vertex1)
	sw	temp, eval(RSP_SCRATCH_OFFSET+16)(vertex1)
	lw	temp, eval(RSP_POINTS_OFFSET+20)(vertex1)
	sw	temp, eval(RSP_SCRATCH_OFFSET+20)(vertex1)
	lw	temp, eval(RSP_POINTS_OFFSET+24)(vertex1)
	sw	temp, eval(RSP_SCRATCH_OFFSET+24)(vertex1)
	lw	temp, eval(RSP_POINTS_OFFSET+28)(vertex1)
	sw	temp, eval(RSP_SCRATCH_OFFSET+28)(vertex1)

	# temp[1] = vert[v2];
	lw	temp, RSP_POINTS_OFFSET(vertex2)
	sw	temp, RSP_SCRATCH_OFFSET(vertex2)
	lw	temp, eval(RSP_POINTS_OFFSET+4)(vertex2)
	sw	temp, eval(RSP_SCRATCH_OFFSET+4)(vertex2)
	lw	temp, eval(RSP_POINTS_OFFSET+8)(vertex2)
	sw	temp, eval(RSP_SCRATCH_OFFSET+8)(vertex2)
	lw	temp, eval(RSP_POINTS_OFFSET+12)(vertex2)
	sw	temp, eval(RSP_SCRATCH_OFFSET+12)(vertex2)
	lw	temp, eval(RSP_POINTS_OFFSET+16)(vertex2)
	sw	temp, eval(RSP_SCRATCH_OFFSET+16)(vertex2)
	lw	temp, eval(RSP_POINTS_OFFSET+20)(vertex2)
	sw	temp, eval(RSP_SCRATCH_OFFSET+20)(vertex2)
	lw	temp, eval(RSP_POINTS_OFFSET+24)(vertex2)
	sw	temp, eval(RSP_SCRATCH_OFFSET+24)(vertex2)
	lw	temp, eval(RSP_POINTS_OFFSET+28)(vertex2)
	sw	temp, eval(RSP_SCRATCH_OFFSET+28)(vertex2)

	# temp[2] = vert[v3];
	lw	temp, RSP_POINTS_OFFSET(vertex3)
	sw	temp, RSP_SCRATCH_OFFSET(vertex3)
	lw	temp, eval(RSP_POINTS_OFFSET+4)(vertex3)
	sw	temp, eval(RSP_SCRATCH_OFFSET+4)(vertex3)
	lw	temp, eval(RSP_POINTS_OFFSET+8)(vertex3)
	sw	temp, eval(RSP_SCRATCH_OFFSET+8)(vertex3)
	lw	temp, eval(RSP_POINTS_OFFSET+12)(vertex3)
	sw	temp, eval(RSP_SCRATCH_OFFSET+12)(vertex3)
	lw	temp, eval(RSP_POINTS_OFFSET+16)(vertex3)
	sw	temp, eval(RSP_SCRATCH_OFFSET+16)(vertex3)
	lw	temp, eval(RSP_POINTS_OFFSET+20)(vertex3)
	sw	temp, eval(RSP_SCRATCH_OFFSET+20)(vertex3)
	lw	temp, eval(RSP_POINTS_OFFSET+24)(vertex3)
	sw	temp, eval(RSP_SCRATCH_OFFSET+24)(vertex3)
	lw	temp, eval(RSP_POINTS_OFFSET+28)(vertex3)
	sw	temp, eval(RSP_SCRATCH_OFFSET+28)(vertex3)

	or	i, dmembase, zero		# start vertex of input (ptr)
	ori	n, dmembase, 96			# num of verts in input (*32)

	ori	one, zero, 1			# constant 1 (useful)
	ori	num_planes, zero, 6		# upper bound of plane loop
	or	plane, zero, zero		# index for "plane" loop

	#
	# for (plane = 0; plane < 6; plane++) {
	#

plane_loop:

	#
	# 	flag = 1 << plane;
	#

	sllv	flag, one, plane

	#
	# 	if (!(flag & cc_clip)) {
	# 		/* This plane not cut by anyone */
	# 		continue;
	# 	}
	#

	and	temp, flag, cc_clip
	beq	temp, zero, plane_continue
	nop

	#
	# 	cc_clip = 0;		/* We'll rebuild this from scratch */
	#

	or	cc_clip, zero, zero

	#
	# 	axis = plane >> 1;	/* Which axis we're dealing with   */
	#

	# Actually, the axis is kept as 0, 2, or 4 for x, y, and z.

	andi	axis, plane, 6

	#
	# 	j = s + n - 1;		/* Previous vertex		   */
	#

	addi	previous, n, -32

	#
	# 	inside = !(temp[j].cc & flag); /* If last vertex is inside */
	#

	lb	inside, eval(RSP_SCRATCH_OFFSET+24)(previous)
	and	inside, inside, flag
	sltiu	inside, inside, 1

	#
	# 	os = 9 - s;
	#

	# This works because i is either 288 or 0:

	xori	os, i, 288		# 288 = 9 * 32

	#
	# 	on = os;
	#

	or	on, os, os

	#
	# 	for (i = s; i < s + n; i++) {
	#

vertex_loop:

	#
	# 		last_in = inside;
	#

	or	last_in, inside, inside

	#
	# 		inside = !(temp[i].cc & flag);
	#

	lb	inside, eval(RSP_SCRATCH_OFFSET+24)(i)
	and	inside, inside, flag
	sltiu	inside, inside, 1

	#
	# 		if (inside ^ last_in) { /* We've crossed the plane */
	#

	xor	temp, inside, last_in
	beq	temp, zero, not_crossed
	nop

	#
	# 			/* Always clip in-to-out so shared edges have
	# 			   the same vertex. */
	# 			if (inside) {
	#

	# else clause first:
	or	in, previous, previous
	or	out, i, i

	beq	inside, zero, outside
	nop

	#
	# 				in = i;
	# 				out = j;
	#

	or	in, i, i
	or	out, previous, previous

	# 			} else {
	# 				in = j;
	# 				out = i;
	# 			}
	#

outside:
	# (done before the if above)

	#
	# 			wi = MKFIX (temp[in].wint, temp[in].wfrac);
	# 			wo = MKFIX (temp[out].wint, temp[out].wfrac);
	#

	lhu	wi, eval(RSP_SCRATCH_OFFSET+6)(in)
	sll	wi, wi, 16
	lhu	temp, eval(RSP_SCRATCH_OFFSET+14)(in)
	or	wi, wi, temp

	lhu	wo, eval(RSP_SCRATCH_OFFSET+6)(out)
	sll	wo, wo, 16
	lhu	temp, eval(RSP_SCRATCH_OFFSET+14)(out)
	or	wo, wo, temp

	#
	# 			wone = 1 << SHIFT;
	#

	sll	wone, one, 16

	#
	# 			switch (axis) {
	# 				case 0: pi = MKFIX (temp[in].xint,
	# 						temp[in].xfrac);
	# 					po = MKFIX (temp[out].xint,
	# 						temp[out].xfrac);
	# 					break;	
	# 				case 1: pi = MKFIX (temp[in].yint,
	# 						temp[in].yfrac);
	# 					po = MKFIX (temp[out].yint,
	# 						temp[out].yfrac);
	# 					break;	
	# 				case 2: pi = MKFIX (temp[in].zint,
	# 						temp[in].zfrac);
	# 					po = MKFIX (temp[out].zint,
	# 						temp[out].zfrac);
	# 					break;	
	# 			}
	#

	# Entire switch statement above replaced with this:

	add	temp, axis, in
	lhu	pi, RSP_SCRATCH_OFFSET(temp)
	sll	pi, pi, 16
	add	temp, axis, in
	lhu	temp, eval(RSP_SCRATCH_OFFSET+8)(temp)
	or	pi, pi, temp

	add	temp, axis, out
	lhu	po, RSP_SCRATCH_OFFSET(temp)
	sll	po, po, 16
	add	temp, axis, out
	lhu	temp, eval(RSP_SCRATCH_OFFSET+8)(temp)
	or	po, po, temp

	#
	# 			if (!(plane & 1)) { /* Negative w plane	   */
	#

	andi	temp, plane, 1
	bne	temp, zero, not_negative_w
	nop

	#
	# 				wi = -wi;
	# 				wo = -wo;
	# 				wone = -1 << SHIFT;
	#

	subu	wi, zero, wi
	subu	wo, zero, wo
	subu	wone, zero, wone

	#
	# 			}
	#

not_negative_w:

	#
	#			d = Div (wi - pi - wone, po - pi - wo + wi);
	#

	subu	num, wi, pi
	subu	num, num, wone

	subu	den, po, pi
	subu	den, den, wo
	add	den, den, wi

	#
	# Send them to the vector unit
	#

	mtc2	den, $v1
	sra	den, den, 16
	mtc2	den, $v0

	#
	# XXX: This next nop is necessary for the simulator to seperate
	#  the above scalar instruction with the vector instruction below.
	#  Remove it for the hardware version.
	#

	nop

	#
	# Find reciprocal
	#

	vrcpdh  $v0, $v0
	vrcpdl  $v1, $v1

	#
	# Mult by Numerator
	#

	mtc2	num, $v3
	sra	num, num, 16
	mtc2	num, $v2

	vmudl	$v12, $v1, $v3[0]
	vmadm	$v12, $v0, $v3[0]
	vmadn	$v12, $v1, $v2[0]
	vmadh	$v11, $v0, $v2[0]
	vmadn	$v12, $v0, $v20[0]  #XXX should be zero

	# 			pi = MKFIX (temp[in].xint, temp[in].xfrac);
	# 			po = MKFIX (temp[out].xint, temp[out].xfrac);
	# 			pi = Mult (po - pi, d) + pi;
	# 			temp[on].xint = pi >> SHIFT;
	# 			temp[on].xfrac = pi & MASK;

	# 			pi = MKFIX (temp[in].yint, temp[in].yfrac);
	# 			po = MKFIX (temp[out].yint, temp[out].yfrac);
	# 			pi = Mult (po - pi, d) + pi;
	#			temp[on].yit = pi >> SHIFT;
	# 			temp[on].yfrac = pi & MASK;

	# 			pi = MKFIX (temp[in].zint, temp[in].zfrac);
	# 			po = MKFIX (temp[out].zint, temp[out].zfrac);
	# 			pi = Mult (po - pi, d) + pi;
	# 			temp[on].zint = pi >> SHIFT;
	# 			temp[on].zfrac = pi & MASK;

	# 			pi = MKFIX (temp[in].wint, temp[in].wfrac);
	# 			po = MKFIX (temp[out].wint, temp[out].wfrac);
	# 			pi = Mult (po - pi, d) + pi;
	# 			temp[on].wint = pi >> SHIFT;
	# 			temp[on].wfrac = pi & MASK;

	#
	# Load x, y, z, w int (out)
	#

	# Hack because offset to ldv instructions it only 7 bits.
	addi	out, out, RSP_SCRATCH_OFFSET
	addi	in, in, RSP_SCRATCH_OFFSET

	ldv	$v3, 0(out)

	#
	# Load x, y, z, w frac (out)
	#

	ldv	$v4, 8(out)

	#
	# Load x, y, z, w int (in)
	#

	ldv	$v5, 0(in)

	#
	# Load x, y, z, w frac (in)
	#

	ldv	$v6, 8(in)

	#
	# Subtract
	#

	vsub	$v7, $v3, $v5
	vsub	$v8, $v4, $v6

	# Hack because offset to ldv instructions it only 7 bits.
	addi	out, out, eval(-RSP_SCRATCH_OFFSET)
	addi	in, in, eval(-RSP_SCRATCH_OFFSET)

	#
	# Multiply
	#

	vmudl   $v10, $v8, $v12[0]
	vmadm   $v9, $v7, $v12[0]
	vmadn   $v10, $v0, $v20[0]  # XXX Should be zero

	#
	# Add
	#

	vadd	$v9, $v9, $v5
	vadd	$v10, $v10, $v6

	#
	# Save x, y, z, w int and frac
	#

	# hack because offset to sdv is only 7 bits
	addi	on, on, RSP_SCRATCH_OFFSET

	sdv	$v9, 0(on)
	sdv	$v10, 8(on)

	# hack because offset to sdv is only 7 bits
	addi	on, on, eval(-RSP_SCRATCH_OFFSET)

	# 			pi = MKFIX (temp[in].r, 0);
	# 			po = MKFIX (temp[out].r, 0);
	# 			temp[on].r = (Mult (po - pi, d) + pi) >> SHIFT;

	# 			pi = MKFIX (temp[in].g, 0);
	# 			po = MKFIX (temp[out].g, 0);
	# 			temp[on].g = (Mult (po - pi, d) + pi) >> SHIFT;

	# 			pi = MKFIX (temp[in].b, 0);
	# 			po = MKFIX (temp[out].b, 0);
	# 			temp[on].b = (Mult (po - pi, d) + pi) >> SHIFT;

	# 			pi = MKFIX (temp[in].a, 0);
	# 			po = MKFIX (temp[out].a, 0);
	# 			temp[on].a = (Mult (po - pi, d) + pi) >> SHIFT;

	# 			pi = MKFIX (temp[in].s, 0);
	# 			po = MKFIX (temp[out].s, 0);
	# 			temp[on].s = (Mult (po - pi, d) + pi) >> SHIFT;

	# 			pi = MKFIX (temp[in].t, 0);
	# 			po = MKFIX (temp[out].t, 0);
	# 			temp[on].t = (Mult (po - pi, d) + pi) >> SHIFT;

	# (r, g, b, a, s, t not done yet)

	#
	# 			temp[on].cc = gen_clip_codes (&temp[on]);
	#

	addi	$1, on, RSP_SCRATCH_OFFSET
	jal	gen_outcodes	# outcode in $1
	nop
	sb	$1, eval(RSP_SCRATCH_OFFSET+24)(on)

	#
	# 			cc_clip |= temp[on].cc;
	#

	or	cc_clip, cc_clip, $1

	#
	# 			on++;
	#

	addi	on, on, 32

	#
	# 		}
	#

not_crossed:

	#
	# 		if (inside) {	/* This is a good point -- keep it */
	#

	beq	inside, zero, not_inside
	nop

	#
	# 			temp[on] = temp[i];
	#

	lw	temp, RSP_SCRATCH_OFFSET(i)
	sw	temp, RSP_SCRATCH_OFFSET(on)
	lw	temp, eval(RSP_SCRATCH_OFFSET+4)(i)
	sw	temp, eval(RSP_SCRATCH_OFFSET+4)(on)
	lw	temp, eval(RSP_SCRATCH_OFFSET+8)(i)
	sw	temp, eval(RSP_SCRATCH_OFFSET+8)(on)
	lw	temp, eval(RSP_SCRATCH_OFFSET+12)(i)
	sw	temp, eval(RSP_SCRATCH_OFFSET+12)(on)
	lw	temp, eval(RSP_SCRATCH_OFFSET+16)(i)
	sw	temp, eval(RSP_SCRATCH_OFFSET+16)(on)
	lw	temp, eval(RSP_SCRATCH_OFFSET+20)(i)
	sw	temp, eval(RSP_SCRATCH_OFFSET+20)(on)
	lw	temp, eval(RSP_SCRATCH_OFFSET+24)(i)
	sw	temp, eval(RSP_SCRATCH_OFFSET+24)(on)
	lw	temp, eval(RSP_SCRATCH_OFFSET+28)(i)
	sw	temp, eval(RSP_SCRATCH_OFFSET+28)(on)

	#
	# 			cc_clip |= temp[on].cc;
	#

	lb	temp, eval(RSP_SCRATCH_OFFSET+24)(on)
	or	cc_clip, cc_clip, temp

	#
	# 			on++;
	# 		}
	#

	addi	on, on, 32

not_inside:

	#
	# 		j = i;
	#

	or	previous, i, i

	#
	# 	}
	#
	# End of vertex loop
	#

	addi	i, i, 32
	bne	i, n, vertex_loop
	nop

	#
	# 	n = on - os;
	#

	sub	n, on, os

	#
	# 	s = os;
	#

	or	i, os, os
	add	n, n, i

	#
	# }
	#
	# End of "plane" loop
	#

plane_continue:
	addi	plane, plane, 1
	bne	plane, num_planes, plane_loop
	nop

	#
	# Call triangle rasterization routine for every triangle in the
	# current input queue...
	#

	jr	return

.unname	vertex1
.unname	vertex2
.unname	vertex3
.unname	cc_clip
.unname	num_planes
.unname	plane
.unname	one
.unname	flag
.unname	i
.unname	n
.unname	os
.unname	on
.unname	axis
.unname	previous
.unname	inside
.unname	last_in
.unname	in
.unname	out
.unname	wi
.unname	wo
.unname	wone
.unname	pi
.unname	po
.unname	num
.unname	den

	#
	# gen_outcodes
	#
	#   Simulates the instruction that generated outcodes.  Vertex
	#   pointer is in $1, outcodes returned in $1.  Register $9 (temp)
	#   clobbered, all others saved.
	#

gen_outcodes:

	#
	# Save these registers
	#

	sw	$2, 0(dmembase)
	sw	$3, 4(dmembase)
	sw	$4, 8(dmembase)
	sw	$5, 12(dmembase)
	sw	$6, 16(dmembase)

	#
	# Clear temporary outcode.
	#

	or	$2, zero, zero

	#
	# Get W
	#

	lhu	$4, 6($1)
	sll	$4, $4, 16
	lhu	temp, 14($1)
	or	$4, $4, temp

	#
	# Get -W
	#

	subu	$5, zero, $4

	#
	# Get X
	#

	lhu	$3, 0($1)
	sll	$3, $3, 16
	lhu	temp, 8($1)
	or	$3, $3, temp

	slt	temp, $3, $5	# X <= -W
	or	$2, $2, temp
	slt	temp, $4, $3	# W <= X
	sll	temp, temp, 1
	or	$2, $2, temp

	#
	# Get Y
	#

	lhu	$3, 2($1)
	sll	$3, $3, 16
	lhu	temp, 10($1)
	or	$3, $3, temp

	slt	temp, $3, $5	# Y <= -W
	sll	temp, temp, 2
	or	$2, $2, temp	# W <= Y
	slt	temp, $4, $3
	sll	temp, temp, 3
	or	$2, $2, temp

	#
	# Get Z
	#

	lhu	$3, 4($1)
	sll	$3, $3, 16
	lhu	temp, 12($1)
	or	$3, $3, temp

	slt	temp, $3, $5	# Z <= -W
	sll	temp, temp, 4
	or	$2, $2, temp	# W <= Z
	slt	temp, $4, $3
	sll	temp, temp, 5
	or	$2, $2, temp

	#
	# Move to output register
	#

	or	$1, $2, $2

	#
	# Restore scratch registers
	#

	lw	$2, 0(dmembase)
	lw	$3, 4(dmembase)
	lw	$4, 8(dmembase)
	lw	$5, 12(dmembase)
	lw	$6, 16(dmembase)

	#
	# Return
	#

	jr	return
	nop