rspctl.c 25.4 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 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134

/**************************************************************************
 *                                                                        *
 *               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:	rspctl.c
 * Creator:	hsa@sgi.com
 * Create Date:	Tue Feb  8 11:50:41 PST 1994
 *
 * This file is the control unit for the RSP simulator.
 *
 */

#include <stdio.h>
#include "rsp.h"
#include "rspctl.h"
#include "memory.h"
#include "cop0.h"
#include "opcode.h"
#include "su.h"
#include "vu.h"
#include "std_cmds.h"


/*
 * The RSP clock.
 */
u32	rsp_clock;

/*
 * The RSP PC, points into the instruction cache.
 */
u32	rsp_programCounter;

/*
 * Temp PC, set during execution cycle, updates the real PC
 * at the end of the instruction.
 *
 * After the execution cycle, if the clock is equal to the
 * value of delayClock, the PC is modified.
 */
static u32	tempPC;
static u32	delayClock;
#define rsp_DELAY_INVALID	0xffffffff

static boolean	rsp_LoadStoreSlip[2];

/*
 * The RSP control register, accessible to the host processor.
 */
u32	rsp_controlReg;

typedef struct {
    int		count;
    int		waiting;
} rsp_SUStall_t;

rsp_SUStall_t	rsp_SURegStalls[32];

/*
 * RSP step functions, implemented in each module, they execute one clock
 * step. Each module installs these during their own init.
 */
rsp_stepProc_t	rsp_procStepProcs[rsp_STEP_PROC_MAX];
static int	rsp_stepProcCount = 0;

/*
 * Breakpoints. List of addresses or clock values.
 */
typedef struct {
    u32		where;
    boolean	isAddr;
    boolean	enabled;
    int		cmdCnt;
    char	*cmd[16];
} rsp_bkpt_t;
#define rsp_BREAK_COUNT_MAX	32
static rsp_bkpt_t	rsp_breakPoints[rsp_BREAK_COUNT_MAX];
static int		rsp_breakCount = 0;

static u32		rsp_watchPoints[rsp_BREAK_COUNT_MAX];
static int		rsp_watchCount = 0;

/*
 * fetched and execute instructions:
 */
typedef struct {
    i16		group_size;
    u32		instV, instS;
    u32		pcV, pcS;
    boolean	validV, validS;
    boolean	Vfirst;
    boolean	SisLoad;
    boolean	SisStore;
} rsp_instGroup_t;

static rsp_instGroup_t	rsp_fetchInst, rsp_execInst;

/*
 * Per-clock tick function.
 */
static boolean	rsp_InstFetch(void);
static boolean	rsp_InstExec(void);
static boolean	rsp_SUStaller(void);
static boolean	rsp_SUAddLocks(u32 inst);
static boolean	rsp_SUCheckLocks(u32 inst);

static boolean	inst_is_scalar(u32 inst);

/*
 * reset processor state.
 */
/* passing pc argument during initialization (Kishor 9/26/94)*/
void
rsp_ProcessorReset(int pc)
{
    rsp_clock = 0;
    rsp_programCounter = pc;
    rsp_controlReg = 0x0;
    tempPC = 0;
    delayClock = rsp_DELAY_INVALID;

    bzero((char *)&rsp_fetchInst, sizeof(rsp_instGroup_t));
    bzero((char *)&rsp_execInst, sizeof(rsp_instGroup_t));

    /* clear registers... */
}

/*
 * RSP control logic initialize.
 */
void
rsp_ProcessorInit(void)
{
    rsp_ProcessorReset(rsp_ICACHE_LOW);
    rsp_controlReg = Flag(rsp_controlReg, rsp_CtlHaltMask);

    /* clear out step proc list */
    bzero((void *) &(rsp_procStepProcs[0]), 
	  (rsp_STEP_PROC_MAX * sizeof(rsp_stepProc_t)));
    rsp_stepProcCount = 0;

    rsp_LoadStoreSlip[0] = FALSE;
    rsp_LoadStoreSlip[1] = FALSE;

    /*
     * control step installs:
     * Exec is before fetch so we exec previously fetched instruction.
     */
    rsp_ProcessorStepInstall(rsp_SUStaller);
    rsp_ProcessorStepInstall(rsp_InstExec);
    rsp_ProcessorStepInstall(rsp_InstFetch);
}

/*
 * This function is called form other modules, it installs
 * a 'step' routine, that will be executed once per clock tick.
 */
void
rsp_ProcessorStepInstall(rsp_stepProc_t StepProc)
{
    rsp_procStepProcs[rsp_stepProcCount++] = StepProc;
}

/*
 * This function is called to install a breakpoint at the
 * specified address.
 */
void
rsp_ProcessorBreakpointInstall(u32 where, boolean isAddr)
{
    if (rsp_breakCount < rsp_BREAK_COUNT_MAX) {
	rsp_breakPoints[rsp_breakCount].where = where;
	rsp_breakPoints[rsp_breakCount].isAddr = isAddr;
	rsp_breakPoints[rsp_breakCount].enabled = TRUE;
	rsp_breakPoints[rsp_breakCount].cmdCnt = 0;
	rsp_breakCount++;
    } else {
	/* print error message... */
    }
}

/*
 * init breakpoint commands
 */
void
rsp_ProcessorBreakpointCmdInit(int bp)
{
    register rsp_bkpt_t	*bpp = &(rsp_breakPoints[bp]);
    int		j;

    if (bp < rsp_breakCount) {
	for (j=0; j<bpp->cmdCnt; j++) {
	    if (bpp->cmd[j] != NULL)
		free(bpp->cmd[j]);
	}
	bpp->cmdCnt = 0;
    } else {
	/* print error message... */
    }
}

/*
 * install a command at a breakpoint.
 */
void
rsp_ProcessorBreakpointCmdInstall(int bp, char *string)
{
    register rsp_bkpt_t	*bpp = &(rsp_breakPoints[bp]);

    if (bp < rsp_breakCount) {
	bpp->cmd[bpp->cmdCnt] = (char *) malloc(strlen(string)+1);
	strcpy(bpp->cmd[bpp->cmdCnt++], string);
    } else {
	/* print error message... */
    }
}

/*
 * enable (or disable) a breakpoint.
 */
void
rsp_ProcessorBreakpointEnable(int bp, boolean doEnable)
{
    register rsp_bkpt_t	*bpp = &(rsp_breakPoints[bp]);

    if (bp < rsp_breakCount) {
	bpp->enabled = doEnable;
    } else {
	/* print error message... */
    }
}

/*
 * list out all the breakpoints.
 */
void
rsp_ProcessorBreakpointList(FILE *out)
{
    register rsp_bkpt_t	*bp = &(rsp_breakPoints[0]);
    int			i, j;
    u32			inst;
    char 		instd[32];

    if (rsp_breakCount > 0) {
	rsp_fprintf(out,"\n");
    }

    for (i=0; i<rsp_breakCount; i++, bp++) {
	if (bp->enabled)
	    rsp_fprintf(out," ");
	else
	    rsp_fprintf(out,"d");
	rsp_fprintf(out," [%d]  %08x ", i, bp->where);
	if (bp->isAddr) {
	    inst = rsp_ExamineMemory(bp->where);
	    rsp_DisasmInst(inst, instd, bp->where);
	    rsp_fprintf(out,"\t%s\n",instd);
	} else {
	    rsp_fprintf(out,"(pc)\n");
	}

	for (j=0; j<bp->cmdCnt; j++) {
	    rsp_fprintf(out,"\t%s",bp->cmd[j]);
	}
	rsp_fprintf(out,"\n");
    }

    if (rsp_breakCount > 0) {
	rsp_fprintf(out,"\n");
    }

    fflush(out);
}

/*
 * return 0 if not breakpoint, 1 if active, 2 if inactive
 */
int
rsp_ProcessorBreakpointCheck(u32 addr)
{
    register rsp_bkpt_t	*bp = &(rsp_breakPoints[0]);
    int			i;

    for (i=0; i<rsp_breakCount; i++, bp++) {
	if (bp->where == addr) {
	    return bp->enabled ? 1 : 2;
	}
    }

    return 0;
}

/*
 * return breakpoint number from address
 */

int
rsp_ProcessorBreakpointNumber(u32 addr)
{
    register rsp_bkpt_t	*bp = &(rsp_breakPoints[0]);
    int			i;

    for (i=0; i<rsp_breakCount; i++, bp++) {
	if (bp->where == addr) {
	    return i;
	}
    }

    return -1;
}

/*
 * return all breakpoint addresses and enables in two arrays
 */

int
rsp_ProcessorBreakpointGetAll(u32 addrs[], int enabled[])
{
    register rsp_bkpt_t	*bp = &(rsp_breakPoints[0]);
    int			i;

    for (i=0; i<rsp_breakCount; i++, bp++) {
	addrs[i] = bp->where;
	enabled[i] = bp->enabled;
    }

    return rsp_breakCount;
}


/*
 * This function is called to install a memory watchpoint at the
 * specified address.
 */
void
rsp_ProcessorWatchpointInstall(u32 where)
{
    if (rsp_watchCount < rsp_BREAK_COUNT_MAX) {
	rsp_watchPoints[rsp_watchCount++] = where;
    } else {
	/* print error message... */
    }
}

void
rsp_CheckWatchpoint(u32 addr)
{
    int i;

    for (i=0; i<rsp_watchCount; i++) {
	if (addr == rsp_watchPoints[i]) {
	    rsp_controlReg = Flag(rsp_controlReg, rsp_CtlHaltMask);
	    break;
	}
    }
}

static boolean _doingSSTEP;

/* slip processor 1 clock, for taken branch bubble */
boolean
rsp_ProcessorSlip(boolean doClock)
{
    if (doClock)
	rsp_clock++;
    counter_clock++;
}

boolean
rsp_ProcessorSStep(void)
{
    boolean	retval;
    int		i, j;
    u32		inst;
    int		inst_type;

    /* disable future fetching */
    _doingSSTEP = TRUE;

    /*
     * clear fetched instructions.
     */
    bzero((char *)&rsp_fetchInst, sizeof(rsp_instGroup_t));
    bzero((char *)&rsp_execInst, sizeof(rsp_instGroup_t));

    /*
     * fetch 1 instruction using PC.
     */
    inst = rsp_ExamineMemory1(rsp_programCounter, rsp_ICACHE_ACCESS);

    /*
     * classify the instruction:
     */
    inst_type = inst_is_scalar(inst);
    rsp_fetchInst.group_size = 1;
    if (inst_type) {
	rsp_fetchInst.instS = inst;
	rsp_fetchInst.pcS = rsp_programCounter;
	rsp_fetchInst.validS = TRUE;
	rsp_fetchInst.validV = FALSE;
    } else {
	rsp_fetchInst.instV = inst;
	rsp_fetchInst.pcV = rsp_programCounter;
	rsp_fetchInst.validV = TRUE;
	rsp_fetchInst.validS = FALSE;
    }

    /*
     * execute that instruction.
     */
    /* do each thing in the list.. */
    for (i=0; i<rsp_stepProcCount; i++) {
	if (rsp_procStepProcs[i] != (rsp_stepProc_t) NULL) {
	    retval = (*rsp_procStepProcs[i])();
	}
    }
    bzero((char *)&rsp_fetchInst, sizeof(rsp_instGroup_t));

    /*
     * run the clock till it's done.
     */
    for (j=0; j<10; j++) {
	/* do each thing in the list.. */
	for (i=0; i<rsp_stepProcCount; i++) {
	    if (rsp_procStepProcs[i] != (rsp_stepProc_t) NULL) {
		retval = (*rsp_procStepProcs[i])();
	    }
	}
    }

    rsp_clock++;

    /*
     * clear fetched instructions.
     */
    bzero((char *)&rsp_fetchInst, sizeof(rsp_instGroup_t));
    bzero((char *)&rsp_execInst, sizeof(rsp_instGroup_t));

    _doingSSTEP = FALSE;

    return(TRUE);
}

/*
 * Step RSP one clock tick.
 */
boolean
rsp_ProcessorStep(void)
{
    register rsp_bkpt_t	*bp;
    boolean	retval, do_continue = TRUE;
    int		i, j;
    char	cmd[32];

    /* do each thing in the list.. */
    for (i=0; i<rsp_stepProcCount; i++) {
	if (rsp_procStepProcs[i] != (rsp_stepProc_t) NULL) {
	    retval = (*rsp_procStepProcs[i])();
	}
    }

    rsp_clock++;

    /*
     * check for somebody that has halted the processor;
     * could be memory watchpoint, exception, etc.
     */
    if (Flagged(rsp_controlReg, rsp_CtlHaltMask)) {
	do_continue = FALSE;
    }

    /*
     * Check for breakpoint...
     */
    bp = &(rsp_breakPoints[0]);
    for (i=0; i<rsp_breakCount && do_continue; i++, bp++) {
	if (bp->enabled && bp->isAddr) { /* PC */
	    if (rsp_programCounter == bp->where) {
		do_continue = FALSE;
	    }
	} else if (bp->enabled) {		/* clock */
	    if (rsp_clock == bp->where) {
		do_continue = FALSE;
	    }
	}

	if (!do_continue) {
	    rsp_Verbose(stderr,"break: [%d] RSP halted at PC = 0x%08x CLK = 0x%08x\n",
		    i, rsp_programCounter, rsp_clock);
	    for (j=0; j<bp->cmdCnt; j++) {
		sscanf(bp->cmd[j],"%s",cmd);
		process_command(cmd, bp->cmd[j], stdin);
	    }
	}
    }

    return(do_continue);
}

/*
 * Run RSP (until breakpoint or completion)
 */
void
rsp_ProcessorRun(void)
{
    boolean	do_continue = TRUE;
    boolean	old_verbose;

    while (do_continue) {
	do_continue = rsp_ProcessorStep();
	rsp_UpdateVisual ();
    }

    old_verbose = rsp_SetVerbose (TRUE);
    rsp_UpdateVisual ();
    rsp_SetVerbose (old_verbose);
}

/*
 * private function that implements the instruction classification
 * algorithm. Basically, COP2 op or not, but will correctly handle
 * vload/vstore as SU instructions.
 *
 * Returns TRUE if an SU instruction, or FALSE if a VU instruction.
 *
 */
static boolean
inst_is_scalar(u32 inst)
{
    int	opKey;

    opKey = ExtractOpcode(inst);
    if (opKey == rsp_COP2 && (inst & 0x2000000)) {	/* bit 25 set */
	return (FALSE);
    }

    return(TRUE);
}

/*
 * private function that computes a mask of the destination Vregs
 * Basically, COP2 op or not, but will correctly handle
 * vload/vstore as SU instructions.
 *
 * Returns TRUE if an SU instruction, or FALSE if a VU instruction.
 *
 */
static u32
vect_dest_mask(u32 inst)
{
    u32 mask;
    int vd, i;
    static char buf[80];
    extern int debug_vmask;

    mask = 0;

    rsp_DisasmInst( inst, buf, 0 );

    if( buf[0] == 'B' ) 	/* BAD OP */
	return( mask );

    if( buf[0] == 's' ) 	/* store */
	return( mask );

    if( (buf[0] == 'm') && (buf[0] == 'f') ) 	/* Move From */
	return( mask );

    vd = -1;

    for(i=1; buf[i] != '\0'; i++)
	if( buf[i] == '$' )
	    break;
    
    if( buf[i] == '$' && buf[i+1] == 'v' ) {
	vd = buf[i+2] - '0';

	if( isdigit( buf[i+3] ) )
	    vd = 10*vd + (buf[i+3] - '0');
    };

    if( vd < 0 )
	return( mask );
    
    if( (buf[0] == 'l') && (buf[1] == 't') )	/* Transpose */
	mask = 0xff << (vd & 0x18);
    else
	mask = 1 << vd;
    
    if( debug_vmask )
	fprintf(stderr, "VECT_DEST_MASK: vd=%d, mask=0x%08x, inst=<%s>\n", vd, mask, buf );

    return( mask );
	    
}


static boolean branch_inst( u32 );
static boolean ctc2_inst( u32 );
static boolean load_inst( u32 );
static boolean store_inst( u32 );

/*
 * Fetch one (or two) instructions from the cache
 * to execute.
 */
static boolean
rsp_InstFetch(void)
{
    u32		inst1, inst2, tinst;
    u32		vmask1, vmask2;
    char	cmd[32];
    boolean	inst1_type, inst2_type;
    int		new_pc, group_size;
    boolean	inst1_branch;
    static int 	prevDelayClock = 0;
    static int	prev_branch    = 0;

    if (_doingSSTEP)
	return TRUE;

    /*
     * just return if stalled.
     */
    if (rsp_SUIsStalled() || rsp_VUIsStalled()) {
	return FALSE;
    }

    /* using new function which uses memtype argument (Kishor 9/26/94)*/
    inst1 = rsp_ExamineMemory1(rsp_programCounter,rsp_ICACHE_ACCESS);
    inst2 = rsp_ExamineMemory1((rsp_programCounter+4),rsp_ICACHE_ACCESS);

    /*
     * The RSP can execute up to two instructions per clock cycle:
     *
     * zero or one SU instruction and zero or one VU instruction
     * 
     * if the next two instructions are for the same unit, only
     * one instruction group can execute.
     *
     * a branch delay slot is always a one instruction group.
     *
     */

    /*
     * classify each of the instructions:
     */
    inst1_type = inst_is_scalar(inst1);
    inst2_type = inst_is_scalar(inst2);

    inst1_branch = branch_inst(inst1);


    /*
     * build an instruction group to execute.
     * if we are in a branch delay, only allow one instruction.
     */
    if ( prev_branch ) {	/* Always single issue after a branch */
	inst2_type = inst1_type;		/* Single Issue Hack */
    }

    if( (prevDelayClock != rsp_DELAY_INVALID) && /* We are at the target of a taken branch */
	((rsp_programCounter & 0x4) != 0) ) {	/* Destination is not double word aligned */
	inst2_type = inst1_type;		/* Single Issue Hack */
	rsp_ProcessorSlip(TRUE);
    };

    prevDelayClock = delayClock;		


    if( inst2_type != inst1_type ) {		/* Dual issue? */
	vmask1 = vect_dest_mask( inst1 );
	vmask2 = vect_dest_mask( inst2 );
	if( vmask1 & vmask2 )
	    inst2_type = inst1_type;		/* Single Issue Hack */
    };

    if( inst2_type != inst1_type ) {		/* Dual issue? */
	if( ctc2_inst(inst1) || ctc2_inst(inst2) )	/* TOO general, but OK for now */
	    inst2_type = inst1_type;		/* Single Issue Hack */
    };


    if( inst2_type != inst1_type )		/* Dual issue? */
	prev_branch = inst1_branch || branch_inst(inst2); /* Are either a branch? */
    else
	prev_branch = inst1_branch;		 /* Is single inst. a branch? */


    if (!(rsp_SUIsStalled() || rsp_VUIsStalled())) {
	if ( !inst1_branch &&
	    ((inst1_type && !inst2_type) ||
	    (inst2_type && !inst1_type))   ) {
	    rsp_fetchInst.group_size = 2;
	    if (inst1_type) {
		rsp_fetchInst.instS = inst1;
		rsp_fetchInst.instV = inst2;
		rsp_fetchInst.pcS = rsp_programCounter;
		rsp_fetchInst.pcV = rsp_programCounter+4;
		rsp_fetchInst.validV = TRUE;
		rsp_fetchInst.validS = TRUE;
		rsp_fetchInst.Vfirst = FALSE;
		rsp_fetchInst.SisLoad = load_inst(inst1);
		rsp_fetchInst.SisStore = store_inst(inst1);
	    } else {
		rsp_fetchInst.instV = inst1;
		rsp_fetchInst.instS = inst2;
		rsp_fetchInst.pcV = rsp_programCounter;
		rsp_fetchInst.pcS = rsp_programCounter+4;
		rsp_fetchInst.validV = TRUE;
		rsp_fetchInst.validS = TRUE;
		rsp_fetchInst.Vfirst = TRUE;
		rsp_fetchInst.SisLoad = load_inst(inst2);
		rsp_fetchInst.SisStore = store_inst(inst2);
	    }
	} else {
	    rsp_fetchInst.group_size = 1;
	    if (inst1_type) {
		rsp_fetchInst.instS = inst1;
		rsp_fetchInst.pcS = rsp_programCounter;
		rsp_fetchInst.validS = TRUE;
		rsp_fetchInst.validV = FALSE;
		rsp_fetchInst.SisLoad = load_inst(inst1);
		rsp_fetchInst.SisStore = store_inst(inst1);
	    } else {
		rsp_fetchInst.instV = inst1;
		rsp_fetchInst.pcV = rsp_programCounter;
		rsp_fetchInst.validV = TRUE;
		rsp_fetchInst.validS = FALSE;
		rsp_fetchInst.SisLoad = FALSE;
		rsp_fetchInst.SisStore = FALSE;
	    }
	}

	/* lock any scalar registers for moves/loads: */
	if (rsp_fetchInst.validS) {
	    if (load_inst(rsp_fetchInst.instS)) {
		rsp_SUAddLocks(rsp_fetchInst.instS);
	    }
	}

    }

    return TRUE;
}

static boolean
branch_inst( u32 inst )
{
    char cmd[1024];

    rsp_DisasmInst(inst, cmd, 0);

    return( (cmd[0] == 'b') || (cmd[0] == 'j') );
}


static boolean
ctc2_inst( u32 inst )
{
    char cmd[1024];

    rsp_DisasmInst(inst, cmd, 0);

    return( (cmd[0] == 'c') &&
/*	    (cmd[1] == 't') &&		/* Maybe allow cfc2 also */
	    (cmd[2] == 'c') &&
	    (cmd[3] == '2'));
}

static boolean
load_inst( u32 inst )
{
    char cmd[1024];

    rsp_DisasmInst(inst, cmd, 0);

    if ((strncmp(cmd, "lb", 2) == 0) ||
	(strncmp(cmd, "lh", 2) == 0) ||
	(strncmp(cmd, "lw", 2) == 0) ||
	(strncmp(cmd, "ls", 2) == 0) ||
	(strncmp(cmd, "ll", 2) == 0) ||
	(strncmp(cmd, "ld", 2) == 0) ||
	(strncmp(cmd, "lq", 2) == 0) ||
	(strncmp(cmd, "lr", 2) == 0) ||
	(strncmp(cmd, "lp", 2) == 0) ||
	(strncmp(cmd, "luv", 3) == 0) ||
	(strncmp(cmd, "lf", 2) == 0) ||
	(strncmp(cmd, "lw", 2) == 0) ||
	(strncmp(cmd, "lt", 2) == 0) ||
	(strncmp(cmd, "mf", 2) == 0) ||
	(strncmp(cmd, "mt", 2) == 0) ||
	(strncmp(cmd, "cf", 2) == 0) ||
	(strncmp(cmd, "ct", 2) == 0)) {
	return (TRUE);
    } else {
	return (FALSE);
    }
}

static boolean
store_inst( u32 inst )
{
    char cmd[1024];

    rsp_DisasmInst(inst, cmd, 0);

    if ((strncmp(cmd, "sb", 2) == 0) ||
	(strncmp(cmd, "sh", 2) == 0) ||
	(strncmp(cmd, "sw", 2) == 0) ||
	(strncmp(cmd, "ss", 2) == 0) ||
	(strncmp(cmd, "sl", 2) == 0) ||
	(strncmp(cmd, "sd", 2) == 0) ||
	(strncmp(cmd, "sq", 2) == 0) ||
	(strncmp(cmd, "sr", 2) == 0) ||
	(strncmp(cmd, "sp", 2) == 0) ||
	(strncmp(cmd, "suv", 3) == 0) ||
	(strncmp(cmd, "sf", 2) == 0) ||
	(strncmp(cmd, "sw", 2) == 0) ||
	(strncmp(cmd, "st", 2) == 0) ||
	(strncmp(cmd, "mf", 2) == 0) ||
	(strncmp(cmd, "mt", 2) == 0) ||
	(strncmp(cmd, "cf", 2) == 0) ||
	(strncmp(cmd, "ct", 2) == 0)) {
	return (TRUE);
    } else {
	return (FALSE);
    }
}

/*
 * Execute an instruction group. (fetched on the previous
 * clock cycle)
 */
static boolean
rsp_InstExec(void)
{
    char	cmd[32];
    int		new_pc;

    /*
     * just return if stalled.
     */
    if (rsp_VUIsStalled()) {
	return FALSE;
    }

    /*
     * check for SU stalls before executing:
     */
    if (rsp_fetchInst.validS) {
	if (rsp_SUCheckLocks(rsp_fetchInst.instS)) {
	    rsp_Verbose(stderr,"SU is stalled!\n");
	    return FALSE;
	}
    }

    /*
     * move previously-fetched instruction to execute..
     */
    rsp_LoadStoreSlip[1] = rsp_LoadStoreSlip[0];
    rsp_LoadStoreSlip[0] = rsp_execInst.SisLoad;
    bcopy((char *)&rsp_fetchInst, (char *)&rsp_execInst, 
	  sizeof(rsp_instGroup_t));

/*
    fprintf(stderr,"SU load/store: last 2 instructions %s %s...\n",
	    (rsp_LoadStoreSlip[0]) ? "TRUE" : "FALSE",
	    (rsp_LoadStoreSlip[1]) ? "TRUE" : "FALSE");

    fprintf(stderr,"[%08x] SisStore = %s\n",
	    rsp_programCounter,(rsp_execInst.SisStore) ? "TRUE" : "FALSE");
*/
    if (rsp_execInst.SisStore && rsp_LoadStoreSlip[1]) {
	rsp_Verbose(stderr,"SU load/store stall!\n");
	rsp_ProcessorSlip(TRUE);
    }

    /* 
     * execute the instruction group.
     */
    new_pc = rsp_programCounter;
    if (rsp_execInst.group_size == 1) {
	if (rsp_execInst.validS) {
	    rsp_DisasmInst(rsp_execInst.instS, cmd, new_pc);
	    rsp_Verbose(stderr,"0x%08x - 0x%08x:    %s\n",
		    rsp_clock, rsp_programCounter, cmd);
	    if (vtout) {
		rsp_fprintf(vtout,"0x%08x - 0x%08x:    %s\n",
			    rsp_clock, rsp_programCounter, cmd);
	    }
	    rsp_SUExec(rsp_execInst.instS, rsp_execInst.pcS);
	    new_pc += 4;
	} else if (rsp_execInst.validV) {
	    rsp_DisasmInst(rsp_execInst.instV, cmd, new_pc);
	    rsp_Verbose(stderr,"0x%08x - 0x%08x:    \t\t\t%s\n",
		    rsp_clock, rsp_programCounter, cmd);
	    if (vtout) {
		rsp_fprintf(vtout,"0x%08x - 0x%08x:    %s\n",
			    rsp_clock, rsp_programCounter, cmd);
	    }
	    rsp_VUExec(rsp_execInst.instV, rsp_execInst.pcV);
	    new_pc += 4;
	} else {
	    /* error */
	}
    } else if (rsp_execInst.group_size == 2) {

	rsp_DisasmInst(rsp_execInst.instS, cmd, rsp_execInst.pcS);
	rsp_Verbose(stderr,"0x%08x - 0x%08x:    %s\t",
		rsp_clock, rsp_programCounter, cmd);

	if (vtout) {
	    rsp_fprintf(vtout,"0x%08x - 0x%08x:    %s\t",
			rsp_clock, rsp_programCounter, cmd);
	}

	rsp_DisasmInst(rsp_execInst.instV, cmd, rsp_execInst.pcV);
	rsp_Verbose(stderr,"%s\n",cmd);

	if (vtout) {
	    rsp_fprintf(vtout,"%s\n",cmd);
	}

	if (rsp_execInst.Vfirst) {
	    rsp_VUExec(rsp_execInst.instV, rsp_execInst.pcV);
	    new_pc = (new_pc + 4) & 0xffc;
	    rsp_programCounter = 0x04001000 + (new_pc& 0xffc);
	    rsp_SUExec(rsp_execInst.instS, rsp_execInst.pcS);
	    new_pc = (new_pc + 4) & 0xffc;
	} else {
	    rsp_SUExec(rsp_execInst.instS, rsp_execInst.pcS);
	    new_pc = (new_pc + 4) & 0xffc;
	    rsp_programCounter = 0x04001000 + (new_pc& 0xffc);
	    rsp_VUExec(rsp_execInst.instV, rsp_execInst.pcV);
	    new_pc = (new_pc + 4) & 0xffc;
	}
    }

    /*
     * the PC is either updated correctly (either 1 or 2 instructions
     * executed), or updated by a branch target.
     */
    if (rsp_clock >= delayClock) {
	rsp_programCounter = tempPC;
	delayClock = rsp_DELAY_INVALID;
	/* kill fetched instruction */
	bzero((char *)&rsp_fetchInst, sizeof(rsp_instGroup_t));
    } else {
	rsp_programCounter = 0x04001000 + (new_pc& 0xffc);
    }

    return(TRUE);
}

/*
 * this function sets the value when a jump/branch
 * needs to set the PC.
 */
void
rsp_PCSet(u32 addr, int clockDelay)
{
    tempPC = 0x04001000 + (addr & 0xffc);
    delayClock = rsp_clock + clockDelay;
}

/*
 * This function maintains a 'stall' table for the scalar registers.
 * If, when we go to use any SU register, it's 'count' value is > 0,
 * then we must stall untill it counts down to zero.
 *
 * This proc runs every clock tick, doing the count down. It also manages
 * a 'waiting' queue, in case 2 instructions in a row lock a register.
 *
 * This stall machinery is disabled when VUZeroPipe is TRUE, meaning
 * we are running in an 'instruction boundary' mode.
 */
static boolean
rsp_SUStaller(void)
{
    int		i;

    for (i=0; i<32; i++) {
	if (rsp_SURegStalls[i].count > 0) {
	    rsp_SURegStalls[i].count--;	/* decrement lock */
/*
	    if (rsp_SURegStalls[i].count == 0)
		rsp_Verbose(stderr,"SU : un-locking register $%d\n",i);
*/
	}

	if (rsp_SURegStalls[i].count == 0 && rsp_SURegStalls[i].waiting > 0) {
/*
	    rsp_Verbose(stderr,"SU : locking register $%d\n",i);
*/
	    rsp_SURegStalls[i].count = 4;	/* reset lock */
	    rsp_SURegStalls[i].waiting--;	/* decrement wait queue */
	}
    }

    return(TRUE);
}

/* lock an SU register */
boolean
rsp_SULockReg(int reg)
{
    if (VUZeroPipe) {
	/* don't allow locks or stalls */
	rsp_SURegStalls[reg].count = 0;
    } else {
	if (rsp_SURegStalls[reg].count > 0) {
	    rsp_SURegStalls[reg].waiting++; /* add to wait queue */
	} else {
	    /*
	       rsp_Verbose(stderr,"SU : locking register $%d\n",reg);
	       */
	    rsp_SURegStalls[reg].count = 4;
	}
    }
}

/* is this SU register locked? */
boolean
rsp_SURegIsLocked(int reg)
{
    return(rsp_SURegStalls[reg].count);
}

static boolean
rsp_SUAddLocks(u32 inst)
{
    int		opKey, rt;

    opKey = ExtractOpcode(inst);

    /* regular SU loads */
    if (opKey >= rsp_LB && opKey <= rsp_LHU) {
	rt = ExtractBits(inst, 20, 16);
	rsp_SULockReg(rt);
    }

    /* cop0 moves */
    if (opKey == rsp_COP0) {
	opKey = ExtractBits(inst, 25, 21);	/* what kind of op */
	if (opKey == 0x04) {	/* only lock MTC0 */
	    rt = ExtractBits(inst, 20, 16);
	    rsp_SULockReg(rt);
	}
    }

    /* VU loads and moves handled elsewhere...  */

    return(TRUE);
}


static boolean
rsp_SUCheckLocks(u32 inst)
{
    int		rs = -1, rd = -1;
    u32		opKey;

    if (VUZeroPipe) {
	/* don't allow locks or stalls */
	return FALSE;
    }

    opKey = ExtractOpcode(inst);
    switch (opKey) {
      case rsp_SPECIAL:
	opKey = ExtractBits(inst, 5, 0);
	if (opKey == rsp_BREAK) {
	    /* do nothing */
	} else if (opKey >= rsp_JR && opKey <= rsp_JALR) {
	    if (opKey == rsp_JR) {
		rs = ExtractBits(inst, 25, 21);
	    }
	} else {
	    rs = ExtractBits(inst, 25, 21);
	    rd = ExtractBits(inst, 20, 16);
	}
	break;

      case rsp_REGIMM:
	/* no registers to check */
	break;
	
      case rsp_COP0:	/* MFC0/MTC0 BC0T/BC0F */
	break;

      case rsp_COP2:	/* MFC2/MTC2 CFC2/CTC2 */
	break;

      default:
	if (opKey >= rsp_J && opKey <= rsp_BGTZ)
	    /* no registers to check */
	    ;
	else if (opKey >= rsp_ADDI && opKey <= rsp_LUI)
	    rs = ExtractBits(inst, 25, 21);
	else if (opKey >= rsp_LB && opKey <= rsp_LHU)
	    /* don't check for loads */
	    ;
	else if (opKey >= rsp_SB && opKey <= rsp_SW)
	    /* don't check for stores */
	    ;
	else if (opKey >= rsp_LWC2 && opKey <= rsp_SWC2)
	    /* don't check for VU load/stores */
	    ;
	break;
    }

    if (rs != -1) {
	if (rsp_SURegIsLocked(rs))
	    return(TRUE);
    }

    if (rd != -1) {
	if (rsp_SURegIsLocked(rd))
	    return(TRUE);
    }

    return(FALSE);
}