vsel.c 13 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

/**************************************************************************
 *                                                                        *
 *               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:	vsel.c
 * Creator:	hsa@sgi.com
 * Create Date:	Wed Jun 15 17:23:25 PDT 1994
 *
 * This file holds the select instructions for the VU.
 *
 */

#include <stdio.h>
#include "rsp.h"
#include "i128.h"
#include "rspctl.h"
#include "opcode.h"
#include "vu.h"
#include "trace_print.h"

#define ACC_LOW(indx,val)	((rsp_ACC[indx] & ~0xffffLL) | (val & 0xffff))

rsp_vuPipe_t	vu_SelPipe[rsp_VUPIPEDEPTH+1];
u8  vco_carry[8],vco_equal[8];

/* vector < */
static void
vlt(rsp_vuPipe_t *mp)
{
    int		element, i;
    i16		si, ti;

    rsp_VCC = 0;

    for (i=0; i<8; i++) {

	element = ((i & cmask_tab[(mp->format & 0x0e) >> 1]) + 
		   (mp->format & emask_tab[(mp->format & 0x0e) >> 1]));

	si = (i16) Get128By16(&(rsp_VUR[mp->rs]), i);
	ti = (i16) Get128By16(&(rsp_VUR[mp->rt]), element);

        if ((si < ti) || (si==ti && vco_carry[i] && !vco_equal[i])) {
            rsp_VCC |= (0x01 << i);
            Set128By16(&(mp->result), (u16) si, i);
            rsp_ACC[i] = ACC_LOW(i,si); /* HW compatibility */
        } else {
            rsp_VCC &= ~(0x01 << i);
            Set128By16(&(mp->result), (u16) ti, i);
            rsp_ACC[i] = ACC_LOW(i,ti); /* HW compatibility */
        }
        rsp_VCO = UnFlag(rsp_VCO, (0x0101  << i));

    }
}

/* vector <= */
static void
vle(rsp_vuPipe_t *mp)
{
    rsp_fprintf(stderr,"ERROR : vle not implemented. use vcl\n");
#if 0
    int		element, i;
    i16		si, ti;

    for (i=0; i<8; i++) {

	element = ((i & cmask_tab[(mp->format & 0x0e) >> 1]) + 
		   (mp->format & emask_tab[(mp->format & 0x0e) >> 1]));

	si = (i16) Get128By16(&(rsp_VUR[mp->rs]), i);
	ti = (i16) Get128By16(&(rsp_VUR[mp->rt]), element);

	if (si <= ti) {
	    rsp_VCC |= (0x01 << i);
	    Set128By16(&(mp->result), (u16) si, i);
            rsp_ACC[i] = ACC_LOW(i,si); /* HW compatibility */
	} else {
	    rsp_VCC &= ~(0x01 << i);
	    Set128By16(&(mp->result), (u16) ti, i);
            rsp_ACC[i] = ACC_LOW(i,ti); /* HW compatibility */
	}
	rsp_VCO = UnFlag(rsp_VCO, (0x01 << i));
    }
#endif /* 0 */
}

/* vector == */
static void
veq(rsp_vuPipe_t *mp)
{
    int		element, i;
    i16		si, ti;

    rsp_VCC = 0;

    for (i=0; i<8; i++) {

	element = ((i & cmask_tab[(mp->format & 0x0e) >> 1]) + 
		   (mp->format & emask_tab[(mp->format & 0x0e) >> 1]));

	si = (i16) Get128By16(&(rsp_VUR[mp->rs]), i);
	ti = (i16) Get128By16(&(rsp_VUR[mp->rt]), element);

        if (si == ti && vco_equal[i])  {
            rsp_VCC |= (0x01 << i);
            Set128By16(&(mp->result), (u16) si, i);
            rsp_ACC[i] = ACC_LOW(i,si); /* HW compatibility */
        } else {
            rsp_VCC &= ~(0x01 << i);
            Set128By16(&(mp->result), (u16) ti, i);
            rsp_ACC[i] = ACC_LOW(i,ti); /* HW compatibility */
        }

        rsp_VCO = UnFlag(rsp_VCO, (0x0101 << i));
    }
}

/* vector != */
static void
vne(rsp_vuPipe_t *mp)
{
    int		element, i;
    i16		si, ti;

    rsp_VCC = 0;

    for (i=0; i<8; i++) {

	element = ((i & cmask_tab[(mp->format & 0x0e) >> 1]) + 
		   (mp->format & emask_tab[(mp->format & 0x0e) >> 1]));

	si = (i16) Get128By16(&(rsp_VUR[mp->rs]), i);
	ti = (i16) Get128By16(&(rsp_VUR[mp->rt]), element);

        if ((si != ti) || (si == ti) && !vco_equal[i]) {
            rsp_VCC |= (0x01 << i);
            Set128By16(&(mp->result), (u16) si, i);
            rsp_ACC[i] = ACC_LOW(i,si); /* HW compatibility */
        } else {
            rsp_VCC &= ~(0x01 << i);
            Set128By16(&(mp->result), (u16) ti, i);
            rsp_ACC[i] = ACC_LOW(i,ti); /* HW compatibility */
        }
        rsp_VCO = UnFlag(rsp_VCO, (0x0101 << i));

    }
}

/* vector >= */
static void
vge(rsp_vuPipe_t *mp)
{
    int		element, i;
    i16		si, ti;

    rsp_VCC = 0;

    for (i=0; i<8; i++) {

	element = ((i & cmask_tab[(mp->format & 0x0e) >> 1]) + 
		   (mp->format & emask_tab[(mp->format & 0x0e) >> 1]));

	si = (i16) Get128By16(&(rsp_VUR[mp->rs]), i);
	ti = (i16) Get128By16(&(rsp_VUR[mp->rt]), element);

        if ((si > ti) || (si == ti && (vco_equal[i] || !vco_carry[i])))  {
            rsp_VCC |= (0x01 << i);
            Set128By16(&(mp->result), (u16) si, i);
            rsp_ACC[i] = ACC_LOW(i,si); /* HW compatibility */
        } else {
            rsp_VCC &= ~(0x01 << i);
            Set128By16(&(mp->result), (u16) ti, i);
            rsp_ACC[i] = ACC_LOW(i,ti); /* HW compatibility */
        }
        rsp_VCO = UnFlag(rsp_VCO, (0x0101 << i));
    }
}

/* vector > */
/* Not implemented in HW */
static void
vgt(rsp_vuPipe_t *mp)
{
    int		element, i;
    i16		si, ti;

    for (i=0; i<8; i++) {

	element = ((i & cmask_tab[(mp->format & 0x0e) >> 1]) + 
		   (mp->format & emask_tab[(mp->format & 0x0e) >> 1]));

	si = (i16) Get128By16(&(rsp_VUR[mp->rs]), i);
	ti = (i16) Get128By16(&(rsp_VUR[mp->rt]), element);

	if (si > ti) {
	    rsp_VCC |= (0x01 << i);
	    Set128By16(&(mp->result), (u16) si, i);
	} else {
	    rsp_VCC &= ~(0x01 << i);
	    Set128By16(&(mp->result), (u16) ti, i);
	}
	rsp_VCO = UnFlag(rsp_VCO, (0x01 << i));
    }
}

/* high_part_of_double_precision/single_precision clip/clamp */
static void
vch(rsp_vuPipe_t *mp)
{
    int		element, i;
    i16		si, ti;
    i16		di;
    int		ge, le, vce,sn,neq,eq;

    rsp_VCC = rsp_VCO = rsp_VCE = 0x0; 

    for (i=0; i<8; i++) {

	element = ((i & cmask_tab[(mp->format & 0x0e) >> 1]) + 
		   (mp->format & emask_tab[(mp->format & 0x0e) >> 1]));

	si = (i16) Get128By16(&(rsp_VUR[mp->rs]), i);
	ti = (i16) Get128By16(&(rsp_VUR[mp->rt]), element);

        sn = ((si^ti) < 0);

        if (sn) {
             ge = (ti<0);
             le = ((si+ti)<=0);
            vce = ((si+ti)==-1);
             eq = ((si+ti)==0) | vce;

	     di = (le) ? -ti  : si;
             rsp_ACC[i] = ACC_LOW(i,di); /* HW compatibility */
          }
        else   {
             le = (ti<0);
             ge = ((si-ti)>=0);
             eq = ((si-ti)==0);

            vce = 0; /* to Match the HW */

	     di = (ge) ? ti  : si;
             rsp_ACC[i] = ACC_LOW(i,di); /* HW compatibility */
          }
	Set128By16(&(mp->result), (u16) di, i);
	neq = ~eq & 0x1;
        rsp_VCC = rsp_VCC | ( ge<<(i+8)) | (le<<i);
        rsp_VCO = rsp_VCO | (neq<<(i+8)) | (sn<<i);
        rsp_VCE = rsp_VCE | (vce<<i);
    }
}

/* low_part_of_double_precision clip/clamp */
static void
vcl(rsp_vuPipe_t *mp)
{
    int		element, i;
    u16		sf, tf;
    i32		di;
    int		ge,le,vce,sn,eq,carry;

    for (i=0; i<8; i++) {

        le  = (rsp_VCC >>  i   ) & 0x1;
        ge  = (rsp_VCC >> (i+8)) & 0x1;
        sn  = (rsp_VCO >>  i   ) & 0x1;
        eq  =~(rsp_VCO >> (i+8)) & 0x1;
        vce = (rsp_VCE >>  i   ) & 0x1;

	element = ((i & cmask_tab[(mp->format & 0x0e) >> 1]) + 
		   (mp->format & emask_tab[(mp->format & 0x0e) >> 1]));

	sf = (u16) Get128By16(&(rsp_VUR[mp->rs]), i);
	tf = (u16) Get128By16(&(rsp_VUR[mp->rt]), element);

        if (sn) {
	  di = sf + tf;
          carry = (di > 65535);
          if (eq)  
             le = (!vce & (((di&0xffff)==0) & !carry)) |
                  ( vce & (((di&0xffff)==0) | !carry));
           di = (le) ? -tf  : sf;
           rsp_ACC[i] = ACC_LOW(i,di); /* HW compatibility */

          }
        else {
	  di = sf - tf;
            if (eq) 
                ge = (di>=0);
            di = (ge) ? tf  : sf;
            rsp_ACC[i] = ACC_LOW(i,di); /* HW compatibility */
        }

        Set128By16(&(mp->result), (u16) di, i);
        rsp_VCC = rsp_VCC&(~(0x101<<i)) | (ge<<(i+8)) | (le<<i);
    }
        rsp_VCO = rsp_VCE = 0;
}

/* 1's compl single precision clip/clamp */
static void
vcr(rsp_vuPipe_t *mp)
{
    int		element, i;
    i16		si, ti;
    i16		di;
    int		ge, le, vce,sn,eq,neq;

    rsp_VCC = 0;

    for (i=0; i<8; i++) {

	element = ((i & cmask_tab[(mp->format & 0x0e) >> 1]) + 
		   (mp->format & emask_tab[(mp->format & 0x0e) >> 1]));

	si = (i16) Get128By16(&(rsp_VUR[mp->rs]), i);
	ti = (i16) Get128By16(&(rsp_VUR[mp->rt]), element);

        sn = ((si^ti) < 0);

        if (sn) {
             ge = (ti<0);
             le = ((si+ti+1)<=0);  
	     di = (le) ? ~ti  : si;
             rsp_ACC[i] = ACC_LOW(i,di); /* HW compatibility */
          }
        else   {
             le = (ti<0);
             ge = ((si-ti)>=0);
	     di = (ge) ? ti  : si;
             rsp_ACC[i] = ACC_LOW(i,di); /* HW compatibility */
          }
	Set128By16(&(mp->result), (u16) di, i);
        rsp_VCC = rsp_VCC | ( ge<<(i+8)) | (le<<i);
    }
    rsp_VCO = rsp_VCE = 0;
}

/* vector merge */
static void
vmrg(rsp_vuPipe_t *mp)
{
    int		element, i;
    u16		si, ti;

    rsp_VCO = 0;

    for (i=0; i<8; i++) {

	element = ((i & cmask_tab[(mp->format & 0x0e) >> 1]) + 
		   (mp->format & emask_tab[(mp->format & 0x0e) >> 1]));

	si = (u16) Get128By16(&(rsp_VUR[mp->rs]), i);
	ti = (u16) Get128By16(&(rsp_VUR[mp->rt]), element);

	if (rsp_VCC & (0x01 << i)) {
	    Set128By16(&(mp->result), si, i);
            rsp_ACC[i] = ACC_LOW(i,si); /* HW compatibility */
	} else {
	    Set128By16(&(mp->result), ti, i);
            rsp_ACC[i] = ACC_LOW(i,ti); /* HW compatibility */
	}
    }
}

/*
 * decode and execute a Sel instruction
 */
static void
rsp_VUSelectExec(rsp_vuPipe_t *mp)
{
    int		i, opKey;

    /* decode instruction */
    opKey = ExtractBits(mp->inst, 5, 0);
    mp->format = ExtractBits(mp->inst, 24, 21);
    mp->rt = ExtractBits(mp->inst, 20, 16);
    mp->rs = ExtractBits(mp->inst, 15, 11);
    mp->rd = ExtractBits(mp->inst, 10,  6);
    rsp_VURegLock(mp->rd, mp->pc);

    /* check for stall. If okay, execute and decrement delay field */
    if (rsp_VURegIsLocked(mp->rt, mp->pc) || rsp_VURegIsLocked(mp->rs, mp->pc)) {
	/* can't do anything right now... */
	mp->stalled = TRUE;
	rsp_VUStalled = TRUE;
	rsp_Verbose(stderr,"VU select stalled... (%08x)\n",mp->pc);
    } else {

        for (i=0; i<8; i++) {
            vco_carry[i] = (rsp_VCO >> i) & 0x01;
            vco_equal[i] = (~rsp_VCO >> (i+8)) & 0x01;
            }

	/* execute */
	switch (opKey) {
	  case rsp_VLT:
	    strcpy(mp->opString, "vlt");
	    vlt(mp);
	    break;

	  case rsp_VEQ:
	    strcpy(mp->opString, "veq");
	    veq(mp);
	    break;

	  case rsp_VNE:
	    strcpy(mp->opString, "vne");
	    vne(mp);
	    break;

	  case rsp_VGE:
	    strcpy(mp->opString, "vge");
	    vge(mp);
	    break;

	  case rsp_VCH:
	    strcpy(mp->opString, "vch");
	    vch(mp);
	    break;

	  case rsp_VCL:
	    strcpy(mp->opString, "vcl");
	    vcl(mp);
	    break;

	  case rsp_VCR:
	    strcpy(mp->opString, "vcr");
	    vcr(mp);
	    break;

	  case rsp_VMRG:
	    strcpy(mp->opString, "vmrg");
	    vmrg(mp);
	    break;

	}

	mp->stalled = FALSE;
	mp->delay--;
    }
}


/* PUBLIC FUNCTIONS */

/*
 * check Select pipeline for stalls
 * returns TRUE is stalled.
 */
boolean
rsp_VUSelectCheckStall(void)
{
    int		i;

    for (i=0; i<rsp_VUPIPEDEPTH; i++) {
	if (vu_SelPipe[i].delay > 0 && vu_SelPipe[i].stalled)
	    return TRUE;
    }

    return FALSE;
}

/*
 * install a VU instruction into the pipeline
 */
void
rsp_VUSelectInstall(u32 inst, u32 pc)
{
    int		i;

    for (i=0; i<rsp_VUPIPEDEPTH; i++) {
	if (vu_SelPipe[i].delay == 0 || VUZeroPipe) {
	    vu_SelPipe[i].inst = inst;
	    vu_SelPipe[i].pc = pc;
	    vu_SelPipe[i].delay = (VUZeroPipe) ? 1 : rsp_VUPIPEDEPTH;
	    vu_SelPipe[i].stalled = FALSE;
	    strcpy(vu_SelPipe[i].opString,"?");
	    if (VUZeroPipe) rsp_VUSelectPipeStep();
	    break;
	}
    }
}

/*
 * this function is called once per clock, advances the select
 * pipeline one step.
 */
boolean
rsp_VUSelectPipeStep(void)
{
    int		i;

    /* advance all the things in the pipe */
    for (i=0; i<rsp_VUPIPEDEPTH; i++) {
	if (vu_SelPipe[i].delay > 0) {

	    if (vu_SelPipe[i].delay == rsp_VUPIPEDEPTH-1 || VUZeroPipe) {
		/* do decode and exec, if possible */
		rsp_VUSelectExec(&(vu_SelPipe[i]));
	    } else {
		/*
		 * fakes pipelining by waiting to write-back answer
		 */
               if (rsp_VUStalled==FALSE ||
                         vu_SelPipe[i].delay<rsp_VUPIPE_STAGE_EX)
		vu_SelPipe[i].delay--;
	    }

	    if (vu_SelPipe[i].delay == 0) {
		/* do write-back */
		rsp_Verbose(stderr,"VU SEL (%s) did write-back. (v%d)\n",
			    vu_SelPipe[i].opString, vu_SelPipe[i].rd);
		Set128(&(rsp_VUR[vu_SelPipe[i].rd]), &(vu_SelPipe[i].result));

 		traceVUbyVU(vu_SelPipe[i].rd,0xff,&(vu_SelPipe[i].result),
				vu_SelPipe[i].pc);

		/* mark registers not in use */
		rsp_VURegUnLock(vu_SelPipe[i].rd, vu_SelPipe[i].pc);
	    }
	}
    }
}