driver.c 11.8 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
/*
 *  Driver.c, reads RDP command input files and places vector values
 *  on wires each clock.
 *
 *  Ignores arguments for formatted dumps.
 *
 *
 *  Usage:  cs_test -i <filename> -o <dumpname> -t test_number
 *
 *  The -i flag wants no suffix on the filename and expects the
 *  actual suffix to be ".tab".  It also expects the file to be
 *  in ./InData.  The -o flag expects the name of a previously 
 *  named dump.
 */

#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#include "driver.h"
#include "csclk.h"
#include "csgclk.h"
#include "ck.h"


#define OPTARG          "o:d:f:i:t:s:x:y:h"


#define POSEDGE 	(save_clk && !save_clk_old)
#define NEGEDGE 	(!save_clk && save_clk_old)
#define DMA_LENGTH	9

/*
 *  Globals
 */

static char *MyOpts[] = { "all" };
static enum  MYOPTS     {  CS_ALL = 0 };
static int DumpFunctions = 0;

static FILE *InFile, *OutFile;

/* not currently used, only one file per run */
extern int Fargc;     /* number of file numbers */
extern char **Fargv;  /* pointer to file numbers from expanded range string */
static int CurFile = 0;  /* index to current file number */
static int LineNum = 0;

/*
 *  Forward References 
 */
void range_expand(char *);
static void print_cs_all(csclk_t *cp0, csclk_t *cp1, 
			csgclk_t *p0, csgclk_t *p1,
			driver_t *d0, driver_t *d1);

/*
 *  formatted_dump(): called by simulation executive after all other modules.
 *  end of arguments list denoted by a zero arg.  Must do this way because
 *  order and number of arguments is not guaranteed, and user may want one
 *  tab file from multiple sources.
 */
void
  formatted_dump( void *np, ... )
{
  static int save_clk = 0;
  va_list ap;
  void *ptr;
  int ptr_no = 0;
  driver_t *drv_p0, *drv_p1;
  csclk_t *csclk_p0, *csclk_p1;
  csgclk_t *csgclk_p0, *csgclk_p1;
  

  ptr = (void *) va_start(ap, np);
  if(ptr)
  do
  {
    if(!strcmp(((driver_t *)ptr)->label, "DRIVER"))
      drv_p0 = (driver_t *)ptr;
    else if(!strcmp(((driver_t *)ptr)->label, "DRIVER_P1"))
      drv_p1 = (driver_t *)ptr;
    else if(!strcmp(((driver_t *)ptr)->label, "CSCLK"))
      csclk_p0 = (csclk_t *)ptr;
    else if(!strcmp(((driver_t *)ptr)->label, "CSCLK_P1"))
      csclk_p1 = (csclk_t *)ptr;
    else if(!strcmp(((driver_t *)ptr)->label, "CSGCLK"))
      csgclk_p0 = (csgclk_t *)ptr;
    else if(!strcmp(((driver_t *)ptr)->label, "CSGCLK_P1"))
      csgclk_p1 = (csgclk_t *)ptr;
  } while(ptr = (void *)va_arg(ap, void *));
  va_end(ap);

  /* negedge */
  if(save_clk && !drv_p0->clk_old)
  {
    if(DumpFunctions & (1 << CS_ALL))
      print_cs_all(csclk_p0, csclk_p1, csgclk_p0, csgclk_p1, drv_p0, drv_p1);
  }

  /* this is a conspiracy between the drive function and the dump
     function so that we can detect the positive edge of the clock */
  save_clk = drv_p0->clk_old;
}
 
static void
  print_cs_all_header(void)
{
  int i;

  fprintf(OutFile, "#\n");
  fprintf(OutFile, "#  command shuffle verification\n");
  fprintf(OutFile, "#\n");
  fprintf(OutFile, "clk                     @C 1(8) 0(8)\n");
  fprintf(OutFile, "gclk                    @C 1(8) 0(8)\n");

  /* inputs */
  fprintf(OutFile, "xbus_cs_data[63:0]      @I @E 2\n");
  fprintf(OutFile, "xbus_cs_valid           @I @E 2\n");
  fprintf(OutFile, "ew_cs_busy              @I @E 2\n");
  fprintf(OutFile, "ms_busy                 @I @E 2\n");
  fprintf(OutFile, "rel_sync_tile           @I @E 2\n");
  fprintf(OutFile, "rel_sync_pipe           @I @E 2\n");
  fprintf(OutFile, "rel_sync_full           @I @E 2\n");
  fprintf(OutFile, "rel_sync_load           @I @E 2\n");
  fprintf(OutFile, "texel_size[1:0]         @I @E 2\n");
  fprintf(OutFile, "copy_fill               @I @E 2\n");
  fprintf(OutFile, "reset_l                 @I @E 2\n");

  /* internal registers */
  fprintf(OutFile, "w_addr_in[5:0]          @O @S 15 @V dv_1\n");
  fprintf(OutFile, "read_adrs[5:0]          @O @S 15 @V dv_1\n");
  fprintf(OutFile, "shf_state[4:0]          @O @S 15 @V dv_1\n");
  fprintf(OutFile, "sync_tile_state         @O @S 15 @V dv_1\n");
  fprintf(OutFile, "sync_pipe_state         @O @S 15 @V dv_1\n");
  fprintf(OutFile, "sync_full_state         @O @S 15 @V dv_1\n");
  fprintf(OutFile, "sync_load_state         @O @S 15 @V dv_1\n");

  /* asynchronous outputs */
  fprintf(OutFile, "tile_addr[2:0]          @O @S 15 @V dv_1\n");
  fprintf(OutFile, "cs_tc_data[47:0]        @O @S 15 @V dv_1\n");
  fprintf(OutFile, "we_tile_size            @O @S 15 @V dv_1\n");
  fprintf(OutFile, "we_tile_attr            @O @S 15 @V dv_1\n");
  fprintf(OutFile, "cs_ew_data[63:0]        @O @S 15 @V dv_1\n");
  fprintf(OutFile, "cs_ew_newprim           @O @S 15 @V dv_1\n");

  /* synchronous outputs */
  fprintf(OutFile, "cs_xbus_req             @O @S 15 @V dv_2\n");
  fprintf(OutFile, "cmd[5:0]                @O @S 15 @V dv_1\n");
  fprintf(OutFile, "start_prim              @O @S 15 @V dv_1\n");
  fprintf(OutFile, "attr_valid              @O @S 15 @V dv_1\n");
  fprintf(OutFile, "cmd_busy                @O @S 15 @V dv_1\n");

  /* data valids */
  for (i = 0; i < DELAY_TWO+1; i++)
    fprintf(OutFile, "dv_%d                    @V\n", i);

  fprintf(OutFile, "false                   @V\n");

  /* required by format */
  fprintf(OutFile, "\n");
}

static void
  print_cs_all(csclk_t *cp0, csclk_t *cp1,
               csgclk_t *p0, csgclk_t *p1,
               driver_t *d0, driver_t *d1)
{
  int i;

  /* inputs */
  fprintf(OutFile, "0x%08x%08x %d %d %d %d %d %d %d 0x%x %d %d   ",
	  cp1->xbus_cs_data.word1, cp1->xbus_cs_data.word0,
	  cp1->xbus_cs_valid,
	  p1->ew_cs_busy,
	  p1->ms_busy,
	  p1->rel_sync_tile,
	  p1->rel_sync_pipe,
	  p1->rel_sync_full,
	  p1->rel_sync_load,
	  p1->texel_size,
	  p1->copy_fill,
	  cp1->reset_l);

  /* internal registers */
  fprintf(OutFile, "0x%02x 0x%02x 0x%02x %d %d %d %d        ",
	  cp1->wr_adrs,
	  p1->read_adrs,
	  p1->shf_state,
	  p1->sync_tile_state,
	  p1->sync_pipe_state,
	  p1->sync_full_state,
	  p1->sync_load_state);

  /* asynchronous outputs */
  fprintf(OutFile, "0x%x 0x%04x%08x %d %d 0x%08x%08x %d   ",
	  p0->tile_addr,
	  p0->cs_tc_data.word1, p0->cs_tc_data.word0,
	  p0->we_tile_size,
	  p0->we_tile_attr,
	  p0->cs_ew_data.word1, p0->cs_ew_data.word0,
	  p0->cs_ew_newprim);

  /* synchronous outputs */
 fprintf(OutFile, "%d 0x%02x %d %d %d  ",
	  cp1->cs_xbus_req,
	  p1->cmd,
	  p1->start_prim,
	  p1->attr_valid,
	  p1->cmd_busy);

  /* valid signals */
  for (i = 0; i < DELAY_TWO; i++)
    fprintf(OutFile, "%d ",	d1->dv_delay[i]);
  fprintf(OutFile, "%d     ",	d1->dv);

  /* False signal */

  fprintf(OutFile, "%d", 0);

  fprintf(OutFile, "\n");
}

/*
 *  open_file()
 *
 *
 */
static FILE *
  open_file(char *fn)
{
  FILE *fp;
  char fname[50];
  char line[1024];

  if(!fn)
  {
    fprintf(stderr,"Error, null pointer passed to open_file()\n");
    exit(1);
  }
  else
  {
    sprintf(fname,"InData/inp%s.tab\0", fn);
    fprintf(stderr,"reading %s\n", fname);
    if((fp = fopen(fname,"rb")) == NULL)
    {
      fprintf(stderr,"unable to open %s\n", fname);
      exit(1);
    }
  }

  /* skip header */
  while(fgets(line, 1023, fp))
  {
    if(line[0] == '\n')
      break;
  }
  LineNum = 0;
  return(fp);
}


static void
  read_line(char *linep)
{
  /* read line */
  if(!(fgets(linep, 1023, InFile)))
  {
    printf("Done processing\n");
    exit(0);
  }
  LineNum++;
  
  while(linep[0] == '#')
  {
    if(DumpFunctions != 0)
      fprintf(OutFile, "%s", linep);
    if(!(fgets(linep, 1023, InFile)))
    {
      printf("Done processing\n");
      exit(0);
    }
    LineNum++;
  }
}

static void
  scan_line(char *linep, driver_t *p0)
{
  int ret;

  ret = sscanf(linep, "%i%i%i%i%i%i%i%i%i%i%i%i%i",
		     &p0->xbus_cs_data.word1, &p0->xbus_cs_data.word0,
		     &p0->xbus_cs_valid,
		     &p0->ew_cs_busy,
		     &p0->ms_busy,
		     &p0->rel_sync_tile,
		     &p0->rel_sync_pipe,
		     &p0->rel_sync_full,
		     &p0->rel_sync_load,
		     &p0->texel_size,
		     &p0->copy_fill,
		     &p0->reset_l,
		     &p0->dv_delay[0]);
  if(ret != 13)
  {
    fprintf(stderr,"Error, short read line %d: %s\n", LineNum, linep);
    exit(1);
  }
}

/*
 *  driver()
 */

void
  driver(driver_t **pp0, driver_t **pp1)
{
  driver_t *p0, *p1;
  int save_clk;
  int save_clk_old;
  int do_defaults;
  static int saved_line = 0;
  static char line[1024];
  int i;

  /* get initial pointers */
  p0 = *pp0;
  p1 = *pp1;
  save_clk = p0->clk;
  save_clk_old = p1->clk_old;


  if(POSEDGE)
  {
    /* transfer all next-clock register values to register outputs. */
    *pp0 = p1; /* swap */
    *pp1 = p0;
    p0 = *pp0; /* fix pointers */
    p1 = *pp1;


    /* Update all next clock register values */
    
    if (p1->reset_l) {
      switch(p1->state)
      {
	case WAIT:
	  if(p1->request) {
	    p0->state = OUTPUT_WORD;
	    p0->count = DMA_LENGTH;
	  }
	  else
	    p0->state = WAIT;
	  break;

	case OUTPUT_WORD:
	  if(p1->count == 0)
	    p0->state = DELAY1;
	  else
	    p0->state = OUTPUT_WORD;
	  break;

	case DELAY1:
	  p0->state = DELAY2;
	  break;

	case DELAY2:
	  p0->state = WAIT;
	  break;

	default:
	  printf("Invalid DMA state\n");
	  p0->state = WAIT;
	  break;
      }
    }
    else
    {
      p0->state = OUTPUT_WORD;
      p0->count = DMA_LENGTH;
    }

    if(p1->state == OUTPUT_WORD)
    {
      if (saved_line)
	saved_line = 0;
      else
	read_line(line);
      scan_line(line, p0);

      if (p0->xbus_cs_valid)
     	 p0->count = p1->count - 1;
      else
     	 p0->count = p1->count;

    }
    else	/* p1->state != OUTPUT_WORD */
    {
      if (saved_line)
	saved_line = 0;
      else 
	read_line(line);
      scan_line(line, p0);

      if (p0->xbus_cs_valid) {
	do_defaults = 1;
	saved_line = 1;
      }
      else
      {
	do_defaults = 0;
      }
      if (do_defaults)
      {
	p0->xbus_cs_data.word0 = 0;
	p0->xbus_cs_data.word1 = 0;
	p0->xbus_cs_valid = 0;
	p0->ew_cs_busy = 0;
	p0->rel_sync_tile = 0;
	p0->rel_sync_pipe = 0;
	p0->rel_sync_full = 0;
	p0->rel_sync_load = 0;
	p0->texel_size = 0;
	p0->reset_l = 1;
	p0->dv_delay[0] = 1;
      }
    }

    for (i = 1; i < DELAY_TWO; i++)
      p0->dv_delay[i] = p1->dv_delay[i-1];

    p0->dv = p1->dv_delay[DELAY_TWO-1];
  }

  /* keep last clock state, also used by dump function */
  p0->clk_old = p1->clk_old = save_clk;

}   


/*
 *   driver_init()
 */

void
  driver_init(driver_t *p0, driver_t *p1)
{
  int c, i;
  char *infile;
  char *options, *value;
  extern char *optarg;
  extern int optind, opterr, optopt;
  extern int Fargc;
  extern char **Fargv;
  char dumpFileName[_POSIX_PATH_MAX];
  int testNumber;

  optind = 1;
  opterr = 0;

  p0->clk = p1->clk = 0;
  p0->clk_old = p1->clk_old = 0;
  p0->state = p1->state = WAIT;

  while((c = getopt(p1->argc, p1->argv, "i:o:t:h")) != EOF)
  {
     switch(c)
     {
       case 'i':
	  range_expand(optarg);
          break;

       case 'h': fprintf(stderr,"Usage for driver:\n");
		fprintf(stderr," -i <range expression>\n");
	        return;

       case 'o':
       options = optarg;
       while(*options != '\0')
       {
         switch(getsubopt(&options, MyOpts, &value))
         {
           case CS_ALL:
             DumpFunctions |= (1 << CS_ALL);
	     break;
           default:
             printf("Error, output dump %s not known \n", value);
             break;
         }
       }
       break;

       case 't':
    	  testNumber = atoi(optarg);
          break;

       default:
	  break;
     }
  }

  /* open first file */

  if(Fargc <= 0) {
    fprintf(stderr,"Error, no input files given\n");
    exit(1);
  }
  InFile = open_file(Fargv[CurFile++]);

  if(DumpFunctions != 0) {
    sprintf(dumpFileName, "OutData/test%03d.tab", testNumber);
    if ((OutFile = fopen(dumpFileName, "w")) == NULL) {
      fprintf(stderr, "cs_test: %s: cannot create (%s)\n",
	dumpFileName, strerror(errno));
      exit(1);
    }
    setbuf(OutFile, NULL);	/* JEFF */
    if(DumpFunctions & (1 << CS_ALL))
      print_cs_all_header();
  }
}