⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hex_core.c

📁 在嵌入式系统中用CPU下载CPLD的代码
💻 C
📖 第 1 页 / 共 2 页
字号:

/****************************************************************************
*                   ispVM(tm) Embedded Core Parsing VME Files               *                      
*              Lattice Semiconductor Corp. Copyright 1998 - 2001.           *
* 10/15/01 enhanced to support multiple device SVF file.                    *
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "VMOpcode.h"

#ifdef VME_DEBUG
#include "Debug.h"
 int row = 0;
#endif

unsigned short  tdodata = 0;                /*shift out data from device and compare*/

unsigned short maskdata = 0;               /*mask data exist or not*/
unsigned short tdidata = 0;                /*tdi data exist or not*/
/*10/15/01 required to support multiple device SVF file*/
unsigned short hirdata = 0;                /*hir data exist or not*/
unsigned short tirdata = 0;                /*tir data exist or not*/
unsigned short hdrdata = 0;                /*tdr data exist or not*/
unsigned short tdrdata = 0;                /*hdr data exist or not*/
/*2/15/02 tnt added 1532 support*/
unsigned short crcdata = 0;				   /*crc data exist or not*/
unsigned short cmaskdata = 0;			   /*cmask data exist or not*/
unsigned short int DataSize = 0;     /*the number(bits) of data or instruction
                                     to be shifted into or out from devices*/
unsigned short dmaskdata = 0;			   /*dmask data exist or not*/

/*2/12/02 tnt added variables needed for looping*/
unsigned short heapdata = 0;		/*holds the size of the heap*/
unsigned short DataType=0x0000;		/*holds the data type of the current row*/

unsigned char *HEAPMem = NULL;		/*holds the repeat loop*/
int OPCount=0;						/*holds the updated value of LSH or RSH*/
int HEAPSize=0;						/*holds the size of the heap*/
int RepeatLoops, repeatsize;
int HeapCounter;					/*holds the current byte address of the Heap*/

extern FILE *fpr;
                                       
/*prototypes*/
unsigned char   GetByte(void);
char ispVMCode(void);
char ispVMDataCode();
unsigned short int ispVMDataSize(void);
void ispVMData();
char ispVMLoop(unsigned short loop_cnt);
char ispVMAmble(char Code);

/*********************************************************************
                                                                  
                     READ_ISPVM_DATA_SIZE                      
 Extract the data size of the current row from the ispVM file.  
 Return                                                             
     DataSize-----------The data bit length of the current row.     
**********************************************************************/
unsigned short int ispVMDataSize()
{
	unsigned short int temp;
    char cur_char,i;
    temp = 0;
    i = 0;
    while ((cur_char=GetByte()) & 0x80) {
        temp |= ((unsigned short)(cur_char & 0x7F))<< i;
        i += 7;
    }
    temp |= ((unsigned short)(cur_char & 0x7F))<< i;
	return temp;
}

/*********************************************************************
                                                                    
                     READ_ISPVM_FILE                                
 Extract an opcode and the immediate operand from the VMF File.     
 Return                                                             
 Global var:                                                        
      compression    --read instruction without compression         
                       read data with compression                   
      tdodata        --true if TDO token is detected to indicate    
                       reading is necessary                                                         
      DataSize        --the number of bits of data to be processed   
**********************************************************************/
char ispVMCode()
{
 unsigned char xch;
 char rcode;
 char cur_code;
#ifdef VME_DEBUG
    int i;
	short int row = 0;
#endif
 rcode = 0;
 cur_code = 0;
 
 while ((xch=GetByte()) >=0 ) { /*all opcode except ENDFILE are all positive*/
#ifdef VME_DEBUG
		row++;
                for (i=0; i<opcode_cnt; i++)
                    if (ispVMOpcodes[i].token==xch) break;
                printf("row =%d opcode = %s\n", row, ispVMOpcodes[i].text);
                
#endif

       switch (xch) {
	      case STATE: /*step BSCAN state machine to specified state*/
                      GetByte();
                      break;
          case SIR:   /*shift instruction stream into devices*/
                      DataSize = ispVMDataSize();
                      /*fetch the data stream*/
                      cur_code=ispVMDataCode();
                      break;
          case XSDR: 
          case SDR:   /*Scan address or program data into devices*/
                      DataSize = ispVMDataSize();
                      /*fetch and keep the data stream*/
                      cur_code = ispVMDataCode();
                      break;                            
          case WAIT:  /*opcode to wait for specified time in us or ms*/      
                      ispVMDataSize();
                      break;
          case TCK:   /*pulse TCK signal the specified time*/
                      ispVMDataSize();
                      break;
          case ENDDR: /*modify the BSCAN state after SDR opcode*/
                      GetByte();
                      break;
          case ENDIR: /*modify the BSCAN state after SIR opcode*/
                      GetByte();
                      break;
          case HIR:  
          case TIR:   
          case HDR:   
          case TDR:  
					  rcode = ispVMAmble(xch);
					  if (rcode == -4)
						  return 1;
					  break;
          case MEM:   
					  ispVMDataSize();
                      break;
		  case FREQUENCY:
					  ispVMDataSize();
				      break;
		  case VENDOR:
					  GetByte();
					  break;
		  case HEAP:  /*the size of HEAP memory to store loops*/
			          GetByte(); /*Throw away SECUREHEAP opcode*/
					  HEAPSize = ispVMDataSize();
					  if (HEAPSize > heapdata)
						  heapdata = HEAPSize;
					  if (HEAPMem!=NULL)
						  free(HEAPMem);  
					  HEAPMem = (unsigned char *)malloc(HEAPSize + 2);
                      break;
		  case REPEAT:   /*the begin of a monolithic loop*/
			          RepeatLoops = 0;
					  repeatsize = ispVMDataSize();
                      rcode = ispVMLoop(repeatsize);
					  break;
          case ENDLOOP:
			          return 1;
		  case SHR:   
					  GetByte();
                      break;
          case SHL:   
			          GetByte();
                      break;
		  case VUES:
					  break;
          case ENDDATA:  /*reach end of the current VME file*/ 
                      return 1;
          default:    /*invalid opcode encountered*/
					  return 1;		  
          }

  }
    return (rcode);         
}


/****************************************************************************
                     ispVM Data Code                            
 Extract tdi tdo or mask from the VME file.                     
 Input                                                          
     SF-----------------Data has compression code if true       
 Return                                                         
     tdidata, tdodata, maskdata-----Return 1 if found, else 0.  
*****************************************************************************/
char ispVMDataCode()
{
 short int data;
 unsigned char xch;
#ifdef VME_DEBUG
 int i;
#endif
 
 while ((data = GetByte()) != 0xFF) { 
        xch =(unsigned char)data;
#ifdef VME_DEBUG
        
                for (i=0; i<opcode_cnt; i++)
                    if (ispVMOpcodes[i].token==xch) break;
                printf("at ispVMDataCode opcode = %s\n",ispVMOpcodes[i].text);
                
#endif

        switch (xch) {
            case XTDI: 
            case TDI:  
					   if (DataSize>tdidata) 
						   tdidata=DataSize;
                       ispVMData();
                       break;
            case XTDO: 
            case TDO:  if (DataSize > tdodata) 
						   tdodata=DataSize;  
                       ispVMData();
                       break;
            case MASK: 
					   if (DataSize > maskdata) 
						   maskdata = DataSize;
					   ispVMData();
                       break;
			case CRC:
					   if (DataSize > crcdata)
						   crcdata = DataSize;
					   ispVMData();
					   break;
			case CMASK:
				       if (DataSize > cmaskdata)
						   cmaskdata = DataSize;
					   ispVMData();
					   break;
			case READ:
				       ispVMData();
					   break;
			case RMASK:
				       ispVMData();
					   break;
			case DMASK:
					   if (DataSize > dmaskdata)
						   dmaskdata = DataSize;
				       ispVMData();
					   break;
			case CONTINUE:
					   return (0);
            default:   /*save the un-processed opcode*/
#ifdef VME_DEBUG
        
                for (i=0; i<opcode_cnt; i++)
                    if (ispVMOpcodes[i].token==xch) break;
                printf(" at ispVMDataCode return opcode = %s\n",ispVMOpcodes[i].text);
                
#endif

                       return xch;
            }
	}
  return (0);
}

/****************************************************************************
                     ispVM Data                         
 Extract one row of data operand from the current data type opcode. Perform
 the decompression if necessary. Extra RAM is not required for the
 decompression process. The decompression scheme employed in this module
 is on row by row basis. The format of the data stream:
 [compression code][compressed data stream]
  0x00    --No compression
  0x01    --Compress by 0x00.
            Example:
            Original stream:   0x000000000000000000000001
            Compressed stream: 0x01000901
            Detail:            0x01 is the code, 0x00 is the key, 
                               0x09 is the count of 0x00 bytes,
                               0x01 is the uncompressed byte.
  0x02    --Compress by 0xFF.
            Example:
            Original stream:   0xFFFFFFFFFFFFFFFFFFFFFF01
            Compressed stream: 0x02FF0901
            Detail:            0x02 is the code, 0xFF is the key, 
                               0x09 is the count of 0xFF bytes,
                               0x01 is the uncompressed byte.
  0x03
  : :
  0xFE   -- Compress by nibble blocks.
            Example:
            Original stream:   0x84210842108421084210
            Compressed stream: 0x0584210
            Detail:            0x05 is the code, means 5 nibbles block.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -