bendime.cpp

来自「基于4个mips核的noc设计」· C++ 代码 · 共 421 行

CPP
421
字号
#include <stdio.h>
#include <dimesdl.h>
#include "vidime.h"
#include <vector>
#include <string>
#include <math.h>
#include <conio.h>
#include <time.h>

using namespace std;

void Exit(DWORD Value);

void DMATest(VIDIME_HANDLE hVIDIME, DIME_HANDLE hCard);
void DMATest2(DIME_HANDLE hCard, unsigned mem);
void RegTest(VIDIME_HANDLE hVIDIME);
void rwreg( DIME_HANDLE hCard, DWORD addr, DWORD data );

int WriteDataFile( const char *filename, vector<unsigned>& data, int offset = 0, int words=-1 );
int ReadDataFile( const char *filename, vector<unsigned>* data, int offset = 0, int words = 1 );

int WriteMem( DIME_HANDLE hCard, int mem, int addr, vector<unsigned>& data, int words=-1, bool skip=false );
int ReadMem( DIME_HANDLE hCard, int mem, int addr, vector<unsigned>* data, int words );

struct mem_transfer{
  bool write;
  int mem;
  int address;
  int size;
  string filename;
  int fileoffset;
  mem_transfer( bool w, int m, int a, int s, string f, int o ):
    write(w), mem(m), address(a), size(s), filename(f), fileoffset(o) {}
};


void usage(){
  printf("Usage : bendime [-b bitfile] (-[w|r] <memtrans>)* [-v][-R][-t <sec>][-s]\n");
  printf("\tProgram BenADDA with bitfile\n");
  printf("\tPerform a number of memory transfers ('r'ead or 'w'rite)\n");
  printf("\tMemory transfer is defined as <mem> <addr> <size> <file> <offset>\n");
  printf("\t\t<mem> is memory select word (1,2,4,8 - mem1, 2, 3, 4)\n");
  printf("\t\t<addr> is address of block start\n");
  printf("\t\t<size> is block size in WORDS\n");
  printf("\t\t<filename> is input/output file\n");
  printf("\t\t<offset> is file offset (in bytes) to place/read block from\n");
  printf("\tWith -v, each write transfer is verified with readback transfer\n");
  printf("\t-R prevents initial reset and clock setup\n");
  printf("\t-t <sec> run processors for <sec> seconds rather than until keypress\n");
  printf("\t-s skips blocks of more than 4 zero-words when writing\n");


}

int main(int argc, char* argv[])
{
  LOCATE_HANDLE hLocate=NULL;
  DWORD ErrorNum,NumOfCards,Cntr,NumModules,LoopCntr,DevCntr;
  char ErrorString[1000];
  DIME_HANDLE hCard=NULL;
  DWORD Result,Cntr2,CurrCount,TotalErrors;

  if( argc == 1 ){
    usage();
    return 0;
  }

  char BitFile[1024];
  bool config=false, verify=false, noreset=false, skip=false;
  int stop=-1;

  vector<mem_transfer> transfers;

  int a = 1;
  int m, s, o, addr;
  string fn;
  char c;
  while(a<argc){
    if(argv[a][0]=='-'){
      switch((c=argv[a][1])){
      case 'b' : config = true; strcpy(BitFile,argv[++a]); break;
      case 'v' : verify = true; break;
      case 'R' : noreset = true; break;
      case 's' : skip = true; break;
      case 't' : stop = atoi(argv[++a]); break;
      case 'w' :
      case 'r' :
        m = atoi(argv[++a]);
        addr = atoi(argv[++a]);
        s = atoi(argv[++a]);
        fn = argv[++a];
        o = atoi(argv[++a]);
        transfers.push_back( mem_transfer( (c=='w'), m, addr, s, fn, o ) );
        break;
      default : printf("Ignored switch %s\n",argv[a]); break;
      }
    }
    else{
      printf("Ignored parameter %s\n", argv[a]);
    }
    a++;
  }

  VIDIME_HANDLE hVIDIME;

  //Call the function to locate all Nallatech cards on the PCI interface.
  if( (hLocate = DIME_LocateCard(dlUSB, mbtALL,NULL,dldrDEFAULT,dlDEFAULT)) == NULL)
    {//Error hLocate still NULL Locate handle returned.
      //Get the error then terminate the program
      DIME_GetError(NULL, &ErrorNum, ErrorString);
      printf("Error Number %d\n", ErrorNum);
      printf("%s\n",ErrorString);
      Exit(1);
    }

  //Determine how many Nallatech cards have been found.
  NumOfCards = DIME_LocateStatus(hLocate,0,dlNUMCARDS);
  printf("%d Nallatech card(s) found.\n", NumOfCards);

  //Get the details for each card detected.
  for (LoopCntr=1; LoopCntr<=NumOfCards; LoopCntr++)
    {
      printf("Details of card number %d, of %d:\n",LoopCntr,NumOfCards);
      printf("\tThe card driver for this card is a %s.\n",(char*)DIME_LocateStatusPtr(hLocate,LoopCntr,dlDESCRIPTION));
      printf("\tThe cards motherboard type is %d.\n",DIME_LocateStatus(hLocate,LoopCntr,dlMBTYPE));
    }

  //At this stage we now have all the information we need to open a card up.

  //Open up the first card found.
  printf("\nOpening card 1 ...");
  hCard = DIME_OpenCard(hLocate, 1, dccOPEN_DEFAULT);
  if (hCard == NULL) //check to see if the open worked.
    {
      DIME_GetError(NULL, &ErrorNum, ErrorString);
      printf("Error Number %d\n", ErrorNum);
      printf("%s\n",ErrorString);

      printf("Card Number One failed to open.\n");
      Exit(2);
    }

  printf(" done\n");

  //Get some informtaion about the card and its modules.
  printf("\nThis card is a %s.\n",DIME_CardStatusPtr(hCard,dinfDESCRIPTION));
  NumModules = DIME_CardStatus(hCard,dinfNUMBERMODULES);
  printf("There are %d modules on this card.\nThe Modules are:\n",NumModules);
  // List the module Descriptions
  for(LoopCntr=0; LoopCntr<NumModules; LoopCntr++)
    {
      printf("\tModule %d is a %s\n",LoopCntr,DIME_ModuleStatusPtr(hCard,LoopCntr,dinfDESCRIPTION));
      for(DevCntr=0; DevCntr<DIME_ModuleStatus(hCard,LoopCntr,dinfNUMDEVICES); DevCntr++ )
        printf("\t\tDevice %d is a %s\n",DevCntr,DIME_DeviceStatusPtr(hCard,LoopCntr,DevCntr,dinfDESCRIPTION));
    }

  if(!noreset)
  {
  DIME_CardResetControl(hCard,drONBOARDFPGA, drENABLE,0);
  DIME_CardResetControl(hCard,drSYSTEM, drENABLE,0);

  double freq;
  DIME_SetOscillatorFrequency( hCard, 1, 20, &freq );
  printf("Frequency (1) set to %f\n", freq );
  DIME_SetOscillatorFrequency( hCard, 2, 40, &freq );
  printf("Frequency (2) set to %f\n", freq );
  }
  //now boot the on board FPGA
  if( config )
    {
      printf("\nConfiguring with %s ...", BitFile );
      if((Result=DIME_ConfigDevice(hCard, BitFile, 0, 1, 0, 0))!=dcfgOK_STATUS)
        {
          printf("Error configuring on board FPGA! Result=%d\n",Result);
          Exit(5);
        }
      printf("...done\n");
    }

  if( !noreset ){
        DIME_CardResetControl(hCard,drONBOARDFPGA, drDISABLE,0);
        DIME_CardResetControl(hCard,drINTERFACE, drTOGGLE,0);
        DIME_CardResetControl(hCard,drSYSTEM, drDISABLE,0);
  }

  // write transfers
  for( vector<mem_transfer>::const_iterator trans = transfers.begin();
       trans != transfers.end();
       ++trans )
    if( trans->write )
      {
        printf("\nUpload %dB of %s @ %d to mem 0x%x\n", trans->size*4, trans->filename.c_str(), trans->fileoffset, trans->mem);
        vector<unsigned> data;
        ReadDataFile(trans->filename.c_str(), &data, trans->fileoffset, trans->size);
        WriteMem( hCard, trans->mem, trans->address, data, trans->size, skip );
        if( verify ){
          vector<unsigned> data_r;
          printf("Readback...\n");
          ReadMem( hCard, trans->mem, trans->address, &data_r, data.size() );
          for( unsigned i = 0 ; i < data.size() ; i++ )
            if( data[i] != data_r[i] ){
              printf("Readback error: word %4d wrote: 0x%08x read: 0x%08x\n", i, data[i], data_r[i]);
            }
        }
      }

  viDIME_WriteRegister( hCard, 4, 0xffffffff, 5000 );
  int x = 0;
  printf("\nRunning (press key to stop)\n");
  const char *tab = "|/-\\" ;
  time_t t = time(0);
  while(1){
    if( tab[x] == 0 ) x = 0;
    printf("\r0x%08x 0x%08x 0x%08x 0x%08x %c", viDIME_ReadRegister(hCard, 6, 1000), viDIME_ReadRegister(hCard, 7, 1000), viDIME_ReadRegister(hCard, 8, 1000), viDIME_ReadRegister(hCard, 9, 1000), tab[x++]);
    Sleep(100);
    if( kbhit() || stop>0 && t-time(0)>=stop ) break;
  }
  printf("\n");
  viDIME_WriteRegister( hCard, 4, 0, 5000 );


  // read transfers
  for( vector<mem_transfer>::const_iterator trans = transfers.begin();
       trans != transfers.end();
       ++trans )
    if( !trans->write )
      {
        printf("\nDownload %dB of mem 0x%x to %s @ %d\n", trans->size*4, trans->mem, trans->filename.c_str(), trans->fileoffset);
        vector<unsigned> data;
        ReadMem( hCard, trans->mem, trans->address, &data, trans->size );
        WriteDataFile( trans->filename.c_str(), data, trans->fileoffset, trans->size );
      }

  DIME_CloseCard(hCard);//Closes down the first card.

  //Finally the last thing that should be done is to close down the locate.
  DIME_CloseLocate(hLocate);

  return 0;
}

void Exit(DWORD Value)
{
  printf ("\nPress return to terminate the application.\n");
  getchar();
  exit(Value);
}



int WriteMem( DIME_HANDLE hCard, int mem, int addr, vector<unsigned>& data, int words, bool skip )
{
  if( words<0 ) words = data.size();

  printf("Writing %d words\n", words);

  if( viDIME_WriteRegister( hCard, 3, addr, 5000 ) != 0 ){
    printf("Error writing address register\n");
    return -1;
  }
  if( viDIME_WriteRegister( hCard, 2, mem, 5000 ) != 0 ){
    printf("Error writing memsel register\n");
    return -1;
  }

  unsigned start = GetTickCount();
  int i;
  unsigned w;
  for( i = 0 ; i < words ; i++ ){
    if( i != 0 && (i%10 == 0) ) printf("\r%d", i);
    if( i < data.size() ) w = data[i];
    else w = 0;

    if( skip && i < data.size()-4 && i < words - 4
        && data[i]==0 && data[i+1]==0 && data[i+2]==0 && data[i+3]==0 )
    {
        while( i < data.size() && i < words && data[i] == 0 )
                i++;
        printf("\nSkipping to word %d\n", i );
        if( i == data.size() || i == words ) break;
        if( viDIME_WriteRegister( hCard, 3, addr+4*i, 5000 ) != 0 ){
            printf("Error writing address register\n");
            return -1;
        }
        w = data[i];
    }

    if( viDIME_WriteRegister( hCard, 5, w, 5000 ) != 0 ){
      printf("Error writing word %d\n", i);
      return -1;
    }
  }
  unsigned end = GetTickCount();
  printf("\r%d done in %d ms ", i, end-start);

  printf("addr=%d\n", viDIME_ReadRegister( hCard, 3, 5000 ));

  if( viDIME_WriteRegister( hCard, 3, 0, 5000 ) != 0 ){
    printf("Error reseting address register\n");
    return -1;
  }
  if( viDIME_WriteRegister( hCard, 2, 0, 5000 ) != 0 ){
    printf("Error writing memsel register\n");
    return -1;
  }

  return data.size();
}


int ReadMem( DIME_HANDLE hCard, int mem, int addr, vector<unsigned>* data, int words )
{
  printf("Reading %d words\n", words);

  if( viDIME_WriteRegister( hCard, 3, addr, 5000 ) != 0 ){
    printf("Error writing address register\n");
    return -1;
  }
  if( viDIME_WriteRegister( hCard, 2, mem, 5000 ) != 0 ){
    printf("Error writing memsel register\n");
    return -1;
  }

  unsigned start = GetTickCount();
  int i;
  for( i = 0 ; i < words ; i++ ){
    if( i != 0 && (i%10 == 0) ) printf("\r%d", i);
    data->push_back( viDIME_ReadRegister( hCard, 5, 5000 ) );
  }
  unsigned end = GetTickCount();
  printf("\r%i done in %d ms ", i, end-start);

  printf("addr=%d\n", viDIME_ReadRegister( hCard, 3, 5000 ));

  if( viDIME_WriteRegister( hCard, 3, 0, 5000 ) != 0 ){
    printf("Error reseting address register\n");
    return -1;
  }
  if( viDIME_WriteRegister( hCard, 2, 0, 5000 ) != 0 ){
    printf("Error writing memsel register\n");
    return -1;
  }

  return words;
}

int WriteDataFile( const char *filename, vector<unsigned>& data, int offset, int words )
{
  int i, j, c;
  FILE *ifile;
  if( offset < 0 )
      ifile = fopen(filename, "wb");
  else
      ifile = fopen(filename, "a+b");

  if(!ifile) {
    printf("Cannot open %s for writing\n", filename );
    return -1;
  }

  fseek(ifile, offset, SEEK_SET);

  unsigned w, m;
  if( words == -1 ) words = data.size();
  for( i = 0 ; i < words; ++i )
    {
      if( i < data.size() ) w = data[i];
      else w = 0;
        
      for( j = 0 ; j < 4; j++)
        {
          c = (w >> 8*(3-j)) & 0xff;
          fputc(c, ifile);
        }
    }
  fclose(ifile);
  return i;
}

int ReadDataFile( const char *filename, vector<unsigned>* data, int offset, int words )
{
  FILE *ifile;
  ifile = fopen(filename, "rb");

  if(!ifile) {
    printf("Cannot open %s for reading\n", filename );
    return -1;
  }

  if(offset) fseek(ifile, offset, SEEK_SET);

  unsigned nonzero=offset-1, len=offset, c;
  while(1){
    c = fgetc(ifile);
    if( c == EOF ) break;
    if( c != 0 ) nonzero = len;
    len++;
  }
  printf("%s : %d bytes, %d data\n", filename, len, nonzero );
  if( words < 0 ) words = ceil((double)(nonzero+1)/4.0);

  fseek(ifile, offset, SEEK_SET);
  unsigned w = 0x0, i, j;
  for( i = 0 ; i < words ; i++ )
    {
      w = 0x0;
      for( j = 0 ; j < 4; j++ )
        {
          c = fgetc(ifile);
          if( c == EOF ) c = 0;
          w <<= 8;
          w |= (c & 0xff);
        }
      data->push_back(w);
    }

  fclose(ifile);
  return ceil((double)len/4.0);
}

⌨️ 快捷键说明

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