signed char ispVMCode()

in common/recipes-utils/cpldupdate/files/ivm_core.c [455:977]


signed char ispVMCode()
{
	//09/11/07 NN added local variables initialization
	unsigned short iRepeatSize = 0;
	signed char cOpcode		   = 0;
	signed char cRetCode       = 0;
	unsigned char ucState      = 0;
	unsigned short usDelay     = 0;
	unsigned short usToggle    = 0;
	unsigned char usByte       = 0;

	/***************************************************************
	*
	* Check the compression flag only if this is the first time
	* this function is entered. Do not check the compression flag if
	* it is being called recursively from other functions within
	* the embedded engine.
	*
	***************************************************************/

	if ( !( g_usDataType & LHEAP_IN ) && !( g_usDataType & HEAP_IN ) ) {
		usByte = GetByte();
		if ( usByte == 0xf1 ) {
			g_usDataType |= COMPRESS;
		}
		else if ( usByte == 0xf2 ) {
			g_usDataType &= ~COMPRESS;
		}
		else {
			return VME_INVALID_FILE;
		}
	}

	/***************************************************************
	*
	* Begin looping through all the VME opcodes.
	*
	***************************************************************/

	while ( ( cOpcode = GetByte() ) >= 0 ) {

		switch ( cOpcode ) {
		case STATE:

			/***************************************************************
			*
			* Step the JTAG state machine.
			*
			***************************************************************/

			ucState = GetByte();

			/***************************************************************
			*
			* Step the JTAG state machine to DRCAPTURE to support Looping.
			*
			***************************************************************/

			if ( (g_usDataType & LHEAP_IN) &&
				 (ucState == DRPAUSE ) &&
				 ( g_cCurrentJTAGState == ucState ))
			{
				ispVMStateMachine( DRCAPTURE );
			}

			ispVMStateMachine( ucState );

#ifdef VME_DEBUG
			if ( g_usDataType & LHEAP_IN ) {
				printf( "LDELAY %s ", GetState( ucState ) );
			}
			else {
				printf( "STATE %s;\n", GetState( ucState ) );
			}
#endif //VME_DEBUG
			break;
		case SIR:
		case SDR:
		case XSDR:

#ifdef VME_DEBUG
			switch( cOpcode ) {
			case SIR:
				printf( "SIR " );
				break;
			case SDR:
			case XSDR:
				if ( g_usDataType & LHEAP_IN ) {
					printf( "LSDR " );
				}
				else {
					printf( "SDR " );
				}
				break;
			}
#endif //VME_DEBUG
			/***************************************************************
			*
			* Shift in data into the device.
			*
			***************************************************************/

			cRetCode = ispVMShift( cOpcode );
			if ( cRetCode != 0 ) {
				return ( cRetCode );
			}
			break;
		case WAIT:

			/***************************************************************
			*
			* Observe delay.
			*
			***************************************************************/

			//09/11/07 NN Type cast mismatch variables
			usDelay = (unsigned short) ispVMDataSize();
			ispVMDelay( usDelay );

#ifdef VME_DEBUG
			if ( usDelay & 0x8000 ) {

				/***************************************************************
				*
				* Since MSB is set, the delay time must be decoded to
				* millisecond. The SVF2VME encodes the MSB to represent
				* millisecond.
				*
				***************************************************************/

				usDelay &= ~0x8000;
				if ( g_usDataType & LHEAP_IN ) {
					printf( "%.2E SEC;\n", ( float ) usDelay / 1000 );
				}
				else {
					printf( "RUNTEST %.2E SEC;\n", ( float ) usDelay / 1000 );
				}
			}
			else {
				/***************************************************************
				*
				* Since MSB is not set, the delay time is given as microseconds.
				*
				***************************************************************/

				if ( g_usDataType & LHEAP_IN ) {
					printf( "%.2E SEC;\n", ( float ) usDelay / 1000000 );
				}
				else {
					printf( "RUNTEST %.2E SEC;\n", ( float ) usDelay / 1000000 );
				}
			}
#endif //VME_DEBUG
			break;
		case TCK:

			/***************************************************************
			*
			* Issue clock toggles.
			*
			***************************************************************/

			//09/11/07 NN Type cast mismatch variables
			usToggle = (unsigned short) ispVMDataSize();
			ispVMClocks( usToggle );

#ifdef VME_DEBUG
			printf( "RUNTEST %d TCK;\n", usToggle );
#endif //VME_DEBUG
			break;
		case ENDDR:

			/***************************************************************
			*
			* Set the ENDDR.
			*
			***************************************************************/

			g_ucEndDR = GetByte();

#ifdef VME_DEBUG
			printf( "ENDDR %s;\n", GetState( g_ucEndDR ) );
#endif //VME_DEBUG
			break;
		case ENDIR:

			/***************************************************************
			*
			* Set the ENDIR.
			*
			***************************************************************/

			g_ucEndIR = GetByte();

#ifdef VME_DEBUG
			printf( "ENDIR %s;\n", GetState( g_ucEndIR ) );
#endif //VME_DEBUG
			break;
		case HIR:
		case TIR:
		case HDR:
		case TDR:

#ifdef VME_DEBUG
			switch( cOpcode ) {
			case HIR:
				printf( "HIR " );
				break;
			case TIR:
				printf( "TIR " );
				break;
			case HDR:
				printf( "HDR " );
				break;
			case TDR:
				printf( "TDR " );
				break;
			}
#endif //VME_DEBUG

			/***************************************************************
			*
			* Set the header/trailer of the device in order to bypass
			* successfully.
			*
			***************************************************************/

			cRetCode = ispVMAmble( cOpcode );
			if ( cRetCode != 0 ) {
				return ( cRetCode );
			}

#ifdef VME_DEBUG
			printf( ";\n" );
#endif //VME_DEBUG
			break;
		case MEM:

			/***************************************************************
			*
			* The maximum RAM required to support processing one row of the
			* VME file.
			*
			***************************************************************/

			//09/11/07 NN Type cast mismatch variables
			g_usMaxSize = (unsigned short) ispVMDataSize();

#ifdef VME_DEBUG
			printf( "// MEMSIZE %d\n", g_usMaxSize );
#endif //VME_DEBUG
			break;
		case VENDOR:

			/***************************************************************
			*
			* Set the VENDOR type.
			*
			***************************************************************/

			cOpcode = GetByte();
			switch ( cOpcode ) {
			case LATTICE:
#ifdef VME_DEBUG
				printf( "// VENDOR LATTICE\n" );
#endif //VME_DEBUG
				g_cVendor = LATTICE;
				break;
			case ALTERA:
#ifdef VME_DEBUG
				printf( "// VENDOR ALTERA\n" );
#endif //VME_DEBUG
				g_cVendor = ALTERA;
				break;
			case XILINX:
#ifdef VME_DEBUG
				printf( "// VENDOR XILINX\n" );
#endif //VME_DEBUG
				g_cVendor = XILINX;
				break;
			default:
				break;
			}
			break;
		case SETFLOW:

			/***************************************************************
			*
			* Set the flow control. Flow control determines the personality
			* of the embedded engine.
			*
			***************************************************************/

			//09/11/07 NN Type cast mismatch variables
			g_usFlowControl |= (unsigned short) ispVMDataSize();
			break;
		case RESETFLOW:

			/***************************************************************
			*
			* Unset the flow control.
			*
			***************************************************************/

			//09/11/07 NN Type cast mismatch variables
			g_usFlowControl &= (unsigned short) ~( ispVMDataSize() );
			break;
		case HEAP:

			/***************************************************************
			*
			* Allocate heap size to store loops.
			*
			***************************************************************/

			cRetCode = GetByte();
			if ( cRetCode != SECUREHEAP ) {
				return VME_INVALID_FILE;
			}
			//09/11/07 NN Type cast mismatch variables
			g_iHEAPSize = (unsigned short) ispVMDataSize();

			/****************************************************************************
			*
			* Store the maximum size of the HEAP buffer. Used to convert VME to HEX.
			*
			*****************************************************************************/

			if ( g_iHEAPSize > g_usHeapSize ) {
				g_usHeapSize = g_iHEAPSize;
			}

			ispVMMemManager( HEAP, ( unsigned short ) g_iHEAPSize );
			break;
		case REPEAT:

			/***************************************************************
			*
			* Execute loops.
			*
			***************************************************************/

			g_usRepeatLoops = 0;

			//09/11/07 NN Type cast mismatch variables
			iRepeatSize = (unsigned short) ispVMDataSize();

			cRetCode = ispVMLoop( ( unsigned short ) iRepeatSize );
			if ( cRetCode != 0 ) {
				return ( cRetCode );
			}
			break;
		case ENDLOOP:

			/***************************************************************
			*
			* Exit point from processing loops.
			*
			***************************************************************/

			return ( cRetCode );
		case ENDVME:

			/***************************************************************
			*
			* The only valid exit point that indicates end of programming.
			*
			***************************************************************/

			return ( cRetCode );
		case SHR:

			/***************************************************************
			*
			* Right-shift address.
			*
			***************************************************************/

			g_usFlowControl |= SHIFTRIGHT;

			//09/11/07 NN Type cast mismatch variables
			g_usShiftValue = (unsigned short) (g_usRepeatLoops * (unsigned short)GetByte());
			break;
		case SHL:

			/***************************************************************
			*
			* Left-shift address.
			*
			***************************************************************/

			g_usFlowControl |= SHIFTLEFT;

			//09/11/07 NN Type cast mismatch variables
			g_usShiftValue = (unsigned short)(g_usRepeatLoops * (unsigned short)GetByte());
			break;
		case FREQUENCY:

			/***************************************************************
			*
			* Set the frequency.
			*
			***************************************************************/

			//09/11/07 NN Type cast mismatch variables
			g_iFrequency = (int) (ispVMDataSize() );
			//10/23/08 NN changed to check if the frequency smaller than 1000
			if(g_iFrequency >= 1000)
			{
				g_iFrequency = g_iFrequency / 1000;
				if(g_iFrequency == 1)
					g_iFrequency = 1000;
#ifdef VME_DEBUG
			printf( "FREQUENCY %.2E HZ;\n", ( float ) g_iFrequency * 1000 );
#endif //VME_DEBUG
			}
			else
			{
				if(g_iFrequency == 0)
					g_iFrequency = 1000;
#ifdef VME_DEBUG
			printf( "FREQUENCY %.2E HZ;\n", ( float ) g_iFrequency );
#endif //VME_DEBUG
			}
			break;
		case LCOUNT:

			/***************************************************************
			*
			* Process LCOUNT command.
			*
			***************************************************************/

			cRetCode = ispVMLCOUNT( ( unsigned short ) ispVMDataSize() );
			if ( cRetCode != 0 ) {
				return ( cRetCode );
			}
			break;
		case VUES:

			/***************************************************************
			*
			* Set the flow control to verify USERCODE.
			*
			***************************************************************/

			g_usFlowControl |= VERIFYUES;
			break;
		case COMMENT:

			/***************************************************************
			*
			* Display comment.
			*
			***************************************************************/

			ispVMComment( ( unsigned short ) ispVMDataSize() );
			printf( "Warning! Don't power off......\n\n");
			isp_print_progess_bar(0);
			break;
		case LVDS:

			/***************************************************************
			*
			* Process LVDS command.
			*
			***************************************************************/

			ispVMProcessLVDS( ( unsigned short ) ispVMDataSize() );
			break;
		case HEADER:

			/***************************************************************
			*
			* Discard header.
			*
			***************************************************************/

			ispVMHeader( ( unsigned short ) ispVMDataSize() );
			break;
		/* 03/14/06 Support Toggle ispENABLE signal*/
		case ispEN:
			ucState = GetByte();
			if((ucState == ON)||(ucState == 0x01))
				writePort( g_ucPinENABLE, 0x01 );
			else
				writePort( g_ucPinENABLE, 0x00 );
			ispVMDelay( 1 );
			break;
        /* 05/24/06 support Toggle TRST pin*/
		case TRST:
			ucState = GetByte();
			if(ucState == 0x01)
				writePort( g_ucPinTRST, 0x01 );
			else
				writePort( g_ucPinTRST, 0x00 );
			ispVMDelay( 1 );
			break;
		default:

			/***************************************************************
			*
			* Invalid opcode encountered.
			*
			***************************************************************/

#ifdef VME_DEBUG
			printf( "\nINVALID OPCODE: 0x%.2X\n", cOpcode );
#endif //VME_DEBUG

			return VME_INVALID_FILE;
		}
	}

	/***************************************************************
	*
	* Invalid exit point. Processing the token 'ENDVME' is the only
	* valid way to exit the embedded engine.
	*
	***************************************************************/

	return ( VME_INVALID_FILE );
}