gs2bg1cycL.s 31.8 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 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
/*---------------------------------------------------------------------
	Copyright (C) 1997, Nintendo.
	
	File		gs2bg1cycL.s
	Coded    by	Yoshitaka Yasumoto.	May 27, 1997.
	Modified by	
	Comments	
	
	$Id: gs2bg1cycL.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,v27,v28,v29,v30,v31)

#ifndef	BG1CYCL_LIST
#define	BG1CYCL_LIST
 #----------------------------------------------------------------------------
 #	前半パート
 #----------------------------------------------------------------------------

 #----------------------------------------------------------------------------
 #	case_G_BG_1CYC_Cont:
 #	拡大縮小可能な BG の作画ルーチンの本体
 #
 #	typedef	struct	{
 #	  u16   imageX;		/*テクスチャの左上位置 X 座標 (u11.5)*/ 
 #	  u16	imageW;		/*テクスチャの幅*/
 #	  u16	frameX;		/*転送されるフレームの左上位置*/
 #	  u16	frameW;		/*転送されるフレームの幅*/
 #	  u16   imageY; 	/*テクスチャの左上位置 Y 座標 (u11.5)*/ 
 #	  u16	imageH;		/*テクスチャの高さ*/
 #	  u16	frameY;		/*転送されるフレームの左上位置*/
 #	  u16	frameH;		/*転送されるフレームの高さ*/
 #	  u64  *imagePtr;	/*DRAM 上のテクスチャソースアドレス*/
 #	  u16	imageLoad; 
 #	  u8	imageFmt; 
 #	  u8	imageSiz;
 #	  u16   imagePal; 
 #	  u16   imageFlip;
 #
 #	  u16   scaleW;
 #	  u16   scaleH;
 #	  s32   imageYorig;
 #
 #        u8    padding[4];
 #
 #	} uObjScaleBg_t;	/* 36 バイト使用 */
 #
 #	BG 用転送処理
 #----------------------------------------------------------------------------
        #---------------------------------------------------------------------
        # DMA 転送をかける.
        #---------------------------------------------------------------------
  AssignForDMAproc
	jal     DMAread
	_li     (dma_len, 39)		# dram_adrs は AdrsFixup で設定
	
        #---------------------------------------------------------------------
        # DMA 転送終了を待ち. その間にパラメータのロードを行なう.
        # 転送終了後 Object のジオメトリデータおよびイメージデータを取得.
        #---------------------------------------------------------------------
  Assign(tptr, 1)	
  Assign(scissor, v2)
	_li	(tptr, RSP_TMP_AREA)
#ifdef	UCODE_S2DEX2
	llv     scissor[4],  oRSP_SCISSOR_XL(vecptr)  # [2|3] = [SXL|SXH]
	jal     DMAwait
	llv     scissor[12], oRSP_SCISSOR_YL(vecptr)  # [6|7] = [SYL|SYH]
#else
	llv     scissor[4],  RSP_STATEP_SCISSOR_XL(zero)  # [2|3] = [SXL|SXH]
	jal     DMAwait
	llv     scissor[12], RSP_STATEP_SCISSOR_YL(zero)  # [6|7] = [SYL|SYH]
#endif
  EndAssignForDMAproc
	
        #---------------------------------------------------------------------
	# 拡大パラメータの取得
        #---------------------------------------------------------------------
  Assign(bgparam, v3)
  Assign(vscale, v4)
	lqv	bgparam[0], RSP_BG_IMAGEX($20)
	lsv	vscale[0],  RSP_SBG_SCALEW($20)
	lsv	vscale[8],  RSP_SBG_SCALEH($20)

  Assign(vscalei, v5)
  Assign(vscalef, v6)
  Assign(ivscalei, v7)
  Assign(ivscalef, v8)
	vmudm	vscalei, vscale, _0x0040	# (s5.10)->(s15.16)  [0|4]
	vmadn	vscalef, vconst, _0x0000
	vrcp	ivscalef[0], vscale[0]		# (s5.10)->(s11.20)   [0|4]
	vrcph	ivscalei[0], _0x0000
	vrcp	ivscalef[4], vscale[4]
	vrcph	ivscalei[4], _0x0000
	vmudn	vtmp,      vone,     _0x0001
	vmadl	vtmp,      ivscalef, _0x0800	# (s11.20)->(s15.16)  [0|4]
	vmadm	ivscalei,  ivscalei, _0x0800
	vmadn	ivscalef,  vconst,   _0x0000
  Assign(bgparami, v10)
	vmudn	vtmp,     bgparam, _0x4000		# [3|7]
	vmadh	bgparami, vconst,  _0x0000

  Assign(frameMaxL, v11)
  Assign(frameMaxH, v12)
  Assign(frameWH, v13)	
  Assign(imageFlip, 2)
	#
	# イメージと拡大率からフレームの最大サイズを求める
	#  frameWmax = ((((s32)bg->s.imageW << 10) / scaleW)-1) & ~3
	#  frameHmax = ((((s32)bg->s.imageH << 10) / scaleH)-1) & ~3
	#
   lbu      imageFlip, RSP_INPUT_BG+RSP_BG_IMAGEFLIP(zero)
	vmudn	vtmp,      vone,     _0xffff
	vmadl	vtmp,      ivscalef, bgparam[1h]	# [0|4]
   andi     imageFlip, imageFlip, 0x01
	vmadm	frameMaxH, ivscalei, bgparam[1h]	# [0|4]
	vmadn	frameMaxL, vconst,   _0x0000		# [0|4]
	vand	frameMaxL, frameMaxL, _0xfffc		# [0|4]
	# フレームサイズをクランプする
	vsubc	frameMaxL, bgparam,   frameMaxL[0h]	# [3|7]
	vsub	frameMaxH, vzero,     frameMaxH[0h]	# [3|7]
	vge	frameMaxH, frameMaxH, _0x0000		# [3|7]
	vmrg	frameMaxL, frameMaxL, _0x0000		# [3|7]
   beq      imageFlip, zero, bg1cyc_skip1
	vsub	frameWH, bgparam, frameMaxL[3h]		# [3|7]
	# X-Flip なら frameX0 += frameWmax
	vadd	vtmp,    bgparam, frameMaxL[3]		# [2]
	vmov	bgparam[2], vtmp[2]
bg1cyc_skip1:
  EndAssign(frameMaxL, v11)
  EndAssign(frameMaxH, v12)
	### imageFlip, tptr
	### scissor, bgparam
	### vscale, vscalei, vscalef, ivscalei, ivscalef
	### frameWH, bgparami
  Assign(pix0, v9)
  Assign(pix1, v11)
	#
	# 画面からのはみ出し量の計算
	#   pix0[-|-| ScXL-fX | ScXH-fW |-|-| ScYL-fY | ScYH-fH ]
	#   pix1[-|-| fX+fW-ScXH |-|-|-| fY+fH-ScYH |-]
	#
	vsub	pix0, scissor, bgparam		# [2|6]
	vsub	pix1, bgparam, scissor[3h]	# [2|6]
	vmadn	pix1, vone,    frameWH[3h]	# [2|6]
	#
	# 画面内ならはみ出し量 = 0 とする
	#
	vge	pix0, pix0, _0x0000
	vge	pix1, pix1, _0x0000
  EndAssign(scissor, v2)	
	### imageFlip, tptr
	### bgparam, pix0, pix1
	### vscale, vscalei, vscalef, ivscalei, ivscalef
	### frameWH, bgparami
	
  Assign(frameXY0, v2)
	#
	# フレームのはみ出し分をカットする
	#
	vsub	frameWH,  frameWH, pix0[2h]	# [3|7]
	vsub	frameWH,  frameWH, pix1[2h]	# [3|7]
	vadd	frameXY0, bgparam, pix0[2h]	# [2|6]
	#
	# はみ出し分の処理の結果, 描画範囲が残っていないなら終了
	#
	vge	vtmp, frameWH, _0x0001		# [3|7]
	ctc2	sys0, $vcc
	andi	sys0, sys0, 0x88
	bne	sys0, zero, GfxDone
	#
	# フレームの計算
	#   frameX0, frameX1, framePtrY0, frameRemain
	#
  Assign(frameXY1, v12)
	vadd	frameXY1,    frameXY0, frameWH[3h]	# [2|6]
	vmov	frameXY0[7], frameWH[7]
	vmudm	vtmp,        frameXY0, _0x4000
	ssv	frameXY0[4], oRSPBG_TMP_frameX0(tptr)
	ssv	frameXY1[4], oRSPBG_TMP_frameX1(tptr)
    beq   imageFlip, zero, bg1cyc_skip2
	slv	vtmp[12],  oRSPBG_TMP_framePtrY0(tptr)
  EndAssign(frameXY0, v2)
  EndAssign(frameXY1, v12)
	#
	# X-Flip なら pix0X <- pix1X
	#
	vmov	pix0[2], pix1[2]
  EndAssign(pix1, v11)
bg1cyc_skip2:

	### imageFlip, tptr
	### bgparam, pix0
	### vscale, vscalei, vscalef, ivscalei, ivscalef
	### frameWH, bgparami	
  Assign(imageSrcWH, v2)
	#
	# イメージの桁あわせ
	#    imageSrcW = bg->s.imageW << 3;	/* (u11.5) */
	#    imageSrcH = bg->s.imageH << 3;	/* (u11.5) */
	#
	vmudn	imageSrcWH, bgparam, _0x0008	# [1|5]
	#
	# イメージの位置 imageX0,Y0 取得
	#
  Assign(imageXY0, v11)
  Assign(imageXY0f, v12)
	vmudn	imageXY0f, vscale, pix0[2h]
	vmadh	imageXY0,  vconst, _0x0000
	vmudn	imageXY0f, imageXY0f, _0x0200
	vmadh	imageXY0,  imageXY0,  _0x0200		# [0|4]
	vmadh	imageXY0,  vone, bgparam[0h]		# [0|4]
  EndAssign(imageXY0f, v12)
  EndAssign(pix0, v9)
	
	### imageFlip, tptr
	### bgparam
	### vscale, vscalei, vscalef, ivscalei, ivscalef
	### frameWH, bgparami.imageSrcWH, imageXY0
	#
	# 描画領域の左端がイメージソースの右端を超えた場合はループさせ 1 段下へ
	#
  Assign(imageX0, 3)
  Assign(imageY0, 4)
  Assign(imageSrcW, 5)
  Assign(imageSrcH, 6)
  Assign(imageYorig, 7)
	mfc2	imageX0,    imageXY0[0]
	mfc2	imageSrcW,  imageSrcWH[2]
	mfc2	imageY0,    imageXY0[8]
	lw	imageYorig, RSP_INPUT_BG+RSP_SBG_IMAGEYORIG(zero)
	mfc2	imageSrcH,  imageSrcWH[10]
bg1cyc_loop1:
	sub	sys0, imageX0, imageSrcW
	bltz	sys0, bg1cyc_loop2
	mtc2	imageX0, imageXY0[0]
	sub	imageX0, imageX0, imageSrcW
	addi	imageY0, imageY0, 32
	j	bg1cyc_loop1
	addi	imageYorig, imageYorig, 32
bg1cyc_loop2:	
	sub	sys0, imageY0, imageSrcH
	bltz	sys0, bg1cyc_loop2end
	mtc2	imageY0, imageXY0[8]
	sub	imageY0, imageY0, imageSrcH
	j	bg1cyc_loop2
	sub	imageYorig, imageYorig, imageSrcH
bg1cyc_loop2end:
	sw	imageYorig, RSPBG_TMP_imageYorig(zero)
	sub	sys0, imageY0, imageYorig
	sll	sys0, sys0, 5
	sw	sys0, RSPBG_TMP_imageLYoffset(zero)
  EndAssign(imageY0, 4)
  EndAssign(imageSrcH, 6)
  EndAssign(imageYorig, 7)
	#
	# イメージ範囲の取得
	# flagBilerp を取得し,imageSliceW の計算を行なう
	#    imageSliceW = (imageW = frameW * scaleW >> 7) + flagBilerp * 32
	#
  Assign(imageW, v9)
  Assign(imageWf, v12)
	vmudn	imageWf, vscale, frameWH[3h]	# [0|4]
	vmadh	imageW,  vconst, _0x0000
	vmudn	imageWf, imageWf, _0x0200		# [0|4]
	vmadh	imageW,  imageW,  _0x0200
  EndAssign(imageWf, v12)
  EndAssign(frameWH, v13)
  Assign(imageSliceW, 4)
  Assign(flagBilerp, 6)
	lbu	sys0, RSPOBJ_RENDERMODE(zero)
	andi	sys0, sys0, 0x08		# G_OBJRM_BILERP
	srl	flagBilerp, sys0, 3		# Bilerp なら 1
	mfc2	imageSliceW, imageW[0]
	sb	flagBilerp, RSPBG_TMP_flagBilerp(zero)
	sll	sys0, flagBilerp, 5		# Bilerp なら 32
	addi	imageSliceW, imageSliceW, sys0
	#
	# 接続処理の判定  flagSplit == 0 なら接続処理
	#    flagSplit = (imageX0 + imageSliceW >= imageSrcW)
	#
	add	sys1, imageX0, imageSliceW
	slt	sys1, sys1, imageSrcW
	xori	sys1, sys1, 1
	sb	sys1, RSPBG_TMP_flagSplit(zero)	
  EndAssign(imageSliceW, 4)
  EndAssign(imageX0, 3)
  EndAssign(imageSrcW, 5)	
	### imageFlip, tptr
	### bgparam
	### vscale, vscalei, vscalef, ivscalei, ivscalef
	### imageW, bgparami.imageSrcWH, imageXY0
	#			
	# TMEM へのロード用パラメータ取得
	#  tmemParam[tmemMask|tmemShift|tmemSize]
	#
  Assign(tmemParam, v12)
	lbu	sys0, RSP_INPUT_BG+RSP_BG_IMAGEFMT(zero)
	lbu	sys1, RSP_INPUT_BG+RSP_BG_IMAGESIZ(zero)
	sll	sys0, sys0, 1
	addi	sys0, sys0, RSPSBG_TMEMSIZE_TBL
	sll	sys1, sys1, 2
	addi	sys1, sys1, RSPSBG_TMEMPARAM
	lsv	tmemParam[4], 0(sys0)
	llv	tmemParam[0], 0(sys1)
	#
  Assign(flagBilerpv, v13)
  Assign(imageSliceWmaxL, v14)
  Assign(imageSliceWmaxH, v15)
	mtc2	flagBilerp, flagBilerpv[0]
	vmudn	imageSliceWmaxL, vscale, bgparam[3]	# [0]
	vmadh	imageSliceWmaxH, vconst, _0x0000	# [0]
  EndAssign(flagBilerp, 6)
  EndAssign(bgparam, v3)
  Assign(tmemSliceWmax, v3)
	vmudn	vtmp,            flagBilerpv,     _0x0020
	vmadl	vtmp,            imageSliceWmaxL, _0x0200
	vmadm	imageSliceWmaxH, imageSliceWmaxH, _0x0200
	vmadn	imageSliceWmaxL, vconst,          _0x0000
	vsubc	vtmp, imageSliceWmaxL, imageSrcWH[1]
	vlt	vtmp, imageSliceWmaxH, _0x0000
	vmrg	vtmp, imageSliceWmaxL, imageSrcWH[1]
	vmadn	tmemSliceWmax, vone, tmemParam[0]	# tmemMask 加算 [0]
	vmudh	vtmp, vone, _0x0001
	vmadm	tmemSliceWmax, tmemSliceWmax, tmemParam[1]
	ssv	tmemSliceWmax[0], oRSPBG_TMP_tmemSliceWmax(tptr)
  EndAssign(imageSliceWmaxL, v14)
  EndAssign(imageSliceWmaxH, v15)
	### imageFlip, tptr
	### tmemParam, tmemSliceWmax, flagBilerpv
	### vscale, vscalei, vscalef, ivscalei, ivscalef
	### imageW, bgparami.imageSrcWH, imageXY0
	
  Assign(itmemSliceWmaxi, v14)
  Assign(itmemSliceWmaxf, v15)
  Assign(imageSliceLinesv, v16)
	vrcp	itmemSliceWmaxf[0], tmemSliceWmax[0]
	vrcph	itmemSliceWmaxi[0], _0x0000
	vmudh	vtmp, flagBilerpv,  _0xffff		# [0]
	vmadl	vtmp,             itmemSliceWmaxf, tmemParam[2]
	vmadm	imageSliceLinesv, itmemSliceWmaxi, tmemParam[2]
		
  EndAssign(flagBilerpv, v13)
  EndAssign(tmemSliceWmax, v3)
  EndAssign(itmemSliceWmaxi, v14)
  EndAssign(itmemSliceWmaxf, v15)
	### imageFlip, tptr
	### tmemParam, imageSliceLinesv
	### vscale, vscalei, vscalef, ivscalei, ivscalef
	### imageW, bgparami.imageSrcWH, imageXY0
	
  Assign(frameSliceLinesH, v3)
  Assign(frameSliceLinesL, v13)
	vmudn	frameSliceLinesL, imageSliceLinesv, _0x0400	# [0]
	vmadh	frameSliceLinesH, vconst,           _0x0000	# [0]
	vmudl	vtmp,             frameSliceLinesL, ivscalef[4]
	vmadm	vtmp,             frameSliceLinesH, ivscalef[4]
	vmadn	frameSliceLinesL, frameSliceLinesL, ivscalei[4]
	vmadh	frameSliceLinesH, frameSliceLinesH, ivscalei[4]	# [0]
	ssv	frameSliceLinesH[0], oRSPBG_TMP_frameSliceLines+0(tptr)
	ssv	frameSliceLinesL[0], oRSPBG_TMP_frameSliceLines+2(tptr)
	#
	# image Y 座標のオフセット値をフレームラインへ整列させる
	#  imageLYoffset = ((s32)imageY0 - imageYorig) << 5;
	#  if (imageLYoffset < 0) imageLYoffset -= (scaleH - 1);
	#  frameLYoffset = imageLYoffset / scaleH;
	#  frameLYoffset <<= 10;
	#
  Assign(frameLYoffsetL, v14)
  Assign(frameLYoffsetH, v15)
	lsv	frameLYoffsetL[0], oRSPBG_TMP_imageLYoffset+2(tptr)
	lsv	frameLYoffsetH[0], oRSPBG_TMP_imageLYoffset+0(tptr)
	vmudl	vtmp,           frameLYoffsetL, ivscalef[4]
	vmadm	vtmp,           frameLYoffsetH, ivscalef[4]
	vmadn	frameLYoffsetL, frameLYoffsetL, ivscalei[4]
	vmadh	frameLYoffsetH, frameLYoffsetH, ivscalei[4]	# [0]
	vand	frameLYoffsetL, frameLYoffsetL, _0xfc00
  EndAssign(ivscalef, v8)
  EndAssign(ivscalei, v7)
	#
	# image Y に対応するスライス番号の取得
	# imageNumSlice = framLYoffset / frameSliceLines;
	#
  Assign(imageNumSliceL, v17)
  Assign(imageNumSlice, v18)
  Assign(iframeSliceLinesL, v7)
  Assign(iframeSliceLinesH, v8)
	vrcph	vtmp[0],              frameSliceLinesH[0]
	vrcpl	iframeSliceLinesL[0], frameSliceLinesL[0]
	vrcph	iframeSliceLinesH[0], _0x0000
	vmudl	vtmp,           frameLYoffsetL, iframeSliceLinesL	# [0]
	vmadm	vtmp,           frameLYoffsetH, iframeSliceLinesL
	vmadn	imageNumSliceL, frameLYoffsetL, iframeSliceLinesH
	vmadh	imageNumSlice,  frameLYoffsetH, iframeSliceLinesH
	vmudn	imageNumSliceL, imageNumSliceL, _0x0002
	vmadh	imageNumSlice,  imageNumSlice,  _0x0002			# [0]
	#
	# imageNumSlice+1 の取得
	#
	vmadh	imageNumSliceL, vone, _0x0001
  EndAssign(iframeSliceLinesL, v7)
  EndAssign(iframeSliceLinesH, v8)
	#
	# imageNumSlice の誤差修正
	#
  Assign(frameLYoffsetL1, v7)
  Assign(frameLYoffsetH1, v8)
	vmudn	frameLYoffsetL1, frameSliceLinesL, imageNumSliceL
	vmadh	frameLYoffsetH1, frameSliceLinesH, imageNumSliceL
	vsubc	vtmp, frameLYoffsetL, frameLYoffsetL1
	vge	vtmp, frameLYoffsetH, frameLYoffsetH1
	vmrg	imageNumSlice, imageNumSliceL, imageNumSlice
  EndAssign(frameLYoffsetL1, v7)
  EndAssign(frameLYoffsetH1, v8)
  EndAssign(imageNumSliceL, v17)

	### imageFlip, tptr
	### tmemParam, imageSliceLinesv
	### vscale, vscalei, vscalef
	### imageW, bgparami.imageSrcWH, imageXY0
	### frameSliceLinesH, frameSliceLinesL
	### frameLYoffsetL, frameLYoffsetH
	### imageNumSlice
	#	
	# 描画開始スライスがフレーム上部にどれだけ隠れているかを計算
	#
  Assign(frameLSliceL0L, v7)
  Assign(frameLSliceL0H, v8)
	#
	# frameLSliceL0 = frameSliceLines * imageNumSlice
	#
	vmudn	frameLSliceL0L, frameSliceLinesL, imageNumSlice[0]	# [0]
	vmadh	frameLSliceL0H, frameSliceLinesH, imageNumSlice[0]
  Assign(frameLYsliceL, v17)
	#
	# frameLYslice  = frameLSliceL0 & ~1023
	#
	vand	frameLYsliceL, frameLSliceL0L, _0xfc00		# [0]
  Assign(frameLHiddenL, v19)
  Assign(frameLHiddenH, v20)
	#
	# frameLHidden = frameLYoffset - frameLYslice
	#
	vsubc	frameLHiddenL, frameLYoffsetL, frameLYsliceL
	vsub	frameLHiddenH, frameLYoffsetH, frameLSliceL0H
  EndAssign(frameLYoffsetL, v14)
  EndAssign(frameLYoffsetH, v15)
  EndAssign(frameLYsliceL, v17)
  Assign(imageLHiddenL, v14)
  Assign(imageLHiddenH, v15)
	#
	# imageLHidden = (frameLHidden >> 10) * scaleH
	#
	vmudl	vtmp,          frameLHiddenL, _0x0040
	vmadm	imageLHiddenH, frameLHiddenH, _0x0040
	vmadn	imageLHiddenL, vconst,        _0x0000		# [0]
	vmudn	imageLHiddenL, imageLHiddenL, vscale[4]
	vmadh	imageLHiddenH, imageLHiddenH, vscale[4]		# [0]

	### imageFlip, tptr
	### tmemParam, imageSliceLinesv
	### vscale, vscalei, vscalef
	### imageW, bgparami.imageSrcWH, imageXY0
	### frameSliceLinesH, frameSliceLinesL
	### imageNumSlice
	### frameLSliceL0L, frameLSliceL0H
	### frameLHiddenL, frameLHiddenH
	### imageLHiddenL, imageLHiddenH

	#
	# 描画開始スライスのフレームサイズの計算
	#   frameLSliceL0 = 
	#       (frameLSliceL0 & 1023) + frameSliceLines - frameLHidden;
	#
	vmudh	vtmp,           frameSliceLinesH, _0x0001
	vand	vtmp,           frameLSliceL0L,   _0x03ff
	vmadn	vtmp,           frameSliceLinesL, _0x0001
	vmadn	frameLSliceL0L, frameLHiddenL,    _0xffff
	vmadh	frameLSliceL0H, frameLHiddenH,    _0xffff
	ssv	frameLSliceL0H[0], oRSPBG_TMP_frameLSliceL0+0(tptr)
	ssv	frameLSliceL0L[0], oRSPBG_TMP_frameLSliceL0+2(tptr)
  EndAssign(frameLSliceL0L, v7)
  EndAssign(frameLSliceL0H, v8)
  EndAssign(frameSliceLinesL, v13)
  EndAssign(frameSliceLinesH, v3)
  EndAssign(frameLHiddenL, v19)
  EndAssign(frameLHiddenH, v20)
	### imageFlip, tptr
	### tmemParam, imageSliceLinesv
	### vscale, vscalei, vscalef
	### imageW, bgparami.imageSrcWH, imageXY0
	### imageNumSlice
	### imageLHiddenL, imageLHiddenH
	#
	# スライスを途中から描画するためのイメージパラメータの計算
	#
  Assign(imageT, v3)
	#
	# imageT = (imageLHidden >> 5) & 31
	#
	vmudm	imageT, imageLHiddenL, _0x0800
	vand	imageT, imageT, _0x001f
	#
	# imageLHidden >>= 10
	#
	vmudl	imageLHiddenL, imageLHiddenL, _0x0040
	vmadm	imageLHiddenH, imageLHiddenH, _0x0040
	vmadn	imageLHiddenL, vconst,        _0x0000			# [0]
	#
	# imageISliceL0 = imageSliceLines - imageLHidden
	#	
  Assign(imageISliceL0v, v7)
	vsub	imageISliceL0v, imageSliceLinesv, imageLHiddenL[0]	# [0]
	vmov	imageISliceL0v[1], imageSliceLinesv[0]
	j	bg1cyc_main
	slv	imageISliceL0v[0], oRSPBG_TMP_imageISliceL0(tptr)
	#
	# bg1cyc_main へ Jump する
	#
	
 #----------------------------------------------------------------------------
 #	TMEM へのロード A,B
 #----------------------------------------------------------------------------
  FixedAssign(TimagePtr,  17)
  FixedAssign(TloadLines, 18)
  FixedAssign(TtmemSH,    19)
  FixedAssign(TtmemAdrs,  20)
tmemLoad_A:
		lw	sys0, RSPBG_TMP_rdpSetTile_w0(zero)
		sbv	_TILESYNC,     0(outp)
		addi	outp, outp, 16
		or	sys0, sys0, TtmemAdrs
		sw	sys0, 8-16(outp)
		slv	_TX_LOADTILE, 12-16(outp)
tmemLoad_B:
		lw	sys0, RSPBG_TMP_rdpSetTimg_w0(zero)
		sw	TimagePtr, 4(outp)
		addi	outp, outp, 24
		sw	sys0, 0-24(outp)
		sbv	_LOADSYNC, 8-24(outp)
		lui	sys0, 0xf400
		sw	sys0, 16-24(outp)
		addi	sys0, TtmemSH, 0x0700-1
		sh	sys0, 20-24(outp)
		sll	sys0, TloadLines, 2
		addi	sys0, sys0, -1
		jr	return
		sh	sys0, 22-24(outp)

 #----------------------------------------------------------------------------
 #	テクスチャのロード
 #	imagePtr
 #	imageRemain
 #	imageSliceH	を入力とする
 #----------------------------------------------------------------------------
  Assign(imageRemain, 3)
  Assign(imageSliceH, 4)
  Assign(imagePtr, 5)
  Assign(imageSliceSize, 6)
  FixedAssign(loadParam, v24)
  FixedAssign(vtmp2,     v25)
  FixedAssign(vtmp3,     v26)
#define		loadLines	gfx0
#define		iLoadable	gfx1
bg1cyc_tmemLoad:
	lbu	sys0, RSPBG_TMP_flagBilerp(zero)	
	lbu	sys1, RSPBG_TMP_flagSplit(zero)	# bg1cyc_tmemLoad_C2 まで保存
	add	loadLines, imageSliceH, sys0
	sub	iLoadable, imageRemain, sys1
	sub	sys0, iLoadable, loadLines
	bltz	sys0, bg1cyc_tmemLoad_Connect
	lhu	TtmemSH, RSPBG_TMP_tmemSliceWmax(zero) 	# in Delay
	#
	# 通常のロード処理
	#
	_mov	(TloadLines, loadLines)
	jal	tmemLoad_B
	_mov	(TimagePtr, imagePtr)
	sub	imageRemain, imageRemain, imageSliceH
	j	bg1cyc_tmemLoad_return
	add	imagePtr, imagePtr, imageSliceSize
	
bg1cyc_tmemLoad_Connect:
	#
	# 下端境界をまたぐ場合
	#
	#  SubSliceY2 = *imageRemain;
	#  SubSliceL2 = loadLines - SubSliceY2;
	#
#define  SubSliceY2	TtmemAdrs
#define  SubSliceL2	TloadLines
#define  imagePtr2      TimagePtr
	mtc2	TtmemSH, loadParam[0]	# [0|1]=[tmemSliceWmaxv|imageSrcWsize]
	sub	SubSliceL2, loadLines, imageRemain
	blez	SubSliceL2, bg1cyc_tmemLoad_C2
	addi	SubSliceY2, imageRemain, 0
	# if (SubSliceL2 > 0)
	# {
		#
		# imagePtr2 = imageTop + imagePtrX0
		#
		lw	imagePtr2, RSP_INPUT_BG+RSP_BG_IMAGEPTR(zero)
		lhu	sys0,      RSPBG_TMP_imagePtrX0(zero)
		andi	return, SubSliceY2, 1
		beq	return, zero, bg1cyc_tmemLoad_C1
		add	imagePtr2, imagePtr2, sys0
		# 
		# if (SubSliceY2 & 1)
		# {
			#
			# imagePtr2 -= imageSrcWsize
			# SubSliceY2 --
			# SubSliceL2 ++
			#
			lhu	sys0, RSPBG_TMP_imageSrcWsize(zero)
			addi	SubSliceY2, SubSliceY2, -1
			addi	SubSliceL2, SubSliceL2,  1
			sub	imagePtr2, imagePtr2, sys0
		# }
bg1cyc_tmemLoad_C1:
		#
		# tmemLoad_A(pkt, imagePtr2,
		#    SubSliceL2, SubSliceY2 * tmemSliceWmax, tmemSliceWmax);
		#
		mtc2	SubSliceY2, vtmp[0]
		vmudn	vtmp, vtmp, loadParam[0]
		jal	tmemLoad_A
		mfc2	TtmemAdrs, vtmp[0]
	#}
bg1cyc_tmemLoad_C2:	
#undef  SubSliceY2
#undef  SubSliceL2
#undef  imagePtr2
	#
	# ここまで sys1 を保存しておく
	#	
	beq	sys1, zero, bg1cyc_tmemLoad_C3
	lsv	loadParam[2], oRSPBG_TMP_imageSrcWsize(tptr)
	#
	# if (flagSplit)
	# {
	#	
#define	 imagePtr1A     sys1
#define	 imagePtr1B	TimagePtr
#define  SubSliceL1	TloadLines
		#
		# imagePtr1A = (*imagePtr) + iLoadable * imageSrcWsize
		# imagePtr1B = imageTop
		# SubSliceY1 = iLoadable
		#
		mtc2	iLoadable, vtmp3[2]
		vmudn	vtmp,  vtmp3,  loadParam[1]
		vmadh	vtmp2, vconst, _0x0000
		ssv	vtmp [2], oRSPOBJ_VSCRATCH+2(vecptr)
		ssv	vtmp2[2], oRSPOBJ_VSCRATCH+0(vecptr)
		lw	sys0,     oRSPOBJ_VSCRATCH+0(vecptr)
		lw	imagePtr1B, RSP_INPUT_BG+RSP_BG_IMAGEPTR(zero)
		andi	SubSliceL1, iLoadable, 1
		beq	SubSliceL1, zero, bg1cyc_tmemLoad_C4
		add	imagePtr1A, imagePtr, sys0
		# if (SubSliceL1 = iLoadable & 1)
		# {
			#
			# imagePtr1A -= imageSrcWsize
			# imagePtr1B -= imageSrcWsize
			# SubSliceY1 --		/* as vtmp3[1] */
			#
			lhu	sys0, RSPBG_TMP_imageSrcWsize(zero)
			vsub	vtmp3, vtmp3, _0x0001
			sub	imagePtr1A, imagePtr1A, sys0
			sub	imagePtr1B, imagePtr1B, sys0
		# }
bg1cyc_tmemLoad_C4:
		#
		# SubSliceL1 ++
		# tmemSH_A = (imageSrcWsize - imagePtrX0) >> 3
		# tmemSH_B =  tmemSliceWmax - tmemSH_A
		#
		lsv	loadParam[4], oRSPBG_TMP_imagePtrX0(tptr)
		addi	SubSliceL1, SubSliceL1, 1
		vsub	vtmp, loadParam, loadParam[2]	# [1]
		vmudm	vtmp,  vtmp, _0x2000		# tmemSH_A
		vsub	vtmp2, loadParam, vtmp[1]	# [0]
		mfc2	TtmemSH, vtmp2[0]		# tmemSH_B
		#
		vmudn	vtmp2, vtmp3, loadParam[0]	# [1]
		vmadn	vtmp3, vtmp,  _0x0001		# [1]
		jal	tmemLoad_A
		mfc2	TtmemAdrs, vtmp3[2]
		_mov	(TimagePtr, imagePtr1A)
		mfc2	TtmemAdrs, vtmp2[2]
		jal	tmemLoad_A
		mfc2	TtmemSH,   vtmp[2]
	#}
#undef	imagePtr1A
#undef	imagePtr1B
#undef	SubSliceL1
bg1cyc_tmemLoad_C3:
	bgtz	iLoadable, bg1cyc_tmemLoad_C5
	sub	imageRemain, imageRemain, imageSliceH
	#
	# [SetTile]
	#{
		lw	sys0, RSPBG_TMP_rdpSetTile_w0(zero)
		addi	outp, outp, 8
		sw	sys0, 0-8(outp)
		j	bg1cyc_tmemLoad_C6
		slv	_TX_LOADTILE, 4-8(outp)
	#}
bg1cyc_tmemLoad_C5:
	#
	# tmemLoad_A(pkt, *imagePtr, iLoadable, 0, tmemSliceWmax)
	#{
		_mov	(TimagePtr, imagePtr)
		lhu	TtmemSH, RSPBG_TMP_tmemSliceWmax(zero)
		_mov	(TloadLines, iLoadable)
		jal	tmemLoad_A
		_li	(TtmemAdrs, 0)
	#}
bg1cyc_tmemLoad_C6:
	bgtz	imageRemain, bg1cyc_tmemLoad_return
	#{
		add	imagePtr, imagePtr, imageSliceSize
	#} else {
	#
	#    imagePtr = imageTop - imageRemain * imageSrcWsize + imagePtrX0
	#    imageRemain = tmemSrcLines + imageRemain
	#
		sub	sys0,  zero, imageRemain
		mtc2	sys0,  vtmp[0]
		lhu	sys0,     RSPBG_TMP_imagePtrX0(zero)
		lw	imagePtr, RSP_INPUT_BG+RSP_BG_IMAGEPTR(zero)
		vmudn	vtmp,  vtmp,   loadParam[1]
		vmadh	vtmp2, vconst, _0x0000
		add	imagePtr, imagePtr, sys0
		ssv	vtmp[0],  oRSPOBJ_VSCRATCH+2(vecptr)
		lhu	sys1,     RSPBG_TMP_tmemSrcLines(zero)
		ssv	vtmp2[0], oRSPOBJ_VSCRATCH+0(vecptr)
		lw	sys0,     oRSPOBJ_VSCRATCH+0(vecptr)
		add	imageRemain, imageRemain, sys1
		j	bg1cyc_tmemLoad_return
		add	imagePtr,    imagePtr,    sys0		
	#}
 #}	
  EndAssign(TimagePtr, 17)
  EndAssign(TloadLines, 18)
  EndAssign(TtmemSH, 19)
  EndAssign(TtmemAdrs, 20)
  EndAssign(loadParam, v24)
  EndAssign(vtmp2, v25)
  EndAssign(vtmp3, v26)
#undef	loadLines
#undef	iLoadable
 #!Reserved  (17,18,19,20,v24,v25,v26)
		
#else
#undef	BG1CYCL_LIST
 #----------------------------------------------------------------------------
 #	後半パート
 #----------------------------------------------------------------------------
bg1cyc_main:
	#
	# imageIY0 = imageSliceLines * imageNumSlice
	#          + (imageYorig & ~31) / 32 + imageLHidden;
	#
  Assign(imageIY0v, v8)
  Assign(imageYorigL, v13)
  Assign(imageYorigH, v17)
	lsv	imageYorigL[0], oRSPBG_TMP_imageYorig+2(tptr)
	lsv	imageYorigH[0], oRSPBG_TMP_imageYorig+0(tptr)
	vmudn	vtmp, imageSliceLinesv, imageNumSlice	
	vmadn	vtmp, imageLHiddenL,   _0x0001
	vmadh	vtmp, imageLHiddenH,   _0x0001
	vmadm	vtmp,      imageYorigH, _0x0800
	vmadl	imageIY0v, imageYorigL, _0x0800		# [0]
  EndAssign(imageNumSlice, v18)
  EndAssign(imageYorigL, v13)
  EndAssign(imageYorigH, v17)
  EndAssign(imageLHiddenL, v14)
  EndAssign(imageLHiddenH, v15)
  EndAssign(imageSliceLinesv, v16)
	#
	#  if (imageIY0 < 0)                    imageIY0 += (bg->s.imageH>>2)
	#  if (imageIY0 >= (bg->s.imageH >> 2)) imageIY0 -= (bg->s.imageH>>2)
	#
  Assign(subparam, v13)
  Assign(addparam, v14)
	vsub	subparam,  imageIY0v, bgparami[5]
	vadd	addparam,  imageIY0v, bgparami[5]
	vge	vtmp,      imageIY0v, _0x0000
	vmrg	imageIY0v, imageIY0v, addparam
	vge	vtmp,      subparam,  _0x0000
	vmrg	imageIY0v, subparam,  imageIY0v
  EndAssign(bgparami, v10)
  EndAssign(subparam, v13)
  EndAssign(addparam, v14)
	### imageFlip, tptr
	### tmemParam, imageT, imageISliceL0v
	### vscale, vscalei, vscalef
	### imageW, imageSrcWH, imageXY0
	### imageIY0v

  Assign(imageParam, v10)
	#
	# imageSrcWsize = (imageSrcW / tmemShift) << 3
	# imagePtrX0    = (imageX0   / tmemShift) << 3
	#
	vmov	imageXY0[1],   imageSrcWH[1]
	vmudm	imageParam,    imageXY0,   tmemParam[1]
	vmudn	imageParam,    imageParam, _0x0008
	slv	imageParam[0], oRSPBG_TMP_imagePtrX0(tptr)
	ssv	imageIY0v[0],  oRSPBG_TMP_imageIY0(tptr)
	
  EndAssign(imageSrcWH, v2)
  Assign(imagePtrL, v2)
  Assign(imagePtrH, v13)
	#
	# imagePtr のアドレス変換
	# imagePtr = imageTop + imageSrcWsize * imageIY0 + imagePtrX0;
	#
		lw	gfx1, RSP_INPUT_BG+RSP_BG_IMAGEPTR(zero)
	vmudn	vtmp,      imageParam, _0x0001   
		jal	AdrsFixup
	vmadn	imagePtrL, imageIY0v,  imageParam[1]	# [0]
#ifdef	UCODE_S2DEX2
		sw	gfx1, RSP_INPUT_BG+RSP_BG_IMAGEPTR(zero)
#else
		sw	$19,  RSP_INPUT_BG+RSP_BG_IMAGEPTR(zero)
#endif
	vmadh	imagePtrH, vconst,     _0x0000
	ssv	imagePtrL[0], oRSPOBJ_VSCRATCH+2(vecptr)
	ssv	imagePtrH[0], oRSPOBJ_VSCRATCH+0(vecptr)
	lw	imagePtr, oRSPOBJ_VSCRATCH+0(vecptr)
#ifdef	UCODE_S2DEX2
	add	imagePtr, imagePtr, gfx1
#else
	add	imagePtr, imagePtr, $19
#endif
  EndAssign(imageIY0v, v8)
  EndAssign(imagePtrL, v2)
  EndAssign(imagePtrH, v13)

  Assign(imageSliceSizeL, v2)
  Assign(imageSliceSizeH, v8)
	#
	# imageSliceSize = imageSrcWsize * imageSliceH
	#
	vmudm	imageSliceSizeH, imageISliceL0v, imageParam[1]	# [0]
	vmadn	imageSliceSizeL, vconst,         _0x0000
	ssv	imageSliceSizeH[0], oRSPBG_TMP_imageSliceSize0+0(tptr)
	ssv	imageSliceSizeH[2], oRSPBG_TMP_imageSliceSize+0(tptr)
	ssv	imageSliceSizeL[0], oRSPBG_TMP_imageSliceSize0+2(tptr)
	ssv	imageSliceSizeL[2], oRSPBG_TMP_imageSliceSize+2(tptr)
  EndAssign(imageSliceSizeL, v2)
  EndAssign(imageSliceSizeH, v8)	
  EndAssign(imageParam, v10)
  EndAssign(imageISliceL0v, v7)
	
  Assign(imageS, v2)
	beq	imageFlip, zero, bg1cyc_skip3
	vand	imageS, imageXY0, tmemParam[0]	# [0]
	vmudn	vtmp,   imageS,   _0xffff
	vmadn	imageS, imageW,   _0xffff	# [0]
bg1cyc_skip3:	
  EndAssign(imageFlip, 2)
  EndAssign(imageXY0, v11)
  EndAssign(imageW, v9)
  EndAssign(tmemParam, v12)
	#
	# RDP コマンドの定数値の作成
	#
  Assign(rdpSetTimg_w0, 2)
  Assign(rdpSetTile_w0, 7)
	lhu	sys0, RSPBG_TMP_imageSrcWsize(zero)
	lhu	sys1, RSPBG_TMP_tmemSliceWmax(zero)
	lui	rdpSetTimg_w0, 0xfd10
	srl	sys0, sys0, 1
	addi	sys0, sys0, -1
	or	rdpSetTimg_w0, rdpSetTimg_w0, sys0
	sw	rdpSetTimg_w0, RSPBG_TMP_rdpSetTimg_w0(zero)
	lui	rdpSetTile_w0, 0xf510
	sll	sys1, sys1, 9
	or	rdpSetTile_w0, rdpSetTile_w0, sys1
	sw	rdpSetTile_w0, RSPBG_TMP_rdpSetTile_w0(zero)
	
	# [SetTile:7]
	sw	rdpSetTile_w0, 0(outp)
	slv	_TX_LOADTILE,  4(outp)
	# [SetTile:0]
	sw	rdpSetTile_w0, 8(outp)
	lbu	sys0, RSP_INPUT_BG+RSP_BG_IMAGEFMT(zero)
	lbu	sys1, RSP_INPUT_BG+RSP_BG_IMAGESIZ(zero)
	sll	sys0, sys0, 5
	sll	sys1, sys1, 3
	or	sys0, sys0, sys1
	sb	sys0, 9(outp)
	lbu	sys0, RSP_INPUT_BG+RSP_BG_IMAGEPAL(zero)
	lw	sys1, RSPOBJ_SETTILE_MIRROR(zero)
	sll	sys0, sys0, 20
	or	sys0, sys0, sys1
	sw	sys0, 12(outp)
	# [SetTileSize:7]
	slv	_SETTILESIZE, 16(outp)	# CMD.0=0x32000000
	sw	zero, 20(outp)		# CMD.1=0x00000000
	addi	outp, outp, 24
  EndAssign(rdpSetTimg_w0, 2)
  EndAssign(rdpSetTile_w0, 7)

	### tptr, imagePtr, imageSliceH, imageRemain
	### imageS, imageT
	### vscale, vscalei, vscalef
	#
	# imageRemain = (imageH >> 2) - imageIY0
	# imageSliceH     = imageISliceL0;
	# frameSliceCount = frameLSliceL0;
	#
  Assign(frameSliceCount, 2)
  Assign(frameX0, 7)
  Assign(frameX1, 8)
  Assign(framePtrY0, 9)
  Assign(framePtrY1, 10)
  Assign(frameSliceH, 13)
  Assign(frameRemain, 14)
  Assign(frameSliceLines, 15)
	lhu	imageRemain,     RSP_INPUT_BG+RSP_BG_IMAGEH(zero)
	lhu	sys0,            RSPBG_TMP_imageIY0(zero)
	lh	imageSliceH,     RSPBG_TMP_imageISliceL0(zero)
	lw	frameSliceCount, RSPBG_TMP_frameLSliceL0(zero)
	lw	frameSliceLines, RSPBG_TMP_frameSliceLines(zero)
	lhu	frameRemain,     RSPBG_TMP_frameRemain(zero)
	srl	imageRemain, imageRemain, 2
	sh	imageRemain, RSPBG_TMP_tmemSrcLines(zero)
	sub	imageRemain, imageRemain, sys0
	lw	imageSliceSize, RSPBG_TMP_imageSliceSize0(zero)
	lhu	frameX0, RSPBG_TMP_frameX0(zero)
	lhu	frameX1, RSPBG_TMP_frameX1(zero)
	lhu	framePtrY0, RSPBG_TMP_framePtrY0(zero)
	sll	frameX0, frameX0, 12
	sll	frameX1, frameX1, 12
	#
	# ループ
	#	
  Assign(imagePvL, v7)
  Assign(imagePvH, v8)
bg1cyc_mainloop:
	#
	# 描画ライン数の計算をし, 描画を行なうかどうか判定する
	#
	srl	frameSliceH, frameSliceCount, 10
	bgtz	frameSliceH, bg1cyc_mainloop_1
	nop
	#
	# if (frameSliceH <= 0)
	# {
		# 描画をしない場合
		# imageRemain -= imageSliceH
		# if (imageRemain > 0){
		#   
		sub	imageRemain, imageRemain, imageSliceH
		bgtz	imageRemain, bg1cyc_mainloop_2
		# {
		#	imagePtr += imageSrcWsize * imageSliceH
		#
			add	imagePtr, imagePtr, imageSliceSize
		#
		# } else {
		#	imagePtr = imageTop
		#		 - (imageRemain * imageSrcWsize) + imagePtrX0
		#	imageRemain += tmemSrcLines
		#
			lsv	imagePvL[0], oRSPBG_TMP_imageSrcWsize(tptr)
			sub	sys0, zero, imageRemain
			mtc2	sys0, imagePvL[2]
			lw	imagePtr, RSP_INPUT_BG+RSP_BG_IMAGEPTR(zero)
			vmudn	imagePvL, imagePvL, imagePvL[1]
			vmadh	imagePvH, vconst,   _0x0000
			ssv	imagePvL[0], oRSPOBJ_VSCRATCH+2(vecptr)
			ssv	imagePvH[0], oRSPOBJ_VSCRATCH+0(vecptr)
			lw	sys0, oRSPOBJ_VSCRATCH+0(vecptr)
			lhu	sys1, RSPBG_TMP_imagePtrX0(zero)
			lhu	return, RSPBG_TMP_tmemSrcLines(zero)
			add	imagePtr, imagePtr, sys0
			add	imagePtr, imagePtr, sys1
			j	bg1cyc_mainloop_2
			add	imageRemain, imageRemain, return
		# }
	# } else {
bg1cyc_mainloop_1:
		#  描画をする場合
		#
		# frameSliceCount &= 1023
		# if ((frameRemain -= frameSliceH) < 0)
		# {
		#
		sub	frameRemain, frameRemain, frameSliceH
		bgez	frameRemain, bg1cyc_mainloop_3
		andi	frameSliceCount, frameSliceCount, 1023
			#
			# 最終スライスの処理
			# 
			# imageSliceH += ((frameRemain * scaleH) >> 10) + 1
			#
			mtc2	frameRemain, vtmp[0]
			vmudn	imagePvL, vscale, vtmp[0]	# [4]
			vmadh	imagePvH, vconst, _0x0000
			lhu	sys1, RSPBG_TMP_imageSliceLines(zero)
			ssv	imagePvH[8], oRSPOBJ_VSCRATCH+0(vecptr)
			ssv	imagePvL[8], oRSPOBJ_VSCRATCH+2(vecptr)
			lw	sys0,        oRSPOBJ_VSCRATCH+0(vecptr)
			addi	imageSliceH, imageSliceH, 1
			sra	sys0, sys0,  10
			add	imageSliceH, imageSliceH, sys0
			#
			# frameSliceH += frameRemain
			# if (imageSliceH > imageSliceLines)
			# {
			#
			sub	sys0, imageSliceH, sys1
			bltz	sys0, bg1cyc_mainloop_3
			add	frameSliceH, frameSliceH, frameRemain
				#	imageSliceH = imageSliceLines
				addi	imageSliceH, sys1, 0
			# }
bg1cyc_mainloop_3:	
  EndAssign(imagePvL, v7)
  EndAssign(imagePvH, v8)
		#
		# テクスチャロード
		#
		j	bg1cyc_tmemLoad
		#
		# 描画フレーム範囲の取得
		#
		add	framePtrY1, framePtrY0, frameSliceH
bg1cyc_tmemLoad_return:	
		#
		# [PipeSync] 先行する LOADTILE 命令の終了を待つ
		#
		sbv	_PIPESYNC,  0(outp)
		#
		# Rectangle 描画
		#
		sll	sys0, framePtrY1, 2
		or	sys0, sys0, frameX1
		sw	sys0,       8(outp)
		sbv	_TXTRRECT,  8(outp)
		sll	sys0, framePtrY0, 2
		or	sys0, sys0, frameX0	
		sw	sys0,      12(outp)
		ssv	imageS[0], 16(outp)
		ssv	imageT[0], 18(outp)
		ssv	vscale[0], 20(outp)
		ssv	vscale[8], 22(outp)
		jal	OutputClose
		addiu	outp, outp, 24
		# フレームの未描画ライン数が無くなったら終わる
		blez	frameRemain, GfxDone
		# 領域更新
		addi	framePtrY0, framePtrY1, 0
	# }
bg1cyc_mainloop_2:
	add	frameSliceCount, frameSliceCount, frameSliceLines
	lh	imageSliceH, RSPBG_TMP_imageSliceLines(zero)
	lw	imageSliceSize, RSPBG_TMP_imageSliceSize(zero)
	j	bg1cyc_mainloop
	vxor	imageT, imageT, imageT
  EndAssign(frameX0, 7)
  EndAssign(frameX1, 8)
  EndAssign(framePtrY0, 9)
  EndAssign(framePtrY1, 10)
  EndAssign(frameSliceH, 13)
  EndAssign(frameRemain, 14)
  EndAssign(frameSliceLines, 15)
  EndAssign(frameSliceCount, 2)
  EndAssign(vscale, v4)
  EndAssign(vscalei, v5)
  EndAssign(vscalef, v6)
  EndAssign(imageS, v2)
  EndAssign(imageT, v3)  
  EndAssign(imagePtr, 5)
  EndAssign(imageSliceH, 4)
  EndAssign(imageRemain, 3)
  EndAssign(imageSliceSize, 6)
  EndAssign(tptr, 1)
#endif
	
/*======== End of gs2bg1cycL.s ========*/