gs2sprite.s 25.1 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
/*---------------------------------------------------------------------
	Copyright (C) 1997, Nintendo.
	
	File		gs2sprite.s
	Coded    by	Yoshitaka Yasumoto.	Feb 13, 1997.
	Modified by	
	Comments	S2DEX sprite handler
	
	$Id: gs2sprite.s,v 1.1.1.1 2002/05/02 03:29:12 blythe Exp $
  ---------------------------------------------------------------------*/
 #!Reserved  (0,11,12,17,18,19,20,23,24,25,26,27,30,31)
 #!Reserved  (v0,v1,v28,v29,v30,v31)

 #----------------------------------------------------------------------------
 #	case_G_OBJ_SPRITE:
 #	回転可能な OBJECT の作画
 #----------------------------------------------------------------------------
case_G_OBJ_SPRITE:
	#---------------------------------------------------------------------
	# uObjSprite データのアドレスを物理アドレスに変換し, 
	# DMA 転送をかける.
	#---------------------------------------------------------------------
  AssignForDMAproc
	jal	AdrsFixup
	_li	(dma_len, 23)		# dram_adrs は AdrsFixup で設定される
	jal	DMAread
case_G_OBJ_SPRITE_1:
	_li	(dmem_adrs, RSP_INPUT_SPRITE)		# 64bit alignment
  EndAssignForDMAproc

	#---------------------------------------------------------------------
	# DMA 転送終了を待ち. その間にパラメータのロードを行なう.
	# 転送終了後 Object のジオメトリデータおよびイメージデータを取得.
	#
	#   sprite [objX|scaleX|imageS|imageW| objY|scaleY|imageT|imageH]
	#---------------------------------------------------------------------
  FixedAssign(sprite, v2)	# objX,sclX,imgS,imgW, objY,sclY,imgT,imgH
  FixedAssign(rsmode, v3)	# RenderMode AA/BLP パラメータ
  FixedAssign(shrink, v4)	# RenderMode Shrink パラメータ  
  FixedAssign(sptr, 20)		# uObjSprite へのポインタを設定 = dmem_adrs
	lhu	sys0, RSPOBJ_RENDERP_SMODE(zero)
	ldv	shrink[0], oRSPOBJ_SHRINKPARAM(vecptr)	# Shrink パラメータ
	jal	DMAwait
	ldv	rsmode[0], 0(sys0)			# AA/BLP パラメータ 
  case_G_OBJ_SPRITE_Txtr:	# LoadTxtr との複合命令の飛び先
	lqv	sprite[0], RSP_SPR_OBJXY(sptr)
	
	#---------------------------------------------------------------------
	# マトリクスの C,D を解析し, Y 座標に関する Pre-Sort を行なう
	#---------------------------------------------------------------------
  Assign(idxp, 1)
  Assign(Cper, 2)
  Assign(Dper, 3)
	#                 C       D      C^D
	# C>0,D>0 なら 0x0000  0x0000  0x0000
	# C<0,D>0 なら 0x0080  0x0000  0x0080
	# C>0,D<0 なら 0x0000  0x0180  0x0180
	# C<0,D<0 なら 0x0080  0x0180  0x0100
	#
	lbu	Cper, (RSPOBJ_MATRIX+ 8)(zero)
	lb	Dper, (RSPOBJ_MATRIX+12)(zero)
	xor	Cper, Cper, Dper
	andi	Cper, Cper, 0x0180
	srl	Cper, Cper, 3
	addi	idxp, Cper, RSP_VTX_INDEX
  EndAssign(Cper, 2)
  EndAssign(Dper, 3)

	#---------------------------------------------------------------------
	# スプライトデータを各モードに応じて修正する
	#---------------------------------------------------------------------
  Assign(spriteXY, v5)		# objXY 修正値
  Assign(spriteST, v6)		# imgST 修正値
  Assign(spriteWH, v7)		# imgWH 修正値
		# Shrink の処理
		vsub	spriteWH, sprite,   shrink[iSHRINK_IMGWH]	# [2|6]

		# AA/Bilerp に伴う座標, テクスチャシフトの処理
		vadd	spriteST, shrink, rsmode[iR_ADD_IMGST] #[iSHRINK_IMGST]
		vadd	spriteXY, sprite, shrink[iSHRINK_S_OBJXY]	# [0|4]
  EndAssign(shrink, v4)		# RenderMode Shrink パラメータ  

	#---------------------------------------------------------------------
	# Object の幅 高さを計算する.
	#   objW = (s32)imgW * 128 / sp->s.scaleX;
	#   objH = (s32)imgH * 128 / sp->s.scaleY;
	#   結果は objSZ[3], objSZ[7] に代入される.
	#---------------------------------------------------------------------
		#-------------------------------------------------------------
		# scale[XY] 値の逆数を計算する
		#   1/(s5.10) = (s10.21)
  Assign(iscalei, v4)		# 1/scaleX, 1/scaleY
  Assign(iscalef, v8)		# 1/scaleX, 1/scaleY
/*Delay 3*/	vrcp	iscalef[0], sprite[1]
		vrcph	iscalei[0], vtmp[0]
		vrcp	iscalef[4], sprite[5]
		vrcph	iscalei[4], vtmp[0]

		#-------------------------------------------------------------
		# vrcp 命令では 1 の逆数が 1 にならないので値の補正をする
		#
		#	0x00010000 の逆数が 0x0001ffff となるので
		#	High 部分を Low に加算してやる
		#	形としては 0x0001.0001 を乗ずることになる.
		#
		vaddc	iscalef, iscalef, iscalei
		vadd	iscalei, iscalei, _0x0000
	
  EndAssign(sprite, v2)		# imageS,imageT,imageW,imageH
		#-------------------------------------------------------------
		# image[WH] / scale[XY] を計算する
		#   (u11.5)*(s10.21) = (s21.26) = (s21.10)  IFxF=IF
  Assign(objSZ, v2)
  Assign(objSZf, v9)
/*Delay 2*/	vmudl	vtmp,   spriteWH, iscalef[0h]	# FxF	[2|6]
		vmadm	objSZ,  spriteWH, iscalei[0h]	# FxI=I
		vmadn	objSZf, vtmp,      _0x0000	#    =F
  EndAssign(iscalei, v4)		# 1/scaleX, 1/scaleY
  EndAssign(iscalef, v8)		# 1/scaleX, 1/scaleY 
		#-------------------------------------------------------------
		# 小数点を合わせる
		#   (s21.10)>>8 = (s21.10)*(1/256) = (s29.2) = (s13.2)
		#       IFxF=IF で F のみ使用するので I の計算はいい加減
/*Delay 2*/	vmudm	vtmp,  objSZ,  _0x0100		# FxI 
		vmadl	objSZ, objSZf, _0x0100		# FxF=F
  EndAssign(objSZf, v9)

	#---------------------------------------------------------------------
	# 4 頂点の S,T 座標の取得
	#   S Flip, T Flip の処理を行なう
	#	RSP_SPR_IMGFLAGS(sptr) の D0: S 反転/ D4: T 反転
	#   sn = imageS(sprite[2]) + vindex[n] * imageW(sprite[3])
	#   tn = imageT(sprite[6]) + vindex[n] * imageH(sprite[7])
	#   結果は objTxtr = [s1|s2|s0|s3|t1|t2|t0|t3] に代入される.
	#---------------------------------------------------------------------
  Assign(objTxtr, v4)
  Assign(vindexST, v8)
  Assign(vindexXY, v9)
  Assign(stFlip, v10)
	lqv	vindexXY[0], 0(idxp)
	lbu	sys0, RSP_SPR_IMGFLAGS(sptr)	# S,T Flip 処理
	ctc2	sys0, $vcc
	vmrg	stFlip, vone, vzero
	vxor	vindexST, vindexXY, stFlip[0h]
		vmudh	vtmp,    vone,     spriteST[iSHRINK_IMGST]
		vmadh	objTxtr, vindexST, spriteWH[2h]
  EndAssign(idxp, 1)
  EndAssign(spriteST, v6)		# imgST 修正値
  EndAssign(spriteWH, v7)		# imgWH 修正値
  EndAssign(vindexST, v8)
  EndAssign(stFlip, v10)
	
	#---------------------------------------------------------------------
	# 4 頂点の X,Y 座標の取得
	#   xn = objX(sprite[0]) + vindex[n] * objW(objSZ[2])
	#   yn = objY(sprite[4]) + vindex[n] * objH(objSZ[6])
	#   結果は objPosX = [x1|x2|x0|x3|x1|x2|x0|x3] と
	#          objPosY = [y1|y2|y0|y3|y1|y2|y0|y3] に代入される
	#---------------------------------------------------------------------
  Assign(objPosX, v6)
  Assign(objPosY, v7)
		vmudh	vtmp,    vone,     spriteXY[0h]
		vmadh	objPosX, vindexXY, objSZ[2h]
		vmadh	objPosY, vzero,    _0x0000
		sqv	objPosX[0], oRSPOBJ_VSCRATCH+0(vecptr)
		ldv	objPosX[8], oRSPOBJ_VSCRATCH+0(vecptr)
		ldv	objPosY[0], oRSPOBJ_VSCRATCH+8(vecptr)
  EndAssign(spriteXY, v5)		# objXY 修正値
  EndAssign(objSZ, v2)

	#---------------------------------------------------------------------
	# マトリクス要素の取得
	#
	#   X,Y 要素のサブピクセル成分を処理する.
	#     objMtx = [Ai|Af|Bi|Bf|Ci|Cf|Di|Df]
	#     objOfs = [X |        |Y |        ]
	#---------------------------------------------------------------------
  Assign(objMtx, v2)		# Object 変形行列 A, B, C, D
  Assign(objOfs, v5)		# Object 変形行列 X,Y
		lqv	objMtx[0], oRSPOBJ_MATRIX(vecptr)		
		lsv	objOfs[0], oRSPOBJ_MATRIX+16(vecptr)
		lsv	objOfs[8], oRSPOBJ_MATRIX+18(vecptr)
		vadd	objOfs, objOfs, rsmode[iS_ADD_OBJOFS]
		vand	objOfs, objOfs, rsmode[iCUT_SUBPIX]

	#---------------------------------------------------------------------
	# 4 頂点のスクリーン座標の計算
	#   結果は objPos = [x1|x2|x0|x3|y1|y2|y0|y3] と代入される.
	#   また値は上限で Clamp される.
	#---------------------------------------------------------------------
  Assign(objPos, v8)
		# [X|0|Y|0] を ACC へ代入
		vmudh	vtmp,   vone,    objOfs[0h]
		# A*xn,C*xn を ACC へ加算  I(s13.2)*IF(s15.16)=(s13.18)
		vmadm	vtmp,   objPosX, objMtx[1h]	# Af=[1] Cf=[5]
		vmadh	vtmp,   objPosX, objMtx[0h]	# Ai=[0] Ci=[4]
		# B*yn,D*yn を ACC へ加算  I(s13.2)*IF(s15.16)=(s13.18)
		vmadm	vtmp,   objPosY, objMtx[3h]	# Bf=[3] Df=[7]
		vmadh	objPos, objPosY, objMtx[2h]	# Bi=[2] Di=[6]
  EndAssign(objPosX, v6)
  EndAssign(objPosY, v7)		
  EndAssign(objMtx, v2)		# Object 変形行列 A,B,C,D
  EndAssign(objOfs, v5)		# Object 変形行列 X,Y
  EndAssign(vindexXY, v9)

	#---------------------------------------------------------------------
	# シザリング範囲内かどうかの判定
	#   作画領域がシザリング範囲内に入っていなければ終了する.
	#---------------------------------------------------------------------
  Assign(scissor, v2)			# シザリング範囲
#ifdef	UCODE_S2DEX2
	llv	scissor[0], oRSP_SCISSOR_XL(vecptr)
	llv	scissor[8], oRSP_SCISSOR_YL(vecptr)
#else
	llv	scissor[0], RSP_STATEP_SCISSOR_XL(zero)
	llv	scissor[8], RSP_STATEP_SCISSOR_YL(zero)
#endif
		#-------------------------------------------------------------
		# xn >= ScissorXL, yn >= ScissorYU なら VCC=1
		# 全ての xn が ScissorXL の左側 (VCC[3,2,1,0] == 0) または
		# 全ての yn が ScissorYU の上側 (VCC[7,6,5,4] == 0) なら終了
		#
		vge	vtmp, objPos, scissor[0h]
		cfc2	sys0, $vcc
		andi	sys1, sys0, 0x0f
		beq	sys1, zero, GfxDone
		andi	sys1, sys0, 0xf0
		beq	sys1, zero, GfxDone
		
		#-------------------------------------------------------------
		# xn < ScissorXR, yn < ScissorYD なら VCC=1
		# 全ての xn が ScissorXR の右側 (VCC[3,2,1,0] == 0) または
		# 全ての yn が ScissorYD の下側 (VCC[7,6,5,4] == 0) なら終了
		#
		vlt	vtmp, objPos, scissor[1h]
  EndAssign(scissor, v2)			# シザリング範囲
		cfc2	sys0, $vcc
		andi	sys1, sys0, 0x0f
		beq	sys1, zero, GfxDone
		andi	sys1, sys0, 0xf0
		beq	sys1, zero, GfxDone		
	
	#---------------------------------------------------------------------
	# ベクトルレジスタを使用しての y1, y2 の Y ソートを行なう
	#   結果 y0<=y1<=y2<=y3 となる
	#---------------------------------------------------------------------
		#-------------------------------------------------------------
		# 以下のデータの作成
		#     sortkey = [ y1 |  y2 | y0 |  y3 | y1 |  y2 | y0 |  y3 ]
		#     flipXY  = [  0 |x1^x2|  0 |x0^x3|  0 |y1^y2|  0 |y0^y3]
		#     flipST  = [  0 |s1^s2|  0 |s0^s3|  0 |t1^t2|  0 |t0^t3]
		#
  Assign(sortkey, v2)
  Assign(flipXY, v5)
  Assign(flipST, v6)
		vadd	sortkey, objPos, _0x0000
		sdv	objPos[8],  oRSPOBJ_VSCRATCH(vecptr)
		vxor	flipXY,  objPos,  objPos[0q]
		ldv	sortkey[0], oRSPOBJ_VSCRATCH(vecptr)
		vxor	flipST,  objTxtr, objTxtr[0q]
		#-------------------------------------------------------------
		# sortkey の隣との比較で, flipXY, flipST を選ぶ.
		#   y1 < y2 なら    flipXY[0|4] = flipXY[0|4] = [0|0]
		#   そうでなければ  flipXY[0|4] = flipXY[1|5] = [x1^x2|y1^y2]
		#   flipST についても同様
		#
/* Delay-3 */	vlt	sortkey, sortkey, sortkey[1q]
		vmrg	flipXY,  flipXY,  flipXY[1q]
		vmrg	flipST,  flipST,  flipST[1q]
  EndAssign(sortkey, v2)
		#-------------------------------------------------------------
		# v1, v2 の入れ替え.
		#   元々 y0<y3 なので v0 と v3 は入れ替わらない
		#	
/* Delay-1 */	vxor	objPos,  objPos,  flipXY[0q]
		vxor	objTxtr, objTxtr, flipST[0q]		
  EndAssign(rsmode, v3)		# RenderMode パラメータ
  EndAssign(flipXY, v5)
  EndAssign(flipST, v6)

	#---------------------------------------------------------------------
	#   Tile 記述子の設定
	#---------------------------------------------------------------------
  Assign(tile, 1)
	lbu	tile,  RSP_STATEP_RENDERTILE(zero)
	xori	tile,  tile,  0x02
	sb	tile,  RSP_STATEP_RENDERTILE(zero)

	#---------------------------------------------------------------------
	# Tilesync コマンドの処理
	#       もし rdpstat = RDP_STAT_PRIMITIVE(tile) なら Tilesync
	#---------------------------------------------------------------------
		lb	sys1, RSP_STATEP_RDP_STAT(zero)	# 状態の取得
		sbv	_TILESYNC, 0(outp)	
		bne	tile, sys1, NoNeedSync		# 状態の比較
		sb	tile, RSP_STATEP_RDP_STAT(zero)	# 状態の更新
		addiu	outp, outp, 8
NoNeedSync:	
	
	#---------------------------------------------------------------------
	# SetTile コマンドの作成
	#---------------------------------------------------------------------
  Assign(Dfmt, 2)
  Assign(Dsiz, 3)
  Assign(Dstr, 4)
  Assign(Dadr, 5)
  Assign(Dpal, 6)
  Assign(Dlow, 7)
		lhu	Dlow, RSPOBJ_RENDERP_TWINDOW(zero)
		lbu	Dfmt, RSP_SPR_IMGFMT(sptr)
		lbu	Dsiz, RSP_SPR_IMGSIZ(sptr)
		lhu	Dstr, RSP_SPR_IMGSTR(sptr)
		lhu	Dadr, RSP_SPR_IMGADRS(sptr)
		lbu	Dpal, RSP_SPR_IMGPAL(sptr)
		lw	Dlow, 4(Dlow)	
		sll	Dfmt, Dfmt, 21
		sll	Dsiz, Dsiz, 19
		or	Dfmt, Dfmt, Dsiz
		sll	Dstr, Dstr,  9
		or	Dfmt, Dfmt, Dstr
		or	Dfmt, Dfmt, Dadr
		sw	Dfmt, 0(outp)		# 頭の 1byte は上書かれる
		sbv	_SETTILE, 0(outp)
		sll	Dpal, Dpal, 20		
		or	Dpal, Dpal, Dlow
		sw	Dpal, 4(outp)		# 頭の 1byte は上書かれる
		sb	tile, 4(outp)
  EndAssign(Dfmt, 2)
  EndAssign(Dsiz, 3)
  EndAssign(Dstr, 4)
  EndAssign(Dadr, 5)
  EndAssign(Dpal, 6)
  EndAssign(Dlow, 7)	
	
	#---------------------------------------------------------------------
	# SetTileSize コマンドの作成
	#    gDPSetTileSize((*glistp)++,
	#	   spriteRenderTile, 0, 0, sp->s.imageW/8-4, sp->s.imageH/8-4);
	#---------------------------------------------------------------------
  Assign(Dpc0, 2)
  Assign(Dpc1, 3)
  Assign(SH, 4)
  Assign(TH, 5)
		lhu	SH, RSP_SPR_IMGW(sptr)
		slv	_SETTILESIZE, 8(outp)
		lhu	TH, RSP_SPR_IMGH(sptr)
		andi	SH, SH, 0x7ff8
		sll	SH, SH, 9
		sra	TH, TH, 3
		or	Dpc1, SH,   TH
		addi	Dpc1, Dpc1, -0x4004
		sw	Dpc1, 12(outp)
		sb	tile, 12(outp)
		sbv	_PIPESYNC, 16(outp)
		addiu	outp, outp, 24
  EndAssign(Dpc0, 2)
  EndAssign(Dpc1, 3)
  EndAssign(SH, 4)
  EndAssign(TH, 5)
	
	#---------------------------------------------------------------------
	#  3 角形の描画処理
	#	vtx0 [ - | - | X0 | S0 | T0 | 0x7fff | Y0 ]
	#	vtx1 [ - | - | X1 | S1 | T1 | 0x7fff | Y1 ]
	#	vtx2 [ - | - | X2 | S2 | T2 | 0x7fff | Y2 ]
	#	vtx3 [ - | - | X3 | S3 | T3 | 0x7fff | Y3 ]
	#---------------------------------------------------------------------
  Assign(vtx0, v2)
  Assign(vtx1, v3)
  Assign(vtx2, v5)
  Assign(vtx3, v6)
  Assign(wval, v7)
		vnor	wval, vzero, _0x8000	# _0x7fff
		_li	(sys0, 0x20)
		ctc2	 sys0, $vcc
		_li	(sys1, 0xe7)
		vmrg	vtx0, wval, objPos[2h]
		vmrg	vtx1, wval, objPos[0h]
		vmrg	vtx2, wval, objPos[1h]
		vmrg	vtx3, wval, objPos[3h]
		ctc2	 sys1, $vcc
		vmrg	vtx0, vtx0, objTxtr[2h]
		vmrg	vtx1, vtx1, objTxtr[0h]
		vmrg	vtx2, vtx2, objTxtr[1h]
		vmrg	vtx3, vtx3, objTxtr[3h]
  EndAssign(wval, v7)
  EndAssign(objPos, v8)
  EndAssign(objTxtr, v4)

	#---------------------------------------------------------------------
	# 外積値の計算
	#
	#   dv20 = [ - | - |Dx20|Ds20|Dt20| 0 |Dy20]
	#   dv10 = [ - | - |Dx10|Ds10|Dt10| 0 |Dy10]
	#   dv01 = [ - | - |Dx01|Ds01|Dt01| 0 |Dy01]
	#   dv21 = [ - | - |Dx21|Ds21|Dt21| 0 |Dy21]
	#
	#   r[2] = Dx20*Dy10 - Dx10*Dy20 = Dx20*Dy10 + Dx01*Dy20
	#
	#   (s13.2)*(s13.2)=(ss26.4)
	#---------------------------------------------------------------------
Start_Sprite_Half:
  Assign(dv20, v4)
  Assign(dv10, v7)
  Assign(dv01, v8)
  Assign(dv21, v9)
  Assign(ri, v10)
  Assign(rf, v11)
		vsub	dv20, vtx2, vtx0
		vsub	dv10, vtx1, vtx0
		vsub	dv01, vtx0, vtx1
		vsub	dv21, vtx2, vtx1
		vmudh	vtmp, dv20, dv10[6]
		vmadh	vtmp, dv01, dv20[6]
		vsar	ri, ri, ri[0]
		vsar	rf, rf, rf[1]		# r*[2] が外積値

	#---------------------------------------------------------------------
	# Y の差の逆数計算 および Dx の用意
	#  iDy* = [ 1/Dy20 | 1/Dy10 | 1/Dy21 ]   (s13.2) -> (s2.29)
	#  dv21 = [  Dx20  |  Dx10  |  Dx21  ]
	#
	# 外積値の逆数のアンダーフローによる精度落ちを軽減するための正規化
	#   |r| < n のときと |r| <= n のときで係数を変化させる
	#---------------------------------------------------------------------
  Assign(iDyi, v12)
  Assign(iDyf, v13)
  Assign(Norm, v14)
			vrcp	iDyf[0], dv20[6]
			vrcph	iDyi[0], _0x0000
		vabs	vtmp, ri,      ri
			vrcp	iDyf[1], dv10[6]	
			vrcph	iDyi[1], _0x0000
		vlt	vtmp, vtmp,    _0x0008
		vmrg	Norm, vconst1, _0x0100	# vconst[1] を使用 = _0x8000
			vrcp	iDyf[2], dv21[6]
			vrcph	iDyi[2], _0x0000
		vmudl	vtmp, rf,     Norm[1]
		vmadm	ri,   ri,     Norm[1]
		vmadn	rf,   vconst, _0x0000
			vmov	dv21[0], dv20[2]
			vmov	dv21[1], dv10[2]
		vaddc	iDyf, iDyf, iDyi
		vadd	iDyi, iDyi, _0x0000	
#define	USE_NEWTON
#ifdef  USE_NEWTON
  Assign(iDyf2, v15)
  Assign(iDyi2, v16)
  Assign(Dy, v17)
  Assign(vtwo, v18)
		#
		# Newton 法で精度を上げる
		#
		vmov	Dy[0], dv20[6] 
		vmov	Dy[1], dv10[6] 
		vmov	Dy[2], dv21[6]
		vadd	vtwo,  vzero,  _0x0002
		vmudn	iDyf,  iDyf,   _0x0002	# (s2.29)->(s1.30)
		vmadh	iDyi,  iDyi,   _0x0002		
		vmudl	vtmp,  iDyf,   Dy	# (ss14.32)
		vmadm	iDyi2, iDyi,   Dy
		vmadn	iDyf2, vconst, _0x0000
		vsubc	iDyf2, vzero,  iDyf2
		vsub	iDyi2, vtwo,   iDyi2
		vmudl	vtmp,  iDyf,   iDyf2
 #		vmadl	vtmp,  vone,   _0x8000	# 四捨五入 (#)
		vmadm	vtmp,  iDyi,   iDyf2
		vmadn	iDyf,  iDyf,   iDyi2
		vmadh	iDyi,  iDyi,   iDyi2
  EndAssign(iDyf2, v15)
  EndAssign(iDyi2, v16)
  EndAssign(Dy, v17)
  EndAssign(vtwo, v18)
#endif
	#---------------------------------------------------------------------
	# コマンドブロックの作成
	#   メジャーエッジの判定
	#---------------------------------------------------------------------
  Assign(major, 2)
	mfc2	major, ri[3]		# 外積値の符号の取得
	andi	major, major, 0x80
	or	major, major, tile
	ori	major, major, 0x0a00

	#---------------------------------------------------------------------
	# 外積値の逆数計算
	#   invr*[2] = 1 / r	(s27.4)->(s4.27)
	#---------------------------------------------------------------------
  Assign(invri, v15)
  Assign(invrf, v16)
		vrcph	vtmp[0],  ri[2]
		vrcpl	invrf[2], rf[2]
		vrcph	invri[2], _0x0000
  EndAssign(ri, v10)
  EndAssign(rf, v11)
 
	#---------------------------------------------------------------------
	# Dx/Dy の計算
	#   DxDy = [ DxDy20 | DxDy10 | DxDy21 ]
	#   
	#	(s13.2)*(s1.30)=(ss14.32)=(s15.16)  IFxI=IF
	#---------------------------------------------------------------------
  Assign(DxDyf, v10)
  Assign(DxDyi, v11)
#ifdef	USE_NEWTON
		vmudn	vtmp,  iDyf, dv21
		vmadh	vtmp,  iDyi, dv21
		vsar	DxDyf, DxDyf, DxDyf[1]
		vsar	DxDyi, DxDyi, DxDyi[0]
#else
		vmudm	DxDyi, dv21,  _0x0002
		vmadn	DxDyf, vzero, _0x0002
		vmudl	vtmp,  iDyf,  DxDyf
		vmadm	vtmp,  iDyi,  DxDyf
		vmadn   DxDyf, iDyf,  DxDyi
		vmadh   DxDyi, iDyi,  DxDyi
#endif
  EndAssign(iDyi, v12)
  EndAssign(iDyf, v13)
				
	#---------------------------------------------------------------------
	# Da/Dx, Da/Dy の計算
	#   必要なのは
	#	DaDx[3|4|5] = Dy10*Da20-Dy20*Da10 = Dy10*Da20+Dy20*Da01
	#	DaDy[3|4|5] = Dx20*Da10-Dx10*Da20 = Dx20*Da10+Dx01*Da20
	#
	#      (s15.0)*(s13.2)=(ss28.2)
	#---------------------------------------------------------------------
  Assign(DaDxf, v12)
  Assign(DaDxi, v13)
  Assign(DaDyf, v17)
  Assign(DaDyi, v18)
		vmudh	vtmp,  dv20,  dv10[6]
		vmadh	vtmp,  dv01,  dv20[6]
		vsar	DaDxf, DaDxf, DaDxf[1]
		vsar	DaDxi, DaDxi, DaDxi[0]
		vmudh	vtmp,  dv10,  dv20[2]
		vmadh	vtmp,  dv20,  dv01[2]
		vsar	DaDyf, DaDyf, DaDyf[1]
		vsar	DaDyi, DaDyi, DaDyi[0]
  EndAssign(dv20, v4)
  EndAssign(dv10, v7)
  EndAssign(dv01, v8)
  EndAssign(dv21, v9)
	
	#---------------------------------------------------------------------
	# DaDx, DaDy への 1/r の乗算
	#
	#	invr[2], DaDx[3|4|5], DaDy[3|4|5]
	#
	#	(s29.2) * (s4.27) = (ss33.29) = (s18.13)
	#---------------------------------------------------------------------
		vmudl	vtmp,  DaDxf, invrf[2]
		vmadm	vtmp,  DaDxi, invrf[2]
		vmadn	DaDxf, DaDxf, invri[2]
		vmadh	DaDxi, DaDxi, invri[2]
		vmudl	vtmp,  DaDyf, invrf[2]
		vmadm	vtmp,  DaDyi, invrf[2]
		vmadn	DaDyf, DaDyf, invri[2]
		vmadh	DaDyi, DaDyi, invri[2]
  EndAssign(invri, v15)
  EndAssign(invrf, v16)
	
	#---------------------------------------------------------------------
	# Y 座標サブピクセルに対する X 座標の調整
	#
	#     vtx0[2|6] = [ x0 | y0 ]
	#     DxDy[0|1] = [ DxDy20 | DxDy10 ]
	#
	#   これより以下の計算を行なう
	#	xM = x0 - (y0 & 3)/4 * DxDy10
	#       xH = x0 - (y0 & 3)/4 * DxDy20
	#   結果, 以下のようになる
	#     xi/xf[0|1] = [ xH | xM ]
	#---------------------------------------------------------------------
  Assign(yf, v4)
  Assign(yi, v7)
  Assign(xf, v8)
  Assign(xi, v9)
  Assign(XL, 3)
  Assign(quart, v15)
  Assign(chop, v16)
  Assign(xMf, v19)
  Assign(xMi, v20)
	mfc2	XL, vtx1[4]
		vadd	quart, vzero, _0x4000
		vmudl	vtmp,  DxDyf, _0x4000
		vmadm	xi,    DxDyi, _0x4000
	sll	XL,  XL,  14
		vmadn	xf,    vzero, _0x0000
		vand	yf,    vtx0,  _0x0003	# yf[6]
		vsub	yf,    vzero, yf
		vlt	vtmp,  DxDyi, _0x0000	# DxDy21 > 0 なら xM ++
		vmrg	chop,  vzero, _0x0001
		vmudn	vtmp,  quart, vtx0[2]
		vmadn	xf,    xf,    yf[6]
		vmadh	xi,    xi,    yf[6]	
		vmudn	yf,    quart, yf[6]	# yf[6] = (y0 & 3) 
		vmadh	yi,    vzero, _0x0000	# 符号拡張
		vaddc	xMf,   xf,    chop[0]
		vadd	xMi,   xi,    _0x0000
  EndAssign(quart, v15)
  EndAssign(chop, v16)
	
	#---------------------------------------------------------------------
	# 各パラメータの桁合わせ. および符号合わせ
	#  objPos (s13.2)  -> (s15.16) / 4
	#  DaDx   (s18.13) -> (s15.16) * 8
	#  DaDy   (s18.13) -> (s15.16) * 8
	#---------------------------------------------------------------------
		vmudl	vtmp,  DaDxf,  Norm[1]
		vmadm	DaDxi, DaDxi,  Norm[1]
		vmadn	DaDxf, vconst, _0x0000
		vmudl	vtmp,  DaDyf,  Norm[1]
		vmadm	DaDyi, DaDyi,  Norm[1]
		vmadn	DaDyf, vconst, _0x0000
		vmudn	DaDxf, DaDxf,  _0x0008
		vmadh	DaDxi, DaDxi,  _0x0008
		vmudn	DaDyf, DaDyf,  _0x0008
		vmadh	DaDyi, DaDyi,  _0x0008
  EndAssign(Norm, v14)
	
	#---------------------------------------------------------------------
	# Da/De の計算
	#   DaDe = DaDy + DaDx * DxDy20
	#
	#   DxDy[0]     = [DxDy20]
	#   DaDx[3|4|5] = [ DsDx | DsDx | 0 ]
	#   DaDy[3|4|5] = [ DsDy | DsDy | 0 ]
	#   DaDe[3|4|5] = [ DsDe | DsDe | 0 ]
	#
	#   すでに (s15.16) に合わせてある.
	#---------------------------------------------------------------------
  Assign(DaDef, v14)
  Assign(DaDei, v15)
		vmudl	vtmp,  DaDxf, DxDyf[0]
		vmadm	vtmp,  DaDxi, DxDyf[0]
		vmadn	vtmp,  DaDxf, DxDyi[0]
		vmadh	vtmp,  DaDxi, DxDyi[0]
		vmadn	DaDef, DaDyf, _0x0001	
		vmadh	DaDei, DaDyi, _0x0001
	
	#---------------------------------------------------------------------
	# Y 座標サブピクセルに対する S,T 座標の調整
	#
	#   先ず以下のデータを作成する
	#     vtx0[3|4] = [ s0 | t0 ]
	#     yi/yf[6]  = [ y0 & 3 ]
	#     DaDe[3|4] = [DsDe|DtDe]
	#
	#   これより以下の計算を行なう
	#	s0 = s0 - yf * DsDe    t0 = t0 - yf * DtDe
	#---------------------------------------------------------------------
  Assign(stf, v16)
  Assign(sti, v21)
		vmudh	vtmp, vtx0,  _0x0001
		vmadl	vtmp, DaDef, yf[6]
		vmadm	vtmp, DaDei, yf[6]
		vmadn	stf,  DaDef, yi[6]
		vmadh	sti,  DaDei, yi[6]
  EndAssign(yf, v4)
  EndAssign(yi, v7)

	#---------------------------------------------------------------------
	# outp バッファへの出力
	#---------------------------------------------------------------------
		sh	major,		 0(outp)
		ssv	vtx2[12],	 2(outp)	# y2
		ssv	vtx1[12],	 4(outp)	# y1
		ssv	vtx0[12],	 6(outp)	# y0
		sw	XL,		 8(outp)	# x1
		ssv	DxDyi[4],	12(outp)	# DxLDy 21
		ssv	DxDyf[4],	14(outp)	# DxLDy 21
	 	ssv	xi[0],		16(outp)	# xH
		ssv	xf[0],		18(outp)	# xH.f
		ssv	DxDyi[0],	20(outp)	# DxHDy 20
		ssv	DxDyf[0],	22(outp)	# DxHDy 20
		ssv	xMi[2],		24(outp)	# xM
		ssv	xMf[2],		26(outp)	# xM.f
		ssv	DxDyi[2],	28(outp)	# DxMDy 10
		ssv	DxDyf[2],	30(outp)	# DxMDy 10
		sdv	sti[6],		32(outp)	# S0,T0,W0
		sdv	DaDxi[6],	40(outp)	# DsDx,DtDx,DwDx
		sdv	stf[6],		48(outp)	# 
		sdv	DaDxf[6],	56(outp)	#
		sdv	DaDei[6],	64(outp)	# DsDe,DtDe,DwDe
		sdv	DaDyi[6],	72(outp)	# DsDy,DtDy,DwDy
		sdv	DaDef[6],	80(outp)
		sdv	DaDyf[6],	88(outp)

	#---------------------------------------------------------------------
	# outp バッファへの送信
	#---------------------------------------------------------------------
		beq	gfx0, zero, OutputCloseGfxDone
		addiu	outp, outp, 96

	#---------------------------------------------------------------------
	# 後半部の描画
	#---------------------------------------------------------------------
		vadd	vtx0, vtx1, _0x0000
		_li	(gfx0, 0)
		vadd	vtx1, vtx2, _0x0000
		j	Start_Sprite_Half
		vadd	vtx2, vtx3, _0x0000
		
  EndAssign(major, 2)
  EndAssign(tile, 1)
  EndAssign(XL, 3)
  EndAssign(sptr, 20)		# uObjSprite へのポインタを設定済み
  EndAssign(vtx0, v2)
  EndAssign(vtx1, v3)
  EndAssign(vtx2, v5)
  EndAssign(vtx3, v6)
  EndAssign(xi, v9)
  EndAssign(xf, v8)
  EndAssign(xMf, v19)
  EndAssign(xMi, v20)
  EndAssign(sti, v21)
  EndAssign(stf, v16)
  EndAssign(DxDyf, v10)
  EndAssign(DxDyi, v11)
  EndAssign(DaDxf, v12)
  EndAssign(DaDxi, v13)
  EndAssign(DaDyf, v17)
  EndAssign(DaDyi, v18)
  EndAssign(DaDef, v14)
  EndAssign(DaDei, v15)

	#
	# gs2bg1cyc.s とサイズを合わせるためのダミー
	#
	nop nop nop nop nop  nop nop nop nop nop
	nop nop nop nop nop  nop nop nop nop nop
	nop nop nop nop nop  nop nop nop nop nop
	nop nop nop nop nop  nop nop nop
#ifdef	UCODE_S2DEX2
	nop
#endif
					
 #----------------------------------------------------------------------------
 #	case_G_BG_1CYC:
 #	拡大縮小可能な BG の作画ルーチンのオーバーレイコード
 #----------------------------------------------------------------------------
overlay_G_BG_1CYC:	.symbol	dummy_overlay_G_BG_1CYC, 0
			.symbol	adrs_G_BG_1CYC,	overlay_G_BG_1CYC
	
	#---------------------------------------------------------------------
	#   オーバーレイ終了後 case_G_BG_1CYC へジャンプする
	#	既に sys0 に飛び先が代入されているので, そのまま overlay を
	#	行なう.
	#	gs2bg1cyc とオーバーレイを行なうので, この処理の命令数は
	#	gs2bg1cyc 側と合わせなければならない
	#---------------------------------------------------------------------
#ifdef	UCODE_S2DEX2
	_li	(sys1, case_G_BG_1CYC_0)
	j	LoadOverlay
	_li	(sys0, RSP_LSTAT_OVERLAY_BG1CYC)
#else
	_li	(sys1, adrs_G_BG_1CYC)
	j	loadOverlaySR1
	_li	(sys0, 16)		# Overlay code 2 をロード	
#endif
	#
	#  gs2bg1cyc で参照するアドレスの宣言
	#
	.symbol	adrs_G_OBJ_SPRITE,      case_G_OBJ_SPRITE
	.symbol	adrs_G_OBJ_SPRITE_Txtr, case_G_OBJ_SPRITE_Txtr
	
/*======== End of gs2sprite.s ========*/