gmain.s 13.2 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


/**************************************************************************
 *								          *
 *               Copyright (C) 1994, Silicon Graphics, Inc.       	  *
 *								          *
 *  These coded instructions, statements, and computer programs  contain  *
 *  unpublished  proprietary  information of Silicon Graphics, Inc., and  *
 *  are protected by Federal copyright  law.  They  may not be disclosed  *
 *  to  third  parties  or copied or duplicated in any form, in whole or  *
 *  in part, without the prior written consent of Silicon Graphics, Inc.  *
 *								          *
 *************************************************************************/

/*
 * File:		gmain.s
 * Creator:		hsa@sgi.com
 * Create Date:		Mon Jun 20 11:48:48 PDT 1994
 *
 * This is the 'graphics task' for the RSP. it takes blocks of display
 * list commands and interprets them:	 transforming, shading, clipping,
 * DDA setup, etc., preparing geometry to be rasterized. It is also the
 * center of the graphics system from the game's point of view, and
 * receives all of the RDP control commands, which it passes along.
 *
 * In the absence of a linker, the different source code modules of the
 * graphics task will be include'd here during compilation. See the Makefile
 * for details.
 *
 */


#include <rsp.h>
#include <rcp.h>
#include <os.h>
#include <sptask.h>
#include "mbi.h"

#ifdef FASTLIGHT3D
		.text	RSP_IMEM_BASE	# this is coordinated with rspboot.s
#else /* FASTLIGHT3D */
		.text	TASKBASE	# this is coordinated with rspboot.s
#endif /* FASTLIGHT3D */
		.data	0
	
#define MAIN
#include "gdmem.h"
#include "gfx_regs.h"

		.text

	#################################################################
	#
	#  Begin task initialization
	#
	#  Register $1 holds the task header address
	# format for the task header:
	#
	# (see task.h)
	#

 #=================================================
 #======= in FASTLIGHT doInit is an overlay =======
 #=================================================
#ifdef FASTLIGHT3D

#include "newt.s"
 #		j	doInit		# initialization routine
	# set state pointer:
 #		addi	rsp_state, zero, RSP_STATE_OFFSET
 ### JUMP OCCURS here to doInit:
	
 #=================================================
 #======= ...Otherwise it is #included here =======
 #=================================================
#else /* FASTLIGHT3D */

	# set state pointer:
		addi	rsp_state, zero, RSP_STATE_OFFSET

#include "ginit.s"

#endif /* FASTLIGHT3D */
	
		.ent	main

	#################################################################
	#
	# Note about display list command processing:	
	#
	#    - 'inp' always points to the "next" DL command in DRAM. This
	# 	is the pointer pushed on the stack and used to retrieve the
	#	next block of DL commands.
	#
	#    - 'dinp' is the local pointer in DMEM to the part of the display
	#	list in DMEM.
	#
	#    - 'dlcount' parallels dinp, counting down the remaining DL
	#	commands in DMEM.
	#
	# So when (dlcount == 0) we need to fetch more display list.
	#
	# NOTE:
	# It would be useful to support an "immediate" mode from the host.
	# This would allow a better performance/space trade-off using 
	# generated data, etc. In order to do this we need to modify the
	# display list processing -- basically allow the gtask to go idle
	# and wait for more display list, rather than exit. Needs a couple
	# status registers to sync with the host (GO, IDLE, etc.)
	#
	# Fri Oct  7 10:45:04 PDT 1994
	#
	#################################################################
	#
	#
DMAWaitDL:
		jal	DMAwait
		# note delay slot...		
	
 	# process gfx list:	
.name	dlcmd,	$1
		addi	dinp, zero, RSP_DLINPUT_OFFSET	# delay
DecodeDL:	lw	gfx0, 0(dinp)
		lw	gfx1, 4(dinp)
		srl	dlcmd, gfx0, 29
		andi	dlcmd, dlcmd, 0x06	# GFX op type (jump offset)

	# advance these pointers after consuming gfx0/1
		addi	inp, inp, 8
		addi	dinp, dinp, 8
		addi	dlcount, dlcount, -8
	
	#
	# Get a head start on the next command, if it's a DMA.
	# The register in_bufp points to the fetched data, and
	# should implment the double-buffering...
	#
.name dma_len,	$18
.name dram_addr, $19
.name dmem_addr, $20
		bgtz	dlcmd, ContDecode	# not a DMA...
		# note delay slot	
	#
	# despite being a 16-bit field, the maximum DMA transfer
	# is 256 bytes (16 vertices). Since the G_DL command will
	# also come through here, with a much larger DMA length, we
	# protect the length by masking to 0x01ff...
	#

	# WARNING!
	# We are passing the wrong length to the DMA routine here. 
	# We should pass 'length-1'. We choose not to spend the extra 
	# instruction and correct this, because the place in DMEM 
	# that will potentially be trashed by the extra DMA is 
	# guarenteed to be trash-able. (setup tmp area)
	#
		andi	dma_len, gfx0, 0x01ff	# pick off DMA length
		addi	in_bufp, zero, RSP_INPUT_OFFSET

		jal	AddrFixup
		add	dram_addr, gfx1, zero		# delay slot

		add	dmem_addr, zero, in_bufp
		jal	DMAproc
		addi	$17, zero, 0	# delay slot
.unname dma_len
.unname dram_addr
.unname dmem_addr

	# we do the wait for the DMA in the gdma module. This
	# lets us do some useful work while we wait...
	
.name op_addr, $2
    ContDecode:		
				# compute jump address
		lh	op_addr, OPTYPE_JMP_OFFSET(dlcmd)
		jr	op_addr
	# this delay slot is the first thing the other routines do...
		srl	$2, gfx0, 23		# table byte offset...
.unname op_addr
.unname dlcmd

	

.name 	yield,		$2
.name	overeturn,	$21	# return address from loadOverlay
	
GfxDone: 	# we're done with this one, do the next one (if available)...
	#
	# stick our head up, see if we need to yield the SP. If so,
	# checkpoint everything then exit.
	#						#
		mfc0	yield, SP_STATUS		# need to yield?
		andi	yield, yield, SP_STATUS_YIELD	#
		bne	yield, zero, RSPYield		#
		lh	overeturn, TASKYIELD(zero)    	# return to RSPYield
							#
.unname yield						#
							#
noYield:						#
 		bgtz	dlcount, DecodeDL		#
		nop					#
 ### BRANCH OCCURS to DecodeDL if dlcount is not zero yet
		j	LoadDL				#
		lh	return, DMAWAITDL(zero)		# return to DMAWaitDL
 ### BRANCH OCCURS to LoadDL: and returns to DMAWaitDL:
							#
		.end	main				#
							#
 #======================================================================
 #======= in FASTLIGHT TaskDone & Yield are overlays - load them =======
 #======================================================================
#ifdef FASTLIGHT3D
.unname	return_save
.name	overp,		$30	# which overlay to load
TaskDone:
		lh	overeturn, TASKDONE(zero)        # return to start
RSPYield:
		j	loadOverlay		  # Load the overlay
		ori	overp, zero, OVERLAY_DONE # Load gdone overlay
 ### BRANCH OCCURS HERE TO loadOverlay:

.unname overp
.unname overeturn
.name	return_save,	$30
 #======================================================================
 #======= ...Otherwise include Done and Yield here (Yield is in done) ==
 #======================================================================
#else /* FASTLIGHT3D */
.unname overeturn

#include "gdone.s"
#endif /* FASTLIGHT3D */
	#
	#
	##################################################################

	##################################################################
	#
	# start the DMA of the display list into DMEM.
	#
	# Always reads RSP_DLINPUT_SIZE8 bytes into the buffer. We
	# require lists to be terminated with G_ENDDL, so this might be
	# reading a little beyond the actual end.
	#
	# Registers upon call:
	#		inp	pointer to read from
	#		return	where to go when we're done
	#
	# Registers upon return:
	#		dlcount	size actually read (bytes)
	#		dinp	pointer to data we read in
	#
	# Registers used:
	#		return, zero, inp, dlcount, dinp
	#

		.ent	LoadDL
LoadDL:		
 		addi	dlcount, zero, RSP_DLINPUT_SIZE8
		add	$21, zero, return
 		addi	$20, zero, RSP_DLINPUT_OFFSET
		add	$19, zero, inp
 		addi	$18, zero, RSP_DLINPUT_SIZE8 - 1
		jal	DMAproc
		addi	$17, zero, 0
		jr	$21
		addi	dinp, zero, RSP_DLINPUT_OFFSET # delay slot

		.end LoadDL
	#
	#
	##################################################################

   	##################################################################
   	#
   	# This routine loads one of the overlays from the DMEM table.
   	# Register $4 has the index into the overlay table to load.
   	#
   	# Assumes overlay table is the first thing in DMEM.
   	#
.unname	return_save
.name	overp,		$30
.name	imem_addr,	$20
.name	dram_addr,	$19
.name	dma_len,	$18
.name	iswrite,	$17
.name	overeturn,	$21
	
	loadOverlaySR:
		add	overeturn, zero, return
	loadOverlay:
		lw	dram_addr, OVERLAY_OFFSET(overp)
		lh	dma_len,   OVERLAY_SIZE(overp)
		lh	imem_addr, OVERLAY_DEST(overp)
		jal	DMAproc
		addi	iswrite, zero, 0	# delay slot
		jal	DMAwait
		nop
		jr	overeturn
		# note delay slot
.unname	overp
.unname	imem_addr
.unname	dram_addr
.unname	dma_len
.unname	iswrite
.unname	overeturn
.name	return_save,	$30
   	#
   	#
   	##################################################################
	
	##################################################################
	#
	# Address Fix-up routine.
	#
	# Takes a segment/offset address in register $19, computes
	# the proper DRAM address using the segment table. Returns
	# the answer in register $19
	#
	# (The use of register $19 is NOT random... It's the DRAM
	# address used in DMAproc, often the next thing called.)
	#
	# This code is shared among several routines, the registers
	# used are chosen not to conflict with those.
	#
.name dma_addr,	$19
.name mask, 	$11
.name seg_id,	$12
.name seg_ptr,	$13
		.ent	AddrFixup
    AddrFixup:
		lw	mask, SEGADDR_MASK_OFFSET(zero)
		srl	seg_id, dma_addr, 22
		andi	seg_id, seg_id, 0x3c	# Seg num only RWW
 #		sll	seg_id, seg_id, 2	# leave mult. by 4 for offset
		and	dma_addr, dma_addr, mask
		add	seg_ptr, zero, seg_id
		lw	seg_id, RSP_SEG_OFFSET(seg_ptr)
		jr	return
		add	dma_addr, dma_addr, seg_id	# delay slot
		.end	AddrFixup
.unname dma_addr
.unname mask
.unname seg_id
.unname seg_ptr
	#
	##################################################################
	

	##################################################################
	#
	# Procedure to do DMA reads/writes.
	#
	# Registers:
	#
	#	$20	mem_addr
	#	$19	dram_addr
	#	$18	dma_len
	#	$17	iswrite?
	#
	#	$11	used as tmp
	#
.name	mem_addr,	$20
.name	dram_addr,	$19
.name	dma_len,	$18
.name	iswrite,	$17
.name	tmp,		$11

DMAproc:
#if 0
/* this code is not needed, we rely on the CPU's good
 * behavior to not be using the DMA engine.
 */
	# request DMA access: (get semaphore)
		mfc0	tmp, SP_RESERVED
		bne	tmp, zero, DMAproc		
		# note delay slot
#endif

		mfc0	tmp, DMA_FULL
		bne	tmp, zero, DMAproc
		nop
	# set DMA registers:
		mtc0	mem_addr, DMA_CACHE
	# handle writes:
		bgtz	iswrite, DMAWrite
 		mtc0	dram_addr, DMA_DRAM

		jr	return
		mtc0	dma_len, DMA_READ_LENGTH

	DMAWrite:
		jr	return
		mtc0	dma_len, DMA_WRITE_LENGTH
.unname	mem_addr
.unname	dram_addr
.unname	dma_len
.unname	iswrite
.unname	tmp
	#
	#
	##################################################################
	
	##################################################################
	#
	# Procedure to do DMA waits.
	#
	# Registers:
	#
	#	$11	used as tmp
	#
 #.name	tmp,	$20
 #.name	tmp,	$16 !! $16 needed as clr2 in lighting
.name	tmp,	$11
	
DMAwait:		
#if 0
/* this code is not needed, we rely on the CPU's good
 * behavior to not be using the DMA engine.
 */
	# request DMA access: (get semaphore)
		mfc0	tmp, SP_RESERVED
		bne	tmp, zero, DMAwait
		# note delay slot
#endif
		mfc0	tmp, DMA_BUSY
		bne	tmp, zero, DMAwait
		nop
		jr	return
		nop
.unname	tmp
	#
	#
	##################################################################
	
	##################################################################
	#
	# other "modules" are appended by #include
	#

 #==============================================================================
 #===== If FASTLIGHT Yield Restart is in init overlay, otherwise it's here =====
 #==============================================================================
#ifndef FASTLIGHT3D
#	define YIELD_RESTART
#	define YIELD_STOP
#	include "gyield.s"
#	undef YIELD_STOP
#	undef YIELD_RESTART
#endif /* FASTLIGHT3D */
	
 #==============================================================================
 #===== Output module (either DRAM or directly to XBUS) ========================
 #==============================================================================
#ifdef OUTPUT_DRAM
#   include "goutdram.s"
#else
#ifdef OUTPUT_FIFO
#   include "goutfifo.s"
#else
#   include "goutxbus.s"
#endif
#endif
	
 #==============================================================================
 #===== Command parsing modules for immediate, rdp, and dma commands ===========
 #==============================================================================
#include "gimm.s"
	
#include "grdp.s"
	
#include "gdma.s"
	

 #==============================================================================
 #===== Clip and/or light and/or setup code for lines or polygons ==============
 #==============================================================================
#ifdef LINE3D
#   include "gclipl.s"	
#   include "gline.s"
#else
#   include "goverlays.s"
#   ifndef NODATA
#ifndef _HW_VERSION_1
#ifdef SUPER
#       include "gssetup.s"
#else
#       include "gsetup.s"
#endif
#else
#       include "gsetup1.s"
#endif
#   endif
#endif

	EndOfProg:
		.print	__FILE__
		.print	" : IMEM used %d instructions ", (EndOfProg - 0x04001000)/4
		.print  "(must be < 1024).\n"
		.dmax	4096
	#
	#
	##################################################################