Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

Timky.cxx

Go to the documentation of this file.
00001 //File: Timky.cxx
00002 
00014 /* ---------------------------------------------------------------------------
00015  * Execute TIM keywords read from a file or interactively
00016  *
00017  * Author : J.B.Lane UCL January 2001                                  timky.c
00018  * ---------------------------------------------------------------------------
00019  */
00020 
00021 #include <iostream>
00022 #include <string.h>  /* strstr (strchr) strcmp */
00023 #include  <ctype.h> /* toupper */
00024 
00025 using namespace std;  // needed here for VmeModule!
00026 
00027 #include "../RodCrate/TimDefine.h"
00028 #include "../RodCrate/TimSequin.h"
00029 #include "../RodCrate/TimModule.h"
00030 #include "../VmeInterface/RCCVmeInterface.h"
00031 //#include "../CommonWithDsp/processor.h"
00032 
00033 #include  "../Timlet/ask.h" // OK clashes with VmeInterface
00034 
00035 /* Definitions */
00036 
00037 enum name {NONE = -999} ;
00038 
00039 #define MAXLINE 128
00040 
00041 using namespace SctPixelRod; // L1A etc undeclared if before includes
00042 
00043 /* Prototypes */
00044 
00045 int  damon(const char *FileName, unsigned short *buffer);
00046 int  winky(const char *FileName);
00047 int  open_window(const int Addr, const int Mode);
00048 FILE *open_file(const char *FileName, const char *FileMode);
00049 int  close_file(FILE *fp, const char *FileName);
00050 void test(void);
00051 void timKeyword(const char *String);
00052 
00053 const UINT32 baseAddr = 0x0D000000;  // VME base address for TIM slot 13
00054 const UINT32  mapSize =    0x10000;  // VME window size
00055 
00056 VmeInterface *vme = new RCCVmeInterface();  // Create VME interface
00057 TimModule    *tim = new TimModule( baseAddr, mapSize, *vme );  // Create tim
00058 TimSequin    *seq = new TimSequin();  // Create
00059 
00060 int main (int argc, char *argv[])
00061 {
00062   if (vme == 0 || tim == 0 || seq == 0) {
00063     cout << "No object " << vme << tim << seq << endl;
00064   }
00065   const char *FileName = argv[1];
00066   int r;
00067 
00068   if (argc > 1)
00069   {
00070     r = winky( FileName);
00071   }
00072   else
00073   {
00074     r = 0;
00075 //***    memory_ptr = ask_vme () ;
00076 //***  open_window(Addr, Mode) ;
00077     //    if (memory_ptr != NULL) test () ;
00078     //    else printf ("Error opening VME window\n") ;
00079 
00080     test () ;
00081   }
00082 
00083   delete seq;
00084   delete tim;
00085   delete vme;
00086   return r;
00087 }
00088 
00089 unsigned short* vme_get_window (const int dummy0,
00090                                 const int dummy1, const int dummy2) {
00091   return 0;
00092 }
00093 
00094 int open_window(const int Addr, const int Mode)
00095 {
00096   printf("addr: %x\n", Addr);
00097   //  memory_ptr = vme_get_window(Addr, Mode); /* open VME window */
00098   //  if (memory_ptr == NULL)
00099   {   printf("error mapping window\n");
00100       return 1;
00101   }
00102   tim->reset();
00103   tim->initialize();
00104 
00105   return 0;
00106 }
00107 
00108 FILE *open_file(const char *FileName, const char *FileMode)
00109 {
00110   FILE *fp;
00111 
00112   fp = fopen(FileName, FileMode);
00113   if (fp == NULL)
00114   {   printf("can't open file %s\n", FileName);
00115       perror(FileName);
00116       return fp;
00117   }
00118   printf("\nreading file %s\n\n", FileName);
00119 
00120   return fp;
00121 }
00122 
00123 int close_file(FILE *fp, const char *FileName)
00124 {
00125   if (ferror(fp))
00126   {   printf("error reading file %s\n", FileName);
00127       perror(FileName);
00128       return 1;
00129   }
00130   if (fclose(fp) == EOF) printf("error closing file\n"); /* fp is not const */
00131   return 0;
00132 }
00133 
00134 int winky(const char *FileName)
00135 {
00136   char   line[MAXLINE];      /* input line  (includes final \0) */
00137   FILE *fp;                  /* file pointer */
00138   int  r;
00139 
00140 /***
00141   printf("VME base address is first line in file,  then each line is:\n"
00142          "[addr[ data]][text]\n"
00143          " addr = D16 offset in hex           (blank means text only)\n"
00144          " data = D16 data   in hex for write (blank means  VME read)\n"
00145          " text : any ? character or wait string means prompt\n "
00146          "       any \" character or blank line  means comment"
00147          " (line not printed)\n\n"
00148         );
00149 ***/
00150 
00151   /* recursive use of timKeyword */
00152   timKeyword("REST"); /* clear buffer */
00153 
00154   fp = open_file(FileName, "r");
00155   if (fp == NULL) return 1;
00156 
00157   while (!feof(fp))
00158   { if (fgets(line, MAXLINE, fp) != NULL)    /* trap end of file or error */
00159     {
00160       printf("file: %s", line);
00161 
00162 /***  else if (nval == EOF) ; */              /*     blank line = comment */
00163 
00164       timKeyword(line);
00165 
00166       if (strstr(line, "wait") != NULL)          /* wait present = prompt */
00167       {   printf("Press Return to continue :");
00168           (void) getchar();
00169           printf("\n");
00170       }
00171     }
00172   }
00173   printf("end of file\n");
00174 
00175   r = close_file(fp, FileName);
00176 
00177   return r;
00178 }
00179 
00180 int damon(const char *FileName, unsigned short *buffer)
00181 {
00182   unsigned int word;
00183   int          byte, i, n, r;
00184   FILE *fp;                  /* file pointer */
00185 
00186   /* recursive use of timKeyword */
00187   timKeyword("REST"); /* clear buffer */
00188 
00189   fp = open_file(FileName, "rb"); /* binary */
00190   if (fp == NULL) return 1;
00191 
00192   i = 0;
00193   n = 0;
00194   while (!feof(fp))
00195   {
00196     byte = fgetc(fp); /* read a byte */
00197     word = 0;
00198     if (byte != EOF)    /* trap end of file or error */
00199     {
00200       if (byte != 0)
00201       {
00202         if (n%2 == 1)
00203             printf("ERROR odd byte: %5d %5o %4x %2X\n", n, n, n, byte);
00204 
00205         /* swap bit order */
00206 
00207         if (byte & 0x20) word |= TIM_L1A;
00208         if (byte & 0x10) word |= TIM_ECR;
00209         if (byte & 0x08) word |= TIM_BCR;
00210         if (byte & 0x04) word |= TIM_CAL;
00211         if (byte & 0x02) word |= TIM_SID;
00212         if (byte & 0x01) word |= TIM_STT;
00213 
00214         printf("%5d %4x %2X %2X\n", n/2, n, byte, word);
00215 /*      printf("%2X%c", byte, (n%20==19) ? '\n' : ' '); */
00216         i ++;
00217         if (n/2 < TIM_SEQ_SIZE) buffer[n/2] = word; /* store byte */
00218         else printf("ERROR: buffer overflow");
00219       }
00220       n ++;
00221     }
00222   }
00223   printf("\nend of file after %d bytes %d non-zero\n\n", n, i);
00224 
00225   r = close_file(fp, FileName);
00226 
00227   return r;
00228 }
00229 
00230 void test (void)
00231 {
00232   char line[MAXLINE] = "HELP";
00233   int r ;
00234 
00235   timKeyword("REST"); /* clear buffer */
00236 
00237   tim->reset();
00238   tim->initialize();
00239 
00240   for ( ; ; )
00241   {
00242     r = ask_line ("Enter keyword [value] (eg MENU)", line, line);
00243     timKeyword(line);
00244   }
00245 }
00246 
00247 void timKeyword(const char *String)
00248 {
00249   static unsigned short buffer[TIM_SEQ_SIZE];
00250   /*
00251   enum keywords
00252    {
00253       HELP , MENU , A24  , A32  , FILE , MODE , L1Ac , ECRc , BCRc , CALc ,
00254       DUMP , FILL , LOAD , PEEK , POKE , READ , RUN  , SCAN , SIZE , DAMO ,
00255       REST
00256    };
00257   */
00258   typedef enum
00259    {
00260       HELP , MENU , A24  , A32  , FILE , MODE , L1Ac , ECRc , BCRc , CALc ,
00261       DUMP , FILL , LOAD , PEEK , POKE , READ , RUN  , SCAN , SIZE , DAMO ,
00262       REST
00263    } keywords;
00264   static const int Command[] =
00265    {
00266       HELP , MENU , A24  , A32  , FILE , MODE , L1Ac , ECRc , BCRc , CALc ,
00267       DUMP , FILL , LOAD , PEEK , POKE , READ , RUN  , SCAN , SIZE , DAMO ,
00268       REST
00269    };
00270   static const char * Name[] =
00271    {
00272      "HELP","MENU","A24" ,"A32" ,"FILE","MODE","L1A" ,"ECR" ,"BCR" ,"CAL" ,
00273      "DUMP","FILL","LOAD","PEEK","POKE","READ","RUN" ,"SCAN","SIZE","DAMO",
00274      "REST"
00275    };
00276   static const char * Help =
00277    {
00278      "To run TIM sequencer, prepare program's buffer, then LOAD and RUN.\n"
00279      "Numbers are hex by default.\n"
00280      "HELP: description of commands\n"
00281      "MENU: list of commands\n"
00282      "REST: reset buffer, L1ID, BCID, MODE, SIZE\n"
00283      "A24  [addr ]: open VME A24 base address\n"
00284      "A32  [addr ]: open VME A32 base address\n"
00285      "DAMO [name ]: read Damon's binary file into buffer\n"
00286      "FILE [name ]: execute file of these commands\n"
00287      "L1A  [index]: add L1A in buffer[index]\n"
00288      "ECR  [index]: add ECR in buffer[index]\n"
00289      "BCR  [index]: add BCR in buffer[index]\n"
00290      "CAL  [index]: add CAL in buffer[index] (without L1A)\n"
00291      "DUMP [byte ]: dump TIM info to screen (byte 0 = source, 1 = sink)\n"
00292      "FILL [value]: fill buffer with value\n"
00293      "LOAD        : load TIM sequencer with buffer\n"
00294      "MODE        : toggle mode stand-alone/TTC\n"
00295      "RUN         : run  TIM sequencer\n"
00296      "SCAN        : scan buffer (in program's memory)\n"
00297      "SIZE        : set  buffer and TIM sequencer size (16-bit words)\n"
00298      "PEEK [addr ]      : read  data at byte address\n"
00299      "POKE [addr ][data]: write data at byte address\n"
00300      "READ [addr ][size]: read  data at byte address for size (16-bit words)\n"
00301    };
00302 /* warning: string length `1037' is greater than the minimum length `509'
00303  *  ISO C89 is required to support
00304  */
00305   static      char fileName[MAXLINE] = "../RodCrate/timky.dat"; /* default */
00306   static      char damoName[MAXLINE] = "../source/4SctEvts.timData";
00307   static const int SizeCmd = sizeof(Command) / sizeof(int);
00308   static       int   size     = TIM_SEQ_SIZE;
00309   static       int   runMode  = 0; /* default is VME mode */
00310   int nval, index, i, r;
00311   unsigned int addr;
00312   char token[MAXLINE] = "";
00313   int value = NONE;
00314   int extra = NONE;
00315   int key = NONE;
00316 
00317   nval = sscanf(String, "%s %i %i", token, &value, &extra);
00318 
00319   for (i = 0 ; (token[i] = toupper((int)token[i])) != '\0' ; i ++)
00320       ;
00321 
00322   if (nval < 2 && value != NONE) printf ("Oops\n");
00323   if (nval < 3 && extra != NONE) printf ("Oops\n");
00324 
00325   for (i = 0 ; i < SizeCmd ; i ++)
00326       if (strcmp(token,Name[i]) == 0) key = Command[i];
00327 
00328   keywords keyword = (keywords) key;
00329 
00330   switch (keyword)
00331   {
00332     case DAMO:
00333       nval = sscanf(String, "%s %s", token, damoName);
00334       if (nval != 2) r = ask_line("Enter file name", damoName, damoName);
00335       break;
00336     case FILE:
00337       nval = sscanf(String, "%s %s", token, fileName);
00338       if (nval != 2) r = ask_line("Enter file name", fileName, fileName);
00339       break;
00340     case A24 :
00341     case A32 :
00342       if (value == NONE) r = ask_int ("Enter addr ", 0, &value);
00343       break;
00344     case READ:
00345       if (extra == NONE) r = ask_int ("Enter words", 0, &extra);
00346     case POKE:
00347       if (extra == NONE) r = ask_int ("Enter value", 0, &extra);
00348     case PEEK:
00349       if (value == NONE) r = ask_int ("Enter addr ", 0, &value);
00350       if (value < (int)mapSize) /* cast ***/
00351           addr = value;
00352       else
00353       { printf("ERROR: addr %04x out of range\n", value);
00354         addr = 0;
00355       }
00356       break;
00357     case ECRc:
00358     case BCRc:
00359     case CALc:
00360     case L1Ac:
00361       if (value == NONE) r = ask_int ("Enter index", 0, &value);
00362       if (value < size)
00363           index = value;
00364       else
00365       { printf("ERROR: index %04x >= size %04x\n", value, size);
00366         index = size - 1;
00367       }
00368       break;
00369     case DUMP:
00370       if (value == NONE) r = ask_int ("Enter byte ", 1, &value);
00371       break;
00372     case FILL:
00373       if (value == NONE) r = ask_int ("Enter value", 0, &value);
00374       break;
00375     case SIZE:
00376       if (value == NONE) r = ask_int("Enter sequencer RAM size", size, &value);
00377       if (value <= TIM_SEQ_SIZE)
00378           size = value;
00379       else
00380       { printf("ERROR: size %04x out of range\n", value);
00381         size = TIM_SEQ_SIZE;
00382       }
00383       break;
00384     default :
00385       break;
00386   }
00387   /*
00388   printf(" cmd: token %s value %i %04x extra %i %04x key %i\n",
00389                 token,   value, value, extra, extra, key);
00390   */
00391 /* NB when should l1id be reset? ***/
00392 
00393   switch (keyword)
00394   {
00395     case REST:
00396       seq->reset();
00397       tim->reset();
00398       tim->initialize();
00399 
00400       runMode  = 0;
00401       size = TIM_SEQ_SIZE;
00402       for (i = 0 ; i < TIM_SEQ_SIZE ; i ++)
00403           buffer[i] = 0;
00404       break;
00405     case DAMO:
00406       r = damon(damoName, buffer);
00407       break;
00408     case FILE:
00409       r = winky(fileName);
00410       break;
00411     case A24 :
00412       /* what if r = 1 ? ***/
00413       r = open_window(value, DIPSY_A24);
00414       break;
00415     case A32 :
00416       r = open_window(value, DIPSY_A32);
00417       break;
00418     case MODE:
00419       if (runMode)
00420       {   runMode = 0;
00421           //***          timSetupVME();
00422           printf("Setup VME mode\n");
00423       }
00424       else
00425       {   runMode = 1;
00426           //***          timSetupRun();
00427           printf("Setup Run mode\n");
00428       }
00429       break;
00430     case READ:
00431       /* check for extra overflow ***/
00432       for (i = 0 ; i < extra ; i ++)
00433           printf("%04X%c",tim->vmeFetch( i*2+addr ), (i%16==15) ? '\n' : ' ');
00434       printf ("\n");
00435       break;
00436     case POKE:
00437       tim->vmeLoad( addr, extra );
00438       break;
00439     case PEEK:
00440       printf("addr %04x data %04X\n", addr, tim->vmeFetch( addr ));
00441       break;
00442     case ECRc:
00443       seq->addByIndex(TIM_ECR, index);
00444       break;
00445     case BCRc:
00446       seq->addByIndex(TIM_BCR, index);
00447       break;
00448     case CALc:
00449       seq->addByIndex(TIM_CAL, index);
00450       break;
00451     case L1Ac:
00452       seq->addByIndex(TIM_L1A, index);
00453       break;
00454     case DUMP:
00455       tim->status();
00456       tim->seqFetch( size, buffer );
00457       seq->scanRun();
00458 
00459 //*** timDump(size, value); /* 0 = source, 1 = sink */
00460       break;
00461     case FILL:
00462       seq->fill( size, value );
00463       break;
00464     case LOAD:
00465       /* timWait_seqBusys(); think ***/
00466 
00467       seq->getBuffer( size, buffer );
00468       tim->seqLoad(   size, buffer );
00469       break;
00470     case RUN :
00471       tim->seqRun( size ); /* end = 0x4000 sees 0 */
00472       break;
00473     case SCAN:
00474       seq->scanRun();
00475       break;
00476     case SIZE:
00477       break;
00478     case MENU:
00479       for (i = 0 ; i < SizeCmd ; i ++)
00480           printf("%4s%c", Name[i], (i%16==15) ? '\n' : ' ');
00481       printf ("\n");
00482       break;
00483     case HELP:
00484       printf("\n%s\n", Help);
00485       break;
00486     case NONE:
00487       printf ("NONE\n");
00488       break;
00489     default :
00490       printf("\n Trouble at Mill \n");
00491       break;
00492   }
00493 }

Generated on Sun Jun 27 19:57:31 2004 for TimModule by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002