tst2c.c 5.09 KB

/*************************************************************************
 *
 *  File: tst2c.c
 *
 *  This file converts test file to actual C calling routines.
 *
 *  $Header: /root/leakn64/depot/rf/sw/n64os20l/iosim/src/tst2c.c,v 1.1.1.1 2002/05/30 05:41:20 whs Exp $
 *
 */
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>

#include "iomap.h"
#include "iotest.h"
#include "sim.h"
#include "simipc.h"


/***********************************************************************
 * Extern definitions
 */
extern	TstCmd	TstCmdTbl[];



/***********************************************************************
 * Macro definitions
 */
#define	PROC_INC1_MSG	"#include <stdio.h>\n#include \"trace.h\"\n"
#define	PROC_INC2_MSG	"#include \"iomap.h\"\n#include \"iotest.h\"\n"
#define	PROC_BEGIN_MSG	"\nvoid IoTestAll(void)\n{\n"
#define	PROC_END_MSG	"}\n"
#define	PROC_VAR_MSG	"\tint i, ret, errCount;\n\tint (*handler)(int, int, int, int);\n\n\ti = errCount = 0;\n"

#define	PROC1_MSG	"\ti++;\n\tret = %s(0x%x, 0x%x, 0x%x, 0x%x);\n"

#define	PROC2_MSG	"\tif (ret == -1) errCount++;\n"

#define	TRACE_RES_MSG	"\t_TRACE(DALL, fprintf(LogFp, \n\t\"\\n***** THE END (total test = %%d, error count = %%d) *****\\n\", \n\t\ti, errCount));\n\n"

#define TRACE_LOG_MSG	"\t_TRACE(DALL, fprintf(LogFp, \n\t\t\"%%3d. %s(0x%x, 0x%x, 0x%x, 0x%x) -> %%s\\n\\n\", \n\t\ti, (ret == -1) ? \"Failed\":\"Passed\"));\n\n"

#define	TEST_CMD_FILE	"all.tst"
#define	OPTARG		"f:o:h"


/***********************************************************************
 * Global definitions
 */
int		IpcFd = -1;
unsigned long	Dflags;
char		*CmdFile = TEST_CMD_FILE;
FILE		*LogFp, *CmdFp;
int		ConfigRdramV = 0;
int             NumberStallCycles = 0;


/***********************************************************************
 * Local static definitions
 */
static void		usage(void);
static void		convertTst2C(char *);
static void 		dumpCmdTbl(TstCmd *);


main(int argc, char **argv)
{

	int c, testId;
	char	*file;
	FILE	*fp = NULL;

	extern char *optarg;
	extern int optind, opterr, optopt;

	optind = 1;
	Dflags = 0;
	LogFp  = stderr;

	while ((c = getopt(argc, argv, OPTARG)) != EOF) {
		switch (c) {
			case 'f': {	/* Command file */
				CmdFile = optarg;
				printf("Command file = %s\n", CmdFile);
				break;
			}
			case 'o': {	/* Output log file */
				file = optarg;
				printf("Output file = %s\n", file);
				if ((fp = fopen(file, "w+")) == NULL) {
					printf("Unable to open log: %s\n",
						file);
				}
				else
					LogFp = fp;
				break;
			}
			case 'h': {	/* Help */
				usage();
				exit(0);
			}
			default:
				usage();
				exit(0);
		}
	}  /* while */


	convertTst2C(CmdFile);

	/*
	printf("TstCmdTbl = 0x%08x\n", TstCmdTbl);
	dumpCmdTbl(TstCmdTbl);
	*/

	if (fp)
		fclose(fp);

	exit(0);
}


static void
usage(void)
{
	printf("Usage: tst2c \n");
	printf("       -o <output file>\n");
	printf("       -f <test_file>\n");
}


/***********************************************************************
 *
 *	- Read each line from test command file
 *	- Execute test based on id
 */
void
dumpCmdTbl(TstCmd *CmdTbl)
{
	TstCmd *ct;
	int i;

	i = 0;
	printf("Command Table:\n");
	for (ct = CmdTbl, i = 0; ct->id != -1; ct++, i++) {
		printf("%3d. Id=%3d, Handler=0x%08x, Cmd=0x%08x\n", 
			i, ct->id, ct->handler, ct);
	}

}  /* dumpCmdTbl */


static TstCmd *
lookupCmd(TstCmd *CmdTbl, int id)
{
	TstCmd *ct;

	/* Search through TstCmdTable to see if id exists 
	 * If found, return pointer to TstCmd structure, otherwise return NULL
	 */
	for (ct = CmdTbl; ct->id != -1; ct++) {
		if (ct->id == id) 
			return(ct);
	}
	return(NULL);
}  /* lookupCmd */


/***********************************************************************
 */
void 
convertTst2C(char *testFile)
{


	FILE		*fp;
	char		line[BUF_SIZE], dumpFile[BUF_SIZE];
	int 		i, ret, tid;
	unsigned int	address, data[PKT_DATA_WORD];
	unsigned int	a1, a2, a3, a4;
	TstCmd		*cmd;

	fp = (FILE *)NULL;
	line[0] = '\0';
	dumpFile[0] = '\0';


	/* Load test file */
	if ((fp = fopen(testFile, "r")) == NULL) {
		fprintf(LogFp, "Unable to open %s\n", testFile);
		return;
	}
	
	/* Write out routine declaration */
	fprintf(LogFp, PROC_INC1_MSG);
	fprintf(LogFp, PROC_INC2_MSG);
	fprintf(LogFp, PROC_BEGIN_MSG);
	fprintf(LogFp, PROC_VAR_MSG);

	/* 
	 * line format (size= # of bytes)
	 * ===========
	 * t <id> <a1> <a2> ... <a4>	// execute test <id>
	 * q				// quit
	 *
	 */
	while (fgets(line, BUF_SIZE, fp)) {
		if (line[0] == 't') { 
			sscanf(line, "%*s %d %x %x %x %x", 
				&tid, &a1, &a2, &a3, &a4);

			printf("t %d %x %x %x %x\n", tid, a1, a2, a3, a4);

			if ((cmd = lookupCmd(TstCmdTbl, tid)) != NULL) {

				fprintf(LogFp, PROC1_MSG, cmd->name, 
					a1, a2, a3, a4);
				fprintf(LogFp, PROC2_MSG);

				fprintf(LogFp, TRACE_LOG_MSG, cmd->name, 
					a1, a2, a3, a4);

			}
			else {
				fprintf(stderr, "ERROR: illegal test cmd=%d\n", 
					tid);
			}
			continue;
		}
		else if (line[0] == 'q' || line[0] == 'Q') {
			break;
		}  /* else */
	}  /* while */
	fclose(fp);

	fprintf(LogFp, TRACE_RES_MSG);
	fprintf(LogFp, PROC_END_MSG);


}  /* convertTst2C */