inp003.c 5.07 KB
/*
 *  inp002.c: Random test case
 */

#define NUM_RUNS	(1000)

#define MAX_DX	(5.0)
#define BIAS_L	(MAX_DX+2)
#define BIAS_R	(BIAS_L+2)

#include <stdio.h>
#include "sigs.h"

char *strcpy (char *s1, const char *s2);

int  gen_test_case( void );
void send_params( void );
void send_pixels( int );
void dump_test( void );

int time( int );
int srandom( int );
int random( void );

#define RXX(x,y) ((x)+(random()%((y)-(x)+1)))
#define RX2x(x) (RX(-(x),(x)))
#define RX2(x)  (random()&(x))
#define R2(x)   (random()&((x)-1))

/*
 *   Generate test vectors 
 */
main(int argc, char **argv)
{
  int i;
  int n;
  int rseed = 0x12345678;
  
  if( argc > 1 )
    rseed = time( 0 );

  srandom(rseed);

  x_major = 0x00;
  cycle_type = 0;
  left = 1;

  print_input_header();
 
  printf("# Seed = %d\n", rseed );
  printf("# reset 6 clocks\n"); 
  reset = 0;
  for(i = 0; i < 6; i++)
    print_input_sigs();

  reset = 1;

  for(i=0; i<NUM_RUNS; i++) {

      cycle_type = R2(2);
      left = R2(2);

      n = gen_test_case();

      send_params();

      send_pixels( n );
  };
}

/*

    Generate Random Spans
*/
double xleft[4],
       xright[4];

int xval;
int xmin,
    xmax;

void gen_xs( double *, double, double );

int
gen_test_case( void )
{
    int i;
    int shft;

    shft = RXX(-6,1029);

    gen_xs( xleft,  MAX_DX, shft );
    gen_xs( xright, MAX_DX, shft+1 );

    xval = 0;
    xmin = xleft[0];
    xmax = xright[0];
    for(i=0; i<4; i++) {

	if(xleft[i] < xright[i])
	    xval |= (1<<i);
	
	if( xleft[i] < xmin )
	    xmin = xleft[i];
    
	if( xright[i] < xmin )
	    xmin = xright[i];
    
	if( xleft[i] > xmax )
	    xmax = xleft[i];
    
	if( xright[i] > xmax )
	    xmax = xright[i];

	if( xleft[i]  < 0 )
	    xleft[i] = 0;
	else if( xleft[i]  > 1023.9 )
	    xleft[i] = 1023.9;

	if( xright[i]  < 0 )
	    xright[i] = 0;
	else if( xright[i]  > 1023.9 )
	    xright[i] = 1023.9;
    };
    
    return( BIAS_R+MAX_DX+1 );
}

double frand( void );
#include <values.h>

void
gen_xs( double *xlist, double dx, double bias )
{
    int iy;
    double cx, cy;
    double dxt, dxb;
    double x,y;

    cx = frand();
    cy = frand();

    dxt = (2*dx*frand()) - dx;
    dxb = (2*dx*frand()) - dx;

    for(iy=0; iy<4; iy++) {
	y = iy * 0.25;

	if( y < cy )	/* Top */
	    x = (y-cy)*dxt;
	else
	    x = (y-cy)*dxb;
	
	xlist[iy] = x + cx + bias;
    };
}

double
frand( void )
{
    return( random()/(MAXINT * 1.0) );
}

/*

    Send Span Parameters
*/

#define XM(x)	(end[i]*8)
#define XH(x)	(start[i]*8)

void
send_params( void )
{
  int i;
  double *start, *end;

  dump_test();

  if( left ) {
    start = xleft;
    end   = xright;
  } else {
    start = xright;
    end   = xleft;
  };

  for(i=0; i<4; i++) {
      printf("# xh%d (%lg)\n", i, end[i]); 
      ew_cv_data = XH(i);
      print_input_sigs();

      printf("# xm%d (%lg)\n", i, start[i]); 
      ew_cv_data = XM(i);
      print_input_sigs();
  };

  printf("# xval, latch xm's, xh's\n"); 
  ew_cv_data = xval;
  ew_cv_newspan = 1; 
  if( left )
      x_major = xmin-1;
  else
      x_major = xmax+1;
  print_input_sigs();

  ew_cv_newspan = 0; 
  if( cycle_type )
      print_input_sigs();

  dv = 1; /* one clock after new span, data should be valid */
}

void 
send_pixels( int n )
{
    int i;

    printf("# Range xmin = %d, xmax = %d\n", xmin, xmax );
    if( left )
	for(i=xmin-1; i<=xmax+1; i++) {
	    printf("#pixel x = %d\n", i );
	    x_major = i;
	    print_input_sigs();
	    if( cycle_type )
		print_input_sigs();
	}
    else
	for(i=xmax+1; i>=xmin-1; i--) {
	    printf("#pixel x = %d\n", i );
	    x_major = i;
	    print_input_sigs();
	    if( cycle_type )
		print_input_sigs();
	}
}
/*

	General Support Routines
*/

/*
 *  Prints header info for input signals, same for all dumps
 */
static void
  print_input_header( void )
{
  printf("gclk @C 1(8) 0(8)\n");
  printf("ew_cv_data[12:0] @I @E 0\n");
  printf("x_major[11:0] @I @E 0\n");
  printf("cycle_type @I @E 0\n");
  printf("ew_cv_newspan @I @E 0\n");
  printf("left @I @E 0\n");
  printf("reset @I @E 0\n");
  printf("dv @I @E 0\n");
  printf("\n");
}


/*
 *  Prints signal values info for input signals, same for all dumps
 */
static void
  print_input_sigs( void )
{
  printf("0x%.4x 0x%0.3x %d %d %d %d %d\n",
    ew_cv_data,
    x_major & 0xfff,
    cycle_type,
    ew_cv_newspan,
    left,
    reset,
    dv);
}

char *top    = "  %4d  ";

char *row[5] = { "* - * - ",
		 "| * . * ",
		 "* . * . ",
		 "| * . * ",
		 "+ - - - "
		};

char *lastc = "+|||+";

double floor (double x);

void
dump_test( void )
{
    int i;
    int x,y;
    char tmp[9];

    printf("# ");
    for(x=xmin; x<=xmax; x++) {
	printf(top,x);
    };
    printf("\n");

    for(y=0; y<4; y++) {
	printf("# ");
	for(x=xmin; x<=xmax; x++) {
	    strcpy( tmp, row[y] );

	    i = floor( 8*(xleft[y] - x) );
	    if( (i>=0) && (i<8) )
		tmp[i] = 'L';

	    i = floor( 8*(xright[y] - x) );
	    if( (i>=0) && (i<8) ) {
		if( tmp[i] == 'L' )
		    tmp[i] = 'X';
		else
		    tmp[i] = 'R';
	    };
	    printf(tmp);
	};
	printf("%c\n", lastc[y] );
    };

    printf("# ");
    for(x=xmin; x<=xmax; x++) {
	printf(row[4]);
    };
    printf("%c\n", lastc[4]);
}